Usable version:
[tatoo.git] / src / hcons.mli
1 (***********************************************************************)
2 (*                                                                     *)
3 (*                               TAToo                                 *)
4 (*                                                                     *)
5 (*                     Kim Nguyen, LRI UMR8623                         *)
6 (*                   Université Paris-Sud & CNRS                       *)
7 (*                                                                     *)
8 (*  Copyright 2010-2012 Université Paris-Sud and Centre National de la *)
9 (*  Recherche Scientifique. All rights reserved.  This file is         *)
10 (*  distributed under the terms of the GNU Lesser General Public       *)
11 (*  License, with the special exception on linking described in file   *)
12 (*  ../LICENSE.                                                        *)
13 (*                                                                     *)
14 (***********************************************************************)
15
16 (** Implementation of generic hashconsing. *)
17
18 module type Abstract =
19   sig
20     type data
21       (** The type of the data to be hashconsed *)
22
23     type t
24       (** The type of hashconsed data *)
25
26     val make : data -> t
27       (** [make v] internalizes the value [v], making it an hashconsed
28           value.
29       *)
30
31     val node : t -> data
32       (** [node h] extract the original data from its hashconsed value
33       *)
34
35     val hash : t -> int
36       (** [hash h] returns a hash of [h], such that for every [h1] and
37           [h2], [equal h1 h2] implies [hash h1 = hash h2].
38       *)
39
40     val uid : t -> Uid.t
41       (** [uid h] returns a unique identifier *)
42     val equal : t -> t -> bool
43       (** Equality between hashconsed values. Equivalent to [==] *)
44
45     val init : unit -> unit
46       (** Initializes the internal storage. Any previously hashconsed
47           element is discarded. *)
48
49   end
50 (** Abstract signature of a module implementing an hashconsed datatype *)
51
52 module type S =
53 sig
54   type data
55   type t = private { id   : Uid.t;
56                      key  : int;
57                      node : data }
58   include Abstract with type data := data and type t := t
59 end
60
61 (** Output signature of the functor {!Hcons.Make} *)
62
63 module Make (H : Sigs.HashedType) : S with type data = H.t
64 (** Functor building an implementation of hashconsed values for a given
65     implementation of {!Sigs.HashedType}. Hashconsed values are
66     persistent: the are kept in memory even if no external reference
67     remain. Calling [init()] explicitely will reclaim the space.
68 *)
69
70 module Weak (H : Sigs.HashedType) : S with type data = H.t
71 (** Functor building an implementation of hashconsed values for a given
72     implementation of {!Sigs.HashedType}. Hashconsed values have a
73     weak semantics: they may be reclaimed as soon as no external
74     reference to them exists. The space may still be reclaimed
75     explicitely by calling [init].
76 *)
77
78 module PosInt : Abstract with type data = int and type t = int
79 (** Compact implementation of hashconsed positive integer that
80     avoids boxing. [PosInt.make v] raises [Invalid_argument] if
81     [ v < 0 ]
82 *)