Using lenses

Tyson Whitehead twhitehead
Thu Oct 3 16:21:20 UTC 2013

The thing that really clicked lenses for me was reading through some of Twan van Laarhoven's slides where he presented an isomorphic definition of lenses.  Googling reveals he actually has a very nice blog entry about this now.

To strip out the essential element.  A lens is

  -- Isomorphisms/bijections between type @a@ and @b@
  data Iso a b = Iso { fw :: a -> b, bw :: b -> a }

  -- Lenses with a data wrapper, in practice you might want to unpack the Iso type
  data Lens a b = forall r. Lens (Iso a (b,r))

That is, a lense is two functions

  1 - fw : data -> (element, residue data)  [ this takes apart the data ]

  2 - bw : (element, residue data) -> data  [ this puts it back together ]

such that they are an isomorphism (i.e., if you take it apart and then put it back together you get the same thing).

  fw . bw = bw . fw = id

This made sense to me pretty much as soon as I encountered it.  This was certainly not the case with the other.

Cheers!  -Tyson

PS:  I would really encourage looking at the blog post.  It is a relatively easy read and well worth it.

More information about the Libraries mailing list