HORIZON HASKELLDocslts/ghc-10.0.x2b276322026-10-31Search packagesCtrl K
Theme: |

GHC 10.0.0.20260917 · lts/ghc-10.0.x · 2b27632 · 2026-10-31

Modulelens-5.3.6Haskell2010

Control.Lens.Setter

A Setter s t a b is a generalization of fmap from Functor. It allows you to map into a structure and change out the contents, but it isn't strong enough to allow you to enumerate those contents. Starting with fmap :: Functor f => (a -> b) -> f a -> f b we monomorphize the type to obtain (a -> b) -> s -> t and then decorate it with Identity to obtain:

type Setter s t a b = (a -> Identity b) -> s -> Identity t

Every Traversal is a valid Setter, since Identity is Applicative.

Everything you can do with a Functor, you can do with a Setter. There are combinators that generalize fmap and (<$).

Contents
  1. Setters
  2. Building Setters
  3. Common Setters
  4. Functional Combinators
  5. State Combinators
  6. Writer Combinators
  7. Reader Combinators
  8. Simplified State Setting
  9. Indexed Setters
  10. Arrow operators
  11. Exported for legible error messages
  12. Deprecated
Synopsis
  • Packagelens-5.3.6
  • Exports77
  • LanguageHaskell2010
  • LicenceBSD-2-Clause
  • SourceSetter.hs

Setters

typetype Setter s t a b = forall (f :: Type -> Type). Settable f => (a -> f b) -> s -> f t

The only LensLike law that can apply to a Setter l is that

set l y (set l x a) ≡ set l y a

You can't view a Setter in general, so the other two laws are irrelevant.

However, two Functor laws apply to a Setter:

over l id ≡ id
over l f . over l g ≡ over l (f . g)

These can be stated more directly:

l pure ≡ pure
l f . untainted . l g ≡ l (f . untainted . g)

You can compose a Setter with a Lens or a Traversal using (.) from the Prelude and the result is always only a Setter and nothing more.

Example1 expression
over traverse f [a,b,c,d][f a,f b,f c,f d]
Example1 expression
over _1 f (a,b)(f a,b)
Example1 expression
over (traverse._1) f [(a,b),(c,d)][(f a,b),(f c,d)]
Example1 expression
over both f (a,b)(f a,f b)
Example1 expression
over (traverse.both) f [(a,b),(c,d)][(f a,f b),(f c,f d)]
typetype ASetter s t a b = (a -> Identity b) -> s -> Identity t

Running a Setter instantiates it to a concrete type.

When consuming a setter directly to perform a mapping, you can use this type, but most user code will not need to use this type.

typetype Setting' (p :: Type -> Type -> Type) s a = Setting p s s a a

This is a convenient alias when defining highly polymorphic code that takes both ASetter' and AnIndexedSetter' as appropriate. If a function takes this it is expecting one of those two things based on context.

Building Setters

Common Setters

valuemapped :: Functor f => Setter (f a) (f b) a b

This Setter can be used to map over all of the values in a Functor.

fmap ≡ over mapped
fmapDefault ≡ over traverse
(<$) ≡ set mapped
Example1 expression
over mapped f [a,b,c][f a,f b,f c]
Example1 expression
over mapped (+1) [1,2,3][2,3,4]
Example1 expression
set mapped x [a,b,c][x,x,x]
Example1 expression
[[a,b],[c]] & mapped.mapped +~ x[[a + x,b + x],[c + x]]
Example1 expression
over (mapped._2) length [("hello","world"),("leaders","!!!")][("hello",5),("leaders",3)]
mapped :: Functor f => Setter (f a) (f b) a b

If you want an IndexPreservingSetter use setting fmap.

valuecontramapped :: Contravariant f => Setter (f b) (f a) a b

This Setter can be used to map over all of the inputs to a Contravariant.

contramap ≡ over contramapped
Example1 expression
getPredicate (over contramapped (*2) (Predicate even)) 5True
Example1 expression
getOp (over contramapped (*5) (Op show)) 100"500"
Example1 expression
Prelude.map ($ 1) $ over (mapped . _Unwrapping' Op . contramapped) (*12) [(*2),(+1),(^3)][24,13,1728]
valueargument :: Profunctor p => Setter (p b r) (p a r) a b

This Setter can be used to map over the input of a Profunctor.

The most common Profunctor to use this with is (->).

Example1 expression
(argument %~ f) g xg (f x)
Example1 expression
(argument %~ show) length [1,2,3]7
Example1 expression
(argument %~ f) h x yh (f x) y

Map over the argument of the result of a function -- i.e., its second argument:

Example1 expression
(mapped.argument %~ f) h x yh x (f y)
argument :: Setter (b -> r) (a -> r) a b

Functional Combinators

valueover :: ASetter s t a b -> (a -> b) -> s -> t

Modify the target of a Lens or all the targets of a Setter or Traversal with a function.

fmap ≡ over mapped
fmapDefault ≡ over traverse
sets . over ≡ id
over . sets ≡ id

Given any valid Setter l, you can also rely on the law:

over l f . over l g = over l (f . g)

e.g.

Example1 expression
over mapped f (over mapped g [a,b,c]) == over mapped (f . g) [a,b,c]True

Another way to view over is to say that it transforms a Setter into a "semantic editor combinator".

Example1 expression
over mapped f (Just a)Just (f a)
Example1 expression
over mapped (*10) [1,2,3][10,20,30]
Example1 expression
over _1 f (a,b)(f a,b)
Example1 expression
over _1 show (10,20)("10",20)
over :: Setter s t a b -> (a -> b) -> s -> t
over :: ASetter s t a b -> (a -> b) -> s -> t
valueset :: ASetter s t a b -> b -> s -> t

Replace the target of a Lens or all of the targets of a Setter or Traversal with a constant value.

(<$) ≡ set mapped
Example1 expression
set _2 "hello" (1,())(1,"hello")
Example1 expression
set mapped () [1,2,3,4][(),(),(),()]

Note: Attempting to set a Fold or Getter will fail at compile time with an relatively nice error message.

set :: Setter s t a b    -> b -> s -> t
set :: Iso s t a b       -> b -> s -> t
set :: Lens s t a b      -> b -> s -> t
set :: Traversal s t a b -> b -> s -> t
value(.~) :: ASetter s t a b -> b -> s -> t

Replace the target of a Lens or all of the targets of a Setter or Traversal with a constant value.

This is an infix version of set, provided for consistency with (.=).

f <$ a ≡ mapped .~ f $ a
Example1 expression
(a,b,c,d) & _4 .~ e(a,b,c,e)
Example1 expression
(42,"world") & _1 .~ "hello"("hello","world")
Example1 expression
(a,b) & both .~ c(c,c)
(.~) :: Setter s t a b    -> b -> s -> t
(.~) :: Iso s t a b       -> b -> s -> t
(.~) :: Lens s t a b      -> b -> s -> t
(.~) :: Traversal s t a b -> b -> s -> t
value(%~) :: ASetter s t a b -> (a -> b) -> s -> t

Modifies the target of a Lens or all of the targets of a Setter or Traversal with a user supplied function.

This is an infix version of over.

fmap f ≡ mapped %~ f
fmapDefault f ≡ traverse %~ f
Example1 expression
(a,b,c) & _3 %~ f(a,b,f c)
Example1 expression
(a,b) & both %~ f(f a,f b)
Example1 expression
_2 %~ length $ (1,"hello")(1,5)
Example1 expression
traverse %~ f $ [a,b,c][f a,f b,f c]
Example1 expression
traverse %~ even $ [1,2,3][False,True,False]
Example1 expression
traverse.traverse %~ length $ [["hello","world"],["!!!"]][[5,5],[3]]
(%~) :: Setter s t a b    -> (a -> b) -> s -> t
(%~) :: Iso s t a b       -> (a -> b) -> s -> t
(%~) :: Lens s t a b      -> (a -> b) -> s -> t
(%~) :: Traversal s t a b -> (a -> b) -> s -> t
value(+~) :: Num a => ASetter s t a a -> a -> s -> t

Increment the target(s) of a numerically valued Lens, Setter or Traversal.

Example1 expression
(a,b) & _1 +~ c(a + c,b)
Example1 expression
(a,b) & both +~ c(a + c,b + c)
Example1 expression
(1,2) & _2 +~ 1(1,3)
Example1 expression
[(a,b),(c,d)] & traverse.both +~ e[(a + e,b + e),(c + e,d + e)]
(+~) :: Num a => Setter' s a    -> a -> s -> s
(+~) :: Num a => Iso' s a       -> a -> s -> s
(+~) :: Num a => Lens' s a      -> a -> s -> s
(+~) :: Num a => Traversal' s a -> a -> s -> s
value(-~) :: Num a => ASetter s t a a -> a -> s -> t

Decrement the target(s) of a numerically valued Lens, Iso, Setter or Traversal.

Example1 expression
(a,b) & _1 -~ c(a - c,b)
Example1 expression
(a,b) & both -~ c(a - c,b - c)
Example1 expression
_1 -~ 2 $ (1,2)(-1,2)
Example1 expression
mapped.mapped -~ 1 $ [[4,5],[6,7]][[3,4],[5,6]]
(-~) :: Num a => Setter' s a    -> a -> s -> s
(-~) :: Num a => Iso' s a       -> a -> s -> s
(-~) :: Num a => Lens' s a      -> a -> s -> s
(-~) :: Num a => Traversal' s a -> a -> s -> s
value(*~) :: Num a => ASetter s t a a -> a -> s -> t

Multiply the target(s) of a numerically valued Lens, Iso, Setter or Traversal.

Example1 expression
(a,b) & _1 *~ c(a * c,b)
Example1 expression
(a,b) & both *~ c(a * c,b * c)
Example1 expression
(1,2) & _2 *~ 4(1,8)
Example1 expression
Just 24 & mapped *~ 2Just 48
(*~) :: Num a => Setter' s a    -> a -> s -> s
(*~) :: Num a => Iso' s a       -> a -> s -> s
(*~) :: Num a => Lens' s a      -> a -> s -> s
(*~) :: Num a => Traversal' s a -> a -> s -> s
value(//~) :: Fractional a => ASetter s t a a -> a -> s -> t

Divide the target(s) of a numerically valued Lens, Iso, Setter or Traversal.

Example1 expression
(a,b) & _1 //~ c(a / c,b)
Example1 expression
(a,b) & both //~ c(a / c,b / c)
Example1 expression
("Hawaii",10) & _2 //~ 2("Hawaii",5.0)
(//~) :: Fractional a => Setter' s a    -> a -> s -> s
(//~) :: Fractional a => Iso' s a       -> a -> s -> s
(//~) :: Fractional a => Lens' s a      -> a -> s -> s
(//~) :: Fractional a => Traversal' s a -> a -> s -> s
value(**~) :: Floating a => ASetter s t a a -> a -> s -> t

Raise the target(s) of a floating-point valued Lens, Setter or Traversal to an arbitrary power.

Example1 expression
(a,b) & _1 **~ c(a**c,b)
Example1 expression
(a,b) & both **~ c(a**c,b**c)
Example1 expression
_2 **~ 10 $ (3,2)(3,1024.0)
(**~) :: Floating a => Setter' s a    -> a -> s -> s
(**~) :: Floating a => Iso' s a       -> a -> s -> s
(**~) :: Floating a => Lens' s a      -> a -> s -> s
(**~) :: Floating a => Traversal' s a -> a -> s -> s
value(<>~) :: Semigroup a => ASetter s t a a -> a -> s -> t

Modify the target of a Semigroup value by using (<>).

Example1 expression
(Sum a,b) & _1 <>~ Sum c(Sum {getSum = a + c},b)
Example1 expression
(Sum a,Sum b) & both <>~ Sum c(Sum {getSum = a + c},Sum {getSum = b + c})
Example1 expression
both <>~ "!!!" $ ("hello","world")("hello!!!","world!!!")
(<>~) :: Semigroup a => Setter s t a a    -> a -> s -> t
(<>~) :: Semigroup a => Iso s t a a       -> a -> s -> t
(<>~) :: Semigroup a => Lens s t a a      -> a -> s -> t
(<>~) :: Semigroup a => Traversal s t a a -> a -> s -> t
value(<>:~) :: Semigroup b => ASetter s t b b -> b -> s -> t

Modify the target of a Semigroup value by using (<>). However, unlike <>~, it is prepend to the head side.

Example1 expression
["world"] & id <>:~ ["hello"]["hello","world"]
Example1 expression
(["world"], ["lens"]) & _1 <>:~ ["hello"](["hello","world"],["lens"])
value(<.~) :: ASetter s t a b -> b -> s -> (b, t)

Set with pass-through.

This is mostly present for consistency, but may be useful for chaining assignments.

If you do not need a copy of the intermediate result, then using l .~ t directly is a good idea.

Example1 expression
(a,b) & _1 <.~ c(c,(c,b))
Example1 expression
("good","morning","vietnam") & _3 <.~ "world"("world",("good","morning","world"))
Example1 expression
(42,Map.fromList [("goodnight","gracie")]) & _2.at "hello" <.~ Just "world"(Just "world",(42,fromList [("goodnight","gracie"),("hello","world")]))
(<.~) :: Setter s t a b    -> b -> s -> (b, t)
(<.~) :: Iso s t a b       -> b -> s -> (b, t)
(<.~) :: Lens s t a b      -> b -> s -> (b, t)
(<.~) :: Traversal s t a b -> b -> s -> (b, t)
value(?~) :: ASetter s t a (Maybe b) -> b -> s -> t

Set the target of a Lens, Traversal or Setter to Just a value.

l ?~ t ≡ set l (Just t)
Example1 expression
Nothing & id ?~ aJust a
Example1 expression
Map.empty & at 3 ?~ xfromList [(3,x)]

?~ can be used type-changily:

Example1 expression
('a', ('b', 'c')) & _2.both ?~ 'x'('a',(Just 'x',Just 'x'))
(?~) :: Setter s t a (Maybe b)    -> b -> s -> t
(?~) :: Iso s t a (Maybe b)       -> b -> s -> t
(?~) :: Lens s t a (Maybe b)      -> b -> s -> t
(?~) :: Traversal s t a (Maybe b) -> b -> s -> t
value(<?~) :: ASetter s t a (Maybe b) -> b -> s -> (b, t)

Set to Just a value with pass-through.

This is mostly present for consistency, but may be useful for for chaining assignments.

If you do not need a copy of the intermediate result, then using l ?~ d directly is a good idea.

Example2 expressions
import qualified Data.Map as Map_2.at "hello" <?~ "world" $ (42,Map.fromList [("goodnight","gracie")])("world",(42,fromList [("goodnight","gracie"),("hello","world")]))
(<?~) :: Setter s t a (Maybe b)    -> b -> s -> (b, t)
(<?~) :: Iso s t a (Maybe b)       -> b -> s -> (b, t)
(<?~) :: Lens s t a (Maybe b)      -> b -> s -> (b, t)
(<?~) :: Traversal s t a (Maybe b) -> b -> s -> (b, t)

State Combinators

valueassign :: MonadState s m => ASetter s s a b -> b -> m ()

Replace the target of a Lens or all of the targets of a Setter or Traversal in our monadic state with a new value, irrespective of the old.

This is an alias for (.=).

Example1 expression
execState (do assign _1 c; assign _2 d) (a,b)(c,d)
Example1 expression
execState (both .= c) (a,b)(c,c)
assign :: MonadState s m => Iso' s a       -> a -> m ()
assign :: MonadState s m => Lens' s a      -> a -> m ()
assign :: MonadState s m => Traversal' s a -> a -> m ()
assign :: MonadState s m => Setter' s a    -> a -> m ()
value(.=) :: MonadState s m => ASetter s s a b -> b -> m ()

Replace the target of a Lens or all of the targets of a Setter or Traversal in our monadic state with a new value, irrespective of the old.

This is an infix version of assign.

Example1 expression
execState (do _1 .= c; _2 .= d) (a,b)(c,d)
Example1 expression
execState (both .= c) (a,b)(c,c)
(.=) :: MonadState s m => Iso' s a       -> a -> m ()
(.=) :: MonadState s m => Lens' s a      -> a -> m ()
(.=) :: MonadState s m => Traversal' s a -> a -> m ()
(.=) :: MonadState s m => Setter' s a    -> a -> m ()

It puts the state in the monad or it gets the hose again.

value(%=) :: MonadState s m => ASetter s s a b -> (a -> b) -> m ()

Map over the target of a Lens or all of the targets of a Setter or Traversal in our monadic state.

Example1 expression
execState (do _1 %= f;_2 %= g) (a,b)(f a,g b)
Example1 expression
execState (do both %= f) (a,b)(f a,f b)
(%=) :: MonadState s m => Iso' s a       -> (a -> a) -> m ()
(%=) :: MonadState s m => Lens' s a      -> (a -> a) -> m ()
(%=) :: MonadState s m => Traversal' s a -> (a -> a) -> m ()
(%=) :: MonadState s m => Setter' s a    -> (a -> a) -> m ()
(%=) :: MonadState s m => ASetter s s a b -> (a -> b) -> m ()
value(+=) :: (MonadState s m, Num a) => ASetter' s a -> a -> m ()

Modify the target(s) of a Lens', Iso, Setter or Traversal by adding a value.

Example:

fresh :: MonadState Int m => m Int
fresh = do
  id += 1
  use id
Example1 expression
execState (do _1 += c; _2 += d) (a,b)(a + c,b + d)
Example1 expression
execState (do _1.at 1.non 0 += 10) (Map.fromList [(2,100)],"hello")(fromList [(1,10),(2,100)],"hello")
(+=) :: (MonadState s m, Num a) => Setter' s a    -> a -> m ()
(+=) :: (MonadState s m, Num a) => Iso' s a       -> a -> m ()
(+=) :: (MonadState s m, Num a) => Lens' s a      -> a -> m ()
(+=) :: (MonadState s m, Num a) => Traversal' s a -> a -> m ()
value(//=) :: (MonadState s m, Fractional a) => ASetter' s a -> a -> m ()

Modify the target(s) of a Lens', Iso, Setter or Traversal by dividing by a value.

Example1 expression
execState (do _1 //= c; _2 //= d) (a,b)(a / c,b / d)
(//=) :: (MonadState s m, Fractional a) => Setter' s a    -> a -> m ()
(//=) :: (MonadState s m, Fractional a) => Iso' s a       -> a -> m ()
(//=) :: (MonadState s m, Fractional a) => Lens' s a      -> a -> m ()
(//=) :: (MonadState s m, Fractional a) => Traversal' s a -> a -> m ()
value(^^=)
  1. :: (MonadState s m, Fractional a, Integral e)
  2. => ASetter' s a
  3. -> e
  4. -> m ()

Raise the target(s) of a numerically valued Lens, Setter or Traversal to an integral power.

(^^=) ::  (MonadState s m, Fractional a, Integral e) => Setter' s a    -> e -> m ()
(^^=) ::  (MonadState s m, Fractional a, Integral e) => Iso' s a       -> e -> m ()
(^^=) ::  (MonadState s m, Fractional a, Integral e) => Lens' s a      -> e -> m ()
(^^=) ::  (MonadState s m, Fractional a, Integral e) => Traversal' s a -> e -> m ()
value(**=) :: (MonadState s m, Floating a) => ASetter' s a -> a -> m ()

Raise the target(s) of a numerically valued Lens, Setter or Traversal to an arbitrary power

Example1 expression
execState (do _1 **= c; _2 **= d) (a,b)(a**c,b**d)
(**=) ::  (MonadState s m, Floating a) => Setter' s a    -> a -> m ()
(**=) ::  (MonadState s m, Floating a) => Iso' s a       -> a -> m ()
(**=) ::  (MonadState s m, Floating a) => Lens' s a      -> a -> m ()
(**=) ::  (MonadState s m, Floating a) => Traversal' s a -> a -> m ()
value(||=) :: MonadState s m => ASetter' s Bool -> Bool -> m ()

Modify the target(s) of a Lens', 'Iso, Setter or Traversal by taking their logical || with a value.

Example1 expression
execState (do _1 ||= True; _2 ||= False; _3 ||= True; _4 ||= False) (True,True,False,False)(True,True,True,False)
(||=) :: MonadState s m => Setter' s Bool    -> Bool -> m ()
(||=) :: MonadState s m => Iso' s Bool       -> Bool -> m ()
(||=) :: MonadState s m => Lens' s Bool      -> Bool -> m ()
(||=) :: MonadState s m => Traversal' s Bool -> Bool -> m ()
value(<>=) :: (MonadState s m, Semigroup a) => ASetter' s a -> a -> m ()

Modify the target(s) of a Lens', Iso, Setter or Traversal by using (<>).

Example1 expression
execState (do _1 <>= Sum c; _2 <>= Product d) (Sum a,Product b)(Sum {getSum = a + c},Product {getProduct = b * d})
Example1 expression
execState (both <>= "!!!") ("hello","world")("hello!!!","world!!!")
(<>=) :: (MonadState s m, Semigroup a) => Setter' s a -> a -> m ()
(<>=) :: (MonadState s m, Semigroup a) => Iso' s a -> a -> m ()
(<>=) :: (MonadState s m, Semigroup a) => Lens' s a -> a -> m ()
(<>=) :: (MonadState s m, Semigroup a) => Traversal' s a -> a -> m ()
value(&&=) :: MonadState s m => ASetter' s Bool -> Bool -> m ()

Modify the target(s) of a Lens', Iso, Setter or Traversal by taking their logical && with a value.

Example1 expression
execState (do _1 &&= True; _2 &&= False; _3 &&= True; _4 &&= False) (True,True,False,False)(True,False,False,False)
(&&=) :: MonadState s m => Setter' s Bool    -> Bool -> m ()
(&&=) :: MonadState s m => Iso' s Bool       -> Bool -> m ()
(&&=) :: MonadState s m => Lens' s Bool      -> Bool -> m ()
(&&=) :: MonadState s m => Traversal' s Bool -> Bool -> m ()
value(<.=) :: MonadState s m => ASetter s s a b -> b -> m b

Set with pass-through

This is useful for chaining assignment without round-tripping through your Monad stack.

do x <- _2 <.= ninety_nine_bottles_of_beer_on_the_wall

If you do not need a copy of the intermediate result, then using l .= d will avoid unused binding warnings.

(<.=) :: MonadState s m => Setter s s a b    -> b -> m b
(<.=) :: MonadState s m => Iso s s a b       -> b -> m b
(<.=) :: MonadState s m => Lens s s a b      -> b -> m b
(<.=) :: MonadState s m => Traversal s s a b -> b -> m b
value(?=) :: MonadState s m => ASetter s s a (Maybe b) -> b -> m ()

Replace the target of a Lens or all of the targets of a Setter or Traversal in our monadic state with Just a new value, irrespective of the old.

Example1 expression
execState (do at 1 ?= a; at 2 ?= b) Map.emptyfromList [(1,a),(2,b)]
Example1 expression
execState (do _1 ?= b; _2 ?= c) (Just a, Nothing)(Just b,Just c)
(?=) :: MonadState s m => Iso' s (Maybe a)       -> a -> m ()
(?=) :: MonadState s m => Lens' s (Maybe a)      -> a -> m ()
(?=) :: MonadState s m => Traversal' s (Maybe a) -> a -> m ()
(?=) :: MonadState s m => Setter' s (Maybe a)    -> a -> m ()
value(<?=) :: MonadState s m => ASetter s s a (Maybe b) -> b -> m b

Set Just a value with pass-through

This is useful for chaining assignment without round-tripping through your Monad stack.

do x <- at "foo" <?= ninety_nine_bottles_of_beer_on_the_wall

If you do not need a copy of the intermediate result, then using l ?= d will avoid unused binding warnings.

(<?=) :: MonadState s m => Setter s s a (Maybe b)    -> b -> m b
(<?=) :: MonadState s m => Iso s s a (Maybe b)       -> b -> m b
(<?=) :: MonadState s m => Lens s s a (Maybe b)      -> b -> m b
(<?=) :: MonadState s m => Traversal s s a (Maybe b) -> b -> m b
value(<~) :: MonadState s m => ASetter s s a b -> m b -> m ()

Run a monadic action, and set all of the targets of a Lens, Setter or Traversal to its result.

(<~) :: MonadState s m => Iso s s a b       -> m b -> m ()
(<~) :: MonadState s m => Lens s s a b      -> m b -> m ()
(<~) :: MonadState s m => Traversal s s a b -> m b -> m ()
(<~) :: MonadState s m => Setter s s a b    -> m b -> m ()

As a reasonable mnemonic, this lets you store the result of a monadic action in a Lens rather than in a local variable.

do foo <- bar
   ...

will store the result in a variable, while

do foo <~ bar
   ...

will store the result in a Lens, Setter, or Traversal.

Writer Combinators

Reader Combinators

valuelocally :: MonadReader s m => ASetter s s a b -> (a -> b) -> m r -> m r

Modify the value of the Reader environment associated with the target of a Setter, Lens, or Traversal.

locally l id a ≡ a
locally l f . locally l g ≡ locally l (f . g)
Example1 expression
(1,1) & locally _1 (+1) (uncurry (+))3
Example1 expression
"," & locally ($) ("Hello" <>) (<> " world!")"Hello, world!"
locally :: MonadReader s m => Iso s s a b       -> (a -> b) -> m r -> m r
locally :: MonadReader s m => Lens s s a b      -> (a -> b) -> m r -> m r
locally :: MonadReader s m => Traversal s s a b -> (a -> b) -> m r -> m r
locally :: MonadReader s m => Setter s s a b    -> (a -> b) -> m r -> m r
valueilocally
  1. :: MonadReader s m
  2. => AnIndexedSetter i s s a b
  3. -> i -> a -> b
  4. -> m r
  5. -> m r

This is a generalization of locally that allows one to make indexed local changes to a Reader environment associated with the target of a Setter, Lens, or Traversal.

locally l f ≡ ilocally l f . const
ilocally l f ≡ locally l f . Indexed
ilocally :: MonadReader s m => IndexedLens s s a b      -> (i -> a -> b) -> m r -> m r
ilocally :: MonadReader s m => IndexedTraversal s s a b -> (i -> a -> b) -> m r -> m r
ilocally :: MonadReader s m => IndexedSetter s s a b    -> (i -> a -> b) -> m r -> m r

Simplified State Setting

valueset' :: ASetter' s a -> a -> s -> s

Replace the target of a Lens or all of the targets of a Setter' or Traversal with a constant value, without changing its type.

This is a type restricted version of set, which retains the type of the original.

Example1 expression
set' mapped x [a,b,c,d][x,x,x,x]
Example1 expression
set' _2 "hello" (1,"world")(1,"hello")
Example1 expression
set' mapped 0 [1,2,3,4][0,0,0,0]

Note: Attempting to adjust set' a Fold or Getter will fail at compile time with an relatively nice error message.

set' :: Setter' s a    -> a -> s -> s
set' :: Iso' s a       -> a -> s -> s
set' :: Lens' s a      -> a -> s -> s
set' :: Traversal' s a -> a -> s -> s

Indexed Setters

value(%@=)
  1. :: MonadState s m
  2. => AnIndexedSetter i s s a b
  3. -> i -> a -> b
  4. -> m ()

Adjust every target in the current state of an IndexedSetter, IndexedLens or IndexedTraversal with access to the index.

When you do not need access to the index then (%=) is more liberal in what it can accept.

l %= f ≡ l %@= const f
(%@=) :: MonadState s m => IndexedSetter i s s a b    -> (i -> a -> b) -> m ()
(%@=) :: MonadState s m => IndexedLens i s s a b      -> (i -> a -> b) -> m ()
(%@=) :: MonadState s m => IndexedTraversal i s t a b -> (i -> a -> b) -> m ()
value(.@=) :: MonadState s m => AnIndexedSetter i s s a b -> (i -> b) -> m ()

Replace every target in the current state of an IndexedSetter, IndexedLens or IndexedTraversal with access to the index.

When you do not need access to the index then (.=) is more liberal in what it can accept.

l .= b ≡ l .@= const b
(.@=) :: MonadState s m => IndexedSetter i s s a b    -> (i -> b) -> m ()
(.@=) :: MonadState s m => IndexedLens i s s a b      -> (i -> b) -> m ()
(.@=) :: MonadState s m => IndexedTraversal i s t a b -> (i -> b) -> m ()

Arrow operators

valueassignA :: Arrow p => ASetter s t a b -> p s b -> p s t

Run an arrow command and use the output to set all the targets of a Lens, Setter or Traversal to the result.

assignA can be used very similarly to (<~), except that the type of the object being modified can change; for example:

runKleisli action ((), (), ()) where
  action =      assignA _1 (Kleisli (const getVal1))
           >>> assignA _2 (Kleisli (const getVal2))
           >>> assignA _3 (Kleisli (const getVal3))
  getVal1 :: Either String Int
  getVal1 = ...
  getVal2 :: Either String Bool
  getVal2 = ...
  getVal3 :: Either String Char
  getVal3 = ...

has the type Either String (Int, Bool, Char)

assignA :: Arrow p => Iso s t a b       -> p s b -> p s t
assignA :: Arrow p => Lens s t a b      -> p s b -> p s t
assignA :: Arrow p => Traversal s t a b -> p s b -> p s t
assignA :: Arrow p => Setter s t a b    -> p s b -> p s t

Exported for legible error messages

classclass (Applicative f, Distributive f, Traversable f) => Settable (f :: Type -> Type) where

Anything Settable must be isomorphic to the Identity Functor.

Instances3Settable
newtypenewtype Identity a

Identity functor and monad. (a non-strict monad)

Examples
Example1 expression
fmap (+1) (Identity 0)Identity 1
Example1 expression
Identity [1, 2, 3] <> Identity [4, 5, 6]Identity [1,2,3,4,5,6]
>>> do
      x <- Identity 10
      y <- Identity (x + 5)
      pure (x + y)
Identity 25

Constructors

Instances80Monad, Functor, MonadFix, Applicative, Foldable, Traversable, …

Deprecated