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))
25 Path_indexing.PSTrie.empty
28 let index = Path_indexing.index
29 and remove_index = Path_indexing.remove_index
30 and in_index = Path_indexing.in_index;;
32 let get_candidates mode trie term =
35 | Matching -> Path_indexing.retrieve_generalizations trie term
36 | Unification -> Path_indexing.retrieve_unifiables trie term
38 Path_indexing.PosEqSet.elements s
44 Discrimination_tree.DiscriminationTree.empty
47 let index = Discrimination_tree.index
48 and remove_index = Discrimination_tree.remove_index
49 and in_index = Discrimination_tree.in_index;;
51 let get_candidates mode tree term =
55 | Matching -> Discrimination_tree.retrieve_generalizations tree term
56 | Unification -> Discrimination_tree.retrieve_unifiables tree term
58 Discrimination_tree.PosEqSet.elements s
60 (* print_candidates mode term res; *)
66 let rec find_matches metasenv context ugraph lift_amount term =
68 let module U = Utils in
69 let module S = CicSubstitution in
70 let module M = CicMetaSubst in
71 let module HL = HelmLibraryObjects in
72 let cmp = !Utils.compare_terms in
73 let names = Utils.names_of_context context in
77 let pos, (proof, (ty, left, right, o), metas, args) = candidate in
78 let do_match c other eq_URI =
79 let subst', metasenv', ugraph' =
80 Inference.matching (metasenv @ metas) context
81 term (S.lift lift_amount c) ugraph
83 Some (C.Rel (1 + lift_amount), subst', metasenv', ugraph',
86 let c, other, eq_URI =
87 if pos = Utils.Left then left, right, HL.Logic.eq_ind_URI
88 else right, left, HL.Logic.eq_ind_r_URI
90 if o <> U.Incomparable then
92 do_match c other eq_URI
94 find_matches metasenv context ugraph lift_amount term tl
96 let res = try do_match c other eq_URI with e -> None in
98 | Some (_, s, _, _, _) ->
99 let c' = M.apply_subst s c
100 and other' = M.apply_subst s other in
101 let order = cmp c' other' in
102 let names = U.names_of_context context in
106 find_matches metasenv context ugraph lift_amount term tl
108 find_matches metasenv context ugraph lift_amount term tl
112 let rec find_all_matches ?(unif_fun=CicUnification.fo_unif)
113 metasenv context ugraph lift_amount term =
114 let module C = Cic in
115 let module U = Utils in
116 let module S = CicSubstitution in
117 let module M = CicMetaSubst in
118 let module HL = HelmLibraryObjects in
119 let cmp = !Utils.compare_terms in
120 let names = Utils.names_of_context context in
124 let pos, (proof, (ty, left, right, o), metas, args) = candidate in
125 let do_match c other eq_URI =
126 let subst', metasenv', ugraph' =
127 unif_fun (metasenv @ metas) context
128 term (S.lift lift_amount c) ugraph
130 (C.Rel (1 + lift_amount), subst', metasenv', ugraph',
133 let c, other, eq_URI =
134 if pos = Utils.Left then left, right, HL.Logic.eq_ind_URI
135 else right, left, HL.Logic.eq_ind_r_URI
137 if o <> U.Incomparable then
139 let res = do_match c other eq_URI in
140 res::(find_all_matches ~unif_fun metasenv context ugraph
143 find_all_matches ~unif_fun metasenv context ugraph
147 let res = do_match c other eq_URI in
150 let c' = M.apply_subst s c
151 and other' = M.apply_subst s other in
152 let order = cmp c' other' in
153 let names = U.names_of_context context in
154 if order <> U.Lt && order <> U.Le then
155 res::(find_all_matches ~unif_fun metasenv context ugraph
158 find_all_matches ~unif_fun metasenv context ugraph
161 find_all_matches ~unif_fun metasenv context ugraph
166 let subsumption env table target =
167 let _, (ty, tl, tr, _), tmetas, _ = target in
168 let metasenv, context, ugraph = env in
169 let metasenv = metasenv @ tmetas in
170 let samesubst subst subst' =
171 let tbl = Hashtbl.create (List.length subst) in
172 List.iter (fun (m, (c, t1, t2)) -> Hashtbl.add tbl m (c, t1, t2)) subst;
174 (fun (m, (c, t1, t2)) ->
176 let c', t1', t2' = Hashtbl.find tbl m in
177 if (c = c') && (t1 = t1') && (t2 = t2') then true
183 let subsaux left right =
184 let leftc = get_candidates Matching table left in
186 find_all_matches ~unif_fun:Inference.matching
187 metasenv context ugraph 0 left leftc
189 let ok what (_, subst, menv, ug, ((pos, (_, (_, l, r, o), _, _)), _)) =
191 let other = if pos = Utils.Left then r else l in
192 let subst', menv', ug' =
193 Inference.matching metasenv context what other ugraph in
194 samesubst subst subst'
198 let r = List.exists (ok right) leftr in
202 let rightc = get_candidates Matching table right in
204 find_all_matches ~unif_fun:Inference.matching
205 metasenv context ugraph 0 right rightc
207 List.exists (ok left) rightr
209 let res = subsaux tl tr in
211 Printf.printf "subsumption!:\ntarget: %s\n"
212 (Inference.string_of_equality ~env target);
219 let rec demodulate_term metasenv context ugraph table lift_amount term =
220 let module C = Cic in
221 let module S = CicSubstitution in
222 let module M = CicMetaSubst in
223 let module HL = HelmLibraryObjects in
224 let candidates = get_candidates Matching table term in
229 find_matches metasenv context ugraph lift_amount term candidates
240 (res, tl @ [S.lift 1 t])
243 demodulate_term metasenv context ugraph table
247 | None -> (None, tl @ [S.lift 1 t])
248 | Some (rel, _, _, _, _) -> (r, tl @ [rel]))
253 | Some (_, subst, menv, ug, eq_found) ->
254 Some (C.Appl ll, subst, menv, ug, eq_found)
256 | C.Prod (nn, s, t) ->
258 demodulate_term metasenv context ugraph table lift_amount s in (
262 demodulate_term metasenv
263 ((Some (nn, C.Decl s))::context) ugraph
264 table (lift_amount+1) t
268 | Some (t', subst, menv, ug, eq_found) ->
269 Some (C.Prod (nn, (S.lift 1 s), t'),
270 subst, menv, ug, eq_found)
272 | Some (s', subst, menv, ug, eq_found) ->
273 Some (C.Prod (nn, s', (S.lift 1 t)),
274 subst, menv, ug, eq_found)
281 let rec demodulation newmeta env table target =
282 let module C = Cic in
283 let module S = CicSubstitution in
284 let module M = CicMetaSubst in
285 let module HL = HelmLibraryObjects in
286 let metasenv, context, ugraph = env in
287 let proof, (eq_ty, left, right, order), metas, args = target in
288 let metasenv' = metasenv @ metas in
289 let build_newtarget is_left (t, subst, menv, ug, (eq_found, eq_URI)) =
290 let pos, (proof', (ty, what, other, _), menv', args') = eq_found in
291 let what, other = if pos = Utils.Left then what, other else other, what in
292 let newterm, newproof =
293 let bo = M.apply_subst subst (S.subst other t) in
295 C.Appl ([C.MutInd (HL.Logic.eq_URI, 0, []);
297 if is_left then [bo; S.lift 1 right] else [S.lift 1 left; bo])
299 let t' = C.Lambda (C.Anonymous, ty, bo'') in
301 M.apply_subst subst (C.Appl [C.Const (eq_URI, []); ty; what; t';
302 proof; other; proof'])
304 let left, right = if is_left then newterm, right else left, newterm in
306 (Inference.metas_of_term left) @ (Inference.metas_of_term right)
308 let newmetasenv = List.filter (fun (i, _, _) -> List.mem i m) metas
311 (function C.Meta (i, _) -> List.mem i m | _ -> assert false)
314 let ordering = !Utils.compare_terms left right in
315 newmeta, (newproof, (eq_ty, left, right, ordering), newmetasenv, newargs)
317 let res = demodulate_term metasenv' context ugraph table 0 left in
318 let build_identity (p, (t, l, r, o), m, a) =
320 | Utils.Gt -> (p, (t, r, r, Utils.Eq), m, a)
321 | _ -> (p, (t, l, l, Utils.Eq), m, a)
325 let newmeta, newtarget = build_newtarget true t in
326 if (Inference.is_identity (metasenv', context, ugraph) newtarget) ||
327 (Inference.meta_convertibility_eq target newtarget) then
330 if subsumption env table newtarget then
331 newmeta, build_identity newtarget
333 demodulation newmeta env table newtarget
335 let res = demodulate_term metasenv' context ugraph table 0 right in
338 let newmeta, newtarget = build_newtarget false t in
339 if (Inference.is_identity (metasenv', context, ugraph) newtarget) ||
340 (Inference.meta_convertibility_eq target newtarget) then
343 if subsumption env table newtarget then
344 newmeta, build_identity newtarget
346 demodulation newmeta env table newtarget
352 let rec betaexpand_term metasenv context ugraph table lift_amount term =
353 let module C = Cic in
354 let module S = CicSubstitution in
355 let module M = CicMetaSubst in
356 let module HL = HelmLibraryObjects in
357 let candidates = get_candidates Unification table term in
358 let res, lifted_term =
363 (fun arg (res, lifted_tl) ->
366 let arg_res, lifted_arg =
367 betaexpand_term metasenv context ugraph table
371 (fun (t, s, m, ug, eq_found) ->
372 (Some t)::lifted_tl, s, m, ug, eq_found)
377 (fun (l, s, m, ug, eq_found) ->
378 (Some lifted_arg)::l, s, m, ug, eq_found)
380 (Some lifted_arg)::lifted_tl)
383 (fun (r, s, m, ug, eq_found) ->
384 None::r, s, m, ug, eq_found) res,
390 (fun (l, s, m, ug, eq_found) ->
391 (C.Meta (i, l), s, m, ug, eq_found)) l'
393 e, C.Meta (i, lifted_l)
396 [], if m <= lift_amount then C.Rel m else C.Rel (m+1)
398 | C.Prod (nn, s, t) ->
400 betaexpand_term metasenv context ugraph table lift_amount s in
402 betaexpand_term metasenv ((Some (nn, C.Decl s))::context) ugraph
403 table (lift_amount+1) t in
406 (fun (t, s, m, ug, eq_found) ->
407 C.Prod (nn, t, lifted_t), s, m, ug, eq_found) l1
410 (fun (t, s, m, ug, eq_found) ->
411 C.Prod (nn, lifted_s, t), s, m, ug, eq_found) l2 in
412 l1' @ l2', C.Prod (nn, lifted_s, lifted_t)
417 (fun arg (res, lifted_tl) ->
418 let arg_res, lifted_arg =
419 betaexpand_term metasenv context ugraph table lift_amount arg
423 (fun (a, s, m, ug, eq_found) ->
424 a::lifted_tl, s, m, ug, eq_found)
429 (fun (r, s, m, ug, eq_found) ->
430 lifted_arg::r, s, m, ug, eq_found)
432 lifted_arg::lifted_tl)
436 (fun (l, s, m, ug, eq_found) -> (C.Appl l, s, m, ug, eq_found)) l',
439 | t -> [], (S.lift lift_amount t)
442 | C.Meta _ -> res, lifted_term
445 find_all_matches metasenv context ugraph lift_amount term candidates
451 let superposition_left (metasenv, context, ugraph) table target =
452 let module C = Cic in
453 let module S = CicSubstitution in
454 let module M = CicMetaSubst in
455 let module HL = HelmLibraryObjects in
456 let module CR = CicReduction in
457 let module U = Utils in
458 let proof, (eq_ty, left, right, ordering), _, _ = target in
460 let term = if ordering = U.Gt then left else right in
461 betaexpand_term metasenv context ugraph table 0 term
463 let build_new (bo, s, m, ug, (eq_found, eq_URI)) =
464 let pos, (proof', (ty, what, other, _), menv', args') = eq_found in
465 let what, other = if pos = Utils.Left then what, other else other, what in
466 let newgoal, newproof =
467 let bo' = M.apply_subst s (S.subst other bo) in
470 [C.MutInd (HL.Logic.eq_URI, 0, []);
472 if ordering = U.Gt then [bo'; S.lift 1 right]
473 else [S.lift 1 left; bo'])
475 let t' = C.Lambda (C.Anonymous, ty, bo'') in
478 (C.Appl [C.Const (eq_URI, []); ty; what; t';
479 proof; other; proof'])
482 if ordering = U.Gt then newgoal, right else left, newgoal in
483 let neworder = !Utils.compare_terms left right in
484 (newproof, (eq_ty, left, right, neworder), [], [])
486 List.map build_new expansions
490 let superposition_right newmeta (metasenv, context, ugraph) table target =
491 let module C = Cic in
492 let module S = CicSubstitution in
493 let module M = CicMetaSubst in
494 let module HL = HelmLibraryObjects in
495 let module CR = CicReduction in
496 let module U = Utils in
497 let eqproof, (eq_ty, left, right, ordering), newmetas, args = target in
498 let metasenv' = metasenv @ newmetas in
499 let maxmeta = ref newmeta in
502 | U.Gt -> fst (betaexpand_term metasenv' context ugraph table 0 left), []
503 | U.Lt -> [], fst (betaexpand_term metasenv' context ugraph table 0 right)
507 (fun (_, subst, _, _, _) ->
508 let subst = M.apply_subst subst in
509 let o = !Utils.compare_terms (subst l) (subst r) in
510 o <> U.Lt && o <> U.Le)
511 (fst (betaexpand_term metasenv' context ugraph table 0 l))
513 (res left right), (res right left)
515 let build_new ordering (bo, s, m, ug, (eq_found, eq_URI)) =
516 let pos, (proof', (ty, what, other, _), menv', args') = eq_found in
517 let what, other = if pos = Utils.Left then what, other else other, what in
518 let newgoal, newproof =
519 let bo' = M.apply_subst s (S.subst other bo) in
522 [C.MutInd (HL.Logic.eq_URI, 0, []); S.lift 1 eq_ty] @
523 if ordering = U.Gt then [bo'; S.lift 1 right]
524 else [S.lift 1 left; bo'])
526 let t' = C.Lambda (C.Anonymous, ty, bo'') in
529 (C.Appl [C.Const (eq_URI, []); ty; what; t';
530 eqproof; other; proof'])
532 let newmeta, newequality =
534 if ordering = U.Gt then newgoal, M.apply_subst s right
535 else M.apply_subst s left, newgoal in
536 let neworder = !Utils.compare_terms left right
537 and newmenv = newmetas @ menv'
538 and newargs = args @ args' in
539 let eq' = (newproof, (eq_ty, left, right, neworder), newmenv, newargs)
540 and env = (metasenv, context, ugraph) in
541 let newm, eq' = Inference.fix_metas !maxmeta eq' in
547 let new1 = List.map (build_new U.Gt) res1
548 and new2 = List.map (build_new U.Lt) res2 in
550 | _, (_, left, right, _), _, _ ->
551 not (fst (CR.are_convertible context left right ugraph))
554 (List.filter ok (new1 @ new2)))