caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: skaller <skaller@users.sourceforge.net>
To: Richard Jones <rich@annexia.org>
Cc: caml-list@inria.fr
Subject: Re: [Caml-list] How to compile different implementations of the	same lib
Date: Fri, 02 Dec 2005 10:47:35 +1100	[thread overview]
Message-ID: <1133480856.9266.43.camel@rosella> (raw)
In-Reply-To: <20051201200944.GA30251@furbychan.cocan.org>

On Thu, 2005-12-01 at 20:09 +0000, Richard Jones wrote:

> I don't quite understand this.  I get that if the .cmx isn't
> available, then one obviously cannot inline the code.  But why is it
> that you cannot use a simple call instruction even if the .cmx isn't
> available?

A function is not a piece of code. It is a piece of code
plus an object which together are called a closure. 
The object represents the environment.

It is probably easier to understand the Felix representation:
a function is a C++ class with an apply method. The class
has other members -- local variables of the function, and
a pointer to local variables of the function containing it .. etc.

This object is allocated on the heap.

For example:

	let f x = 
 		let g y = x + y
		g
	;;
	g 42
	;;

here, when you call f x, you get back g. But g is not just
a piece of code -- g remembers 42 somehow :)

Closures are required to implement lexical scoping,
and are what makes it possible to remember the environment
as in the case above -- that is, to provide first class
lexically scoped functions.

Actually I think ML and Ocaml have poor design here.
Ocaml allows:

	let x = 1;;
	let f y = y + x;;

So that top level functions (a) may still have an environment
such as 'x' above, and (b) f itself is initialised at start
up to refer to the environment.

It would be more correct IMHO to ban variables from the top
level and allow only functions, and, instantiate the
functions when they're used. If Ocaml did that, it would
ONLY need the code pointer. (Felix has the same problem,
but I think of a 'library' statement which bans global
scope variables and a 'program' statement which permits
them).

Note that failure to do (a) also plagues C/C++ and has
a high cost -- dynamic linkage of 'static' data is 
very expensive, nasty, and doesn't really work on
most platforms, and, it interferes with re-entrancy,
which is the principal requirement for thread safety.

This mess because actually .. even in C/C++ functions
are closures.

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


  reply	other threads:[~2005-12-01 23:48 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2005-11-30  9:05 Best way to choose an implementation of a lib ? Christophe Raffalli
2005-11-30 12:19 ` [Caml-list] " Jacques Garrigue
2005-11-30 14:38   ` How to compile different implementations of the same lib Daniel Bünzli
2005-11-30 16:09     ` [Caml-list] " Daniel Bünzli
2005-12-01  1:07       ` Jacques Garrigue
2005-12-01  1:53         ` malc
2005-12-01 17:03         ` Christophe Raffalli
2005-12-01 17:54           ` skaller
2005-12-01 20:09             ` Richard Jones
2005-12-01 23:47               ` skaller [this message]
2005-12-02  3:29               ` Jacques Garrigue
2005-12-01 21:33             ` Christophe Raffalli
2005-12-02  3:06               ` Jacques Garrigue
2005-11-30 16:24   ` how to detect .cmx incompatibility Stefano Zacchiroli
2005-11-30 19:48   ` [Caml-list] Best way to choose an implementation of a lib ? Eric Cooper
2005-12-01 17:05     ` Christophe Raffalli
2005-12-01 17:31       ` Eric Cooper

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=1133480856.9266.43.camel@rosella \
    --to=skaller@users.sourceforge.net \
    --cc=caml-list@inria.fr \
    --cc=rich@annexia.org \
    /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;
as well as URLs for NNTP newsgroup(s).