Safe Haskell | None |
---|---|
Language | Haskell2010 |
Lens
is a generalised or first-class *field*.
If we have a value s :: S
, and a l ::
,
we can get the "field value" of type Lens'
S AA
using view
.
We can also update (or put or set) the value using over
(or set
).
>>>
data Human = Human { _name :: String, _location :: String } deriving Show
>>>
let human = Human "Bob" "London"
We can make a Lens
for _name
field:
>>>
let name = lens _name $ \s x -> s { _name = x }
Which we can use as a Getter
>>>
view name human
"Bob"
or a Setter
>>>
set name "Robert" human
Human {_name = "Robert", _location = "London"}
Synopsis
- data A_Lens :: OpticKind
- type Lens s t a b = Optic A_Lens '[] s t a b
- type Lens' s a = Optic' A_Lens '[] s a
- lens :: (s -> a) -> (s -> b -> t) -> Lens s t a b
- withLens :: Is k A_Lens => Optic k is s t a b -> ((s -> a) -> (s -> b -> t) -> r) -> r
- toLens :: Is k A_Lens => Optic k is s t a b -> Optic A_Lens is s t a b
- type LensVL s t a b = forall f. Functor f => (a -> f b) -> s -> f t
- type LensVL' s a = LensVL s s a a
- lensVL :: LensVL s t a b -> Lens s t a b
- toLensVL :: Is k A_Lens => Optic k is s t a b -> LensVL s t a b
- withLensVL :: Is k A_Lens => Optic k is s t a b -> (LensVL s t a b -> r) -> r
- module Optics.Optic
Formation
Tag for a lens.
Instances
Introduction
withLens :: Is k A_Lens => Optic k is s t a b -> ((s -> a) -> (s -> b -> t) -> r) -> r #
Work with a lens as a getter and a setter.
toLens :: Is k A_Lens => Optic k is s t a b -> Optic A_Lens is s t a b #
Explicitly cast an optic to a lens.
Elimination
Lens
is a Setter
and a Getter
, therefore you can
view1
::Lens
i s t a b -> s -> aset
::Lens
i s t a b -> b -> s -> tover
::Lens
i s t a b -> (a -> b) -> s -> t
Computation
Well-formedness
GetPut: You get back what you put in:
view l (set l v s) = v
PutGet: Putting back what you got doesn’t change anything:
set l (view l s) s = s
PutPut: Setting twice is the same as setting once:
set l v' (set l v s) = set l v' s
van Laarhoven encoding
type LensVL s t a b = forall f. Functor f => (a -> f b) -> s -> f t #
Type synonym for a type-modifying van Laarhoven lens.
toLensVL :: Is k A_Lens => Optic k is s t a b -> LensVL s t a b #
Convert a lens to the van Laarhoven representation.
withLensVL :: Is k A_Lens => Optic k is s t a b -> (LensVL s t a b -> r) -> r #
Work with a lens in the van Laarhoven representation.
Re-exports
module Optics.Optic