Date: Thu, 8 May 1997 22:15:23 +0200 (MET DST)
From: David Monniaux <David.Monniaux@ens-lyon.fr>
To: Christian Lindig <lindig@ips.cs.tu-bs.de>
Subject: Re: arity of type constructors
In-Reply-To: <199705071316.PAA14881@infbsst5.ips.cs.tu-bs.de>
[en francais: pourquoi un constructeur qui prend une paire comme argument
n'est pas la meme chose qu'un constructeur qui prend deux arguments]
On Wed, 7 May 1997, Christian Lindig wrote:
> the following example of applying arguments to a new type constructor
> was surprising for me. I'm wondering if it's a bug or a feature:
It's a feature.
> Objective Caml version 1.05
> # type t = T of int * int;;
> type t = | T of int * int
> # let x = (3,4);;
> val x : int * int = 3, 4
> # T x;;
> The constructor T expects 2 argument(s), but is here applied
> to 1 argument(s)
> # T (3,4);;
> - : t = T (3, 4)
>
> Applying T to x does not work, but applying it to (3,4) does. Why is
> the pair (3,4) counted as 2 arguments?
The problem is exactly there.
If you declare type t = T of int*int, it declares a type t whose only
constructor takes two parameters, of respective types int and int, not a
constructor that takes one paramete of type int*int. Thus it can't be
applied to a pair.
That is reflected in the way memory objects are handled. A constructor
that takes a pair as an argument will have the following layout:
T -> pair [ int
[ int
if it has two arguments, the layout is the following:
T [ int
[ int
Using a pair adds one level of indirection.
For what you want to work, just do the following:
Objective Caml version 1.05
# type t = T of (int*int);;
type t = | T of (int * int)
# let x = (3,4);;
val x : int * int = 3, 4
# T x;;
- : t = T (3, 4)
Cheers.
"Le MIM est un magistère." (private joke)
Computer science student at ENS, Lyon, France
http://www.ens-lyon.fr/~dmonniau
This archive was generated by hypermail 2b29 : Sun Jan 02 2000 - 11:58:10 MET