Mailing list for all users of the OCaml language and system.
 help / color / mirror / Atom feed
From: skaller <skaller@users.sourceforge.net>
To: Tom <tom.primozic@gmail.com>
Cc: Caml List <caml-list@yquem.inria.fr>
Subject: Re: [Caml-list] Function application implementation
Date: Thu, 26 Apr 2007 19:17:02 +1000	[thread overview]
Message-ID: <1177579022.8651.23.camel@rosella.wigram> (raw)
In-Reply-To: <c1490a380704260152h34df93e9p20decd2f13459bbe@mail.gmail.com>

On Thu, 2007-04-26 at 10:52 +0200, Tom wrote:
> 
> 
> On 26/04/07, skaller <skaller@users.sourceforge.net> wrote:
>         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). 

push (eval f) calculates the expression f,
which results in a closure. Apply, with the stack:

	closure f <-- top
	value 1
	...

calculates 

	apply(closure f, value 1)

That is how functions are called. In practice, a compiler may do
optimisations.

In the Felix compiler for example, in the expression:

	apply(f,e)

if the subexpresion f is a simple function constant, then the compiler
can inline the function. Otherwise, a closure has to be formed. In Felix
this means instantiating a C++ class (the function f) to make a closure
(an object of the class). In Felix the actual C++ used is:

	(new f(environment)) -> apply (e)

In other words, all compilers will look for optimisations such
as are made possible when a direct call is detected, inlining
in such cases being one possible optimisation which could be applied.

the actual sequence I have above may not be how the Ocaml compiler
organises it: the point is that the model is built to not need
to make the distinction you're asking about: that's just an 
optimisation.

-- 
John Skaller <skaller at users dot sf dot net>
Felix, successor to C++: http://felix.sf.net


  reply	other threads:[~2007-04-26  9:17 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2007-04-25 22:38 Tom
2007-04-26  3:13 ` [Caml-list] " skaller
2007-04-26  8:52   ` Tom
2007-04-26  9:17     ` skaller [this message]
2007-04-26  9:25     ` Xavier Leroy

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1177579022.8651.23.camel@rosella.wigram \
    --to=skaller@users.sourceforge.net \
    --cc=caml-list@yquem.inria.fr \
    --cc=tom.primozic@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox