optics-core-0.2: Optics as an abstract interface: core definitions

Safe HaskellNone
LanguageHaskell2010

Optics.Setter

Contents

Description

A Setter S T A B has the ability to lift a function of type A -> B over a function of type S -> T, applying the function to update all the As contained in S. This can be used to set all the As to a single value (by lifting a constant function).

This can be seen as a generalisation of fmap, where the type S does not need to be a type constructor with A as its last parameter.

Synopsis

Formation

type Setter s t a b = Optic A_Setter NoIx s t a b Source #

Type synonym for a type-modifying setter.

type Setter' s a = Optic' A_Setter NoIx s a Source #

Type synonym for a type-preserving setter.

Introduction

sets :: ((a -> b) -> s -> t) -> Setter s t a b Source #

Build a setter from a function to modify the element(s), which must respect the well-formedness laws.

Elimination

over :: Is k A_Setter => Optic k is s t a b -> (a -> b) -> s -> t Source #

Apply a setter as a modifier.

Computation

over (sets f) ≡ f

Well-formedness

  • PutPut: Setting twice is the same as setting once:

    set l v' (set l v s) ≡ set l v' s
    
  • Functoriality: Setters must preserve identities and composition:

    over s idid
    over s f . over s g ≡ over s (f . g)
    

Additional introduction forms

See also setmapped, which changes the elements of a Set.

mapped :: Functor f => Setter (f a) (f b) a b Source #

Create a Setter for a Functor.

over mappedfmap

Additional elimination forms

set :: Is k A_Setter => Optic k is s t a b -> b -> s -> t Source #

Apply a setter.

set o v ≡ over o (const v)
>>> set _1 'x' ('y', 'z')
('x','z')

set' :: Is k A_Setter => Optic k is s t a b -> b -> s -> t Source #

Apply a setter, strictly.

TODO DOC: what exactly is the strictness property?

over' :: Is k A_Setter => Optic k is s t a b -> (a -> b) -> s -> t Source #

Apply a setter as a modifier, strictly.

TODO DOC: what exactly is the strictness property?

Example:

 f :: Int -> (Int, a) -> (Int, a)
 f k acc
   | k > 0     = f (k - 1) $ over' _1 (+1) acc
   | otherwise = acc

runs in constant space, but would result in a space leak if used with over.

Note that replacing $ with $! or _1 with _1' (which amount to the same thing) doesn't help when over is used, because the first coordinate of a pair is never forced.

Subtyping

data A_Setter :: OpticKind Source #

Tag for a setter.

Instances
Is A_Traversal A_Setter Source # 
Instance details

Defined in Optics.Internal.Optic.Subtyping

Is An_AffineTraversal A_Setter Source # 
Instance details

Defined in Optics.Internal.Optic.Subtyping

Is A_Prism A_Setter Source # 
Instance details

Defined in Optics.Internal.Optic.Subtyping

Methods

implies :: proxy A_Prism A_Setter p -> (Constraints A_Prism p -> r) -> Constraints A_Setter p -> r Source #

Is A_Lens A_Setter Source # 
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 Source #

Is An_Iso A_Setter Source # 
Instance details

Defined in Optics.Internal.Optic.Subtyping

Methods

implies :: proxy An_Iso A_Setter p -> (Constraints An_Iso p -> r) -> Constraints A_Setter p -> r Source #

IxOptic A_Setter s t a b Source # 
Instance details

Defined in Optics.Indexed.Core

Methods

noIx :: NonEmptyIndices is => Optic A_Setter is s t a b -> Optic A_Setter NoIx s t a b Source #