caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Mike Furr <furr@cs.umd.edu>
To: Caml List <caml-list@inria.fr>
Subject: Re: [Caml-list] Instanciating functor types with extra parameters
Date: Mon, 06 Aug 2007 09:47:29 -0400	[thread overview]
Message-ID: <46B72671.3040301@cs.umd.edu> (raw)
In-Reply-To: <46B4A343.5030900@lix.polytechnique.fr>

Arnaud Spiwack wrote:

> Something that naive intuition would allow you to do is something like :
> 
> module GenOrder : OrderedType =
>  struct
>    type t = 'a
>    let compare = compare
>  end

I had a similar problem while developing my OCaml-Reins data structure 
library (first release will be "real-soon-now").  Stephen Weeks offered 
the following solution:  the main idea is that you build a generic core 
module that is parameterized by the maximum number of type variables 
desired.  So for a set, you might define

module BaseSet = struct
   type 'a elt_
   type 'a set_ = Empty | Node of 'a set_ * 'a elt_ * 'a set_
   val empty : 'a set_
   val add : 'a elt_ -> 'a set_ -> 'a set_
   ...
   val compare_ : ('a elt_ -> 'a elt_ -> int) ->
      'a set_ -> 'a set_ -> int
   ...
end

Then, you can create a polymorphic version
module PolySet = struct
   include BaseSet
   type 'a t = 'a set_
   type 'a elt = 'a
   let compare = compare_ Pervasives.compare
   ...
end

or a monomorphic version:
module MonoSet(C : OrderedType) = struct
   type elt = C.t
   type 'a elt_ = elt
   type t = C.t set_
   let compare = compare_ C.compare
end

Thus allowing you to share all of the underlying code with only a little 
boilerplate.

Cheers,
-mike


      parent reply	other threads:[~2007-08-06 13:47 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2007-08-04 16:03 Arnaud Spiwack
2007-08-04 16:15 ` [Caml-list] " Denis Bueno
2007-08-04 18:51 ` rossberg
2007-08-04 19:13 ` Oliver Bandel
2007-08-06 13:47 ` Mike Furr [this message]

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=46B72671.3040301@cs.umd.edu \
    --to=furr@cs.umd.edu \
    --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).