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/.
26 module DiscriminationTreeIndexing =
30 type path_string_elem = Cic.term;;
31 type path_string = path_string_elem list;;
34 (* needed by the retrieve_* functions, to know the arities of the "functions" *)
36 let arities = Hashtbl.create 11;;
39 let rec path_string_of_term = function
40 | Cic.Meta _ -> [Cic.Implicit None]
41 | Cic.Appl ((hd::tl) as l) ->
42 if not (Hashtbl.mem arities hd) then
43 Hashtbl.add arities hd (List.length tl);
44 List.concat (List.map path_string_of_term l)
49 module OrderedPathStringElement = struct
50 type t = path_string_elem
52 let compare = Pervasives.compare
55 module PSMap = Map.Make(OrderedPathStringElement);;
59 module DiscriminationTree = Trie.Make(PSMap);;
61 type t = A.t DiscriminationTree.t
63 let empty = DiscriminationTree.empty
66 module OrderedPosEquality = struct
67 type t = Utils.pos * Inference.equality
68 let compare = Pervasives.compare
71 module PosEqSet = Set.Make(OrderedPosEquality);;
73 let string_of_discrimination_tree tree =
74 let rec to_string level = function
75 | DiscriminationTree.Node (value, map) ->
79 (String.make (2 * level) ' ') ^
80 "{" ^ (String.concat "; "
83 "(" ^ (Utils.string_of_pos p) ^ ", " ^
84 (Inference.string_of_equality e) ^ ")")
85 (PosEqSet.elements v))) ^ "}"
92 let ks = CicPp.ppterm k in
93 let rs = to_string (level+1) v in
94 ((String.make (2 * level) ' ') ^ ks ^ "\n" ^ rs)::s)
103 let index tree term info =
104 let ps = path_string_of_term term in
106 try DiscriminationTree.find ps tree
107 with Not_found -> A.empty in
109 DiscriminationTree.add ps (A.add info ps_set) tree in
113 let index tree equality =
114 let _, _, (_, l, r, ordering), _, _ = equality in
115 let psl = path_string_of_term l
116 and psr = path_string_of_term r in
117 let index pos tree ps =
119 try DiscriminationTree.find ps tree with Not_found -> PosEqSet.empty in
121 DiscriminationTree.add ps (PosEqSet.add (pos, equality) ps_set) tree in
125 | Utils.Gt -> index Utils.Left tree psl
126 | Utils.Lt -> index Utils.Right tree psr
128 let tree = index Utils.Left tree psl in
129 index Utils.Right tree psr
133 let remove_index tree term info =
134 let ps = path_string_of_term term in
137 A.remove info (DiscriminationTree.find ps tree) in
138 if A.is_empty ps_set then
139 DiscriminationTree.remove ps tree
141 DiscriminationTree.add ps ps_set tree
146 let remove_index tree equality =
147 let _, _, (_, l, r, ordering), _, _ = equality in
148 let psl = path_string_of_term l
149 and psr = path_string_of_term r in
150 let remove_index pos tree ps =
153 PosEqSet.remove (pos, equality) (DiscriminationTree.find ps tree) in
154 if PosEqSet.is_empty ps_set then
155 DiscriminationTree.remove ps tree
157 DiscriminationTree.add ps ps_set tree
162 | Utils.Gt -> remove_index Utils.Left tree psl
163 | Utils.Lt -> remove_index Utils.Right tree psr
165 let tree = remove_index Utils.Left tree psl in
166 remove_index Utils.Right tree psr
171 let in_index tree term test =
172 let ps = path_string_of_term term in
174 let ps_set = DiscriminationTree.find ps tree in
180 let in_index tree equality =
181 let _, _, (_, l, r, ordering), _, _ = equality in
182 let psl = path_string_of_term l
183 and psr = path_string_of_term r in
184 let meta_convertibility = Inference.meta_convertibility_eq equality in
187 let set = DiscriminationTree.find ps tree in
188 PosEqSet.exists (fun (p, e) -> meta_convertibility e) set
197 let head_of_term = function
198 | Cic.Appl (hd::tl) -> hd
203 let rec subterm_at_pos pos term =
209 (try subterm_at_pos pos (List.nth l index)
210 with Failure _ -> raise Not_found)
211 | _ -> raise Not_found
215 let rec after_t pos term =
218 | [] -> raise Not_found
219 | pos -> List.fold_right (fun i r -> if r = [] then [i+1] else i::r) pos []
222 let t = subterm_at_pos pos' term in pos'
226 (fun i (r, b) -> if b then (i::r, true) else (r, true)) pos ([], false)
232 let next_t pos term =
233 let t = subterm_at_pos pos term in
235 let _ = subterm_at_pos [1] t in
240 | pos -> after_t pos term
244 let retrieve_generalizations tree term =
245 let rec retrieve tree term pos =
247 | DiscriminationTree.Node (Some s, _) when pos = [] -> s
248 | DiscriminationTree.Node (_, map) ->
251 let hd_term = head_of_term (subterm_at_pos pos term) in
252 let n = PSMap.find hd_term map in
254 | DiscriminationTree.Node (Some s, _) -> s
255 | DiscriminationTree.Node (None, _) ->
256 let newpos = try next_t pos term with Not_found -> [] in
257 retrieve n term newpos
262 let n = PSMap.find (Cic.Implicit None) map in
263 let newpos = try after_t pos term with Not_found -> [-1] in
264 if newpos = [-1] then
266 | DiscriminationTree.Node (Some s, _) -> A.union s res
269 A.union res (retrieve n term newpos)
273 retrieve tree term []
277 let jump_list = function
278 | DiscriminationTree.Node (value, map) ->
281 | DiscriminationTree.Node (v, m) ->
287 let a = try Hashtbl.find arities k with Not_found -> 0 in
288 (get (n-1 + a) v) @ res) m []
292 let arity = try Hashtbl.find arities k with Not_found -> 0 in
298 let retrieve_unifiables tree term =
299 let rec retrieve tree term pos =
301 | DiscriminationTree.Node (Some s, _) when pos = [] -> s
302 | DiscriminationTree.Node (_, map) ->
304 try Some (subterm_at_pos pos term) with Not_found -> None
308 | Some (Cic.Meta _) ->
309 let newpos = try next_t pos term with Not_found -> [] in
310 let jl = jump_list tree in
312 (fun r s -> A.union r s)
314 (List.map (fun t -> retrieve t term newpos) jl)
318 let hd_term = head_of_term subterm in
319 let n = PSMap.find hd_term map in
321 | DiscriminationTree.Node (Some s, _) -> s
322 | DiscriminationTree.Node (None, _) ->
323 retrieve n term (next_t pos term)
328 let n = PSMap.find (Cic.Implicit None) map in
329 let newpos = try after_t pos term with Not_found -> [-1] in
330 if newpos = [-1] then
332 | DiscriminationTree.Node (Some s, _) -> A.union s res
335 A.union res (retrieve n term newpos)
339 retrieve tree term []