Copyright | (C) 2012-16 Edward Kmett Michael Sloan |
---|---|
License | BSD-style (see the file LICENSE) |
Maintainer | Edward Kmett <ekmett@gmail.com> |
Stability | experimental |
Portability | Rank2, MPTCs, fundeps |
Safe Haskell | Trustworthy |
Language | Haskell2010 |
The Wrapped
class provides similar functionality as Control.Newtype
,
from the newtype
package, but in a more convenient and efficient form.
There are a few functions from newtype
that are not provided here, because
they can be done with the Iso
directly:
Control.Newtype.overSum
f ≡_Unwrapping
Sum
%~
f Control.Newtype.underSum
f ≡_Wrapping
Sum
%~
f Control.Newtype.overFSum
f ≡mapping
(_Unwrapping
Sum
)%~
f Control.Newtype.underFSum
f ≡mapping
(_Wrapping
Sum
)%~
f
under
can also be used with _Unwrapping
to provide the equivalent of
Control.Newtype.under
. Also, most use cases don't need full polymorphism,
so only the single constructor _Wrapping
functions would be needed.
These equivalences aren't 100% honest, because newtype
's operators
need to rely on two Newtype
constraints. This means that the wrapper used
for the output is not necessarily the same as the input.
Synopsis
- class Wrapped s where
- _Unwrapped' :: Wrapped s => Iso' (Unwrapped s) s
- _Wrapping' :: Wrapped s => (Unwrapped s -> s) -> Iso' s (Unwrapped s)
- _Unwrapping' :: Wrapped s => (Unwrapped s -> s) -> Iso' (Unwrapped s) s
- class Wrapped s => Rewrapped (s :: *) (t :: *)
- class (Rewrapped s t, Rewrapped t s) => Rewrapping s t
- _Wrapped :: Rewrapping s t => Iso s t (Unwrapped s) (Unwrapped t)
- _Unwrapped :: Rewrapping s t => Iso (Unwrapped t) (Unwrapped s) t s
- _Wrapping :: Rewrapping s t => (Unwrapped s -> s) -> Iso s t (Unwrapped s) (Unwrapped t)
- _Unwrapping :: Rewrapping s t => (Unwrapped s -> s) -> Iso (Unwrapped t) (Unwrapped s) t s
- op :: Wrapped s => (Unwrapped s -> s) -> s -> Unwrapped s
- ala :: (Functor f, Rewrapping s t) => (Unwrapped s -> s) -> ((Unwrapped t -> t) -> f s) -> f (Unwrapped s)
- alaf :: (Functor f, Functor g, Rewrapping s t) => (Unwrapped s -> s) -> (f t -> g s) -> f (Unwrapped t) -> g (Unwrapped s)
- pattern Wrapped :: Rewrapped s s => Unwrapped s -> s
- pattern Unwrapped :: Rewrapped t t => t -> Unwrapped t
- _GWrapped' :: (Generic s, D1 d (C1 c (S1 s' (Rec0 a))) ~ Rep s, Unwrapped s ~ GUnwrapped (Rep s)) => Iso' s (Unwrapped s)
Wrapping and Unwrapping monomorphically
Wrapped
provides isomorphisms to wrap and unwrap newtypes or
data types with one constructor.
Nothing
_Wrapped' :: Iso' s (Unwrapped s) #
An isomorphism between s
and a
.
If your type has a Generic
instance, _Wrapped'
will default to _GWrapped'
,
and you can choose to not override it with your own definition.
Instances
_Unwrapped' :: Wrapped s => Iso' (Unwrapped s) s #
_Wrapping' :: Wrapped s => (Unwrapped s -> s) -> Iso' s (Unwrapped s) #
This is a convenient version of _Wrapped
with an argument that's ignored.
The user supplied function is ignored, merely its type is used.
_Unwrapping' :: Wrapped s => (Unwrapped s -> s) -> Iso' (Unwrapped s) s #
This is a convenient version of _Wrapped
with an argument that's ignored.
The user supplied function is ignored, merely its type is used.
Wrapping and unwrapping polymorphically
class Wrapped s => Rewrapped (s :: *) (t :: *) #
Instances
class (Rewrapped s t, Rewrapped t s) => Rewrapping s t #
Instances
(Rewrapped s t, Rewrapped t s) => Rewrapping s t # | |
Defined in Control.Lens.Wrapped |
_Wrapped :: Rewrapping s t => Iso s t (Unwrapped s) (Unwrapped t) #
Work under a newtype wrapper.
>>>
Const "hello" & _Wrapped %~ Prelude.length & getConst
5
_Wrapped
≡from
_Unwrapped
_Unwrapped
≡from
_Wrapped
_Unwrapped :: Rewrapping s t => Iso (Unwrapped t) (Unwrapped s) t s #
_Wrapping :: Rewrapping s t => (Unwrapped s -> s) -> Iso s t (Unwrapped s) (Unwrapped t) #
This is a convenient version of _Wrapped
with an argument that's ignored.
The user supplied function is ignored, merely its types are used.
_Unwrapping :: Rewrapping s t => (Unwrapped s -> s) -> Iso (Unwrapped t) (Unwrapped s) t s #
This is a convenient version of _Unwrapped
with an argument that's ignored.
The user supplied function is ignored, merely its types are used.
Operations
ala :: (Functor f, Rewrapping s t) => (Unwrapped s -> s) -> ((Unwrapped t -> t) -> f s) -> f (Unwrapped s) #
This combinator is based on ala
from Conor McBride's work on Epigram.
As with _Wrapping
, the user supplied function for the newtype is ignored.
>>>
ala Sum foldMap [1,2,3,4]
10
>>>
ala All foldMap [True,True]
True
>>>
ala All foldMap [True,False]
False
>>>
ala Any foldMap [False,False]
False
>>>
ala Any foldMap [True,False]
True
>>>
ala Product foldMap [1,2,3,4]
24
You may want to think of this combinator as having the following, simpler, type.
ala :: Rewrapping s t => (Unwrapped s -> s) -> ((Unwrapped t -> t) -> e -> s) -> e -> Unwrapped s
alaf :: (Functor f, Functor g, Rewrapping s t) => (Unwrapped s -> s) -> (f t -> g s) -> f (Unwrapped t) -> g (Unwrapped s) #
This combinator is based on ala'
from Conor McBride's work on Epigram.
As with _Wrapping
, the user supplied function for the newtype is ignored.
alaf :: Rewrapping s t => (Unwrapped s -> s) -> ((r -> t) -> e -> s) -> (r -> Unwrapped t) -> e -> Unwrapped s
>>>
alaf Sum foldMap Prelude.length ["hello","world"]
10