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
Function application implementation
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2007-04-25 (22:38)
From: Tom <tom.primozic@g...>
Subject: Function application implementation
I have a question about how are function applications compiled. In
particular, how does the program "know" the difference between f and g in:

let f w x y z = w + x + y - z

let g w x y =
     let m = ref (w + x + y) in
     function z -> !m - z

print_int (f 1 2 3 4)
print_int (g 1 2 3 4)

According to my logic, the first call should compile something like
   push 1
   push 2
   push 3
   push 4
   call function_f
and the second like
   push 1
   push 2
   push 3
   call function_g   // the return value, new function closure, is (for
example) in register A
   push 4
   call A

But obviously, this cannot be the case, as the functions aren't determined
in advance at all call sites (in a functional language). How does the
compiler deal with that?

(I am simplifying here, disregarding the matters of closure construction and
deconstruction, along with environments. However, the refuced example still
causes me problems.)

 - Tom