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

Modulelens-5.3.5Haskell2010

Control.Lens.Lens

A Lens s t a b is a purely functional reference.

While a Control.Lens.Traversal.Traversal could be used for Getting like a valid Control.Lens.Fold.Fold, it wasn't a valid Control.Lens.Getter.Getter as a Control.Lens.Getter.Getter can't require an Applicative constraint.

Functor, however, is a constraint on both.

type Lens s t a b = forall f. Functor f => (a -> f b) -> s -> f t

Every Lens is a valid Control.Lens.Setter.Setter.

Every Lens can be used for Getting like a Control.Lens.Fold.Fold that doesn't use the Applicative or Contravariant.

Every Lens is a valid Control.Lens.Traversal.Traversal that only uses the Functor part of the Applicative it is supplied.

Every Lens can be used for Getting like a valid Control.Lens.Getter.Getter.

Since every Lens can be used for Getting like a valid Control.Lens.Getter.Getter it follows that it must view exactly one element in the structure.

The Lens laws follow from this property and the desire for it to act like a Traversable when used as a Control.Lens.Traversal.Traversal.

In the examples below, getter and setter are supplied as example getters and setters, and are not actual functions supplied by this package.

  • 10 types
  • 95 values
  • Packagelens-5.3.5
  • Exports105
  • LanguageHaskell2010
  • LicenceBSD-2-Clause
  • SourceLens.hs

Lenses

4 declarations
typetype Lens s t a b = forall (f :: Type -> Type). Functor f => (a -> f b) -> s -> f t
#

A Lens is actually a lens family as described in http://comonad.com/reader/2012/mirrored-lenses/.

With great power comes great responsibility and a Lens is subject to the three common sense Lens laws:

1) You get back what you put in:

view l (set l v s)  ≡ v

2) Putting back what you got doesn't change anything:

set l (view l s) s  ≡ s

3) Setting twice is the same as setting once:

set l v' (set l v s) ≡ set l v' s

These laws are strong enough that the 4 type parameters of a Lens cannot vary fully independently. For more on how they interact, read the "Why is it a Lens Family?" section of http://comonad.com/reader/2012/mirrored-lenses/.

There are some emergent properties of these laws:

1) set l s must be injective for every s This is a consequence of law #1

2) set l must be surjective, because of law #2, which indicates that it is possible to obtain any v from some s such that set s v = s

3) Given just the first two laws you can prove a weaker form of law #3 where the values v that you are setting match:

set l v (set l v s) ≡ set l v s

Every Lens can be used directly as a Control.Lens.Setter.Setter or Traversal.

You can also use a Lens for Getting as if it were a Fold or Getter.

Since every Lens is a valid Traversal, the Traversal laws are required of any Lens you create:

l pure ≡ pure
fmap (l f) . l g ≡ getCompose . l (Compose . fmap f . g)
type Lens s t a b = forall f. Functor f => LensLike f s t a b

Concrete Lenses

typetype ALens s t a b = LensLike (Pretext (->) a b) s t a b
#

When you see this as an argument to a function, it expects a Lens.

This type can also be used when you need to store a Lens in a container, since it is rank-1. You can turn them back into a Lens with cloneLens, or use it directly with combinators like storing and (^#).

Combinators

12 declarations
valuelens :: (s -> a) -> (s -> b -> t) -> Lens s t a b
#

Build a Lens from a getter and a setter.

lens :: Functor f => (s -> a) -> (s -> b -> t) -> (a -> f b) -> s -> f t
Example1 expression
s ^. lens getter settergetter s
Example1 expression
s & lens getter setter .~ bsetter s b
Example1 expression
s & lens getter setter %~ fsetter s (f (getter s))
lens :: (s -> a) -> (s -> a -> s) -> Lens' s a
valueilens :: (s -> (i, a)) -> (s -> b -> t) -> IndexedLens i s t a b
#

Build an IndexedLens from a Control.Lens.Getter.Getter and a Control.Lens.Setter.Setter.

valueiplens :: (s -> a) -> (s -> b -> t) -> IndexPreservingLens s t a b
#

Build an index-preserving Lens from a Control.Lens.Getter.Getter and a Control.Lens.Setter.Setter.

valuewithLens :: ALens s t a b -> ((s -> a) -> (s -> b -> t) -> r) -> r
#

Obtain a getter and a setter from a lens, reversing lens.

value(%%~) :: LensLike f s t a b -> (a -> f b) -> s -> f t
#

(%%~) can be used in one of two scenarios:

When applied to a Lens, it can edit the target of the Lens in a structure, extracting a functorial result.

When applied to a Traversal, it can edit the targets of the traversals, extracting an applicative summary of its actions.

Example1 expression
[66,97,116,109,97,110] & each %%~ \a -> ("na", chr a)("nananananana","Batman")

For all that the definition of this combinator is just:

(%%~) ≡ id

It may be beneficial to think about it as if it had these even more restricted types, however:

(%%~) :: Functor f =>     Control.Lens.Iso.Iso s t a b       -> (a -> f b) -> s -> f t
(%%~) :: Functor f =>     Lens s t a b      -> (a -> f b) -> s -> f t
(%%~) :: Applicative f => Traversal s t a b -> (a -> f b) -> s -> f t

When applied to a Traversal, it can edit the targets of the traversals, extracting a supplemental monoidal summary of its actions, by choosing f = ((,) m)

(%%~) ::             Control.Lens.Iso.Iso s t a b       -> (a -> (r, b)) -> s -> (r, t)
(%%~) ::             Lens s t a b      -> (a -> (r, b)) -> s -> (r, t)
(%%~) :: Monoid m => Traversal s t a b -> (a -> (m, b)) -> s -> (m, t)
value(%%=) :: MonadState s m => Over p (Tuple2 r) s s a b -> p a (r, b) -> m r
#

Modify the target of a Lens in the current state returning some extra information of type r or modify all targets of a Control.Lens.Traversal.Traversal in the current state, extracting extra information of type r and return a monoidal summary of the changes.

Example1 expression
runState (_1 %%= \x -> (f x, g x)) (a,b)(f a,(g a,b))
(%%=) ≡ (state .)

It may be useful to think of (%%=), instead, as having either of the following more restricted type signatures:

(%%=) :: MonadState s m             => Control.Lens.Iso.Iso s s a b       -> (a -> (r, b)) -> m r
(%%=) :: MonadState s m             => Lens s s a b      -> (a -> (r, b)) -> m r
(%%=) :: (MonadState s m, Monoid r) => Control.Lens.Traversal.Traversal s s a b -> (a -> (r, b)) -> m r
value(%%@~) :: Over (Indexed i) f s t a b -> (i -> a -> f b) -> s -> f t
#

Adjust the target of an IndexedLens returning a supplementary result, or adjust all of the targets of an Control.Lens.Traversal.IndexedTraversal and return a monoidal summary of the supplementary results and the answer.

(%%@~) ≡ Control.Lens.Indexed.withIndex
(%%@~) :: Functor f => IndexedLens i s t a b      -> (i -> a -> f b) -> s -> f t
(%%@~) :: Applicative f => Control.Lens.Traversal.IndexedTraversal i s t a b -> (i -> a -> f b) -> s -> f t

In particular, it is often useful to think of this function as having one of these even more restricted type signatures:

(%%@~) ::             IndexedLens i s t a b      -> (i -> a -> (r, b)) -> s -> (r, t)
(%%@~) :: Monoid r => Control.Lens.Traversal.IndexedTraversal i s t a b -> (i -> a -> (r, b)) -> s -> (r, t)
value(%%@=)
  1. :: MonadState s m
  2. => Over (Indexed i) (Tuple2 r) s s a b
  3. -> i -> a -> (r, b)
  4. -> m r
#

Adjust the target of an IndexedLens returning a supplementary result, or adjust all of the targets of an Control.Lens.Traversal.IndexedTraversal within the current state, and return a monoidal summary of the supplementary results.

l %%@= f ≡ state (l %%@~ f)
(%%@=) :: MonadState s m                 => IndexedLens i s s a b      -> (i -> a -> (r, b)) -> s -> m r
(%%@=) :: (MonadState s m, Monoid r) => Control.Lens.Traversal.IndexedTraversal i s s a b -> (i -> a -> (r, b)) -> s -> m r
value(<%@~)
  1. :: Over (Indexed i) (Tuple2 b) s t a b
  2. -> i -> a -> b
  3. -> s
  4. -> (b, t)
#

Adjust the target of an IndexedLens returning the intermediate result, or adjust all of the targets of an Control.Lens.Traversal.IndexedTraversal and return a monoidal summary along with the answer.

l <%~ f ≡ l <%@~ const f

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

If you do not need the intermediate result, you can use (%@~) or even (%~).

(<%@~) ::             IndexedLens i s t a b      -> (i -> a -> b) -> s -> (b, t)
(<%@~) :: Monoid b => Control.Lens.Traversal.IndexedTraversal i s t a b -> (i -> a -> b) -> s -> (b, t)
value(<%@=)
  1. :: MonadState s m
  2. => Over (Indexed i) (Tuple2 b) s s a b
  3. -> i -> a -> b
  4. -> m b
#

Adjust the target of an IndexedLens returning the intermediate result, or adjust all of the targets of an Control.Lens.Traversal.IndexedTraversal within the current state, and return a monoidal summary of the intermediate results.

(<%@=) :: MonadState s m                 => IndexedLens i s s a b      -> (i -> a -> b) -> m b
(<%@=) :: (MonadState s m, Monoid b) => Control.Lens.Traversal.IndexedTraversal i s s a b -> (i -> a -> b) -> m b
value(<<%@~)
  1. :: Over (Indexed i) (Tuple2 a) s t a b
  2. -> i -> a -> b
  3. -> s
  4. -> (a, t)
#

Adjust the target of an IndexedLens returning the old value, or adjust all of the targets of an Control.Lens.Traversal.IndexedTraversal and return a monoidal summary of the old values along with the answer.

(<<%@~) ::             IndexedLens i s t a b      -> (i -> a -> b) -> s -> (a, t)
(<<%@~) :: Monoid a => Control.Lens.Traversal.IndexedTraversal i s t a b -> (i -> a -> b) -> s -> (a, t)
value(<<%@=)
  1. :: MonadState s m
  2. => Over (Indexed i) (Tuple2 a) s s a b
  3. -> i -> a -> b
  4. -> m a
#

Adjust the target of an IndexedLens returning the old value, or adjust all of the targets of an Control.Lens.Traversal.IndexedTraversal within the current state, and return a monoidal summary of the old values.

(<<%@=) :: MonadState s m                 => IndexedLens i s s a b      -> (i -> a -> b) -> m a
(<<%@=) :: (MonadState s m, Monoid b) => Control.Lens.Traversal.IndexedTraversal i s s a b -> (i -> a -> b) -> m a

General Purpose Combinators

value(&) :: a -> (a -> b) -> b
#

& is a reverse application operator. This provides notational convenience. Its precedence is one higher than that of the forward application operator $, which allows & to be nested in $.

This is a version of flip id, where id is specialized from a -> a to (a -> b) -> (a -> b) which by the associativity of (->) is (a -> b) -> a -> b. flipping this yields a -> (a -> b) -> b which is the type signature of &

Examples
Example1 expression
5 & (+1) & show"6"
Example1 expression
sqrt $ [1 / n^2 | n <- [1..1000]] & sum & (*6)3.1406380562059946
value(<&>) :: Functor f => f a -> (a -> b) -> f b
#

Flipped version of <$>.

(<&>) = flip fmap
Examples

Apply (+1) to a list, a Just and a Right:

Example1 expression
Just 2 <&> (+1)Just 3
Example1 expression
[1,2,3] <&> (+1)[2,3,4]
Example1 expression
Right 3 <&> (+1)Right 4
value(??) :: Functor f => f (a -> b) -> a -> f b
#

This is convenient to flip argument order of composite functions defined as:

fab ?? a = fmap ($ a) fab

For the Functor instance f = ((->) r) you can reason about this function as if the definition was (??) ≡ flip:

Example1 expression
(h ?? x) ah a x
Example1 expression
execState ?? [] $ modify (1:)[1]
Example1 expression
over _2 ?? ("hello","world") $ length("hello",5)
Example1 expression
over ?? length ?? ("hello","world") $ _2("hello",5)
value(&~) :: s -> State s a -> s
#

This can be used to chain lens operations using op= syntax rather than op~ syntax for simple non-type-changing cases.

Example1 expression
(10,20) & _1 .~ 30 & _2 .~ 40(30,40)
Example1 expression
(10,20) &~ do _1 .= 30; _2 .= 40(30,40)

This does not support type-changing assignment, e.g.

Example1 expression
(10,20) & _1 .~ "hello"("hello",20)

Lateral Composition

4 declarations
valuechoosing
  1. :: Functor f
  2. => LensLike f s t a b
  3. -> LensLike f s' t' a b
  4. -> LensLike f (Either s s') (Either t t') a b
#

Merge two lenses, getters, setters, folds or traversals.

chosen ≡ choosing id id
choosing :: Control.Lens.Getter.Getter s a     -> Control.Lens.Getter.Getter s' a     -> Control.Lens.Getter.Getter (Either s s') a
choosing :: Control.Lens.Fold.Fold s a       -> Control.Lens.Fold.Fold s' a       -> Control.Lens.Fold.Fold (Either s s') a
choosing :: Lens' s a      -> Lens' s' a      -> Lens' (Either s s') a
choosing :: Control.Lens.Traversal.Traversal' s a -> Control.Lens.Traversal.Traversal' s' a -> Control.Lens.Traversal.Traversal' (Either s s') a
choosing :: Control.Lens.Setter.Setter' s a    -> Control.Lens.Setter.Setter' s' a    -> Control.Lens.Setter.Setter' (Either s s') a
valuechosen
  1. :: (Conjoined p, Functor f)
  2. => p a (f b)
  3. -> p (Either a a) (f (Either b b))
#

This is a Lens that updates either side of an Either, where both sides have the same type.

chosen ≡ choosing id id
Example1 expression
Left a^.chosena
Example1 expression
Right a^.chosena
Example1 expression
Right "hello"^.chosen"hello"
Example1 expression
Right a & chosen *~ bRight (a * b)
chosen :: Lens (Either a a) (Either b b) a b
chosen f (Left a)  = Left <$> f a
chosen f (Right a) = Right <$> f a
valuealongside
  1. :: LensLike (AlongsideLeft f b') s t a b
  2. -> LensLike (AlongsideRight f t) s' t' a' b'
  3. -> LensLike f (s, s') (t, t') (a, a') (b, b')
#

alongside makes a Lens from two other lenses or a Getter from two other getters by executing them on their respective halves of a product.

Example1 expression
(Left a, Right b)^.alongside chosen chosen(a,b)
Example1 expression
(Left a, Right b) & alongside chosen chosen .~ (c,d)(Left c,Right d)
alongside :: Lens   s t a b -> Lens   s' t' a' b' -> Lens   (s,s') (t,t') (a,a') (b,b')
alongside :: Getter s   a   -> Getter s'    a'    -> Getter (s,s')        (a,a')
valueinside
  1. :: Corepresentable p
  2. => ALens s t a b
  3. -> Lens (p e s) (p e t) (p e a) (p e b)
#

Lift a Lens so it can run under a function (or other corepresentable profunctor).

inside :: Lens s t a b -> Lens (e -> s) (e -> t) (e -> a) (e -> b)
Example1 expression
(\x -> (x-1,x+1)) ^. inside _1 $ 54
Example1 expression
runState (modify (1:) >> modify (2:)) ^. (inside _2) $ [][2,1]

Setting Functionally with Passthrough

26 declarations
value(<%~) :: LensLike (Tuple2 b) s t a b -> (a -> b) -> s -> (b, t)
#

Modify the target of a Lens and return the result.

When you do not need the result of the operation, (%~) is more flexible.

(<%~) ::             Lens s t a b      -> (a -> b) -> s -> (b, t)
(<%~) ::             Control.Lens.Iso.Iso s t a b       -> (a -> b) -> s -> (b, t)
(<%~) :: Monoid b => Control.Lens.Traversal.Traversal s t a b -> (a -> b) -> s -> (b, t)
value(<+~) :: Num a => LensLike (Tuple2 a) s t a a -> a -> s -> (a, t)
#

Increment the target of a numerically valued Lens and return the result.

When you do not need the result of the addition, (+~) is more flexible.

(<+~) :: Num a => Lens' s a -> a -> s -> (a, s)
(<+~) :: Num a => Control.Lens.Iso.Iso' s a  -> a -> s -> (a, s)
value(<-~) :: Num a => LensLike (Tuple2 a) s t a a -> a -> s -> (a, t)
#

Decrement the target of a numerically valued Lens and return the result.

When you do not need the result of the subtraction, (-~) is more flexible.

(<-~) :: Num a => Lens' s a -> a -> s -> (a, s)
(<-~) :: Num a => Control.Lens.Iso.Iso' s a  -> a -> s -> (a, s)
value(<*~) :: Num a => LensLike (Tuple2 a) s t a a -> a -> s -> (a, t)
#

Multiply the target of a numerically valued Lens and return the result.

When you do not need the result of the multiplication, (*~) is more flexible.

(<*~) :: Num a => Lens' s a -> a -> s -> (a, s)
(<*~) :: Num a => Control.Lens.Iso.Iso'  s a -> a -> s -> (a, s)
value(<//~) :: Fractional a => LensLike (Tuple2 a) s t a a -> a -> s -> (a, t)
#

Divide the target of a fractionally valued Lens and return the result.

When you do not need the result of the division, (//~) is more flexible.

(<//~) :: Fractional a => Lens' s a -> a -> s -> (a, s)
(<//~) :: Fractional a => Control.Lens.Iso.Iso'  s a -> a -> s -> (a, s)
value(<^~)
  1. :: (Num a, Integral e)
  2. => LensLike (Tuple2 a) s t a a
  3. -> e
  4. -> s
  5. -> (a, t)
#

Raise the target of a numerically valued Lens to a non-negative Integral power and return the result.

When you do not need the result of the operation, (^~) is more flexible.

(<^~) :: (Num a, Integral e) => Lens' s a -> e -> s -> (a, s)
(<^~) :: (Num a, Integral e) => Control.Lens.Iso.Iso' s a -> e -> s -> (a, s)
value(<**~) :: Floating a => LensLike (Tuple2 a) s t a a -> a -> s -> (a, t)
#

Raise the target of a floating-point valued Lens to an arbitrary power and return the result.

When you do not need the result of the operation, (**~) is more flexible.

(<**~) :: Floating a => Lens' s a -> a -> s -> (a, s)
(<**~) :: Floating a => Control.Lens.Iso.Iso' s a  -> a -> s -> (a, s)
value(<<>~) :: Semigroup m => LensLike (Tuple2 m) s t m m -> m -> s -> (m, t)
#

(<>) a Semigroup value onto the end of the target of a Lens and return the result.

When you do not need the result of the operation, (<>~) is more flexible.

value(<<>:~) :: Semigroup m => LensLike (Tuple2 m) s t m m -> m -> s -> (m, t)
#

(<>) a Semigroup value onto the front of the target of a Lens and return the result. However, unlike (<<>~), it is prepended to the head side.

When you do not need the result of the operation, (<>:~) is more flexible.

value(<<%~) :: LensLike (Tuple2 a) s t a b -> (a -> b) -> s -> (a, t)
#

Modify the target of a Lens, but return the old value.

When you do not need the old value, (%~) is more flexible.

(<<%~) ::             Lens s t a b      -> (a -> b) -> s -> (a, t)
(<<%~) ::             Control.Lens.Iso.Iso s t a b       -> (a -> b) -> s -> (a, t)
(<<%~) :: Monoid a => Control.Lens.Traversal.Traversal s t a b -> (a -> b) -> s -> (a, t)
value(<<.~) :: LensLike (Tuple2 a) s t a b -> b -> s -> (a, t)
#

Replace the target of a Lens, but return the old value.

When you do not need the old value, (.~) is more flexible.

(<<.~) ::             Lens s t a b      -> b -> s -> (a, t)
(<<.~) ::             Control.Lens.Iso.Iso s t a b       -> b -> s -> (a, t)
(<<.~) :: Monoid a => Control.Lens.Traversal.Traversal s t a b -> b -> s -> (a, t)
value(<<?~) :: LensLike (Tuple2 a) s t a (Maybe b) -> b -> s -> (a, t)
#

Replace the target of a Lens with a Just value, but return the old value.

If you do not need the old value (?~) is more flexible.

Example2 expressions
import qualified Data.Map as Map_2.at "hello" <<?~ "world" $ (42,Map.fromList [("goodnight","gracie")])(Nothing,(42,fromList [("goodnight","gracie"),("hello","world")]))
(<<?~) :: Iso s t a (Maybe b)       -> b -> s -> (a, t)
(<<?~) :: Lens s t a (Maybe b)      -> b -> s -> (a, t)
(<<?~) :: Traversal s t a (Maybe b) -> b -> s -> (a, t)
value(<<+~) :: Num a => LensLike' (Tuple2 a) s a -> a -> s -> (a, s)
#

Increment the target of a numerically valued Lens and return the old value.

When you do not need the old value, (+~) is more flexible.

Example1 expression
(a,b) & _1 <<+~ c(a,(a + c,b))
Example1 expression
(a,b) & _2 <<+~ c(b,(a,b + c))
(<<+~) :: Num a => Lens' s a -> a -> s -> (a, s)
(<<+~) :: Num a => Iso' s a -> a -> s -> (a, s)
value(<<-~) :: Num a => LensLike' (Tuple2 a) s a -> a -> s -> (a, s)
#

Decrement the target of a numerically valued Lens and return the old value.

When you do not need the old value, (-~) is more flexible.

Example1 expression
(a,b) & _1 <<-~ c(a,(a - c,b))
Example1 expression
(a,b) & _2 <<-~ c(b,(a,b - c))
(<<-~) :: Num a => Lens' s a -> a -> s -> (a, s)
(<<-~) :: Num a => Iso' s a -> a -> s -> (a, s)
value(<<*~) :: Num a => LensLike' (Tuple2 a) s a -> a -> s -> (a, s)
#

Multiply the target of a numerically valued Lens and return the old value.

When you do not need the old value, (-~) is more flexible.

Example1 expression
(a,b) & _1 <<*~ c(a,(a * c,b))
Example1 expression
(a,b) & _2 <<*~ c(b,(a,b * c))
(<<*~) :: Num a => Lens' s a -> a -> s -> (a, s)
(<<*~) :: Num a => Iso' s a -> a -> s -> (a, s)
value(<<//~) :: Fractional a => LensLike' (Tuple2 a) s a -> a -> s -> (a, s)
#

Divide the target of a numerically valued Lens and return the old value.

When you do not need the old value, (//~) is more flexible.

Example1 expression
(a,b) & _1 <<//~ c(a,(a / c,b))
Example1 expression
("Hawaii",10) & _2 <<//~ 2(10.0,("Hawaii",5.0))
(<<//~) :: Fractional a => Lens' s a -> a -> s -> (a, s)
(<<//~) :: Fractional a => Iso' s a -> a -> s -> (a, s)
value(<<**~) :: Floating a => LensLike' (Tuple2 a) s a -> a -> s -> (a, s)
#

Raise the target of a floating-point valued Lens to an arbitrary power and return the old value.

When you do not need the old value, (**~) is more flexible.

Example1 expression
(a,b) & _1 <<**~ c(a,(a**c,b))
Example1 expression
(a,b) & _2 <<**~ c(b,(a,b**c))
(<<**~) :: Floating a => Lens' s a -> a -> s -> (a, s)
(<<**~) :: Floating a => Iso' s a -> a -> s -> (a, s)
value(<<&&~) :: LensLike' (Tuple2 Bool) s Bool -> Bool -> s -> (Bool, s)
#

Logically && the target of a Bool-valued Lens and return the old value.

When you do not need the old value, (&&~) is more flexible.

Example1 expression
(False,6) & _1 <<&&~ True(False,(False,6))
Example1 expression
("hello",True) & _2 <<&&~ False(True,("hello",False))
(<<&&~) :: Lens' s Bool -> Bool -> s -> (Bool, s)
(<<&&~) :: Iso' s Bool -> Bool -> s -> (Bool, s)
value(<<<>~) :: Semigroup r => LensLike' (Tuple2 r) s r -> r -> s -> (r, s)
#

Modify the target of a monoidally valued Lens by using (<>) a new value and return the old value.

When you do not need the old value, (<>~) is more flexible.

Example1 expression
(Sum a,b) & _1 <<<>~ Sum c(Sum {getSum = a},(Sum {getSum = a + c},b))
Example1 expression
_2 <<<>~ ", 007" $ ("James", "Bond")("Bond",("James","Bond, 007"))
(<<<>~) :: Semigroup r => Lens' s r -> r -> s -> (r, s)
(<<<>~) :: Semigroup r => Iso' s r -> r -> s -> (r, s)
value(<<<>:~) :: Semigroup m => LensLike' (Tuple2 m) s m -> m -> s -> (m, s)
#

(<>) a Semigroup value onto the front of the target of a Lens and return the old result. However, unlike (<<>~), it is prepended to the head side.

When you do not need the result of the operation, (<>:~) is more flexible.

Setting State with Passthrough

27 declarations
value(<%=) :: MonadState s m => LensLike (Tuple2 b) s s a b -> (a -> b) -> m b
#

Modify the target of a Lens into your Monad's state by a user supplied function and return the result.

When applied to a Control.Lens.Traversal.Traversal, it this will return a monoidal summary of all of the intermediate results.

When you do not need the result of the operation, (%=) is more flexible.

(<%=) :: MonadState s m             => Lens' s a      -> (a -> a) -> m a
(<%=) :: MonadState s m             => Control.Lens.Iso.Iso' s a       -> (a -> a) -> m a
(<%=) :: (MonadState s m, Monoid a) => Control.Lens.Traversal.Traversal' s a -> (a -> a) -> m a
value(<+=) :: (MonadState s m, Num a) => LensLike' (Tuple2 a) s a -> a -> m a
#

Add to the target of a numerically valued Lens into your Monad's state and return the result.

When you do not need the result of the addition, (+=) is more flexible.

(<+=) :: (MonadState s m, Num a) => Lens' s a -> a -> m a
(<+=) :: (MonadState s m, Num a) => Control.Lens.Iso.Iso' s a -> a -> m a
value(<-=) :: (MonadState s m, Num a) => LensLike' (Tuple2 a) s a -> a -> m a
#

Subtract from the target of a numerically valued Lens into your Monad's state and return the result.

When you do not need the result of the subtraction, (-=) is more flexible.

(<-=) :: (MonadState s m, Num a) => Lens' s a -> a -> m a
(<-=) :: (MonadState s m, Num a) => Control.Lens.Iso.Iso' s a -> a -> m a
value(<*=) :: (MonadState s m, Num a) => LensLike' (Tuple2 a) s a -> a -> m a
#

Multiply the target of a numerically valued Lens into your Monad's state and return the result.

When you do not need the result of the multiplication, (*=) is more flexible.

(<*=) :: (MonadState s m, Num a) => Lens' s a -> a -> m a
(<*=) :: (MonadState s m, Num a) => Control.Lens.Iso.Iso' s a -> a -> m a
value(<<>:=)
  1. :: (MonadState s m, Semigroup r)
  2. => LensLike' (Tuple2 r) s r
  3. -> r
  4. -> m r
#

(<>) a Semigroup value onto the front of the target of a Lens into your Monad's state and return the result. However, unlike (<<>=), it is prepended to the head side.

When you do not need the result of the operation, (<>:=) is more flexible.

value(<<%=)
  1. :: (Strong p, MonadState s m)
  2. => Over p (Tuple2 a) s s a b
  3. -> p a b
  4. -> m a
#

Modify the target of a Lens into your Monad's state by a user supplied function and return the old value that was replaced.

When applied to a Control.Lens.Traversal.Traversal, this will return a monoidal summary of all of the old values present.

When you do not need the result of the operation, (%=) is more flexible.

(<<%=) :: MonadState s m             => Lens' s a      -> (a -> a) -> m a
(<<%=) :: MonadState s m             => Control.Lens.Iso.Iso' s a       -> (a -> a) -> m a
(<<%=) :: (MonadState s m, Monoid a) => Control.Lens.Traversal.Traversal' s a -> (a -> a) -> m a
(<<%=) :: MonadState s m => LensLike ((,)a) s s a b -> (a -> b) -> m a
value(<<.=) :: MonadState s m => LensLike (Tuple2 a) s s a b -> b -> m a
#

Replace the target of a Lens into your Monad's state with a user supplied value and return the old value that was replaced.

When applied to a Control.Lens.Traversal.Traversal, this will return a monoidal summary of all of the old values present.

When you do not need the result of the operation, (.=) is more flexible.

(<<.=) :: MonadState s m             => Lens' s a      -> a -> m a
(<<.=) :: MonadState s m             => Control.Lens.Iso.Iso' s a       -> a -> m a
(<<.=) :: (MonadState s m, Monoid a) => Control.Lens.Traversal.Traversal' s a -> a -> m a
value(<<?=) :: MonadState s m => LensLike (Tuple2 a) s s a (Maybe b) -> b -> m a
#

Replace the target of a Lens into your Monad's state with Just a user supplied value and return the old value that was replaced.

When applied to a Control.Lens.Traversal.Traversal, this will return a monoidal summary of all of the old values present.

When you do not need the result of the operation, (?=) is more flexible.

(<<?=) :: MonadState s m             => Lens s t a (Maybe b)      -> b -> m a
(<<?=) :: MonadState s m             => Control.Lens.Iso.Iso s t a (Maybe b)       -> b -> m a
(<<?=) :: (MonadState s m, Monoid a) => Control.Lens.Traversal.Traversal s t a (Maybe b) -> b -> m a
value(<<<>:=)
  1. :: (MonadState s m, Semigroup r)
  2. => LensLike' (Tuple2 r) s r
  3. -> r
  4. -> m r
#

(<>) a Semigroup value onto the front of the target of a Lens into your Monad's state and return the old result. However, unlike (<<<>=), it is prepended to the head side.

When you do not need the result of the operation, (<>:=) is more flexible.

value(<<~) :: MonadState s m => ALens s s a b -> m b -> m b
#

Run a monadic action, and set the target of Lens to its result.

(<<~) :: MonadState s m => Control.Lens.Iso.Iso s s a b   -> m b -> m b
(<<~) :: MonadState s m => Lens s s a b  -> m b -> m b

NB: This is limited to taking an actual Lens than admitting a Control.Lens.Traversal.Traversal because there are potential loss of state issues otherwise.

Cloning Lenses

3 declarations
valuecloneLens :: ALens s t a b -> Lens s t a b
#

Cloning a Lens is one way to make sure you aren't given something weaker, such as a Control.Lens.Traversal.Traversal and can be used as a way to pass around lenses that have to be monomorphic in f.

Note: This only accepts a proper Lens.

Example1 expression
let example l x = set (cloneLens l) (x^.cloneLens l + 1) x in example _2 ("hello",1,"you")("hello",2,"you")

Arrow operators

1 declaration
valueoverA :: Arrow ar => LensLike (Context a b) s t a b -> ar a b -> ar s t
#

over for Arrows.

Unlike over, overA can't accept a simple Control.Lens.Setter.Setter, but requires a full lens, or close enough.

Example1 expression
overA _1 ((+1) *** (+2)) ((1,2),6)((2,4),6)
overA :: Arrow ar => Lens s t a b -> ar a b -> ar s t

ALens Combinators

12 declarations
valuestoring :: ALens s t a b -> b -> s -> t
#

A version of set that works on ALens.

Example1 expression
storing _2 "world" ("hello","there")("hello","world")
value(^#) :: s -> ALens s t a b -> a
#

A version of (^.) that works on ALens.

Example1 expression
("hello","world")^#_2"world"
value(#~) :: ALens s t a b -> b -> s -> t
#

A version of (.~) that works on ALens.

Example1 expression
("hello","there") & _2 #~ "world"("hello","world")
value(#%~) :: ALens s t a b -> (a -> b) -> s -> t
#

A version of (%~) that works on ALens.

Example1 expression
("hello","world") & _2 #%~ length("hello",5)
value(#%%~) :: Functor f => ALens s t a b -> (a -> f b) -> s -> f t
#

A version of (%%~) that works on ALens.

Example1 expression
("hello","world") & _2 #%%~ \x -> (length x, x ++ "!")(5,("hello","world!"))
value(<#~) :: ALens s t a b -> b -> s -> (b, t)
#

A version of (<.~) that works on ALens.

Example1 expression
("hello","there") & _2 <#~ "world"("world",("hello","world"))
value(<#%~) :: ALens s t a b -> (a -> b) -> s -> (b, t)
#

A version of (<%~) that works on ALens.

Example1 expression
("hello","world") & _2 <#%~ length(5,("hello",5))

Common Lenses

4 declarations
valuedevoid :: Over p f Void Void a b
#

There is a field for every type in the Void. Very zen.

Example1 expression
[] & mapped.devoid +~ 1[]
Example1 expression
Nothing & mapped.devoid %~ absNothing
devoid :: Lens' Void a
valueunited :: Functor f => (() -> f ()) -> a -> f a
#

We can always retrieve a () from any type.

Example1 expression
"hello"^.united()
Example1 expression
"hello" & united .~ ()"hello"
valuehead1 :: Traversable1 t => Lens' (t a) a
#

A Lens focusing on the first element of a Traversable1 container.

Example1 expression
2 :| [3, 4] & head1 +~ 1012 :| [3,4]
Example1 expression
Identity True ^. head1True
valuelast1 :: Traversable1 t => Lens' (t a) a
#

A Lens focusing on the last element of a Traversable1 container.

Example1 expression
2 :| [3, 4] & last1 +~ 102 :| [3,14]
Example1 expression
Node 'a' [Node 'b' [], Node 'c' []] ^. last1'c'

Context

3 declarations
datadata Context a b t
#

The indexed store can be used to characterize a Control.Lens.Lens.Lens and is used by cloneLens.

Context a b t is isomorphic to newtype Context a b t = Context { runContext :: forall f. Functor f => (a -> f b) -> f t }, and to exists s. (s, Control.Lens.Lens.Lens s t a b).

A Context is like a Control.Lens.Lens.Lens that has already been applied to a some structure.

Constructors

Instances7IndexedComonad, IndexedComonadStore, IndexedFunctor, ComonadStore, Functor, Comonad, …

Lens fusion

1 declaration
valuefusing :: Functor f => LensLike (Yoneda f) s t a b -> LensLike f s t a b
#

Fuse a composition of lenses using Yoneda to provide fmap fusion.

In general, given a pair of lenses foo and bar

fusing (foo.bar) = foo.bar

however, foo and bar are either going to fmap internally or they are trivial.

fusing exploits the Yoneda lemma to merge these separate uses into a single fmap.

This is particularly effective when the choice of functor f is unknown at compile time or when the Lens foo.bar in the above description is recursive or complex enough to prevent inlining.

fusing :: Lens s t a b -> Lens s t a b