| Portability | Rank2Types |
|---|---|
| Stability | provisional |
| Maintainer | Edward Kmett <ekmett@gmail.com> |
| Safe Haskell | None |
Control.Lens.Getter
Description
A is just any function Getter a c(a -> c), which we've flipped
into continuation passing style, (c -> r) -> a -> r and decorated
with Accessor to obtain:
typeGettingr a b c d = (c ->Accessorr d) -> a ->Accessorr b
If we restrict access to knowledge about the type r and can work for
any d and b, we could get:
typeGettera c = forall r.Gettingr a a c c
But we actually hide the use of Accessor behind a class Gettable
to error messages from type class resolution rather than at unification
time, where they are much uglier.
typeGettera c = forall f.Gettablef => (c -> f c) -> a -> f a
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.
- type Getter a c = forall f. Gettable f => (c -> f c) -> a -> f a
- type Getting r a b c d = (c -> Accessor r d) -> a -> Accessor r b
- to :: (a -> c) -> Getter a c
- (^.) :: a -> Getting c a b c d -> c
- (^$) :: Getting c a b c d -> a -> c
- view :: Getting c a b c d -> a -> c
- views :: Getting m a b c d -> (c -> m) -> a -> m
- use :: MonadState a m => Getting c a b c d -> m c
- uses :: MonadState a m => Getting e a b c d -> (c -> e) -> m e
- query :: MonadReader a m => Getting c a b c d -> m c
- queries :: MonadReader a m => Getting e a b c d -> (c -> e) -> m e
- newtype ReifiedGetter a c = ReifyGetter {
- reflectGetter :: Getter a c
Getters
type Getter a c = forall f. Gettable f => (c -> f c) -> a -> f aSource
A Getter describes how to retrieve a single value in a way that can be
composed with other lens-like 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 (a -> c).
Moreover, a Getter can be used directly as a Fold,
since it just ignores the Applicative.
type Getting r a b c d = (c -> Accessor r d) -> a -> Accessor r bSource
Most Getter combinators are able to be used with both a Getter or a
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, Traversal and
Iso we also restricted choices of the irrelevant b and
d parameters.
If a function accepts a , then when Getting r a b c dr is a Monoid, then
you can pass a Fold (or
Traversal), otherwise you can only pass this a
Getter or Lens.
Building Getters
Combinators for Getters and Folds
(^.) :: a -> Getting c a b c d -> cSource
View the value pointed to by a Getter or Lens or the
result of folding over all the results of a Fold or
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 (.)
>>>import Control.Lens>>>("hello","world")^._2"world"
>>>import Data.Complex>>>((0, 1 :+ 2), 3)^._1._2.to magnitude2.23606797749979
(^.) :: a ->Gettera c -> c (^.) ::Monoidm => a ->Folda m -> m (^.) :: a ->SimpleIsoa c -> c (^.) :: a ->SimpleLensa c -> c (^.) ::Monoidm => a ->SimpleTraversala m -> m
(^$) :: Getting c a b c d -> a -> cSource
View the value pointed to by a Getter, Iso or
Lens or the result of folding over all the results of a
Fold or Traversal that points
at a monoidal values.
This is the same operation as view, only infix.
tof^$x = f x
>>>import Control.Lens>>>_2 ^$ (1, "hello")"hello"
(^$) ::Gettera c -> a -> c (^$) ::Monoidm =>Folda m -> a -> m (^$) ::SimpleIsoa c -> a -> c (^$) ::SimpleLensa c -> a -> c (^$) ::Monoidm =>SimpleTraversala m -> a -> m
view :: Getting c a b c d -> a -> cSource
View the value pointed to by a Getter, Iso or
Lens or the result of folding over all the results of a
Fold or Traversal that points
at a monoidal values.
view.to=id
>>>import Control.Lens>>>view _2 (1,"hello")"hello"
>>>view (to succ) 56
>>>view (_2._1) ("hello",("world","!!!"))"world"
It may be useful to think of view as having one of these more restrictive
signatures:
view::Gettera c -> a -> cview::Monoidm =>Folda m -> a -> mview::SimpleIsoa c -> a -> cview::SimpleLensa c -> a -> cview::Monoidm =>SimpleTraversala m -> a -> m
views :: Getting m a b c d -> (c -> m) -> a -> mSource
View the value of a Getter, Iso,
Lens or the result of folding over the result of mapping
the targets of a Fold or
Traversal.
It may be useful to think of views as having these more restrictive
signatures:
viewsl f =view(l.tof)
>>>import Control.Lens>>>views _2 length (1,"hello")5
views::Gettera c -> (c -> d) -> a -> dviews::Monoidm =>Folda c -> (c -> m) -> a -> mviews::SimpleIsoa c -> (c -> d) -> a -> dviews::SimpleLensa c -> (c -> d) -> a -> dviews::Monoidm =>SimpleTraversala c -> (c -> m) -> a -> m
use :: MonadState a m => Getting c a b c d -> m cSource
Use the target of a Lens, Iso, or
Getter in the current state, or use a summary of a
Fold or Traversal that points
to a monoidal value.
use::MonadStatea m =>Gettera c -> m cuse:: (MonadStatea m,Monoidr) =>Folda r -> m ruse::MonadStatea m =>SimpleIsoa c -> m cuse::MonadStatea m =>SimpleLensa c -> m cuse:: (MonadStatea m,Monoidr) =>SimpleTraversala r -> m r
uses :: MonadState a m => Getting e a b c d -> (c -> e) -> m eSource
Use the target of a Lens, Iso or
Getter in the current state, or use a summary of a
Fold or Traversal that
points to a monoidal value.
uses::MonadStatea m =>Gettera c -> (c -> e) -> m euses:: (MonadStatea m,Monoidr) =>Folda c -> (c -> r) -> m ruses::MonadStatea m =>SimpleLensa c -> (c -> e) -> m euses::MonadStatea m =>SimpleIsoa c -> (c -> e) -> m euses:: (MonadStatea m,Monoidr) =>SimpleTraversala c -> (c -> r) -> m r
query :: MonadReader a m => Getting c a b c d -> m cSource
Query the target of a Lens, Iso or
Getter in the current state, or use a summary of a
Fold or Traversal that points
to a monoidal value.
query::MonadReadera m =>Gettera c -> m cquery:: (MonadReadera m,Monoidc) =>Folda c -> m cquery::MonadReadera m =>SimpleIsoa c -> m cquery::MonadReadera m =>SimpleLensa c -> m cquery:: (MonadReadera m,Monoidc) =>SimpleTraversala c -> m c
queries :: MonadReader a m => Getting e a b c d -> (c -> e) -> m eSource
Use the target of a Lens, Iso or
Getter in the current state, or use a summary of a
Fold or Traversal that points
to a monoidal value.
queries::MonadReadera m =>Gettera c -> (c -> e) -> m equeries:: (MonadReadera m,Monoidc) =>Folda c -> (c -> e) -> m equeries::MonadReadera m =>SimpleIsoa c -> (c -> e) -> m equeries::MonadReadera m =>SimpleLensa c -> (c -> e) -> m equeries:: (MonadReadera m,Monoidc) =>SimpleTraversala c -> (c -> e) -> m e
Storing Getters
newtype ReifiedGetter a c Source
Useful for storing getters in containers.
Constructors
| ReifyGetter | |
Fields
| |