| 1 | (************************************************************************) |
| 2 | (* * The Rocq Prover / The Rocq Development Team *) |
| 3 | (* v * Copyright INRIA, CNRS and contributors *) |
| 4 | (* <O___,, * (see version control and CREDITS file for authors & dates) *) |
| 5 | (* \VV/ **************************************************************) |
| 6 | (* // * This file is distributed under the terms of the *) |
| 7 | (* * GNU Lesser General Public License Version 2.1 *) |
| 8 | (* * (see LICENSE file for the text of the license) *) |
| 9 | (************************************************************************) |
| 10 | |
| 11 | (** An imperative implementation of partitions via Union-Find *) |
| 12 | |
| 13 | (** Paths are compressed imperatively at each lookup of a |
| 14 | canonical representative. Each union also modifies in-place |
| 15 | the partition structure. |
| 16 | |
| 17 | Nota: for the moment we use Pervasive's comparison for |
| 18 | choosing the smallest object as representative. This could |
| 19 | be made more generic. |
| 20 | *) |
| 21 | |
| 22 | module type PartitionSig = sig |
| 23 | |
| 24 | (** The type of elements in the partition *) |
| 25 | type elt |
| 26 | |
| 27 | (** A set structure over elements *) |
| 28 | type set |
| 29 | |
| 30 | (** The type of partitions *) |
| 31 | type t |
| 32 | |
| 33 | (** Initialise an empty partition *) |
| 34 | val create : unit -> t |
| 35 | |
| 36 | (** Add (in place) an element in the partition, or do nothing |
| 37 | if the element is already in the partition. *) |
| 38 | val add : elt -> t -> unit |
| 39 | |
| 40 | (** Find the canonical representative of an element. |
| 41 | Raise [not_found] if the element isn't known yet. *) |
| 42 | val find : elt -> t -> elt |
| 43 | |
| 44 | (** Merge (in place) the equivalence classes of two elements. |
| 45 | This will add the elements in the partition if necessary. *) |
| 46 | val union : elt -> elt -> t -> unit |
| 47 | |
| 48 | (** Merge (in place) the equivalence classes of many elements. *) |
| 49 | val union_set : set -> t -> unit |
| 50 | |
| 51 | (** Listing the different components of the partition *) |
| 52 | val partition : t -> set list |
| 53 | |
| 54 | end |
| 55 | |
| 56 | module type SetS = |
| 57 | sig |
| 58 | type t |
| 59 | type elt |
| 60 | val singleton : elt -> t |
| 61 | val union : t -> t -> t |
| 62 | val choose : t -> elt |
| 63 | val iter : (elt -> unit) -> t -> unit |
| 64 | end |
| 65 | (** Minimal interface for sets, subtype of stdlib's Set. *) |
| 66 | |
| 67 | module type MapS = |
| 68 | sig |
| 69 | type key |
| 70 | type +'a t |
| 71 | val empty : 'a t |
| 72 | val find : key -> 'a t -> 'a |
| 73 | val add : key -> 'a -> 'a t -> 'a t |
| 74 | val mem : key -> 'a t -> bool |
| 75 | val fold : (key -> 'a -> 'b -> 'b) -> 'a t -> 'b -> 'b |
| 76 | end |
| 77 | (** Minimal interface for maps, subtype of stdlib's Map. *) |
| 78 | |
| 79 | module Make : |
| 80 | functor (S:SetS) -> |
| 81 | functor (_:MapS with type key = S.elt) -> |
| 82 | PartitionSig with type elt = S.elt and type set = S.t |