caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* Re: [Caml-list] Code-execution (evaluation) time
@ 2010-10-19 20:50 mark
  0 siblings, 0 replies; 4+ messages in thread
From: mark @ 2010-10-19 20:50 UTC (permalink / raw)
  To: caml-list

It's also worth noting a few other things about execution order:

1) function call arguments - All arguments in a function call are evaluated,
even if not needed (in right-to-left order - as David says - but don't rely
on this).

2) function bodies - These are executed when at least as many arguments are
supplied as are explicitly supplied in the definition of the function.  For
example:

let f x = (print_string "X"; fun y -> (print_string "Y"; x + y));;
- this will not print anything as 'f' is defined

let g = f 5;;
- this will print "X" as 'g' is defined
- "Y" will be printed each time 'g' is called with an argument.

3) blank assignments - These are still fully evaluated, even though the
results are thrown away.  For example:

let _ = (print_string "X"; 5);;
- this will still print "X"

4) 'match' branch choice - The first matching branch (i.e. as it appears in
the file) is chosen (if the conditions overlap).

5) 'if' and 'match' unused branches - These (obviously) don't execute at
all.  So these do not behave like functions.

6) logical operator short circuits - '||' and '&&' short-circuit
left-to-right (i.e. don't evaluate the second argument if the first (i.e.
leftmost) argument is "true" (for '||') or "false (for '&&').  So these do
not behave like functions.  For example:

let foo x y = x && y;;

(print_string "X"; false) && (print_string "Y"; false);;
- this will print just "X"

foo (print_string "X"; false) (print_string "Y"; false);;
- this will print "YX", because it's a function call and not a logical
operation.

Mark.


on 19/10/10 8:54 PM, David Allsopp <dra-news@metastack.com> wrote:

> Oliver Bandel wrote:
>> Hello,
>>
>> I want to refresh my OCaml knowledge after I (again) paused for a while.
>>
>> I want to know, when certain parts of the code are executed,
>> for example if I have more than one   let _ = ...
>> statement in my code (for example in different comp0ilaion units each one
>> of them).
>
> Expressions (as in, things listed in
> http://caml.inria.fr/pub/docs/manual-ocaml/expr.html) are evaluated in the
> order in which they appear in a source file. If you have two files A.ml
and
> B.ml which contain top-level statements, the order in which they execute
> will depend on the order in which you link the resulting .cmo or .cmx
files
> (which should be left-to-right as passed to ocamlc/ocamlopt - don't know
> where that's specified).
>
> As far as I can see, in pretty much all contexts within an expression
where
> order of evaluation could matter including, but probably not limited to:
>
> * Record initialisation (e.g. let i = ref 0 in {a = (incr i; !i); b =
(incr
> i; !i)})
> * Function application (similarly)
> * let foo = _ and bar = _ in _
>
> are intentionally left as unspecified (in practice, application and record
> initialisation are right-to-left and let x = _ and y = _ is left-to-right
> but the point is that you are never supposed to rely on that).
>
>> I also found an old code snippet with the following parts:
>>
>> ==============================
>> let fnames = List.tl(Array.to_list Sys.argv)
>>
>> (* some more code *)
>>
>> let _ =
>>    List.iter print_days_old fnames
>> ==============================
>>
>> So, the value fnames contains the args from the command line and can be
>> used in the first statement of the code.
>
> [let fnames = ..] is a complete expression so it's evaluated before the
[let
> _] further down.
>
> David


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [Caml-list] Code-execution (evaluation) time
  2010-10-19 19:52 ` [Caml-list] " David Allsopp
  2010-10-21 19:43   ` oliver
@ 2010-10-21 22:34   ` oliver
  1 sibling, 0 replies; 4+ messages in thread
From: oliver @ 2010-10-21 22:34 UTC (permalink / raw)
  To: 'caml-list@yquem.inria.fr'

Hello David, hello Mark,

thanks for your answers.


In detail:
  thanks to Mark for the refresher
and
 thanks to David for the enhancer. :)


Ciao,
   Oliver


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [Caml-list] Code-execution (evaluation) time
  2010-10-19 19:52 ` [Caml-list] " David Allsopp
@ 2010-10-21 19:43   ` oliver
  2010-10-21 22:34   ` oliver
  1 sibling, 0 replies; 4+ messages in thread
From: oliver @ 2010-10-21 19:43 UTC (permalink / raw)
  To: caml-list

Hello David, hello Mark,

thanks for your answers.


In detail:
  thanks to Mark for the refresher
and
 thanks to David for the enhancer. :)


Ciao,
   Oliver




^ permalink raw reply	[flat|nested] 4+ messages in thread

* RE: [Caml-list] Code-execution (evaluation) time
  2010-10-19 18:39 Oliver Bandel
@ 2010-10-19 19:52 ` David Allsopp
  2010-10-21 19:43   ` oliver
  2010-10-21 22:34   ` oliver
  0 siblings, 2 replies; 4+ messages in thread
From: David Allsopp @ 2010-10-19 19:52 UTC (permalink / raw)
  To: 'Oliver Bandel', 'caml-list@yquem.inria.fr'

Oliver Bandel wrote:
> Hello,
> 
> I want to refresh my OCaml knowledge after I (again) paused for a while.
> 
> I want to know, when certain parts of the code are executed,
> for example if I have more than one   let _ = ...
> statement in my code (for example in different comp0ilaion units each one
> of them).

Expressions (as in, things listed in http://caml.inria.fr/pub/docs/manual-ocaml/expr.html) are evaluated in the order in which they appear in a source file. If you have two files A.ml and B.ml which contain top-level statements, the order in which they execute will depend on the order in which you link the resulting .cmo or .cmx files (which should be left-to-right as passed to ocamlc/ocamlopt - don't know where that's specified).

As far as I can see, in pretty much all contexts within an expression where order of evaluation could matter including, but probably not limited to:

* Record initialisation (e.g. let i = ref 0 in {a = (incr i; !i); b = (incr i; !i)})
* Function application (similarly)
* let foo = _ and bar = _ in _

are intentionally left as unspecified (in practice, application and record initialisation are right-to-left and let x = _ and y = _ is left-to-right but the point is that you are never supposed to rely on that).

> I also found an old code snippet with the following parts:
> 
> ==============================
> let fnames = List.tl(Array.to_list Sys.argv)
> 
> (* some more code *)
> 
> let _ =
>    List.iter print_days_old fnames
> ==============================
> 
> So, the value fnames contains the args from the command line and can be
> used in the first statement of the code.

[let fnames = ..] is a complete expression so it's evaluated before the [let _] further down.


David


^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2010-10-21 22:34 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2010-10-19 20:50 [Caml-list] Code-execution (evaluation) time mark
  -- strict thread matches above, loose matches on Subject: below --
2010-10-19 18:39 Oliver Bandel
2010-10-19 19:52 ` [Caml-list] " David Allsopp
2010-10-21 19:43   ` oliver
2010-10-21 22:34   ` oliver

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).