caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* arity of type constructors
@ 1997-05-07 13:16 Christian Lindig
  1997-05-08 20:15 ` David Monniaux
  1997-05-09  5:57 ` Michel Quercia
  0 siblings, 2 replies; 7+ messages in thread
From: Christian Lindig @ 1997-05-07 13:16 UTC (permalink / raw)
  To: caml-list


Dear Caml-Enthusiasts,

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:

        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?


Christian

[sorry, no french version - will visit Paris in summer as a compensation :-)]
------------------------------------------------------------------------------
 Christian Lindig  				       lindig@ips.cs.tu-bs.de
 TU Braunschweig				       fon   +49 531 391 7465
 Institut fuer Programmiersprachen		       fax   +49 531 391 8140
 D-38106 Braunschweig             		       http://www.cs.tu-bs.de







^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: arity of type constructors
  1997-05-07 13:16 arity of type constructors Christian Lindig
@ 1997-05-08 20:15 ` David Monniaux
  1997-05-21 10:50   ` John Harrison
  1997-05-09  5:57 ` Michel Quercia
  1 sibling, 1 reply; 7+ messages in thread
From: David Monniaux @ 1997-05-08 20:15 UTC (permalink / raw)
  To: Christian Lindig; +Cc: caml-list

[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





^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: arity of type constructors
  1997-05-07 13:16 arity of type constructors Christian Lindig
  1997-05-08 20:15 ` David Monniaux
@ 1997-05-09  5:57 ` Michel Quercia
  1 sibling, 0 replies; 7+ messages in thread
From: Michel Quercia @ 1997-05-09  5:57 UTC (permalink / raw)
  To: Christian Lindig; +Cc: caml-list

Christian Lindig wrote:
> 
>         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?
> 

here is the trick :

        Objective Caml version 1.05

# type t = T of int * int;;
type t = | T of int * int
# T(3,4);;
- : t = T (3, 4)
# let x = (3,4) in T(x);;
The constructor T expects 2 argument(s), but is here applied to 1
argument(s)
# type u = U of (int * int);;
type u = | U of (int * int)
# let x = (3,4) in U(x);;    
- : u = U (3, 4)
# 


-- 
Michel Quercia
Lycee Carnot  16 bd Thiers  21000 Dijon
mailto:quercia@cal.enst.fr




^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: arity of type constructors
  1997-05-08 20:15 ` David Monniaux
@ 1997-05-21 10:50   ` John Harrison
  1997-05-21 18:46     ` Xavier Leroy
  0 siblings, 1 reply; 7+ messages in thread
From: John Harrison @ 1997-05-21 10:50 UTC (permalink / raw)
  To: caml-list; +Cc: John.Harrison


David Monniaux writes:

| 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.

In that case, what happens during pattern-matching? For example:

  >       Caml Light version 0.73
 
  #type triv = Triv of int*int;;
  Type triv defined.
  #let getpair = fun (Triv(p)) -> p;;
  getpair : triv -> int * int = <fun>
 
Is this sort of definition safe? Perhaps one should use the following
to ensure a pair is reconstructed:

  #let getpair = fun (Triv(a,b)) -> a,b;;

John.





^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: arity of type constructors
  1997-05-21 10:50   ` John Harrison
@ 1997-05-21 18:46     ` Xavier Leroy
  0 siblings, 0 replies; 7+ messages in thread
From: Xavier Leroy @ 1997-05-21 18:46 UTC (permalink / raw)
  To: John Harrison; +Cc: caml-list

> 
>   >       Caml Light version 0.73
>  
>   #type triv = Triv of int*int;;
>   Type triv defined.
>   #let getpair = fun (Triv(p)) -> p;;
>   getpair : triv -> int * int = <fun>
>  
> Is this sort of definition safe? Perhaps one should use the following
> to ensure a pair is reconstructed:
> 
>   #let getpair = fun (Triv(a,b)) -> a,b;;

The Caml Light compiler will figure it out itself -- it emits code to
reconstruct the pair as in your second version of getpair.

The reason Objective Caml insists on constructors having an arity,
instead of generating correcting code as Caml Light, is that
Objective Caml's module calculus, and in particular functors, makes it
much harder to generate the right correcting code. That problem plagues all
implementations of Standard ML, and considerable ingenuity has been
expanded to handle it. I think it simply makes more sense to expose
the arity of constructors to the programmers.

- Xavier Leroy






^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: arity of type constructors
@ 1997-05-12  7:08 Hubert Canon
  0 siblings, 0 replies; 7+ messages in thread
From: Hubert Canon @ 1997-05-12  7:08 UTC (permalink / raw)
  To: caml-list

> # type t = T of int * int;;
> type t = | T of int * int
> # T(3,4);;

I do not understand why we don't use another syntax for constructors :

If we had something like :
# T 3 4;;
- : t = (T 3 4)

it would be less confusing.

-- 
Hubert Canon





^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re:  arity of type constructors
@ 1997-05-09 11:53 Damien Doligez
  0 siblings, 0 replies; 7+ messages in thread
From: Damien Doligez @ 1997-05-09 11:53 UTC (permalink / raw)
  To: caml-list

>Applying T to x does not work, but applying it to (3,4) does. Why is
>the pair (3,4) counted as 2 arguments?

Constructors have an arity.  Here, T expects two arguments.  When you
write T(3,4), you're not applying T to a pair, but applying T to two
arguments, 3 and 4.  It happens that the syntax looks like a pair, but
this is more a misfeature of the syntax than anything else.

The alternative is to make all constructors take one argument, and use
a pair as argument when you want two arguments.  This approach is
harder to implement and it makes it possible to write horribly
obfuscated code.  Moreover, the pair has to be something special.  In
the current system, the pair is just another binary constructor.

We have tried both approaches in Caml Light, and I really think the
current one is the right one.


En Francais:

Les constructeurs ont une arite.  Quand on ecrit T(3,4), on n'applique
pas T a la paire (3,4), comme la syntaxe pourrait le faire croire,
mais on applique T a deux arguments, 3 et 4.

-- Damien





^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~1997-05-22  7:42 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1997-05-07 13:16 arity of type constructors Christian Lindig
1997-05-08 20:15 ` David Monniaux
1997-05-21 10:50   ` John Harrison
1997-05-21 18:46     ` Xavier Leroy
1997-05-09  5:57 ` Michel Quercia
1997-05-09 11:53 Damien Doligez
1997-05-12  7:08 Hubert Canon

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).