Skip to content

Instantly share code, notes, and snippets.

@lspitzner
Created October 28, 2018 13:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lspitzner/378e0413505b1d6fa581e67720763d11 to your computer and use it in GitHub Desktop.
Save lspitzner/378e0413505b1d6fa581e67720763d11 to your computer and use it in GitHub Desktop.
the ContCatchT monad
newtype ContCatchT r e m a =
ContCatchT { getContCatchT :: ((e -> m r) -> a -> m r) -> (e -> m r) -> m r }
instance Functor (ContCatchT r e m) where
fmap f (ContCatchT k) =
ContCatchT $ \c1 c2 -> k (\c3 -> c1 c3 . f) c2
instance Applicative (ContCatchT r e m) where
pure x = ContCatchT $ \c1 c2 -> c1 c2 x
ContCatchT cf <*> ContCatchT cx = ContCatchT
$ \c1 c2 -> cf (\c3f f -> cx (\c3x -> c1 c3x . f) c3f) c2
instance Monad (ContCatchT r e m) where
return = pure
c >>= f = ContCatchT $ \c1 c2 ->
getContCatchT c (\c3x x -> getContCatchT (f x) c1 c3x) c2
instance MonadIO m => MonadIO (ContCatchT r e m) where
liftIO m = ContCatchT $ \k1 k2 -> liftIO m >>= k1 k2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment