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-family-core-2.1.3Haskell2010

Lens.Family

This is the main module for end-users of lens-families-core. If you are not building your own optics such as lenses, traversals, grates, etc., but just using optics made by others, this is the only module you need.

  • 24 types
  • 1 class
  • 34 values

Lenses

0 declarations

This module provides ^. for accessing fields and .~ and %~ for setting and modifying fields. Lenses are composed with . from the Prelude and id is the identity lens.

Lens composition in this library enjoys the following identities.

  • x^.l1.l2 === x^.l1^.l2
  • l1.l2 %~ f === l1 %~ l2 %~ f

The identity lens behaves as follows.

  • x^.id === x
  • id %~ f === f

The & operator, allows for a convenient way to sequence record updating:

record & l1 .~ value1 & l2 .~ value2

Lenses are implemented in van Laarhoven style. Lenses have type Functor f => (a -> f a) -> s -> f s and lens families have type Functor f => (a i -> f (a j)) -> s i -> f (s j).

Keep in mind that lenses and lens families can be used directly for functorial updates. For example, _2 id gives you strength.

_2 id :: Functor f => (a, f b) -> f (a, b)

Here is an example of code that uses the Maybe functor to preserves sharing during update when possible.

-- | 'sharedUpdate' returns the *identical* object if the update doesn't change anything.
-- This is useful for preserving sharing.
sharedUpdate :: Eq a => LensLike' Maybe s a -> (a -> a) -> s -> s
sharedUpdate l f s = fromMaybe s (l f' s)
 where
  f' a | b == a    = Nothing
       | otherwise = Just b
   where
    b = f a

Traversals

0 declarations

^. can be used with traversals to access monoidal fields. The result will be a Data.Monid.mconcat of all the fields referenced. The various fooOf functions can be used to access different monoidal summaries of some kinds of values.

^? can be used to access the first value of a traversal. Nothing is returned when the traversal has no references.

^.. can be used with a traversals and will return a list of all fields referenced.

When .~ is used with a traversal, all referenced fields will be set to the same value, and when %~ is used with a traversal, all referenced fields will be modified with the same function.

A variant of ^? call matching returns Either a Right value which is the first value of the traversal, or a Left value which is a "proof" that the traversal has no elements. The "proof" consists of the original input structure, but in the case of polymorphic families, the type parameter is replaced with a fresh type variable, thus proving that the type parameter was unused.

Like all optics, traversals can be composed with ., and because every lens is automatically a traversal, lenses and traversals can be composed with . yielding a traversal.

Traversals are implemented in van Laarhoven style. Traversals have type Applicative f => (a -> f a) -> s -> f s and traversal families have type Applicative f => (a i -> f (a j)) -> s i -> f (s j).

Grates

0 declarations

zipWithOf can be used with grates to zip two structure together provided a binary operation.

under can be used to modify each value in a structure according to a function. This works analogous to how over works for lenses and traversals.

review can be used with grates to construct a constant grate from a single value. This is like a 0-ary zipWith function.

degrating can be used to build higher arity zipWithOf functions:

zipWith3Of :: AGrate s t a b -> (a -> a -> a -> b) -> s -> s -> s -> t
zipWith3Of l f s1 s2 s3 = degrating l (\k -> f (k s1) (k s2) (k s3))

Like all optics, grates can be composed with ., and id is the identity grate.

Grates are implemented in van Laarhoven style.

Grates have type Functor g => (g a -> a) -> g s -> s and grate families have type Functor g => (g (a i) -> a j) -> g (s i) -> s j.

Keep in mind that grates and grate families can be used directly for functorial zipping. For example,

both sum :: Num a => [(a, a)] -> (a, a)

will take a list of pairs return the sum of the first components and the sum of the second components. For another example,

cod id :: Functor f => f (r -> a) -> r -> f a

will turn a functor full of functions into a function returning a functor full of results.

Adapters, Grids, and Prisms

0 declarations

The Adapter, Prism, and Grid optics are all AdapterLike optics and typically not used directly, but either converted to a LensLike optic using under, or into a GrateLike optic using over. See under and over for details about which conversions are possible.

These optics are implemented in van Laarhoven style.

Keep in mind that these optics and their families can sometimes be used directly, without using over and under. Sometimes you can take advantage of the fact that

   LensLike f (g s) t (g a) b
  ==
   AdapterLike f g s t a b
  ==
   GrateLike g s (f t) a (f b)

For example, if you have a grid for your structure to another type that has an Arbitray instance, such as grid from a custom word type to Bool, e.g. myWordBitVector :: (Applicative f, Functor g) => AdapterLike' f g MyWord Bool, you can use the grid to create an Arbitrary instance for your structure by directly applying review:

instance Arbitrary MyWord where
  arbitrary = review myWordBitVector arbitrary

Building and Finding Optics

0 declarations

Documentation

27 declarations
valueto :: Phantom f => (s -> a) -> LensLike f s t a b
#
to :: (s -> a) -> Getter s t a b

to promotes a projection function to a read-only lens called a getter. To demote a lens to a projection function, use the section (^.l) or view l.

Example1 expression
(3 :+ 4, "example")^._1.to(abs)5.0 :+ 0.0
valueview :: FoldLike a s t a b -> s -> a
#
view :: Getter s t a b -> s -> a

Demote a lens or getter to a projection function.

view :: Monoid a => Fold s t a b -> s -> a

Returns the monoidal summary of a traversal or a fold.

value(^.) :: s -> FoldLike a s t a b -> a
#
(^.) :: s -> Getter s t a b -> a

Access the value referenced by a getter or lens.

(^.) :: Monoid a => s -> Fold s t a b -> a

Access the monoidal summary referenced by a traversal or a fold.

valuefolding
  1. :: (Foldable g, Phantom f, Applicative f)
  2. => s -> g a
  3. -> LensLike f s t a b
#
folding :: (s -> [a]) -> Fold s t a b

folding promotes a "toList" function to a read-only traversal called a fold.

To demote a traversal or fold to a "toList" function use the section (^..l) or toListOf l.

valueviews :: FoldLike r s t a b -> (a -> r) -> s -> r
#
views :: Monoid r => Fold s t a b -> (a -> r) -> s -> r

Given a fold or traversal, return the foldMap of all the values using the given function.

views :: Getter s t a b -> (a -> r) -> s -> r

views is not particularly useful for getters or lenses, but given a getter or lens, it returns the referenced value passed through the given function.

views l f s = f (view l s)
value(^..) :: s -> FoldLike [a] s t a b -> [a]
#
(^..) :: s -> Fold s t a b -> [a]

Returns a list of all of the referenced values in order.

value(^?) :: s -> FoldLike (First a) s t a b -> Maybe a
#
(^?) :: s -> Fold s t a b -> Maybe a

Returns Just the first referenced value. Returns Nothing if there are no referenced values.

valuetoListOf :: FoldLike [a] s t a b -> s -> [a]
#
toListOf :: Fold s t a b -> s -> [a]

Returns a list of all of the referenced values in order.

valueallOf :: FoldLike All s t a b -> (a -> Bool) -> s -> Bool
#
allOf :: Fold s t a b -> (a -> Bool) -> s -> Bool

Returns true if all of the referenced values satisfy the given predicate.

valueanyOf :: FoldLike Any s t a b -> (a -> Bool) -> s -> Bool
#
anyOf :: Fold s t a b -> (a -> Bool) -> s -> Bool

Returns true if any of the referenced values satisfy the given predicate.

valuefirstOf :: FoldLike (First a) s t a b -> s -> Maybe a
#
firstOf :: Fold s t a b -> s -> Maybe a

Returns Just the first referenced value. Returns Nothing if there are no referenced values. See ^? for an infix version of firstOf

valuelastOf :: FoldLike (Last a) s t a b -> s -> Maybe a
#
lastOf :: Fold s t a b -> s -> Maybe a

Returns Just the last referenced value. Returns Nothing if there are no referenced values.

valuesumOf :: Num a => FoldLike (Sum a) s t a b -> s -> a
#
sumOf :: Num a => Fold s t a b -> s -> a

Returns the sum of all the referenced values.

valueproductOf :: Num a => FoldLike (Product a) s t a b -> s -> a
#
productOf :: Num a => Fold s t a b -> s -> a

Returns the product of all the referenced values.

valuelengthOf :: Num r => FoldLike (Sum r) s t a b -> s -> r
#
lengthOf :: Num r => Fold s t a b -> s -> r

Counts the number of references in a traversal or fold for the input.

valuenullOf :: FoldLike All s t a b -> s -> Bool
#
nullOf :: Fold s t a b -> s -> Bool

Returns true if the number of references in the input is zero.

valuematching :: LensLike (Either a) s t a b -> s -> Either t a
#
matching :: Traversal s t a b -> s -> Either t a

Returns Right of the first referenced value. Returns Left the original value when there are no referenced values. In case there are no referenced values, the result might have a fresh type parameter, thereby proving the original value had no referenced values.

valueover :: ASetter s t a b -> (a -> b) -> s -> t
#
over :: Setter s t a b -> (a -> b) -> s -> t

Demote a setter to a semantic editor combinator.

over :: Prism s t a b -> Reviwer s t a b
over :: Grid s t a b -> Grate s t a b
over :: Adapter s t a b -> Grate s t a b

Covert an AdapterLike optic into a GrateLike optic.

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

Modify all referenced fields.

valueset :: ASetter s t a b -> b -> s -> t
#

Set all referenced fields to the given value.

value(.~) :: ASetter s t a b -> b -> s -> t
#

Set all referenced fields to the given value.

valuereview :: GrateLike (Constant ()) s t a b -> b -> t
#
review :: Grate s t a b -> b -> t
review :: Reviewer s t a b -> b -> t
valuezipWithOf
  1. :: GrateLike (Prod Identity Identity) s t a b
  2. -> a -> a -> b
  3. -> s
  4. -> s
  5. -> t
#
zipWithOf :: Grate s t a b -> (a -> a -> b) -> s -> s -> t

Returns a binary instance of a grate.

zipWithOf l f x y = degrating l (k -> f (k x) (k y))
valuedegrating :: AGrate s t a b -> ((s -> a) -> b) -> t
#
degrating :: Grate s t a b -> ((s -> a) -> b) -> t

Demote a grate to its normal, higher-order function, form.

degrating . grate = id
grate . degrating = id
valueunder :: AResetter s t a b -> (a -> b) -> s -> t
#
under :: Resetter s t a b -> (a -> b) -> s -> t

Demote a resetter to a semantic editor combinator.

under :: Prism s t a b -> Traversal s t a b
under :: Grid s t a b -> Traversal s t a b
under :: Adapter s t a b -> Lens s t a b

Covert an AdapterLike optic into a LensLike optic.

Note: this function is unrelated to the lens package's under function.

valuereset :: AResetter s t a b -> b -> s -> t
#
reset :: Resetter s t a b -> b -> s -> t

Set all referenced fields to the given value.

value(&) :: s -> (s -> t) -> t
#

A flipped version of ($).

Pseudo-imperatives

7 declarations
value(<>~) :: Monoid a => ASetter s t a a -> a -> s -> t
#

Monoidally append a value to all referenced fields.

Types

18 declarations
newtypenewtype PCont i j a
#
Instances1Functor
  • Functor (PCont i j)Defined in lens-family-core-2.1.3 · Lens.Family
newtypenewtype First a
#
Instances2Semigroup, Monoid
  • Semigroup (First a)Defined in lens-family-core-2.1.3 · Lens.Family
  • Monoid (First a)Defined in lens-family-core-2.1.3 · Lens.Family
newtypenewtype Last a
#
Instances2Semigroup, Monoid
  • Semigroup (Last a)Defined in lens-family-core-2.1.3 · Lens.Family
  • Monoid (Last a)Defined in lens-family-core-2.1.3 · Lens.Family
classclass Functor f => Phantom (f :: Type -> Type) where
#
Instances8Phantom, …

Re-exports

7 declarations
newtypenewtype Constant a (b :: k)
#

Constant functor.

Instances28Generic1, Bifoldable, Bifunctor, Bitraversable, Eq2, Ord2, …
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
Instances40Monad, Functor, MonadFix, Applicative, Foldable, Traversable, …
newtypenewtype All
#

Boolean monoid under conjunction (&&).

All x <> All y = All (x && y)
Examples
Example1 expression
All True <> mempty <> All False)All {getAll = False}
Example1 expression
mconcat (map (\x -> All (even x)) [2,4,6,7,8])All {getAll = False}
Example1 expression
All True <> memptyAll {getAll = True}
Instances11Bounded, Eq, Data, Ord, Read, Show, …
  • Bounded AllDefined in ghc-internal-9.1003.0 · GHC.Internal.Data.Semigroup.Internal
  • Eq AllDefined in ghc-internal-9.1003.0 · GHC.Internal.Data.Semigroup.Internal
  • Data AllDefined in ghc-internal-9.1003.0 · GHC.Internal.Data.Data
  • Ord AllDefined in ghc-internal-9.1003.0 · GHC.Internal.Data.Semigroup.Internal
  • Read AllDefined in ghc-internal-9.1003.0 · GHC.Internal.Data.Semigroup.Internal
  • Show AllDefined in ghc-internal-9.1003.0 · GHC.Internal.Data.Semigroup.Internal
  • Generic AllDefined in ghc-internal-9.1003.0 · GHC.Internal.Data.Semigroup.Internal
  • Semigroup AllDefined in ghc-internal-9.1003.0 · GHC.Internal.Data.Semigroup.Internal
  • Monoid AllDefined in ghc-internal-9.1003.0 · GHC.Internal.Data.Semigroup.Internal
  • NFData AllDefined in deepseq-1.5.0.0 · Control.DeepSeq
  • type Rep All = D1 ('MetaData "All" "GHC.Internal.Data.Semigroup.Internal" "ghc-internal" 'True) (C1 ('MetaCons "All" 'PrefixI 'True) (S1 ('MetaSel ('Just "getAll") 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 Bool)))Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Semigroup.Internal
newtypenewtype Any
#

Boolean monoid under disjunction (||).

Any x <> Any y = Any (x || y)
Examples
Example1 expression
Any True <> mempty <> Any FalseAny {getAny = True}
Example1 expression
mconcat (map (\x -> Any (even x)) [2,4,6,7,8])Any {getAny = True}
Example1 expression
Any False <> memptyAny {getAny = False}
Instances11Bounded, Eq, Data, Ord, Read, Show, …
  • Bounded AnyDefined in ghc-internal-9.1003.0 · GHC.Internal.Data.Semigroup.Internal
  • Eq AnyDefined in ghc-internal-9.1003.0 · GHC.Internal.Data.Semigroup.Internal
  • Data AnyDefined in ghc-internal-9.1003.0 · GHC.Internal.Data.Data
  • Ord AnyDefined in ghc-internal-9.1003.0 · GHC.Internal.Data.Semigroup.Internal
  • Read AnyDefined in ghc-internal-9.1003.0 · GHC.Internal.Data.Semigroup.Internal
  • Show AnyDefined in ghc-internal-9.1003.0 · GHC.Internal.Data.Semigroup.Internal
  • Generic AnyDefined in ghc-internal-9.1003.0 · GHC.Internal.Data.Semigroup.Internal
  • Semigroup AnyDefined in ghc-internal-9.1003.0 · GHC.Internal.Data.Semigroup.Internal
  • Monoid AnyDefined in ghc-internal-9.1003.0 · GHC.Internal.Data.Semigroup.Internal
  • NFData AnyDefined in deepseq-1.5.0.0 · Control.DeepSeq
  • type Rep Any = D1 ('MetaData "Any" "GHC.Internal.Data.Semigroup.Internal" "ghc-internal" 'True) (C1 ('MetaCons "Any" 'PrefixI 'True) (S1 ('MetaSel ('Just "getAny") 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 Bool)))Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Semigroup.Internal
newtypenewtype Sum a
#

Monoid under addition.

Sum a <> Sum b = Sum (a + b)
Examples
Example1 expression
Sum 1 <> Sum 2 <> memptySum {getSum = 3}
Example1 expression
mconcat [ Sum n | n <- [3 .. 9]]Sum {getSum = 42}
Instances23Monad, Functor, MonadFix, Applicative, Foldable, Traversable, …
  • Monad SumDefined in ghc-internal-9.1003.0 · GHC.Internal.Data.Semigroup.Internal
  • Functor SumDefined in ghc-internal-9.1003.0 · GHC.Internal.Data.Semigroup.Internal
  • MonadFix SumDefined in ghc-internal-9.1003.0 · GHC.Internal.Control.Monad.Fix
  • Applicative SumDefined in ghc-internal-9.1003.0 · GHC.Internal.Data.Semigroup.Internal
  • Foldable SumDefined in ghc-internal-9.1003.0 · GHC.Internal.Data.Foldable
  • Traversable SumDefined in ghc-internal-9.1003.0 · GHC.Internal.Data.Traversable
  • MonadZip SumDefined in base-4.20.2.0 · Control.Monad.Zip
  • Foldable1 SumDefined in base-4.20.2.0 · Data.Foldable1
  • NFData1 SumDefined in deepseq-1.5.0.0 · Control.DeepSeq
  • Generic1 SumDefined in ghc-internal-9.1003.0 · GHC.Internal.Data.Semigroup.Internal
  • Bounded a => Bounded (Sum a)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Semigroup.Internal
  • Eq a => Eq (Sum a)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Semigroup.Internal
  • Data a => Data (Sum a)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Data
  • Num a => Num (Sum a)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Semigroup.Internal
  • Ord a => Ord (Sum a)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Semigroup.Internal
  • Read a => Read (Sum a)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Semigroup.Internal
  • Show a => Show (Sum a)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Semigroup.Internal
  • Generic (Sum a)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Semigroup.Internal
  • Num a => Semigroup (Sum a)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Semigroup.Internal
  • Num a => Monoid (Sum a)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Semigroup.Internal
  • NFData a => NFData (Sum a)Defined in deepseq-1.5.0.0 · Control.DeepSeq
  • type Rep (Sum a) = D1 ('MetaData "Sum" "GHC.Internal.Data.Semigroup.Internal" "ghc-internal" 'True) (C1 ('MetaCons "Sum" 'PrefixI 'True) (S1 ('MetaSel ('Just "getSum") 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 a)))Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Semigroup.Internal
  • type Rep1 Sum = D1 ('MetaData "Sum" "GHC.Internal.Data.Semigroup.Internal" "ghc-internal" 'True) (C1 ('MetaCons "Sum" 'PrefixI 'True) (S1 ('MetaSel ('Just "getSum") 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) Par1))Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Semigroup.Internal
newtypenewtype Product a
#

Monoid under multiplication.

Product x <> Product y == Product (x * y)
Examples
Example1 expression
Product 3 <> Product 4 <> memptyProduct {getProduct = 12}
Example1 expression
mconcat [ Product n | n <- [2 .. 10]]Product {getProduct = 3628800}
Instances23Monad, Functor, MonadFix, Applicative, Foldable, Traversable, …