MonadError with variable list of error types - Haskell

Welcome to the Functional Programming Zulip Chat Archive. You can join the chat here.

Sridhar Ratnakumar

Is something like this possible in Haskell today with a library?

doSomething :: MonadError '[FooError, BarError] m => m ()
doSomething = do
  doFoo -- can throw FooError
  doBar -- can throw BarError
Sridhar Ratnakumar

Came across this:

But nothing concrete in the comments section, unfortunately.

Posted in r/haskell by u/rgh • 19 points and 37 comments
Sridhar Ratnakumar

But it doesn't work in GHC 8.8.

Sridhar Ratnakumar

Wild question: would this no longer be a problem if I switch to polysemy?

Alex Chapman

Yes, polysemy's Error effect allows dealing with each error separately. I haven't found a solution that I like in mtl. There is classy errors, as Matt Parsons refers to here: where your type signatures end up looking like this:

doSomething :: (MonadError e m, AsFooError e, AsBarError e) => m ()
doSomething = do
  doFoo -- can use `throwing _FooError`
  doBar -- can use `throwing _BarError`

But as he mentions in that post, this only allows you to compose your errors, it doesn't make it easy to catch them in different places. I think you can catch them, but you can't show that you've caught them in the type signature. He's gone on to write which is aimed at solving this problem.


Problem with mtl are it's fundeps - in polysemy we instead use plugin which when fails to infer member constraint let's user fill in concrete error types manually

Sridhar Ratnakumar

As well as discussion of open union types (that's the keyword I should have used in Google for this problem) here:

Posted in r/haskell by u/hsyl20 • 39 points and 55 comments