caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Xavier Leroy <Xavier.Leroy@inria.fr>
To: "Krishnaswami, Neel" <neelk@cswcasa.com>
Cc: "'caml-list@inria.fr'" <caml-list@inria.fr>
Subject: Re: [Caml-list] Why can't I use constructors as functions?
Date: Fri, 3 Aug 2001 10:16:51 +0200	[thread overview]
Message-ID: <20010803101651.C29703@pauillac.inria.fr> (raw)
In-Reply-To: <B1E4D3274D57D411BE8400D0B783FF322E8692@exchange1.cswv.com>; from neelk@cswcasa.com on Mon, Jul 30, 2001 at 02:51:07PM -0400

> I'm curious as to the reason why I can't use a datatype constructor
> as a function. Eg, in SML I can write a function like this:
>   fun add a b = fold Succ a b (* Use the Succ constructor as a funtion *)
> If I try something similar in Caml, 
> Instead I need to wrap it in a function:
>   # let add a b = fold (fun x -> Succ x) a b

The old Caml V3.1 implementation treated constructors as functions like SML.
In Caml Light, I chose to drop this equivalence for several reasons:

- Simplicity of the compiler.  Internally, constructors are not
functions, and a special case is needed to transform Succ into
(fun x -> Succ x) when needed.  This isn't hard, but remember that
Caml Light was really a minimal, stripped-down version of Caml.

- Constructors in Caml Light and OCaml really have an arity, e.g.
C of int * int is really a constructor with two integer arguments,
not a constructor taking one argument that is a pair.  Hence, there
would be two ways to map the constructor C to a function:
        fun (x,y) -> C(x,y)
or
        fun x y -> C(x,y)
The former is more natural if you come from an SML background
(where constructors have 0 or 1 argument), but the latter fits better
the Caml Light / OCaml execution model, which favors curried
functions.  By not treating constructors like functions, we avoid
having to choose...

- Code clarity.  While using a constructor as a function is sometimes
convenient, I would argue it is often hard to read.  Writing
"fun x -> Succ x" is more verbose, but easier to read, I think.

- 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-03  8:16 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2001-07-30 18:51 Krishnaswami, Neel
2001-08-03  8:16 ` Xavier Leroy [this message]
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
     [not found] <200108031627.JAA19052@dhpc0010.pdx.intel.com>
2001-08-06  8:14 ` 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=20010803101651.C29703@pauillac.inria.fr \
    --to=xavier.leroy@inria.fr \
    --cc=caml-list@inria.fr \
    --cc=neelk@cswcasa.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;
as well as URLs for NNTP newsgroup(s).