OCaml and -dlambda #1

Hi,

Have you ever tried compiling with either ocamlc or ocamlopt with the -dlambda option ?

Compiling the following code :

1
2
3
4
5
let f = List.map (fun x -> x+1)
 
let print_list = List.iter (fun x -> Printf.printf " %d " x)
 
let _ = print_list ( f [1;2;3;4;5] )

give the following “lambda code” :

?View Code LAMBDA
1
2
3
4
5
6
7
8
9
10
11
12
13
(* $ ocamlc -dlambda test.ml *)
(setglobal Test!
  (let
    (f/58
       (function l/59
         (apply (field 10 (global List!)) (function x/60 (+ x/60 1)) l/59))
     print_list/61
       (apply (field 9 (global List!))
         (function x/62 (apply (field 1 (global Printf!)) " %d " x/62))))
    (seq
      (apply print_list/61
        (apply f/58 [0: 1 [0: 2 [0: 3 [0: 4 [0: 5 0a]]]]]))
      (makeblock 0 f/58 print_list/61))))
?View Code LAMBDA
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
(* $ ocamlopt -dlambda las.ml *)
(seq
  (let
    (f/58
       (function l/59
         (apply (field 10 (global List!)) (function x/60 (+ x/60 1)) l/59)))
    (setfield_imm 0 (global Test!) f/58))
  (let
    (print_list/61
       (apply (field 9 (global List!))
         (function x/62 (apply (field 1 (global Printf!)) " %d " x/62))))
    (setfield_imm 1 (global Test!) print_list/61))
  (apply (field 1 (global Test!))
    (apply (field 0 (global Test!)) [0: 1 [0: 2 [0: 3 [0: 4 [0: 5 0a]]]]]))
  0a)

There are many phases in the compilation process, and the lambda generation phase is the last before either bytecode/native code generation.

‘global’ keyword seems to reference modules (Test is the module of my file, because of the name, test.ml).
‘apply’ just does function calls.
‘field’ seems to reference function in modules, like index references values in arrays.

More in the next exploration of the -dlambda option !