Mailing list for all users of the OCaml language and system.
 help / color / mirror / Atom feed
From: Alain Frisch <Alain.Frisch@inria.fr>
To: Jon Harrop <jon@ffconsultancy.com>
Cc: caml-list@yquem.inria.fr
Subject: Re: [Caml-list] try .. finally ..
Date: Sun, 20 May 2007 21:30:50 +0200	[thread overview]
Message-ID: <4650A1EA.3050302@inria.fr> (raw)
In-Reply-To: <200705201842.59978.jon@ffconsultancy.com>

Jon Harrop wrote:
> (* The function that returns unique identifiers *)
> let new_id = 
>   let counter = ref 0 in
>   fun () ->
>     incr counter;
>     "__finally" ^ string_of_int !counter
> 
> (* The function that converts our syntax into a single OCaml expression,
>    i.e. an "expr" node of the syntax tree *)
> let expand loc e1 e2 =
>   let id = new_id () in
>   let id_patt = <:patt< $lid:id$ >> in
>   let id_expr = <:expr< $lid:id$ >> in
>   <:expr<
>   let $id_patt$ =
>     try do { $e1$; None } 
>     with [ exn -> Some exn ] in
>   do { $e2$;
>        match $id_expr$ with
>            [ None -> ()
>            | Some exn -> raise exn ] }
>   >>

Note that this implementation breaks tail positions of e2 (when no
exception is raised in e1, one might want to preserve tail-calls in e2
-- or not)...

>   let unwind_protect f g =
>     let fin = ref false in
>     try
>       let x = f () in
>       fin := true;
>       g();
>       x
>     with exn when !fin ->
>       g();
>       raise exn

and this one too, and you want (not !fin) instead of !fin.

Why not:

let unwind_protect f g =
  match (try f (); None with exn -> Some exn) with
   | None -> g ()
   | Some exn -> g (); raise exn

?




In the original version, I believe one could always use the same
identifier, without any extra conflicts. In the second version, you also
need to reserve one identifier (unwind_protect); of course, you can
inline its definition.

> Is this a general observation about macros?

If you mean that regular OCaml abstractions are better behaved w.r.t. to
the binding of identifiers than Camlp4 macros, I'd say the answer is yes.

-- Alain


  reply	other threads:[~2007-05-20 18:30 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2007-05-20 17:42 Jon Harrop
2007-05-20 19:30 ` Alain Frisch [this message]
2007-05-21  7:49   ` [Caml-list] " Florian Weimer

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=4650A1EA.3050302@inria.fr \
    --to=alain.frisch@inria.fr \
    --cc=caml-list@yquem.inria.fr \
    --cc=jon@ffconsultancy.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