Version française
Home     About     Download     Resources     Contact us    

This site is updated infrequently. For up-to-date information, please visit the new OCaml website at

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: 2009-07-31 (16:23)
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 = a1 a2 in
          if r = 0 then b1 b2
          else r

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