Re: [Haskell] ANNNOUNCE: lens 3.7 released

2012-12-09 Thread Ashley Yakeley

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

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.


-- Ashley

___
Haskell mailing list
Haskell@haskell.org
http://www.haskell.org/mailman/listinfo/haskell


Re: [Haskell] ANNNOUNCE: lens 3.7 released

2012-12-09 Thread Edward Kmett
On Sun, Dec 9, 2012 at 10:14 PM, Ashley Yakeley ash...@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
___
Haskell mailing list
Haskell@haskell.org
http://www.haskell.org/mailman/listinfo/haskell


[Haskell] ANNNOUNCE: lens 3.7 released

2012-12-07 Thread Edward Kmett
Greetings!

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.

In its simplest form, a lens is a getter/setter pair, that can be composed
and reasoned about with common sense laws that you can also derive from the
Functor and Traversable laws you already know or even derive by reasoning
in terms of comonad coalgebras for the even more theoretically inclined.

The lens package provides lenses along with a surprisingly powerful set of
generalizations and specializations of this idea, in a manner that subsumes
the notion of a semantic editor combinator, allowing you to enrich them
with the ability to read back from the targets. In the form taken by the
lenses in this package, lenses are empowered to safely change the types of
the fields that they edit in a manner that not only can you still reason
about, but where the changes in types helps you to reason about what they
can or cannot do.

One of the major design goals of lens has been that you should be able to
pick up lens combinators and apply them meaningfully to a mishmash of
lenses, traversals, isomorphisms, getters, and setters, etc. even without
fully understanding all of the types. This encourages active exploration
and users who are pleasantly surprised rather than frustrated and angry. To
this end we've actively stocked the haddocks for the project with types it
may be easier to think about each combinator as having, which can serve as
training wheels that will help you make your way around.

Most interestingly from a package maintainer perspective, unlike previous
packages that provided lenses, it is possible to provide lenses (and
traversals) that are compatible with the lens library without incurring any
dependency for your package at all. For instance the simplest Traversal is
traverse from Data.Traversable. traverse . traverse is also a valid
Traversal, and it can be composed with other lenses and traversals without
any casting or coercion.

A large number of combinators are provided that automatically 'do the right
thing' when presented with the various generalizations and specializations
of the concept of a lens, e.g. when supplied a Traversal instead of a Lens,
a combinator that returned a result based on the target of the Lens, may
now return a monoidal summary of all of the results targered by the
Traversal.

Major Features:

* Lenses, getters, setters, isomorphisms, folds, prisms, monadic actions,
and indexed versions of these constructions that can all be composed with
(.) from the Prelude in a manner that they read quite naturally to an
imperative programmer who expects (.) to be field access and to compose in
the 'wrong' direction, while still retaining the ability to reason about
the resulting code.

* Type-safe zippers into arbitrary user data structures, where you can move
down into a lens or laterally through a traversal and can come back up,
following a breadcrumb trail in the type.

* Lens contains a generalized version of Neil Mitchell's uniplate in such a
way that the uniplate combinators themselves that many people already know
how to use can be used on an arbitrary traversal, and uniplate/biplate
simply act as a Traversal, and are often ~35% or more faster than the
original.

* Lens comes batteries included with classes and combinators for working
with many common libraries that fall within the Haskell Platform. No
dependencies are incurred that fall outside of the platform, unless those
dependencies are needed to implement lens itself.

* We provide configurable template-haskell generators for producing lenses,
isomorphisms and traversals for your own data types.

New in this release:

* Prisms are categorically dual to Lenses and provide a form of first class
pattern. They can be used directly as a Traversal. Many operations that
formerly required an isomorphism can be used directly on a Prism, and every
Isomorphism can be used as a Prism.

* With this latest release we've incorporated a large amount of community
feedback into the API design and have vastly expanded the documentation
with hundreds of additional examples and test cases.

* We've renamed a number of operations to reduce naming conflicts with
third party libraries to a minimum, and improve consistency. in particular
we no longer conflict with Control.Arrow.

* We've overhauled the zipper API to permit easier use of multiple
simultaneous zippers, and to make zipper movements more compositional.

Resources:

* Wiki http://github.com/ekmett/lens/wiki/: We have a
FAQhttps://github.com/ekmett/lens/wiki/FAQ,
which includes a number of links to source material, a quick
getting-startedhttps://github.com/ekmett/lens#examples guide,
a discussion of the Derivationhttps://github.com/ekmett/lens/wiki/Derivation,
and even a UML diagram https://github.com/ekmett/lens#field-guide distributed
among the other content of the wiki. If you find something missing or
otherwise