HORIZON HASKELLDocslts/ghc-9.10.x248f8f02026-10-05Search names, modules, packages, or :: a typeCtrl K

GHC 9.10.3 · lts/ghc-9.10.x · 248f8f0 · 2026-10-05

Modulelens-5.3.5Haskell2010

Control.Lens.Getter

A Getter s a is just any function (s -> a), which we've flipped into continuation passing style, (a -> r) -> s -> r and decorated with Const to obtain:

type Getting r s a = (a -> Const r a) -> s -> Const r s

If we restrict access to knowledge about the type r, we could get:

type Getter s a = forall r. Getting r s a

However, for Getter (but not for Getting) we actually permit any functor f which is an instance of both Functor and Contravariant:

type Getter s a = forall f. (Contravariant f, Functor f) => (a -> f a) -> s -> f s

Everything you can do with a function, you can do with a Getter, but note that because of the continuation passing style (.) composes them in the opposite order.

Since it is only a function, every Getter obviously only retrieves a single value for a given input.

A common question is whether you can combine multiple Getters to retrieve multiple values. Recall that all Getters are Folds and that we have a Monoid m => Applicative (Const m) instance to play with. Knowing this, we can use Data.Semigroup.<> to glue Folds together:

Example1 expression
(1, 2, 3, 4, 5) ^.. (_2 <> _3 <> _5)[2,3,5]
  • 6 types
  • 1 class
  • 19 values
  • Packagelens-5.3.5
  • Exports26
  • LanguageHaskell2010
  • LicenceBSD-2-Clause
  • SourceGetter.hs

Getters

5 declarations
typetype Getter s a = forall (f :: Type -> Type). (Contravariant f, Functor f) => (a -> f a) -> s -> f s
#

A Getter describes how to retrieve a single value in a way that can be composed with other LensLike constructions.

Unlike a Lens a Getter is read-only. Since a Getter cannot be used to write back there are no Lens laws that can be applied to it. In fact, it is isomorphic to an arbitrary function from (s -> a).

Moreover, a Getter can be used directly as a Control.Lens.Fold.Fold, since it just ignores the Applicative.

typetype Getting r s a = (a -> Const r a) -> s -> Const r s
#

When you see this in a type signature it indicates that you can pass the function a Lens, Getter, Control.Lens.Traversal.Traversal, Control.Lens.Fold.Fold, Control.Lens.Prism.Prism, Control.Lens.Iso.Iso, or one of the indexed variants, and it will just "do the right thing".

Most Getter combinators are able to be used with both a Getter or a Control.Lens.Fold.Fold in limited situations, to do so, they need to be monomorphic in what we are going to extract with Const. To be compatible with Lens, Control.Lens.Traversal.Traversal and Control.Lens.Iso.Iso we also restricted choices of the irrelevant t and b parameters.

If a function accepts a Getting r s a, then when r is a Monoid, then you can pass a Control.Lens.Fold.Fold (or Control.Lens.Traversal.Traversal), otherwise you can only pass this a Getter or Lens.

typetype Accessing (p :: Type -> Type -> Type) m s a = p a (Const m a) -> s -> Const m s
#

This is a convenient alias used when consuming (indexed) getters and (indexed) folds in a highly general fashion.

Building Getters

4 declarations
valueto :: (Profunctor p, Contravariant f) => (s -> a) -> Optic' p f s a
#

Build an (index-preserving) Getter from an arbitrary Haskell function.

to f . to g ≡ to (g . f)
a ^. to f ≡ f a
Example1 expression
a ^.to ff a
Example1 expression
("hello","world")^.to snd"world"
Example1 expression
5^.to succ6
Example1 expression
(0, -5)^._2.to abs5
to :: (s -> a) -> IndexPreservingGetter s a

Combinators for Getters and Folds

7 declarations
value(^.) :: s -> Getting a s a -> a
#

View the value pointed to by a Getter or Lens or the result of folding over all the results of a Control.Lens.Fold.Fold or Control.Lens.Traversal.Traversal that points at a monoidal values.

This is the same operation as view with the arguments flipped.

The fixity and semantics are such that subsequent field accesses can be performed with (Prelude..).

Example1 expression
(a,b)^._2b
Example1 expression
("hello","world")^._2"world"
Example2 expressions
import Data.Complex((0, 1 :+ 2), 3)^._1._2.to magnitude2.23606797749979
(^.) ::             s -> Getter s a     -> a
(^.) :: Monoid m => s -> Control.Lens.Fold.Fold s m       -> m
(^.) ::             s -> Control.Lens.Iso.Iso' s a       -> a
(^.) ::             s -> Lens' s a      -> a
(^.) :: Monoid m => s -> Control.Lens.Traversal.Traversal' s m -> m
valueview :: MonadReader s m => Getting a s a -> m a
#

View the value pointed to by a Getter, Control.Lens.Iso.Iso or Lens or the result of folding over all the results of a Fold or Control.Lens.Traversal.Traversal that points at a monoidal value.

view . to ≡ id
Example1 expression
view (to f) af a
Example1 expression
view _2 (1,"hello")"hello"
Example1 expression
view (to succ) 56
Example1 expression
view (_2._1) ("hello",("world","!!!"))"world"

As view is commonly used to access the target of a Getter or obtain a monoidal summary of the targets of a Fold, It may be useful to think of it as having one of these more restricted signatures:

view ::             Getter s a     -> s -> a
view :: Monoid m => Fold s m       -> s -> m
view ::             Control.Lens.Iso.Iso' s a       -> s -> a
view ::             Lens' s a      -> s -> a
view :: Monoid m => Control.Lens.Traversal.Traversal' s m -> s -> m

In a more general setting, such as when working with a Monad transformer stack you can use:

view :: MonadReader s m             => Getter s a     -> m a
view :: (MonadReader s m, Monoid a) => Fold s a       -> m a
view :: MonadReader s m             => Control.Lens.Iso.Iso' s a       -> m a
view :: MonadReader s m             => Lens' s a      -> m a
view :: (MonadReader s m, Monoid a) => Control.Lens.Traversal.Traversal' s a -> m a
valueviews :: MonadReader s m => LensLike' (Const r) s a -> (a -> r) -> m r
#

View a function of the value pointed to by a Getter or Lens or the result of folding over the result of mapping the targets of a Fold or Control.Lens.Traversal.Traversal.

views l f ≡ view (l . to f)
Example1 expression
views (to f) g ag (f a)
Example1 expression
views _2 length (1,"hello")5

As views is commonly used to access the target of a Getter or obtain a monoidal summary of the targets of a Fold, It may be useful to think of it as having one of these more restricted signatures:

views ::             Getter s a     -> (a -> r) -> s -> r
views :: Monoid m => Fold s a       -> (a -> m) -> s -> m
views ::             Control.Lens.Iso.Iso' s a       -> (a -> r) -> s -> r
views ::             Lens' s a      -> (a -> r) -> s -> r
views :: Monoid m => Control.Lens.Traversal.Traversal' s a -> (a -> m) -> s -> m

In a more general setting, such as when working with a Monad transformer stack you can use:

views :: MonadReader s m             => Getter s a     -> (a -> r) -> m r
views :: (MonadReader s m, Monoid r) => Fold s a       -> (a -> r) -> m r
views :: MonadReader s m             => Control.Lens.Iso.Iso' s a       -> (a -> r) -> m r
views :: MonadReader s m             => Lens' s a      -> (a -> r) -> m r
views :: (MonadReader s m, Monoid r) => Control.Lens.Traversal.Traversal' s a -> (a -> r) -> m r
views :: MonadReader s m => Getting r s a -> (a -> r) -> m r
valueuse :: MonadState s m => Getting a s a -> m a
#

Use the target of a Lens, Control.Lens.Iso.Iso, or Getter in the current state, or use a summary of a Control.Lens.Fold.Fold or Control.Lens.Traversal.Traversal that points to a monoidal value.

Example1 expression
evalState (use _1) (a,b)a
Example1 expression
evalState (use _1) ("hello","world")"hello"
use :: MonadState s m             => Getter s a     -> m a
use :: (MonadState s m, Monoid r) => Control.Lens.Fold.Fold s r       -> m r
use :: MonadState s m             => Control.Lens.Iso.Iso' s a       -> m a
use :: MonadState s m             => Lens' s a      -> m a
use :: (MonadState s m, Monoid r) => Control.Lens.Traversal.Traversal' s r -> m r
valueuses :: MonadState s m => LensLike' (Const r) s a -> (a -> r) -> m r
#

Use the target of a Lens, Control.Lens.Iso.Iso or Getter in the current state, or use a summary of a Control.Lens.Fold.Fold or Control.Lens.Traversal.Traversal that points to a monoidal value.

Example1 expression
evalState (uses _1 length) ("hello","world")5
uses :: MonadState s m             => Getter s a     -> (a -> r) -> m r
uses :: (MonadState s m, Monoid r) => Control.Lens.Fold.Fold s a       -> (a -> r) -> m r
uses :: MonadState s m             => Lens' s a      -> (a -> r) -> m r
uses :: MonadState s m             => Control.Lens.Iso.Iso' s a       -> (a -> r) -> m r
uses :: (MonadState s m, Monoid r) => Control.Lens.Traversal.Traversal' s a -> (a -> r) -> m r
uses :: MonadState s m => Getting r s t a b -> (a -> r) -> m r
valuelistening :: MonadWriter w m => Getting u w u -> m a -> m (a, u)
#

This is a generalized form of listen that only extracts the portion of the log that is focused on by a Getter. If given a Fold or a Traversal then a monoidal summary of the parts of the log that are visited will be returned.

listening :: MonadWriter w m             => Getter w u     -> m a -> m (a, u)
listening :: MonadWriter w m             => Lens' w u      -> m a -> m (a, u)
listening :: MonadWriter w m             => Iso' w u       -> m a -> m (a, u)
listening :: (MonadWriter w m, Monoid u) => Fold w u       -> m a -> m (a, u)
listening :: (MonadWriter w m, Monoid u) => Traversal' w u -> m a -> m (a, u)
listening :: (MonadWriter w m, Monoid u) => Prism' w u     -> m a -> m (a, u)
valuelistenings
  1. :: MonadWriter w m
  2. => Getting v w u
  3. -> u -> v
  4. -> m a
  5. -> m (a, v)
#

This is a generalized form of listen that only extracts the portion of the log that is focused on by a Getter. If given a Fold or a Traversal then a monoidal summary of the parts of the log that are visited will be returned.

listenings :: MonadWriter w m             => Getter w u     -> (u -> v) -> m a -> m (a, v)
listenings :: MonadWriter w m             => Lens' w u      -> (u -> v) -> m a -> m (a, v)
listenings :: MonadWriter w m             => Iso' w u       -> (u -> v) -> m a -> m (a, v)
listenings :: (MonadWriter w m, Monoid v) => Fold w u       -> (u -> v) -> m a -> m (a, v)
listenings :: (MonadWriter w m, Monoid v) => Traversal' w u -> (u -> v) -> m a -> m (a, v)
listenings :: (MonadWriter w m, Monoid v) => Prism' w u     -> (u -> v) -> m a -> m (a, v)

Indexed Getters

0 declarations

Indexed Getter Combinators

value(^@.) :: s -> IndexedGetting i (i, a) s a -> (i, a)
#

View the index and value of an IndexedGetter or IndexedLens.

This is the same operation as iview with the arguments flipped.

The fixity and semantics are such that subsequent field accesses can be performed with (Prelude..).

(^@.) :: s -> IndexedGetter i s a -> (i, a)
(^@.) :: s -> IndexedLens' i s a  -> (i, a)

The result probably doesn't have much meaning when applied to an IndexedFold.

valueiview :: MonadReader s m => IndexedGetting i (i, a) s a -> m (i, a)
#

View the index and value of an IndexedGetter into the current environment as a pair.

When applied to an IndexedFold the result will most likely be a nonsensical monoidal summary of the indices tupled with a monoidal summary of the values and probably not whatever it is you wanted.

valueiuse :: MonadState s m => IndexedGetting i (i, a) s a -> m (i, a)
#

Use the index and value of an IndexedGetter into the current state as a pair.

When applied to an IndexedFold the result will most likely be a nonsensical monoidal summary of the indices tupled with a monoidal summary of the values and probably not whatever it is you wanted.

valueilistening
  1. :: MonadWriter w m
  2. => IndexedGetting i (i, u) w u
  3. -> m a
  4. -> m (a, (i, u))
#

This is a generalized form of listen that only extracts the portion of the log that is focused on by a Getter. If given a Fold or a Traversal then a monoidal summary of the parts of the log that are visited will be returned.

ilistening :: MonadWriter w m             => IndexedGetter i w u     -> m a -> m (a, (i, u))
ilistening :: MonadWriter w m             => IndexedLens' i w u      -> m a -> m (a, (i, u))
ilistening :: (MonadWriter w m, Monoid u) => IndexedFold i w u       -> m a -> m (a, (i, u))
ilistening :: (MonadWriter w m, Monoid u) => IndexedTraversal' i w u -> m a -> m (a, (i, u))
valueilistenings
  1. :: MonadWriter w m
  2. => IndexedGetting i v w u
  3. -> i -> u -> v
  4. -> m a
  5. -> m (a, v)
#

This is a generalized form of listen that only extracts the portion of the log that is focused on by a Getter. If given a Fold or a Traversal then a monoidal summary of the parts of the log that are visited will be returned.

ilistenings :: MonadWriter w m             => IndexedGetter w u     -> (i -> u -> v) -> m a -> m (a, v)
ilistenings :: MonadWriter w m             => IndexedLens' w u      -> (i -> u -> v) -> m a -> m (a, v)
ilistenings :: (MonadWriter w m, Monoid v) => IndexedFold w u       -> (i -> u -> v) -> m a -> m (a, v)
ilistenings :: (MonadWriter w m, Monoid v) => IndexedTraversal' w u -> (i -> u -> v) -> m a -> m (a, v)

Implementation Details

3 declarations
classclass Contravariant (f :: Type -> Type) where
#

The class of contravariant functors.

Whereas in Haskell, one can think of a Functor as containing or producing values, a contravariant functor is a functor that can be thought of as consuming values.

As an example, consider the type of predicate functions a -> Bool. One such predicate might be negative x = x < 0, which classifies integers as to whether they are negative. However, given this predicate, we can re-use it in other situations, providing we have a way to map values to integers. For instance, we can use the negative predicate on a person's bank balance to work out if they are currently overdrawn:

newtype Predicate a = Predicate { getPredicate :: a -> Bool }

instance Contravariant Predicate where
  contramap :: (a' -> a) -> (Predicate a -> Predicate a')
  contramap f (Predicate p) = Predicate (p . f)
                                         |   `- First, map the input...
                                         `----- then apply the predicate.

overdrawn :: Predicate Person
overdrawn = contramap personBankBalance negative

Any instance should be subject to the following laws:

Identity

contramap id = id

Composition

contramap (g . f) = contramap f . contramap g

Note, that the second law follows from the free theorem of the type of contramap and the first law, so you need only check that the former condition holds.

Methods

  • contramap :: (a' -> a) -> f a -> f a'
  • (>$) :: b -> f b -> f ainfixl 4

    Replace all locations in the output with the same value. The default definition is contramap . const, but this may be overridden with a more efficient version.

Instances51Contravariant, …
newtypenewtype Const a (b :: k)
#

The Const functor.

Examples
Example1 expression
fmap (++ "World") (Const "Hello")Const "Hello"

Because we ignore the second type parameter to Const, the Applicative instance, which has (<*>) :: Monoid m => Const m (a -> b) -> Const m a -> Const m b essentially turns into Monoid m => m -> m -> m, which is (<>)

Example1 expression
Const [1, 2, 3] <*> Const [4, 5, 6]Const [1,2,3,4,5,6]

Constructors

Instances72Semigroupoid, Generic1, FoldableWithIndex, FunctorWithIndex, TraversableWithIndex, Bifoldable, …