Mailing list for all users of the OCaml language and system.
 help / color / mirror / Atom feed
From: "Denis Bueno" <dbueno@gmail.com>
To: "Arnaud Spiwack" <aspiwack@lix.polytechnique.fr>
Cc: "Caml List" <caml-list@inria.fr>
Subject: Re: [Caml-list] Instanciating functor types with extra parameters
Date: Sat, 4 Aug 2007 10:15:41 -0600	[thread overview]
Message-ID: <6dbd4d000708040915l1d8b62cbi265244983bbc5e71@mail.gmail.com> (raw)
In-Reply-To: <46B4A343.5030900@lix.polytechnique.fr>

On 8/4/07, Arnaud Spiwack <aspiwack@lix.polytechnique.fr> wrote:
> When I have a functor type, like for example (not too innocent I'm afraid) :
>
> module type OrderedType =
>   sig
>     type t
>     val compare : t -> t -> int
>   end
>
>
> Something that naive intuition would allow you to do is something like :
>
> module GenOrder : OrderedType =
>   struct
>     type t = 'a
>     let compare = compare
>   end
>
[... snip ...]
> My point is that I know absolutely no way of doing such a thing. Hence I
> can't make a set with total or partial genericity. If I want to add type
> parameters I have to rewrite the *whole* set library. Actually... I've
> got to copy past it, and replace all occurences of    t   with an 'a t
> for instance. Or ('a,'b) t if I have two arguments. I really had to do
> that once, and almost a couple of other time. That's why it infuriates
> me as I said earlier.

You can do something slightly less general that certainly doesn't
require rewriting the whole set library:  One comparator definition
per set you'd like to create.

module StringOrder : OrderedType = struct
  type t = string
  (* just to be clear about which compare to capture)
  let compare = Pervasives.compare
end

module IntOrder : OrderedType = struct
  type t = int
  let compare = Pervasives.compare
end

... and so on.  You can use these to create functors, of course:

module StringSet = Set.Make (StringOrder)
module IntSet = Set.Make (IntSet)

I think you'll find, though, as time goes on that you'll use special
comparators: that is, not just generic string comparison, but some
other total order on strings.  And you'll want to name
MySpecialStringCompare accordnigly.

module CaseInsensitiveStrOrder : OrderedType = struct
  type t = string
  let compare x y = Pervasives.compare (downcase x) (downcase y)
end
module CaseInsensitiveSet = Set.Make (CaseInsensitiveStrOrder)

                      Denis
--
"Program testing can be used to show the presence of bugs, but never to show
their absence." -- Edsger Dijkstra


  reply	other threads:[~2007-08-04 16:15 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 ` Denis Bueno [this message]
2007-08-04 18:51 ` [Caml-list] " rossberg
2007-08-04 19:13 ` Oliver Bandel
2007-08-06 13:47 ` Mike Furr

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=6dbd4d000708040915l1d8b62cbi265244983bbc5e71@mail.gmail.com \
    --to=dbueno@gmail.com \
    --cc=aspiwack@lix.polytechnique.fr \
    --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