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)
97 let string_of_discrimination_tree tree =
98 let rec to_string level = function
99 | DiscriminationTree.Node (value, map) ->
103 (String.make (2 * level) ' ') ^
104 "{" ^ (String.concat "; "
107 "(" ^ (Utils.string_of_pos p) ^ ", " ^
108 (Inference.string_of_equality e) ^ ")")
109 (PosEqSet.elements v))) ^ "}"
116 let ks = CicPp.ppterm k in
117 let rs = to_string (level+1) v in
118 ((String.make (2 * level) ' ') ^ ks ^ "\n" ^ rs)::s)
127 let index tree equality =
128 let _, (_, l, r, ordering), _, _ = equality in
129 let psl = path_string_of_term l
130 and psr = path_string_of_term r in
131 let index pos tree ps =
133 try DiscriminationTree.find ps tree with Not_found -> PosEqSet.empty in
135 DiscriminationTree.add ps (PosEqSet.add (pos, equality) ps_set) tree in
139 | Utils.Gt -> index Utils.Left tree psl
140 | Utils.Lt -> index Utils.Right tree psr
142 let tree = index Utils.Left tree psl in
143 index Utils.Right tree psr
147 let remove_index tree equality =
148 let _, (_, l, r, ordering), _, _ = equality in
149 let psl = path_string_of_term l
150 and psr = path_string_of_term r in
151 let remove_index pos tree ps =
154 PosEqSet.remove (pos, equality) (DiscriminationTree.find ps tree) in
155 if PosEqSet.is_empty ps_set then
156 DiscriminationTree.remove ps tree
158 DiscriminationTree.add ps ps_set tree
163 | Utils.Gt -> remove_index Utils.Left tree psl
164 | Utils.Lt -> remove_index Utils.Right tree psr
166 let tree = remove_index Utils.Left tree psl in
167 remove_index Utils.Right tree psr
171 let in_index tree equality =
172 let _, (_, l, r, ordering), _, _ = equality in
173 let psl = path_string_of_term l
174 and psr = path_string_of_term r in
175 let meta_convertibility = Inference.meta_convertibility_eq equality in
178 let set = DiscriminationTree.find ps tree in
179 PosEqSet.exists (fun (p, e) -> meta_convertibility e) set
187 let head_of_term = function
188 | Cic.Appl (hd::tl) -> hd
189 (* | Cic.Meta _ -> Cic.Implicit None *)
194 let rec subterm_at_pos pos term =
200 (try subterm_at_pos pos (List.nth l index) with _ -> raise Not_found)
201 | _ -> raise Not_found
205 let rec after_t pos term =
208 | [] -> raise Not_found
209 | pos -> List.fold_right (fun i r -> if r = [] then [i+1] else i::r) pos []
212 let t = subterm_at_pos pos' term in pos'
216 (fun i (r, b) -> if b then (i::r, true) else (r, true)) pos ([], false)
222 let next_t pos term =
223 let t = subterm_at_pos pos term in
225 let _ = subterm_at_pos [1] t in
230 | pos -> after_t pos term
234 let retrieve_generalizations tree term =
235 let rec retrieve tree term pos =
237 | DiscriminationTree.Node (Some s, _) when pos = [] -> s
238 | DiscriminationTree.Node (_, map) ->
241 let hd_term = head_of_term (subterm_at_pos pos term) in
242 let n = PSMap.find hd_term map in
244 | DiscriminationTree.Node (Some s, _) -> s
245 | DiscriminationTree.Node (None, _) ->
246 let newpos = try next_t pos term with Not_found -> [] in
247 retrieve n term newpos
252 let n = PSMap.find (Cic.Implicit None) map in
253 let newpos = try after_t pos term with _ -> [-1] in
254 if newpos = [-1] then
256 | DiscriminationTree.Node (Some s, _) -> PosEqSet.union s res
259 PosEqSet.union res (retrieve n term newpos)
263 retrieve tree term []
267 let jump_list = function
268 | DiscriminationTree.Node (value, map) ->
271 | DiscriminationTree.Node (v, m) ->
277 let a = try Hashtbl.find arities k with Not_found -> 0 in
278 (get (n-1 + a) v) @ res) m []
282 let arity = try Hashtbl.find arities k with Not_found -> 0 in
288 let retrieve_unifiables tree term =
289 let rec retrieve tree term pos =
291 | DiscriminationTree.Node (Some s, _) when pos = [] -> s
292 | DiscriminationTree.Node (_, map) ->
294 try Some (subterm_at_pos pos term) with Not_found -> None
297 | None -> PosEqSet.empty
298 | Some (Cic.Meta _) ->
299 let newpos = try next_t pos term with Not_found -> [] in
300 let jl = jump_list tree in
302 (fun r s -> PosEqSet.union r s)
304 (List.map (fun t -> retrieve t term newpos) jl)
308 let hd_term = head_of_term subterm in
309 let n = PSMap.find hd_term map in
311 | DiscriminationTree.Node (Some s, _) -> s
312 | DiscriminationTree.Node (None, _) ->
313 retrieve n term (next_t pos term)
318 let n = PSMap.find (Cic.Implicit None) map in
319 let newpos = try after_t pos term with _ -> [-1] in
320 if newpos = [-1] then
322 | DiscriminationTree.Node (Some s, _) -> PosEqSet.union s res
325 PosEqSet.union res (retrieve n term newpos)
329 retrieve tree term []