1 (* Copyright (C) 2005, HELM Team.
3 * This file is part of HELM, an Hypertextual, Electronic
4 * Library of Mathematics, developed at the Computer Science
5 * Department, University of Bologna, Italy.
7 * HELM is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
12 * HELM is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with HELM; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place - Suite 330, Boston,
22 * For details, see the HELM World-Wide-Web page,
23 * http://cs.unibo.it/helm/.
28 module DiscriminationTreeIndexing =
32 type path_string_elem = Cic.term;;
33 type path_string = path_string_elem list;;
36 (* needed by the retrieve_* functions, to know the arities of the "functions" *)
38 let arities = Hashtbl.create 11;;
41 let rec path_string_of_term = function
42 | Cic.Meta _ -> [Cic.Implicit None]
43 | Cic.Appl ((hd::tl) as l) ->
44 if not (Hashtbl.mem arities hd) then
45 Hashtbl.add arities hd (List.length tl);
46 List.concat (List.map path_string_of_term l)
51 module OrderedPathStringElement = struct
52 type t = path_string_elem
54 let compare = Pervasives.compare
57 module PSMap = Map.Make(OrderedPathStringElement);;
61 module DiscriminationTree = Trie.Make(PSMap);;
63 type t = A.t DiscriminationTree.t
64 let empty = DiscriminationTree.empty
67 module OrderedPosEquality = struct
68 type t = Utils.pos * Inference.equality
69 let compare = Pervasives.compare
72 module PosEqSet = Set.Make(OrderedPosEquality);;
74 let string_of_discrimination_tree tree =
75 let rec to_string level = function
76 | DiscriminationTree.Node (value, map) ->
80 (String.make (2 * level) ' ') ^
81 "{" ^ (String.concat "; "
84 "(" ^ (Utils.string_of_pos p) ^ ", " ^
85 (Inference.string_of_equality e) ^ ")")
86 (PosEqSet.elements v))) ^ "}"
93 let ks = CicPp.ppterm k in
94 let rs = to_string (level+1) v in
95 ((String.make (2 * level) ' ') ^ ks ^ "\n" ^ rs)::s)
104 let index tree term info =
105 let ps = path_string_of_term term in
107 try DiscriminationTree.find ps tree
108 with Not_found -> A.empty in
110 DiscriminationTree.add ps (A.add info ps_set) tree in
114 let index tree equality =
115 let _, _, (_, l, r, ordering), _, _ = equality in
116 let psl = path_string_of_term l
117 and psr = path_string_of_term r in
118 let index pos tree ps =
120 try DiscriminationTree.find ps tree with Not_found -> PosEqSet.empty in
122 DiscriminationTree.add ps (PosEqSet.add (pos, equality) ps_set) tree in
126 | Utils.Gt -> index Utils.Left tree psl
127 | Utils.Lt -> index Utils.Right tree psr
129 let tree = index Utils.Left tree psl in
130 index Utils.Right tree psr
134 let remove_index tree term info =
135 let ps = path_string_of_term term in
138 A.remove info (DiscriminationTree.find ps tree) in
139 if A.is_empty ps_set then
140 DiscriminationTree.remove ps tree
142 DiscriminationTree.add ps ps_set tree
147 let remove_index tree equality =
148 let _, _, (_, l, r, ordering), _, _ = equality in
149 let psl = path_string_of_term l
150 and psr = path_string_of_term r in
151 let remove_index pos tree ps =
154 PosEqSet.remove (pos, equality) (DiscriminationTree.find ps tree) in
155 if PosEqSet.is_empty ps_set then
156 DiscriminationTree.remove ps tree
158 DiscriminationTree.add ps ps_set tree
163 | Utils.Gt -> remove_index Utils.Left tree psl
164 | Utils.Lt -> remove_index Utils.Right tree psr
166 let tree = remove_index Utils.Left tree psl in
167 remove_index Utils.Right tree psr
172 let in_index tree term test =
173 let ps = path_string_of_term term in
175 let ps_set = DiscriminationTree.find ps tree in
181 let in_index tree equality =
182 let _, _, (_, l, r, ordering), _, _ = equality in
183 let psl = path_string_of_term l
184 and psr = path_string_of_term r in
185 let meta_convertibility = Inference.meta_convertibility_eq equality in
188 let set = DiscriminationTree.find ps tree in
189 PosEqSet.exists (fun (p, e) -> meta_convertibility e) set
198 let head_of_term = function
199 | Cic.Appl (hd::tl) -> hd
204 let rec subterm_at_pos pos term =
210 (try subterm_at_pos pos (List.nth l index)
211 with Failure _ -> raise Not_found)
212 | _ -> raise Not_found
216 let rec after_t pos term =
219 | [] -> raise Not_found
220 | pos -> List.fold_right (fun i r -> if r = [] then [i+1] else i::r) pos []
223 ignore(subterm_at_pos pos' term ); pos'
227 (fun i (r, b) -> if b then (i::r, true) else (r, true)) pos ([], false)
233 let next_t pos term =
234 let t = subterm_at_pos pos term in
236 let _ = subterm_at_pos [1] t in
241 | pos -> after_t pos term
245 let retrieve_generalizations tree term =
246 let rec retrieve tree term pos =
248 | DiscriminationTree.Node (Some s, _) when pos = [] -> s
249 | DiscriminationTree.Node (_, map) ->
252 let hd_term = head_of_term (subterm_at_pos pos term) in
253 let n = PSMap.find hd_term map in
255 | DiscriminationTree.Node (Some s, _) -> s
256 | DiscriminationTree.Node (None, _) ->
257 let newpos = try next_t pos term with Not_found -> [] in
258 retrieve n term newpos
263 let n = PSMap.find (Cic.Implicit None) map in
264 let newpos = try after_t pos term with Not_found -> [-1] in
265 if newpos = [-1] then
267 | DiscriminationTree.Node (Some s, _) -> A.union s res
270 A.union res (retrieve n term newpos)
274 retrieve tree term []
278 let jump_list = function
279 | DiscriminationTree.Node (value, map) ->
282 | DiscriminationTree.Node (v, m) ->
288 let a = try Hashtbl.find arities k with Not_found -> 0 in
289 (get (n-1 + a) v) @ res) m []
293 let arity = try Hashtbl.find arities k with Not_found -> 0 in
299 let retrieve_unifiables tree term =
300 let rec retrieve tree term pos =
302 | DiscriminationTree.Node (Some s, _) when pos = [] -> s
303 | DiscriminationTree.Node (_, map) ->
305 try Some (subterm_at_pos pos term) with Not_found -> None
309 | Some (Cic.Meta _) ->
310 let newpos = try next_t pos term with Not_found -> [] in
311 let jl = jump_list tree in
313 (fun r s -> A.union r s)
315 (List.map (fun t -> retrieve t term newpos) jl)
319 let hd_term = head_of_term subterm in
320 let n = PSMap.find hd_term map in
322 | DiscriminationTree.Node (Some s, _) -> s
323 | DiscriminationTree.Node (None, _) ->
324 retrieve n term (next_t pos term)
329 let n = PSMap.find (Cic.Implicit None) map in
330 let newpos = try after_t pos term with Not_found -> [-1] in
331 if newpos = [-1] then
333 | DiscriminationTree.Node (Some s, _) -> A.union s res
336 A.union res (retrieve n term newpos)
340 retrieve tree term []