MonadCatch instance for ContT
- Dominant language
- Haskell
- Stars
- 57
- Forks
- 38
- PR merge metrics
- No merged PRs in 30d
Description
The comment says
```Haskell
-- I don't believe any valid of MonadCatch exists for ContT.
```
Well, what about this?
```Haskell
instance MonadCatch m => MonadCatch (ContT r m) where
catch a handler = ContT $ \c ->
runContT a c `catch` \e -> runContT (handler e) c
> runContT (catch (throwM (TestException "foo") >> error "test" :: ContT r IO a) (\(e :: TestException) -> (lift $ putStrLn "handler") >> (lift $ print e))) (const $ return ())
handler
TestException "foo"
```
I think I can also write `mask`:
```Haskell
instance MonadMask m => MonadMask (ContT r m) where
mask f = ContT $ \c ->
mask $ \restore ->
runContT (f (q restore)) c
where
q :: (forall a. m a -> m a) -> ContT r m a -> ContT r m a
q r x = ContT $ \c -> r $ runContT x c
uninterruptibleMask f = ContT $ \c ->
uninterruptibleMask $ \restore ->
runContT (f (q restore)) c
where
q :: (forall a. m a -> m a) -> ContT r m a -> ContT r m a
q r x = ContT $ \c -> r $ runContT x c
```
These are useful in combination with `resetT` to limit the scope of the mask/catch.
`generalBracket` seems elusive, however. I guess the `IO` implementation in terms of `mask` and `catch` could be used.
Contributor guide
No contributing guide indexed for this repository
Assessment
This issue has not been assessed yet.