2 type retrieval_mode = Matching | Unification;;
5 let print_candidates mode term res =
12 Printf.printf "| candidates Matching %s\n" (CicPp.ppterm term)
14 Printf.printf "| candidates Unification %s\n" (CicPp.ppterm term)
20 Printf.sprintf "| (%s, %s)" (Utils.string_of_pos p)
21 (Inference.string_of_equality e))
29 Path_indexing.PSTrie.empty
32 let index = Path_indexing.index
33 and remove_index = Path_indexing.remove_index
34 and in_index = Path_indexing.in_index;;
36 let get_candidates mode trie term =
40 | Matching -> Path_indexing.retrieve_generalizations trie term
41 | Unification -> Path_indexing.retrieve_unifiables trie term
43 Path_indexing.PosEqSet.elements s
45 print_candidates mode term res;
52 Discrimination_tree.DiscriminationTree.empty
55 let index = Discrimination_tree.index
56 and remove_index = Discrimination_tree.remove_index
57 and in_index = Discrimination_tree.in_index;;
59 let get_candidates mode tree term =
63 | Matching -> Discrimination_tree.retrieve_generalizations tree term
64 | Unification -> Discrimination_tree.retrieve_unifiables tree term
66 Discrimination_tree.PosEqSet.elements s
68 (* print_candidates mode term res; *)
73 let rec find_matches metasenv context ugraph lift_amount term =
75 let module U = Utils in
76 let module S = CicSubstitution in
77 let module M = CicMetaSubst in
78 let module HL = HelmLibraryObjects in
79 let cmp = !Utils.compare_terms in
80 let names = Utils.names_of_context context in
84 let pos, (proof, (ty, left, right, o), metas, args) = candidate in
85 let do_match c other eq_URI =
86 let subst', metasenv', ugraph' =
87 Inference.matching (metasenv @ metas) context
88 term (S.lift lift_amount c) ugraph
90 Some (C.Rel (1 + lift_amount), subst', metasenv', ugraph',
93 let c, other, eq_URI =
94 if pos = Utils.Left then left, right, HL.Logic.eq_ind_URI
95 else right, left, HL.Logic.eq_ind_r_URI
97 if o <> U.Incomparable then
99 do_match c other eq_URI
101 find_matches metasenv context ugraph lift_amount term tl
103 let res = try do_match c other eq_URI with e -> None in
105 | Some (_, s, _, _, _) ->
106 let c' = M.apply_subst s c
107 and other' = M.apply_subst s other in
108 let order = cmp c' other' in
109 let names = U.names_of_context context in
113 find_matches metasenv context ugraph lift_amount term tl
115 find_matches metasenv context ugraph lift_amount term tl
119 let rec find_all_matches ?(unif_fun=CicUnification.fo_unif)
120 metasenv context ugraph lift_amount term =
121 let module C = Cic in
122 let module U = Utils in
123 let module S = CicSubstitution in
124 let module M = CicMetaSubst in
125 let module HL = HelmLibraryObjects in
126 let cmp = !Utils.compare_terms in
127 let names = Utils.names_of_context context in
131 let pos, (proof, (ty, left, right, o), metas, args) = candidate in
132 let do_match c other eq_URI =
133 let subst', metasenv', ugraph' =
134 unif_fun (metasenv @ metas) context
135 term (S.lift lift_amount c) ugraph
137 (C.Rel (1 + lift_amount), subst', metasenv', ugraph',
140 let c, other, eq_URI =
141 if pos = Utils.Left then left, right, HL.Logic.eq_ind_URI
142 else right, left, HL.Logic.eq_ind_r_URI
144 if o <> U.Incomparable then
146 let res = do_match c other eq_URI in
147 res::(find_all_matches ~unif_fun metasenv context ugraph
150 find_all_matches ~unif_fun metasenv context ugraph
154 let res = do_match c other eq_URI in
157 let c' = M.apply_subst s c
158 and other' = M.apply_subst s other in
159 let order = cmp c' other' in
160 let names = U.names_of_context context in
161 if order <> U.Lt && order <> U.Le then
162 res::(find_all_matches ~unif_fun metasenv context ugraph
165 find_all_matches ~unif_fun metasenv context ugraph
168 find_all_matches ~unif_fun metasenv context ugraph
173 let subsumption env table target =
174 let _, (ty, tl, tr, _), tmetas, _ = target in
175 let metasenv, context, ugraph = env in
176 let metasenv = metasenv @ tmetas in
177 let samesubst subst subst' =
178 let tbl = Hashtbl.create (List.length subst) in
179 List.iter (fun (m, (c, t1, t2)) -> Hashtbl.add tbl m (c, t1, t2)) subst;
181 (fun (m, (c, t1, t2)) ->
183 let c', t1', t2' = Hashtbl.find tbl m in
184 if (c = c') && (t1 = t1') && (t2 = t2') then true
190 let subsaux left right =
191 let leftc = get_candidates Matching table left in
193 find_all_matches ~unif_fun:Inference.matching
194 metasenv context ugraph 0 left leftc
196 let ok what (_, subst, menv, ug, ((pos, (_, (_, l, r, o), _, _)), _)) =
198 let other = if pos = Utils.Left then r else l in
199 let subst', menv', ug' =
200 Inference.matching metasenv context what other ugraph in
201 samesubst subst subst'
205 let r = List.exists (ok right) leftr in
209 let rightc = get_candidates Matching table right in
211 find_all_matches ~unif_fun:Inference.matching
212 metasenv context ugraph 0 right rightc
214 List.exists (ok left) rightr
216 let res = subsaux tl tr in
218 Printf.printf "subsumption!:\ntarget: %s\n"
219 (Inference.string_of_equality ~env target);
226 let rec demodulate_term metasenv context ugraph table lift_amount term =
227 let module C = Cic in
228 let module S = CicSubstitution in
229 let module M = CicMetaSubst in
230 let module HL = HelmLibraryObjects in
234 let candidates = get_candidates Matching table term in
236 find_matches metasenv context ugraph lift_amount term candidates
247 (res, tl @ [S.lift 1 t])
250 demodulate_term metasenv context ugraph table
254 | None -> (None, tl @ [S.lift 1 t])
255 | Some (rel, _, _, _, _) -> (r, tl @ [rel]))
260 | Some (_, subst, menv, ug, eq_found) ->
261 Some (C.Appl ll, subst, menv, ug, eq_found)
263 | C.Prod (nn, s, t) ->
265 demodulate_term metasenv context ugraph table lift_amount s in (
269 demodulate_term metasenv
270 ((Some (nn, C.Decl s))::context) ugraph
271 table (lift_amount+1) t
275 | Some (t', subst, menv, ug, eq_found) ->
276 Some (C.Prod (nn, (S.lift 1 s), t'),
277 subst, menv, ug, eq_found)
279 | Some (s', subst, menv, ug, eq_found) ->
280 Some (C.Prod (nn, s', (S.lift 1 t)),
281 subst, menv, ug, eq_found)
288 let rec demodulation newmeta env table target =
289 let module C = Cic in
290 let module S = CicSubstitution in
291 let module M = CicMetaSubst in
292 let module HL = HelmLibraryObjects in
293 let metasenv, context, ugraph = env in
294 let proof, (eq_ty, left, right, order), metas, args = target in
295 let metasenv' = metasenv @ metas in
296 let build_newtarget is_left (t, subst, menv, ug, (eq_found, eq_URI)) =
297 let pos, (proof', (ty, what, other, _), menv', args') = eq_found in
298 let what, other = if pos = Utils.Left then what, other else other, what in
299 let newterm, newproof =
300 let bo = M.apply_subst subst (S.subst other t) in
302 C.Appl ([C.MutInd (HL.Logic.eq_URI, 0, []);
304 if is_left then [bo; S.lift 1 right] else [S.lift 1 left; bo])
306 let t' = C.Lambda (C.Anonymous, ty, bo'') in
308 M.apply_subst subst (C.Appl [C.Const (eq_URI, []); ty; what; t';
309 proof; other; proof'])
311 let left, right = if is_left then newterm, right else left, newterm in
313 (Inference.metas_of_term left) @ (Inference.metas_of_term right)
315 let newmetasenv = List.filter (fun (i, _, _) -> List.mem i m) metas
318 (function C.Meta (i, _) -> List.mem i m | _ -> assert false)
321 let ordering = !Utils.compare_terms left right in
322 newmeta, (newproof, (eq_ty, left, right, ordering), newmetasenv, newargs)
324 let res = demodulate_term metasenv' context ugraph table 0 left in
325 let build_identity (p, (t, l, r, o), m, a) =
327 | Utils.Gt -> (p, (t, r, r, Utils.Eq), m, a)
328 | _ -> (p, (t, l, l, Utils.Eq), m, a)
332 let newmeta, newtarget = build_newtarget true t in
333 if (Inference.is_identity (metasenv', context, ugraph) newtarget) ||
334 (Inference.meta_convertibility_eq target newtarget) then
337 if subsumption env table newtarget then
338 newmeta, build_identity newtarget
340 demodulation newmeta env table newtarget
342 let res = demodulate_term metasenv' context ugraph table 0 right in
345 let newmeta, newtarget = build_newtarget false t in
346 if (Inference.is_identity (metasenv', context, ugraph) newtarget) ||
347 (Inference.meta_convertibility_eq target newtarget) then
350 if subsumption env table newtarget then
351 newmeta, build_identity newtarget
353 demodulation newmeta env table newtarget
359 let rec betaexpand_term metasenv context ugraph table lift_amount term =
360 let module C = Cic in
361 let module S = CicSubstitution in
362 let module M = CicMetaSubst in
363 let module HL = HelmLibraryObjects in
364 let res, lifted_term =
369 (fun arg (res, lifted_tl) ->
372 let arg_res, lifted_arg =
373 betaexpand_term metasenv context ugraph table
377 (fun (t, s, m, ug, eq_found) ->
378 (Some t)::lifted_tl, s, m, ug, eq_found)
383 (fun (l, s, m, ug, eq_found) ->
384 (Some lifted_arg)::l, s, m, ug, eq_found)
386 (Some lifted_arg)::lifted_tl)
389 (fun (r, s, m, ug, eq_found) ->
390 None::r, s, m, ug, eq_found) res,
396 (fun (l, s, m, ug, eq_found) ->
397 (C.Meta (i, l), s, m, ug, eq_found)) l'
399 e, C.Meta (i, lifted_l)
402 [], if m <= lift_amount then C.Rel m else C.Rel (m+1)
404 | C.Prod (nn, s, t) ->
406 betaexpand_term metasenv context ugraph table lift_amount s in
408 betaexpand_term metasenv ((Some (nn, C.Decl s))::context) ugraph
409 table (lift_amount+1) t in
412 (fun (t, s, m, ug, eq_found) ->
413 C.Prod (nn, t, lifted_t), s, m, ug, eq_found) l1
416 (fun (t, s, m, ug, eq_found) ->
417 C.Prod (nn, lifted_s, t), s, m, ug, eq_found) l2 in
418 l1' @ l2', C.Prod (nn, lifted_s, lifted_t)
423 (fun arg (res, lifted_tl) ->
424 let arg_res, lifted_arg =
425 betaexpand_term metasenv context ugraph table lift_amount arg
429 (fun (a, s, m, ug, eq_found) ->
430 a::lifted_tl, s, m, ug, eq_found)
435 (fun (r, s, m, ug, eq_found) ->
436 lifted_arg::r, s, m, ug, eq_found)
438 lifted_arg::lifted_tl)
442 (fun (l, s, m, ug, eq_found) -> (C.Appl l, s, m, ug, eq_found)) l',
445 | t -> [], (S.lift lift_amount t)
448 | C.Meta _ -> res, lifted_term
450 let candidates = get_candidates Unification table term in
452 find_all_matches metasenv context ugraph lift_amount term candidates
458 let superposition_left (metasenv, context, ugraph) table target =
459 let module C = Cic in
460 let module S = CicSubstitution in
461 let module M = CicMetaSubst in
462 let module HL = HelmLibraryObjects in
463 let module CR = CicReduction in
464 let module U = Utils in
465 let proof, (eq_ty, left, right, ordering), _, _ = target in
467 let term = if ordering = U.Gt then left else right in
468 betaexpand_term metasenv context ugraph table 0 term
470 let build_new (bo, s, m, ug, (eq_found, eq_URI)) =
471 let pos, (proof', (ty, what, other, _), menv', args') = eq_found in
472 let what, other = if pos = Utils.Left then what, other else other, what in
473 let newgoal, newproof =
474 let bo' = M.apply_subst s (S.subst other bo) in
477 [C.MutInd (HL.Logic.eq_URI, 0, []);
479 if ordering = U.Gt then [bo'; S.lift 1 right]
480 else [S.lift 1 left; bo'])
482 let t' = C.Lambda (C.Anonymous, ty, bo'') in
485 (C.Appl [C.Const (eq_URI, []); ty; what; t';
486 proof; other; proof'])
489 if ordering = U.Gt then newgoal, right else left, newgoal in
490 let neworder = !Utils.compare_terms left right in
491 (newproof, (eq_ty, left, right, neworder), [], [])
493 List.map build_new expansions
497 let superposition_right newmeta (metasenv, context, ugraph) table target =
498 let module C = Cic in
499 let module S = CicSubstitution in
500 let module M = CicMetaSubst in
501 let module HL = HelmLibraryObjects in
502 let module CR = CicReduction in
503 let module U = Utils in
504 let eqproof, (eq_ty, left, right, ordering), newmetas, args = target in
505 let metasenv' = metasenv @ newmetas in
506 let maxmeta = ref newmeta in
509 | U.Gt -> fst (betaexpand_term metasenv' context ugraph table 0 left), []
510 | U.Lt -> [], fst (betaexpand_term metasenv' context ugraph table 0 right)
514 (fun (_, subst, _, _, _) ->
515 let subst = M.apply_subst subst in
516 let o = !Utils.compare_terms (subst l) (subst r) in
517 o <> U.Lt && o <> U.Le)
518 (fst (betaexpand_term metasenv' context ugraph table 0 l))
520 (res left right), (res right left)
522 let build_new ordering (bo, s, m, ug, (eq_found, eq_URI)) =
523 let pos, (proof', (ty, what, other, _), menv', args') = eq_found in
524 let what, other = if pos = Utils.Left then what, other else other, what in
525 let newgoal, newproof =
526 let bo' = M.apply_subst s (S.subst other bo) in
529 [C.MutInd (HL.Logic.eq_URI, 0, []); S.lift 1 eq_ty] @
530 if ordering = U.Gt then [bo'; S.lift 1 right]
531 else [S.lift 1 left; bo'])
533 let t' = C.Lambda (C.Anonymous, ty, bo'') in
536 (C.Appl [C.Const (eq_URI, []); ty; what; t';
537 eqproof; other; proof'])
539 let newmeta, newequality =
541 if ordering = U.Gt then newgoal, M.apply_subst s right
542 else M.apply_subst s left, newgoal in
543 let neworder = !Utils.compare_terms left right
544 and newmenv = newmetas @ menv'
545 and newargs = args @ args' in
546 let eq' = (newproof, (eq_ty, left, right, neworder), newmenv, newargs)
547 and env = (metasenv, context, ugraph) in
548 let newm, eq' = Inference.fix_metas !maxmeta eq' in
554 let new1 = List.map (build_new U.Gt) res1
555 and new2 = List.map (build_new U.Lt) res2 in
557 | _, (_, left, right, _), _, _ ->
558 not (fst (CR.are_convertible context left right ugraph))
561 (List.filter ok (new1 @ new2)))