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

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.

  • 3 types
  • 8 values

Formation

2 declarations

Introduction

1 declaration
valuesets :: ((a -> b) -> s -> t) -> Setter s t a b
#

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

Elimination

1 declaration
valueover :: Is k A_Setter => Optic k is s t a b -> (a -> b) -> s -> t
#

Apply a setter as a modifier.

Computation

0 declarations
over (sets f) ≡ f

Well-formedness

0 declarations
  • 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 id ≡ id
    over s f . over s g ≡ over s (f . g)
    

Additional introduction forms

1 declaration

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

Additional elimination forms

5 declarations
valueset :: Is k A_Setter => Optic k is s t a b -> b -> s -> t
#

Apply a setter.

set o v ≡ over o (const v)
Example1 expression
set _1 'x' ('y', 'z')('x','z')
valueset' :: Is k A_Setter => Optic k is s t a b -> b -> s -> t
#

Apply a setter, strictly.

TODO DOC: what exactly is the strictness property?

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

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.

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

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

propRewriteOf l r x = all (Data.Just.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.

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

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

Subtyping

1 declaration
datadata A_Setter
#

Tag for a setter.

Instances17Is, JoinKinds, IxOptic, …