caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: "Chris King" <colanderman@gmail.com>
To: "Christophe Raffalli" <raffalli@univ-savoie.fr>
Cc: caml-list@inria.fr
Subject: Re: [Caml-list] try and tail call
Date: Sat, 21 Oct 2006 11:03:25 -0400	[thread overview]
Message-ID: <875c7e070610210803i5abf3b78j6e83c55c991f05cf@mail.gmail.com> (raw)
In-Reply-To: <453A27EB.8070609@univ-savoie.fr>

On 10/21/06, Christophe Raffalli <raffalli@univ-savoie.fr> wrote:
>
> consider this piece of code:
>
>   (try
>     let b = f a in
>     (fun () -> g b)
>   with
>     (fun () -> h a)) ()
>
>
> Is the call to "g" a tail call and is it omptimized as such ?

Yes, it is, at least in the assembly code output by ocamlopt.  (Neat
trick, btw.)

> If not, how to encode a feature like this one:
>
>   try
>     ...
>   end
>     ... (* the exception raised here are not handles by with but
> propagated *)
>   with
>     ...

What's typically used is something along the lines of:

match (try Some (f a) with Not_found -> None) with
| Some b -> g b
| None -> h a

though I wouldn't be surprised if the method you used above is
slightly faster since it avoids creating unnecessary option objects
(the anonymous functions are statically allocated).

> By the way I think this "try ... [end ...] with ..." syntax is usefull
> anyway because you have often a bug when
> you write
>
> try
>   let b = f a in (* you know this may raise Not_found *)
>   g b (* you assume wrongly that this can not raise Not_found *)
> with
>   Not_found -> h a
>
> The unwanted capture of Not_found could be avoided with try ... end ...
> with ...

Many people agree with you... I find almost always that I want
something like the syntax you describe above, rather than what Caml
provides natively.  Martin Jambon provides an example syntax extension
in his Camlp4 tutorial[1] which allows the use of "let-try-in-with"
blocks:

let b = try f a in g b
with Not_found -> h a

Here, only exceptions raised by "f a" are caught by the try block, and
"g b" occurs in a recursive context.

Hope this helps,
Chris

[1] http://martin.jambon.free.fr/extend-ocaml-syntax.html#lettry


  reply	other threads:[~2006-10-21 15:03 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2006-10-21 14:00 Christophe Raffalli
2006-10-21 15:03 ` Chris King [this message]
2006-10-22 17:16   ` [Caml-list] " Christophe Raffalli
2006-10-22  2:44 ` ketty
2006-10-22 17:08   ` Christophe Raffalli

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=875c7e070610210803i5abf3b78j6e83c55c991f05cf@mail.gmail.com \
    --to=colanderman@gmail.com \
    --cc=caml-list@inria.fr \
    --cc=raffalli@univ-savoie.fr \
    /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).