caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* 'a Set?
@ 2005-01-25 23:54 Mike Hamburg
  2005-01-26  8:25 ` [Caml-list] " Jean-Christophe Filliatre
  2005-01-26  9:13 ` Jon Harrop
  0 siblings, 2 replies; 13+ messages in thread
From: Mike Hamburg @ 2005-01-25 23:54 UTC (permalink / raw)
  To: caml-list

Is there any clean way to make a type 'a set, corresponding to Set.Make 
of a module with type t='a and compare=Pervasives.compare?  I'm trying 
to make a module which uses sets of arbitrary types of objects, and I 
don't want to have to make it a functor.

So in other words, I'd like to make a module AlphaSet with type
type 'a t,
val is_empty: 'a t -> bool
val mem: 'a -> 'a t -> bool,
etc.

instead of Set which is a functor producing
types t, elt
val is_empty: t -> bool
val mem: elt -> t -> bool,
etc.

Is there a clean way to do this without removing the code from set.ml 
and modifying it?

Mike Hamburg


^ permalink raw reply	[flat|nested] 13+ messages in thread
* Re: 'a Set?
@ 2005-01-26 14:07 Jeff Shaw
  0 siblings, 0 replies; 13+ messages in thread
From: Jeff Shaw @ 2005-01-26 14:07 UTC (permalink / raw)
  To: hamburg; +Cc: caml-list

[-- Attachment #1: Type: text/plain, Size: 677 bytes --]

Mike,
I know of no way to do what you're asking for. However, you can always use lists as an ad hoc kind of set. Use List.mem to check membership, and some functions to add elements or create a set while looking for duplicates:


    let add n list =
      let rec aux = function
   l::ls when compare l n = 0 -> list
 | l::ls -> aux ls
 | [] -> list @ [n]
      in aux list

    let create list =
      let rec aux accumulator = function
   l::ls -> aux (add l accumulator) ls
 | [] -> accumulator
      in aux [] list

This is terribly inefficient for big sets, but it works. It could be made more efficient by making sure the lists are ordered, etc.

Jeff

[-- Attachment #2: Type: text/html, Size: 1629 bytes --]

^ permalink raw reply	[flat|nested] 13+ messages in thread

end of thread, other threads:[~2005-01-29  9:55 UTC | newest]

Thread overview: 13+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2005-01-25 23:54 'a Set? 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
2005-01-26 15:36   ` Frédéric Gava
2005-01-26 16:06     ` Jon Harrop
2005-01-26 14:07 Jeff Shaw

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).