2 ||M|| This file is part of HELM, an Hypertextual, Electronic
3 ||A|| Library of Mathematics, developed at the Computer Science
4 ||T|| Department, University of Bologna, Italy.
6 ||T|| HELM is free software; you can redistribute it and/or
7 ||A|| modify it under the terms of the GNU General Public License
8 \ / version 2 or (at your option) any later version.
9 \ / This software is distributed as is, NO WARRANTY.
10 V_______________________________________________________________ *)
14 module DiscriminationTreeIndexing =
18 type path_string_elem =
19 | Constant of NUri.uri
20 | Bound of int | Variable | Proposition | Datatype | Dead;;
21 type path_string = path_string_elem list;;
24 (* needed by the retrieve_* functions, to know the arities of the
28 | Constant uri -> NUri.name_of_uri uri
29 | Bound i -> string_of_int i
31 | Proposition -> "Prop"
35 let pppath l = String.concat "::" (List.map ppelem l) ;;
36 let elem_of_cic = function
37 | NCic.Meta _ | NCic.Implicit _ -> Variable
38 | NCic.Rel i -> Bound i
39 | NCic.Sort (NCic.Prop) -> Proposition
40 | NCic.Sort _ -> Datatype
41 | NCic.Const (NReference.Ref (u,_)) -> Constant u
43 assert false (* should not happen *)
44 | NCic.LetIn _ | NCic.Lambda _ | NCic.Prod _ | NCic.Match _ ->
45 prerr_endline "FIXME: the discrimination tree receives an invalid term";
47 (* assert false universe.ml removes these *)
49 let path_string_of_term arities =
50 let set_arity arities k n =
51 (assert (k<>Variable || n=0);
52 if k = Dead then arities else (k,n)::(List.remove_assoc k arities))
54 let rec aux arities = function
55 | NCic.Appl ((hd::tl) as l) ->
57 set_arity arities (elem_of_cic hd) (List.length tl) in
59 (fun (arities,path) t ->
60 let arities,tpath = aux arities t in
63 | t -> arities, [elem_of_cic t]
67 let compare_elem e1 e2 =
69 | Constant u1,Constant u2 -> assert false (*NUri.compare u1 u2*)
70 | e1,e2 -> Pervasives.compare e1 e2
73 module OrderedPathStringElement = struct
74 type t = path_string_elem
75 let compare = compare_elem
78 module PSMap = Map.Make(OrderedPathStringElement);;
82 module DiscriminationTree = Trie.Make(PSMap);;
84 type t = A.t DiscriminationTree.t * (path_string_elem*int) list
85 let empty = DiscriminationTree.empty, [] ;;
88 DiscriminationTree.iter (fun _ x -> f x) dt
91 let index (tree,arity) term info =
92 let arity,ps = path_string_of_term arity term in
94 try DiscriminationTree.find ps tree
95 with Not_found -> A.empty in
96 let tree = DiscriminationTree.add ps (A.add info ps_set) tree in
100 let remove_index (tree,arity) term info =
101 let arity,ps = path_string_of_term arity term in
103 let ps_set = A.remove info (DiscriminationTree.find ps tree) in
104 if A.is_empty ps_set then
105 DiscriminationTree.remove ps tree,arity
107 DiscriminationTree.add ps ps_set tree,arity
112 let in_index (tree,arity) term test =
113 let arity,ps = path_string_of_term arity term in
115 let ps_set = DiscriminationTree.find ps tree in
121 let head_of_term = function
122 | NCic.Appl (hd::tl) -> hd
126 let rec skip_prods = function
127 | NCic.Prod (_,_,t) -> skip_prods t
131 let rec subterm_at_pos pos term =
137 (try subterm_at_pos pos (List.nth l index)
138 with Failure _ -> raise Not_found)
139 | _ -> raise Not_found
143 let rec after_t pos term =
146 | [] -> raise Not_found
149 (fun i r -> if r = [] then [i+1] else i::r) pos []
152 ignore(subterm_at_pos pos' term ); pos'
156 (fun i (r, b) -> if b then (i::r, true) else (r, true))
163 let next_t pos term =
164 let t = subterm_at_pos pos term in
166 let _ = subterm_at_pos [1] t in
171 | pos -> after_t pos term
174 let retrieve_generalizations (tree,arity) term =
175 let term = skip_prods term in
176 let rec retrieve tree term pos =
178 | DiscriminationTree.Node (Some s, _) when pos = [] -> s
179 | DiscriminationTree.Node (_, map) ->
182 elem_of_cic (head_of_term (subterm_at_pos pos term))
184 if hd_term = Variable then A.empty else
186 let n = PSMap.find hd_term map in
188 | DiscriminationTree.Node (Some s, _) -> s
189 | DiscriminationTree.Node (None, _) ->
194 retrieve n term newpos
199 let n = PSMap.find Variable map in
200 let newpos = try after_t pos term with Not_found -> [-1] in
201 if newpos = [-1] then
203 | DiscriminationTree.Node (Some s, _) -> A.union s res
206 A.union res (retrieve n term newpos)
210 retrieve tree term []
214 let jump_list arities = function
215 | DiscriminationTree.Node (value, map) ->
218 | DiscriminationTree.Node (v, m) ->
225 try List.assoc k arities
228 (get (n-1 + a) v) @ res) m []
235 with Not_found -> 0 in
241 let retrieve_unifiables (tree,arities) term =
242 let term = skip_prods term in
243 let rec retrieve tree term pos =
245 | DiscriminationTree.Node (Some s, _) when pos = [] -> s
246 | DiscriminationTree.Node (_, map) ->
248 try Some (subterm_at_pos pos term) with Not_found -> None
252 | Some (NCic.Meta _) ->
253 let newpos = try next_t pos term with Not_found -> [] in
254 let jl = jump_list arities tree in
256 (fun r s -> A.union r s)
258 (List.map (fun t -> retrieve t term newpos) jl)
261 let hd_term = elem_of_cic (head_of_term subterm) in
262 if hd_term = Variable then
265 let n = PSMap.find hd_term map in
267 | DiscriminationTree.Node (Some s, _) -> s
268 | DiscriminationTree.Node (None, _) ->
269 retrieve n term (next_t pos term)
274 let n = PSMap.find Variable map in
277 with Not_found -> [-1]
279 if newpos = [-1] then
281 | DiscriminationTree.Node (Some s, _) ->
285 A.union res (retrieve n term newpos)
289 retrieve tree term []