caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: "blue storm" <bluestorm.dylc@gmail.com>
To: "Conglun Yao" <yaoconglun@gmail.com>
Cc: caml-list@yquem.inria.fr
Subject: Re: [Caml-list] How to handle try .... finally properly?
Date: Wed, 10 Dec 2008 23:58:20 +0100	[thread overview]
Message-ID: <527cf6bc0812101458l33b5a5cdkd60d13c369f0b449@mail.gmail.com> (raw)
In-Reply-To: <f7b50d2a0812100315m6c153153y59afc86e02ae8a1e@mail.gmail.com>

> I'm thinking about how to handle try .... finally in ocaml, and found
> one of camlp4 implementations from
> http://bluestorm.info/camlp4/dev/try/pa_tryfinally.ml.html

As you probably noticed, the website is down right now. I'll see that
the files are reachable again as soon as possible (in the meantime, if
you want specific files, mail me and i'll send them). I should also
warn you that the files under the "dev/" prefix are "in development",
and not supposed to be stable or free of bugs.

> Example code adopted from Gabriel Scherer's pa_tryfinally
The example code actually comes from a mailing-list discussion :
http://caml.inria.fr/pub/ml-archives/caml-list/2007/01/31db0fd9b7e158f5679991560cb8f876.en.html

> At the first glance, it answered my question. However, it solved the
> problem partially, only working on functions with one argument.
>
> If we feed the * transform * a function with more than one argument,
> (it is possible because of curring)
>
> transform (fun x y -> .... some logic staff .... ) x y
>
> will invoke the * after () * before the ((fun x y -> .....) x) y is
> really executed.

In that particular case, using "transform f x" to send a function and
not a value would be plain wrong : the whole point of transform is "do
the dirty things" inside the push/pop of the OpenGL matrix. If you
push/pop when building your function, and then apply it later without
restoring the matrix at the application time, you'll get uncorrect
behavior, and this is not a try/finally-specific issue.

> Ideally, * transform * function is specified as allowing a function f
> with only one parameter. But it seems impossible in OCaml,
> as f : 'a -> 'b doesn't means f can and only accept one argument. ( 'b
> could be 'c -> 'd ) If we could constraint 'b as a non-function type,
> it can solve the problem.

This particular transform function is imperative in nature : the idea
is to do side effects at the right moment. You could probably enforce
the transformed function to return an unit type without that much loss
of generality :
  let transform (f : 'a -> unit) x = ...

That way, you can only transform ('a -> unit) functions, wich is a bit
limited (though you could use side-effects to send back values through
a reference for example) but should fit the intended use of the
function well.

In general, there is no solution to your problem. And I'm not even
sure there is a problem, as you could wish to protect the partial
evaluation of a function from raising exceptions : in some case, the
behaviour you find problematic will actually be the expected and right
thing to do.
If you want your function to be polymorphic, you have to accept it
could send anything back. If you want specialized "transform"
functions for curryfied two-arguments functions or more, you could
write ad-hoc functions for the 3, 4 or 5 parameters case. It sounds
ugly but is a practical solution, in the spirit of what the Haskellers
have done with their "zip, zip3, zip4, zip5" list functions (
http://www.haskell.org/ghc/docs/latest/html/libraries/base/Data-List.html#17
).


  reply	other threads:[~2008-12-10 22:58 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2008-12-10 11:15 Conglun Yao
2008-12-10 22:58 ` blue storm [this message]
2008-12-11  0:09 ` [Caml-list] " Jacques GARRIGUE
2008-12-11  0:52   ` Conglun Yao

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=527cf6bc0812101458l33b5a5cdkd60d13c369f0b449@mail.gmail.com \
    --to=bluestorm.dylc@gmail.com \
    --cc=caml-list@yquem.inria.fr \
    --cc=yaoconglun@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).