Version française
Home     About     Download     Resources     Contact us    
Browse thread
(int * int) <> int*int ?
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Jacques Garrigue <garrigue@m...>
Subject: Re: [Caml-list] (int * int) <> int*int ?
From: Frédéric Gava <gava@univ-paris12.fr>
> < For performance issues, is there a way to not have the rebuild
> > of the pair in
> > # type t = A of int*int | B of (int*int);;
> > # fun x -> match x with A (a,b) -> (a,b) | B a -> a;;
> >
> > ?
> Peraps this idea:
> if (all the time) A of (int*int) ==> A of int*int then in pattern mathching
> "A a" the "a" is directly built as a tuple from the tuple of the concrete
> type (just a little modification of the tag). Is it not a good idea ;-) ?

You can't do this, because values are shared: if you replace the A tag
by 0, you have really changed the original value for x, and subsequent
pattern-matching will be wrong.
Interestingly, it would be safe to just return x itself: when you
access a tuple you never look at its tag. However, this would still be
incorrect because of polymorphic equality: A(a,b) is not structurally
equal to 0(a,b) (assuming A is not the first tag.)

So there's no solution here.

By the way, the answer to your original question is that the syntax
for datatypes is wrong. A datatype does not take a tuple as argument,
but a fixed number of independent values. As somebody already pointed,
the revised syntax corrects this. The standard syntax is just a
consequence of history.
This unfortunate syntax has consequences down to polymorphic variants,
which otherwise could be represented more efficiently.

Jacques