(************************************************************************) (* * The Rocq Prover / The Rocq Development Team *) (* v * Copyright INRIA, CNRS and contributors *) (* <O___,, * (see version control and CREDITS file for authors & dates) *) (* \VV/ **************************************************************) (* // * This file is distributed under the terms of the *) (* * GNU Lesser General Public License Version 2.1 *) (* * (see LICENSE file for the text of the license) *) (************************************************************************)
(* Hash consing of datastructures *)
type'a f = 'a -> int * 'a
(* [t] is the type of object to hash-cons *[hashconsx]isafunctionthathash-consthesub-structuresofx *[eq]isacomparisonfunction.Itisallowedtousephysicalequality *onthesub-termshash-consedbythehashconsfunction.
*)
module type HashconsedType = sig type t val hashcons : t f val eq : t -> t -> bool end
module type HashconsedRecType = sig type t
val hashcons : t f -> t f
val eq : t -> t -> bool
end
(** The output is a function [generate] such that [generate args] creates a hash-tableofthehash-consedobjects,togetherwith[hcons],afunction takingatableandanobject,andhashconsit.Forsimplicityofuse,weuse
the wrapper functions defined below. *)
module type S = sig type t type table val generate : unit -> table val hcons : table -> t f val stats : table -> Hashset.statistics end
module Make (X : HashconsedType) : (S withtype t = X.t) = struct type t = X.t
(* We create the type of hashtables for t, with our comparison fun. *Aninvariantisthatthetablenevercontainstwoentriesequals *w.r.t(=),althoughtheequalityonkeysisX.eq.Thisis *grantedsincewehconsthesubtermsbeforelookingupinthetable.
*)
module Htbl = Hashset.Make(X)
type table = Htbl.t
let generate () = let tab = Htbl.create 97in
tab
let hcons tab x = let h, y = X.hashcons x in
h, Htbl.repr h y tab
let stats = Htbl.stats
end
module MakeRec (X : HashconsedRecType) : (S withtype t = X.t) = struct type t = X.t
module Htbl = Hashset.Make(X)
type table = Htbl.t
let generate () = let tab = Htbl.create 97in
tab
let rec hcons tab x = let h, y = X.hashcons (hcons tab) x in
h, Htbl.repr h y tab
let stats = Htbl.stats
end
(* A few useful wrappers: *takesasargumentthefunction[generate]aboveandbuildafunctionoftype *u->t->tthatcreatesafreshtableeachtimeitisappliedtothe
* sub-hcons functions. *)
(* For non-recursive types it is quite easy. *) let simple_hcons h f u = let table = h u in fun x -> f table x
(* Basic hashcons modules for string and obj. Integers do not need be
hashconsed. *)
module type HashedType = sig type t val hcons : t f end
(* list *)
module Hlist (D:HashedType) : S withtype t = D.t list = struct
module X = struct type t = D.t list let eq l1 l2 =
l1 == l2 || match l1, l2 with
| [], [] -> true
| x1::l1, x2::l2 -> x1==x2 && l1==l2
| _ -> false end
type t = X.t
module Htbl = Hashset.Make(X)
type table = Htbl.t
let generate () = let tab = Htbl.create 97in
tab
let rec hcons tab l = let h, l = match l with
| [] -> 0, []
| x :: l -> let hx, x = D.hcons x in let h, l = hcons tab l in let h = Hashset.Combine.combine hx h in
h, x :: l in
h, Htbl.repr h l tab
let stats = Htbl.stats
end
let hashcons_array hcons a =
CArray.Smart.fold_left_map (fun acc x -> let hx, x = hcons x in
Hashset.Combine.combine acc hx, x) 0
a
Messung V0.5 in Prozent
¤ Dauer der Verarbeitung: 0.10 Sekunden
(vorverarbeitet am 2026-09-27)
¤
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.