{-# LANGUAGE Safe #-}
{-# LANGUAGE CPP #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-|  This module contains a collection of monads that
   are defined in terms of the monad transformers from
   "MonadLib".   The definitions in this module are
   completely mechanical and so this module may become
   obsolete if support for automated derivations for instances
   becomes well supported across implementations.
 -}
module MonadLib.Monads (
  Reader, Writer, State, Exception, Cont,
  runReader, runWriter, runState, runException, runCont,
  module MonadLib
) where
import MonadLib
import MonadLib.Derive
import Control.Monad.Fix

newtype Reader    i a = R' { forall i a. Reader i a -> ReaderT i Id a
unR :: ReaderT    i Id a }
newtype Writer    i a = W' { forall i a. Writer i a -> WriterT i Id a
unW :: WriterT    i Id a }
newtype State     i a = S' { forall i a. State i a -> StateT i Id a
unS :: StateT     i Id a }
newtype Exception i a = X' { forall i a. Exception i a -> ExceptionT i Id a
unX :: ExceptionT i Id a }
newtype Cont      i a = C' { forall i a. Cont i a -> ContT i Id a
unC :: ContT      i Id a }

iso_R :: Iso (ReaderT i Id) (Reader i)
iso_W :: Iso (WriterT i Id) (Writer i)
iso_S :: Iso (StateT i Id) (State i)
iso_X :: Iso (ExceptionT i Id) (Exception i)
iso_C :: Iso (ContT i Id) (Cont i)

iso_R :: forall i. Iso (ReaderT i Id) (Reader i)
iso_R = (forall a. ReaderT i Id a -> Reader i a)
-> (forall a. Reader i a -> ReaderT i Id a)
-> Iso (ReaderT i Id) (Reader i)
forall (m :: * -> *) (n :: * -> *).
(forall a. m a -> n a) -> (forall a. n a -> m a) -> Iso m n
Iso ReaderT i Id a -> Reader i a
forall a. ReaderT i Id a -> Reader i a
forall i a. ReaderT i Id a -> Reader i a
R' Reader i a -> ReaderT i Id a
forall a. Reader i a -> ReaderT i Id a
forall i a. Reader i a -> ReaderT i Id a
unR
iso_W :: forall i. Iso (WriterT i Id) (Writer i)
iso_W = (forall a. WriterT i Id a -> Writer i a)
-> (forall a. Writer i a -> WriterT i Id a)
-> Iso (WriterT i Id) (Writer i)
forall (m :: * -> *) (n :: * -> *).
(forall a. m a -> n a) -> (forall a. n a -> m a) -> Iso m n
Iso WriterT i Id a -> Writer i a
forall a. WriterT i Id a -> Writer i a
forall i a. WriterT i Id a -> Writer i a
W' Writer i a -> WriterT i Id a
forall a. Writer i a -> WriterT i Id a
forall i a. Writer i a -> WriterT i Id a
unW
iso_S :: forall i. Iso (StateT i Id) (State i)
iso_S = (forall a. StateT i Id a -> State i a)
-> (forall a. State i a -> StateT i Id a)
-> Iso (StateT i Id) (State i)
forall (m :: * -> *) (n :: * -> *).
(forall a. m a -> n a) -> (forall a. n a -> m a) -> Iso m n
Iso StateT i Id a -> State i a
forall a. StateT i Id a -> State i a
forall i a. StateT i Id a -> State i a
S' State i a -> StateT i Id a
forall a. State i a -> StateT i Id a
forall i a. State i a -> StateT i Id a
unS
iso_X :: forall i. Iso (ExceptionT i Id) (Exception i)
iso_X = (forall a. ExceptionT i Id a -> Exception i a)
-> (forall a. Exception i a -> ExceptionT i Id a)
-> Iso (ExceptionT i Id) (Exception i)
forall (m :: * -> *) (n :: * -> *).
(forall a. m a -> n a) -> (forall a. n a -> m a) -> Iso m n
Iso ExceptionT i Id a -> Exception i a
forall a. ExceptionT i Id a -> Exception i a
forall i a. ExceptionT i Id a -> Exception i a
X' Exception i a -> ExceptionT i Id a
forall a. Exception i a -> ExceptionT i Id a
forall i a. Exception i a -> ExceptionT i Id a
unX
iso_C :: forall i. Iso (ContT i Id) (Cont i)
iso_C = (forall a. ContT i Id a -> Cont i a)
-> (forall a. Cont i a -> ContT i Id a)
-> Iso (ContT i Id) (Cont i)
forall (m :: * -> *) (n :: * -> *).
(forall a. m a -> n a) -> (forall a. n a -> m a) -> Iso m n
Iso ContT i Id a -> Cont i a
forall a. ContT i Id a -> Cont i a
forall i a. ContT i Id a -> Cont i a
C' Cont i a -> ContT i Id a
forall a. Cont i a -> ContT i Id a
forall i a. Cont i a -> ContT i Id a
unC

instance               BaseM (Reader    i) (Reader    i) where inBase :: forall a. Reader i a -> Reader i a
inBase = Reader i a -> Reader i a
forall a. a -> a
id
instance (Monoid i) => BaseM (Writer    i) (Writer    i) where inBase :: forall a. Writer i a -> Writer i a
inBase = Writer i a -> Writer i a
forall a. a -> a
id
instance               BaseM (State     i) (State     i) where inBase :: forall a. State i a -> State i a
inBase = State i a -> State i a
forall a. a -> a
id
instance               BaseM (Exception i) (Exception i) where inBase :: forall a. Exception i a -> Exception i a
inBase = Exception i a -> Exception i a
forall a. a -> a
id
instance               BaseM (Cont      i) (Cont      i) where inBase :: forall a. Cont i a -> Cont i a
inBase = Cont i a -> Cont i a
forall a. a -> a
id

instance Monad (Reader i) where
  >>= :: forall a b. Reader i a -> (a -> Reader i b) -> Reader i b
(>>=)   = Iso (ReaderT i Id) (Reader i)
-> Reader i a -> (a -> Reader i b) -> Reader i b
forall (m :: * -> *) (n :: * -> *) a b.
Monad m =>
Iso m n -> n a -> (a -> n b) -> n b
derive_bind Iso (ReaderT i Id) (Reader i)
forall i. Iso (ReaderT i Id) (Reader i)
iso_R

#if !MIN_VERSION_base(4,11,0)
  fail = error
#endif

instance (Monoid i) => Monad (Writer i) where
  >>= :: forall a b. Writer i a -> (a -> Writer i b) -> Writer i b
(>>=)   = Iso (WriterT i Id) (Writer i)
-> Writer i a -> (a -> Writer i b) -> Writer i b
forall (m :: * -> *) (n :: * -> *) a b.
Monad m =>
Iso m n -> n a -> (a -> n b) -> n b
derive_bind Iso (WriterT i Id) (Writer i)
forall i. Iso (WriterT i Id) (Writer i)
iso_W

#if !MIN_VERSION_base(4,11,0)
  fail = error
#endif

instance Monad (State i) where
  >>= :: forall a b. State i a -> (a -> State i b) -> State i b
(>>=)   = Iso (StateT i Id) (State i)
-> State i a -> (a -> State i b) -> State i b
forall (m :: * -> *) (n :: * -> *) a b.
Monad m =>
Iso m n -> n a -> (a -> n b) -> n b
derive_bind Iso (StateT i Id) (State i)
forall i. Iso (StateT i Id) (State i)
iso_S

#if !MIN_VERSION_base(4,11,0)
  fail = error
#endif


instance Monad (Exception i) where
  >>= :: forall a b. Exception i a -> (a -> Exception i b) -> Exception i b
(>>=)   = Iso (ExceptionT i Id) (Exception i)
-> Exception i a -> (a -> Exception i b) -> Exception i b
forall (m :: * -> *) (n :: * -> *) a b.
Monad m =>
Iso m n -> n a -> (a -> n b) -> n b
derive_bind Iso (ExceptionT i Id) (Exception i)
forall i. Iso (ExceptionT i Id) (Exception i)
iso_X

#if !MIN_VERSION_base(4,11,0)
  fail = error
#endif

instance Monad (Cont i) where
  >>= :: forall a b. Cont i a -> (a -> Cont i b) -> Cont i b
(>>=)   = Iso (ContT i Id) (Cont i)
-> Cont i a -> (a -> Cont i b) -> Cont i b
forall (m :: * -> *) (n :: * -> *) a b.
Monad m =>
Iso m n -> n a -> (a -> n b) -> n b
derive_bind Iso (ContT i Id) (Cont i)
forall i. Iso (ContT i Id) (Cont i)
iso_C

#if !MIN_VERSION_base(4,11,0)
  fail = error
#endif

instance               Functor (Reader    i) where fmap :: forall a b. (a -> b) -> Reader i a -> Reader i b
fmap = Iso (ReaderT i Id) (Reader i)
-> (a -> b) -> Reader i a -> Reader i b
forall (m :: * -> *) (n :: * -> *) a b.
Functor m =>
Iso m n -> (a -> b) -> n a -> n b
derive_fmap Iso (ReaderT i Id) (Reader i)
forall i. Iso (ReaderT i Id) (Reader i)
iso_R
instance (Monoid i) => Functor (Writer    i) where fmap :: forall a b. (a -> b) -> Writer i a -> Writer i b
fmap = Iso (WriterT i Id) (Writer i)
-> (a -> b) -> Writer i a -> Writer i b
forall (m :: * -> *) (n :: * -> *) a b.
Functor m =>
Iso m n -> (a -> b) -> n a -> n b
derive_fmap Iso (WriterT i Id) (Writer i)
forall i. Iso (WriterT i Id) (Writer i)
iso_W
instance               Functor (State     i) where fmap :: forall a b. (a -> b) -> State i a -> State i b
fmap = Iso (StateT i Id) (State i) -> (a -> b) -> State i a -> State i b
forall (m :: * -> *) (n :: * -> *) a b.
Functor m =>
Iso m n -> (a -> b) -> n a -> n b
derive_fmap Iso (StateT i Id) (State i)
forall i. Iso (StateT i Id) (State i)
iso_S
instance               Functor (Exception i) where fmap :: forall a b. (a -> b) -> Exception i a -> Exception i b
fmap = Iso (ExceptionT i Id) (Exception i)
-> (a -> b) -> Exception i a -> Exception i b
forall (m :: * -> *) (n :: * -> *) a b.
Functor m =>
Iso m n -> (a -> b) -> n a -> n b
derive_fmap Iso (ExceptionT i Id) (Exception i)
forall i. Iso (ExceptionT i Id) (Exception i)
iso_X
instance               Functor (Cont      i) where fmap :: forall a b. (a -> b) -> Cont i a -> Cont i b
fmap = Iso (ContT i Id) (Cont i) -> (a -> b) -> Cont i a -> Cont i b
forall (m :: * -> *) (n :: * -> *) a b.
Functor m =>
Iso m n -> (a -> b) -> n a -> n b
derive_fmap Iso (ContT i Id) (Cont i)
forall i. Iso (ContT i Id) (Cont i)
iso_C

instance Applicative (Reader i) where
  pure :: forall a. a -> Reader i a
pure = Iso (ReaderT i Id) (Reader i) -> a -> Reader i a
forall (m :: * -> *) (n :: * -> *) a.
Monad m =>
Iso m n -> a -> n a
derive_return Iso (ReaderT i Id) (Reader i)
forall i. Iso (ReaderT i Id) (Reader i)
iso_R
  <*> :: forall a b. Reader i (a -> b) -> Reader i a -> Reader i b
(<*>) = Reader i (a -> b) -> Reader i a -> Reader i b
forall (m :: * -> *) a b. Monad m => m (a -> b) -> m a -> m b
ap

instance (Monoid i) => Applicative (Writer i) where
  pure :: forall a. a -> Writer i a
pure = Iso (WriterT i Id) (Writer i) -> a -> Writer i a
forall (m :: * -> *) (n :: * -> *) a.
Monad m =>
Iso m n -> a -> n a
derive_return Iso (WriterT i Id) (Writer i)
forall i. Iso (WriterT i Id) (Writer i)
iso_W
  <*> :: forall a b. Writer i (a -> b) -> Writer i a -> Writer i b
(<*>) = Writer i (a -> b) -> Writer i a -> Writer i b
forall (m :: * -> *) a b. Monad m => m (a -> b) -> m a -> m b
ap

instance Applicative (State i) where
  pure :: forall a. a -> State i a
pure = Iso (StateT i Id) (State i) -> a -> State i a
forall (m :: * -> *) (n :: * -> *) a.
Monad m =>
Iso m n -> a -> n a
derive_return Iso (StateT i Id) (State i)
forall i. Iso (StateT i Id) (State i)
iso_S
  <*> :: forall a b. State i (a -> b) -> State i a -> State i b
(<*>) = State i (a -> b) -> State i a -> State i b
forall (m :: * -> *) a b. Monad m => m (a -> b) -> m a -> m b
ap

instance Applicative (Exception i) where
  pure :: forall a. a -> Exception i a
pure = Iso (ExceptionT i Id) (Exception i) -> a -> Exception i a
forall (m :: * -> *) (n :: * -> *) a.
Monad m =>
Iso m n -> a -> n a
derive_return Iso (ExceptionT i Id) (Exception i)
forall i. Iso (ExceptionT i Id) (Exception i)
iso_X
  <*> :: forall a b. Exception i (a -> b) -> Exception i a -> Exception i b
(<*>) = Exception i (a -> b) -> Exception i a -> Exception i b
forall (m :: * -> *) a b. Monad m => m (a -> b) -> m a -> m b
ap

instance Applicative (Cont i) where
  pure :: forall a. a -> Cont i a
pure = Iso (ContT i Id) (Cont i) -> a -> Cont i a
forall (m :: * -> *) (n :: * -> *) a.
Monad m =>
Iso m n -> a -> n a
derive_return Iso (ContT i Id) (Cont i)
forall i. Iso (ContT i Id) (Cont i)
iso_C
  <*> :: forall a b. Cont i (a -> b) -> Cont i a -> Cont i b
(<*>) = Cont i (a -> b) -> Cont i a -> Cont i b
forall (m :: * -> *) a b. Monad m => m (a -> b) -> m a -> m b
ap

instance               MonadFix (Reader    i) where mfix :: forall a. (a -> Reader i a) -> Reader i a
mfix = Iso (ReaderT i Id) (Reader i) -> (a -> Reader i a) -> Reader i a
forall (m :: * -> *) (n :: * -> *) a.
MonadFix m =>
Iso m n -> (a -> n a) -> n a
derive_mfix Iso (ReaderT i Id) (Reader i)
forall i. Iso (ReaderT i Id) (Reader i)
iso_R
instance (Monoid i) => MonadFix (Writer    i) where mfix :: forall a. (a -> Writer i a) -> Writer i a
mfix = Iso (WriterT i Id) (Writer i) -> (a -> Writer i a) -> Writer i a
forall (m :: * -> *) (n :: * -> *) a.
MonadFix m =>
Iso m n -> (a -> n a) -> n a
derive_mfix Iso (WriterT i Id) (Writer i)
forall i. Iso (WriterT i Id) (Writer i)
iso_W
instance               MonadFix (State     i) where mfix :: forall a. (a -> State i a) -> State i a
mfix = Iso (StateT i Id) (State i) -> (a -> State i a) -> State i a
forall (m :: * -> *) (n :: * -> *) a.
MonadFix m =>
Iso m n -> (a -> n a) -> n a
derive_mfix Iso (StateT i Id) (State i)
forall i. Iso (StateT i Id) (State i)
iso_S
instance               MonadFix (Exception i) where mfix :: forall a. (a -> Exception i a) -> Exception i a
mfix = Iso (ExceptionT i Id) (Exception i)
-> (a -> Exception i a) -> Exception i a
forall (m :: * -> *) (n :: * -> *) a.
MonadFix m =>
Iso m n -> (a -> n a) -> n a
derive_mfix Iso (ExceptionT i Id) (Exception i)
forall i. Iso (ExceptionT i Id) (Exception i)
iso_X

instance ReaderM (Reader i) i where ask :: Reader i i
ask = Iso (ReaderT i Id) (Reader i) -> Reader i i
forall (m :: * -> *) i (n :: * -> *). ReaderM m i => Iso m n -> n i
derive_ask Iso (ReaderT i Id) (Reader i)
forall i. Iso (ReaderT i Id) (Reader i)
iso_R
instance (Monoid i) => WriterM (Writer i) i where put :: i -> Writer i ()
put = Iso (WriterT i Id) (Writer i) -> i -> Writer i ()
forall (m :: * -> *) i (n :: * -> *).
WriterM m i =>
Iso m n -> i -> n ()
derive_put Iso (WriterT i Id) (Writer i)
forall i. Iso (WriterT i Id) (Writer i)
iso_W
instance StateM (State i) i where get :: State i i
get = Iso (StateT i Id) (State i) -> State i i
forall (m :: * -> *) i (n :: * -> *). StateM m i => Iso m n -> n i
derive_get Iso (StateT i Id) (State i)
forall i. Iso (StateT i Id) (State i)
iso_S; set :: i -> State i ()
set = Iso (StateT i Id) (State i) -> i -> State i ()
forall (m :: * -> *) i (n :: * -> *).
StateM m i =>
Iso m n -> i -> n ()
derive_set Iso (StateT i Id) (State i)
forall i. Iso (StateT i Id) (State i)
iso_S
instance ExceptionM (Exception i) i where raise :: forall a. i -> Exception i a
raise = Iso (ExceptionT i Id) (Exception i) -> i -> Exception i a
forall (m :: * -> *) i (n :: * -> *) a.
ExceptionM m i =>
Iso m n -> i -> n a
derive_raise Iso (ExceptionT i Id) (Exception i)
forall i. Iso (ExceptionT i Id) (Exception i)
iso_X
instance ContM (Cont i) where callWithCC :: forall a. ((a -> Label (Cont i)) -> Cont i a) -> Cont i a
callWithCC = Iso (ContT i Id) (Cont i)
-> ((a -> Label (Cont i)) -> Cont i a) -> Cont i a
forall (m :: * -> *) (n :: * -> *) a.
ContM m =>
Iso m n -> ((a -> Label n) -> n a) -> n a
derive_callWithCC Iso (ContT i Id) (Cont i)
forall i. Iso (ContT i Id) (Cont i)
iso_C

runReader     :: i -> Reader i a -> a
runWriter     :: Writer i a -> (a,i)
runState      :: i -> State i a -> (a,i)
runException  :: Exception i a -> Either i a
runCont       :: (a -> i) -> Cont i a -> i

runReader :: forall i a. i -> Reader i a -> a
runReader i
i  = Id a -> a
forall a. Id a -> a
runId (Id a -> a) -> (Reader i a -> Id a) -> Reader i a -> a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. i -> ReaderT i Id a -> Id a
forall i (m :: * -> *) a. i -> ReaderT i m a -> m a
runReaderT i
i          (ReaderT i Id a -> Id a)
-> (Reader i a -> ReaderT i Id a) -> Reader i a -> Id a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Reader i a -> ReaderT i Id a
forall i a. Reader i a -> ReaderT i Id a
unR
runWriter :: forall i a. Writer i a -> (a, i)
runWriter    = Id (a, i) -> (a, i)
forall a. Id a -> a
runId (Id (a, i) -> (a, i))
-> (Writer i a -> Id (a, i)) -> Writer i a -> (a, i)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. WriterT i Id a -> Id (a, i)
forall (m :: * -> *) i a. Monad m => WriterT i m a -> m (a, i)
runWriterT            (WriterT i Id a -> Id (a, i))
-> (Writer i a -> WriterT i Id a) -> Writer i a -> Id (a, i)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Writer i a -> WriterT i Id a
forall i a. Writer i a -> WriterT i Id a
unW
runState :: forall i a. i -> State i a -> (a, i)
runState  i
i  = Id (a, i) -> (a, i)
forall a. Id a -> a
runId (Id (a, i) -> (a, i))
-> (State i a -> Id (a, i)) -> State i a -> (a, i)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. i -> StateT i Id a -> Id (a, i)
forall i (m :: * -> *) a. i -> StateT i m a -> m (a, i)
runStateT i
i           (StateT i Id a -> Id (a, i))
-> (State i a -> StateT i Id a) -> State i a -> Id (a, i)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. State i a -> StateT i Id a
forall i a. State i a -> StateT i Id a
unS
runException :: forall i a. Exception i a -> Either i a
runException = Id (Either i a) -> Either i a
forall a. Id a -> a
runId (Id (Either i a) -> Either i a)
-> (Exception i a -> Id (Either i a))
-> Exception i a
-> Either i a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ExceptionT i Id a -> Id (Either i a)
forall i (m :: * -> *) a. ExceptionT i m a -> m (Either i a)
runExceptionT         (ExceptionT i Id a -> Id (Either i a))
-> (Exception i a -> ExceptionT i Id a)
-> Exception i a
-> Id (Either i a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Exception i a -> ExceptionT i Id a
forall i a. Exception i a -> ExceptionT i Id a
unX
runCont :: forall a i. (a -> i) -> Cont i a -> i
runCont   a -> i
i  = Id i -> i
forall a. Id a -> a
runId (Id i -> i) -> (Cont i a -> Id i) -> Cont i a -> i
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a -> Id i) -> ContT i Id a -> Id i
forall a (m :: * -> *) i. (a -> m i) -> ContT i m a -> m i
runContT (i -> Id i
forall a. a -> Id a
forall (m :: * -> *) a. Monad m => a -> m a
return (i -> Id i) -> (a -> i) -> a -> Id i
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> i
i) (ContT i Id a -> Id i)
-> (Cont i a -> ContT i Id a) -> Cont i a -> Id i
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Cont i a -> ContT i Id a
forall i a. Cont i a -> ContT i Id a
unC

instance RunReaderM (Reader i) i where
  local :: forall a. i -> Reader i a -> Reader i a
local = Iso (ReaderT i Id) (Reader i) -> i -> Reader i a -> Reader i a
forall (m :: * -> *) i (n :: * -> *) a.
RunReaderM m i =>
Iso m n -> i -> n a -> n a
derive_local Iso (ReaderT i Id) (Reader i)
forall i. Iso (ReaderT i Id) (Reader i)
iso_R

instance (Monoid i) => RunWriterM (Writer i) i where
  collect :: forall a. Writer i a -> Writer i (a, i)
collect = Iso (WriterT i Id) (Writer i) -> Writer i a -> Writer i (a, i)
forall (m :: * -> *) i (n :: * -> *) a.
RunWriterM m i =>
Iso m n -> n a -> n (a, i)
derive_collect Iso (WriterT i Id) (Writer i)
forall i. Iso (WriterT i Id) (Writer i)
iso_W

instance RunExceptionM (Exception i) i where
  try :: forall a. Exception i a -> Exception i (Either i a)
try = Iso (ExceptionT i Id) (Exception i)
-> Exception i a -> Exception i (Either i a)
forall (m :: * -> *) i (n :: * -> *) a.
RunExceptionM m i =>
Iso m n -> n a -> n (Either i a)
derive_try Iso (ExceptionT i Id) (Exception i)
forall i. Iso (ExceptionT i Id) (Exception i)
iso_X