@@ -43,22 +43,23 @@ module Control.Monad.Trans.Ether.Dispatch
44 -- * The DispatchT monad transformer
44 -- * The DispatchT monad transformer
46 -- * Dispatch types and functions
49 -- * Dispatch types and functions
47 , K_TagAttach(..)
50 , K_TagAttach(..)
49 , tagAttach
51 , tagAttach
50 , tagReplace
53 , tagReplace
53import Control.Applicative
56import Control.Applicative
54import Control.Monad (MonadPlus)
57import Control.Monad (MonadPlus)
55import Control.Monad.Fix (MonadFix)
58import Control.Monad.Fix (MonadFix)
56−import Control.Monad.Trans.Class (MonadTrans)
59+import Control.Monad.Trans.Class (MonadTrans, lift)
57import Control.Monad.IO.Class (MonadIO)
60import Control.Monad.IO.Class (MonadIO)
58import Control.Monad.Morph (MFunctor, MMonad)
61import Control.Monad.Morph (MFunctor, MMonad)
59import Control.Monad.Catch (MonadThrow, MonadCatch, MonadMask)
62import Control.Monad.Catch (MonadThrow, MonadCatch, MonadMask)
60−import GHC.Generics (Generic)
61−import Data.Coerce (coerce)
63import qualified Control.Monad.Base as MB
64import qualified Control.Monad.Base as MB
64import qualified Control.Monad.Trans.Control as MC
65import qualified Control.Monad.Trans.Control as MC
@@ -81,16 +82,14 @@ import Control.Monad.Ether.State.Common as A
81import Control.Monad.Ether.Except as A
82import Control.Monad.Ether.Except as A
82import Control.Monad.Ether.Writer as A
83import Control.Monad.Ether.Writer as A
84−import qualified Control.Monad.Reader as Class
85+import qualified Control.Monad.Cont.Class as Class
86+import qualified Control.Monad.Reader.Class as Class
85−import qualified Control.Monad.State as Class
87+import qualified Control.Monad.State.Class as Class
86−import qualified Control.Monad.Except as Class
87−import qualified Control.Monad.Writer as Class
88+import qualified Control.Monad.Writer.Class as Class
89+import qualified Control.Monad.Error.Class as Class
89−-- | Encode type-level information for 'tagAttach'.
91+import GHC.Generics (Generic)
90−data K_TagAttach t = TagAttach t
92+import Data.Coerce (coerce)
92−-- | Encode type-level information for 'tagReplace'.
93−data K_TagReplace tOld tNew = TagReplace tOld tNew
95-- | Wrap a monad to change its tags. Under the hood this is
94-- | Wrap a monad to change its tags. Under the hood this is
96-- simply 'Trans.IdentityT', all the work is happening on the type level.
95-- simply 'Trans.IdentityT', all the work is happening on the type level.
@@ -101,17 +100,26 @@ newtype DispatchT dp m a = DispatchT (Trans.IdentityT m a)
101 , MonadFix, MonadTrans, MonadIO, MFunctor, MMonad
100 , MonadFix, MonadTrans, MonadIO, MFunctor, MMonad
102 , MonadThrow, MonadCatch, MonadMask )
101 , MonadThrow, MonadCatch, MonadMask )
104−type DispatchTagAttachT t = DispatchT (TagAttach t)
105−type DispatchTagReplaceT tOld tNew = DispatchT (TagReplace tOld tNew)
107-- | Type-restricted 'coerce'.
103-- | Type-restricted 'coerce'.
108−pack :: Trans.IdentityT m a -> DispatchT dp m a
104+pack :: forall dp m a . Trans.IdentityT m a -> DispatchT dp m a
109pack = coerce
105pack = coerce
111-- | Type-restricted 'coerce'.
107-- | Type-restricted 'coerce'.
112−unpack :: DispatchT dp m a -> Trans.IdentityT m a
108+unpack :: forall dp m a . DispatchT dp m a -> Trans.IdentityT m a
113unpack = coerce
109unpack = coerce
111+-- | Type-restricted 'coerce'.
112+dispatchT :: forall dp m a . m a -> DispatchT dp m a
115+-- | Type-restricted 'coerce'.
116+runDispatchT :: forall dp m a . DispatchT dp m a -> m a
117+runDispatchT = coerce
119+-- | Type-restricted 'coerce'.
120+redispatch :: forall dp dp' m a . DispatchT dp m a -> DispatchT dp' m a
115instance MB.MonadBase b m => MB.MonadBase b (DispatchT dp m) where
123instance MB.MonadBase b m => MB.MonadBase b (DispatchT dp m) where
116 liftBase = MB.liftBaseDefault
124 liftBase = MB.liftBaseDefault
@@ -143,16 +151,62 @@ instance Lift.LiftCallCC (DispatchT dp) where
143 liftCallCC = Lift.defaultLiftCallCC pack unpack
151 liftCallCC = Lift.defaultLiftCallCC pack unpack
144 liftCallCC' = Lift.defaultLiftCallCC' pack unpack
152 liftCallCC' = Lift.defaultLiftCallCC' pack unpack
146−-- | Attach a tag to untagged transformers.
154+-- Instances for mtl classes
147−tagAttach :: forall tag m a . DispatchTagAttachT tag m a -> m a
155+
148−tagAttach = coerce
156+instance {-# OVERLAPPABLE #-}
159+ ) => Class.MonadCont (DispatchT dp m)
161+ callCC = Lift.liftCallCC' Class.callCC
163+instance {-# OVERLAPPABLE #-}
164+ ( Class.MonadReader r m
166+ ) => Class.MonadReader r (DispatchT dp m)
168+ ask = lift Class.ask
169+ local = Lift.liftLocal Class.ask Class.local
170+ reader = lift . Class.reader
172+instance {-# OVERLAPPABLE #-}
173+ ( Class.MonadState s m
175+ ) => Class.MonadState s (DispatchT dp m)
177+ get = lift Class.get
178+ put = lift . Class.put
179+ state = lift . Class.state
181+instance {-# OVERLAPPABLE #-}
182+ ( Class.MonadWriter w m
184+ ) => Class.MonadWriter w (DispatchT dp m)
186+ writer = lift . Class.writer
187+ tell = lift . Class.tell
188+ listen = Lift.liftListen Class.listen
189+ pass = Lift.liftPass Class.pass
191+instance {-# OVERLAPPABLE #-}
192+ ( Class.MonadError e m
194+ ) => Class.MonadError e (DispatchT dp m)
196+ throwError = lift . Class.throwError
197+ catchError = Lift.liftCatch Class.catchError
150−-- | Replace a tag with another tag.
202+-- | Encode type-level information for 'tagAttach'.
151−tagReplace :: forall tOld tNew m a . DispatchTagReplaceT tOld tNew m a -> m a
203+data K_TagAttach t = TagAttach t
205+type DispatchTagAttachT t = DispatchT (TagAttach t)
155−-- TagAttach instances
207+-- | Attach a tag to untagged transformers.
208+tagAttach :: forall tag m a . DispatchTagAttachT tag m a -> m a
157instance MonadReader tag r m
211instance MonadReader tag r m
158 => Class.MonadReader r (DispatchTagAttachT tag m) where
212 => Class.MonadReader r (DispatchTagAttachT tag m) where
@@ -177,7 +231,16 @@ instance MonadWriter tag w m
177 pass = Lift.liftPass (A.pass @tag)
231 pass = Lift.liftPass (A.pass @tag)
180−-- TagReplace instances
234+-- TagReplace
236+-- | Encode type-level information for 'tagReplace'.
237+data K_TagReplace tOld tNew = TagReplace tOld tNew
239+type DispatchTagReplaceT tOld tNew = DispatchT (TagReplace tOld tNew)
241+-- | Replace a tag with another tag.
242+tagReplace :: forall tOld tNew m a . DispatchTagReplaceT tOld tNew m a -> m a
182instance MonadReader tNew r m
245instance MonadReader tNew r m
183 => MonadReader tOld r (DispatchTagReplaceT tOld tNew m) where
246 => MonadReader tOld r (DispatchTagReplaceT tOld tNew m) where