Portability  Rank2Types 

Stability  provisional 
Maintainer  Edward Kmett <ekmett@gmail.com> 
Safe Haskell  Trustworthy 
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:
typeGetting
r a b c d = (c >Accessor
r d) > a >Accessor
r b
If we restrict access to knowledge about the type r
and can work for
any d and b, we could get:
typeGetter
a c = forall r.Getting
r 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.
typeGetter
a c = forall f.Gettable
f => (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
 (%) :: a > (a > b) > b
 (^%) :: a > (a > b) > b
 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
 class Functor f => Gettable f
 data Accessor r a
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 lenslike constructions.
Unlike a Lens
a Getter
is readonly. 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 (.
)
>>>
("hello","world")^._2
"world"
>>>
import Data.Complex
>>>
((0, 1 :+ 2), 3)^._1._2.to magnitude
2.23606797749979
(^.
) :: a >Getter
a c > c (^.
) ::Monoid
m => a >Fold
a m > m (^.
) :: a >Simple
Iso
a c > c (^.
) :: a >Simple
Lens
a c > c (^.
) ::Monoid
m => a >Simple
Traversal
a 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.
to
f^$
x = f x
>>>
_2 ^$ (1, "hello")
"hello"
(^$
) ::Getter
a c > a > c (^$
) ::Monoid
m =>Fold
a m > a > m (^$
) ::Simple
Iso
a c > a > c (^$
) ::Simple
Lens
a c > a > c (^$
) ::Monoid
m =>Simple
Traversal
a m > a > m
(%) :: a > (a > b) > bSource
Passes the result of the left side to the function on the right side (forward pipe operator).
This is the flipped version of ($
), which is more common in languages like F# as (>
) where it is needed
for inference. Here it is supplied for notational convenience and given a precedence that allows it
to be nested inside uses of ($
).
>>>
"hello" % length % succ
6
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
>>>
view _2 (1,"hello")
"hello"
>>>
view (to succ) 5
6
>>>
view (_2._1) ("hello",("world","!!!"))
"world"
It may be useful to think of view
as having one of these more restrictive
signatures:
view
::Getter
a c > a > cview
::Monoid
m =>Fold
a m > a > mview
::Simple
Iso
a c > a > cview
::Simple
Lens
a c > a > cview
::Monoid
m =>Simple
Traversal
a 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:
views
l f =view
(l.
to
f)
>>>
views _2 length (1,"hello")
5
views
::Getter
a c > (c > d) > a > dviews
::Monoid
m =>Fold
a c > (c > m) > a > mviews
::Simple
Iso
a c > (c > d) > a > dviews
::Simple
Lens
a c > (c > d) > a > dviews
::Monoid
m =>Simple
Traversal
a 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
::MonadState
a m =>Getter
a c > m cuse
:: (MonadState
a m,Monoid
r) =>Fold
a r > m ruse
::MonadState
a m =>Simple
Iso
a c > m cuse
::MonadState
a m =>Simple
Lens
a c > m cuse
:: (MonadState
a m,Monoid
r) =>Simple
Traversal
a 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
::MonadState
a m =>Getter
a c > (c > e) > m euses
:: (MonadState
a m,Monoid
r) =>Fold
a c > (c > r) > m ruses
::MonadState
a m =>Simple
Lens
a c > (c > e) > m euses
::MonadState
a m =>Simple
Iso
a c > (c > e) > m euses
:: (MonadState
a m,Monoid
r) =>Simple
Traversal
a 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
::MonadReader
a m =>Getter
a c > m cquery
:: (MonadReader
a m,Monoid
c) =>Fold
a c > m cquery
::MonadReader
a m =>Simple
Iso
a c > m cquery
::MonadReader
a m =>Simple
Lens
a c > m cquery
:: (MonadReader
a m,Monoid
c) =>Simple
Traversal
a 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
::MonadReader
a m =>Getter
a c > (c > e) > m equeries
:: (MonadReader
a m,Monoid
c) =>Fold
a c > (c > e) > m equeries
::MonadReader
a m =>Simple
Iso
a c > (c > e) > m equeries
::MonadReader
a m =>Simple
Lens
a c > (c > e) > m equeries
:: (MonadReader
a m,Monoid
c) =>Simple
Traversal
a c > (c > e) > m e
Storing Getters
newtype ReifiedGetter a c Source
Useful for storing getters in containers.
ReifyGetter  

class Functor f => Gettable f Source
Generalizing Const
so we can apply simple Applicative
transformations to it and so we can get nicer error messages
A Gettable
Functor
ignores its argument, which it carries solely as a
phantom type parameter.
To ensure this, an instance of Gettable
is required to satisfy:
id
=fmap
f =coerce
Functor (Const r) => Gettable (Const r)  
(Functor (Backwards f), Gettable f) => Gettable (Backwards f)  
Functor (Accessor r) => Gettable (Accessor r)  
(Functor (ElementOf f), Gettable f) => Gettable (ElementOf f)  This instance is a lie, but it is a useful lie. 
(Functor (Indexing f), Gettable f) => Gettable (Indexing f)  
(Functor (Compose f g), Functor f, Gettable g) => Gettable (Compose f g)  
Functor (Effect m r) => Gettable (Effect m r)  
Functor (EffectRWS w s m c) => Gettable (EffectRWS w s m c) 
Used instead of Const
to report
No instance of (Settable
Accessor
)
when the user attempts to misuse a Setter
as a
Getter
, rather than a monolithic unification error.
(Monad Identity, Gettable (Accessor r)) => Effective Identity r (Accessor r)  
Functor (Accessor r)  
(Functor (Accessor r), Monoid r) => Applicative (Accessor r)  
Functor (Accessor r) => Gettable (Accessor r)  
(MonadReader b ((>) b), MonadReader a ((>) a)) => Magnify ((>) b) ((>) a) Accessor b a 
