HORIZON HASKELLDocslts/ghc-9.10.xc74966e2026-09-27Search names, modules, packages, or :: a typeCtrl K

GHC 9.10.3 · lts/ghc-9.10.x · c74966e · 2026-09-27

Moduleoptics-core-0.4.1.1Haskell2010

Optics.Lens

A Lens is a generalised or first-class field.

If we have a value s :: S, and a l :: Lens' S A, we can get the "field value" of type A using view l s. We can also update (or put or set) the value using over (or set).

For example, given the following definitions:

Example2 expressions
data Human = Human { _name :: String, _location :: String } deriving Showlet human = Human "Bob" "London"

we can make a Lens for _name field:

Example1 expression
let name = lens _name $ \s x -> s { _name = x }

which we can use as a Getter:

Example1 expression
view name human"Bob"

or a Setter:

Example1 expression
set name "Robert" humanHuman {_name = "Robert", _location = "London"}
  • 5 types
  • 8 values

Formation

2 declarations

Introduction

1 declaration
valuelens :: (s -> a) -> (s -> b -> t) -> Lens s t a b
#

Build a lens from a getter and a setter, which must respect the well-formedness laws.

If you want to build a Lens from the van Laarhoven representation, use lensVL.

Elimination

0 declarations

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

0 declarations
view (lens f g)   s ≡ f s
set  (lens f g) a s ≡ g s a

Well-formedness

0 declarations
  • 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
    

Additional introduction forms

3 declarations

See Data.Tuple.Optics for Lenses for tuples.

If you're looking for chosen, it was moved to Optics.IxLens.

valuealongside
  1. :: (Is k A_Lens, Is l A_Lens)
  2. => Optic k is s t a b
  3. -> Optic l js s' t' a' b'
  4. -> Lens (s, s') (t, t') (a, a') (b, b')
#

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

Example1 expression
(Left 'a', Right 'b') ^. alongside chosen chosen('a','b')
Example1 expression
(Left 'a', Right 'b') & alongside chosen chosen .~ ('c','d')(Left 'c',Right 'd')
valueunited :: Lens' a ()
#

We can always retrieve a () from any type.

Example1 expression
view united "hello"()
Example1 expression
set united () "hello""hello"

Additional elimination forms

1 declaration

Subtyping

1 declaration
datadata A_Lens
#

Tag for a lens.

Instances35ReversibleOptic, Is, ArrowOptic, JoinKinds, IxOptic, ToReadOnly, …

van Laarhoven encoding

5 declarations

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.

typetype LensVL s t a b = forall (f :: Type -> Type). Functor f => (a -> f b) -> s -> f t
#

Type synonym for a type-modifying van Laarhoven lens.

typetype LensVL' s a = LensVL s s a a
#

Type synonym for a type-preserving van Laarhoven lens.

valuelensVL :: LensVL s t a b -> Lens s t a b
#

Build a lens from the van Laarhoven representation.