ekmett / ekmett/exceptions

Add a traslating transformer to implement `MonadThrow` in terms of `MonadIO`

Open
#89 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
Haskell
Stars
57
Forks
38
PR merge metrics
No merged PRs in 30d

Description

I have the following:

```haskell
foo :: (MonadWhatever m, MonadThrow m) => ... -> m ()
```

Later, I have

```
bar :: (MonadIO, MonadWhatever m) =>
bar = do
...
foo
...
```

However, this won't typecheck because `bar` lacks the `MonadThrow` constraint necessary for `foo`. I don't want to add a `MonadThrow` constraint because I know that `MonadIO` implements that. My suggestion is that `exceptions` gains a new transformer that translates `MonadThrow` into `MonadIO`:

```haskell
-- | Translate 'MonadThrow' constraints to an underlying 'MonadIO' instance.
newtype ThrowToIOT m a = ThrowToIOT { throwToIO :: m a }
deriving newtype (Functor, Applicative, Monad, MonadIO, MonadUnliftIO)

instance MonadTrans ThrowToIOT where
lift = ThrowToIOT

instance MonadIO m => MonadThrow (ThrowToIOT m) where
throwM = liftIO . throwM
```

With this, I can now write:

```haskell
bar :: (MonadIO, MonadWhatever m) =>
bar = do
...
throwToIO foo
...
```

and everyone is happy.

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.