on master help sign in
Use liftCatch from transformers-lift
commit67ca0b060c6267f7d6ab6d1fb653a15d9e68850a authorIndex Int <vlad.z.4096@gmail.com> dateThu 18 Jun 2015, 18:16 (11 years ago) parent29f3d4e
8 files changed+50−92
ether.cabal+1 src/Control/Ether/Util.hs+1−6 src/Control/Monad/Ether/Except/Class.hs+7−79 src/Control/Monad/Trans/Ether/Except.hs+9−7 src/Control/Monad/Trans/Ether/Reader.hs+8 src/Control/Monad/Trans/Ether/State/Lazy.hs+8 src/Control/Monad/Trans/Ether/State/Strict.hs+8 src/Control/Monad/Trans/Ether/Writer.hs+8
ether.cabal+1
@@ -61,6 +61,7 @@ library
6161 , transformers-lift >=0.1
6262 , mtl >=2.2.1
6363 , mmorph >=1.0.4
64+ , monad-control >=1.0.0.4
6465 , template-haskell >=2.9
6566 , newtype-generics >=0.4.1
6667
src/Control/Ether/Util.hs+1−6
@@ -4,8 +4,7 @@
44{-# LANGUAGE TypeOperators #-}
55{-# LANGUAGE TypeFamilies #-}
66module Control.Ether.Util
7− ( liftCatch_ExceptT
8− , liftListen_WriterT
7+ ( liftListen_WriterT
98 , liftPass_WriterT
109 , type (++)
1110 , MaybeToList
@@ -13,7 +12,6 @@ module Control.Ether.Util
1312 ) where
1413
1514import qualified Control.Monad.Signatures as Sig
16−import qualified Control.Monad.Trans.Except as Trans
1715import qualified Control.Monad.Trans.Writer as Trans
1816import Prelude hiding (fmap)
1917
@@ -31,9 +29,6 @@ type family MaybeToList (mt :: Maybe k) :: [k] where
3129 MaybeToList 'Nothing = '[]
3230 MaybeToList ('Just t) = '[t]
3331
34−liftCatch_ExceptT :: Sig.Catch e m (Either e' a) -> Sig.Catch e (Trans.ExceptT e' m) a
35−liftCatch_ExceptT catchE m h = Trans.ExceptT $ catchE (Trans.runExceptT m) (Trans.runExceptT . h)
36−
3732-- TODO: Not sure if correct
3833liftListen_WriterT :: Monad m => Sig.Listen w' m (a, w) -> Sig.Listen w' (Trans.WriterT w m) a
3934liftListen_WriterT listen m = Trans.WriterT $ do
src/Control/Monad/Ether/Except/Class.hs+7−79
@@ -20,27 +20,9 @@ module Control.Monad.Ether.Except.Class
2020import Data.Monoid
2121#endif
2222
23−import Data.Proxy (Proxy(Proxy))
24−import Control.Monad.Trans (lift)
25−
2623import Control.Monad.Trans.Ether.Except hiding (throw, catch)
2724import qualified Control.Monad.Trans.Ether.Except as E
28−import qualified Control.Monad.Trans.Ether.Reader as R
29−import qualified Control.Monad.Trans.Ether.Writer as W
30−import qualified Control.Monad.Trans.Ether.State.Lazy as S.L
31−import qualified Control.Monad.Trans.Ether.State.Strict as S.S
32−import qualified Control.Ether.Util as Util
33−
34−-- for mtl instances
35−import qualified Control.Monad.Trans.Except as Trans.E
36−import qualified Control.Monad.Trans.Identity as Trans.I
37−import qualified Control.Monad.Trans.List as Trans.L
38−import qualified Control.Monad.Trans.Maybe as Trans.M
39−import qualified Control.Monad.Trans.Reader as Trans.R
40−import qualified Control.Monad.Trans.State.Lazy as Trans.S.Lazy
41−import qualified Control.Monad.Trans.State.Strict as Trans.S.Strict
42−import qualified Control.Monad.Trans.Writer.Lazy as Trans.W.Lazy
43−import qualified Control.Monad.Trans.Writer.Strict as Trans.W.Strict
25+import qualified Control.Monad.Trans.Lift.Catch as Lift
4426
4527
4628-- | See 'Control.Monad.Except.MonadError'.
@@ -57,63 +39,9 @@ instance {-# OVERLAPPING #-} Monad m => MonadExcept tag e (ExceptT tag e m) wher
5739 throw = E.throw
5840 catch = E.catch
5941
60−instance MonadExcept tag e m => MonadExcept tag e (ExceptT tag' e' m) where
61− throw t = lift . throw t
62− catch t = liftCatch Proxy (catch t)
63−
64−-- Instances for other tagged transformers
65−
66−instance MonadExcept tag e m => MonadExcept tag e (R.ReaderT tag' r m) where
67− throw t = lift . throw t
68− catch t = R.liftCatch Proxy (catch t)
69−
70−instance (Monoid w, MonadExcept tag e m) => MonadExcept tag e (W.WriterT tag' w m) where
71− throw t = lift . throw t
72− catch t = W.liftCatch Proxy (catch t)
73−
74−instance MonadExcept tag e m => MonadExcept tag e (S.L.StateT tag' s m) where
75− throw t = lift . throw t
76− catch t = S.L.liftCatch Proxy (catch t)
77−
78−instance MonadExcept tag e m => MonadExcept tag e (S.S.StateT tag' s m) where
79− throw t = lift . throw t
80− catch t = S.S.liftCatch Proxy (catch t)
81−
82−
83−-- Instances for mtl transformers
84−
85−instance MonadExcept tag e m => MonadExcept tag e (Trans.E.ExceptT e' m) where
86− throw t = lift . throw t
87− catch t = Util.liftCatch_ExceptT (catch t)
88−
89−instance MonadExcept tag e m => MonadExcept tag e (Trans.I.IdentityT m) where
90− throw t = lift . throw t
91− catch t = Trans.I.liftCatch (catch t)
92−
93−instance MonadExcept tag e m => MonadExcept tag e (Trans.L.ListT m) where
94− throw t = lift . throw t
95− catch t = Trans.L.liftCatch (catch t)
96−
97−instance MonadExcept tag e m => MonadExcept tag e (Trans.M.MaybeT m) where
98− throw t = lift . throw t
99− catch t = Trans.M.liftCatch (catch t)
100−
101−instance MonadExcept tag e m => MonadExcept tag e (Trans.R.ReaderT r m) where
102− throw t = lift . throw t
103− catch t = Trans.R.liftCatch (catch t)
104−
105−instance MonadExcept tag e m => MonadExcept tag e (Trans.S.Lazy.StateT s m) where
106− throw t = lift . throw t
107− catch t = Trans.S.Lazy.liftCatch (catch t)
108−
109−instance MonadExcept tag e m => MonadExcept tag e (Trans.S.Strict.StateT s m) where
110− throw t = lift . throw t
111− catch t = Trans.S.Strict.liftCatch (catch t)
112−
113−instance (Monoid w, MonadExcept tag e m) => MonadExcept tag e (Trans.W.Lazy.WriterT w m) where
114− throw t = lift . throw t
115− catch t = Trans.W.Lazy.liftCatch (catch t)
116−
117−instance (Monoid w, MonadExcept tag e m) => MonadExcept tag e (Trans.W.Strict.WriterT w m) where
118− throw t = lift . throw t
119− catch t = Trans.W.Strict.liftCatch (catch t)
42+instance ( Lift.LiftCatch t
43+ , Monad (t m)
44+ , MonadExcept tag e m
45+ ) => MonadExcept tag e (t m) where
46+ throw t = Lift.lift . throw t
47+ catch t = Lift.liftCatch (catch t)
src/Control/Monad/Trans/Ether/Except.hs+9−7
@@ -26,7 +26,6 @@ module Control.Monad.Trans.Ether.Except
2626 , liftCallCC
2727 , liftListen
2828 , liftPass
29− , liftCatch
3029 ) where
3130
3231import Data.Proxy (Proxy(Proxy))
@@ -39,13 +38,14 @@ import Control.Monad.Trans.Class (MonadTrans, lift)
3938import Control.Monad.IO.Class (MonadIO)
4039import Control.Monad.Morph (MFunctor, MMonad)
4140import Control.Ether.Tagged (Taggable(..), Tagged(..))
42−import qualified Control.Ether.Util as Util
4341import GHC.Generics (Generic)
4442import qualified Control.Newtype as NT
4543
4644import qualified Control.Monad.Signatures as Sig
45+import qualified Control.Monad.Trans.Control as MC
4746import qualified Control.Monad.Trans.Except as Trans
4847import qualified Control.Monad.Trans.Lift.Local as Lift
48+import qualified Control.Monad.Trans.Lift.Catch as Lift
4949
5050import qualified Control.Monad.Cont.Class as Class
5151import qualified Control.Monad.Reader.Class as Class
@@ -78,7 +78,13 @@ newtype ExceptT tag e m a = ExceptT (Trans.ExceptT e m a)
7878
7979instance NT.Newtype (ExceptT tag e m a)
8080
81+instance MC.MonadTransControl (ExceptT tag e) where
82+ type StT (ExceptT tag e) a = MC.StT (Trans.ExceptT e) a
83+ liftWith f = ExceptT $ MC.liftWith (f . coerce)
84+ restoreT = ExceptT . MC.restoreT
85+
8186instance Lift.LiftLocal (ExceptT tag e)
87+instance Lift.LiftCatch (ExceptT tag e)
8288
8389instance Taggable (ExceptT tag e m) where
8490 type Tag (ExceptT tag e m) = 'Just tag
@@ -130,10 +136,6 @@ liftListen t listen m = tagged t $ Trans.liftListen listen (coerce m)
130136liftPass :: Monad m => proxy tag -> Sig.Pass w m (Either e a) -> Sig.Pass w (ExceptT tag e m) a
131137liftPass t pass m = tagged t $ Trans.liftPass pass (coerce m)
132138
133−-- | Lift a @catchE@ operation to the new monad.
134−liftCatch :: proxy tag -> Sig.Catch e m (Either e' a) -> Sig.Catch e (ExceptT tag e' m) a
135−liftCatch t catchE m h = tagged t $ Util.liftCatch_ExceptT catchE (coerce m) (coerce h)
136−
137139instance Class.MonadCont m => Class.MonadCont (ExceptT tag e m) where
138140 callCC = liftCallCC Proxy Class.callCC
139141
@@ -155,4 +157,4 @@ instance Class.MonadWriter w m => Class.MonadWriter w (ExceptT tag e m) where
155157
156158instance Class.MonadError e' m => Class.MonadError e' (ExceptT tag e m) where
157159 throwError = lift . Class.throwError
158− catchError = liftCatch Proxy Class.catchError
160+ catchError = Lift.liftCatch Class.catchError
src/Control/Monad/Trans/Ether/Reader.hs+8
@@ -42,8 +42,10 @@ import GHC.Generics (Generic)
4242import qualified Control.Newtype as NT
4343
4444import qualified Control.Monad.Signatures as Sig
45+import qualified Control.Monad.Trans.Control as MC
4546import qualified Control.Monad.Trans.Reader as Trans
4647import qualified Control.Monad.Trans.Lift.Local as Lift
48+import qualified Control.Monad.Trans.Lift.Catch as Lift
4749
4850import qualified Control.Monad.Cont.Class as Class
4951import qualified Control.Monad.Reader.Class as Class
@@ -71,7 +73,13 @@ newtype ReaderT tag r m a = ReaderT (Trans.ReaderT r m a)
7173
7274instance NT.Newtype (ReaderT tag r m a)
7375
76+instance MC.MonadTransControl (ReaderT tag r) where
77+ type StT (ReaderT tag r) a = MC.StT (Trans.ReaderT r) a
78+ liftWith f = ReaderT $ MC.liftWith (f . coerce)
79+ restoreT = ReaderT . MC.restoreT
80+
7481instance Lift.LiftLocal (ReaderT tag r)
82+instance Lift.LiftCatch (ReaderT tag r)
7583
7684instance Taggable (ReaderT tag r m) where
7785 type Tag (ReaderT tag r m) = 'Just tag
src/Control/Monad/Trans/Ether/State/Lazy.hs+8
@@ -47,8 +47,10 @@ import GHC.Generics (Generic)
4747import qualified Control.Newtype as NT
4848
4949import qualified Control.Monad.Signatures as Sig
50+import qualified Control.Monad.Trans.Control as MC
5051import qualified Control.Monad.Trans.State.Lazy as Trans
5152import qualified Control.Monad.Trans.Lift.Local as Lift
53+import qualified Control.Monad.Trans.Lift.Catch as Lift
5254
5355import qualified Control.Monad.Cont.Class as Class
5456import qualified Control.Monad.Reader.Class as Class
@@ -76,7 +78,13 @@ newtype StateT tag s m a = StateT (Trans.StateT s m a)
7678
7779instance NT.Newtype (StateT tag s m a)
7880
81+instance MC.MonadTransControl (StateT tag s) where
82+ type StT (StateT tag s) a = MC.StT (Trans.StateT s) a
83+ liftWith f = StateT $ MC.liftWith (f . coerce)
84+ restoreT = StateT . MC.restoreT
85+
7986instance Lift.LiftLocal (StateT tag s)
87+instance Lift.LiftCatch (StateT tag s)
8088
8189instance Taggable (StateT tag s m) where
8290 type Tag (StateT tag s m) = 'Just tag
src/Control/Monad/Trans/Ether/State/Strict.hs+8
@@ -47,8 +47,10 @@ import GHC.Generics (Generic)
4747import qualified Control.Newtype as NT
4848
4949import qualified Control.Monad.Signatures as Sig
50+import qualified Control.Monad.Trans.Control as MC
5051import qualified Control.Monad.Trans.State.Strict as Trans
5152import qualified Control.Monad.Trans.Lift.Local as Lift
53+import qualified Control.Monad.Trans.Lift.Catch as Lift
5254
5355import qualified Control.Monad.Cont.Class as Class
5456import qualified Control.Monad.Reader.Class as Class
@@ -76,7 +78,13 @@ newtype StateT tag s m a = StateT (Trans.StateT s m a)
7678
7779instance NT.Newtype (StateT tag s m a)
7880
81+instance MC.MonadTransControl (StateT tag s) where
82+ type StT (StateT tag s) a = MC.StT (Trans.StateT s) a
83+ liftWith f = StateT $ MC.liftWith (f . coerce)
84+ restoreT = StateT . MC.restoreT
85+
7986instance Lift.LiftLocal (StateT tag s)
87+instance Lift.LiftCatch (StateT tag s)
8088
8189instance Taggable (StateT tag s m) where
8290 type Tag (StateT tag s m) = 'Just tag
src/Control/Monad/Trans/Ether/Writer.hs+8
@@ -52,8 +52,10 @@ import GHC.Generics (Generic)
5252import qualified Control.Newtype as NT
5353
5454import qualified Control.Monad.Signatures as Sig
55+import qualified Control.Monad.Trans.Control as MC
5556import qualified Control.Monad.Trans.Writer.Lazy as Trans
5657import qualified Control.Monad.Trans.Lift.Local as Lift
58+import qualified Control.Monad.Trans.Lift.Catch as Lift
5759
5860import qualified Control.Monad.Cont.Class as Class
5961import qualified Control.Monad.Reader.Class as Class
@@ -80,7 +82,13 @@ newtype WriterT tag w m a = WriterT (Trans.WriterT w m a)
8082
8183instance NT.Newtype (WriterT tag w m a)
8284
85+instance Monoid w => MC.MonadTransControl (WriterT tag w) where
86+ type StT (WriterT tag w) a = MC.StT (Trans.WriterT w) a
87+ liftWith f = WriterT $ MC.liftWith (f . coerce)
88+ restoreT = WriterT . MC.restoreT
89+
8390instance Monoid w => Lift.LiftLocal (WriterT tag w)
91+instance Monoid w => Lift.LiftCatch (WriterT tag w)
8492
8593instance Taggable (WriterT tag w m) where
8694 type Tag (WriterT tag w m) = 'Just tag