[Haskell-cafe] Speed of Error handling with Continuations vs. Eithers

Max Cantor mxcantor at gmail.com
Sat May 15 15:28:21 EDT 2010


Where is my bind statement doing a case analysis? Isn't it just propagating, in a sense, the case analysis that came from values coming into the monad via return or via throwError?

Also, why wouldn't callCC work here?  I'm not that familiar with the ContT monad so any more details would be very much appreciated.

Max

On May 15, 2010, at 6:40 AM, Derek Elkins wrote:

> On Fri, May 14, 2010 at 4:53 PM, Antoine Latter <aslatter at gmail.com> wrote:
>> On Fri, May 14, 2010 at 4:25 PM, Derek Elkins <derek.a.elkins at gmail.com> wrote:
>>> You did it wrong.  All you did was Church encode the Either type.
>>> Your bind is still doing a case-analysis.  All you have to do is use
>>> ContT r (Either e).  The bind implementation for ContT is completely
>>> independent of the underlying monad.  It doesn't even require the m in
>>> ContT r m to be a functor, let alone a monad.  Therefore the ContT
>>> bind doesn't do any case-analysis because it doesn't know anything
>>> about the underlying monad.  One way to look at what is happening is
>>> to compare it to Andrzej Filiniski's work in "Representing Monads" and
>>> "Representing Layered Monads".
>>> 
>> 
>> Would you then use callCC to get the required short-circuit-on-error behavior?
>> 
>> A church encoding of Either coded as a monad transformer still
>> wouldn't hit the inner monad on bind, even if it is weaving the left
>> and right continuations together.
> 
> callCC wouldn't work well here.  What would work better is another
> control operator commonly called 'control' which does not resume if
> the passed in continuation isn't invoked.  However, it's usually even
> clearer (or at least more concise) in these situations to work with
> the continuation passing style directly.
> 
> -- fail directly using CPS
> fail :: String -> ContT r (Either String) a
> fail s = ContT $ \k -> Left s



More information about the Haskell-Cafe mailing list