[Haskell-cafe] Re: MonadCatchIO, finally and the error monad

Michael Snoyman michael at snoyman.com
Fri Oct 15 01:20:44 EDT 2010

On Thu, Oct 14, 2010 at 11:13 PM, Antoine Latter <aslatter at gmail.com> wrote:
> On Thu, Oct 14, 2010 at 1:28 PM, Michael Snoyman <michael at snoyman.com> wrote:
>> I thought a bit about this, and I believe the only extra primitive we
>> need is one of bracket, bracket_ or finally. I also noticed the
>> exception-transformers package[1], which seems to be a good
>> replacement for MonadCatchIO. I contacted the author about adding a
>> MonadBracket typeclass, and he said he'll try to get to it. I'm
>> planning on making that my replacement for MonadCatchIO (assuming it
>> turns out correctly), and if so I'd recommend others do the same.
> Ah!
> I didn't notice at first that the Exception type class was simply
> re-exported from base. At first glance, that package looked like
> entirely too much for what I wanted.

For a while now I've had the idea of "inverting" monads: flipping them
inside out so that we can automatically apply any of the special
IO-specific functions to them. I always got caught when implementing
Reader and State, but last night I had a break-through and finally got
it to work. For the moment, the code is up in my neither repo on
github[1], and it has a fairly complete test suite[2].

It's a pretty complicated implementation, since it needs to be generic
enough to address a wide variety of monads, but I'm fairly certain
that the theoretical foundations are sound. I intend to write a full
blog post to explain the idea. With this, I have been able to
implement catch, finally, bracket, alloca, allocaBytes and
withForeignPtr simply by calling the standard IO-specific functions.

I'm excited about this approach: I think it has the possibility to
liberate us from a lot of tedious, error-prone code.


[1] http://github.com/snoyberg/neither/blob/master/Control/Monad/Invert.hs
[2] http://github.com/snoyberg/neither/blob/master/runtests.hs

More information about the Haskell-Cafe mailing list