Debian Codegraph @

clib/trie.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 module type S =
12 sig
13 type label
14 type data
15 type t
16 val empty : t
17 val get : t -> data
18 val next : t -> label -> t
19 val labels : t -> label list
20 val add : label list -> data -> t -> t
21 val remove : label list -> data -> t -> t
22 val iter : (label list -> data -> unit) -> t -> unit
23 end
24
25 module type Grp =
26 sig
27 type t
28 val nil : t
29 val is_nil : t -> bool
30 val add : t -> t -> t
31 val sub : t -> t -> t
32 end
33
34 module Make (Y : Map.OrderedType) (X : Grp) =
35 struct
36
37 module T_codom = Map.Make(Y)
38
39 type data = X.t
40 type label = Y.t
41 type t = Node of X.t * t T_codom.t
42
43 let codom_for_all f m =
44 let fold key v accu = f v && accu in
45 T_codom.fold fold m true
46
47 let empty = Node (X.nil, T_codom.empty)
48
49 let next (Node (_,m)) lbl = T_codom.find lbl m
50
51 let get (Node (hereset,_)) = hereset
52
53 let labels (Node (_,m)) =
54 (* FIXME: this is order-dependent. Try to find a more robust presentation? *)
55 List.rev (T_codom.fold (fun x _ acc -> x::acc) m [])
56
57 let is_empty_node (Node(a,b)) = (X.is_nil a) && (T_codom.is_empty b)
58
59 let assure_arc m lbl =
60 if T_codom.mem lbl m then
61 m
62 else
63 T_codom.add lbl (Node (X.nil,T_codom.empty)) m
64
65 let cleanse_arcs (Node (hereset,m)) =
66 let m = if codom_for_all is_empty_node m then T_codom.empty else m in
67 Node(hereset, m)
68
69 let rec at_path f (Node (hereset,m)) = function
70 | [] ->
71 cleanse_arcs (Node(f hereset,m))
72 | h::t ->
73 let m = assure_arc m h in
74 cleanse_arcs (Node(hereset,
75 T_codom.add h (at_path f (T_codom.find h m) t) m))
76
77 let add path v tm =
78 at_path (fun hereset -> X.add v hereset) tm path
79
80 let remove path v tm =
81 at_path (fun hereset -> X.sub hereset v) tm path
82
83 let iter f tlm =
84 let rec apprec pfx (Node(hereset,m)) =
85 let path = List.rev pfx in
86 f path hereset;
87 T_codom.iter (fun l tm -> apprec (l::pfx) tm) m
88 in
89 apprec [] tlm
90
91 end