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 =
33 | Function | Constant of UriManager.uri
34 | Bound of int | Variable | Proposition | Datatype ;;
35 type path_string = path_string_elem list;;
38 (* needed by the retrieve_* functions, to know the arities of the
43 | Constant uri -> UriManager.name_of_uri uri
44 | Bound i -> string_of_int i
46 | Proposition -> "Prop"
49 let pppath l = String.concat "::" (List.map ppelem l) ;;
50 let elem_of_cic = function
51 | Cic.Meta _ -> Variable
52 | Cic.Lambda _ -> Function
53 | Cic.Rel i -> Bound i
54 | Cic.Sort (Cic.Prop) -> Proposition
55 | Cic.Sort _ -> Datatype
57 try Constant (CicUtil.uri_of_term term)
58 with Invalid_argument _ -> Variable (* HACK! *)
60 let path_string_of_term arities =
61 let set_arity arities k n =
62 (assert (k<>Variable || n=0);
63 (k,n)::(List.remove_assoc k arities))
65 let rec aux arities = function
66 | Cic.Appl ((hd::tl) as l) ->
68 set_arity arities (elem_of_cic hd) (List.length tl) in
70 (fun (arities,path) t ->
71 let arities,tpath = aux arities t in
74 | t -> arities, [elem_of_cic t]
78 let compare_elem e1 e2 =
80 | Constant u1,Constant u2 -> UriManager.compare u1 u2
81 | e1,e2 -> Pervasives.compare e1 e2
84 module OrderedPathStringElement = struct
85 type t = path_string_elem
86 let compare = compare_elem
89 module PSMap = Map.Make(OrderedPathStringElement);;
93 module DiscriminationTree = Trie.Make(PSMap);;
95 type t = A.t DiscriminationTree.t * (path_string_elem*int) list
96 let empty = DiscriminationTree.empty, [] ;;
98 let index (tree,arity) term info =
99 let arity,ps = path_string_of_term arity term in
101 try DiscriminationTree.find ps tree
102 with Not_found -> A.empty in
103 let tree = DiscriminationTree.add ps (A.add info ps_set) tree in
107 let remove_index (tree,arity) term info =
108 let arity,ps = path_string_of_term arity term in
110 let ps_set = A.remove info (DiscriminationTree.find ps tree) in
111 if A.is_empty ps_set then
112 DiscriminationTree.remove ps tree,arity
114 DiscriminationTree.add ps ps_set tree,arity
119 let in_index (tree,arity) term test =
120 let arity,ps = path_string_of_term arity term in
122 let ps_set = DiscriminationTree.find ps tree in
128 let head_of_term = function
129 | Cic.Appl (hd::tl) -> hd
133 let rec skip_prods = function
134 | Cic.Prod (_,_,t) -> skip_prods t
138 let rec subterm_at_pos pos term =
144 (try subterm_at_pos pos (List.nth l index)
145 with Failure _ -> raise Not_found)
146 | _ -> raise Not_found
150 let rec after_t pos term =
153 | [] -> raise Not_found
156 (fun i r -> if r = [] then [i+1] else i::r) pos []
159 ignore(subterm_at_pos pos' term ); pos'
163 (fun i (r, b) -> if b then (i::r, true) else (r, true))
170 let next_t pos term =
171 let t = subterm_at_pos pos term in
173 let _ = subterm_at_pos [1] t in
178 | pos -> after_t pos term
181 let retrieve_generalizations (tree,arity) term =
182 let term = skip_prods term in
183 let rec retrieve tree term pos =
185 | DiscriminationTree.Node (Some s, _) when pos = [] -> s
186 | DiscriminationTree.Node (_, map) ->
189 elem_of_cic (head_of_term (subterm_at_pos pos term))
191 if hd_term = Variable then A.empty else
193 let n = PSMap.find hd_term map in
195 | DiscriminationTree.Node (Some s, _) -> s
196 | DiscriminationTree.Node (None, _) ->
201 retrieve n term newpos
206 let n = PSMap.find Variable map in
207 let newpos = try after_t pos term with Not_found -> [-1] in
208 if newpos = [-1] then
210 | DiscriminationTree.Node (Some s, _) -> A.union s res
213 A.union res (retrieve n term newpos)
217 retrieve tree term []
221 let jump_list arities = function
222 | DiscriminationTree.Node (value, map) ->
225 | DiscriminationTree.Node (v, m) ->
232 try List.assoc k arities
235 (get (n-1 + a) v) @ res) m []
242 with Not_found -> 0 in
248 let retrieve_unifiables (tree,arities) term =
249 let term = skip_prods term in
250 let rec retrieve tree term pos =
252 | DiscriminationTree.Node (Some s, _) when pos = [] -> s
253 | DiscriminationTree.Node (_, map) ->
255 try Some (subterm_at_pos pos term) with Not_found -> None
259 | Some (Cic.Meta _) ->
260 let newpos = try next_t pos term with Not_found -> [] in
261 let jl = jump_list arities tree in
263 (fun r s -> A.union r s)
265 (List.map (fun t -> retrieve t term newpos) jl)
268 let hd_term = elem_of_cic (head_of_term subterm) in
269 if hd_term = Variable then
272 let n = PSMap.find hd_term map in
274 | DiscriminationTree.Node (Some s, _) -> s
275 | DiscriminationTree.Node (None, _) ->
276 retrieve n term (next_t pos term)
281 let n = PSMap.find Variable map in
284 with Not_found -> [-1]
286 if newpos = [-1] then
288 | DiscriminationTree.Node (Some s, _) ->
292 A.union res (retrieve n term newpos)
296 retrieve tree term []