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 | Haskell98 |
Control.Lens.Wrapped
Contents
Description
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.
- 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 :: forall s. Rewrapped s s => Unwrapped s -> s
- pattern Unwrapped :: forall t. 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.
Methods
_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.
_Wrapped' :: (Generic s, D1 d (C1 c (S1 s' (Rec0 a))) ~ Rep s, Unwrapped s ~ GUnwrapped (Rep s)) => 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
(~) * t PatternMatchFail => Rewrapped PatternMatchFail t # | |
(~) * t RecSelError => Rewrapped RecSelError t # | |
(~) * t RecConError => Rewrapped RecConError t # | |
(~) * t RecUpdError => Rewrapped RecUpdError t # | |
(~) * t NoMethodError => Rewrapped NoMethodError t # | |
(~) * t TypeError => Rewrapped TypeError t # | |
Rewrapped CDev t # | |
Rewrapped CIno t # | |
Rewrapped CMode t # | |
Rewrapped COff t # | |
Rewrapped CPid t # | |
Rewrapped CSsize t # | |
Rewrapped CGid t # | |
Rewrapped CNlink t # | |
Rewrapped CUid t # | |
Rewrapped CCc t # | |
Rewrapped CSpeed t # | |
Rewrapped CTcflag t # | |
Rewrapped CRLim t # | |
Rewrapped Fd t # | |
Rewrapped Errno t # | |
(~) * t AssertionFailed => Rewrapped AssertionFailed t # | |
Rewrapped CChar t # | |
Rewrapped CSChar t # | |
Rewrapped CUChar t # | |
Rewrapped CShort t # | |
Rewrapped CUShort t # | |
Rewrapped CInt t # | |
Rewrapped CUInt t # | |
Rewrapped CLong t # | |
Rewrapped CULong t # | |
Rewrapped CLLong t # | |
Rewrapped CULLong t # | |
Rewrapped CFloat t # | |
Rewrapped CDouble t # | |
Rewrapped CPtrdiff t # | |
Rewrapped CSize t # | |
Rewrapped CWchar t # | |
Rewrapped CSigAtomic t # | |
Rewrapped CClock t # | |
Rewrapped CTime t # | |
Rewrapped CUSeconds t # | |
Rewrapped CSUSeconds t # | |
Rewrapped CIntPtr t # | |
Rewrapped CUIntPtr t # | |
Rewrapped CIntMax t # | |
Rewrapped CUIntMax t # | |
(~) * t All => Rewrapped All t # | |
(~) * t Any => Rewrapped Any t # | |
(~) * t ErrorCall => Rewrapped ErrorCall t # | |
(~) * t IntSet => Rewrapped IntSet t # | Use |
(~) * t (Par1 p') => Rewrapped (Par1 p) t # | |
(~) * t (Identity b) => Rewrapped (Identity a) t # | |
(~) * t (Min b) => Rewrapped (Min a) t # | |
(~) * t (Max b) => Rewrapped (Max a) t # | |
(~) * t (First b) => Rewrapped (First a) t # | |
(~) * t (Last b) => Rewrapped (Last a) t # | |
(~) * t (WrappedMonoid b) => Rewrapped (WrappedMonoid a) t # | |
(~) * t (Option b) => Rewrapped (Option a) t # | |
(~) * t (NonEmpty b) => Rewrapped (NonEmpty a) t # | |
(~) * t (ZipList b) => Rewrapped (ZipList a) t # | |
(~) * t (Dual b) => Rewrapped (Dual a) t # | |
(~) * t (Endo b) => Rewrapped (Endo a) t # | |
(~) * t (Sum b) => Rewrapped (Sum a) t # | |
(~) * t (Product b) => Rewrapped (Product a) t # | |
(~) * t (First b) => Rewrapped (First a) t # | |
(~) * t (Last b) => Rewrapped (Last a) t # | |
(~) * t (Down b) => Rewrapped (Down a) t # | |
(~) * t (IntMap a') => Rewrapped (IntMap a) t # | Use |
(~) * t (Seq a') => Rewrapped (Seq a) t # | |
((~) * t (Set a'), Ord a) => Rewrapped (Set a) t # | Use |
(~) * t (Predicate b) => Rewrapped (Predicate a) t # | |
(~) * t (Comparison b) => Rewrapped (Comparison a) t # | |
(~) * t (Equivalence b) => Rewrapped (Equivalence a) t # | |
((~) * t (HashSet a'), Hashable a, Eq a) => Rewrapped (HashSet a) t # | Use |
(~) * t (Vector a') => Rewrapped (Vector a) t # | |
(Storable a, (~) * t (Vector a')) => Rewrapped (Vector a) t # | |
(Unbox a, (~) * t (Vector a')) => Rewrapped (Vector a) t # | |
(Prim a, (~) * t (Vector a')) => Rewrapped (Vector a) t # | |
(~) * t (Rec1 f' p') => Rewrapped (Rec1 f p) t # | |
(~) * t (Op a' b') => Rewrapped (Op a b) t # | |
(~) * t (WrappedMonad m' a') => Rewrapped (WrappedMonad m a) t # | |
(~) * t (ArrowMonad m' a') => Rewrapped (ArrowMonad m a) t # | |
((~) * t (Map k' a'), Ord k) => Rewrapped (Map k a) t # | Use |
(~) * t (CatchT m' a') => Rewrapped (CatchT m a) t # | |
(~) * t (Alt f' a') => Rewrapped (Alt f a) t # | |
(~) * t (CoiterT w' a') => Rewrapped (CoiterT w a) t # | |
(~) * t (IterT m' a') => Rewrapped (IterT m a) t # | |
(~) * t (ListT n b) => Rewrapped (ListT m a) t # | |
((~) * t (HashMap k' a'), Hashable k, Eq k) => Rewrapped (HashMap k a) t # | Use |
(~) * t (WrappedApplicative f' a') => Rewrapped (WrappedApplicative f a) t # | |
(~) * t (MaybeApply f' a') => Rewrapped (MaybeApply f a) t # | |
(~) * t (MaybeT n b) => Rewrapped (MaybeT m a) t # | |
(~) * t (K1 i' c' p') => Rewrapped (K1 i c p) t # | |
(~) * t ((:.:) f' g' p') => Rewrapped ((:.:) f g p) t # | |
(~) * t (WrappedArrow a' b' c') => Rewrapped (WrappedArrow a b c) t # | |
(~) * t (Kleisli m' a' b') => Rewrapped (Kleisli m a b) t # | |
(~) * t (Const k1 a' x') => Rewrapped (Const k a x) t # | |
(~) * t (Alt k1 g b) => Rewrapped (Alt k f a) t # | |
(~) * t (Join k1 p' a') => Rewrapped (Join k p a) t # | |
(~) * t (Fix k1 p' a') => Rewrapped (Fix k p a) t # | |
(~) * t (TracedT m' w' a') => Rewrapped (TracedT m w a) t # | |
(~) * t (IdentityT k1 n b) => Rewrapped (IdentityT k m a) t # | |
(~) * t (Compose f' g' a') => Rewrapped (Compose f g a) t # | |
(~) * t (ComposeFC f' g' a') => Rewrapped (ComposeFC f g a) t # | |
(~) * t (ComposeCF f' g' a') => Rewrapped (ComposeCF f g a) t # | |
(~) * t (ApT f' g' a') => Rewrapped (ApT f g a) t # | |
(~) * t (CofreeT f' w' a') => Rewrapped (CofreeT f w a) t # | |
(~) * t (FreeT f' m' a') => Rewrapped (FreeT f m a) t # | |
(~) * t (ErrorT e' m' a') => Rewrapped (ErrorT e m a) t # | |
(~) * t (ExceptT e' m' a') => Rewrapped (ExceptT e m a) t # | |
(~) * t (StateT s' m' a') => Rewrapped (StateT s m a) t # | |
(~) * t (StateT s' m' a') => Rewrapped (StateT s m a) t # | |
(~) * t (WriterT w' m' a') => Rewrapped (WriterT w m a) t # | |
(~) * t (WriterT w' m' a') => Rewrapped (WriterT w m a) t # | |
(~) * t (Star f' d' c') => Rewrapped (Star f d c) t # | |
(~) * t (Costar f' d' c') => Rewrapped (Costar f d c) t # | |
(~) * t (WrappedArrow p' a' b') => Rewrapped (WrappedArrow p a b) t # | |
(~) * t (Forget r' a' b') => Rewrapped (Forget r a b) t # | |
(~) * t (Static f' a' b') => Rewrapped (Static f a b) t # | |
(~) * t (Tagged k1 s' a') => Rewrapped (Tagged k s a) t # | |
(~) * t (Reverse k1 g b) => Rewrapped (Reverse k f a) t # | |
(~) * t (Backwards k1 g b) => Rewrapped (Backwards k f a) t # | |
(~) * t (Constant k1 a' b') => Rewrapped (Constant k a b) t # | |
(~) * t (M1 i' c' f' p') => Rewrapped (M1 i c f p) t # | |
(~) * t (ContT k1 r' m' a') => Rewrapped (ContT k r m a) t # | |
(~) * t (ReaderT k1 s n b) => Rewrapped (ReaderT k r m a) t # | |
(~) * t (Cayley f' p' a' b') => Rewrapped (Cayley f p a b) t # | |
(~) * t (Compose k2 k3 f' g' a') => Rewrapped (Compose k k1 f g a) t # | |
(~) * t (WrappedBifunctor k2 k3 p' a' b') => Rewrapped (WrappedBifunctor k k1 p a b) t # | |
(~) * t (Joker k2 k3 g' a' b') => Rewrapped (Joker k k1 g a b) t # | |
(~) * t (Flip k2 k3 p' a' b') => Rewrapped (Flip k k1 p a b) t # | |
(~) * t (Clown k2 k3 f' a' b') => Rewrapped (Clown k k1 f a b) t # | |
(~) * t (RWST r' w' s' m' a') => Rewrapped (RWST r w s m a) t # | |
(~) * t (RWST r' w' s' m' a') => Rewrapped (RWST r w s m a) t # | |
(~) * t (Dual k2 k3 k' a' b') => Rewrapped (Dual k k1 k4 a b) t # | |
(~) * t (WrappedCategory k2 k3 k' a' b') => Rewrapped (WrappedCategory k k1 k4 a b) t # | |
(~) * t (Semi k2 k3 m' a' b') => Rewrapped (Semi k k1 m a b) t # | |
(~) * t (Tannen k4 k5 k6 f' p' a' b') => Rewrapped (Tannen k k1 k2 f p a b) t # | |
(~) * t (Biff k4 k5 k6 k7 p' f' g' a' b') => Rewrapped (Biff k k1 k2 k3 p f g a b) t # | |
class (Rewrapped s t, Rewrapped t s) => Rewrapping s t #
Instances
(Rewrapped s t, Rewrapped t s) => Rewrapping s t # | |
_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