Version française
Home     About     Download     Resources     Contact us    
Browse thread
[Caml-list] variant with tuple arg in pattern match?
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Patrick M Doane <patrick@w...>
Subject: Re: [Caml-list] variant with tuple arg in pattern match?
Hi Chris,

You'll need to change your type declaration from

  type foo = Foo of int * int

to

  type foo = Foo of (int * int)

or

  type bar = int * int
  type foo = Foo of bar

Caml performs some optimizations on the representation of tuples to place
the arguments for the constructor as well as constructor name into one
word of memory.  This can be a big win, both in memory usage and in access
time because one pointer access is removed.

It makes this distinction syntactically in the type declarations.  In the
first case, the memory layout for values of type foo would be: 

   [ tag word | int1 | int2 ]

where the second case would be

   [ tag word |  pointer ]  ->  [ tag word | int1 | int2 ]

I would certainly like it if Caml could:

  1) Treat this entirely as an optimization issue and not make a syntactic
distinction.

  2) Be able to make reasonable choices about which representation would
be more appropriate.

I doubt that it would do as well as the programmer could and it certainly
would take a bit of effort without a real clear improvement.

On the other hand, left with the syntactic distincation, a possible
improvement would be to make a distinction between

  type foo = Foo of bar

and 

  type foo = Foo of (bar)

so that "equational reasoning" could hold with regard to type
declarations. On several occasions, I have been tempted not to create
another type declaration because of this representation issue.

Hope this helps

Patrick

On Wed, 4 Apr 2001, Chris Hecker wrote:

> 
> I can't find anything about this in the docs, faq, or list archives.
> 
> I expected ii to be bound to the int * int tuple (1,2) in this pattern match:
> 
> # type foo = Foo of int * int;;
> type foo = Foo of int * int
> #   match Foo (1,2) with Foo ii -> ii ;;
> Characters 23-29:
> The constructor Foo expects 2 argument(s),
> but is here applied to 1 argument(s)
> 
> 
> This works:
> 
> # match Foo (1,2) with Foo _ -> () ;;
> - : unit = ()
> 
> But I can't get an "as" clause to work, either:
> 
> # match Foo (1,2) with Foo ((_,_) as ii) -> ii ;;
> Characters 21-38:
> The constructor Foo expects 2 argument(s),
> but is here applied to 1 argument(s)
> 
> But, even if the "as" worked, it's messy compared to the top method.
>  
> Chris
> 
> -------------------
> To unsubscribe, mail caml-list-request@inria.fr.  Archives: http://caml.inria.fr
> 

-------------------
To unsubscribe, mail caml-list-request@inria.fr.  Archives: http://caml.inria.fr