English version
Accueil     À propos     Téléchargement     Ressources     Contactez-nous    

Ce site est rarement mis à jour. Pour les informations les plus récentes, rendez-vous sur le nouveau site OCaml à l'adresse ocaml.org.

Browse thread
[Caml-list] Automatic wrapper generator
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2004-05-18 (20:41)
From: Evan Martin <martine@d...>
Subject: Re: [Caml-list] Functional critical section
On Tue, May 18, 2004 at 04:10:44PM -0400, SWAMPY wrote:
>    I'm very new to ocaml, and I'm trying to figure out how
> to do something that may or may not actually be possible.
> Any suggestions?
>     I'd like to write
>         <name> <expr>
>     as an expression, and have this evaluate <expr> first
> evaluating fixed expression A, and then after evaluting
> <expr> evaluate fixed expression B.

I'm not sure what you mean by A and B here, but in general there is a
specific way* things are evaluated and the only way to make things happen
another way is to use closures to delay evaluation.

>     Then, I could do something like:
>     critical (threadsafeFunction arg1 arg2 arg3)
>     and have this do the equivalent of
>     Mutex.lock someMutex; let tmp = threadsafeFunction arg1
> arg2 arg3; Mutex.unlock someMutex; tmp;;
>     Is this sort of thing possible?  In general I'm
> interested in techniques I could use to wrap
> imperative-biased parts of the library to more functional
> constructs.

Sure, that's the sort of thing that anonymous functions are great for.

let criticial f =
  Mutex.lock someMutex;
  let tmp = f () in
  Mutex.unlock someMutex;

And then call it like this:

 criticial (fun () -> threadsafeFunction arg1 arg2 arg3)

The "trick" here is that we wrap up the inner in its own function, so
that the function "critical" can decide when to run it-- between the
mutex lock and unlock.

* A more pedantic description of evalution order, as I understand it:
Arguments are always evaluated before calling functions.
However, in the application form "e1 e2", the thing being applied (e1)
must also be evaluated to a value, and the order of those evaluations is
either undefined or right to left (I forget which; maybe the former
officially, and the latter in practice?).

That means that in something like e1 e2, e1 and e2 are evaluated to
values, and then the function that e1 evaluates to is evaluated with
e2's value as its argument.

In most situations, e1 is just an identifier like "threadsafeFunction",
and evaluation of it is trivial.  So it doesn't matter whether e2
evaluates before or after that, and the actual application of the
function is always last.

If you ever want to force evaluation order, you can use let.
For example, if I have code like:
 f (function1 1) (function2 31)
and I want to be certain that function2 is called first, you can rewrite
it to
 let f2 = function2 31 in
 f (function1 1) f2

Evan Martin

To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners