caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: David Teller <David.Teller@univ-orleans.fr>
To: yminsky@gmail.com
Cc: OCaml <caml-list@inria.fr>
Subject: Re: [Caml-list] [OSR] Exceptionless error management, take 2
Date: Mon, 11 Feb 2008 09:45:08 +0100	[thread overview]
Message-ID: <1202719508.6348.42.camel@Blefuscu> (raw)
In-Reply-To: <891bd3390802101816r6812a574nc074abd67faf2039@mail.gmail.com>

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
acceptable.

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
checked). 

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
us. 

[1]
http://www.univ-orleans.fr/lifo/Members/David.Teller/software/exceptionless/exceptionless.ml
 
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
type 

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

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

> y

Cheers,
 David

-- 
David Teller
 Security of Distributed Systems
  http://www.univ-orleans.fr/lifo/Members/David.Teller
 Angry researcher: French Universities need reforms, but the LRU act
brings liquidations. 


  reply	other threads:[~2008-02-11  8:45 UTC|newest]

Thread overview: 32+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2008-02-07 15:01 David Teller
2008-02-07 15:09 ` [Caml-list] " Vincent Hanquez
2008-02-07 16:40   ` David Teller
2008-02-07 15:17 ` Jacques Garrigue
2008-02-07 15:22   ` Jon Harrop
2008-02-08  9:54     ` Vincent Hanquez
2008-02-07 15:52   ` David Teller
2008-02-07 16:06     ` Olivier Andrieu
2008-02-07 16:23       ` David Teller
2008-02-08  9:53   ` Vincent Hanquez
2008-02-08 10:52     ` rlehy
2008-02-08 11:56       ` Vincent Hanquez
2008-02-08 12:40         ` Bünzli Daniel
2008-02-08 15:39           ` David Teller
2008-02-08 17:06             ` Eric Cooper
2008-02-08 20:02               ` David Teller
2008-02-08 19:29             ` Bünzli Daniel
2008-02-08 21:13               ` David Teller
2008-02-10 12:35           ` Vincent Hanquez
2008-02-08 19:07     ` Jon Harrop
2008-02-10 11:58       ` Vincent Hanquez
2008-02-10 16:51       ` Matthew William Cox
2008-02-07 15:33 ` Jon Harrop
2008-02-07 16:25   ` David Teller
2008-02-07 23:10 ` David Teller
2008-02-10 18:47 ` Yaron Minsky
2008-02-10 22:05   ` David Teller
2008-02-11  2:16     ` Yaron Minsky
2008-02-11  8:45       ` David Teller [this message]
2008-02-11 12:12         ` Yaron Minsky
2008-02-11 12:53           ` David Teller
2008-02-11 23:09             ` Yaron Minsky

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1202719508.6348.42.camel@Blefuscu \
    --to=david.teller@univ-orleans.fr \
    --cc=caml-list@inria.fr \
    --cc=yminsky@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).