Sign up ×

Could someone explain functional lenses to me? It's a surprisingly difficult subject to google for and I haven't made any progress. All I know is that they provide similar get/set functionality than in OO.

share|improve this question
There is a nice introduction to lenses by Edward Kmett on YouTube. The examples are in Scala, but it shouldn't be too hard to follow. –  hammar Nov 29 '11 at 8:30
Yup, tried to watch those, but having enough time while I'm still alert, is not so easy :P –  Masse Nov 29 '11 at 13:02
I know lenses from Functional Programming with Bananas, Lenses, Envelopes, and Barbed Wire via Btw, epic paper title is epic. –  Jochen Ritzel Nov 29 '11 at 14:35
@Jochen: The lenses described there don't really have much in common with the lenses this question is about. –  sclv Nov 29 '11 at 17:50
Here is nice introduction using pictures: Lenses in Pictures. –  Debjit Mar 3 at 3:29

2 Answers 2

up vote 46 down vote accepted

A lens consists of two functions, a getter and a setter:

data Lens a b = Lens { getter :: a -> b, setter :: b -> a -> a }

For example, we might have lenses for the first and second parts of a pair:

fstLens :: Lens (a, b) a
fstLens = Lens fst $ \x (a, b) -> (x, b)

sndLens :: Lens (a, b) b
sndLens = Lens snd $ \x (a, b) -> (a, x)

The real convenience of lenses is that they compose:

compose :: Lens b c -> Lens a b -> Lens a c
compose f g = Lens (getter f . getter g) $
                   \c a -> setter g (setter f c (getter g a)) a

And they mechanically convert to State transitions:

lensGet :: MonadState s m => Lens s a -> m a
lensGet = gets . getter

lensSet :: MonadState s m => Lens s b -> b -> m ()
lensSet f = modify . setter f

lensMod :: MonadState s m => Lens s b -> (b -> b) -> m ()
lensMod f g = modify $ setter f =<< g . getter f

(+=) :: (MonadState s m, Num b) => Lens s b -> b -> m ()
f += x = lensMod f (+ x)
share|improve this answer
Your compose example didn't typecheck. GHC infers; Lens a a -> Lens a a -> Lens a a –  Masse Nov 29 '11 at 21:17
Masse: I accidentally flipped the f and g. –  Apocalisp Nov 29 '11 at 21:33
It still doesn't type check into a->c. It infers into compose :: Lens a b -> Lens a a -> Lens a b –  Masse Nov 30 '11 at 8:50

See the answer to question lenses, fclabels, data-accessor - which library for structure access and mutation is better - it has a very clear explanation on lenses.

Also, the documentation for the Data.Lenses and fclabel libraries give some good examples of them being used.

share|improve this answer

Your Answer


