Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement MonadError for ContT e (Codensity m) #60

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions src/Control/Monad/Codensity.hs
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,12 @@ import Control.Applicative
import Control.Monad (MonadPlus(..))
import qualified Control.Monad.Fail as Fail
import Control.Monad.Free
import Control.Monad.Error.Class
import Control.Monad.IO.Class
import Control.Monad.Reader.Class
import Control.Monad.State.Class
import Control.Monad.Trans.Class
import Control.Monad.Trans.Cont
import Data.Functor.Adjunction
import Data.Functor.Apply
import Data.Functor.Kan.Ran
Expand Down Expand Up @@ -248,6 +250,17 @@ instance (Functor f, MonadFree f m) => MonadFree f (Codensity m) where
wrap t = Codensity (\h -> wrap (fmap (\p -> runCodensity p h) t))
{-# INLINE wrap #-}

instance {-# OVERLAPPING #-} MonadError e (ContT e (Codensity m)) where
throwError e = ContT (const (pure e))
catchError c recover = ContT $ \successHandler -> Codensity $ \errorHandler ->
runCodensity (runContT c successHandler)
$ \e -> runCodensity (runContT (recover e) successHandler) errorHandler

instance MonadError e m => MonadError e (Codensity m) where
throwError e = Codensity (const (throwError e))
catchError c recover =
Codensity $ \k -> catchError (runCodensity c k) $ \e -> runCodensity (recover e) k

instance MonadReader r m => MonadState r (Codensity m) where
get = Codensity (ask >>=)
{-# INLINE get #-}
Expand Down