@@ -1,9 +1,11 @@
1{-# LANGUAGE CPP #-}
1{-# LANGUAGE CPP #-}
2−{-# LANGUAGE TypeFamilies #-}
2+{-# LANGUAGE RankNTypes #-}
3−{-# LANGUAGE UndecidableInstances #-}
3+-- | Lifting the @callCC@ operation.
4module Control.Monad.Trans.Lift.CallCC
4module Control.Monad.Trans.Lift.CallCC
5 ( LiftCallCC(..)
5 ( LiftCallCC(..)
7 , module Control.Monad.Trans.Class
9 , module Control.Monad.Trans.Class
@@ -13,7 +15,6 @@ import Data.Monoid
14import Control.Monad.Signatures
16import Control.Monad.Signatures
15import Control.Monad.Trans.Class
17import Control.Monad.Trans.Class
16−import Control.Monad.Trans.Control
18import qualified Control.Monad.Trans.Except as E
19import qualified Control.Monad.Trans.Except as E
19import qualified Control.Monad.Trans.Identity as I
20import qualified Control.Monad.Trans.Identity as I
@@ -22,51 +23,124 @@ import qualified Control.Monad.Trans.Maybe as M
22import qualified Control.Monad.Trans.Reader as R
23import qualified Control.Monad.Trans.Reader as R
23import qualified Control.Monad.Trans.RWS.Lazy as RWS.Lazy
24import qualified Control.Monad.Trans.RWS.Lazy as RWS.Lazy
24import qualified Control.Monad.Trans.RWS.Strict as RWS.Strict
25import qualified Control.Monad.Trans.RWS.Strict as RWS.Strict
26+import qualified Control.Monad.Trans.RWS.CPS as RWS.CPS
25import qualified Control.Monad.Trans.State.Lazy as S.Lazy
27import qualified Control.Monad.Trans.State.Lazy as S.Lazy
26import qualified Control.Monad.Trans.State.Strict as S.Strict
28import qualified Control.Monad.Trans.State.Strict as S.Strict
27import qualified Control.Monad.Trans.Writer.Lazy as W.Lazy
29import qualified Control.Monad.Trans.Writer.Lazy as W.Lazy
28import qualified Control.Monad.Trans.Writer.Strict as W.Strict
30import qualified Control.Monad.Trans.Writer.Strict as W.Strict
31+import qualified Control.Monad.Trans.Writer.CPS as W.CPS
33+#if MIN_VERSION_transformers(0,5,3)
34+import qualified Control.Monad.Trans.Accum as Acc
37+import Control.Monad.Trans.Lift.StT
39+-- | The class of monad transformers capable of lifting 'callCC'.
30class MonadTrans t => LiftCallCC t where
40class MonadTrans t => LiftCallCC t where
31− type CallCCStT t a
41+ -- | Lift the @callCC@ operation.
32− type CallCCStT t a = StT t a
42+ -- Should satisfy the uniformity property
44+ -- * @'lift' (f k) = f' ('lift' . k) => 'lift' (cf f) = 'liftCallCC' cf f'@
33− liftCallCC :: Monad m => CallCC m (CallCCStT t a) (CallCCStT t b) -> CallCC (t m) a b
46+ liftCallCC :: Monad m => CallCC m (StT t a) (StT t b) -> CallCC (t m) a b
48+ -- | Lift the @callCC@ operation.
49+ -- This is an alternative version of 'liftCallCC' included for historical
50+ -- reasons. It has a different lifting behavior for the @StateT@ and @RWST@
51+ -- monad transformers. Matches what @mtl@ does but doesn't satisfy the
52+ -- uniformity property.
34− liftCallCC' :: Monad m => CallCC m (CallCCStT t a) (CallCCStT t b) -> CallCC (t m) a b
53+ liftCallCC' :: Monad m => CallCC m (StT t a) (StT t b) -> CallCC (t m) a b
35 liftCallCC' = liftCallCC
54 liftCallCC' = liftCallCC
56+-- | Default definition for the 'liftCallCC' method.
58+ :: (Monad m, LiftCallCC n)
59+ => (forall x . n m x -> t m x)
60+ -- ^ Monad constructor
61+ -> (forall o x . t o x -> n o x)
62+ -- ^ Monad deconstructor
63+ -> CallCC m (StT n a) (StT n b)
65+defaultLiftCallCC t unT callCC f
66+ = t $ liftCallCC callCC (\g -> (unT . f) (t . g))
68+-- | Default definition for the 'liftCallCC'' method.
70+ :: (Monad m, LiftCallCC n)
71+ => (forall x . n m x -> t m x)
72+ -- ^ Monad constructor
73+ -> (forall o x . t o x -> n o x)
74+ -- ^ Monad deconstructor
75+ -> CallCC m (StT n a) (StT n b)
77+defaultLiftCallCC' t unT callCC f
78+ = t $ liftCallCC' callCC (\g -> (unT . f) (t . g))
37instance LiftCallCC (E.ExceptT e) where
80instance LiftCallCC (E.ExceptT e) where
38− liftCallCC = E.liftCallCC
81+ liftCallCC = E.liftCallCC
82+ {-# INLINE liftCallCC #-}
40instance LiftCallCC I.IdentityT where
84instance LiftCallCC I.IdentityT where
41− liftCallCC = I.liftCallCC
85+ liftCallCC = I.liftCallCC
86+ {-# INLINE liftCallCC #-}
43instance LiftCallCC L.ListT where
88instance LiftCallCC L.ListT where
44− liftCallCC = L.liftCallCC
89+ liftCallCC = L.liftCallCC
90+ {-# INLINE liftCallCC #-}
46instance LiftCallCC M.MaybeT where
92instance LiftCallCC M.MaybeT where
47− liftCallCC = M.liftCallCC
93+ liftCallCC = M.liftCallCC
94+ {-# INLINE liftCallCC #-}
49instance LiftCallCC (R.ReaderT r) where
96instance LiftCallCC (R.ReaderT r) where
50− liftCallCC = R.liftCallCC
97+ liftCallCC = R.liftCallCC
98+ {-# INLINE liftCallCC #-}
100+instance Monoid w => LiftCallCC (W.Lazy.WriterT w) where
101+ liftCallCC = W.Lazy.liftCallCC
102+ {-# INLINE liftCallCC #-}
104+instance Monoid w => LiftCallCC (W.Strict.WriterT w) where
105+ liftCallCC = W.Strict.liftCallCC
106+ {-# INLINE liftCallCC #-}
108+instance Monoid w => LiftCallCC (W.CPS.WriterT w) where
109+ liftCallCC = W.CPS.liftCallCC
110+ {-# INLINE liftCallCC #-}
52instance Monoid w => LiftCallCC (RWS.Lazy.RWST r w s) where
112instance Monoid w => LiftCallCC (RWS.Lazy.RWST r w s) where
53 liftCallCC = RWS.Lazy.liftCallCC
113 liftCallCC = RWS.Lazy.liftCallCC
114+ {-# INLINE liftCallCC #-}
54 liftCallCC' = RWS.Lazy.liftCallCC'
115 liftCallCC' = RWS.Lazy.liftCallCC'
116+ {-# INLINE liftCallCC' #-}
56instance Monoid w => LiftCallCC (RWS.Strict.RWST r w s) where
118instance Monoid w => LiftCallCC (RWS.Strict.RWST r w s) where
57 liftCallCC = RWS.Strict.liftCallCC
119 liftCallCC = RWS.Strict.liftCallCC
120+ {-# INLINE liftCallCC #-}
58 liftCallCC' = RWS.Strict.liftCallCC'
121 liftCallCC' = RWS.Strict.liftCallCC'
122+ {-# INLINE liftCallCC' #-}
124+instance Monoid w => LiftCallCC (RWS.CPS.RWST r w s) where
125+ liftCallCC = RWS.CPS.liftCallCC
126+ {-# INLINE liftCallCC #-}
127+ liftCallCC' = RWS.CPS.liftCallCC'
128+ {-# INLINE liftCallCC' #-}
60instance LiftCallCC (S.Lazy.StateT s) where
130instance LiftCallCC (S.Lazy.StateT s) where
61 liftCallCC = S.Lazy.liftCallCC
131 liftCallCC = S.Lazy.liftCallCC
132+ {-# INLINE liftCallCC #-}
62 liftCallCC' = S.Lazy.liftCallCC'
133 liftCallCC' = S.Lazy.liftCallCC'
134+ {-# INLINE liftCallCC' #-}
64instance LiftCallCC (S.Strict.StateT s) where
136instance LiftCallCC (S.Strict.StateT s) where
65 liftCallCC = S.Strict.liftCallCC
137 liftCallCC = S.Strict.liftCallCC
138+ {-# INLINE liftCallCC #-}
66 liftCallCC' = S.Strict.liftCallCC'
139 liftCallCC' = S.Strict.liftCallCC'
140+ {-# INLINE liftCallCC' #-}
142+#if MIN_VERSION_transformers(0,5,3)
68−instance Monoid w => LiftCallCC (W.Lazy.WriterT w) where
143+instance Monoid w => LiftCallCC (Acc.AccumT w) where
69− liftCallCC = W.Lazy.liftCallCC
144+ liftCallCC = Acc.liftCallCC
70−
145+ {-# INLINE liftCallCC #-}
71−instance Monoid w => LiftCallCC (W.Strict.WriterT w) where
146+#endif
72− liftCallCC = W.Strict.liftCallCC