{-# LANGUAGE CPP #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE FunctionalDependencies #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE UndecidableInstances #-}
#if defined(__GLASGOW_HASKELL__) && __GLASGOW_HASKELL__ >= 704
{-# LANGUAGE DefaultSignatures #-}
{-# LANGUAGE TypeFamilies #-}
#endif
{-# OPTIONS_GHC -fno-warn-deprecations #-}
#include "free-common.h"
module Control.Monad.Free.Class
( MonadFree(..)
, liftF
, wrapT
) where
import Control.Monad
import Control.Monad.Trans.Class
import Control.Monad.Trans.Reader
import qualified Control.Monad.Trans.State.Strict as Strict
import qualified Control.Monad.Trans.State.Lazy as Lazy
import qualified Control.Monad.Trans.Writer.Strict as Strict
import qualified Control.Monad.Trans.Writer.Lazy as Lazy
import qualified Control.Monad.Trans.RWS.Strict as Strict
import qualified Control.Monad.Trans.RWS.Lazy as Lazy
import Control.Monad.Trans.Cont
import Control.Monad.Trans.Maybe
import Control.Monad.Trans.List
import Control.Monad.Trans.Error
import Control.Monad.Trans.Except
import Control.Monad.Trans.Identity
#if !(MIN_VERSION_base(4,8,0))
import Control.Applicative
import Data.Monoid
#endif
class Monad m => MonadFree f m | m -> f where
wrap :: f (m a) -> m a
#if defined(__GLASGOW_HASKELL__) && __GLASGOW_HASKELL__ >= 704
default wrap :: (m ~ t n, MonadTrans t, MonadFree f n, Functor f) => f (m a) -> m a
wrap = join . lift . wrap . fmap return
#endif
instance (Functor f, MonadFree f m) => MonadFree f (ReaderT e m) where
wrap fm = ReaderT $ \e -> wrap $ flip runReaderT e <$> fm
instance (Functor f, MonadFree f m) => MonadFree f (Lazy.StateT s m) where
wrap fm = Lazy.StateT $ \s -> wrap $ flip Lazy.runStateT s <$> fm
instance (Functor f, MonadFree f m) => MonadFree f (Strict.StateT s m) where
wrap fm = Strict.StateT $ \s -> wrap $ flip Strict.runStateT s <$> fm
instance (Functor f, MonadFree f m) => MonadFree f (ContT r m) where
wrap t = ContT $ \h -> wrap (fmap (\p -> runContT p h) t)
instance (Functor f, MonadFree f m, Monoid w) => MonadFree f (Lazy.WriterT w m) where
wrap = Lazy.WriterT . wrap . fmap Lazy.runWriterT
instance (Functor f, MonadFree f m, Monoid w) => MonadFree f (Strict.WriterT w m) where
wrap = Strict.WriterT . wrap . fmap Strict.runWriterT
instance (Functor f, MonadFree f m, Monoid w) => MonadFree f (Strict.RWST r w s m) where
wrap fm = Strict.RWST $ \r s -> wrap $ fmap (\m -> Strict.runRWST m r s) fm
instance (Functor f, MonadFree f m, Monoid w) => MonadFree f (Lazy.RWST r w s m) where
wrap fm = Lazy.RWST $ \r s -> wrap $ fmap (\m -> Lazy.runRWST m r s) fm
instance (Functor f, MonadFree f m) => MonadFree f (MaybeT m) where
wrap = MaybeT . wrap . fmap runMaybeT
instance (Functor f, MonadFree f m) => MonadFree f (IdentityT m) where
wrap = IdentityT . wrap . fmap runIdentityT
instance (Functor f, MonadFree f m) => MonadFree f (ListT m) where
wrap = ListT . wrap . fmap runListT
instance (Functor f, MonadFree f m, Error e) => MonadFree f (ErrorT e m) where
wrap = ErrorT . wrap . fmap runErrorT
instance (Functor f, MonadFree f m) => MonadFree f (ExceptT e m) where
wrap = ExceptT . wrap . fmap runExceptT
liftF :: (Functor f, MonadFree f m) => f a -> m a
liftF = wrap . fmap return
wrapT :: (Functor f, MonadFree f m, MonadTrans t, Monad (t m)) => f (t m a) -> t m a
wrapT = join . lift . liftF