Safe Haskell | None |
---|---|
Language | Haskell2010 |
- The fundamental lens operations:
view
,set
andover
- Stock lenses (
_1
,_2
) and mere traversals (_Left
,_Right
,_Just
, etc.) for simple Prelude types LensLike
and important strength-expressing synonyms, from the all-powerfulLens
downward- Support for defining lenses and weaker 'optics' (see also the TH incantations below )
- Operator equivalents for common lens-applying operators: particularly
(^.)
,(.~)
and(%~)
(i.e.view
,set
andover
) - Basic state-related combinators:
zoom
,use
,assign/(.=)
etc. - Convenient state-related operators
- Pseudo-imperatives
- Corresponding state-related imperatives
- More stock lenses
- More stock traversals
- More stock setters
- Other combinators
- TH incantations
- Other type synonyms
- Helper classes
- Helper types
- Re-exports
- view :: FoldLike b a a' b b' -> a -> b
- set :: Setter a a' b b' -> b' -> a -> a'
- over :: ASetter a a' b b' -> (b -> b') -> a -> a'
- _1 :: Functor f => LensLike f (a, b) (a', b) a a'
- _2 :: Functor f => LensLike f (a, b) (a, b') b b'
- _Left :: Applicative f => LensLike f (Either a b) (Either a' b) a a'
- _Right :: Applicative f => LensLike f (Either a b) (Either a b') b b'
- _Just :: Applicative f => LensLike f (Maybe a) (Maybe a') a a'
- _Nothing :: Applicative f => LensLike' f (Maybe a) ()
- both :: Applicative f => LensLike f (a, a) (b, b) a b
- type Lens a a' b b' = forall f. Functor f => LensLike f a a' b b'
- type Traversal a a' b b' = forall f. Applicative f => LensLike f a a' b b'
- type Setter a a' b b' = forall f. Identical f => LensLike f a a' b b'
- type Getter a a' b b' = forall f. Phantom f => LensLike f a a' b b'
- type Fold a a' b b' = forall f. (Phantom f, Applicative f) => LensLike f a a' b b'
- type FoldLike r a a' b b' = LensLike (Constant r) a a' b b'
- type SetterLike a a' b b' = LensLike Identity a a' b b'
- type LensLike f a a' b b' = (b -> f b') -> a -> f a'
- lens :: (a -> b) -> (a -> b' -> a') -> Lens a a' b b'
- iso :: (a -> b) -> (b' -> a') -> Lens a a' b b'
- to :: (a -> b) -> Getter a a' b b'
- setting :: ((b -> b') -> a -> a') -> Setter a a' b b'
- folding :: Foldable f => (a -> f b) -> Fold a a' b b'
- (^.) :: a -> FoldLike b a a' b b' -> b
- (%~) :: Setter a a' b b' -> (b -> b') -> a -> a'
- (.~) :: Setter a a' b b' -> b' -> a -> a'
- (&) :: a -> (a -> b) -> b
- (??) :: Functor f => f (a -> b) -> a -> f b
- (?~) :: Setter a a' b (Maybe b') -> b' -> a -> a'
- (^..) :: a -> Fold a a' b b' -> [b]
- (^?) :: a -> Fold a a' b b' -> Maybe b
- zoom :: Monad m => LensLike' (Zooming m c) a b -> StateT b m c -> StateT a m c
- use :: MonadState a m => FoldLike b a a' b b' -> m b
- uses :: MonadState a m => FoldLike r a a' b b' -> (b -> r) -> m r
- assign :: MonadState a m => Setter a a b b' -> b' -> m ()
- (%=) :: MonadState a m => Setter a a b b' -> (b -> b') -> m ()
- (.=) :: MonadState a m => Setter a a b b' -> b' -> m ()
- (%%=) :: MonadState a m => LensLike (Writer c) a a b b' -> (b -> (c, b')) -> m c
- (<~) :: MonadState a m => Setter a a b b' -> m b' -> m ()
- (+~) :: Num b => Setter' a b -> b -> a -> a
- (*~) :: Num b => Setter' a b -> b -> a -> a
- (-~) :: Num b => Setter' a b -> b -> a -> a
- (//~) :: Fractional b => Setter' a b -> b -> a -> a
- (&&~) :: Setter' a Bool -> Bool -> a -> a
- (||~) :: Setter' a Bool -> Bool -> a -> a
- (<>~) :: Monoid o => Setter' a o -> o -> a -> a
- (+=) :: (MonadState a m, Num b) => Setter' a b -> b -> m ()
- (-=) :: (MonadState a m, Num b) => Setter' a b -> b -> m ()
- (*=) :: (MonadState a m, Num b) => Setter' a b -> b -> m ()
- (//=) :: (MonadState a m, Fractional b) => Setter' a b -> b -> m ()
- (&&=) :: MonadState a m => Setter' a Bool -> Bool -> m ()
- (||=) :: MonadState a m => Setter' a Bool -> Bool -> m ()
- (<>=) :: (Monoid o, MonadState a m) => Setter' a o -> o -> m ()
- chosen :: Functor f => LensLike f (Either a a) (Either b b) a b
- ix :: Eq k => k -> Lens' (k -> v) v
- at :: Ord k => k -> Lens' (Map k v) (Maybe v)
- intAt :: Int -> Lens' (IntMap v) (Maybe v)
- contains :: Ord k => k -> Lens' (Set k) Bool
- intContains :: Int -> Lens' IntSet Bool
- ignored :: Applicative f => LensLike f a a b b'
- mapped :: Functor f => Setter (f a) (f a') a a'
- views :: FoldLike r a a' b b' -> (b -> r) -> a -> r
- toListOf :: Fold a a' b b' -> a -> [b]
- allOf :: Fold a a' b b' -> (b -> Bool) -> a -> Bool
- anyOf :: Fold a a' b b' -> (b -> Bool) -> a -> Bool
- firstOf :: Fold a a' b b' -> a -> Maybe b
- lastOf :: Fold a a' b b' -> a -> Maybe b
- sumOf :: Num b => Fold a a' b b' -> a -> b
- productOf :: Num b => Fold a a' b b' -> a -> b
- lengthOf :: Num r => Fold a a' b b' -> a -> r
- nullOf :: Fold a a' b b' -> a -> Bool
- backwards :: LensLike (Backwards f) a a' b b' -> LensLike f a a' b b'
- choosing :: Functor f => LensLike f a a' c c' -> LensLike f b b' c c' -> LensLike f (Either a b) (Either a' b') c c'
- alongside :: Functor f => LensLike (AlongsideLeft f b2') a1 a1' b1 b1' -> LensLike (AlongsideRight f a1') a2 a2' b2 b2' -> LensLike f (a1, a2) (a1', a2') (b1, b2) (b1', b2')
- beside :: Applicative f => LensLike f a a' c c' -> LensLike f b b' c c' -> LensLike f (a, b) (a', b') c c'
- makeLenses :: Name -> Q [Dec]
- makeTraversals :: Name -> Q [Dec]
- makeLensesBy :: (String -> Maybe String) -> Name -> Q [Dec]
- makeLensesFor :: [(String, String)] -> Name -> Q [Dec]
- type LensLike' f a b = (b -> f b) -> a -> f a
- type Lens' a b = forall f. Functor f => LensLike' f a b
- type Traversal' a b = forall f. Applicative f => LensLike' f a b
- type Getter' a b = forall f. Phantom f => LensLike' f a b
- type Setter' a b = forall f. Identical f => LensLike' f a b
- type FoldLike' r a b = LensLike' (Constant r) a b
- type ASetter' a b = LensLike' Identity a b
- type ASetter a a' b b' = LensLike Identity a a' b b'
- class Applicative f => Identical f
- class Functor f => Phantom f
- data AlongsideLeft f b a :: (* -> *) -> * -> * -> *
- data AlongsideRight f a b :: (* -> *) -> * -> * -> *
- data Zooming m c a :: (* -> *) -> * -> * -> *
- newtype Constant a b :: * -> * -> * = Constant {
- getConstant :: a
- newtype Identity a :: * -> * = Identity {
- runIdentity :: a
- class Monoid a where
- (<>) :: Monoid m => m -> m -> m
The fundamental lens operations: view
, set
and over
view :: FoldLike b a a' b b' -> a -> b
view :: Getter a a' b b' -> a -> b
Demote a lens or getter to a projection function.
view :: Monoid b => Fold a a' b b' -> a -> b
Returns the monoidal summary of a traversal or a fold.
Stock lenses (_1
, _2
) and mere traversals (_Left
, _Right
, _Just
, etc.) for simple Prelude types
_Left :: Applicative f => LensLike f (Either a b) (Either a' b) a a'
_Right :: Applicative f => LensLike f (Either a b) (Either a b') b b'
_Just :: Applicative f => LensLike f (Maybe a) (Maybe a') a a'
both :: Applicative f => LensLike f (a, a) (b, b) a b
Traversals on both elements of a pair (a,a)
.
LensLike
and important strength-expressing synonyms, from the all-powerful Lens
downward
type Traversal a a' b b' = forall f. Applicative f => LensLike f a a' b b'
type Fold a a' b b' = forall f. (Phantom f, Applicative f) => LensLike f a a' b b'
type SetterLike a a' b b' = LensLike Identity a a' b b' Source
type LensLike f a a' b b' = (b -> f b') -> a -> f a'
Support for defining lenses and weaker 'optics' (see also the TH incantations below )
:: (a -> b) | getter |
-> (a -> b' -> a') | setter |
-> Lens a a' b b' |
Build a lens from a getter
and setter
families.
Caution: In order for the generated lens family to be well-defined, you must ensure that the three lens laws hold:
getter (setter a b) === b
setter a (getter a) === a
setter (setter a b1) b2) === setter a b2
:: (a -> b) | yin |
-> (b' -> a') | yang |
-> Lens a a' b b' |
Build a lens from isomorphism families.
Caution: In order for the generated lens family to be well-defined, you must ensure that the two isomorphism laws hold:
yin . yang === id
yang . yin === id
to :: (a -> b) -> Getter a a' b 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
.
>>>
(3 :+ 4, "example")^._1.to(abs)
5.0 :+ 0.0
:: ((b -> b') -> a -> a') | sec (semantic editor combinator) |
-> Setter a a' b b' |
setting
promotes a "semantic editor combinator" to a modify-only lens.
To demote a lens to a semantic edit combinator, use the section (l %~)
or over l
from Lens.Family2.
>>>
setting map . fstL %~ length $ [("The",0),("quick",1),("brown",1),("fox",2)]
[(3,0),(5,1),(5,1),(3,2)]
Caution: In order for the generated setter family to be well-defined, you must ensure that the two functors laws hold:
sec id === id
sec f . sec g === sec (f . g)
folding :: Foldable f => (a -> f b) -> Fold a a' b 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
.
Operator equivalents for common lens-applying operators: particularly (^.)
, (.~)
and (%~)
(i.e. view
, set
and over
)
(^.) :: a -> FoldLike b a a' b b' -> b infixl 8
(^.) :: a -> Getter a a' b b' -> b
Access the value referenced by a getter or lens.
(^.) :: Monoid b => a -> Fold a a' b b' -> b
Access the monoidal summary referenced by a getter or lens.
(&) :: a -> (a -> b) -> b infixl 1
(??) :: Functor f => f (a -> b) -> a -> f b infixl 1 Source
Generalized infix flip, replicating Control.Lens.Lens.??
>>>
execStateT ?? (0,"") $ do _1 += 1; _1 += 1; _2 <>= "hello"
(2,"hello")
(^..) :: a -> Fold a a' b b' -> [b] infixl 8
Returns a list of all of the referenced values in order.
Basic state-related combinators: zoom
, use
, assign/(.=)
etc.
zoom :: Monad m => LensLike' (Zooming m c) a b -> StateT b m c -> StateT a m c
zoom :: Monad m => Lens' a b -> StateT b m c -> StateT a m c
Lift a stateful operation on a field to a stateful operation on the whole state. This is a good way to call a "subroutine" that only needs access to part of the state.
zoom :: (Monoid c, Monad m) => Traversal' a b -> StateT b m c -> StateT a m c
Run the "subroutine" on each element of the traversal in turn and mconcat
all the results together.
zoom :: Monad m => Traversal' a b -> StateT b m () -> StateT a m ()
Run the "subroutine" on each element the traversal in turn.
use :: MonadState a m => FoldLike b a a' b b' -> m b
use :: MonadState a m => Getter a a' b b' -> m b
Retrieve a field of the state
use :: (Monoid b, MonadState a m) => Fold a a' b b' -> m b
Retrieve a monoidal summary of all the referenced fields from the state
uses :: MonadState a m => FoldLike r a a' b b' -> (b -> r) -> m r
uses :: (MonadState a m, Monoid r) => Fold a a' b b' -> (b -> r) -> m r
Retrieve all the referenced fields from the state and foldMap the results together with f :: b -> r
.
uses :: MonadState a m => Getter a a' b b' -> (b -> r) -> m r
Retrieve a field of the state and pass it through the function f :: b -> r
.
uses l f = f <$> use l
assign :: MonadState a m => Setter a a b b' -> b' -> m ()
Set a field of the state.
Convenient state-related operators
(%=) :: MonadState a m => Setter a a b b' -> (b -> b') -> m () infix 4
Modify a field of the state.
(.=) :: MonadState a m => Setter a a b b' -> b' -> m () infix 4
Set a field of the state.
(%%=) :: MonadState a m => LensLike (Writer c) a a b b' -> (b -> (c, b')) -> m c infix 4
(%%=) :: MonadState a m => Lens a a b b' -> (b -> (c, b')) -> m c
Modify a field of the state while returning another value.
(%%=) :: (MonadState a m, Monoid c) => Traversal a a b b' -> (b -> (c, b')) -> m c
Modify each field of the state and return the mconcat
of the other values.
(<~) :: MonadState a m => Setter a a b b' -> m b' -> m () infixr 2
Set a field of the state using the result of executing a stateful command.
Pseudo-imperatives
(//~) :: Fractional b => Setter' a b -> b -> a -> a infixr 4
(<>~) :: Monoid o => Setter' a o -> o -> a -> a infixr 4
Monoidally append a value to all referenced fields.
Corresponding state-related imperatives
(+=) :: (MonadState a m, Num b) => Setter' a b -> b -> m () infixr 4
(-=) :: (MonadState a m, Num b) => Setter' a b -> b -> m () infixr 4
(*=) :: (MonadState a m, Num b) => Setter' a b -> b -> m () infixr 4
(//=) :: (MonadState a m, Fractional b) => Setter' a b -> b -> m () infixr 4
(&&=) :: MonadState a m => Setter' a Bool -> Bool -> m () infixr 4
(||=) :: MonadState a m => Setter' a Bool -> Bool -> m () infixr 4
(<>=) :: (Monoid o, MonadState a m) => Setter' a o -> o -> m () infixr 4
Monoidally append a value to all referenced fields of the state.
More stock lenses
chosen :: Functor f => LensLike f (Either a a) (Either b b) a b
Lens on the Left or Right element of an (Either
a a).
More stock traversals
ignored :: Applicative f => LensLike f a a b b'
The empty traveral on any type.
More stock setters
Other combinators
views :: FoldLike r a a' b b' -> (b -> r) -> a -> r
views :: Monoid r => Fold a a' b b' -> (b -> r) -> a -> r
Given a fold or traversal, return the foldMap
of all the values using the given function.
views :: Getter a a' b b' -> (b -> r) -> a -> 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 a = f (view l a)
allOf :: Fold a a' b b' -> (b -> Bool) -> a -> Bool
Returns true if all of the referenced values satisfy the given predicate.
anyOf :: Fold a a' b b' -> (b -> Bool) -> a -> Bool
Returns true if any of the referenced values satisfy the given predicate.
lengthOf :: Num r => Fold a a' b b' -> a -> r
Counts the number of references in a traversal or fold for the input.
backwards :: LensLike (Backwards f) a a' b b' -> LensLike f a a' b b'
backwards :: Traversal a a' b b' -> Traversal a a' b b' backwards :: Fold a a' b b' -> Fold a a' b b'
Given a traversal or fold, reverse the order that elements are traversed.
backwards :: Lens a a' b b' -> Lens a a' b b' backwards :: Getter a a' b b' -> Getter a a' b b' backwards :: Setter a a' b b' -> Setter a a' b b'
No effect on lenses, getters or setters.
choosing :: Functor f => LensLike f a a' c c' -> LensLike f b b' c c' -> LensLike f (Either a b) (Either a' b') c c'
choosing :: Lens a a' c c' -> Lens b b' c c' -> Lens (Either a b) (Either a' b') c c'
choosing :: Traversal a a' c c' -> Traversal b b' c c' -> Traversal (Either a b) (Either a' b') c c'
choosing :: Getter a a' c c' -> Getter b b' c c' -> Getter (Either a b) (Either a' b') c c'
choosing :: Fold a a' c c' -> Fold b b' c c' -> Fold (Either a b) (Either a' b') c c'
choosing :: Setter a a' c c' -> Setter b b' c c' -> Setter (Either a b) (Either a' b') c c'
Given two lens/traversal/getter/fold/setter families with the same substructure, make a new lens/traversal/getter/fold/setter on Either
.
alongside :: Functor f => LensLike (AlongsideLeft f b2') a1 a1' b1 b1' -> LensLike (AlongsideRight f a1') a2 a2' b2 b2' -> LensLike f (a1, a2) (a1', a2') (b1, b2) (b1', b2')
alongside :: Lens a1 a1' b1 b1' -> Lens a2 a2' b2 b2' -> Lens (a1, a2) (a1', a2') (b1, b2) (b1', b2')
alongside :: Getter a1 a1' b1 b1' -> Getter a2 a2' b2 b2' -> Getter (a1, a2) (a1', a2') (b1, b2) (b1', b2')
Given two lens/getter families, make a new lens/getter on their product.
beside :: Applicative f => LensLike f a a' c c' -> LensLike f b b' c c' -> LensLike f (a, b) (a', b') c c'
beside :: Traversal a a' c c' -> Traversal b' b' c c' -> Traversal (a,b) (a',b') c c'
beside :: Fold a a' c c' -> Fold b' b' c c' -> Fold (a,b) (a',b') c c'
beside :: Setter a a' c c' -> Setter b' b' c c' -> Setter (a,b) (a',b') c c'
Given two traversals/folds/setters referencing a type c
, create a traversal/fold/setter on the pair referencing c
.
TH incantations
makeLenses :: Name -> Q [Dec]
Derive lenses for the record selectors in a single-constructor data declaration, or for the record selector in a newtype declaration. Lenses will only be generated for record fields which are prefixed with an underscore.
Example usage:
$(makeLenses ''Foo)
makeTraversals :: Name -> Q [Dec]
Derive traversals for each constructor in a data or newtype declaration, Traversals will be named by prefixing the constructor name with an underscore.
Example usage:
$(makeTraversals ''Foo)
makeLensesBy :: (String -> Maybe String) -> Name -> Q [Dec]
Derive lenses with the provided name transformation
and filtering function. Produce Just lensName
to generate a lens
of the resultant name, or Nothing
to not generate a lens
for the input record name.
Example usage:
$(makeLensesBy (\n -> Just (n ++ "L")) ''Foo)
makeLensesFor :: [(String, String)] -> Name -> Q [Dec]
Derive lenses, specifying explicit pairings of (fieldName, lensName)
.
Example usage:
$(makeLensesFor [("_foo", "fooLens"), ("bar", "lbar")] ''Foo)
Other type synonyms
type LensLike' f a b = (b -> f b) -> a -> f a
type Traversal' a b = forall f. Applicative f => LensLike' f a b
Helper classes
class Applicative f => Identical f
extract
coerce
Helper types
data AlongsideLeft f b a :: (* -> *) -> * -> * -> *
Functor f => Functor (AlongsideLeft f a) | |
Phantom f => Phantom (AlongsideLeft f a) |
data AlongsideRight f a b :: (* -> *) -> * -> * -> *
Functor f => Functor (AlongsideRight f a) | |
Phantom f => Phantom (AlongsideRight f a) |
data Zooming m c a :: (* -> *) -> * -> * -> *
Re-exports
newtype Constant a b :: * -> * -> *
Constant functor.
Constant | |
|
Functor (Constant a) | |
Monoid a => Applicative (Constant a) | |
Foldable (Constant a) | |
Traversable (Constant a) | |
Phantom (Constant a) | |
Eq a => Eq1 (Constant a) | |
Ord a => Ord1 (Constant a) | |
Read a => Read1 (Constant a) | |
Show a => Show1 (Constant a) | |
Eq a => Eq (Constant a b) | |
Ord a => Ord (Constant a b) | |
Read a => Read (Constant a b) | |
Show a => Show (Constant a b) |
newtype Identity a :: * -> *
Identity functor and monad. (a non-strict monad)
Since: 4.8.0.0
Identity | |
|
Monad Identity | |
Functor Identity | |
MonadFix Identity | |
Applicative Identity | |
Foldable Identity | |
Traversable Identity | |
Generic1 Identity | |
MonadZip Identity | |
Identical Identity | |
Eq a => Eq (Identity a) | |
Data a => Data (Identity a) | |
Ord a => Ord (Identity a) | |
Read a => Read (Identity a) | This instance would be equivalent to the derived instances of the
|
Show a => Show (Identity a) | This instance would be equivalent to the derived instances of the
|
Generic (Identity a) | |
type Rep1 Identity = D1 D1Identity (C1 C1_0Identity (S1 S1_0_0Identity Par1)) | |
type Rep (Identity a) = D1 D1Identity (C1 C1_0Identity (S1 S1_0_0Identity (Rec0 a))) |
class Monoid a where
The class of monoids (types with an associative binary operation that has an identity). Instances should satisfy the following laws:
mappend mempty x = x
mappend x mempty = x
mappend x (mappend y z) = mappend (mappend x y) z
mconcat =
foldr
mappend mempty
The method names refer to the monoid of lists under concatenation, but there are many other instances.
Some types can be viewed as a monoid in more than one way,
e.g. both addition and multiplication on numbers.
In such cases we often define newtype
s and make those instances
of Monoid
, e.g. Sum
and Product
.
mempty :: a
Identity of mappend
mappend :: a -> a -> a
An associative operation
mconcat :: [a] -> a
Fold a list using the monoid.
For most types, the default definition for mconcat
will be
used, but the function is included in the class definition so
that an optimized version can be provided for specific types.
Monoid Ordering | |
Monoid () | |
Monoid All | |
Monoid Any | |
Monoid IntSet | |
Monoid Doc | |
Monoid [a] | |
Ord a => Monoid (Max a) | |
Ord a => Monoid (Min a) | |
Monoid a => Monoid (Dual a) | |
Monoid (Endo a) | |
Num a => Monoid (Sum a) | |
Num a => Monoid (Product a) | |
Monoid (First a) | |
Monoid (Last a) | |
Monoid a => Monoid (Maybe a) | Lift a semigroup into |
Monoid (IntMap a) | |
Ord a => Monoid (Set a) | |
Monoid b => Monoid (a -> b) | |
(Monoid a, Monoid b) => Monoid (a, b) | |
Monoid (Proxy k s) | |
Ord k => Monoid (Map k v) | |
(Monoid a, Monoid b, Monoid c) => Monoid (a, b, c) | |
Alternative f => Monoid (Alt * f a) | |
(Monoid a, Monoid b, Monoid c, Monoid d) => Monoid (a, b, c, d) | |
(Monoid a, Monoid b, Monoid c, Monoid d, Monoid e) => Monoid (a, b, c, d, e) |