Version française
Home     About     Download     Resources     Contact us    

This site is updated infrequently. For up-to-date information, please visit the new OCaml website at

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-26 (09:25)
From: Xavier Leroy <Xavier.Leroy@i...>
Subject: Re: [Caml-list] Function application implementation
>     It knows the type of the function expression, and that is all
>     that is required. Incidentally Ocaml evaluates right to left. So
>             f x y z
>     will be roughly:
>             push (eval z)
>             push (eval y)
>             push (eval x)
>             push (eval f)
>             apply
>             apply
>             apply
> But that doesn't explain how does each apply know what to do, either to
> build a new closure (in the case above, the first two applies) or to
> actually call the code (the third apply).

The generated abstract machine code is more like:
             push (eval z)
             push (eval y)
             push (eval x)
             push (eval f)
             apply 3        (*  number of arguments provided *)

"apply" doesn't do anything clever, it just enters the code of the
called function f.  It's the code of f that determines what to do
with the arguments provided on the stack.

More details can be found in one of my talks:

- Xavier Leroy