Debian Codegraph @

clib/hashcons.mli

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 (** Generic hash-consing. *)
12
13 (** {6 Hashconsing functorial interface} *)
14
15 type 'a f = 'a -> int * 'a
16 (** Type of hashconsing function for ['a]. The returned int is the hash. *)
17
18 module type HashconsedType =
19 sig
20 (** {6 Generic hashconsing signature}
21
22 Given an equivalence relation [eq], a hashconsing function is a
23 function that associates the same canonical element to two elements
24 related by [eq]. Usually, the element chosen is canonical w.r.t.
25 physical equality [(==)], so as to reduce memory consumption and
26 enhance efficiency of equality tests.
27
28 In order to ensure canonicality, we need a way to remember the element
29 associated to a class of equivalence; this is done using the table type
30 generated by the [Make] functor.
31 *)
32
33 type t
34 (** Type of objects to hashcons. *)
35
36 val hashcons : t f
37 (** The actual hashconsing function. It should be compatible with [eq], that is
38 [eq x (snd @@ hashcons f x) = true]. It also returns the hash. *)
39
40 val eq : t -> t -> bool
41 (** A comparison function. It is allowed to use physical equality
42 on the sub-terms hashconsed by the [hashcons] function, but it should be
43 insensible to shallow copy of the compared object.
44 It should be compatible with the hash returned by [hashcons], ie
45 [eq x y] implies [fst @@ hashcons x = fst @@ hashcons y].
46 *)
47 end
48
49 module type HashconsedRecType = sig
50 type t
51
52 val hashcons : t f -> t f
53 (** Hashcons the given constructor, calling the provided function on children. *)
54
55 val eq : t -> t -> bool
56 end
57
58 module type S =
59 sig
60 type t
61 (** Type of objects to hashcons. *)
62
63 type table
64 (** Type of hashconsing tables *)
65
66 val generate : unit -> table
67 (** This create a hashtable of the hashconsed objects. *)
68
69 val hcons : table -> t f
70 (** Perform the hashconsing of the given object within the table, and returns the hash. *)
71
72 val stats : table -> Hashset.statistics
73 (** Recover statistics of the hashconsing table. *)
74 end
75
76 module Make (X : HashconsedType) : (S with type t = X.t)
77 (** Create a new hashconsing, given canonicalization functions. *)
78
79 module MakeRec (X : HashconsedRecType) : (S with type t = X.t)
80 (** Create a new hashconsing, given canonicalization functions.
81 [hashcons] will get the resulting [hcons] as first argument. *)
82
83 (** {6 Wrappers} *)
84
85 (** These are intended to be used together with instances of the [Make]
86 functor. *)
87
88 val simple_hcons : ('u -> 'tab) -> ('tab -> 't -> 'v) -> 'u -> 't -> 'v
89 (** Typically used as [let hcons = simple_hcons H.generate H.hcons ()] where [H] is of type [S]. *)
90
91 (** {6 Hashconsing of usual structures} *)
92
93 module type HashedType = sig
94 type t
95 val hcons : t f
96 end
97
98 module Hlist (D:HashedType) : (S with type t = D.t list)
99 (** Hashconsing of lists. *)
100
101 val hashcons_array : 'v f -> 'v array f
102 (** Helper for array hashconsing. Shares the elements producing a new
103 array if needed, does not mutate the array, does not share the
104 array itself. *)