Safe Haskell | None |
---|---|
Language | Haskell98 |
Basic lens type and functions.
The lens package should be a drop-in replacement for this.
Lens type
type Lens s t a b = forall f. Functor f => (a -> f b) -> s -> f t Source #
Purpose
A value of type Lens s t a b
provides the following:
- A reference into the structure
s
to read and update the valuea
inside it. - The possibility to change the type of
s
tot
and the type ofa
tob
.
The Functor
constraint
Operations may do something more interesting inside the f
functor. For the purpose of this module and package, all the
functions below (view
, over
, set
) use a no-op functor
and therefore the above type is equivalent to:
type Lens s t a b = (a -> b) -> (s -> t)
But it is left generic for forward compatibilty with the lens package.
Example
λ> data Person = Person { personChar :: Char, personAge :: Int } deriving Show λ> view $(field 'personChar) (Persona
10)a
λ> over $(field 'personAge) (*2) (Persona
10) Person {personChar =a
, personAge = 20} λ>
Laws
1) Get-Put: You get back what you put in.
view l (set l v s) ≡ v
2) Put-Get: Putting back what you got doesn't change anything.
set l (view l s) s ≡ s
3) Put-Put: Setting is idempotent.
set l v (set l v s) ≡ set l v s
Functions
set :: Lens s t a b -> b -> s -> t Source #
Set the a
inside the s
, optionally changing the types to b
and t
.