Version française
Home     About     Download     Resources     Contact us    
Browse thread
Cartesian product
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Michael Ekstrand <michael+ocaml@e...>
Subject: Re: Cartesian product
Ligia Nistor wrote:
> Thanks for the reply. This is how I thought of doing it, but in the module
> TypeType, type t should be a list of types( the list has to be ref, so that
> it can change its length). This way, you can do the cartesian product of an
> arbitrary number of sets, not only of 2.

That would be possible (use a type of Type.t list), but the resulting
set types would have a static typing hole: you would not be guaranteed
that all elements would have the same length.

Another way to do it would be to have a Product functor, like so:

module Product = functor (T1: Set.ORDERED_TYPE) -> functor (T2:
Set.ORDERED_TYPE) -> struct
    type t = T1.t * T2.t
    let compare (a1,b1) (a2,b2) =
        let r = T1.compare a1 a2 in
          if r = 0 then T2.compare b1 b2
          else r
end

Then you can create a 2-ary product set type:

module Set2 = Set.Make(Product(Type)(Type))

and build further sets by constructing products of products:

module Set3 = Set.Make(Product(Product(Type))(Type)))

That would allow you to construct arbitrarily high-dimensional product
set types that still remain type-safe.

- Michael