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