| Safe Haskell | Safe |
|---|---|
| Language | Haskell2010 |
Optic.Lens
Contents
Lens
type Lens s t a b = forall (f :: * -> *). Functor f => (a -> f b) -> s -> f t #
A Lens is actually a lens family as described in
http://comonad.com/reader/2012/mirrored-lenses/.
With great power comes great responsibility and a Lens is subject to the
three common sense Lens laws:
1) You get back what you put in:
viewl (setl v s) ≡ v
2) Putting back what you got doesn't change anything:
setl (viewl s) s ≡ s
3) Setting twice is the same as setting once:
setl v' (setl v s) ≡setl v' s
These laws are strong enough that the 4 type parameters of a Lens cannot
vary fully independently. For more on how they interact, read the "Why is
it a Lens Family?" section of
http://comonad.com/reader/2012/mirrored-lenses/.
There are some emergent properties of these laws:
1) must be injective for every set l ss This is a consequence of law #1
2) must be surjective, because of law #2, which indicates that it is possible to obtain any set lv from some s such that set s v = s
3) Given just the first two laws you can prove a weaker form of law #3 where the values v that you are setting match:
setl v (setl v s) ≡setl v s
Every Lens can be used directly as a Setter or Traversal.
You can also use a Lens for Getting as if it were a
Fold or Getter.
Since every Lens is a valid Traversal, the
Traversal laws are required of any Lens you create:
lpure≡purefmap(l f).l g ≡getCompose.l (Compose.fmapf.g)
typeLenss t a b = forall f.Functorf =>LensLikef s t a b