optics-core-0.4.1.1: Optics as an abstract interface: core definitions
Safe HaskellSafe-Inferred
LanguageHaskell2010

Optics.Setter

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.

rewriteOf :: Is k A_Setter => Optic k is a b a b -> (b -> Maybe a) -> a -> b Source #

Rewrite by applying a rule everywhere you can. Ensures that the rule cannot be applied anywhere in the result:

propRewriteOf l r x = all (isNothing . r) (universeOf l (rewriteOf l r x))

Usually transformOf is more appropriate, but rewriteOf can give better compositionality. Given two single transformations f and g, you can construct \a -> f a <|> g a which performs both rewrites until a fixed point.

Since: 0.4.1

transformOf :: Is k A_Setter => Optic k is a b a b -> (b -> b) -> a -> b Source #

Transform every element by recursively applying a given Setter in a bottom-up manner.

Since: 0.4.1

Subtyping

data A_Setter :: OpticKind Source #

Tag for a setter.

Instances

Instances details
Is A_Lens A_Setter Source # 
Instance details

Defined in Optics.Internal.Optic.Subtyping

Methods

implies :: forall (p :: Type -> Type -> Type -> Type) r. (Constraints A_Lens p => r) -> Constraints A_Setter p => r Source #

Is A_Prism A_Setter Source # 
Instance details

Defined in Optics.Internal.Optic.Subtyping

Methods

implies :: forall (p :: Type -> Type -> Type -> Type) r. (Constraints A_Prism p => r) -> Constraints A_Setter p => r Source #

Is A_Traversal A_Setter Source # 
Instance details

Defined in Optics.Internal.Optic.Subtyping

Methods

implies :: forall (p :: Type -> Type -> Type -> Type) r. (Constraints A_Traversal p => r) -> Constraints A_Setter p => r Source #

Is An_AffineTraversal A_Setter Source # 
Instance details

Defined in Optics.Internal.Optic.Subtyping

Methods

implies :: forall (p :: Type -> Type -> Type -> Type) r. (Constraints An_AffineTraversal p => r) -> Constraints A_Setter p => r Source #

Is An_Iso A_Setter Source # 
Instance details

Defined in Optics.Internal.Optic.Subtyping

Methods

implies :: forall (p :: Type -> Type -> Type -> Type) r. (Constraints An_Iso p => r) -> Constraints A_Setter p => r Source #

k ~ A_Setter => JoinKinds A_Lens A_Setter k Source # 
Instance details

Defined in Optics.Internal.Optic.Subtyping

Methods

joinKinds :: forall (p :: Type -> Type -> Type -> Type) r. ((Constraints A_Lens p, Constraints A_Setter p) => r) -> Constraints k p => r Source #

k ~ A_Setter => JoinKinds A_Prism A_Setter k Source # 
Instance details

Defined in Optics.Internal.Optic.Subtyping

Methods

joinKinds :: forall (p :: Type -> Type -> Type -> Type) r. ((Constraints A_Prism p, Constraints A_Setter p) => r) -> Constraints k p => r Source #

k ~ A_Setter => JoinKinds A_Setter A_Lens k Source # 
Instance details

Defined in Optics.Internal.Optic.Subtyping

Methods

joinKinds :: forall (p :: Type -> Type -> Type -> Type) r. ((Constraints A_Setter p, Constraints A_Lens p) => r) -> Constraints k p => r Source #

k ~ A_Setter => JoinKinds A_Setter A_Prism k Source # 
Instance details

Defined in Optics.Internal.Optic.Subtyping

Methods

joinKinds :: forall (p :: Type -> Type -> Type -> Type) r. ((Constraints A_Setter p, Constraints A_Prism p) => r) -> Constraints k p => r Source #

k ~ A_Setter => JoinKinds A_Setter A_Setter k Source # 
Instance details

Defined in Optics.Internal.Optic.Subtyping

Methods

joinKinds :: forall (p :: Type -> Type -> Type -> Type) r. ((Constraints A_Setter p, Constraints A_Setter p) => r) -> Constraints k p => r Source #

k ~ A_Setter => JoinKinds A_Setter A_Traversal k Source # 
Instance details

Defined in Optics.Internal.Optic.Subtyping

Methods

joinKinds :: forall (p :: Type -> Type -> Type -> Type) r. ((Constraints A_Setter p, Constraints A_Traversal p) => r) -> Constraints k p => r Source #

k ~ A_Setter => JoinKinds A_Setter An_AffineTraversal k Source # 
Instance details

Defined in Optics.Internal.Optic.Subtyping

Methods

joinKinds :: forall (p :: Type -> Type -> Type -> Type) r. ((Constraints A_Setter p, Constraints An_AffineTraversal p) => r) -> Constraints k p => r Source #

k ~ A_Setter => JoinKinds A_Setter An_Iso k Source # 
Instance details

Defined in Optics.Internal.Optic.Subtyping

Methods

joinKinds :: forall (p :: Type -> Type -> Type -> Type) r. ((Constraints A_Setter p, Constraints An_Iso p) => r) -> Constraints k p => r Source #

k ~ A_Setter => JoinKinds A_Traversal A_Setter k Source # 
Instance details

Defined in Optics.Internal.Optic.Subtyping

Methods

joinKinds :: forall (p :: Type -> Type -> Type -> Type) r. ((Constraints A_Traversal p, Constraints A_Setter p) => r) -> Constraints k p => r Source #

k ~ A_Setter => JoinKinds An_AffineTraversal A_Setter k Source # 
Instance details

Defined in Optics.Internal.Optic.Subtyping

Methods

joinKinds :: forall (p :: Type -> Type -> Type -> Type) r. ((Constraints An_AffineTraversal p, Constraints A_Setter p) => r) -> Constraints k p => r Source #

k ~ A_Setter => JoinKinds An_Iso A_Setter k Source # 
Instance details

Defined in Optics.Internal.Optic.Subtyping

Methods

joinKinds :: forall (p :: Type -> Type -> Type -> Type) r. ((Constraints An_Iso p, Constraints A_Setter p) => r) -> Constraints k p => r Source #

IxOptic A_Setter s t a b Source # 
Instance details

Defined in Optics.Indexed.Core

Methods

noIx :: forall (is :: IxList). NonEmptyIndices is => Optic A_Setter is s t a b -> Optic A_Setter NoIx s t a b Source #