caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Jon Harrop <jon@jdh30.plus.com>
To: caml-list@yquem.inria.fr
Subject: Re: [Caml-list] 'a Set?
Date: Wed, 26 Jan 2005 09:13:08 +0000	[thread overview]
Message-ID: <200501260913.09018.jon@jdh30.plus.com> (raw)
In-Reply-To: <727068A7-6F2C-11D9-8411-0003939A19AA@fas.harvard.edu>

On Tuesday 25 January 2005 23:54, Mike Hamburg wrote:
> Is there a clean way to do this without removing the code from set.ml
> and modifying it?

I do not believe so. I have also had to do this.

Compared to a flat set of functions, the functor approach has the advantage of 
enforcing a consistently used compare function. The same effect can be 
achieved with "elt = 'a" by writing a higher-order function which returns a 
record containing the Set.* functions using the given function argument as 
the compare function. Something equivalent to this:

  type 'a t = 'a list
  type 'a set =
    { empty : 'a t;
      is_empty : 'a t -> bool;
      add : 'a -> 'a t -> 'a t;
      mem : 'a -> 'a t -> bool }

  let rec add compare e = function
    [] -> [e]
  | h :: t -> match compare h e with
      -1 -> e :: h :: t
    | 0 -> e :: t
    | _ -> h :: add compare e t
  let rec mem compare e = function
    [] -> false
  | h :: t -> match compare h e with
      -1 -> false
    | 0 -> true
    | _ -> mem compare e t

  let make ?(compare=compare) () =
    { empty = [];
      is_empty = (fun s -> s=[]);
      add = add compare;
      mem = mem compare }

Possible issues with this are that building closures (i.e. in "make") is 
expensive and that the resulting type is monomorphic ('_a). You can probably 
get a polymorphic type most easily by putting the definitions of "add" etc. 
in the record definition, rather than partially applying their arguments.

Cheers,
Jon.


  parent reply	other threads:[~2005-01-26 14:30 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2005-01-25 23:54 Mike Hamburg
2005-01-26  8:25 ` [Caml-list] " Jean-Christophe Filliatre
2005-01-26 10:13   ` Frédéric Gava
2005-01-26 11:04     ` Radu Grigore
2005-01-26 12:04       ` Jacques Garrigue
2005-01-26 16:00         ` Alex Baretta
2005-01-26 16:14           ` Jacques Carette
2005-01-26 21:09           ` Mike Hamburg
2005-01-29  9:55         ` Radu Grigore
2005-01-26  9:13 ` Jon Harrop [this message]
2005-01-26 15:36   ` Frédéric Gava
2005-01-26 16:06     ` Jon Harrop

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=200501260913.09018.jon@jdh30.plus.com \
    --to=jon@jdh30.plus.com \
    --cc=caml-list@yquem.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).