8 Cic.term * (* left side *)
9 Cic.term * (* right side *)
10 Utils.comparison) * (* ordering *)
11 Cic.metasenv * (* environment for metas *)
12 Cic.term list (* arguments *)
16 | BasicProof of Cic.term
18 Cic.substitution * UriManager.uri *
19 (Cic.name * Cic.term) * Cic.term *
20 (* name, ty, eq_ty, left, right *)
21 (* (Cic.name * Cic.term * Cic.term * Cic.term * Cic.term) * *)
22 (Utils.pos * equality) * proof
23 | ProofGoalBlock of proof * proof (* equality *)
24 (* | ProofSymBlock of Cic.term Cic.explicit_named_substitution * proof *)
25 | ProofSymBlock of Cic.term list * proof
26 | SubProof of Cic.term * int * proof
30 let string_of_equality ?env =
34 | w, _, (ty, left, right, o), _, _ ->
35 Printf.sprintf "Weight: %d, {%s}: %s =(%s) %s" w (CicPp.ppterm ty)
36 (CicPp.ppterm left) (string_of_comparison o) (CicPp.ppterm right)
38 | Some (_, context, _) -> (
39 let names = names_of_context context in
41 | w, _, (ty, left, right, o), _, _ ->
42 Printf.sprintf "Weight: %d, {%s}: %s =(%s) %s" w (CicPp.pp ty names)
43 (CicPp.pp left names) (string_of_comparison o)
44 (CicPp.pp right names)
49 let build_ens_for_sym_eq sym_eq_URI termlist =
50 let obj, _ = CicEnvironment.get_obj CicUniv.empty_ugraph sym_eq_URI in
52 | Cic.Constant (_, _, _, uris, _) ->
53 assert (List.length uris <= List.length termlist);
54 let rec aux = function
56 | (uri::uris), (term::tl) ->
57 let ens, args = aux (uris, tl) in
58 (uri, term)::ens, args
59 | _, _ -> assert false
63 (* [(UriManager.uri_of_string *)
64 (* "cic:/Coq/Init/Logic/Logic_lemmas/equality/A.var", ty); *)
65 (* (UriManager.uri_of_string *)
66 (* "cic:/Coq/Init/Logic/Logic_lemmas/equality/x.var", x); *)
67 (* (UriManager.uri_of_string *)
68 (* "cic:/Coq/Init/Logic/Logic_lemmas/equality/y.var", y)] *)
72 let build_proof_term proof =
73 let rec do_build_proof proof =
76 Printf.fprintf stderr "WARNING: no proof!\n";
78 | BasicProof term -> term
79 | ProofGoalBlock (proofbit, proof) ->
80 print_endline "found ProofGoalBlock, going up...";
81 do_build_goal_proof proofbit proof
82 (* | ProofSymBlock (ens, proof) -> *)
83 (* let proof = do_build_proof proof in *)
85 (* Cic.Const (Utils.sym_eq_URI (), ens); (\* symmetry *\) *)
88 | ProofSymBlock (termlist, proof) ->
89 let proof = do_build_proof proof in
90 let ens, args = build_ens_for_sym_eq (Utils.sym_eq_URI ()) termlist in
91 Cic.Appl ([Cic.Const (Utils.sym_eq_URI (), ens)] @ args @ [proof])
92 | ProofBlock (subst, eq_URI, (name, ty), bo(* t' *), (pos, eq), eqproof) ->
93 let t' = Cic.Lambda (name, ty, bo) in
95 let _, proof', _, _, _ = eq in
98 let eqproof = do_build_proof eqproof in
99 let _, _, (ty, what, other, _), menv', args' = eq in
101 if pos = Utils.Left then what, other else other, what
103 CicMetaSubst.apply_subst subst
104 (Cic.Appl [Cic.Const (eq_URI, []); ty;
105 what; t'; eqproof; other; proof'])
106 | SubProof (term, meta_index, proof) ->
107 let proof = do_build_proof proof in
109 | Cic.Meta (j, _) -> i = j
112 ProofEngineReduction.replace
113 ~equality:eq ~what:[meta_index] ~with_what:[proof] ~where:term
115 and do_build_goal_proof proofbit proof =
116 (* match proofbit with *)
117 (* | BasicProof _ -> do_build_proof proof *)
120 | ProofGoalBlock (pb, p(* eq *)) ->
121 do_build_proof (ProofGoalBlock (replace_proof proofbit pb, p(* eq *)))
122 (* let _, proof, _, _, _ = eq in *)
123 (* let newproof = replace_proof proofbit proof in *)
124 (* do_build_proof newproof *)
126 (* | ProofBlock (subst, eq_URI, t', poseq, eqproof) -> *)
127 (* let eqproof' = replace_proof proofbit eqproof in *)
128 (* do_build_proof (ProofBlock (subst, eq_URI, t', poseq, eqproof')) *)
129 | _ -> do_build_proof (replace_proof proofbit proof) (* assert false *)
131 and replace_proof newproof = function
132 | ProofBlock (subst, eq_URI, namety, bo(* t' *), poseq, eqproof) ->
133 let eqproof' = replace_proof newproof eqproof in
134 ProofBlock (subst, eq_URI, namety, bo(* t' *), poseq, eqproof')
135 | ProofGoalBlock (pb, p(* equality *)) ->
136 let pb' = replace_proof newproof pb in
137 ProofGoalBlock (pb', p(* equality *))
138 (* let w, proof, t, menv, args = equality in *)
139 (* let proof' = replace_proof newproof proof in *)
140 (* ProofGoalBlock (pb, (w, proof', t, menv, args)) *)
141 | BasicProof _ -> newproof
142 | SubProof (term, meta_index, p) ->
143 SubProof (term, meta_index, replace_proof newproof p)
146 (* let _, proof, _, _, _ = equality in *)
151 let rec metas_of_term = function
152 | Cic.Meta (i, c) -> [i]
155 | Cic.MutInd (_, _, ens)
156 | Cic.MutConstruct (_, _, _, ens) ->
157 List.flatten (List.map (fun (u, t) -> metas_of_term t) ens)
160 | Cic.Lambda (_, s, t)
161 | Cic.LetIn (_, s, t) -> (metas_of_term s) @ (metas_of_term t)
162 | Cic.Appl l -> List.flatten (List.map metas_of_term l)
163 | Cic.MutCase (uri, i, s, t, l) ->
164 (metas_of_term s) @ (metas_of_term t) @
165 (List.flatten (List.map metas_of_term l))
168 (List.map (fun (s, i, t1, t2) ->
169 (metas_of_term t1) @ (metas_of_term t2)) il)
170 | Cic.CoFix (i, il) ->
172 (List.map (fun (s, t1, t2) ->
173 (metas_of_term t1) @ (metas_of_term t2)) il)
178 exception NotMetaConvertible;;
180 let meta_convertibility_aux table t1 t2 =
181 let module C = Cic in
185 (fun (k, v) -> Printf.sprintf "(%d, %d)" k v) t)
187 let rec aux ((table_l, table_r) as table) t1 t2 =
188 (* Printf.printf "aux %s, %s\ntable_l: %s, table_r: %s\n" *)
189 (* (CicPp.ppterm t1) (CicPp.ppterm t2) *)
190 (* (print_table table_l) (print_table table_r); *)
192 | C.Meta (m1, tl1), C.Meta (m2, tl2) ->
193 let m1_binding, table_l =
194 try List.assoc m1 table_l, table_l
195 with Not_found -> m2, (m1, m2)::table_l
196 and m2_binding, table_r =
197 try List.assoc m2 table_r, table_r
198 with Not_found -> m1, (m2, m1)::table_r
200 (* let m1_binding, m2_binding, table = *)
201 (* let m1b, table = *)
202 (* try List.assoc m1 table, table *)
203 (* with Not_found -> m2, (m1, m2)::table *)
205 (* let m2b, table = *)
206 (* try List.assoc m2 table, table *)
207 (* with Not_found -> m1, (m2, m1)::table *)
209 (* m1b, m2b, table *)
211 (* Printf.printf "table_l: %s\ntable_r: %s\n\n" *)
212 (* (print_table table_l) (print_table table_r); *)
213 if (m1_binding <> m2) || (m2_binding <> m1) then
214 raise NotMetaConvertible
220 | None, Some _ | Some _, None -> raise NotMetaConvertible
222 | Some t1, Some t2 -> (aux res t1 t2))
223 (table_l, table_r) tl1 tl2
224 with Invalid_argument _ ->
225 raise NotMetaConvertible
227 | C.Var (u1, ens1), C.Var (u2, ens2)
228 | C.Const (u1, ens1), C.Const (u2, ens2) when (UriManager.eq u1 u2) ->
229 aux_ens table ens1 ens2
230 | C.Cast (s1, t1), C.Cast (s2, t2)
231 | C.Prod (_, s1, t1), C.Prod (_, s2, t2)
232 | C.Lambda (_, s1, t1), C.Lambda (_, s2, t2)
233 | C.LetIn (_, s1, t1), C.LetIn (_, s2, t2) ->
234 let table = aux table s1 s2 in
236 | C.Appl l1, C.Appl l2 -> (
237 try List.fold_left2 (fun res t1 t2 -> (aux res t1 t2)) table l1 l2
238 with Invalid_argument _ -> raise NotMetaConvertible
240 | C.MutInd (u1, i1, ens1), C.MutInd (u2, i2, ens2)
241 when (UriManager.eq u1 u2) && i1 = i2 -> aux_ens table ens1 ens2
242 | C.MutConstruct (u1, i1, j1, ens1), C.MutConstruct (u2, i2, j2, ens2)
243 when (UriManager.eq u1 u2) && i1 = i2 && j1 = j2 ->
244 aux_ens table ens1 ens2
245 | C.MutCase (u1, i1, s1, t1, l1), C.MutCase (u2, i2, s2, t2, l2)
246 when (UriManager.eq u1 u2) && i1 = i2 ->
247 let table = aux table s1 s2 in
248 let table = aux table t1 t2 in (
249 try List.fold_left2 (fun res t1 t2 -> (aux res t1 t2)) table l1 l2
250 with Invalid_argument _ -> raise NotMetaConvertible
252 | C.Fix (i1, il1), C.Fix (i2, il2) when i1 = i2 -> (
255 (fun res (n1, i1, s1, t1) (n2, i2, s2, t2) ->
256 if i1 <> i2 then raise NotMetaConvertible
258 let res = (aux res s1 s2) in aux res t1 t2)
260 with Invalid_argument _ -> raise NotMetaConvertible
262 | C.CoFix (i1, il1), C.CoFix (i2, il2) when i1 = i2 -> (
265 (fun res (n1, s1, t1) (n2, s2, t2) ->
266 let res = aux res s1 s2 in aux res t1 t2)
268 with Invalid_argument _ -> raise NotMetaConvertible
270 | t1, t2 when t1 = t2 -> table
271 | _, _ -> raise NotMetaConvertible
273 and aux_ens table ens1 ens2 =
274 let cmp (u1, t1) (u2, t2) =
275 compare (UriManager.string_of_uri u1) (UriManager.string_of_uri u2)
277 let ens1 = List.sort cmp ens1
278 and ens2 = List.sort cmp ens2 in
281 (fun res (u1, t1) (u2, t2) ->
282 if not (UriManager.eq u1 u2) then raise NotMetaConvertible
285 with Invalid_argument _ -> raise NotMetaConvertible
291 let meta_convertibility_eq eq1 eq2 =
292 let _, _, (ty, left, right, _), _, _ = eq1
293 and _, _, (ty', left', right', _), _, _ = eq2 in
296 else if (left = left') && (right = right') then
298 else if (left = right') && (right = left') then
302 let table = meta_convertibility_aux ([], []) left left' in
303 let _ = meta_convertibility_aux table right right' in
305 with NotMetaConvertible ->
307 let table = meta_convertibility_aux ([], []) left right' in
308 let _ = meta_convertibility_aux table right left' in
310 with NotMetaConvertible ->
315 let meta_convertibility t1 t2 =
319 (fun (k, v) -> Printf.sprintf "(%d, %d)" k v) t)
325 let l, r = meta_convertibility_aux ([], []) t1 t2 in
326 (* Printf.printf "meta_convertibility:\n%s\n%s\n\n" (f l) (f r); *)
328 with NotMetaConvertible ->
334 let replace_metas (* context *) term =
335 let module C = Cic in
336 let rec aux = function
339 (* CicMkImplicit.identity_relocation_list_for_metavariable context *)
341 (* if c = irl then *)
342 (* C.Implicit (Some (`MetaIndex i)) *)
344 (* Printf.printf "WARNING: c non e` un identity_relocation_list!\n%s\n" *)
345 (* (String.concat "\n" *)
347 (* (function None -> "" | Some t -> CicPp.ppterm t) c)); *)
350 C.Implicit (Some (`MetaInfo (i, c)))
351 | C.Var (u, ens) -> C.Var (u, aux_ens ens)
352 | C.Const (u, ens) -> C.Const (u, aux_ens ens)
353 | C.Cast (s, t) -> C.Cast (aux s, aux t)
354 | C.Prod (name, s, t) -> C.Prod (name, aux s, aux t)
355 | C.Lambda (name, s, t) -> C.Lambda (name, aux s, aux t)
356 | C.LetIn (name, s, t) -> C.LetIn (name, aux s, aux t)
357 | C.Appl l -> C.Appl (List.map aux l)
358 | C.MutInd (uri, i, ens) -> C.MutInd (uri, i, aux_ens ens)
359 | C.MutConstruct (uri, i, j, ens) -> C.MutConstruct (uri, i, j, aux_ens ens)
360 | C.MutCase (uri, i, s, t, l) ->
361 C.MutCase (uri, i, aux s, aux t, List.map aux l)
364 List.map (fun (s, i, t1, t2) -> (s, i, aux t1, aux t2)) il in
368 List.map (fun (s, t1, t2) -> (s, aux t1, aux t2)) il in
372 List.map (fun (u, t) -> (u, aux t)) ens
380 let restore_metas (* context *) term =
381 let module C = Cic in
382 let rec aux = function
383 | C.Implicit (Some (`MetaInfo (i, c))) ->
385 (* CicMkImplicit.identity_relocation_list_for_metavariable context *)
388 (* let local_context:(C.term option) list = *)
389 (* Marshal.from_string mc 0 *)
391 (* C.Meta (i, local_context) *)
393 | C.Var (u, ens) -> C.Var (u, aux_ens ens)
394 | C.Const (u, ens) -> C.Const (u, aux_ens ens)
395 | C.Cast (s, t) -> C.Cast (aux s, aux t)
396 | C.Prod (name, s, t) -> C.Prod (name, aux s, aux t)
397 | C.Lambda (name, s, t) -> C.Lambda (name, aux s, aux t)
398 | C.LetIn (name, s, t) -> C.LetIn (name, aux s, aux t)
399 | C.Appl l -> C.Appl (List.map aux l)
400 | C.MutInd (uri, i, ens) -> C.MutInd (uri, i, aux_ens ens)
401 | C.MutConstruct (uri, i, j, ens) -> C.MutConstruct (uri, i, j, aux_ens ens)
402 | C.MutCase (uri, i, s, t, l) ->
403 C.MutCase (uri, i, aux s, aux t, List.map aux l)
406 List.map (fun (s, i, t1, t2) -> (s, i, aux t1, aux t2)) il in
410 List.map (fun (s, t1, t2) -> (s, aux t1, aux t2)) il in
414 List.map (fun (u, t) -> (u, aux t)) ens
421 let rec restore_subst (* context *) subst =
423 (fun (i, (c, t, ty)) ->
424 i, (c, restore_metas (* context *) t, ty))
430 let rec check_irl start = function
432 | None::tl -> check_irl (start+1) tl
433 | (Some (Cic.Rel x))::tl ->
434 if x = start then check_irl (start+1) tl else false
438 let rec is_simple_term = function
439 | Cic.Appl ((Cic.Meta _)::_) -> false
440 | Cic.Appl l -> List.for_all is_simple_term l
441 | Cic.Meta (i, l) -> check_irl 1 l
443 | Cic.Const _ -> true
444 | Cic.MutInd (_, _, []) -> true
445 | Cic.MutConstruct (_, _, _, []) -> true
450 let lookup_subst meta subst =
452 | Cic.Meta (i, _) -> (
453 try let _, (_, t, _) = List.find (fun (m, _) -> m = i) subst in t
454 with Not_found -> meta
460 let unification_simple metasenv context t1 t2 ugraph =
461 let module C = Cic in
462 let module M = CicMetaSubst in
463 let module U = CicUnification in
464 let lookup = lookup_subst in
465 let rec occurs_check subst what where =
467 | t when what = t -> true
468 | C.Appl l -> List.exists (occurs_check subst what) l
470 let t = lookup where subst in
471 if t <> where then occurs_check subst what t else false
474 let rec unif subst menv s t =
475 let s = match s with C.Meta _ -> lookup s subst | _ -> s
476 and t = match t with C.Meta _ -> lookup t subst | _ -> t
479 | s, t when s = t -> subst, menv
480 | C.Meta (i, _), C.Meta (j, _) when i > j ->
482 | C.Meta _, t when occurs_check subst s t ->
484 (U.UnificationFailure
485 (U.failure_msg_of_string "Inference.unification.unif"))
486 | C.Meta (i, l), t -> (
488 let _, _, ty = CicUtil.lookup_meta i menv in
490 if not (List.mem_assoc i subst) then (i, (context, t, ty))::subst
493 let menv = menv in (* List.filter (fun (m, _, _) -> i <> m) menv in *)
495 with CicUtil.Meta_not_found m ->
496 let names = names_of_context context in
499 (Printf.sprintf "Meta_not_found %d!: %s %s\n%s\n\n%s" m
500 (CicPp.pp t1 names) (CicPp.pp t2 names)
501 (print_metasenv menv) (print_metasenv metasenv)));
504 | _, C.Meta _ -> unif subst menv t s
505 | C.Appl (hds::_), C.Appl (hdt::_) when hds <> hdt ->
506 raise (U.UnificationFailure
507 (U.failure_msg_of_string "Inference.unification.unif"))
508 | C.Appl (hds::tls), C.Appl (hdt::tlt) -> (
511 (fun (subst', menv) s t -> unif subst' menv s t)
512 (subst, menv) tls tlt
513 with Invalid_argument _ ->
514 raise (U.UnificationFailure
515 (U.failure_msg_of_string "Inference.unification.unif"))
518 raise (U.UnificationFailure
519 (U.failure_msg_of_string "Inference.unification.unif"))
521 let subst, menv = unif [] metasenv t1 t2 in
525 try let _ = List.find (fun (i, _) -> m = i) subst in false
526 with Not_found -> true)
529 List.rev subst, menv, ugraph
533 let unification metasenv context t1 t2 ugraph =
534 (* Printf.printf "| unification %s %s\n" (CicPp.ppterm t1) (CicPp.ppterm t2); *)
535 let subst, menv, ug =
536 if not (is_simple_term t1) || not (is_simple_term t2) then (
539 (Printf.sprintf "NOT SIMPLE TERMS: %s %s"
540 (CicPp.ppterm t1) (CicPp.ppterm t2)));
541 CicUnification.fo_unif metasenv context t1 t2 ugraph
543 unification_simple metasenv context t1 t2 ugraph
545 let rec fix_term = function
546 | (Cic.Meta (i, l) as t) ->
547 let t' = lookup_subst t subst in
548 if t <> t' then fix_term t' else t
549 | Cic.Appl l -> Cic.Appl (List.map fix_term l)
552 let rec fix_subst = function
554 | (i, (c, t, ty))::tl -> (i, (c, fix_term t, fix_term ty))::(fix_subst tl)
556 (* Printf.printf "| subst: %s\n" (print_subst ~prefix:" ; " subst); *)
557 (* print_endline "|"; *)
558 fix_subst subst, menv, ug
562 (* let unification = CicUnification.fo_unif;; *)
564 exception MatchingFailure;;
567 let matching_simple metasenv context t1 t2 ugraph =
568 let module C = Cic in
569 let module M = CicMetaSubst in
570 let module U = CicUnification in
571 let lookup meta subst =
574 try let _, (_, t, _) = List.find (fun (m, _) -> m = i) subst in t
575 with Not_found -> meta
579 let rec do_match subst menv s t =
580 (* Printf.printf "do_match %s %s\n%s\n" (CicPp.ppterm s) (CicPp.ppterm t) *)
581 (* (print_subst subst); *)
582 (* print_newline (); *)
583 (* let s = match s with C.Meta _ -> lookup s subst | _ -> s *)
584 (* let t = match t with C.Meta _ -> lookup t subst | _ -> t in *)
585 (* Printf.printf "after apply_subst: %s %s\n%s" *)
586 (* (CicPp.ppterm s) (CicPp.ppterm t) (print_subst subst); *)
587 (* print_newline (); *)
589 | s, t when s = t -> subst, menv
590 (* | C.Meta (i, _), C.Meta (j, _) when i > j -> *)
591 (* do_match subst menv t s *)
592 (* | C.Meta _, t when occurs_check subst s t -> *)
593 (* raise MatchingFailure *)
594 (* | s, C.Meta _ when occurs_check subst t s -> *)
595 (* raise MatchingFailure *)
596 | s, C.Meta (i, l) ->
597 let filter_menv i menv =
598 List.filter (fun (m, _, _) -> i <> m) menv
601 let value = lookup t subst in
603 (* | C.Meta (i', l') when Hashtbl.mem table i' -> *)
604 (* (i', (context, s, ty))::subst, menv (\* filter_menv i' menv *\) *)
605 | value when value = t ->
606 let _, _, ty = CicUtil.lookup_meta i menv in
607 (i, (context, s, ty))::subst, filter_menv i menv
608 | value when value <> s ->
609 raise MatchingFailure
610 | value -> do_match subst menv s value
613 (* else if value <> s then *)
614 (* raise MatchingFailure *)
616 (* if not (List.mem_assoc i subst) then (i, (context, t, ty))::subst *)
619 (* let menv = List.filter (fun (m, _, _) -> i <> m) menv in *)
621 (* | _, C.Meta _ -> do_match subst menv t s *)
622 (* | C.Appl (hds::_), C.Appl (hdt::_) when hds <> hdt -> *)
623 (* raise MatchingFailure *)
624 | C.Appl ls, C.Appl lt -> (
627 (fun (subst, menv) s t -> do_match subst menv s t)
629 with Invalid_argument _ ->
630 (* print_endline (Printexc.to_string e); *)
631 (* Printf.printf "NO MATCH: %s %s\n" (CicPp.ppterm s) (CicPp.ppterm t); *)
632 (* print_newline (); *)
633 raise MatchingFailure
636 (* Printf.printf "NO MATCH: %s %s\n" (CicPp.ppterm s) (CicPp.ppterm t); *)
637 (* print_newline (); *)
638 raise MatchingFailure
640 let subst, menv = do_match [] metasenv t1 t2 in
641 (* Printf.printf "DONE!: subst = \n%s\n" (print_subst subst); *)
642 (* print_newline (); *)
647 let matching metasenv context t1 t2 ugraph =
648 (* if (is_simple_term t1) && (is_simple_term t2) then *)
649 (* let subst, menv, ug = *)
650 (* matching_simple metasenv context t1 t2 ugraph in *)
651 (* (\* Printf.printf "matching %s %s:\n%s\n" *\) *)
652 (* (\* (CicPp.ppterm t1) (CicPp.ppterm t2) (print_subst subst); *\) *)
653 (* (\* print_newline (); *\) *)
654 (* subst, menv, ug *)
657 (* (Printf.sprintf "matching %s %s" (CicPp.ppterm t1) (CicPp.ppterm t2)); *)
658 (* print_newline (); *)
660 let subst, metasenv, ugraph =
661 (* CicUnification.fo_unif metasenv context t1 t2 ugraph *)
662 unification metasenv context t1 t2 ugraph
664 let t' = CicMetaSubst.apply_subst subst t1 in
665 if not (meta_convertibility t1 t') then
666 raise MatchingFailure
668 let metas = metas_of_term t1 in
669 let fix_subst = function
670 | (i, (c, Cic.Meta (j, lc), ty)) when List.mem i metas ->
671 (j, (c, Cic.Meta (i, lc), ty))
674 let subst = List.map fix_subst subst in
676 (* Printf.printf "matching %s %s:\n%s\n" *)
677 (* (CicPp.ppterm t1) (CicPp.ppterm t2) (print_subst subst); *)
678 (* print_newline (); *)
680 subst, metasenv, ugraph
682 | CicUnification.UnificationFailure _
683 | CicUnification.Uncertain _ ->
684 (* Printf.printf "failed to match %s %s\n" *)
685 (* (CicPp.ppterm t1) (CicPp.ppterm t2); *)
686 (* print_endline (Printexc.to_string e); *)
687 raise MatchingFailure
691 (* let profile = CicUtil.profile "Inference.matching" in *)
692 (* (fun metasenv context t1 t2 ugraph -> *)
693 (* profile (matching metasenv context t1 t2) ugraph) *)
697 let beta_expand ?(metas_ok=true) ?(match_only=false)
698 what type_of_what where context metasenv ugraph =
699 let module S = CicSubstitution in
700 let module C = Cic in
703 (* let names = names_of_context context in *)
704 (* Printf.printf "beta_expand:\nwhat: %s, %s\nwhere: %s, %s\n" *)
705 (* (CicPp.pp what names) (CicPp.ppterm what) *)
706 (* (CicPp.pp where names) (CicPp.ppterm where); *)
707 (* print_newline (); *)
711 ((list of all possible beta expansions, subst, metasenv, ugraph),
714 let rec aux lift_amount term context metasenv subst ugraph =
715 (* Printf.printf "enter aux %s\n" (CicPp.ppterm term); *)
716 let res, lifted_term =
719 [], if m <= lift_amount then C.Rel m else C.Rel (m+1)
721 | C.Var (uri, exp_named_subst) ->
722 let ens', lifted_ens =
723 aux_ens lift_amount exp_named_subst context metasenv subst ugraph
727 (fun (e, s, m, ug) ->
728 (C.Var (uri, e), s, m, ug)) ens'
730 expansions, C.Var (uri, lifted_ens)
735 (fun arg (res, lifted_tl) ->
738 let arg_res, lifted_arg =
739 aux lift_amount arg context metasenv subst ugraph in
742 (fun (a, s, m, ug) -> (Some a)::lifted_tl, s, m, ug)
747 (fun (r, s, m, ug) -> (Some lifted_arg)::r, s, m, ug)
749 (Some lifted_arg)::lifted_tl)
752 (fun (r, s, m, ug) -> None::r, s, m, ug)
759 (fun (l, s, m, ug) ->
760 (C.Meta (i, l), s, m, ug)) l'
762 e, C.Meta (i, lifted_l)
765 | C.Implicit _ as t -> [], t
769 aux lift_amount s context metasenv subst ugraph in
771 aux lift_amount t context metasenv subst ugraph
775 (fun (t, s, m, ug) ->
776 C.Cast (t, lifted_t), s, m, ug) l1 in
779 (fun (t, s, m, ug) ->
780 C.Cast (lifted_s, t), s, m, ug) l2 in
781 l1'@l2', C.Cast (lifted_s, lifted_t)
783 | C.Prod (nn, s, t) ->
785 aux lift_amount s context metasenv subst ugraph in
787 aux (lift_amount+1) t ((Some (nn, C.Decl s))::context)
788 metasenv subst ugraph
792 (fun (t, s, m, ug) ->
793 C.Prod (nn, t, lifted_t), s, m, ug) l1 in
796 (fun (t, s, m, ug) ->
797 C.Prod (nn, lifted_s, t), s, m, ug) l2 in
798 l1'@l2', C.Prod (nn, lifted_s, lifted_t)
800 | C.Lambda (nn, s, t) ->
802 aux lift_amount s context metasenv subst ugraph in
804 aux (lift_amount+1) t ((Some (nn, C.Decl s))::context)
805 metasenv subst ugraph
809 (fun (t, s, m, ug) ->
810 C.Lambda (nn, t, lifted_t), s, m, ug) l1 in
813 (fun (t, s, m, ug) ->
814 C.Lambda (nn, lifted_s, t), s, m, ug) l2 in
815 l1'@l2', C.Lambda (nn, lifted_s, lifted_t)
817 | C.LetIn (nn, s, t) ->
819 aux lift_amount s context metasenv subst ugraph in
821 aux (lift_amount+1) t ((Some (nn, C.Def (s, None)))::context)
822 metasenv subst ugraph
826 (fun (t, s, m, ug) ->
827 C.LetIn (nn, t, lifted_t), s, m, ug) l1 in
830 (fun (t, s, m, ug) ->
831 C.LetIn (nn, lifted_s, t), s, m, ug) l2 in
832 l1'@l2', C.LetIn (nn, lifted_s, lifted_t)
836 aux_list lift_amount l context metasenv subst ugraph
838 (List.map (fun (l, s, m, ug) -> (C.Appl l, s, m, ug)) l',
841 | C.Const (uri, exp_named_subst) ->
842 let ens', lifted_ens =
843 aux_ens lift_amount exp_named_subst context metasenv subst ugraph
847 (fun (e, s, m, ug) ->
848 (C.Const (uri, e), s, m, ug)) ens'
850 (expansions, C.Const (uri, lifted_ens))
852 | C.MutInd (uri, i ,exp_named_subst) ->
853 let ens', lifted_ens =
854 aux_ens lift_amount exp_named_subst context metasenv subst ugraph
858 (fun (e, s, m, ug) ->
859 (C.MutInd (uri, i, e), s, m, ug)) ens'
861 (expansions, C.MutInd (uri, i, lifted_ens))
863 | C.MutConstruct (uri, i, j, exp_named_subst) ->
864 let ens', lifted_ens =
865 aux_ens lift_amount exp_named_subst context metasenv subst ugraph
869 (fun (e, s, m, ug) ->
870 (C.MutConstruct (uri, i, j, e), s, m, ug)) ens'
872 (expansions, C.MutConstruct (uri, i, j, lifted_ens))
874 | C.MutCase (sp, i, outt, t, pl) ->
875 let pl_res, lifted_pl =
876 aux_list lift_amount pl context metasenv subst ugraph
878 let l1, lifted_outt =
879 aux lift_amount outt context metasenv subst ugraph in
881 aux lift_amount t context metasenv subst ugraph in
885 (fun (outt, s, m, ug) ->
886 C.MutCase (sp, i, outt, lifted_t, lifted_pl), s, m, ug) l1 in
889 (fun (t, s, m, ug) ->
890 C.MutCase (sp, i, lifted_outt, t, lifted_pl), s, m, ug) l2 in
893 (fun (pl, s, m, ug) ->
894 C.MutCase (sp, i, lifted_outt, lifted_t, pl), s, m, ug) pl_res
896 (l1'@l2'@l3', C.MutCase (sp, i, lifted_outt, lifted_t, lifted_pl))
899 let len = List.length fl in
902 (fun (nm, idx, ty, bo) (res, lifted_tl) ->
903 let lifted_ty = S.lift lift_amount ty in
904 let bo_res, lifted_bo =
905 aux (lift_amount+len) bo context metasenv subst ugraph in
908 (fun (a, s, m, ug) ->
909 (nm, idx, lifted_ty, a)::lifted_tl, s, m, ug)
914 (fun (r, s, m, ug) ->
915 (nm, idx, lifted_ty, lifted_bo)::r, s, m, ug) res),
916 (nm, idx, lifted_ty, lifted_bo)::lifted_tl)
920 (fun (fl, s, m, ug) -> C.Fix (i, fl), s, m, ug) fl',
921 C.Fix (i, lifted_fl))
924 let len = List.length fl in
927 (fun (nm, ty, bo) (res, lifted_tl) ->
928 let lifted_ty = S.lift lift_amount ty in
929 let bo_res, lifted_bo =
930 aux (lift_amount+len) bo context metasenv subst ugraph in
933 (fun (a, s, m, ug) ->
934 (nm, lifted_ty, a)::lifted_tl, s, m, ug)
939 (fun (r, s, m, ug) ->
940 (nm, lifted_ty, lifted_bo)::r, s, m, ug) res),
941 (nm, lifted_ty, lifted_bo)::lifted_tl)
945 (fun (fl, s, m, ug) -> C.CoFix (i, fl), s, m, ug) fl',
946 C.CoFix (i, lifted_fl))
950 | C.Meta _ when (not metas_ok) ->
954 (* if match_only then replace_metas context term *)
958 let subst', metasenv', ugraph' =
959 (* Printf.printf "provo a unificare %s e %s\n" *)
960 (* (CicPp.ppterm (S.lift lift_amount what)) (CicPp.ppterm term); *)
962 matching metasenv context term (S.lift lift_amount what) ugraph
964 CicUnification.fo_unif metasenv context
965 (S.lift lift_amount what) term ugraph
967 (* Printf.printf "Ok, trovato: %s\n\nwhat: %s" (CicPp.ppterm term) *)
968 (* (CicPp.ppterm (S.lift lift_amount what)); *)
969 (* Printf.printf "substitution:\n%s\n\n" (print_subst subst'); *)
970 (* Printf.printf "metasenv': %s\n" (print_metasenv metasenv'); *)
971 (* Printf.printf "metasenv: %s\n\n" (print_metasenv metasenv); *)
972 (* if match_only then *)
973 (* let t' = CicMetaSubst.apply_subst subst' term in *)
974 (* if not (meta_convertibility term t') then ( *)
975 (* res, lifted_term *)
977 (* let metas = metas_of_term term in *)
978 (* let fix_subst = function *)
979 (* | (i, (c, C.Meta (j, lc), ty)) when List.mem i metas -> *)
980 (* (j, (c, C.Meta (i, lc), ty)) *)
983 (* let subst' = List.map fix_subst subst' in *)
984 (* ((C.Rel (1 + lift_amount), subst', metasenv', ugraph')::res, *)
988 ((C.Rel (1 + lift_amount), subst', metasenv', ugraph')::res,
992 | CicUnification.UnificationFailure _
993 | CicUnification.Uncertain _ ->
996 (* Printf.printf "exit aux\n"; *)
999 and aux_list lift_amount l context metasenv subst ugraph =
1001 (fun arg (res, lifted_tl) ->
1002 let arg_res, lifted_arg =
1003 aux lift_amount arg context metasenv subst ugraph in
1005 (fun (a, s, m, ug) -> a::lifted_tl, s, m, ug) arg_res
1008 (fun (r, s, m, ug) -> lifted_arg::r, s, m, ug) res),
1009 lifted_arg::lifted_tl)
1012 and aux_ens lift_amount exp_named_subst context metasenv subst ugraph =
1014 (fun (u, arg) (res, lifted_tl) ->
1015 let arg_res, lifted_arg =
1016 aux lift_amount arg context metasenv subst ugraph in
1019 (fun (a, s, m, ug) -> (u, a)::lifted_tl, s, m, ug) arg_res
1021 (l1 @ (List.map (fun (r, s, m, ug) ->
1022 (u, lifted_arg)::r, s, m, ug) res),
1023 (u, lifted_arg)::lifted_tl)
1024 ) exp_named_subst ([], [])
1029 (* if match_only then replace_metas (\* context *\) where *)
1032 aux 0 where context metasenv [] ugraph
1035 (* if match_only then *)
1036 (* (fun (term, subst, metasenv, ugraph) -> *)
1038 (* C.Lambda (C.Anonymous, type_of_what, restore_metas term) *)
1039 (* and subst = restore_subst subst in *)
1040 (* (term', subst, metasenv, ugraph)) *)
1042 (fun (term, subst, metasenv, ugraph) ->
1043 let term' = C.Lambda (C.Anonymous, type_of_what, term) in
1044 (term', subst, metasenv, ugraph))
1046 List.map mapfun expansions
1050 let find_equalities context proof =
1051 let module C = Cic in
1052 let module S = CicSubstitution in
1053 let module T = CicTypeChecker in
1054 let eq_uri = LibraryObjects.eq_URI () in
1055 let newmeta = ProofEngineHelpers.new_meta_of_proof ~proof in
1056 let ok_types ty menv =
1057 List.for_all (fun (_, _, mt) -> mt = ty) menv
1059 let rec aux index newmeta = function
1061 | (Some (_, C.Decl (term)))::tl ->
1062 let do_find context term =
1064 | C.Prod (name, s, t) ->
1065 (* let newmeta = ProofEngineHelpers.new_meta_of_proof ~proof in *)
1066 let (head, newmetas, args, newmeta) =
1067 ProofEngineHelpers.saturate_term newmeta []
1068 context (S.lift index term) 0
1071 if List.length args = 0 then
1074 C.Appl ((C.Rel index)::args)
1077 | C.Appl [C.MutInd (uri, _, _); ty; t1; t2]
1078 when (UriManager.eq uri eq_uri) && (ok_types ty newmetas) ->
1081 (Printf.sprintf "OK: %s" (CicPp.ppterm term)));
1083 (* Printf.sprintf "args: %s\n" *)
1084 (* (String.concat ", " (List.map CicPp.ppterm args)))); *)
1085 (* debug_print (lazy ( *)
1086 (* Printf.sprintf "newmetas:\n%s\n" *)
1087 (* (print_metasenv newmetas))); *)
1088 let o = !Utils.compare_terms t1 t2 in
1089 let w = compute_equality_weight ty t1 t2 in
1090 let proof = BasicProof p in
1091 let e = (w, proof, (ty, t1, t2, o), newmetas, args) in
1093 | _ -> None, newmeta
1095 | C.Appl [C.MutInd (uri, _, _); ty; t1; t2]
1096 when UriManager.eq uri eq_uri ->
1097 let t1 = S.lift index t1
1098 and t2 = S.lift index t2 in
1099 let o = !Utils.compare_terms t1 t2 in
1100 let w = compute_equality_weight ty t1 t2 in
1101 let e = (w, BasicProof (C.Rel index), (ty, t1, t2, o), [], []) in
1103 | _ -> None, newmeta
1105 match do_find context term with
1106 | Some p, newmeta ->
1107 let tl, newmeta' = (aux (index+1) newmeta tl) in
1108 (index, p)::tl, max newmeta newmeta'
1110 aux (index+1) newmeta tl
1113 aux (index+1) newmeta tl
1115 let il, maxm = aux 1 newmeta context in
1116 let indexes, equalities = List.split il in
1117 indexes, equalities, maxm
1121 let equations_blacklist =
1123 (fun s u -> UriManager.UriSet.add (UriManager.uri_of_string u) s)
1124 UriManager.UriSet.empty [
1125 "cic:/Coq/Init/Logic/eq.ind#xpointer(1/1/1)";
1126 "cic:/Coq/Init/Logic/trans_eq.con";
1127 "cic:/Coq/Init/Logic/f_equal.con";
1128 "cic:/Coq/Init/Logic/f_equal2.con";
1129 "cic:/Coq/Init/Logic/f_equal3.con";
1130 "cic:/Coq/Init/Logic/f_equal4.con";
1131 "cic:/Coq/Init/Logic/f_equal5.con";
1132 "cic:/Coq/Init/Logic/sym_eq.con";
1133 "cic:/Coq/Init/Logic/eq_ind.con";
1134 "cic:/Coq/Init/Logic/eq_ind_r.con";
1135 "cic:/Coq/Init/Logic/eq_rec.con";
1136 "cic:/Coq/Init/Logic/eq_rec_r.con";
1137 "cic:/Coq/Init/Logic/eq_rect.con";
1138 "cic:/Coq/Init/Logic/eq_rect_r.con";
1139 "cic:/Coq/Logic/Eqdep/UIP.con";
1140 "cic:/Coq/Logic/Eqdep/UIP_refl.con";
1141 "cic:/Coq/Logic/Eqdep_dec/eq2eqT.con";
1142 "cic:/Coq/ZArith/Zcompare/rename.con";
1143 (* ALB !!!! questo e` imbrogliare, ma x ora lo lasciamo cosi`...
1144 perche' questo cacchio di teorema rompe le scatole :'( *)
1145 "cic:/Rocq/SUBST/comparith/mult_n_2.con";
1149 let find_library_equalities dbd context status maxmeta =
1150 let module C = Cic in
1151 let module S = CicSubstitution in
1152 let module T = CicTypeChecker in
1155 (fun s u -> UriManager.UriSet.add u s)
1157 [eq_XURI (); sym_eq_URI (); trans_eq_URI (); eq_ind_URI ();
1163 let suri = UriManager.string_of_uri uri in
1164 if UriManager.UriSet.mem uri blacklist then
1167 let t = CicUtil.term_of_uri uri in
1169 CicTypeChecker.type_of_aux' [] context t CicUniv.empty_ugraph
1173 (MetadataQuery.equations_for_goal ~dbd status)
1175 let eq_uri1 = eq_XURI () (* UriManager.uri_of_string HelmLibraryObjects.Logic.eq_XURI *)
1176 and eq_uri2 = LibraryObjects.eq_URI () in (* HelmLibraryObjects.Logic.eq_URI in *)
1178 (UriManager.eq uri eq_uri1) || (UriManager.eq uri eq_uri2)
1180 let ok_types ty menv =
1181 List.for_all (fun (_, _, mt) -> mt = ty) menv
1183 let rec aux newmeta = function
1185 | (uri, term, termty)::tl ->
1188 (Printf.sprintf "Examining: %s (%s)"
1189 (CicPp.ppterm term) (CicPp.ppterm termty)));
1192 | C.Prod (name, s, t) ->
1193 let head, newmetas, args, newmeta =
1194 ProofEngineHelpers.saturate_term newmeta [] context termty 0
1197 if List.length args = 0 then
1203 | C.Appl [C.MutInd (uri, _, _); ty; t1; t2]
1204 when (iseq uri) && (ok_types ty newmetas) ->
1207 (Printf.sprintf "OK: %s" (CicPp.ppterm term)));
1208 let o = !Utils.compare_terms t1 t2 in
1209 let w = compute_equality_weight ty t1 t2 in
1210 let proof = BasicProof p in
1211 let e = (w, proof, (ty, t1, t2, o), newmetas, args) in
1213 | _ -> None, newmeta
1215 | C.Appl [C.MutInd (uri, _, _); ty; t1; t2] when iseq uri ->
1216 let o = !Utils.compare_terms t1 t2 in
1217 let w = compute_equality_weight ty t1 t2 in
1218 let e = (w, BasicProof term, (ty, t1, t2, o), [], []) in
1220 | _ -> None, newmeta
1224 let tl, newmeta' = aux newmeta tl in
1225 (uri, e)::tl, max newmeta newmeta'
1229 let found, maxm = aux maxmeta candidates in
1230 let uriset, eqlist =
1232 (fun (s, l) (u, e) ->
1233 if List.exists (meta_convertibility_eq e) l then (
1236 (Printf.sprintf "NO!! %s already there!"
1237 (string_of_equality e)));
1238 (UriManager.UriSet.add u s, l)
1239 ) else (UriManager.UriSet.add u s, e::l))
1240 (UriManager.UriSet.empty, []) found)
1242 uriset, eqlist, maxm
1246 let find_library_theorems dbd env status equalities_uris =
1247 let module C = Cic in
1248 let module S = CicSubstitution in
1249 let module T = CicTypeChecker in
1252 UriManager.uri_of_string "cic:/Coq/Init/Logic/eq.ind#xpointer(1/1/1)" in
1254 UriManager.UriSet.remove refl_equal
1255 (UriManager.UriSet.union equalities_uris equations_blacklist)
1258 (fun s u -> UriManager.UriSet.add u s)
1259 s [sym_eq_URI (); trans_eq_URI (); eq_ind_URI (); eq_ind_r_URI ()]
1261 let metasenv, context, ugraph = env in
1265 if UriManager.UriSet.mem uri blacklist then l
1267 let t = CicUtil.term_of_uri uri in
1268 let ty, _ = CicTypeChecker.type_of_aux' metasenv context t ugraph in
1270 [] (MetadataQuery.signature_of_goal ~dbd status)
1276 let find_context_hypotheses env equalities_indexes =
1277 let metasenv, context, ugraph = env in
1280 (fun (n, l) entry ->
1284 if List.mem n equalities_indexes then
1287 let t = Cic.Rel n in
1289 CicTypeChecker.type_of_aux' metasenv context t ugraph in
1290 (n+1, (t, ty, [])::l))
1297 let fix_metas newmeta ((w, p, (ty, left, right, o), menv, args) as equality) =
1298 (* print_endline ("fix_metas " ^ (string_of_int newmeta)); *)
1299 let table = Hashtbl.create (List.length args) in
1300 let is_this_case = ref false in
1301 let newargs, newmeta =
1303 (fun t (newargs, index) ->
1305 | Cic.Meta (i, l) ->
1306 if Hashtbl.mem table i then
1307 let idx = Hashtbl.find table i in
1308 ((Cic.Meta (idx, l))::newargs, index+1)
1310 let _ = Hashtbl.add table i index in
1311 ((Cic.Meta (index, l))::newargs, index+1)
1312 | _ -> assert false)
1313 args ([], newmeta+1)
1316 ProofEngineReduction.replace ~equality:(=) ~what:args ~with_what:newargs
1321 (fun (i, context, term) menv ->
1323 let index = Hashtbl.find table i in
1324 (index, context, term)::menv
1326 (i, context, term)::menv)
1330 and left = repl left
1331 and right = repl right in
1332 let metas = (metas_of_term left) @ (metas_of_term right) in
1333 let menv' = List.filter (fun (i, _, _) -> List.mem i metas) menv' in
1336 (function Cic.Meta (i, _) -> List.mem i metas | _ -> assert false) newargs
1339 if List.length metas > 0 then
1340 let first = List.hd metas in
1344 (function Cic.Meta (i, _) -> i = v | _ -> assert false)
1346 Hashtbl.replace table k first)
1347 (Hashtbl.copy table)
1349 let rec fix_proof = function
1350 | NoProof -> NoProof
1351 | BasicProof term -> BasicProof (repl term)
1352 | ProofBlock (subst, eq_URI, namety, bo(* t' *), (pos, eq), p) ->
1354 (* Printf.printf "fix_proof of equality %s, subst is:\n%s\n" *)
1355 (* (string_of_equality equality) (print_subst subst); *)
1357 (* debug_print "table is:"; *)
1359 (* (fun k v -> debug_print (Printf.sprintf "%d: %d" k v)) *)
1365 | Cic.Meta (i, l) -> (
1367 let j = Hashtbl.find table i in
1368 if List.mem_assoc i subst then
1371 let _, context, ty = CicUtil.lookup_meta i menv in
1372 (i, (context, Cic.Meta (j, l), ty))::s
1374 (* debug_print ("Not_found meta ?" ^ (string_of_int i)); *)
1377 | _ -> assert false)
1381 (* Printf.printf "subst' is:\n%s\n" (print_subst subst'); *)
1382 (* print_newline (); *)
1384 ProofBlock (subst' @ subst, eq_URI, namety, bo(* t' *), (pos, eq), p)
1387 let neweq = (w, fix_proof p, (ty, left, right, o), menv', newargs) in
1388 (newmeta + 1, neweq)
1392 let term_is_equality term =
1393 let iseq uri = UriManager.eq uri (LibraryObjects.eq_URI ()) in
1395 | Cic.Appl [Cic.MutInd (uri, _, _); _; _; _] when iseq uri -> true
1400 exception TermIsNotAnEquality;;
1402 let equality_of_term proof term =
1403 let eq_uri = LibraryObjects.eq_URI () in
1404 let iseq uri = UriManager.eq uri eq_uri in
1406 | Cic.Appl [Cic.MutInd (uri, _, _); ty; t1; t2] when iseq uri ->
1407 let o = !Utils.compare_terms t1 t2 in
1408 let w = compute_equality_weight ty t1 t2 in
1409 let e = (w, BasicProof proof, (ty, t1, t2, o), [], []) in
1411 (* (proof, (ty, t1, t2, o), [], []) *)
1413 raise TermIsNotAnEquality
1417 type environment = Cic.metasenv * Cic.context * CicUniv.universe_graph;;
1421 let superposition_left (metasenv, context, ugraph) target source =
1422 let module C = Cic in
1423 let module S = CicSubstitution in
1424 let module M = CicMetaSubst in
1425 let module HL = HelmLibraryObjects in
1426 let module CR = CicReduction in
1427 (* we assume that target is ground (does not contain metavariables): this
1428 * should always be the case (I hope, at least) *)
1429 let proof, (eq_ty, left, right, t_order), _, _ = target in
1430 let eqproof, (ty, t1, t2, s_order), newmetas, args = source in
1432 let compare_terms = !Utils.compare_terms in
1437 let where, is_left =
1438 match t_order (* compare_terms left right *) with
1439 | Lt -> right, false
1442 Printf.printf "????????? %s = %s" (CicPp.ppterm left)
1443 (CicPp.ppterm right);
1445 assert false (* again, for ground terms this shouldn't happen... *)
1448 let metasenv' = newmetas @ metasenv in
1449 let result = s_order (* compare_terms t1 t2 *) in
1452 | Gt -> (beta_expand t1 ty where context metasenv' ugraph), []
1453 | Lt -> [], (beta_expand t2 ty where context metasenv' ugraph)
1457 (fun (t, s, m, ug) ->
1458 compare_terms (M.apply_subst s t1) (M.apply_subst s t2) = Gt)
1459 (beta_expand t1 ty where context metasenv' ugraph)
1462 (fun (t, s, m, ug) ->
1463 compare_terms (M.apply_subst s t2) (M.apply_subst s t1) = Gt)
1464 (beta_expand t2 ty where context metasenv' ugraph)
1468 (* let what, other = *)
1469 (* if is_left then left, right *)
1470 (* else right, left *)
1472 let build_new what other eq_URI (t, s, m, ug) =
1473 let newgoal, newgoalproof =
1475 | C.Lambda (nn, ty, bo) ->
1476 let bo' = S.subst (M.apply_subst s other) bo in
1479 [C.MutInd (HL.Logic.eq_URI, 0, []);
1481 if is_left then [bo'; S.lift 1 right]
1482 else [S.lift 1 left; bo'])
1484 let t' = C.Lambda (nn, ty, bo'') in
1485 S.subst (M.apply_subst s other) bo,
1487 (C.Appl [C.Const (eq_URI, []); ty; what; t';
1488 proof; other; eqproof])
1492 if is_left then (eq_ty, newgoal, right, compare_terms newgoal right)
1493 else (eq_ty, left, newgoal, compare_terms left newgoal)
1495 (newgoalproof (* eqproof *), equation, [], [])
1497 let new1 = List.map (build_new t1 t2 HL.Logic.eq_ind_URI) res1
1498 and new2 = List.map (build_new t2 t1 HL.Logic.eq_ind_r_URI) res2 in
1503 let superposition_right newmeta (metasenv, context, ugraph) target source =
1504 let module C = Cic in
1505 let module S = CicSubstitution in
1506 let module M = CicMetaSubst in
1507 let module HL = HelmLibraryObjects in
1508 let module CR = CicReduction in
1509 let eqproof, (eq_ty, left, right, t_order), newmetas, args = target in
1510 let eqp', (ty', t1, t2, s_order), newm', args' = source in
1511 let maxmeta = ref newmeta in
1513 let compare_terms = !Utils.compare_terms in
1515 if eq_ty <> ty' then
1518 (* let ok term subst other other_eq_side ugraph = *)
1519 (* match term with *)
1520 (* | C.Lambda (nn, ty, bo) -> *)
1521 (* let bo' = S.subst (M.apply_subst subst other) bo in *)
1522 (* let res, _ = CR.are_convertible context bo' other_eq_side ugraph in *)
1524 (* | _ -> assert false *)
1526 let condition left right what other (t, s, m, ug) =
1527 let subst = M.apply_subst s in
1528 let cmp1 = compare_terms (subst what) (subst other) in
1529 let cmp2 = compare_terms (subst left) (subst right) in
1530 (* cmp1 = Gt && cmp2 = Gt *)
1531 cmp1 <> Lt && cmp1 <> Le && cmp2 <> Lt && cmp2 <> Le
1532 (* && (ok t s other right ug) *)
1534 let metasenv' = metasenv @ newmetas @ newm' in
1535 let beta_expand = beta_expand ~metas_ok:false in
1536 let cmp1 = t_order (* compare_terms left right *)
1537 and cmp2 = s_order (* compare_terms t1 t2 *) in
1538 let res1, res2, res3, res4 =
1542 (beta_expand s eq_ty l context metasenv' ugraph)
1544 match cmp1, cmp2 with
1546 (beta_expand t1 eq_ty left context metasenv' ugraph), [], [], []
1548 [], (beta_expand t2 eq_ty left context metasenv' ugraph), [], []
1550 [], [], (beta_expand t1 eq_ty right context metasenv' ugraph), []
1552 [], [], [], (beta_expand t2 eq_ty right context metasenv' ugraph)
1554 let res1 = res left right t1 t2
1555 and res2 = res left right t2 t1 in
1558 let res3 = res right left t1 t2
1559 and res4 = res right left t2 t1 in
1562 let res1 = res left right t1 t2
1563 and res3 = res right left t1 t2 in
1566 let res2 = res left right t2 t1
1567 and res4 = res right left t2 t1 in
1570 let res1 = res left right t1 t2
1571 and res2 = res left right t2 t1
1572 and res3 = res right left t1 t2
1573 and res4 = res right left t2 t1 in
1574 res1, res2, res3, res4
1576 let newmetas = newmetas @ newm' in
1577 let newargs = args @ args' in
1578 let build_new what other is_left eq_URI (t, s, m, ug) =
1579 (* let what, other = *)
1580 (* if is_left then left, right *)
1581 (* else right, left *)
1583 let newterm, neweqproof =
1585 | C.Lambda (nn, ty, bo) ->
1586 let bo' = M.apply_subst s (S.subst other bo) in
1589 [C.MutInd (HL.Logic.eq_URI, 0, []); S.lift 1 eq_ty] @
1590 if is_left then [bo'; S.lift 1 right]
1591 else [S.lift 1 left; bo'])
1593 let t' = C.Lambda (nn, ty, bo'') in
1596 (C.Appl [C.Const (eq_URI, []); ty; what; t';
1597 eqproof; other; eqp'])
1600 let newmeta, newequality =
1602 if is_left then (newterm, M.apply_subst s right)
1603 else (M.apply_subst s left, newterm) in
1604 let neworder = compare_terms left right in
1606 (neweqproof, (eq_ty, left, right, neworder), newmetas, newargs)
1611 let new1 = List.map (build_new t1 t2 true HL.Logic.eq_ind_URI) res1
1612 and new2 = List.map (build_new t2 t1 true HL.Logic.eq_ind_r_URI) res2
1613 and new3 = List.map (build_new t1 t2 false HL.Logic.eq_ind_URI) res3
1614 and new4 = List.map (build_new t2 t1 false HL.Logic.eq_ind_r_URI) res4 in
1616 | _, (_, left, right, _), _, _ ->
1617 not (fst (CR.are_convertible context left right ugraph))
1620 (List.filter ok (new1 @ new2 @ new3 @ new4)))
1625 let is_identity ((_, context, ugraph) as env) = function
1626 | ((_, _, (ty, left, right, _), _, _) as equality) ->
1628 (meta_convertibility left right) ||
1629 (fst (CicReduction.are_convertible context left right ugraph)))
1634 let demodulation newmeta (metasenv, context, ugraph) target source =
1635 let module C = Cic in
1636 let module S = CicSubstitution in
1637 let module M = CicMetaSubst in
1638 let module HL = HelmLibraryObjects in
1639 let module CR = CicReduction in
1641 let proof, (eq_ty, left, right, t_order), metas, args = target
1642 and proof', (ty, t1, t2, s_order), metas', args' = source in
1644 let compare_terms = !Utils.compare_terms in
1649 let first_step, get_params =
1650 match s_order (* compare_terms t1 t2 *) with
1651 | Gt -> 1, (function
1652 | 1 -> true, t1, t2, HL.Logic.eq_ind_URI
1653 | 0 -> false, t1, t2, HL.Logic.eq_ind_URI
1654 | _ -> assert false)
1655 | Lt -> 1, (function
1656 | 1 -> true, t2, t1, HL.Logic.eq_ind_r_URI
1657 | 0 -> false, t2, t1, HL.Logic.eq_ind_r_URI
1658 | _ -> assert false)
1660 let first_step = 3 in
1661 let get_params step =
1663 | 3 -> true, t1, t2, HL.Logic.eq_ind_URI
1664 | 2 -> false, t1, t2, HL.Logic.eq_ind_URI
1665 | 1 -> true, t2, t1, HL.Logic.eq_ind_r_URI
1666 | 0 -> false, t2, t1, HL.Logic.eq_ind_r_URI
1669 first_step, get_params
1671 let rec demodulate newmeta step metasenv target =
1672 let proof, (eq_ty, left, right, t_order), metas, args = target in
1673 let is_left, what, other, eq_URI = get_params step in
1675 let env = metasenv, context, ugraph in
1676 let names = names_of_context context in
1678 (* "demodulate\ntarget: %s\nwhat: %s\nother: %s\nis_left: %s\n" *)
1679 (* (string_of_equality ~env target) (CicPp.pp what names) *)
1680 (* (CicPp.pp other names) (string_of_bool is_left); *)
1681 (* Printf.printf "step: %d" step; *)
1682 (* print_newline (); *)
1684 let ok (t, s, m, ug) =
1685 compare_terms (M.apply_subst s what) (M.apply_subst s other) = Gt
1688 let r = (beta_expand ~metas_ok:false ~match_only:true
1689 what ty (if is_left then left else right)
1690 context (metasenv @ metas) ugraph)
1692 (* let m' = metas_of_term what *)
1693 (* and m'' = metas_of_term (if is_left then left else right) in *)
1694 (* if (List.mem 527 m'') && (List.mem 6 m') then ( *)
1696 (* "demodulate\ntarget: %s\nwhat: %s\nother: %s\nis_left: %s\n" *)
1697 (* (string_of_equality ~env target) (CicPp.pp what names) *)
1698 (* (CicPp.pp other names) (string_of_bool is_left); *)
1699 (* Printf.printf "step: %d" step; *)
1700 (* print_newline (); *)
1701 (* print_endline "res:"; *)
1702 (* List.iter (fun (t, s, m, ug) -> print_endline (CicPp.pp t names)) r; *)
1703 (* print_newline (); *)
1704 (* Printf.printf "metasenv:\n%s\n" (print_metasenv (metasenv @ metas)); *)
1705 (* print_newline (); *)
1711 if step = 0 then newmeta, target
1712 else demodulate newmeta (step-1) metasenv target
1713 | (t, s, m, ug)::_ ->
1714 let newterm, newproof =
1716 | C.Lambda (nn, ty, bo) ->
1717 (* let bo' = M.apply_subst s (S.subst other bo) in *)
1718 let bo' = S.subst (M.apply_subst s other) bo in
1721 [C.MutInd (HL.Logic.eq_URI, 0, []);
1723 if is_left then [bo'; S.lift 1 right]
1724 else [S.lift 1 left; bo'])
1726 let t' = C.Lambda (nn, ty, bo'') in
1727 (* M.apply_subst s (S.subst other bo), *)
1730 (C.Appl [C.Const (eq_URI, []); ty; what; t';
1731 proof; other; proof'])
1734 let newmeta, newtarget =
1736 (* if is_left then (newterm, M.apply_subst s right) *)
1737 (* else (M.apply_subst s left, newterm) in *)
1738 if is_left then newterm, right
1741 let neworder = compare_terms left right in
1742 (* let newmetasenv = metasenv @ metas in *)
1743 (* let newargs = args @ args' in *)
1744 (* fix_metas newmeta *)
1745 (* (newproof, (eq_ty, left, right), newmetasenv, newargs) *)
1746 let m = (metas_of_term left) @ (metas_of_term right) in
1747 let newmetasenv = List.filter (fun (i, _, _) -> List.mem i m) metas
1750 (function C.Meta (i, _) -> List.mem i m | _ -> assert false)
1754 (newproof, (eq_ty, left, right, neworder), newmetasenv, newargs)
1757 (* "demodulate, newtarget: %s\ntarget was: %s\n" *)
1758 (* (string_of_equality ~env newtarget) *)
1759 (* (string_of_equality ~env target); *)
1760 (* (\* let _, _, newm, newa = newtarget in *\) *)
1761 (* (\* Printf.printf "newmetasenv:\n%s\nnewargs:\n%s\n" *\) *)
1762 (* (\* (print_metasenv newm) *\) *)
1763 (* (\* (String.concat "\n" (List.map CicPp.ppterm newa)); *\) *)
1764 (* print_newline (); *)
1765 if is_identity env newtarget then
1768 demodulate newmeta first_step metasenv newtarget
1770 demodulate newmeta first_step (metasenv @ metas') target
1775 let demodulation newmeta env target source =
1781 let subsumption env target source =
1782 let _, (ty, tl, tr, _), tmetas, _ = target
1783 and _, (ty', sl, sr, _), smetas, _ = source in
1787 let metasenv, context, ugraph = env in
1788 let metasenv = metasenv @ tmetas @ smetas in
1789 let names = names_of_context context in
1790 let samesubst subst subst' =
1791 (* Printf.printf "samesubst:\nsubst: %s\nsubst': %s\n" *)
1792 (* (print_subst subst) (print_subst subst'); *)
1793 (* print_newline (); *)
1794 let tbl = Hashtbl.create (List.length subst) in
1795 List.iter (fun (m, (c, t1, t2)) -> Hashtbl.add tbl m (c, t1, t2)) subst;
1797 (fun (m, (c, t1, t2)) ->
1799 let c', t1', t2' = Hashtbl.find tbl m in
1800 if (c = c') && (t1 = t1') && (t2 = t2') then true
1806 let subsaux left right left' right' =
1808 let subst, menv, ug = matching metasenv context left left' ugraph
1809 and subst', menv', ug' = matching metasenv context right right' ugraph
1811 (* Printf.printf "left = right: %s = %s\n" *)
1812 (* (CicPp.pp left names) (CicPp.pp right names); *)
1813 (* Printf.printf "left' = right': %s = %s\n" *)
1814 (* (CicPp.pp left' names) (CicPp.pp right' names); *)
1815 samesubst subst subst'
1817 (* print_endline (Printexc.to_string e); *)
1821 if subsaux tl tr sl sr then true
1822 else subsaux tl tr sr sl
1825 Printf.printf "subsumption!:\ntarget: %s\nsource: %s\n"
1826 (string_of_equality ~env target) (string_of_equality ~env source);
1834 let extract_differing_subterms t1 t2 =
1835 let module C = Cic in
1838 | C.Appl l1, C.Appl l2 when (List.length l1) <> (List.length l2) ->
1840 | C.Appl (h1::tl1), C.Appl (h2::tl2) ->
1841 let res = List.concat (List.map2 aux tl1 tl2) in
1843 if res = [] then [(h1, h2)] else [(t1, t2)]
1845 if List.length res > 1 then [(t1, t2)] else res
1847 if t1 <> t2 then [(t1, t2)] else []
1849 let res = aux t1 t2 in
1856 let rec string_of_proof = function
1857 | NoProof -> "NoProof"
1858 | BasicProof t -> "BasicProof " ^ (CicPp.ppterm t)
1859 | SubProof (t, i, p) ->
1860 Printf.sprintf "SubProof(%s, %s, %s)"
1861 (CicPp.ppterm t) (string_of_int i) (string_of_proof p)
1862 | ProofSymBlock _ -> "ProofSymBlock"
1863 | ProofBlock _ -> "ProofBlock"
1864 | ProofGoalBlock (p1, p2) ->
1865 Printf.sprintf "ProofGoalBlock(%s, %s)"
1866 (string_of_proof p1) (string_of_proof p2)