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

This module exports the majority of the types that need to appear in user signatures or in documentation when talking about lenses. The remaining types for consuming lenses are distributed across various modules in the hierarchy.

  • 53 types
  • Packagelens-5.3.5
  • Exports53
  • LanguageHaskell2010
  • LicenceBSD-2-Clause
  • SourceType.hs

Other

9 declarations
typetype Equality (s :: k1) (t :: k2) (a :: k1) (b :: k2) = forall k3 (p :: k1 -> k3 -> Type) (f :: k2 -> k3). p a (f b) -> p s (f t)
#

A witness that (a ~ s, b ~ t).

Note: Composition with an Equality is index-preserving.

typetype As (a :: k2) = Equality' a a
#

Composable asTypeOf. Useful for constraining excess polymorphism, foo . (id :: As Int) . bar.

typetype Iso s t a b = forall (p :: Type -> Type -> Type) (f :: Type -> Type). (Profunctor p, Functor f) => p a (f b) -> p s (f t)
#

Isomorphism families can be composed with another Lens using (.) and id.

Since every Iso is both a valid Lens and a valid Prism, the laws for those types imply the following laws for an Iso f:

f . from f ≡ id
from f . f ≡ id

Note: Composition with an Iso is index- and measure- preserving.

typetype Prism s t a b = forall (p :: Type -> Type -> Type) (f :: Type -> Type). (Choice p, Applicative f) => p a (f b) -> p s (f t)
#

A Prism l is a Traversal that can also be turned around with re to obtain a Getter in the opposite direction.

There are three laws that a Prism should satisfy:

First, if I re or review a value with a Prism and then preview or use (^?), I will get it back:

preview l (review l b) ≡ Just b

Second, if you can extract a value a using a Prism l from a value s, then the value s is completely described by l and a:

preview l s ≡ Just a ⟹ review l a ≡ s

Third, if you get non-match t, you can convert it result back to s:

Control.Lens.Combinators.matching l s ≡ Left t ⟹ Control.Lens.Combinators.matching l t ≡ Left s

The first two laws imply that the Traversal laws hold for every Prism and that we traverse at most 1 element:

lengthOf l x <= 1

It may help to think of this as an Iso that can be partial in one direction.

Every Prism is a valid Traversal.

Every Iso is a valid Prism.

For example, you might have a Prism' Integer Numeric.Natural.Natural allows you to always go from a Numeric.Natural.Natural to an Integer, and provide you with tools to check if an Integer is a Numeric.Natural.Natural and/or to edit one if it is.

nat :: Prism' Integer Numeric.Natural.Natural
nat = prism toInteger $ \ i ->
   if i < 0
   then Left i
   else Right (fromInteger i)

Now we can ask if an Integer is a Numeric.Natural.Natural.

Example1 expression
5^?natJust 5
Example1 expression
(-5)^?natNothing

We can update the ones that are:

Example1 expression
(-3,4) & both.nat *~ 2(-3,8)

And we can then convert from a Numeric.Natural.Natural to an Integer.

Example1 expression
5 ^. re nat -- :: Natural5

Similarly we can use a Prism to traverse the Left half of an Either:

Example1 expression
Left "hello" & _Left %~ lengthLeft 5

or to construct an Either:

Example1 expression
5^.re _LeftLeft 5

such that if you query it with the Prism, you will get your original input back.

Example1 expression
5^.re _Left ^? _LeftJust 5

Another interesting way to think of a Prism is as the categorical dual of a Lens -- a co-Lens, so to speak. This is what permits the construction of outside.

Note: Composition with a Prism is index-preserving.

Lenses, Folds and Traversals

11 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
typetype Traversal s t a b = forall (f :: Type -> Type). Applicative f => (a -> f b) -> s -> f t
#

A Traversal can be used directly as a Control.Lens.Setter.Setter or a Fold (but not as a Lens) and provides the ability to both read and update multiple fields, subject to some relatively weak Traversal laws.

These have also been known as multilenses, but they have the signature and spirit of

traverse :: Traversable f => Traversal (f a) (f b) a b

and the more evocative name suggests their application.

Most of the time the Traversal you will want to use is just traverse, but you can also pass any Lens or Iso as a Traversal, and composition of a Traversal (or Lens or Iso) with a Traversal (or Lens or Iso) using (.) forms a valid Traversal.

The laws for a Traversal t follow from the laws for Traversable as stated in "The Essence of the Iterator Pattern".

t pure ≡ pure
fmap (t f) . t g ≡ getCompose . t (Compose . fmap f . g)

One consequence of this requirement is that a Traversal needs to leave the same number of elements as a candidate for subsequent Traversal that it started with. Another testament to the strength of these laws is that the caveat expressed in section 5.5 of the "Essence of the Iterator Pattern" about exotic Traversable instances that traverse the same entry multiple times was actually already ruled out by the second law in that same paper!

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 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 Fold s a = forall (f :: Type -> Type). (Contravariant f, Applicative f) => (a -> f a) -> s -> f s
#

A Fold describes how to retrieve multiple values in a way that can be composed with other LensLike constructions.

A Fold s a provides a structure with operations very similar to those of the Foldable typeclass, see foldMapOf and the other Fold combinators.

By convention, if there exists a foo method that expects a Foldable (f a), then there should be a fooOf method that takes a Fold s a and a value of type s.

A Getter is a legal Fold that just ignores the supplied Monoid.

Unlike a Control.Lens.Traversal.Traversal a Fold is read-only. Since a Fold cannot be used to write back there are no Lens laws that apply.

Indexed

11 declarations
typetype IndexedTraversal i s t a b = forall (p :: Type -> Type -> Type) (f :: Type -> Type). (Indexable i p, Applicative f) => p a (f b) -> s -> f t
#

Every IndexedTraversal is a valid Traversal or Control.Lens.Fold.IndexedFold.

The Indexed constraint is used to allow an IndexedTraversal to be used directly as a Traversal.

The Traversal laws are still required to hold.

In addition, the index i should satisfy the requirement that it stays unchanged even when modifying the value a, otherwise traversals like indices break the Traversal laws.

Index-Preserving

11 declarations

Common

11 declarations
typetype Simple (f :: k1 -> k1 -> k2 -> k2 -> k) (s :: k1) (a :: k2) = f s s a a
#

A Simple Lens, Simple Traversal, ... can be used instead of a Lens,Traversal, ... whenever the type variables don't change upon setting a value.

_imagPart :: Simple Lens (Complex a) a
traversed :: Simple (IndexedTraversal Int) [a] a

Note: To use this alias in your own code with LensLike f or Setter, you may have to turn on LiberalTypeSynonyms.

This is commonly abbreviated as a "prime" marker, e.g. Lens' = Simple Lens.

typetype LensLike (f :: k -> Type) s (t :: k) a (b :: k) = (a -> f b) -> s -> f t
#

Many combinators that accept a Lens can also accept a Traversal in limited situations.

They do so by specializing the type of Functor that they require of the caller.

If a function accepts a LensLike f s t a b for some Functor f, then they may be passed a Lens.

Further, if f is an Applicative, they may also be passed a Traversal.

typetype Over (p :: k -> Type -> Type) (f :: k1 -> Type) s (t :: k1) (a :: k) (b :: k1) = p a (f b) -> s -> f t
#

This is a convenient alias for use when you need to consume either indexed or non-indexed lens-likes based on context.

typetype Over' (p :: Type -> Type -> Type) (f :: Type -> Type) s a = Over p f s s a a
#

This is a convenient alias for use when you need to consume either indexed or non-indexed lens-likes based on context.

type Over' p f = Simple (Over p f)
typetype IndexedLensLike i (f :: k -> Type) s (t :: k) a (b :: k) = forall (p :: Type -> Type -> Type). Indexable i p => p a (f b) -> s -> f t
#

Convenient alias for constructing indexed lenses and their ilk.

typetype Optical (p :: k -> k1 -> Type) (q :: k2 -> k1 -> Type) (f :: k3 -> k1) (s :: k2) (t :: k3) (a :: k) (b :: k3) = p a (f b) -> q s (f t)
#
type LensLike f s t a b = Optical (->) (->) f s t a b
type Over p f s t a b = Optical p (->) f s t a b
type Optic p f s t a b = Optical p p f s t a b