[Haskell] ANNNOUNCE: lens 3.7 released
Edward Kmett
ekmett at gmail.com
Mon Dec 10 07:07:36 CET 2012
On Sun, Dec 9, 2012 at 10:14 PM, Ashley Yakeley <ashley at semantic.org> wrote:
> On 07/12/12 02:19, Edward Kmett wrote:
>
> I am happy to announce the release of version 3.7 of the lens package,
>> which provides "Lenses, Folds, and Traversals" for working with
>> arbitrary data types.
>>
>
> Do you use types to index the fields of tuples? It's a good general
> mechanism to represent the "tupleness" of certain types. I had a quick look
> and it didn't seem you were doing this.
>
> For instance, consider this type:
>
> data P = MkP Int Bool Char
> There are classes for Field1..Field9 used for the combinators _1.._9.
If you wanted to make instances of them for your type. You could very well
do
instance Field1 P P Int Int where
...
instance Field3 P P Char Char where
_3 f (P a b c) = P a b <$> f c
This lets you use the positional field accessors for monomorphic or
polymorphic types, that may or may not allow field types to change.
If one wants to consider the three fields as separate items, one can
> construct a type that's an index to them:
> data PInd :: * -> * where
> PFirst :: PInd Int
> PSecond :: PInd Bool
> PThird :: PInd Char
> It's then straightforward to construct an isomorphism between P and
> "forall a. PInd a -> a". You can also use it to build lenses for the
> fields, etc.
>
Er, I rather misinterpreted above, (I'll keep it in this reply just in case
it is handy for someone else.)
We do have something rather more limited available, which is that
Control.Lens.Representable let you implement corepresentable endofunctors
in terms of their polymorphic lenses.
e.g.
import Control.Lens
data V2 a = V2 { __x, __y :: a }
makeLenses ''V2
instance Representable V2 where
tabulate f = V2 (f _x) (f _y)
then you can get a number of instances for free
instance Monad V2 where
return = pureRep
(>>=) = bindRep
instance Applicative V2 where
pure = pureRep
ap = apRep
instance Functor V2 where
fmap = fmapRep
etc...
However, the form of representation we use are lenses into the structure
instead of a GADT like index type. This currently limits us to handling
functors.
This is used to good effect in the 'linear' package, which uses
representable functors for all of its vector spaces. This works because, at
least classically, all vector spaces are free vector spaces, and those are
isomorphic to a function from a basis, so we can use a representable
functor with a representation equivalent to that basis as a way to memoize
the vector space.
Since it gets comparatively little use relative to the rest of the API and
now that the rest of lens has matured around it, it feels somewhat
different than the rest of lens, we may want to split this part off into a
separate package eventually.
That said, even if we were to add the extra GADT-like index guiding
tabulate, it could only do product-like constructions.
If you use IRC, the #haskell-lens channel on freenode would be a good place
to dive into this deeper.
-Edward
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/haskell/attachments/20121210/bec57e2a/attachment.htm>
More information about the Haskell
mailing list