2 type retrieval_mode = Matching | Unification;;
5 let print_candidates mode term res =
9 Printf.printf "| candidates Matching %s\n" (CicPp.ppterm term)
11 Printf.printf "| candidates Unification %s\n" (CicPp.ppterm term)
17 Printf.sprintf "| (%s, %s)" (Utils.string_of_pos p)
18 (Inference.string_of_equality e))
24 let indexing_retrieval_time = ref 0.;;
28 Path_indexing.PSTrie.empty
31 let index = Path_indexing.index
32 and remove_index = Path_indexing.remove_index
33 and in_index = Path_indexing.in_index;;
35 let get_candidates mode trie term =
36 let t1 = Unix.gettimeofday () in
40 | Matching -> Path_indexing.retrieve_generalizations trie term
41 | Unification -> Path_indexing.retrieve_unifiables trie term
42 (* Path_indexing.retrieve_all trie term *)
44 Path_indexing.PosEqSet.elements s
46 (* print_candidates mode term res; *)
47 let t2 = Unix.gettimeofday () in
48 indexing_retrieval_time := !indexing_retrieval_time +. (t2 -. t1);
55 Discrimination_tree.DiscriminationTree.empty
58 let index = Discrimination_tree.index
59 and remove_index = Discrimination_tree.remove_index
60 and in_index = Discrimination_tree.in_index;;
62 let get_candidates mode tree term =
66 | Matching -> Discrimination_tree.retrieve_generalizations tree term
67 | Unification -> Discrimination_tree.retrieve_unifiables tree term
69 Discrimination_tree.PosEqSet.elements s
71 (* print_candidates mode term res; *)
76 let match_unif_time_ok = ref 0.;;
77 let match_unif_time_no = ref 0.;;
80 let rec find_matches metasenv context ugraph lift_amount term =
82 let module U = Utils in
83 let module S = CicSubstitution in
84 let module M = CicMetaSubst in
85 let module HL = HelmLibraryObjects in
86 let cmp = !Utils.compare_terms in
87 let names = Utils.names_of_context context in
91 let pos, (proof, (ty, left, right, o), metas, args) = candidate in
92 let do_match c other eq_URI =
93 let subst', metasenv', ugraph' =
94 let t1 = Unix.gettimeofday () in
97 Inference.matching (metasenv @ metas) context
98 term (S.lift lift_amount c) ugraph in
99 let t2 = Unix.gettimeofday () in
100 match_unif_time_ok := !match_unif_time_ok +. (t2 -. t1);
103 let t2 = Unix.gettimeofday () in
104 match_unif_time_no := !match_unif_time_no +. (t2 -. t1);
107 Some (C.Rel (1 + lift_amount), subst', metasenv', ugraph',
110 let c, other, eq_URI =
111 if pos = Utils.Left then left, right, HL.Logic.eq_ind_URI
112 else right, left, HL.Logic.eq_ind_r_URI
114 if o <> U.Incomparable then
116 do_match c other eq_URI
118 find_matches metasenv context ugraph lift_amount term tl
120 let res = try do_match c other eq_URI with e -> None in
122 | Some (_, s, _, _, _) ->
123 let c' = M.apply_subst s c
124 and other' = M.apply_subst s other in
125 let order = cmp c' other' in
126 let names = U.names_of_context context in
130 find_matches metasenv context ugraph lift_amount term tl
132 find_matches metasenv context ugraph lift_amount term tl
136 let rec find_all_matches ?(unif_fun=CicUnification.fo_unif)
137 metasenv context ugraph lift_amount term =
138 let module C = Cic in
139 let module U = Utils in
140 let module S = CicSubstitution in
141 let module M = CicMetaSubst in
142 let module HL = HelmLibraryObjects in
143 let cmp = !Utils.compare_terms in
144 let names = Utils.names_of_context context in
148 let pos, (proof, (ty, left, right, o), metas, args) = candidate in
149 let do_match c other eq_URI =
150 let subst', metasenv', ugraph' =
151 let t1 = Unix.gettimeofday () in
154 unif_fun (metasenv @ metas) context
155 term (S.lift lift_amount c) ugraph in
156 let t2 = Unix.gettimeofday () in
157 match_unif_time_ok := !match_unif_time_ok +. (t2 -. t1);
160 let t2 = Unix.gettimeofday () in
161 match_unif_time_no := !match_unif_time_no +. (t2 -. t1);
164 (C.Rel (1 + lift_amount), subst', metasenv', ugraph',
167 let c, other, eq_URI =
168 if pos = Utils.Left then left, right, HL.Logic.eq_ind_URI
169 else right, left, HL.Logic.eq_ind_r_URI
171 if o <> U.Incomparable then
173 let res = do_match c other eq_URI in
174 res::(find_all_matches ~unif_fun metasenv context ugraph
177 find_all_matches ~unif_fun metasenv context ugraph
181 let res = do_match c other eq_URI in
184 let c' = M.apply_subst s c
185 and other' = M.apply_subst s other in
186 let order = cmp c' other' in
187 let names = U.names_of_context context in
188 if order <> U.Lt && order <> U.Le then
189 res::(find_all_matches ~unif_fun metasenv context ugraph
192 find_all_matches ~unif_fun metasenv context ugraph
195 find_all_matches ~unif_fun metasenv context ugraph
200 let subsumption env table target =
201 let _, (ty, tl, tr, _), tmetas, _ = target in
202 let metasenv, context, ugraph = env in
203 let metasenv = metasenv @ tmetas in
204 let samesubst subst subst' =
205 let tbl = Hashtbl.create (List.length subst) in
206 List.iter (fun (m, (c, t1, t2)) -> Hashtbl.add tbl m (c, t1, t2)) subst;
208 (fun (m, (c, t1, t2)) ->
210 let c', t1', t2' = Hashtbl.find tbl m in
211 if (c = c') && (t1 = t1') && (t2 = t2') then true
217 let subsaux left right =
218 let leftc = get_candidates Matching table left in
220 find_all_matches ~unif_fun:Inference.matching
221 metasenv context ugraph 0 left leftc
223 let ok what (_, subst, menv, ug, ((pos, (_, (_, l, r, o), _, _)), _)) =
225 let other = if pos = Utils.Left then r else l in
226 let subst', menv', ug' =
227 let t1 = Unix.gettimeofday () in
230 Inference.matching metasenv context what other ugraph in
231 let t2 = Unix.gettimeofday () in
232 match_unif_time_ok := !match_unif_time_ok +. (t2 -. t1);
235 let t2 = Unix.gettimeofday () in
236 match_unif_time_no := !match_unif_time_no +. (t2 -. t1);
239 samesubst subst subst'
243 let r = List.exists (ok right) leftr in
247 let rightc = get_candidates Matching table right in
249 find_all_matches ~unif_fun:Inference.matching
250 metasenv context ugraph 0 right rightc
252 List.exists (ok left) rightr
254 let res = subsaux tl tr in
256 Printf.printf "subsumption!:\ntarget: %s\n"
257 (Inference.string_of_equality ~env target);
264 let rec demodulate_term metasenv context ugraph table lift_amount term =
265 let module C = Cic in
266 let module S = CicSubstitution in
267 let module M = CicMetaSubst in
268 let module HL = HelmLibraryObjects in
269 let candidates = get_candidates Matching table term in
274 find_matches metasenv context ugraph lift_amount term candidates
285 (res, tl @ [S.lift 1 t])
288 demodulate_term metasenv context ugraph table
292 | None -> (None, tl @ [S.lift 1 t])
293 | Some (rel, _, _, _, _) -> (r, tl @ [rel]))
298 | Some (_, subst, menv, ug, eq_found) ->
299 Some (C.Appl ll, subst, menv, ug, eq_found)
301 | C.Prod (nn, s, t) ->
303 demodulate_term metasenv context ugraph table lift_amount s in (
307 demodulate_term metasenv
308 ((Some (nn, C.Decl s))::context) ugraph
309 table (lift_amount+1) t
313 | Some (t', subst, menv, ug, eq_found) ->
314 Some (C.Prod (nn, (S.lift 1 s), t'),
315 subst, menv, ug, eq_found)
317 | Some (s', subst, menv, ug, eq_found) ->
318 Some (C.Prod (nn, s', (S.lift 1 t)),
319 subst, menv, ug, eq_found)
326 let rec demodulation newmeta env table target =
327 let module C = Cic in
328 let module S = CicSubstitution in
329 let module M = CicMetaSubst in
330 let module HL = HelmLibraryObjects in
331 let metasenv, context, ugraph = env in
332 let proof, (eq_ty, left, right, order), metas, args = target in
333 let metasenv' = metasenv @ metas in
334 let build_newtarget is_left (t, subst, menv, ug, (eq_found, eq_URI)) =
335 let pos, (proof', (ty, what, other, _), menv', args') = eq_found in
336 let what, other = if pos = Utils.Left then what, other else other, what in
337 let newterm, newproof =
338 let bo = M.apply_subst subst (S.subst other t) in
340 C.Appl ([C.MutInd (HL.Logic.eq_URI, 0, []);
342 if is_left then [bo; S.lift 1 right] else [S.lift 1 left; bo])
344 let t' = C.Lambda (C.Anonymous, ty, bo'') in
346 M.apply_subst subst (C.Appl [C.Const (eq_URI, []); ty; what; t';
347 proof; other; proof'])
349 let left, right = if is_left then newterm, right else left, newterm in
351 (Inference.metas_of_term left) @ (Inference.metas_of_term right)
353 let newmetasenv = List.filter (fun (i, _, _) -> List.mem i m) metas
356 (function C.Meta (i, _) -> List.mem i m | _ -> assert false)
359 let ordering = !Utils.compare_terms left right in
360 newmeta, (newproof, (eq_ty, left, right, ordering), newmetasenv, newargs)
362 let res = demodulate_term metasenv' context ugraph table 0 left in
363 let build_identity (p, (t, l, r, o), m, a) =
365 | Utils.Gt -> (p, (t, r, r, Utils.Eq), m, a)
366 | _ -> (p, (t, l, l, Utils.Eq), m, a)
370 let newmeta, newtarget = build_newtarget true t in
371 if (Inference.is_identity (metasenv', context, ugraph) newtarget) ||
372 (Inference.meta_convertibility_eq target newtarget) then
375 if subsumption env table newtarget then
376 newmeta, build_identity newtarget
378 demodulation newmeta env table newtarget
380 let res = demodulate_term metasenv' context ugraph table 0 right in
383 let newmeta, newtarget = build_newtarget false t in
384 if (Inference.is_identity (metasenv', context, ugraph) newtarget) ||
385 (Inference.meta_convertibility_eq target newtarget) then
388 if subsumption env table newtarget then
389 newmeta, build_identity newtarget
391 demodulation newmeta env table newtarget
397 let rec betaexpand_term metasenv context ugraph table lift_amount term =
398 let module C = Cic in
399 let module S = CicSubstitution in
400 let module M = CicMetaSubst in
401 let module HL = HelmLibraryObjects in
402 let candidates = get_candidates Unification table term in
403 let res, lifted_term =
408 (fun arg (res, lifted_tl) ->
411 let arg_res, lifted_arg =
412 betaexpand_term metasenv context ugraph table
416 (fun (t, s, m, ug, eq_found) ->
417 (Some t)::lifted_tl, s, m, ug, eq_found)
422 (fun (l, s, m, ug, eq_found) ->
423 (Some lifted_arg)::l, s, m, ug, eq_found)
425 (Some lifted_arg)::lifted_tl)
428 (fun (r, s, m, ug, eq_found) ->
429 None::r, s, m, ug, eq_found) res,
435 (fun (l, s, m, ug, eq_found) ->
436 (C.Meta (i, l), s, m, ug, eq_found)) l'
438 e, C.Meta (i, lifted_l)
441 [], if m <= lift_amount then C.Rel m else C.Rel (m+1)
443 | C.Prod (nn, s, t) ->
445 betaexpand_term metasenv context ugraph table lift_amount s in
447 betaexpand_term metasenv ((Some (nn, C.Decl s))::context) ugraph
448 table (lift_amount+1) t in
451 (fun (t, s, m, ug, eq_found) ->
452 C.Prod (nn, t, lifted_t), s, m, ug, eq_found) l1
455 (fun (t, s, m, ug, eq_found) ->
456 C.Prod (nn, lifted_s, t), s, m, ug, eq_found) l2 in
457 l1' @ l2', C.Prod (nn, lifted_s, lifted_t)
462 (fun arg (res, lifted_tl) ->
463 let arg_res, lifted_arg =
464 betaexpand_term metasenv context ugraph table lift_amount arg
468 (fun (a, s, m, ug, eq_found) ->
469 a::lifted_tl, s, m, ug, eq_found)
474 (fun (r, s, m, ug, eq_found) ->
475 lifted_arg::r, s, m, ug, eq_found)
477 lifted_arg::lifted_tl)
481 (fun (l, s, m, ug, eq_found) -> (C.Appl l, s, m, ug, eq_found)) l',
484 | t -> [], (S.lift lift_amount t)
487 | C.Meta _ -> res, lifted_term
490 find_all_matches metasenv context ugraph lift_amount term candidates
496 let superposition_left (metasenv, context, ugraph) table target =
497 let module C = Cic in
498 let module S = CicSubstitution in
499 let module M = CicMetaSubst in
500 let module HL = HelmLibraryObjects in
501 let module CR = CicReduction in
502 let module U = Utils in
503 let proof, (eq_ty, left, right, ordering), _, _ = target in
505 let term = if ordering = U.Gt then left else right in
506 betaexpand_term metasenv context ugraph table 0 term
508 let build_new (bo, s, m, ug, (eq_found, eq_URI)) =
509 let pos, (proof', (ty, what, other, _), menv', args') = eq_found in
510 let what, other = if pos = Utils.Left then what, other else other, what in
511 let newgoal, newproof =
512 let bo' = M.apply_subst s (S.subst other bo) in
515 [C.MutInd (HL.Logic.eq_URI, 0, []);
517 if ordering = U.Gt then [bo'; S.lift 1 right]
518 else [S.lift 1 left; bo'])
520 let t' = C.Lambda (C.Anonymous, ty, bo'') in
523 (C.Appl [C.Const (eq_URI, []); ty; what; t';
524 proof; other; proof'])
527 if ordering = U.Gt then newgoal, right else left, newgoal in
528 let neworder = !Utils.compare_terms left right in
529 (newproof, (eq_ty, left, right, neworder), [], [])
531 List.map build_new expansions
535 let superposition_right newmeta (metasenv, context, ugraph) table target =
536 let module C = Cic in
537 let module S = CicSubstitution in
538 let module M = CicMetaSubst in
539 let module HL = HelmLibraryObjects in
540 let module CR = CicReduction in
541 let module U = Utils in
542 let eqproof, (eq_ty, left, right, ordering), newmetas, args = target in
543 let metasenv' = metasenv @ newmetas in
544 let maxmeta = ref newmeta in
547 | U.Gt -> fst (betaexpand_term metasenv' context ugraph table 0 left), []
548 | U.Lt -> [], fst (betaexpand_term metasenv' context ugraph table 0 right)
552 (fun (_, subst, _, _, _) ->
553 let subst = M.apply_subst subst in
554 let o = !Utils.compare_terms (subst l) (subst r) in
555 o <> U.Lt && o <> U.Le)
556 (fst (betaexpand_term metasenv' context ugraph table 0 l))
558 (res left right), (res right left)
560 let build_new ordering (bo, s, m, ug, (eq_found, eq_URI)) =
561 let pos, (proof', (ty, what, other, _), menv', args') = eq_found in
562 let what, other = if pos = Utils.Left then what, other else other, what in
563 let newgoal, newproof =
564 let bo' = M.apply_subst s (S.subst other bo) in
567 [C.MutInd (HL.Logic.eq_URI, 0, []); S.lift 1 eq_ty] @
568 if ordering = U.Gt then [bo'; S.lift 1 right]
569 else [S.lift 1 left; bo'])
571 let t' = C.Lambda (C.Anonymous, ty, bo'') in
574 (C.Appl [C.Const (eq_URI, []); ty; what; t';
575 eqproof; other; proof'])
577 let newmeta, newequality =
579 if ordering = U.Gt then newgoal, M.apply_subst s right
580 else M.apply_subst s left, newgoal in
581 let neworder = !Utils.compare_terms left right
582 and newmenv = newmetas @ menv'
583 and newargs = args @ args' in
584 let eq' = (newproof, (eq_ty, left, right, neworder), newmenv, newargs)
585 and env = (metasenv, context, ugraph) in
586 let newm, eq' = Inference.fix_metas !maxmeta eq' in
592 let new1 = List.map (build_new U.Gt) res1
593 and new2 = List.map (build_new U.Lt) res2 in
595 | _, (_, left, right, _), _, _ ->
596 not (fst (CR.are_convertible context left right ugraph))
599 (List.filter ok (new1 @ new2)))