| 1 | (** Infinite sets over a chosen [OrderedType]. |
| 2 | |
| 3 | All operations over sets are purely applicative (no side-effects). |
| 4 | *) |
| 5 | |
| 6 | (** Input signature of the functor [Make]. *) |
| 7 | module type OrderedType = |
| 8 | sig |
| 9 | type t |
| 10 | (** The type of the elements in the set. |
| 11 | |
| 12 | The chosen [t] {b must be infinite}. *) |
| 13 | |
| 14 | val compare : t -> t -> int |
| 15 | (** A total ordering function over the set elements. |
| 16 | This is a two-argument function [f] such that: |
| 17 | - [f e1 e2] is zero if the elements [e1] and [e2] are equal, |
| 18 | - [f e1 e2] is strictly negative if [e1] is smaller than [e2], |
| 19 | - and [f e1 e2] is strictly positive if [e1] is greater than [e2]. |
| 20 | *) |
| 21 | end |
| 22 | |
| 23 | module type S = |
| 24 | sig |
| 25 | type elt |
| 26 | (** The type of the elements in the set. *) |
| 27 | |
| 28 | type t |
| 29 | (** The type of sets. *) |
| 30 | |
| 31 | val empty: t |
| 32 | (** The empty set. *) |
| 33 | |
| 34 | val full: t |
| 35 | (** The set of all elements (of type [elm]). *) |
| 36 | |
| 37 | val is_empty: t -> bool |
| 38 | (** Test whether a set is empty or not. *) |
| 39 | |
| 40 | val is_full: t -> bool |
| 41 | (** Test whether a set contains the whole type or not. *) |
| 42 | |
| 43 | val mem: elt -> t -> bool |
| 44 | (** [mem x s] tests whether [x] belongs to the set [s]. *) |
| 45 | |
| 46 | val singleton: elt -> t |
| 47 | (** [singleton x] returns the one-element set containing only [x]. *) |
| 48 | |
| 49 | val add: elt -> t -> t |
| 50 | (** [add x s] returns a set containing all elements of [s], |
| 51 | plus [x]. If [x] was already in [s], then [s] is returned unchanged. *) |
| 52 | |
| 53 | val remove: elt -> t -> t |
| 54 | (** [remove x s] returns a set containing all elements of [s], |
| 55 | except [x]. If [x] was not in [s], then [s] is returned unchanged. *) |
| 56 | |
| 57 | val union: t -> t -> t |
| 58 | (** Set union. *) |
| 59 | |
| 60 | val inter: t -> t -> t |
| 61 | (** Set intersection. *) |
| 62 | |
| 63 | val diff: t -> t -> t |
| 64 | (** Set difference. *) |
| 65 | |
| 66 | val complement: t -> t |
| 67 | (** Set complement. *) |
| 68 | |
| 69 | val equal: t -> t -> bool |
| 70 | (** [equal s1 s2] tests whether the sets [s1] and [s2] are |
| 71 | equal, that is, contain equal elements. *) |
| 72 | |
| 73 | val subset: t -> t -> bool |
| 74 | (** [subset s1 s2] tests whether the set [s1] is a subset of |
| 75 | the set [s2]. *) |
| 76 | |
| 77 | val elements: t -> bool * elt list |
| 78 | (** Gives a finite representation of the predicate: if the |
| 79 | boolean is false, then the predicate is given in extension. |
| 80 | if it is true, then the complement is given *) |
| 81 | |
| 82 | val is_finite : t -> bool |
| 83 | (** [true] if the predicate can be given as a finite set (if [elt] |
| 84 | is a finite type, we can have [is_finite x = false] yet [x] is |
| 85 | finite, but we don't know how to list its elements) *) |
| 86 | end |
| 87 | |
| 88 | (** The [Make] functor constructs an implementation for any [OrderedType]. *) |
| 89 | module Make (Ord : OrderedType) : (S with type elt = Ord.t) |