caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Michael Ekstrand <michael+ocaml@elehack.net>
To: caml-list@inria.fr
Subject: Re: Cartesian product
Date: Fri, 31 Jul 2009 11:23:24 -0500	[thread overview]
Message-ID: <h4v5po$hn4$1@ger.gmane.org> (raw)
In-Reply-To: <8401c38a0907301454q1c60ac2frf8b0ab9027a6475d@mail.gmail.com>

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


  reply	other threads:[~2009-07-31 16:23 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-07-30 17:56 Ligia Nistor
2009-07-30 18:46 ` [Caml-list] " Peng Zang
2009-07-30 18:53   ` Peng Zang
2009-07-30 20:08     ` Erick Matsen
2009-07-30 20:22 ` Brian Hurt
2009-07-30 21:54   ` Ligia Nistor
2009-07-31 16:23     ` Michael Ekstrand [this message]
2009-07-30 21:26 ` Damien Guichard

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='h4v5po$hn4$1@ger.gmane.org' \
    --to=michael+ocaml@elehack.net \
    --cc=caml-list@inria.fr \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).