Version française
Home     About     Download     Resources     Contact us    
Browse thread
arity of type constructors
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: David Monniaux <David.Monniaux@e...>
Subject: Re: arity of type constructors
[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