Debian Codegraph @

clib/hashcons.ml

raw symbols SCIP
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 (* Hash consing of datastructures *)
12
13 type 'a f = 'a -> int * 'a
14
15 (* [t] is the type of object to hash-cons
16 * [hashcons x] is a function that hash-cons the sub-structures of x
17 * [eq] is a comparison function. It is allowed to use physical equality
18 * on the sub-terms hash-consed by the hashcons function.
19 *)
20
21 module type HashconsedType =
22 sig
23 type t
24 val hashcons : t f
25 val eq : t -> t -> bool
26 end
27
28 module type HashconsedRecType = sig
29 type t
30
31 val hashcons : t f -> t f
32
33 val eq : t -> t -> bool
34
35 end
36
37 (** The output is a function [generate] such that [generate args] creates a
38 hash-table of the hash-consed objects, together with [hcons], a function
39 taking a table and an object, and hashcons it. For simplicity of use, we use
40 the wrapper functions defined below. *)
41
42 module type S =
43 sig
44 type t
45 type table
46 val generate : unit -> table
47 val hcons : table -> t f
48 val stats : table -> Hashset.statistics
49 end
50
51 module Make (X : HashconsedType) : (S with type t = X.t) =
52 struct
53 type t = X.t
54
55 (* We create the type of hashtables for t, with our comparison fun.
56 * An invariant is that the table never contains two entries equals
57 * w.r.t (=), although the equality on keys is X.eq. This is
58 * granted since we hcons the subterms before looking up in the table.
59 *)
60 module Htbl = Hashset.Make(X)
61
62 type table = Htbl.t
63
64 let generate () =
65 let tab = Htbl.create 97 in
66 tab
67
68 let hcons tab x =
69 let h, y = X.hashcons x in
70 h, Htbl.repr h y tab
71
72 let stats = Htbl.stats
73
74 end
75
76 module MakeRec (X : HashconsedRecType) : (S with type t = X.t) =
77 struct
78 type t = X.t
79
80 module Htbl = Hashset.Make(X)
81
82 type table = Htbl.t
83
84 let generate () =
85 let tab = Htbl.create 97 in
86 tab
87
88 let rec hcons tab x =
89 let h, y = X.hashcons (hcons tab) x in
90 h, Htbl.repr h y tab
91
92 let stats = Htbl.stats
93
94 end
95
96 (* A few useful wrappers:
97 * takes as argument the function [generate] above and build a function of type
98 * u -> t -> t that creates a fresh table each time it is applied to the
99 * sub-hcons functions. *)
100
101 (* For non-recursive types it is quite easy. *)
102 let simple_hcons h f u =
103 let table = h u in
104 fun x -> f table x
105
106 (* Basic hashcons modules for string and obj. Integers do not need be
107 hashconsed. *)
108
109 module type HashedType = sig
110 type t
111 val hcons : t f
112 end
113
114 (* list *)
115 module Hlist (D:HashedType) : S with type t = D.t list =
116 struct
117 module X =
118 struct
119 type t = D.t list
120 let eq l1 l2 =
121 l1 == l2 ||
122 match l1, l2 with
123 | [], [] -> true
124 | x1::l1, x2::l2 -> x1==x2 && l1==l2
125 | _ -> false
126 end
127
128 type t = X.t
129
130 module Htbl = Hashset.Make(X)
131
132 type table = Htbl.t
133
134 let generate () =
135 let tab = Htbl.create 97 in
136 tab
137
138 let rec hcons tab l =
139 let h, l = match l with
140 | [] -> 0, []
141 | x :: l ->
142 let hx, x = D.hcons x in
143 let h, l = hcons tab l in
144 let h = Hashset.Combine.combine hx h in
145 h, x :: l
146 in
147 h, Htbl.repr h l tab
148
149 let stats = Htbl.stats
150
151 end
152
153 let hashcons_array hcons a =
154 CArray.Smart.fold_left_map (fun acc x ->
155 let hx, x = hcons x in
156 Hashset.Combine.combine acc hx, x)
157 0
158 a