caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Dave Mason <dmason@sarg.Ryerson.CA>
To: Damien Doligez <Damien.Doligez@inria.fr>
Cc: caml-list@inria.fr
Subject: Re: [Caml-list] Evaluation Order
Date: Sun, 10 Jun 2001 14:28:43 -0400	[thread overview]
Message-ID: <200106101828.OAA09593@sarg.Ryerson.CA> (raw)
In-Reply-To: Your message of "Sun, 10 Jun 2001 19:59:11 +0200." <200106101759.TAA0000023297@beaune.inria.fr>

>>>>> On Sun, 10 Jun 2001 19:59:11 +0200 (MET DST), Damien Doligez <Damien.Doligez@inria.fr> said:

> The original argument about optimizations is still as valid as it
> ever was.  The real problem is that there is a conflict between
> left-to-right evaluation order and currying.  Consider the
> expression:

>    f e1 e2 e3

> It only looks like the application of f to 3 arguments.  In reality,
> it is a nested expression with three function applications.  If you
> want to evaluate it in left-to-right order, you have to do the
> following:
>[...]

> If you want to optimize this to get something as efficient as O'Caml
> currently is, you have to know that f doesn't do any side-effect
> before receiving all its arguments, which is generally impossible.
> With right-to-left, the compiler doesn't have to know anything about
> f.

This is also addressable with my suggestion (which I posted last
night, but hasn't made it to the mailing list yet) of annotating
results of functions that cause a side-effect.  So, in your example,
instead of the existing:
	# let x = ref 0;;
	val x : int ref = {contents=0}
	# let f w y = incr x;let x' = !x in function z -> w+x'+y+z;;
	val f : int -> int -> int -> int = <fun>
you would get instead:
	val f : int -> int -> (int -> int effect) effect = <fun>
now when you apply it:
	f e1 e2 e3
you get an error if e3 is also an effect value, because the order of
evaluation between the evaluation of (f e1 e2) and (e3) might cause a
problem.  So you would be forced to rewrite it as:
	let f' = f e1 e2 in f' e3
or
	let e3' = e3 in f e1 e2 e3'
whichever reflected the order of evaluation that you want.  Note that
you would not get an error if e2 was an effect value because it must
be evaluated (because of ml's strict semantics) before f is applied to
it, and it doesn't matter whether e2 is evaluated before or after (f
e1) is.  Similarly, it does not matter if e1 is an effect value
because it has to be evaluated before f can be applied to it.  (Note
that if f was an expression that itself was an effect, this wouldn't
be true.)  If e1 and e2 were *both* effect values, then there would be
a conflict between them and you would get a compilation error.

(Note that in my other posting I suggested a refinement of this that
would get finer granularity on the conflicts, but the principle is the
same.)  This is somewhat related to monads, but with ocaml's strict
semantics it need not be as intrusive as monads.

../Dave
-------------------
Bug reports: http://caml.inria.fr/bin/caml-bugs  FAQ: http://caml.inria.fr/FAQ/
To unsubscribe, mail caml-list-request@inria.fr  Archives: http://caml.inria.fr


  reply	other threads:[~2001-06-11 11:23 UTC|newest]

Thread overview: 33+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2001-06-10 17:59 Damien Doligez
2001-06-10 18:28 ` Dave Mason [this message]
  -- strict thread matches above, loose matches on Subject: below --
2009-06-14 16:36 evaluation order Christophe Raffalli
2009-06-14 19:40 ` [Caml-list] " Jon Harrop
2009-06-14 21:12   ` Christophe Raffalli
2001-06-15 17:00 [Caml-list] Evaluation Order Manuel Fahndrich
2001-06-10  2:44 David McClain
2001-06-10  2:48 ` Patrick M Doane
2001-06-10  5:51   ` David McClain
2001-06-09 15:59 David McClain
2001-06-09 20:17 ` Brian Rogoff
2001-06-09 23:12   ` David McClain
2001-06-09 23:28     ` David McClain
2001-06-10  1:04       ` Dave Mason
2001-06-10  2:25         ` David McClain
2001-06-11 13:03           ` Dave Mason
2001-06-12 17:55             ` John Max Skaller
2001-06-13 16:54               ` Frederick Smith
2001-06-13 21:43                 ` John Max Skaller
2001-06-10  1:06       ` Charles Martin
2001-06-10  2:27         ` David McClain
2001-06-10 11:18         ` Tore Lund
2001-06-10 13:11           ` Tore Lund
2001-06-10 14:31           ` John Max Skaller
2001-06-12 15:12             ` Pierre Weis
2001-06-10 10:40       ` Joerg Czeranski
2001-06-10 14:06       ` John Max Skaller
2001-06-11 12:59         ` Dave Mason
2001-06-12 17:34           ` John Max Skaller
2001-06-10 13:47   ` John Max Skaller
2001-06-10 16:47     ` Brian Rogoff
2001-06-10 17:27       ` Dave Mason
2001-06-12 16:10       ` John Max Skaller
2001-06-09 23:19 ` John Max Skaller

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=200106101828.OAA09593@sarg.Ryerson.CA \
    --to=dmason@sarg.ryerson.ca \
    --cc=Damien.Doligez@inria.fr \
    --cc=caml-list@inria.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).