caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Xavier Leroy <Xavier.Leroy@inria.fr>
To: John R Harrison <johnh@ichips.intel.com>
Cc: caml-list@inria.fr, Neel Krishnaswami <neelk@cswcasa.com>,
	Andreas Rossberg <rossberg@ps.uni-sb.de>,
	Markus Mottl <markus@mail4.ai.univie.ac.at>
Subject: Re: [Caml-list] Why can't I use constructors as functions?
Date: Mon, 6 Aug 2001 10:14:37 +0200	[thread overview]
Message-ID: <20010806101437.D7188@pauillac.inria.fr> (raw)
In-Reply-To: <200108031627.JAA19052@dhpc0010.pdx.intel.com>; from johnh@ichips.intel.com on Fri, Aug 03, 2001 at 09:27:15AM -0700

> | The old Caml V3.1 implementation treated constructors as functions like
> | SML. In Caml Light, I chose to drop this equivalence for several reasons:
> 
> I think you're forgetting that at some stage you must have re-introduced
> it.

You're correct, the "constructor as functions" was implemented in Caml
Light version 0.7.  Tremendous psychological pressure must have been
exerted on me to make me implement this, causing me to forget all
about it afterwards :-)

> I would be happy to have constructors curried or uncurried, but I don't
> see that exposing a distinct notion of arity serves any useful purpose.

It serves the following purpose: all implementations want to represent
specially a constructor that takes a tuple of arguments, e.g.
        C of int * int
representing it as one block tagged C with two integer fields, rather
than the "normal" representation as a block tagged C containing a
pointer to a block representing the pair.  This optimization is
crucial both for memory size (cuts down memory use by a factor of 5/3)
and for speed.

In combination with modules, it's very hard to perform this
representation trick transparently, i.e. as a compiler optimization.
The reason is that a module implementation can define

        type t = C of int * int
        type u = int * int
        let f x = (* code assuming that C(x,y) is represented as 1 block *)

yet its interface can abstract over u:

        type u
        type t = C of u
        val f : ...

and clients of the module assume a different representation for C,
namely that it has only one field containing "the" argument of C.
Some form of representation coercion or run-time type inspection is
necessary to deal with this case, and it can be extremely complex
(see the TIL compiler for an example).  

By exposing a notion of arity for constructors, we prevent the module
implementation above from matching the module signature "type t = C of u",
thus working around the problem.

More generally, I think the representation trick for constructors
taking several arguments is so crucial and so hard to perform as a
transparent optimization that it deserves to be exposed in the
language as a primitive concept of "constructor with arity".

> Isn't it conceptually simpler to have constructors behave as regular
> objects as much as possible? (Of course, not in pattern matching etc.)

As you said, constructors already have a special status in
pattern-matching, so it doesn't seem much more conceptually difficult
to treat them as a primitive concept distinct (and essentially
orthogonal) from functions.

- Xavier Leroy
-------------------
Bug reports: http://caml.inria.fr/bin/caml-bugs  FAQ: http://caml.inria.fr/FAQ/
To unsubscribe, mail caml-list-request@inria.fr  Archives: http://caml.inria.fr


       reply	other threads:[~2001-08-06  8:21 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <200108031627.JAA19052@dhpc0010.pdx.intel.com>
2001-08-06  8:14 ` Xavier Leroy [this message]
2001-07-30 18:51 Krishnaswami, Neel
2001-08-03  8:16 ` Xavier Leroy
2001-08-03  9:19   ` Andreas Rossberg
2001-08-03 10:00   ` Markus Mottl
     [not found]   ` <9kffrd$72l$1@qrnik.zagroda>
2001-08-05 12:57     ` Marcin 'Qrczak' Kowalczyk

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=20010806101437.D7188@pauillac.inria.fr \
    --to=xavier.leroy@inria.fr \
    --cc=caml-list@inria.fr \
    --cc=johnh@ichips.intel.com \
    --cc=markus@mail4.ai.univie.ac.at \
    --cc=neelk@cswcasa.com \
    --cc=rossberg@ps.uni-sb.de \
    /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).