2 type retrieval_mode = Matching | Unification;;
6 Path_indexing.PSTrie.empty
9 let index = Path_indexing.index
10 and remove_index = Path_indexing.remove_index
11 and in_index = Path_indexing.in_index;;
13 let get_candidates mode trie term =
16 | Matching -> Path_indexing.retrieve_generalizations trie term
17 | Unification -> Path_indexing.retrieve_unifiables trie term
19 Path_indexing.PosEqSet.elements s
25 Discrimination_tree.DiscriminationTree.empty
28 let index = Discrimination_tree.index
29 and remove_index = Discrimination_tree.remove_index
30 and in_index = Discrimination_tree.in_index;;
32 let get_candidates mode tree term =
34 | Matching -> Discrimination_tree.retrieve_generalizations tree term
35 | Unification -> Discrimination_tree.retrieve_unifiables tree term
40 let rec find_matches metasenv context ugraph lift_amount term =
42 let module U = Utils in
43 let module S = CicSubstitution in
44 let module M = CicMetaSubst in
45 let module HL = HelmLibraryObjects in
46 let cmp = !Utils.compare_terms in
47 let names = Utils.names_of_context context in
51 let pos, (proof, (ty, left, right, o), metas, args) = candidate in
52 let do_match c other eq_URI =
53 let subst', metasenv', ugraph' =
54 Inference.matching (metasenv @ metas) context
55 term (S.lift lift_amount c) ugraph
57 Some (C.Rel (1 + lift_amount), subst', metasenv', ugraph',
60 let c, other, eq_URI =
61 if pos = Utils.Left then left, right, HL.Logic.eq_ind_URI
62 else right, left, HL.Logic.eq_ind_r_URI
64 if o <> U.Incomparable then
66 do_match c other eq_URI
68 find_matches metasenv context ugraph lift_amount term tl
70 let res = try do_match c other eq_URI with e -> None in
72 | Some (_, s, _, _, _) ->
73 let c' = M.apply_subst s c
74 and other' = M.apply_subst s other in
75 let order = cmp c' other' in
76 let names = U.names_of_context context in
80 find_matches metasenv context ugraph lift_amount term tl
82 find_matches metasenv context ugraph lift_amount term tl
86 let rec find_all_matches ?(unif_fun=CicUnification.fo_unif)
87 metasenv context ugraph lift_amount term =
89 let module U = Utils in
90 let module S = CicSubstitution in
91 let module M = CicMetaSubst in
92 let module HL = HelmLibraryObjects in
93 let cmp = !Utils.compare_terms in
94 let names = Utils.names_of_context context in
98 let pos, (proof, (ty, left, right, o), metas, args) = candidate in
99 let do_match c other eq_URI =
100 let subst', metasenv', ugraph' =
101 unif_fun (metasenv @ metas) context
102 term (S.lift lift_amount c) ugraph
104 (C.Rel (1 + lift_amount), subst', metasenv', ugraph',
107 let c, other, eq_URI =
108 if pos = Utils.Left then left, right, HL.Logic.eq_ind_URI
109 else right, left, HL.Logic.eq_ind_r_URI
111 if o <> U.Incomparable then
113 let res = do_match c other eq_URI in
114 res::(find_all_matches ~unif_fun metasenv context ugraph
117 find_all_matches ~unif_fun metasenv context ugraph
121 let res = do_match c other eq_URI in
124 let c' = M.apply_subst s c
125 and other' = M.apply_subst s other in
126 let order = cmp c' other' in
127 let names = U.names_of_context context in
128 if order <> U.Lt && order <> U.Le then
129 res::(find_all_matches ~unif_fun metasenv context ugraph
132 find_all_matches ~unif_fun metasenv context ugraph
135 find_all_matches ~unif_fun metasenv context ugraph
140 let subsumption env table target =
141 let _, (ty, tl, tr, _), tmetas, _ = target in
142 let metasenv, context, ugraph = env in
143 let metasenv = metasenv @ tmetas in
144 let samesubst subst subst' =
145 let tbl = Hashtbl.create (List.length subst) in
146 List.iter (fun (m, (c, t1, t2)) -> Hashtbl.add tbl m (c, t1, t2)) subst;
148 (fun (m, (c, t1, t2)) ->
150 let c', t1', t2' = Hashtbl.find tbl m in
151 if (c = c') && (t1 = t1') && (t2 = t2') then true
157 let subsaux left right =
158 let leftc = get_candidates Matching table left in
160 find_all_matches ~unif_fun:Inference.matching
161 metasenv context ugraph 0 left leftc
163 let ok what (_, subst, menv, ug, ((pos, (_, (_, l, r, o), _, _)), _)) =
165 let other = if pos = Utils.Left then r else l in
166 let subst', menv', ug' =
167 Inference.matching metasenv context what other ugraph in
168 samesubst subst subst'
172 let r = List.exists (ok right) leftr in
176 let rightc = get_candidates Matching table right in
178 find_all_matches ~unif_fun:Inference.matching
179 metasenv context ugraph 0 right rightc
181 List.exists (ok left) rightr
183 let res = subsaux tl tr in
185 Printf.printf "subsumption!:\ntarget: %s\n"
186 (Inference.string_of_equality ~env target);
193 let rec demodulate_term metasenv context ugraph table lift_amount term =
194 let module C = Cic in
195 let module S = CicSubstitution in
196 let module M = CicMetaSubst in
197 let module HL = HelmLibraryObjects in
198 let candidates = get_candidates Matching table term in
203 find_matches metasenv context ugraph lift_amount term candidates
214 (res, tl @ [S.lift 1 t])
217 demodulate_term metasenv context ugraph table
221 | None -> (None, tl @ [S.lift 1 t])
222 | Some (rel, _, _, _, _) -> (r, tl @ [rel]))
227 | Some (_, subst, menv, ug, eq_found) ->
228 Some (C.Appl ll, subst, menv, ug, eq_found)
230 | C.Prod (nn, s, t) ->
232 demodulate_term metasenv context ugraph table lift_amount s in (
236 demodulate_term metasenv
237 ((Some (nn, C.Decl s))::context) ugraph
238 table (lift_amount+1) t
242 | Some (t', subst, menv, ug, eq_found) ->
243 Some (C.Prod (nn, (S.lift 1 s), t'),
244 subst, menv, ug, eq_found)
246 | Some (s', subst, menv, ug, eq_found) ->
247 Some (C.Prod (nn, s', (S.lift 1 t)),
248 subst, menv, ug, eq_found)
255 let rec demodulation newmeta env table target =
256 let module C = Cic in
257 let module S = CicSubstitution in
258 let module M = CicMetaSubst in
259 let module HL = HelmLibraryObjects in
260 let metasenv, context, ugraph = env in
261 let proof, (eq_ty, left, right, order), metas, args = target in
262 let metasenv' = metasenv @ metas in
263 let build_newtarget is_left (t, subst, menv, ug, (eq_found, eq_URI)) =
264 let pos, (proof', (ty, what, other, _), menv', args') = eq_found in
265 let what, other = if pos = Utils.Left then what, other else other, what in
266 let newterm, newproof =
267 let bo = M.apply_subst subst (S.subst other t) in
269 C.Appl ([C.MutInd (HL.Logic.eq_URI, 0, []);
271 if is_left then [bo; S.lift 1 right] else [S.lift 1 left; bo])
273 let t' = C.Lambda (C.Anonymous, ty, bo'') in
275 M.apply_subst subst (C.Appl [C.Const (eq_URI, []); ty; what; t';
276 proof; other; proof'])
278 let left, right = if is_left then newterm, right else left, newterm in
280 (Inference.metas_of_term left) @ (Inference.metas_of_term right)
282 let newmetasenv = List.filter (fun (i, _, _) -> List.mem i m) metas
285 (function C.Meta (i, _) -> List.mem i m | _ -> assert false)
288 let ordering = !Utils.compare_terms left right in
289 newmeta, (newproof, (eq_ty, left, right, ordering), newmetasenv, newargs)
291 let res = demodulate_term metasenv' context ugraph table 0 left in
292 let build_identity (p, (t, l, r, o), m, a) =
294 | Utils.Gt -> (p, (t, r, r, Utils.Eq), m, a)
295 | _ -> (p, (t, l, l, Utils.Eq), m, a)
299 let newmeta, newtarget = build_newtarget true t in
300 if (Inference.is_identity (metasenv', context, ugraph) newtarget) ||
301 (Inference.meta_convertibility_eq target newtarget) then
304 if subsumption env table newtarget then
305 newmeta, build_identity newtarget
307 demodulation newmeta env table newtarget
309 let res = demodulate_term metasenv' context ugraph table 0 right in
312 let newmeta, newtarget = build_newtarget false t in
313 if (Inference.is_identity (metasenv', context, ugraph) newtarget) ||
314 (Inference.meta_convertibility_eq target newtarget) then
317 if subsumption env table newtarget then
318 newmeta, build_identity newtarget
320 demodulation newmeta env table newtarget
326 let rec betaexpand_term metasenv context ugraph table lift_amount term =
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 candidates = get_candidates Unification table term in
332 let res, lifted_term =
337 (fun arg (res, lifted_tl) ->
340 let arg_res, lifted_arg =
341 betaexpand_term metasenv context ugraph table
345 (fun (t, s, m, ug, eq_found) ->
346 (Some t)::lifted_tl, s, m, ug, eq_found)
351 (fun (l, s, m, ug, eq_found) ->
352 (Some lifted_arg)::l, s, m, ug, eq_found)
354 (Some lifted_arg)::lifted_tl)
357 (fun (r, s, m, ug, eq_found) ->
358 None::r, s, m, ug, eq_found) res,
364 (fun (l, s, m, ug, eq_found) ->
365 (C.Meta (i, l), s, m, ug, eq_found)) l'
367 e, C.Meta (i, lifted_l)
370 [], if m <= lift_amount then C.Rel m else C.Rel (m+1)
372 | C.Prod (nn, s, t) ->
374 betaexpand_term metasenv context ugraph table lift_amount s in
376 betaexpand_term metasenv ((Some (nn, C.Decl s))::context) ugraph
377 table (lift_amount+1) t in
380 (fun (t, s, m, ug, eq_found) ->
381 C.Prod (nn, t, lifted_t), s, m, ug, eq_found) l1
384 (fun (t, s, m, ug, eq_found) ->
385 C.Prod (nn, lifted_s, t), s, m, ug, eq_found) l2 in
386 l1' @ l2', C.Prod (nn, lifted_s, lifted_t)
391 (fun arg (res, lifted_tl) ->
392 let arg_res, lifted_arg =
393 betaexpand_term metasenv context ugraph table lift_amount arg
397 (fun (a, s, m, ug, eq_found) ->
398 a::lifted_tl, s, m, ug, eq_found)
403 (fun (r, s, m, ug, eq_found) ->
404 lifted_arg::r, s, m, ug, eq_found)
406 lifted_arg::lifted_tl)
410 (fun (l, s, m, ug, eq_found) -> (C.Appl l, s, m, ug, eq_found)) l',
413 | t -> [], (S.lift lift_amount t)
416 | C.Meta _ -> res, lifted_term
419 find_all_matches metasenv context ugraph lift_amount term candidates
425 let superposition_left (metasenv, context, ugraph) table target =
426 let module C = Cic in
427 let module S = CicSubstitution in
428 let module M = CicMetaSubst in
429 let module HL = HelmLibraryObjects in
430 let module CR = CicReduction in
431 let module U = Utils in
432 let proof, (eq_ty, left, right, ordering), _, _ = target in
434 let term = if ordering = U.Gt then left else right in
435 betaexpand_term metasenv context ugraph table 0 term
437 let build_new (bo, s, m, ug, (eq_found, eq_URI)) =
438 let pos, (proof', (ty, what, other, _), menv', args') = eq_found in
439 let what, other = if pos = Utils.Left then what, other else other, what in
440 let newgoal, newproof =
441 let bo' = M.apply_subst s (S.subst other bo) in
444 [C.MutInd (HL.Logic.eq_URI, 0, []);
446 if ordering = U.Gt then [bo'; S.lift 1 right]
447 else [S.lift 1 left; bo'])
449 let t' = C.Lambda (C.Anonymous, ty, bo'') in
452 (C.Appl [C.Const (eq_URI, []); ty; what; t';
453 proof; other; proof'])
456 if ordering = U.Gt then newgoal, right else left, newgoal in
457 let neworder = !Utils.compare_terms left right in
458 (newproof, (eq_ty, left, right, neworder), [], [])
460 List.map build_new expansions
464 let superposition_right newmeta (metasenv, context, ugraph) table target =
465 let module C = Cic in
466 let module S = CicSubstitution in
467 let module M = CicMetaSubst in
468 let module HL = HelmLibraryObjects in
469 let module CR = CicReduction in
470 let module U = Utils in
471 let eqproof, (eq_ty, left, right, ordering), newmetas, args = target in
472 let metasenv' = metasenv @ newmetas in
473 let maxmeta = ref newmeta in
476 | U.Gt -> fst (betaexpand_term metasenv' context ugraph table 0 left), []
477 | U.Lt -> [], fst (betaexpand_term metasenv' context ugraph table 0 right)
481 (fun (_, subst, _, _, _) ->
482 let subst = M.apply_subst subst in
483 let o = !Utils.compare_terms (subst l) (subst r) in
484 o <> U.Lt && o <> U.Le)
485 (fst (betaexpand_term metasenv' context ugraph table 0 l))
487 (res left right), (res right left)
489 let build_new ordering (bo, s, m, ug, (eq_found, eq_URI)) =
490 let pos, (proof', (ty, what, other, _), menv', args') = eq_found in
491 let what, other = if pos = Utils.Left then what, other else other, what in
492 let newgoal, newproof =
493 let bo' = M.apply_subst s (S.subst other bo) in
496 [C.MutInd (HL.Logic.eq_URI, 0, []); S.lift 1 eq_ty] @
497 if ordering = U.Gt then [bo'; S.lift 1 right]
498 else [S.lift 1 left; bo'])
500 let t' = C.Lambda (C.Anonymous, ty, bo'') in
503 (C.Appl [C.Const (eq_URI, []); ty; what; t';
504 eqproof; other; proof'])
506 let newmeta, newequality =
508 if ordering = U.Gt then newgoal, M.apply_subst s right
509 else M.apply_subst s left, newgoal in
510 let neworder = !Utils.compare_terms left right
511 and newmenv = newmetas @ menv'
512 and newargs = args @ args' in
513 let eq' = (newproof, (eq_ty, left, right, neworder), newmenv, newargs)
514 and env = (metasenv, context, ugraph) in
515 let newm, eq' = Inference.fix_metas !maxmeta eq' in
521 let new1 = List.map (build_new U.Gt) res1
522 and new2 = List.map (build_new U.Lt) res2 in
524 | _, (_, left, right, _), _, _ ->
525 not (fst (CR.are_convertible context left right ugraph))
528 (List.filter ok (new1 @ new2)))