optics-core-0.1

Safe HaskellNone
LanguageHaskell2010

Optics.Lens

Contents

Description

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

Formation

data A_Lens :: OpticKind #

Tag for a lens.

Instances
ReversibleOptic A_Lens # 
Instance details

Defined in Optics.Internal.Re

Associated Types

type ReversedOptic A_Lens :: OpticKind #

Methods

re :: Optic A_Lens [] s t a b -> Optic (ReversedOptic A_Lens) [] b a t s #

Is A_Lens A_Fold # 
Instance details

Defined in Optics.Internal.Optic.Subtyping

Methods

implies :: proxy A_Lens A_Fold p -> (Constraints A_Lens p -> r) -> Constraints A_Fold p -> r

Is A_Lens An_AffineFold # 
Instance details

Defined in Optics.Internal.Optic.Subtyping

Methods

implies :: proxy A_Lens An_AffineFold p -> (Constraints A_Lens p -> r) -> Constraints An_AffineFold p -> r

Is A_Lens A_Getter # 
Instance details

Defined in Optics.Internal.Optic.Subtyping

Methods

implies :: proxy A_Lens A_Getter p -> (Constraints A_Lens p -> r) -> Constraints A_Getter p -> r

Is A_Lens A_Setter # 
Instance details

Defined in Optics.Internal.Optic.Subtyping

Methods

implies :: proxy A_Lens A_Setter p -> (Constraints A_Lens p -> r) -> Constraints A_Setter p -> r

Is A_Lens A_Traversal # 
Instance details

Defined in Optics.Internal.Optic.Subtyping

Methods

implies :: proxy A_Lens A_Traversal p -> (Constraints A_Lens p -> r) -> Constraints A_Traversal p -> r

Is A_Lens An_AffineTraversal # 
Instance details

Defined in Optics.Internal.Optic.Subtyping

Methods

implies :: proxy A_Lens An_AffineTraversal p -> (Constraints A_Lens p -> r) -> Constraints An_AffineTraversal p -> r

Is An_Iso A_Lens # 
Instance details

Defined in Optics.Internal.Optic.Subtyping

Methods

implies :: proxy An_Iso A_Lens p -> (Constraints An_Iso p -> r) -> Constraints A_Lens p -> r

Is An_Equality A_Lens # 
Instance details

Defined in Optics.Internal.Optic.Subtyping

Methods

implies :: proxy An_Equality A_Lens p -> (Constraints An_Equality p -> r) -> Constraints A_Lens p -> r

Arrow arr => ArrowOptic A_Lens arr # 
Instance details

Defined in Optics.Arrow

Methods

overA :: Optic A_Lens [] s t a b -> arr a b -> arr s t #

ViewableOptic A_Lens r # 
Instance details

Defined in Optics.Internal.View

Associated Types

type ViewResult A_Lens r :: * #

Methods

view :: Optic' A_Lens is s r -> s -> ViewResult A_Lens r #

PermeableOptic A_Lens r # 
Instance details

Defined in Optics.Internal.Passthrough

Methods

passthrough :: Optic A_Lens is s t a b -> (a -> (r, b)) -> s -> (ViewResult A_Lens r, t) #

type ReversedOptic A_Lens # 
Instance details

Defined in Optics.Internal.Re

type ViewResult A_Lens r # 
Instance details

Defined in Optics.Internal.View

type ViewResult A_Lens r = r

type Lens s t a b = Optic A_Lens '[] s t a b #

Type synonym for a type-modifying lens.

type Lens' s a = Optic' A_Lens '[] s a #

Type synonym for a type-preserving lens.

Introduction

lens :: (s -> a) -> (s -> b -> t) -> Lens s t a b #

Build a lens from a getter and a setter.

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 -> a
set   :: Lens i s t a b -> b -> s -> t
over  :: Lens i s t a b -> (a -> b) -> s -> t

Computation

view1 (lens f g)   s = f s
set   (lens f g) a s = g s a

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.

type LensVL' s a = LensVL s s a a #

Type synonym for a type-preserving van Laarhoven lens.

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

Build a lens from the van Laarhoven representation.

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