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
62 let empty = DiscriminationTree.empty
65 module OrderedPosEquality = struct
66 type t = Utils.pos * Inference.equality
67 let compare = Pervasives.compare
70 module PosEqSet = Set.Make(OrderedPosEquality);;
72 let string_of_discrimination_tree tree =
73 let rec to_string level = function
74 | DiscriminationTree.Node (value, map) ->
78 (String.make (2 * level) ' ') ^
79 "{" ^ (String.concat "; "
82 "(" ^ (Utils.string_of_pos p) ^ ", " ^
83 (Inference.string_of_equality e) ^ ")")
84 (PosEqSet.elements v))) ^ "}"
91 let ks = CicPp.ppterm k in
92 let rs = to_string (level+1) v in
93 ((String.make (2 * level) ' ') ^ ks ^ "\n" ^ rs)::s)
102 let index tree term info =
103 let ps = path_string_of_term term in
105 try DiscriminationTree.find ps tree
106 with Not_found -> A.empty in
108 DiscriminationTree.add ps (A.add info ps_set) tree in
112 let index tree equality =
113 let _, _, (_, l, r, ordering), _, _ = equality in
114 let psl = path_string_of_term l
115 and psr = path_string_of_term r in
116 let index pos tree ps =
118 try DiscriminationTree.find ps tree with Not_found -> PosEqSet.empty in
120 DiscriminationTree.add ps (PosEqSet.add (pos, equality) ps_set) tree in
124 | Utils.Gt -> index Utils.Left tree psl
125 | Utils.Lt -> index Utils.Right tree psr
127 let tree = index Utils.Left tree psl in
128 index Utils.Right tree psr
132 let remove_index tree term info =
133 let ps = path_string_of_term term in
136 A.remove info (DiscriminationTree.find ps tree) in
137 if A.is_empty ps_set then
138 DiscriminationTree.remove ps tree
140 DiscriminationTree.add ps ps_set tree
145 let remove_index tree equality =
146 let _, _, (_, l, r, ordering), _, _ = equality in
147 let psl = path_string_of_term l
148 and psr = path_string_of_term r in
149 let remove_index pos tree ps =
152 PosEqSet.remove (pos, equality) (DiscriminationTree.find ps tree) in
153 if PosEqSet.is_empty ps_set then
154 DiscriminationTree.remove ps tree
156 DiscriminationTree.add ps ps_set tree
161 | Utils.Gt -> remove_index Utils.Left tree psl
162 | Utils.Lt -> remove_index Utils.Right tree psr
164 let tree = remove_index Utils.Left tree psl in
165 remove_index Utils.Right tree psr
170 let in_index tree term test =
171 let ps = path_string_of_term term in
173 let ps_set = DiscriminationTree.find ps tree in
179 let in_index tree equality =
180 let _, _, (_, l, r, ordering), _, _ = equality in
181 let psl = path_string_of_term l
182 and psr = path_string_of_term r in
183 let meta_convertibility = Inference.meta_convertibility_eq equality in
186 let set = DiscriminationTree.find ps tree in
187 PosEqSet.exists (fun (p, e) -> meta_convertibility e) set
196 let head_of_term = function
197 | Cic.Appl (hd::tl) -> hd
202 let rec subterm_at_pos pos term =
208 (try subterm_at_pos pos (List.nth l index)
209 with Failure _ -> raise Not_found)
210 | _ -> raise Not_found
214 let rec after_t pos term =
217 | [] -> raise Not_found
218 | pos -> List.fold_right (fun i r -> if r = [] then [i+1] else i::r) pos []
221 let t = subterm_at_pos pos' term in pos'
225 (fun i (r, b) -> if b then (i::r, true) else (r, true)) pos ([], false)
231 let next_t pos term =
232 let t = subterm_at_pos pos term in
234 let _ = subterm_at_pos [1] t in
239 | pos -> after_t pos term
243 let retrieve_generalizations tree term =
244 let rec retrieve tree term pos =
246 | DiscriminationTree.Node (Some s, _) when pos = [] -> s
247 | DiscriminationTree.Node (_, map) ->
250 let hd_term = head_of_term (subterm_at_pos pos term) in
251 let n = PSMap.find hd_term map in
253 | DiscriminationTree.Node (Some s, _) -> s
254 | DiscriminationTree.Node (None, _) ->
255 let newpos = try next_t pos term with Not_found -> [] in
256 retrieve n term newpos
261 let n = PSMap.find (Cic.Implicit None) map in
262 let newpos = try after_t pos term with Not_found -> [-1] in
263 if newpos = [-1] then
265 | DiscriminationTree.Node (Some s, _) -> A.union s res
268 A.union res (retrieve n term newpos)
272 retrieve tree term []
276 let jump_list = function
277 | DiscriminationTree.Node (value, map) ->
280 | DiscriminationTree.Node (v, m) ->
286 let a = try Hashtbl.find arities k with Not_found -> 0 in
287 (get (n-1 + a) v) @ res) m []
291 let arity = try Hashtbl.find arities k with Not_found -> 0 in
297 let retrieve_unifiables tree term =
298 let rec retrieve tree term pos =
300 | DiscriminationTree.Node (Some s, _) when pos = [] -> s
301 | DiscriminationTree.Node (_, map) ->
303 try Some (subterm_at_pos pos term) with Not_found -> None
307 | Some (Cic.Meta _) ->
308 let newpos = try next_t pos term with Not_found -> [] in
309 let jl = jump_list tree in
311 (fun r s -> A.union r s)
313 (List.map (fun t -> retrieve t term newpos) jl)
317 let hd_term = head_of_term subterm in
318 let n = PSMap.find hd_term map in
320 | DiscriminationTree.Node (Some s, _) -> s
321 | DiscriminationTree.Node (None, _) ->
322 retrieve n term (next_t pos term)
327 let n = PSMap.find (Cic.Implicit None) map in
328 let newpos = try after_t pos term with Not_found -> [-1] in
329 if newpos = [-1] then
331 | DiscriminationTree.Node (Some s, _) -> A.union s res
334 A.union res (retrieve n term newpos)
338 retrieve tree term []