1 type path_string_elem = Cic.term;;
2 type path_string = path_string_elem list;;
5 (* needed by the retrieve_* functions, to know the arities of the "functions" *)
6 let arities = Hashtbl.create 11;;
9 let rec path_string_of_term = function
10 | Cic.Meta _ -> [Cic.Implicit None]
11 | Cic.Appl ((hd::tl) as l) ->
12 if not (Hashtbl.mem arities hd) then
13 Hashtbl.add arities hd (List.length tl);
14 List.concat (List.map path_string_of_term l)
19 let string_of_path_string ps =
20 String.concat "." (List.map CicPp.ppterm ps)
24 module OrderedPathStringElement = struct
25 type t = path_string_elem
27 let compare = Pervasives.compare
30 module PSMap = Map.Make(OrderedPathStringElement);;
33 module OrderedPosEquality = struct
34 type t = Utils.pos * Inference.equality
36 let compare = Pervasives.compare
39 module PosEqSet = Set.Make(OrderedPosEquality);;
42 module DiscriminationTree = Trie.Make(PSMap);;
46 module DiscriminationTree = struct
47 type key = path_string
48 type t = Node of PosEqSet.t option * (t PSMap.t)
50 let empty = Node (None, PSMap.empty)
54 | [], Node (None, _) -> raise Not_found
55 | [], Node (Some v, _) -> v
56 | x::r, Node (_, m) -> find r (PSMap.find x m)
60 | [], Node (None, _) -> false
61 | [], Node (Some _, _) -> true
62 | x::r, Node (_, m) -> try mem r (PSMap.find x m) with Not_found -> false
65 let rec ins = function
66 | [], Node (_, m) -> Node (Some v, m)
67 | x::r, Node (v, m) ->
68 let t' = try PSMap.find x m with Not_found -> empty in
69 let t'' = ins (r, t') in
70 Node (v, PSMap.add x t'' m)
76 | [], Node (_, m) -> Node (None, m)
77 | x::r, Node (v, m) ->
79 let t' = remove r (PSMap.find x m) in
80 let m' = if t' = empty then PSMap.remove x m else PSMap.add x t' m in
85 let rec fold f t acc =
86 let rec traverse revp t acc = match t with
88 PSMap.fold (fun x -> traverse (x::revp)) m acc
90 f (List.rev revp) v (PSMap.fold (fun x -> traverse (x::revp)) m acc)
98 let string_of_discrimination_tree tree =
99 let rec to_string level = function
100 | DiscriminationTree.Node (value, map) ->
104 (String.make (2 * level) ' ') ^
105 "{" ^ (String.concat "; "
108 "(" ^ (Utils.string_of_pos p) ^ ", " ^
109 (Inference.string_of_equality e) ^ ")")
110 (PosEqSet.elements v))) ^ "}"
117 let ks = CicPp.ppterm k in
118 let rs = to_string (level+1) v in
119 ((String.make (2 * level) ' ') ^ ks ^ "\n" ^ rs)::s)
128 let index tree equality =
129 let _, (_, l, r, ordering), _, _ = equality in
130 let psl = path_string_of_term l
131 and psr = path_string_of_term r in
132 let index pos tree ps =
134 try DiscriminationTree.find ps tree with Not_found -> PosEqSet.empty in
136 DiscriminationTree.add ps (PosEqSet.add (pos, equality) ps_set) tree in
140 | Utils.Gt -> index Utils.Left tree psl
141 | Utils.Lt -> index Utils.Right tree psr
143 let tree = index Utils.Left tree psl in
144 index Utils.Right tree psr
148 let remove_index tree equality =
149 let _, (_, l, r, ordering), _, _ = equality in
150 let psl = path_string_of_term l
151 and psr = path_string_of_term r in
152 let remove_index pos tree ps =
155 PosEqSet.remove (pos, equality) (DiscriminationTree.find ps tree) in
156 if PosEqSet.is_empty ps_set then
157 DiscriminationTree.remove ps tree
159 DiscriminationTree.add ps ps_set tree
164 | Utils.Gt -> remove_index Utils.Left tree psl
165 | Utils.Lt -> remove_index Utils.Right tree psr
167 let tree = remove_index Utils.Left tree psl in
168 remove_index Utils.Right tree psr
172 let in_index tree equality =
173 let _, (_, l, r, ordering), _, _ = equality in
174 let psl = path_string_of_term l
175 and psr = path_string_of_term r in
176 let meta_convertibility = Inference.meta_convertibility_eq equality in
179 let set = DiscriminationTree.find ps tree in
180 PosEqSet.exists (fun (p, e) -> meta_convertibility e) set
188 let head_of_term = function
189 | Cic.Appl (hd::tl) -> hd
190 (* | Cic.Meta _ -> Cic.Implicit None *)
195 let rec subterm_at_pos pos term =
201 (try subterm_at_pos pos (List.nth l index) with _ -> raise Not_found)
202 | _ -> raise Not_found
206 let rec after_t pos term =
209 | [] -> raise Not_found
210 | pos -> List.fold_right (fun i r -> if r = [] then [i+1] else i::r) pos []
213 let t = subterm_at_pos pos' term in pos'
217 (fun i (r, b) -> if b then (i::r, true) else (r, true)) pos ([], false)
223 let next_t pos term =
224 let t = subterm_at_pos pos term in
226 let _ = subterm_at_pos [1] t in
231 | pos -> after_t pos term
235 let retrieve_generalizations tree term =
236 let rec retrieve tree term pos =
238 | DiscriminationTree.Node (Some s, _) when pos = [] -> s
239 | DiscriminationTree.Node (_, map) ->
242 let hd_term = head_of_term (subterm_at_pos pos term) in
243 let n = PSMap.find hd_term map in
245 | DiscriminationTree.Node (Some s, _) -> s
246 | DiscriminationTree.Node (None, _) ->
247 let newpos = try next_t pos term with Not_found -> [] in
248 retrieve n term newpos
253 let n = PSMap.find (Cic.Implicit None) map in
254 let newpos = try after_t pos term with _ -> [-1] in
255 if newpos = [-1] then
257 | DiscriminationTree.Node (Some s, _) -> PosEqSet.union s res
260 PosEqSet.union res (retrieve n term newpos)
264 retrieve tree term []
268 let jump_list = function
269 | DiscriminationTree.Node (value, map) ->
272 | DiscriminationTree.Node (v, m) ->
278 let a = try Hashtbl.find arities k with Not_found -> 0 in
279 (get (n-1 + a) v) @ res) m []
283 let arity = try Hashtbl.find arities k with Not_found -> 0 in
289 let retrieve_unifiables tree term =
290 let rec retrieve tree term pos =
292 | DiscriminationTree.Node (Some s, _) when pos = [] -> s
293 | DiscriminationTree.Node (_, map) ->
295 try Some (subterm_at_pos pos term) with Not_found -> None
298 | None -> PosEqSet.empty
299 | Some (Cic.Meta _) ->
300 let newpos = try next_t pos term with Not_found -> [] in
301 let jl = jump_list tree in
303 (fun r s -> PosEqSet.union r s)
305 (List.map (fun t -> retrieve t term newpos) jl)
309 let hd_term = head_of_term subterm in
310 let n = PSMap.find hd_term map in
312 | DiscriminationTree.Node (Some s, _) -> s
313 | DiscriminationTree.Node (None, _) ->
314 retrieve n term (next_t pos term)
319 let n = PSMap.find (Cic.Implicit None) map in
320 let newpos = try after_t pos term with _ -> [-1] in
321 if newpos = [-1] then
323 | DiscriminationTree.Node (Some s, _) -> PosEqSet.union s res
326 PosEqSet.union res (retrieve n term newpos)
330 retrieve tree term []