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 type path_string_elem = Cic.term;;
27 type path_string = path_string_elem list;;
30 (* needed by the retrieve_* functions, to know the arities of the "functions" *)
31 let arities = Hashtbl.create 11;;
34 let rec path_string_of_term = function
35 | Cic.Meta _ -> [Cic.Implicit None]
36 | Cic.Appl ((hd::tl) as l) ->
37 if not (Hashtbl.mem arities hd) then
38 Hashtbl.add arities hd (List.length tl);
39 List.concat (List.map path_string_of_term l)
44 let string_of_path_string ps =
45 String.concat "." (List.map CicPp.ppterm ps)
49 module OrderedPathStringElement = struct
50 type t = path_string_elem
52 let compare = Pervasives.compare
55 module PSMap = Map.Make(OrderedPathStringElement);;
58 module OrderedPosEquality = struct
59 type t = Utils.pos * Inference.equality
61 let compare = Pervasives.compare
64 module PosEqSet = Set.Make(OrderedPosEquality);;
67 module DiscriminationTree = Trie.Make(PSMap);;
70 let string_of_discrimination_tree tree =
71 let rec to_string level = function
72 | DiscriminationTree.Node (value, map) ->
76 (String.make (2 * level) ' ') ^
77 "{" ^ (String.concat "; "
80 "(" ^ (Utils.string_of_pos p) ^ ", " ^
81 (Inference.string_of_equality e) ^ ")")
82 (PosEqSet.elements v))) ^ "}"
89 let ks = CicPp.ppterm k in
90 let rs = to_string (level+1) v in
91 ((String.make (2 * level) ' ') ^ ks ^ "\n" ^ rs)::s)
100 let index tree equality =
101 let _, _, (_, l, r, ordering), _, _ = equality in
102 let psl = path_string_of_term l
103 and psr = path_string_of_term r in
104 let index pos tree ps =
106 try DiscriminationTree.find ps tree with Not_found -> PosEqSet.empty in
108 DiscriminationTree.add ps (PosEqSet.add (pos, equality) ps_set) tree in
112 | Utils.Gt -> index Utils.Left tree psl
113 | Utils.Lt -> index Utils.Right tree psr
115 let tree = index Utils.Left tree psl in
116 index Utils.Right tree psr
120 let remove_index tree equality =
121 let _, _, (_, l, r, ordering), _, _ = equality in
122 let psl = path_string_of_term l
123 and psr = path_string_of_term r in
124 let remove_index pos tree ps =
127 PosEqSet.remove (pos, equality) (DiscriminationTree.find ps tree) in
128 if PosEqSet.is_empty ps_set then
129 DiscriminationTree.remove ps tree
131 DiscriminationTree.add ps ps_set tree
136 | Utils.Gt -> remove_index Utils.Left tree psl
137 | Utils.Lt -> remove_index Utils.Right tree psr
139 let tree = remove_index Utils.Left tree psl in
140 remove_index Utils.Right tree psr
144 let in_index tree equality =
145 let _, _, (_, l, r, ordering), _, _ = equality in
146 let psl = path_string_of_term l
147 and psr = path_string_of_term r in
148 let meta_convertibility = Inference.meta_convertibility_eq equality in
151 let set = DiscriminationTree.find ps tree in
152 PosEqSet.exists (fun (p, e) -> meta_convertibility e) set
160 let head_of_term = function
161 | Cic.Appl (hd::tl) -> hd
166 let rec subterm_at_pos pos term =
172 (try subterm_at_pos pos (List.nth l index)
173 with Failure _ -> raise Not_found)
174 | _ -> raise Not_found
178 let rec after_t pos term =
181 | [] -> raise Not_found
182 | pos -> List.fold_right (fun i r -> if r = [] then [i+1] else i::r) pos []
185 let t = subterm_at_pos pos' term in pos'
189 (fun i (r, b) -> if b then (i::r, true) else (r, true)) pos ([], false)
195 let next_t pos term =
196 let t = subterm_at_pos pos term in
198 let _ = subterm_at_pos [1] t in
203 | pos -> after_t pos term
207 let retrieve_generalizations tree term =
208 let rec retrieve tree term pos =
210 | DiscriminationTree.Node (Some s, _) when pos = [] -> s
211 | DiscriminationTree.Node (_, map) ->
214 let hd_term = head_of_term (subterm_at_pos pos term) in
215 let n = PSMap.find hd_term map in
217 | DiscriminationTree.Node (Some s, _) -> s
218 | DiscriminationTree.Node (None, _) ->
219 let newpos = try next_t pos term with Not_found -> [] in
220 retrieve n term newpos
225 let n = PSMap.find (Cic.Implicit None) map in
226 let newpos = try after_t pos term with Not_found -> [-1] in
227 if newpos = [-1] then
229 | DiscriminationTree.Node (Some s, _) -> PosEqSet.union s res
232 PosEqSet.union res (retrieve n term newpos)
236 retrieve tree term []
240 let jump_list = function
241 | DiscriminationTree.Node (value, map) ->
244 | DiscriminationTree.Node (v, m) ->
250 let a = try Hashtbl.find arities k with Not_found -> 0 in
251 (get (n-1 + a) v) @ res) m []
255 let arity = try Hashtbl.find arities k with Not_found -> 0 in
261 let retrieve_unifiables tree term =
262 let rec retrieve tree term pos =
264 | DiscriminationTree.Node (Some s, _) when pos = [] -> s
265 | DiscriminationTree.Node (_, map) ->
267 try Some (subterm_at_pos pos term) with Not_found -> None
270 | None -> PosEqSet.empty
271 | Some (Cic.Meta _) ->
272 let newpos = try next_t pos term with Not_found -> [] in
273 let jl = jump_list tree in
275 (fun r s -> PosEqSet.union r s)
277 (List.map (fun t -> retrieve t term newpos) jl)
281 let hd_term = head_of_term subterm in
282 let n = PSMap.find hd_term map in
284 | DiscriminationTree.Node (Some s, _) -> s
285 | DiscriminationTree.Node (None, _) ->
286 retrieve n term (next_t pos term)
291 let n = PSMap.find (Cic.Implicit None) map in
292 let newpos = try after_t pos term with Not_found -> [-1] in
293 if newpos = [-1] then
295 | DiscriminationTree.Node (Some s, _) -> PosEqSet.union s res
298 PosEqSet.union res (retrieve n term newpos)
302 retrieve tree term []