0

I know how to use the keyword in in OCaml. My question is in which order the compilator execute the expression.

For example let's take the following code :

let v = expr1 in expr2

The compilator looks first at expr2, and then when v appears in expr2 it replaces v by expr1? Or first, it evaluates expr1 and then apply expr2 ?

You may wonder why I am asking this weird question. This is due to the fact that I do not understand how the following code works :

let rec some_function = function 
   | [] -> () 
   | t::q when (*here put a condition*) -> some_function q
   | t::q -> (*some operations here*); some_function q
in 
let s = (*some list*)
some_function s 

How does this code work? I mean when we have the recursive call some_function q then does the program directly goes in the in block and apply the other recursive call some_function s?

1
  • some_function s is not a recursive call. Why would the program go to the in block?
    – melpomene
    Sep 30, 2018 at 8:51

1 Answer 1

6

When the program says let v = expr1 in expr2, OCaml first evaluates expr1, and then begins the evaluation of expr2 in an environment in which v is associated to the value that was obtained as result of expr1.

This evaluation strategy, call-by-value, is not the only one possible, but it is the strategy that OCaml use.

Now let us consider the snippet:

let rec some_function = function ...
in
let s = (*some list*)
some_function s 

When the program contains the snippet above, the following steps take place:

  • function ... is evaluated. This step is short because evaluating a function ... block does not mean that the code inside ... is evaluated. Instead, the result of the evaluation of function ... is a closure (forgetting about optimizations that apply in your example but not in general).
  • the code represented by (*some list*) is evaluated in an environment in which some_function is bound to the closure discussed above.
  • the expression some_function s is evaluated in an environment where some_function is bound to a closure and s is bound to the result of the evaluation of (*some list*). In this environment, this evaluation succeeds and the closure gets applied, which means that the code that defined some_function will now be executed (in an environment where its argument has been provided).

A closure is made of unevaluated code and a partial environment. The environment only needs to be augmented with an additional binding for the argument in order to contain everything needed to evaluate the code. On the other hand, as long as the argument has not been provided, evaluation cannot start because the function's body refers to the argument.

The fact that some_function is recursive does not change the general scheme. It only means that when the body of the closure is evaluated, the environment also contains a binding associating the same some_function to the closure, so that a call to some_closure inside the body is treated the same way it would be outside the body.

3
  • Thank you, it's very clear. So it means that some_function is evaluated as a closure only once (the first time), then some_function is evaluated (with the s argument the first time), and then when we finished evaluating some_function we directly go in the in block and we evaluate some_function s on so on on so forth. So we have : 1- some_function is a closure 2-now using the closure we can evaluate some_function s 3- we've finished evaluating some_function s so we go in the in block and reevaluate some_function s 4- ... we evaluate some_function s forever
    – obla
    Sep 30, 2018 at 11:00
  • Actually, I think I am wrong. After using some tests and rereading your answer I think it does the following : 1- some_function is evaluated as a closure 2- using the closure we go in the in block and evaluate: some_function s and that's it. So at each new call we are not going to evaluate what is happening in the in block. We evaluate what is in the in block only once.
    – obla
    Sep 30, 2018 at 12:03
  • 1
    @obla yes to your second comment. Adding side-effects such as print_endline "here" allows to see when and how many times code is visited. Sep 30, 2018 at 12:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.