| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
module type OrderedType = |
| 12 |
sig |
| 13 |
type t |
| 14 |
val compare : t -> t -> int |
| 15 |
end |
| 16 |
|
| 17 |
module type S = Set.S |
| 18 |
|
| 19 |
module type ExtS = |
| 20 |
sig |
| 21 |
include CSig.SetS |
| 22 |
module List : sig |
| 23 |
val union : t list -> t |
| 24 |
end |
| 25 |
end |
| 26 |
|
| 27 |
module SetExt (M : Set.OrderedType) : |
| 28 |
sig |
| 29 |
type set = Set.Make(M).t |
| 30 |
module List : sig |
| 31 |
val union : set list -> set |
| 32 |
end |
| 33 |
end = |
| 34 |
struct |
| 35 |
module S = Set.Make(M) |
| 36 |
type set = S.t |
| 37 |
|
| 38 |
module List = struct |
| 39 |
let union = List.fold_left S.union S.empty |
| 40 |
end |
| 41 |
end |
| 42 |
|
| 43 |
module Make(M : Set.OrderedType) = |
| 44 |
struct |
| 45 |
include Set.Make(M) |
| 46 |
include SetExt(M) |
| 47 |
end |
| 48 |
|
| 49 |
module type HashedType = |
| 50 |
sig |
| 51 |
type t |
| 52 |
val hash : t -> int |
| 53 |
end |
| 54 |
|
| 55 |
module Hashcons(M : OrderedType)(H : Hashcons.HashedType with type t = M.t) = |
| 56 |
struct |
| 57 |
module Set = Make(M) |
| 58 |
|
| 59 |
type set = Set.t |
| 60 |
type _set = |
| 61 |
| SEmpty |
| 62 |
| SNode of set * M.t * set * int |
| 63 |
|
| 64 |
let set_prj : set -> _set = Obj.magic |
| 65 |
let set_inj : _set -> set = Obj.magic |
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
let rec spine s accu = match set_prj s with |
| 70 |
| SEmpty -> accu |
| 71 |
| SNode (l, v, r, _) -> spine l ((v, r) :: accu) |
| 72 |
|
| 73 |
let rec umap hacc s = match set_prj s with |
| 74 |
| SEmpty -> hacc, set_inj SEmpty |
| 75 |
| SNode (l, v, r, h) -> |
| 76 |
let hacc, l' = umap hacc l in |
| 77 |
let hv, v' = H.hcons v in |
| 78 |
let hacc = Hashset.Combine.combine hacc hv in |
| 79 |
let hacc, r' = umap hacc r in |
| 80 |
hacc, set_inj (SNode (l', v', r', h)) |
| 81 |
|
| 82 |
let rec eqeq s1 s2 = match s1, s2 with |
| 83 |
| [], [] -> true |
| 84 |
| (v1, r1) :: s1, (v2, r2) :: s2 -> |
| 85 |
v1 == v2 && eqeq (spine r1 s1) (spine r2 s2) |
| 86 |
| _ -> false |
| 87 |
|
| 88 |
module Hashed = |
| 89 |
struct |
| 90 |
type t = set |
| 91 |
let eq s1 s2 = s1 == s2 || eqeq (spine s1 []) (spine s2 []) |
| 92 |
let hashcons v = umap 0 v |
| 93 |
end |
| 94 |
|
| 95 |
include Hashcons.Make(Hashed) |
| 96 |
|
| 97 |
end |