Safe Haskell | None |
---|---|

Language | Haskell2010 |

A `Lens`

is a generalised or first-class field.

If we have a value `s :: S`

, and a `l :: `

, we can `Lens'`

S A*get*
the "field value" of type `A`

using

. We
can also `view`

l s*update* (or *put* or *set*) the value using
`over`

(or `set`

).

For example, given the following definitions:

`>>>`

`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`

:

`>>>`

"Bob"`view name human`

or a `Setter`

:

`>>>`

Human {_name = "Robert", _location = "London"}`set name "Robert" human`

## Synopsis

- type Lens s t a b = Optic A_Lens NoIx s t a b
- type Lens' s a = Optic' A_Lens NoIx s a
- lens :: (s -> a) -> (s -> b -> t) -> Lens s t a b
- equality' :: Lens a b a b
- chosen :: Lens (Either a a) (Either b b) a b
- alongside :: (Is k A_Lens, Is l A_Lens) => Optic k is s t a b -> Optic l js s' t' a' b' -> Lens (s, s') (t, t') (a, a') (b, b')
- united :: Lens' a ()
- withLens :: Is k A_Lens => Optic k is s t a b -> ((s -> a) -> (s -> b -> t) -> r) -> r
- data A_Lens :: OpticKind
- 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

# Formation

# Introduction

# Elimination

A `Lens`

is in particular a `Getter`

and a
`Setter`

, therefore you can specialise types to obtain:

`view`

::`Lens'`

s a -> s -> a

`over`

::`Lens`

s t a b -> (a -> b) -> s -> t`set`

::`Lens`

s t a b -> b -> s -> t

If you want to `view`

a type-modifying `Lens`

that is
insufficiently polymorphic to be used as a type-preserving `Lens'`

, use
`getting`

:

`view`

.`getting`

::`Lens`

s t a b -> s -> a

# Computation

# Well-formedness

# Additional introduction forms

See Data.Tuple.Optics for `Lens`

es for tuples.

alongside :: (Is k A_Lens, Is l A_Lens) => Optic k is s t a b -> Optic l js s' t' a' b' -> Lens (s, s') (t, t') (a, a') (b, b') Source #

Make a `Lens`

from two other lenses by executing them on their respective
halves of a product.

`>>>`

('a','b')`(Left 'a', Right 'b') ^. alongside chosen chosen`

`>>>`

(Left 'c',Right 'd')`(Left 'a', Right 'b') & alongside chosen chosen .~ ('c','d')`

We can always retrieve a `()`

from any type.

`>>>`

()`view united "hello"`

`>>>`

"hello"`set united () "hello"`

# Additional elimination forms

# Subtyping

data A_Lens :: OpticKind Source #

Tag for a lens.

## Instances

# van Laarhoven encoding

The van Laarhoven encoding of lenses is isomorphic to the profunctor
encoding used internally by `optics`

, but converting back and forth may
have a performance penalty.

type LensVL s t a b = forall f. Functor f => (a -> f b) -> s -> f t Source #

Type synonym for a type-modifying van Laarhoven lens.

lensVL :: LensVL s t a b -> Lens s t a b Source #

Build a lens from the van Laarhoven representation.