Version française
Home     About     Download     Resources     Contact us    

This site is updated infrequently. For up-to-date information, please visit the new OCaml website at

Browse thread
[OSR] Exceptionless error management, take 2
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2008-02-11 (08:45)
From: David Teller <David.Teller@u...>
Subject: Re: [Caml-list] [OSR] Exceptionless error management, take 2
On Sun, 2008-02-10 at 21:16 -0500, Yaron Minsky wrote:

> Unless I misunderstand (which is quite likely) you can do fully
> monadic error handling without this extra level of indirection.  i.e.:
> let bind v f = match v with Error _ as x -> x | Ok y -> f y
> let return v = Ok v
> There you go, a fully-functioning monad.  Or really a family of
> monads, since you basically get a distinct monad for each type of
> value in the error position.

Indeed, if you replace "Error _ as x -> x" by "Error e -> Error e", you
get a fully-functioning family of monads, a solution which is quite

On the other hand, that solution is rather slow, as it is essentially
equivalent to checking at every step of the way whether this has been an
error, then unrolling the stack manually. Unless I'm mistaken, this is
the Haskell way, and it's bound to be slow (I haven't actually

With the extra indirection in place, we may add a little magic, as
demonstrated in [1] and use actual exceptions to unroll that stack for

The idea here, is that an expression of type ('a, 'b) may_fail is
actually a function which may either produce a result of type 'a or
raise an OCaml exception of type 'b MonadicException. Since polymorphic
exceptions don't exist in OCaml, it's slightly more complicated, but the
idea is there.

Now, since we have actual exceptions, we need to be certain of when they
are evaluated. That's why function result is there.

Note that we could perform a trivial syntactic optimization using Camlp4
to avoid that call to result. Something such as
attempt expr with
  p1 -> e1
| p2 -> e2
| ...
| pn -> en 

being mapped to

match result ( expr ) with
  p1 -> e1
| p2 -> e2
| ...
| pn -> en 

> I really think we should get far enough in thinking this through to
> get a workable suggestion the first time, rather than add this extra
> bit of pervasive syntactic noise to allow for some possible future
> change later.

Good for me.

>         Interesting question. You are correct that "eval" might make
>         more sense
>         and I believe nobody would object to "Ok|Error" instead of
>         "Success|
>         Error". If terseness is a concern, "Bad" might even make a
>         shorter
>         replacement for "Error", although this would probably be
>         harder to read.
> I wouldn't mind type ('a,'b) Result = Ok of 'a | Err of 'b.  Bad
> seems, well, bad.

I personally consider full words more readable, so I'd somewhat object
to "Err".

> I really think the issue is the same for imperative or functional
> code.  It's easy enough, even in an imperative version, to have a
> function like this:
> let throw = function Ok x -> x | Error e -> failwith e
> At least assuming that the thing inside the error is a string.  It
> could also be an exception, which would then only need to be raised.

That's a possibility. However, I believe we mostly agree that dealing
with errors as strings is usually a bad idea. Unfortunately, if the
error is neither a string nor an exception, in the absence of
polymorphic exceptions, your "throw" wouldn't work. Now, there's always
the monadic option, but I'm not completely sure people would like
monadic error-management right in the middle of their imperative code. 

> I do think it would be helpful to have examples of libraries using
> these idioms.  

I concur.

> For what it's worth, in a few weeks we hope to release a version of
> Jane Street's standard library (which we call "core"), which has a
> bunch of examples of these kinds of error-handling patterns.  One
> thing that's nice about our library is that it has actually been used
> in anger, and so we're pretty sure that the approaches we've taken are
> at least reasonably usable in practice.
> (As a side, note, I do think there is a lot of charm for using
> polymorphic variants for error cases.  It's quite lightweight, and
> makes for very pleasant and explicit function signatures.)

On that subject, one day I'm for them, one day I'm against. I believe
that my biggest issue is that the default typing of polymorphic variants
is open:

# `blue ;;
- : [ > `blue ] = `blue

I believe I understand that choice. It's probably needed to be able to

  fun 0 -> `blue | _ -> `red 

Unfortunately, as discussed previously on the mailing-list, open
polymorphic variants are not quite as foolproof as regular variants.

> y


David Teller
 Security of Distributed Systems
 Angry researcher: French Universities need reforms, but the LRU act
brings liquidations.