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
28 let string_of_equality ?env =
32 | w, _, (ty, left, right, o), _, _ ->
33 Printf.sprintf "Weight: %d, {%s}: %s =(%s) %s" w (CicPp.ppterm ty)
34 (CicPp.ppterm left) (string_of_comparison o) (CicPp.ppterm right)
36 | Some (_, context, _) -> (
37 let names = names_of_context context in
39 | w, _, (ty, left, right, o), _, _ ->
40 Printf.sprintf "Weight: %d, {%s}: %s =(%s) %s" w (CicPp.pp ty names)
41 (CicPp.pp left names) (string_of_comparison o)
42 (CicPp.pp right names)
47 let build_proof_term equality =
48 (* Printf.printf "build_term_proof %s" (string_of_equality equality); *)
49 (* print_newline (); *)
53 let rec do_build_proof proof =
56 Printf.fprintf stderr "WARNING: no proof!\n";
57 (* (string_of_equality equality); *)
59 | BasicProof term -> term
60 | ProofGoalBlock (proofbit, proof (* equality *)) ->
61 print_endline "found ProofGoalBlock, going up...";
62 (* let _, proof, _, _, _ = equality in *)
63 do_build_goal_proof proofbit proof
64 | ProofSymBlock (ens, proof) ->
65 let proof = do_build_proof proof in
67 Cic.Const (HelmLibraryObjects.Logic.sym_eq_URI, ens); (* symmetry *)
70 | ProofBlock (subst, eq_URI, (name, ty), bo(* t' *), (pos, eq), eqproof) ->
71 (* Printf.printf "\nsubst:\n%s\n" (print_subst subst); *)
72 (* print_newline (); *)
74 (* let name, ty, eq_ty, left, right = t' in *)
76 (* Cic.Appl [Cic.MutInd (HelmLibraryObjects.Logic.eq_URI, 0, []); *)
77 (* eq_ty; left; right] *)
79 let t' = Cic.Lambda (name, ty, bo) in
80 (* Printf.printf " ProofBlock: eq = %s, eq' = %s" *)
81 (* (string_of_equality eq) (string_of_equality eq'); *)
82 (* print_newline (); *)
84 (* let s = String.make !indent ' ' in *)
87 (* print_endline (s ^ "build proof'------------"); *)
90 let _, proof', _, _, _ = eq in
93 (* print_endline (s ^ "END proof'"); *)
95 (* print_endline (s ^ "build eqproof-----------"); *)
97 let eqproof = do_build_proof eqproof in
99 (* print_endline (s ^ "END eqproof"); *)
103 let _, _, (ty, what, other, _), menv', args' = eq in
105 if pos = Utils.Left then what, other else other, what
107 CicMetaSubst.apply_subst subst
108 (Cic.Appl [Cic.Const (eq_URI, []); ty;
109 what; t'; eqproof; other; proof'])
111 and do_build_goal_proof proofbit proof =
112 (* match proofbit with *)
113 (* | BasicProof _ -> do_build_proof proof *)
116 | ProofGoalBlock (pb, p(* eq *)) ->
117 do_build_proof (ProofGoalBlock (replace_proof proofbit pb, p(* eq *)))
118 (* let _, proof, _, _, _ = eq in *)
119 (* let newproof = replace_proof proofbit proof in *)
120 (* do_build_proof newproof *)
122 (* | ProofBlock (subst, eq_URI, t', poseq, eqproof) -> *)
123 (* let eqproof' = replace_proof proofbit eqproof in *)
124 (* do_build_proof (ProofBlock (subst, eq_URI, t', poseq, eqproof')) *)
125 | _ -> do_build_proof (replace_proof proofbit proof) (* assert false *)
127 and replace_proof newproof = function
128 | ProofBlock (subst, eq_URI, namety, bo(* t' *), poseq, eqproof) ->
129 let eqproof' = replace_proof newproof eqproof in
130 ProofBlock (subst, eq_URI, namety, bo(* t' *), poseq, eqproof')
131 | ProofGoalBlock (pb, p(* equality *)) ->
132 let pb' = replace_proof newproof pb in
133 ProofGoalBlock (pb', p(* equality *))
134 (* let w, proof, t, menv, args = equality in *)
135 (* let proof' = replace_proof newproof proof in *)
136 (* ProofGoalBlock (pb, (w, proof', t, menv, args)) *)
137 | BasicProof _ -> newproof
140 let _, proof, _, _, _ = equality in
145 let rec metas_of_term = function
146 | Cic.Meta (i, c) -> [i]
149 | Cic.MutInd (_, _, ens)
150 | Cic.MutConstruct (_, _, _, ens) ->
151 List.flatten (List.map (fun (u, t) -> metas_of_term t) ens)
154 | Cic.Lambda (_, s, t)
155 | Cic.LetIn (_, s, t) -> (metas_of_term s) @ (metas_of_term t)
156 | Cic.Appl l -> List.flatten (List.map metas_of_term l)
157 | Cic.MutCase (uri, i, s, t, l) ->
158 (metas_of_term s) @ (metas_of_term t) @
159 (List.flatten (List.map metas_of_term l))
162 (List.map (fun (s, i, t1, t2) ->
163 (metas_of_term t1) @ (metas_of_term t2)) il)
164 | Cic.CoFix (i, il) ->
166 (List.map (fun (s, t1, t2) ->
167 (metas_of_term t1) @ (metas_of_term t2)) il)
172 exception NotMetaConvertible;;
174 let meta_convertibility_aux table t1 t2 =
175 let module C = Cic in
179 (fun (k, v) -> Printf.sprintf "(%d, %d)" k v) t)
181 let rec aux ((table_l, table_r) as table) t1 t2 =
182 (* Printf.printf "aux %s, %s\ntable_l: %s, table_r: %s\n" *)
183 (* (CicPp.ppterm t1) (CicPp.ppterm t2) *)
184 (* (print_table table_l) (print_table table_r); *)
186 | C.Meta (m1, tl1), C.Meta (m2, tl2) ->
187 let m1_binding, table_l =
188 try List.assoc m1 table_l, table_l
189 with Not_found -> m2, (m1, m2)::table_l
190 and m2_binding, table_r =
191 try List.assoc m2 table_r, table_r
192 with Not_found -> m1, (m2, m1)::table_r
194 (* let m1_binding, m2_binding, table = *)
195 (* let m1b, table = *)
196 (* try List.assoc m1 table, table *)
197 (* with Not_found -> m2, (m1, m2)::table *)
199 (* let m2b, table = *)
200 (* try List.assoc m2 table, table *)
201 (* with Not_found -> m1, (m2, m1)::table *)
203 (* m1b, m2b, table *)
205 (* Printf.printf "table_l: %s\ntable_r: %s\n\n" *)
206 (* (print_table table_l) (print_table table_r); *)
207 if (m1_binding <> m2) || (m2_binding <> m1) then
208 raise NotMetaConvertible
214 | None, Some _ | Some _, None -> raise NotMetaConvertible
216 | Some t1, Some t2 -> (aux res t1 t2))
217 (table_l, table_r) tl1 tl2
218 with Invalid_argument _ ->
219 raise NotMetaConvertible
221 | C.Var (u1, ens1), C.Var (u2, ens2)
222 | C.Const (u1, ens1), C.Const (u2, ens2) when (UriManager.eq u1 u2) ->
223 aux_ens table ens1 ens2
224 | C.Cast (s1, t1), C.Cast (s2, t2)
225 | C.Prod (_, s1, t1), C.Prod (_, s2, t2)
226 | C.Lambda (_, s1, t1), C.Lambda (_, s2, t2)
227 | C.LetIn (_, s1, t1), C.LetIn (_, s2, t2) ->
228 let table = aux table s1 s2 in
230 | C.Appl l1, C.Appl l2 -> (
231 try List.fold_left2 (fun res t1 t2 -> (aux res t1 t2)) table l1 l2
232 with Invalid_argument _ -> raise NotMetaConvertible
234 | C.MutInd (u1, i1, ens1), C.MutInd (u2, i2, ens2)
235 when (UriManager.eq u1 u2) && i1 = i2 -> aux_ens table ens1 ens2
236 | C.MutConstruct (u1, i1, j1, ens1), C.MutConstruct (u2, i2, j2, ens2)
237 when (UriManager.eq u1 u2) && i1 = i2 && j1 = j2 ->
238 aux_ens table ens1 ens2
239 | C.MutCase (u1, i1, s1, t1, l1), C.MutCase (u2, i2, s2, t2, l2)
240 when (UriManager.eq u1 u2) && i1 = i2 ->
241 let table = aux table s1 s2 in
242 let table = aux table t1 t2 in (
243 try List.fold_left2 (fun res t1 t2 -> (aux res t1 t2)) table l1 l2
244 with Invalid_argument _ -> raise NotMetaConvertible
246 | C.Fix (i1, il1), C.Fix (i2, il2) when i1 = i2 -> (
249 (fun res (n1, i1, s1, t1) (n2, i2, s2, t2) ->
250 if i1 <> i2 then raise NotMetaConvertible
252 let res = (aux res s1 s2) in aux res t1 t2)
254 with Invalid_argument _ -> raise NotMetaConvertible
256 | C.CoFix (i1, il1), C.CoFix (i2, il2) when i1 = i2 -> (
259 (fun res (n1, s1, t1) (n2, s2, t2) ->
260 let res = aux res s1 s2 in aux res t1 t2)
262 with Invalid_argument _ -> raise NotMetaConvertible
264 | t1, t2 when t1 = t2 -> table
265 | _, _ -> raise NotMetaConvertible
267 and aux_ens table ens1 ens2 =
268 let cmp (u1, t1) (u2, t2) =
269 compare (UriManager.string_of_uri u1) (UriManager.string_of_uri u2)
271 let ens1 = List.sort cmp ens1
272 and ens2 = List.sort cmp ens2 in
275 (fun res (u1, t1) (u2, t2) ->
276 if not (UriManager.eq u1 u2) then raise NotMetaConvertible
279 with Invalid_argument _ -> raise NotMetaConvertible
285 let meta_convertibility_eq eq1 eq2 =
286 let _, _, (ty, left, right, _), _, _ = eq1
287 and _, _, (ty', left', right', _), _, _ = eq2 in
290 else if (left = left') && (right = right') then
292 else if (left = right') && (right = left') then
296 let table = meta_convertibility_aux ([], []) left left' in
297 let _ = meta_convertibility_aux table right right' in
299 with NotMetaConvertible ->
301 let table = meta_convertibility_aux ([], []) left right' in
302 let _ = meta_convertibility_aux table right left' in
304 with NotMetaConvertible ->
309 let meta_convertibility t1 t2 =
313 (fun (k, v) -> Printf.sprintf "(%d, %d)" k v) t)
319 let l, r = meta_convertibility_aux ([], []) t1 t2 in
320 (* Printf.printf "meta_convertibility:\n%s\n%s\n\n" (f l) (f r); *)
322 with NotMetaConvertible ->
328 let replace_metas (* context *) term =
329 let module C = Cic in
330 let rec aux = function
333 (* CicMkImplicit.identity_relocation_list_for_metavariable context *)
335 (* if c = irl then *)
336 (* C.Implicit (Some (`MetaIndex i)) *)
338 (* Printf.printf "WARNING: c non e` un identity_relocation_list!\n%s\n" *)
339 (* (String.concat "\n" *)
341 (* (function None -> "" | Some t -> CicPp.ppterm t) c)); *)
344 C.Implicit (Some (`MetaInfo (i, c)))
345 | C.Var (u, ens) -> C.Var (u, aux_ens ens)
346 | C.Const (u, ens) -> C.Const (u, aux_ens ens)
347 | C.Cast (s, t) -> C.Cast (aux s, aux t)
348 | C.Prod (name, s, t) -> C.Prod (name, aux s, aux t)
349 | C.Lambda (name, s, t) -> C.Lambda (name, aux s, aux t)
350 | C.LetIn (name, s, t) -> C.LetIn (name, aux s, aux t)
351 | C.Appl l -> C.Appl (List.map aux l)
352 | C.MutInd (uri, i, ens) -> C.MutInd (uri, i, aux_ens ens)
353 | C.MutConstruct (uri, i, j, ens) -> C.MutConstruct (uri, i, j, aux_ens ens)
354 | C.MutCase (uri, i, s, t, l) ->
355 C.MutCase (uri, i, aux s, aux t, List.map aux l)
358 List.map (fun (s, i, t1, t2) -> (s, i, aux t1, aux t2)) il in
362 List.map (fun (s, t1, t2) -> (s, aux t1, aux t2)) il in
366 List.map (fun (u, t) -> (u, aux t)) ens
374 let restore_metas (* context *) term =
375 let module C = Cic in
376 let rec aux = function
377 | C.Implicit (Some (`MetaInfo (i, c))) ->
379 (* CicMkImplicit.identity_relocation_list_for_metavariable context *)
382 (* let local_context:(C.term option) list = *)
383 (* Marshal.from_string mc 0 *)
385 (* C.Meta (i, local_context) *)
387 | C.Var (u, ens) -> C.Var (u, aux_ens ens)
388 | C.Const (u, ens) -> C.Const (u, aux_ens ens)
389 | C.Cast (s, t) -> C.Cast (aux s, aux t)
390 | C.Prod (name, s, t) -> C.Prod (name, aux s, aux t)
391 | C.Lambda (name, s, t) -> C.Lambda (name, aux s, aux t)
392 | C.LetIn (name, s, t) -> C.LetIn (name, aux s, aux t)
393 | C.Appl l -> C.Appl (List.map aux l)
394 | C.MutInd (uri, i, ens) -> C.MutInd (uri, i, aux_ens ens)
395 | C.MutConstruct (uri, i, j, ens) -> C.MutConstruct (uri, i, j, aux_ens ens)
396 | C.MutCase (uri, i, s, t, l) ->
397 C.MutCase (uri, i, aux s, aux t, List.map aux l)
400 List.map (fun (s, i, t1, t2) -> (s, i, aux t1, aux t2)) il in
404 List.map (fun (s, t1, t2) -> (s, aux t1, aux t2)) il in
408 List.map (fun (u, t) -> (u, aux t)) ens
415 let rec restore_subst (* context *) subst =
417 (fun (i, (c, t, ty)) ->
418 i, (c, restore_metas (* context *) t, ty))
424 let rec check_irl start = function
426 | None::tl -> check_irl (start+1) tl
427 | (Some (Cic.Rel x))::tl ->
428 if x = start then check_irl (start+1) tl else false
432 let rec is_simple_term = function
433 | Cic.Appl ((Cic.Meta _)::_) -> false
434 | Cic.Appl l -> List.for_all is_simple_term l
435 | Cic.Meta (i, l) -> check_irl 1 l
437 | Cic.Const _ -> true
438 | Cic.MutInd (_, _, []) -> true
439 | Cic.MutConstruct (_, _, _, []) -> true
444 let lookup_subst meta subst =
446 | Cic.Meta (i, _) -> (
447 try let _, (_, t, _) = List.find (fun (m, _) -> m = i) subst in t
448 with Not_found -> meta
454 let unification_simple metasenv context t1 t2 ugraph =
455 let module C = Cic in
456 let module M = CicMetaSubst in
457 let module U = CicUnification in
458 let lookup = lookup_subst in
459 let rec occurs_check subst what where =
461 | t when what = t -> true
462 | C.Appl l -> List.exists (occurs_check subst what) l
464 let t = lookup where subst in
465 if t <> where then occurs_check subst what t else false
468 let rec unif subst menv s t =
469 let s = match s with C.Meta _ -> lookup s subst | _ -> s
470 and t = match t with C.Meta _ -> lookup t subst | _ -> t
473 | s, t when s = t -> subst, menv
474 | C.Meta (i, _), C.Meta (j, _) when i > j ->
476 | C.Meta _, t when occurs_check subst s t ->
477 raise (U.UnificationFailure (U.failure_msg_of_string "Inference.unification.unif"))
478 | C.Meta (i, l), t -> (
480 let _, _, ty = CicUtil.lookup_meta i menv in
482 if not (List.mem_assoc i subst) then (i, (context, t, ty))::subst
485 let menv = menv in (* List.filter (fun (m, _, _) -> i <> m) menv in *)
487 with CicUtil.Meta_not_found m ->
488 let names = names_of_context context in
490 Printf.sprintf "Meta_not_found %d!: %s %s\n%s\n\n%s" m
491 (CicPp.pp t1 names) (CicPp.pp t2 names)
492 (print_metasenv menv) (print_metasenv metasenv)));
495 | _, C.Meta _ -> unif subst menv t s
496 | C.Appl (hds::_), C.Appl (hdt::_) when hds <> hdt ->
497 raise (U.UnificationFailure (U.failure_msg_of_string "Inference.unification.unif"))
498 | C.Appl (hds::tls), C.Appl (hdt::tlt) -> (
501 (fun (subst', menv) s t -> unif subst' menv s t)
502 (subst, menv) tls tlt
503 with Invalid_argument _ ->
504 raise (U.UnificationFailure (U.failure_msg_of_string "Inference.unification.unif"))
506 | _, _ -> raise (U.UnificationFailure (U.failure_msg_of_string "Inference.unification.unif"))
508 let subst, menv = unif [] metasenv t1 t2 in
512 try let _ = List.find (fun (i, _) -> m = i) subst in false
513 with Not_found -> true)
516 List.rev subst, menv, ugraph
520 let unification metasenv context t1 t2 ugraph =
521 (* Printf.printf "| unification %s %s\n" (CicPp.ppterm t1) (CicPp.ppterm t2); *)
522 let subst, menv, ug =
523 if not (is_simple_term t1) || not (is_simple_term t2) then (
525 Printf.sprintf "NOT SIMPLE TERMS: %s %s"
526 (CicPp.ppterm t1) (CicPp.ppterm t2)));
527 CicUnification.fo_unif metasenv context t1 t2 ugraph
529 unification_simple metasenv context t1 t2 ugraph
531 let rec fix_term = function
532 | (Cic.Meta (i, l) as t) ->
533 let t' = lookup_subst t subst in
534 if t <> t' then fix_term t' else t
535 | Cic.Appl l -> Cic.Appl (List.map fix_term l)
538 let rec fix_subst = function
540 | (i, (c, t, ty))::tl -> (i, (c, fix_term t, fix_term ty))::(fix_subst tl)
542 (* Printf.printf "| subst: %s\n" (print_subst ~prefix:" ; " subst); *)
543 (* print_endline "|"; *)
544 fix_subst subst, menv, ug
548 (* let unification = CicUnification.fo_unif;; *)
550 exception MatchingFailure;;
553 let matching_simple metasenv context t1 t2 ugraph =
554 let module C = Cic in
555 let module M = CicMetaSubst in
556 let module U = CicUnification in
557 let lookup meta subst =
560 try let _, (_, t, _) = List.find (fun (m, _) -> m = i) subst in t
561 with Not_found -> meta
565 let rec do_match subst menv s t =
566 (* Printf.printf "do_match %s %s\n%s\n" (CicPp.ppterm s) (CicPp.ppterm t) *)
567 (* (print_subst subst); *)
568 (* print_newline (); *)
569 (* let s = match s with C.Meta _ -> lookup s subst | _ -> s *)
570 (* let t = match t with C.Meta _ -> lookup t subst | _ -> t in *)
571 (* Printf.printf "after apply_subst: %s %s\n%s" *)
572 (* (CicPp.ppterm s) (CicPp.ppterm t) (print_subst subst); *)
573 (* print_newline (); *)
575 | s, t when s = t -> subst, menv
576 (* | C.Meta (i, _), C.Meta (j, _) when i > j -> *)
577 (* do_match subst menv t s *)
578 (* | C.Meta _, t when occurs_check subst s t -> *)
579 (* raise MatchingFailure *)
580 (* | s, C.Meta _ when occurs_check subst t s -> *)
581 (* raise MatchingFailure *)
582 | s, C.Meta (i, l) ->
583 let filter_menv i menv =
584 List.filter (fun (m, _, _) -> i <> m) menv
587 let value = lookup t subst in
589 (* | C.Meta (i', l') when Hashtbl.mem table i' -> *)
590 (* (i', (context, s, ty))::subst, menv (\* filter_menv i' menv *\) *)
591 | value when value = t ->
592 let _, _, ty = CicUtil.lookup_meta i menv in
593 (i, (context, s, ty))::subst, filter_menv i menv
594 | value when value <> s ->
595 raise MatchingFailure
596 | value -> do_match subst menv s value
599 (* else if value <> s then *)
600 (* raise MatchingFailure *)
602 (* if not (List.mem_assoc i subst) then (i, (context, t, ty))::subst *)
605 (* let menv = List.filter (fun (m, _, _) -> i <> m) menv in *)
607 (* | _, C.Meta _ -> do_match subst menv t s *)
608 (* | C.Appl (hds::_), C.Appl (hdt::_) when hds <> hdt -> *)
609 (* raise MatchingFailure *)
610 | C.Appl ls, C.Appl lt -> (
613 (fun (subst, menv) s t -> do_match subst menv s t)
615 with Invalid_argument _ ->
616 (* print_endline (Printexc.to_string e); *)
617 (* Printf.printf "NO MATCH: %s %s\n" (CicPp.ppterm s) (CicPp.ppterm t); *)
618 (* print_newline (); *)
619 raise MatchingFailure
622 (* Printf.printf "NO MATCH: %s %s\n" (CicPp.ppterm s) (CicPp.ppterm t); *)
623 (* print_newline (); *)
624 raise MatchingFailure
626 let subst, menv = do_match [] metasenv t1 t2 in
627 (* Printf.printf "DONE!: subst = \n%s\n" (print_subst subst); *)
628 (* print_newline (); *)
633 let matching metasenv context t1 t2 ugraph =
634 (* if (is_simple_term t1) && (is_simple_term t2) then *)
635 (* let subst, menv, ug = *)
636 (* matching_simple metasenv context t1 t2 ugraph in *)
637 (* (\* Printf.printf "matching %s %s:\n%s\n" *\) *)
638 (* (\* (CicPp.ppterm t1) (CicPp.ppterm t2) (print_subst subst); *\) *)
639 (* (\* print_newline (); *\) *)
640 (* subst, menv, ug *)
642 (* Printf.printf "matching %s %s" (CicPp.ppterm t1) (CicPp.ppterm t2); *)
643 (* print_newline (); *)
645 let subst, metasenv, ugraph =
646 (* CicUnification.fo_unif metasenv context t1 t2 ugraph *)
647 unification metasenv context t1 t2 ugraph
649 let t' = CicMetaSubst.apply_subst subst t1 in
650 if not (meta_convertibility t1 t') then
651 raise MatchingFailure
653 let metas = metas_of_term t1 in
654 let fix_subst = function
655 | (i, (c, Cic.Meta (j, lc), ty)) when List.mem i metas ->
656 (j, (c, Cic.Meta (i, lc), ty))
659 let subst = List.map fix_subst subst in
661 (* Printf.printf "matching %s %s:\n%s\n" *)
662 (* (CicPp.ppterm t1) (CicPp.ppterm t2) (print_subst subst); *)
663 (* print_newline (); *)
665 subst, metasenv, ugraph
667 | CicUnification.UnificationFailure _
668 | CicUnification.Uncertain _ ->
669 (* Printf.printf "failed to match %s %s\n" *)
670 (* (CicPp.ppterm t1) (CicPp.ppterm t2); *)
671 (* print_endline (Printexc.to_string e); *)
672 raise MatchingFailure
676 (* let profile = CicUtil.profile "Inference.matching" in *)
677 (* (fun metasenv context t1 t2 ugraph -> *)
678 (* profile (matching metasenv context t1 t2) ugraph) *)
682 let beta_expand ?(metas_ok=true) ?(match_only=false)
683 what type_of_what where context metasenv ugraph =
684 let module S = CicSubstitution in
685 let module C = Cic in
688 (* let names = names_of_context context in *)
689 (* Printf.printf "beta_expand:\nwhat: %s, %s\nwhere: %s, %s\n" *)
690 (* (CicPp.pp what names) (CicPp.ppterm what) *)
691 (* (CicPp.pp where names) (CicPp.ppterm where); *)
692 (* print_newline (); *)
696 ((list of all possible beta expansions, subst, metasenv, ugraph),
699 let rec aux lift_amount term context metasenv subst ugraph =
700 (* Printf.printf "enter aux %s\n" (CicPp.ppterm term); *)
701 let res, lifted_term =
704 [], if m <= lift_amount then C.Rel m else C.Rel (m+1)
706 | C.Var (uri, exp_named_subst) ->
707 let ens', lifted_ens =
708 aux_ens lift_amount exp_named_subst context metasenv subst ugraph
712 (fun (e, s, m, ug) ->
713 (C.Var (uri, e), s, m, ug)) ens'
715 expansions, C.Var (uri, lifted_ens)
720 (fun arg (res, lifted_tl) ->
723 let arg_res, lifted_arg =
724 aux lift_amount arg context metasenv subst ugraph in
727 (fun (a, s, m, ug) -> (Some a)::lifted_tl, s, m, ug)
732 (fun (r, s, m, ug) -> (Some lifted_arg)::r, s, m, ug)
734 (Some lifted_arg)::lifted_tl)
737 (fun (r, s, m, ug) -> None::r, s, m, ug)
744 (fun (l, s, m, ug) ->
745 (C.Meta (i, l), s, m, ug)) l'
747 e, C.Meta (i, lifted_l)
750 | C.Implicit _ as t -> [], t
754 aux lift_amount s context metasenv subst ugraph in
756 aux lift_amount t context metasenv subst ugraph
760 (fun (t, s, m, ug) ->
761 C.Cast (t, lifted_t), s, m, ug) l1 in
764 (fun (t, s, m, ug) ->
765 C.Cast (lifted_s, t), s, m, ug) l2 in
766 l1'@l2', C.Cast (lifted_s, lifted_t)
768 | C.Prod (nn, s, t) ->
770 aux lift_amount s context metasenv subst ugraph in
772 aux (lift_amount+1) t ((Some (nn, C.Decl s))::context)
773 metasenv subst ugraph
777 (fun (t, s, m, ug) ->
778 C.Prod (nn, t, lifted_t), s, m, ug) l1 in
781 (fun (t, s, m, ug) ->
782 C.Prod (nn, lifted_s, t), s, m, ug) l2 in
783 l1'@l2', C.Prod (nn, lifted_s, lifted_t)
785 | C.Lambda (nn, s, t) ->
787 aux lift_amount s context metasenv subst ugraph in
789 aux (lift_amount+1) t ((Some (nn, C.Decl s))::context)
790 metasenv subst ugraph
794 (fun (t, s, m, ug) ->
795 C.Lambda (nn, t, lifted_t), s, m, ug) l1 in
798 (fun (t, s, m, ug) ->
799 C.Lambda (nn, lifted_s, t), s, m, ug) l2 in
800 l1'@l2', C.Lambda (nn, lifted_s, lifted_t)
802 | C.LetIn (nn, s, t) ->
804 aux lift_amount s context metasenv subst ugraph in
806 aux (lift_amount+1) t ((Some (nn, C.Def (s, None)))::context)
807 metasenv subst ugraph
811 (fun (t, s, m, ug) ->
812 C.LetIn (nn, t, lifted_t), s, m, ug) l1 in
815 (fun (t, s, m, ug) ->
816 C.LetIn (nn, lifted_s, t), s, m, ug) l2 in
817 l1'@l2', C.LetIn (nn, lifted_s, lifted_t)
821 aux_list lift_amount l context metasenv subst ugraph
823 (List.map (fun (l, s, m, ug) -> (C.Appl l, s, m, ug)) l',
826 | C.Const (uri, exp_named_subst) ->
827 let ens', lifted_ens =
828 aux_ens lift_amount exp_named_subst context metasenv subst ugraph
832 (fun (e, s, m, ug) ->
833 (C.Const (uri, e), s, m, ug)) ens'
835 (expansions, C.Const (uri, lifted_ens))
837 | C.MutInd (uri, i ,exp_named_subst) ->
838 let ens', lifted_ens =
839 aux_ens lift_amount exp_named_subst context metasenv subst ugraph
843 (fun (e, s, m, ug) ->
844 (C.MutInd (uri, i, e), s, m, ug)) ens'
846 (expansions, C.MutInd (uri, i, lifted_ens))
848 | C.MutConstruct (uri, i, j, exp_named_subst) ->
849 let ens', lifted_ens =
850 aux_ens lift_amount exp_named_subst context metasenv subst ugraph
854 (fun (e, s, m, ug) ->
855 (C.MutConstruct (uri, i, j, e), s, m, ug)) ens'
857 (expansions, C.MutConstruct (uri, i, j, lifted_ens))
859 | C.MutCase (sp, i, outt, t, pl) ->
860 let pl_res, lifted_pl =
861 aux_list lift_amount pl context metasenv subst ugraph
863 let l1, lifted_outt =
864 aux lift_amount outt context metasenv subst ugraph in
866 aux lift_amount t context metasenv subst ugraph in
870 (fun (outt, s, m, ug) ->
871 C.MutCase (sp, i, outt, lifted_t, lifted_pl), s, m, ug) l1 in
874 (fun (t, s, m, ug) ->
875 C.MutCase (sp, i, lifted_outt, t, lifted_pl), s, m, ug) l2 in
878 (fun (pl, s, m, ug) ->
879 C.MutCase (sp, i, lifted_outt, lifted_t, pl), s, m, ug) pl_res
881 (l1'@l2'@l3', C.MutCase (sp, i, lifted_outt, lifted_t, lifted_pl))
884 let len = List.length fl in
887 (fun (nm, idx, ty, bo) (res, lifted_tl) ->
888 let lifted_ty = S.lift lift_amount ty in
889 let bo_res, lifted_bo =
890 aux (lift_amount+len) bo context metasenv subst ugraph in
893 (fun (a, s, m, ug) ->
894 (nm, idx, lifted_ty, a)::lifted_tl, s, m, ug)
899 (fun (r, s, m, ug) ->
900 (nm, idx, lifted_ty, lifted_bo)::r, s, m, ug) res),
901 (nm, idx, lifted_ty, lifted_bo)::lifted_tl)
905 (fun (fl, s, m, ug) -> C.Fix (i, fl), s, m, ug) fl',
906 C.Fix (i, lifted_fl))
909 let len = List.length fl in
912 (fun (nm, ty, bo) (res, lifted_tl) ->
913 let lifted_ty = S.lift lift_amount ty in
914 let bo_res, lifted_bo =
915 aux (lift_amount+len) bo context metasenv subst ugraph in
918 (fun (a, s, m, ug) ->
919 (nm, lifted_ty, a)::lifted_tl, s, m, ug)
924 (fun (r, s, m, ug) ->
925 (nm, lifted_ty, lifted_bo)::r, s, m, ug) res),
926 (nm, lifted_ty, lifted_bo)::lifted_tl)
930 (fun (fl, s, m, ug) -> C.CoFix (i, fl), s, m, ug) fl',
931 C.CoFix (i, lifted_fl))
935 | C.Meta _ when (not metas_ok) ->
939 (* if match_only then replace_metas context term *)
943 let subst', metasenv', ugraph' =
944 (* Printf.printf "provo a unificare %s e %s\n" *)
945 (* (CicPp.ppterm (S.lift lift_amount what)) (CicPp.ppterm term); *)
947 matching metasenv context term (S.lift lift_amount what) ugraph
949 CicUnification.fo_unif metasenv context
950 (S.lift lift_amount what) term ugraph
952 (* Printf.printf "Ok, trovato: %s\n\nwhat: %s" (CicPp.ppterm term) *)
953 (* (CicPp.ppterm (S.lift lift_amount what)); *)
954 (* Printf.printf "substitution:\n%s\n\n" (print_subst subst'); *)
955 (* Printf.printf "metasenv': %s\n" (print_metasenv metasenv'); *)
956 (* Printf.printf "metasenv: %s\n\n" (print_metasenv metasenv); *)
957 (* if match_only then *)
958 (* let t' = CicMetaSubst.apply_subst subst' term in *)
959 (* if not (meta_convertibility term t') then ( *)
960 (* res, lifted_term *)
962 (* let metas = metas_of_term term in *)
963 (* let fix_subst = function *)
964 (* | (i, (c, C.Meta (j, lc), ty)) when List.mem i metas -> *)
965 (* (j, (c, C.Meta (i, lc), ty)) *)
968 (* let subst' = List.map fix_subst subst' in *)
969 (* ((C.Rel (1 + lift_amount), subst', metasenv', ugraph')::res, *)
973 ((C.Rel (1 + lift_amount), subst', metasenv', ugraph')::res,
977 | CicUnification.UnificationFailure _
978 | CicUnification.Uncertain _ ->
981 (* Printf.printf "exit aux\n"; *)
984 and aux_list lift_amount l context metasenv subst ugraph =
986 (fun arg (res, lifted_tl) ->
987 let arg_res, lifted_arg =
988 aux lift_amount arg context metasenv subst ugraph in
990 (fun (a, s, m, ug) -> a::lifted_tl, s, m, ug) arg_res
993 (fun (r, s, m, ug) -> lifted_arg::r, s, m, ug) res),
994 lifted_arg::lifted_tl)
997 and aux_ens lift_amount exp_named_subst context metasenv subst ugraph =
999 (fun (u, arg) (res, lifted_tl) ->
1000 let arg_res, lifted_arg =
1001 aux lift_amount arg context metasenv subst ugraph in
1004 (fun (a, s, m, ug) -> (u, a)::lifted_tl, s, m, ug) arg_res
1006 (l1 @ (List.map (fun (r, s, m, ug) ->
1007 (u, lifted_arg)::r, s, m, ug) res),
1008 (u, lifted_arg)::lifted_tl)
1009 ) exp_named_subst ([], [])
1014 (* if match_only then replace_metas (\* context *\) where *)
1017 aux 0 where context metasenv [] ugraph
1020 (* if match_only then *)
1021 (* (fun (term, subst, metasenv, ugraph) -> *)
1023 (* C.Lambda (C.Anonymous, type_of_what, restore_metas term) *)
1024 (* and subst = restore_subst subst in *)
1025 (* (term', subst, metasenv, ugraph)) *)
1027 (fun (term, subst, metasenv, ugraph) ->
1028 let term' = C.Lambda (C.Anonymous, type_of_what, term) in
1029 (term', subst, metasenv, ugraph))
1031 List.map mapfun expansions
1035 let find_equalities ?(eq_uri=HelmLibraryObjects.Logic.eq_URI) context proof =
1036 let module C = Cic in
1037 let module S = CicSubstitution in
1038 let module T = CicTypeChecker in
1039 let newmeta = ProofEngineHelpers.new_meta_of_proof ~proof in
1040 let ok_types ty menv =
1041 List.for_all (fun (_, _, mt) -> mt = ty) menv
1043 let rec aux index newmeta = function
1045 | (Some (_, C.Decl (term)))::tl ->
1046 let do_find context term =
1048 | C.Prod (name, s, t) ->
1049 (* let newmeta = ProofEngineHelpers.new_meta_of_proof ~proof in *)
1050 let (head, newmetas, args, newmeta) =
1051 ProofEngineHelpers.saturate_term newmeta []
1052 context (S.lift index term) 0
1055 if List.length args = 0 then
1058 C.Appl ((C.Rel index)::args)
1061 | C.Appl [C.MutInd (uri, _, _); ty; t1; t2]
1062 when (UriManager.eq uri eq_uri) && (ok_types ty newmetas) ->
1064 Printf.sprintf "OK: %s" (CicPp.ppterm term)));
1065 (* debug_print (lazy ( *)
1066 (* Printf.sprintf "args: %s\n" *)
1067 (* (String.concat ", " (List.map CicPp.ppterm args)))); *)
1068 (* debug_print (lazy ( *)
1069 (* Printf.sprintf "newmetas:\n%s\n" *)
1070 (* (print_metasenv newmetas))); *)
1071 let o = !Utils.compare_terms t1 t2 in
1072 let w = compute_equality_weight ty t1 t2 in
1073 let proof = BasicProof p in
1074 let e = (w, proof, (ty, t1, t2, o), newmetas, args) in
1076 | _ -> None, newmeta
1078 | C.Appl [C.MutInd (uri, _, _); ty; t1; t2]
1079 when UriManager.eq uri eq_uri ->
1080 let t1 = S.lift index t1
1081 and t2 = S.lift index t2 in
1082 let o = !Utils.compare_terms t1 t2 in
1083 let w = compute_equality_weight ty t1 t2 in
1084 let e = (w, BasicProof (C.Rel index), (ty, t1, t2, o), [], []) in
1086 | _ -> None, newmeta
1088 match do_find context term with
1089 | Some p, newmeta ->
1090 let tl, newmeta' = (aux (index+1) newmeta tl) in
1091 p::tl, max newmeta newmeta'
1093 aux (index+1) newmeta tl
1096 aux (index+1) newmeta tl
1098 aux 1 newmeta context
1102 let equations_blacklist =
1104 (fun s u -> UriManager.UriSet.add (UriManager.uri_of_string u) s)
1105 UriManager.UriSet.empty [
1106 "cic:/Coq/Init/Logic/eq.ind#xpointer(1/1/1)";
1107 "cic:/Coq/Init/Logic/trans_eq.con";
1108 "cic:/Coq/Init/Logic/f_equal.con";
1109 "cic:/Coq/Init/Logic/f_equal2.con";
1110 "cic:/Coq/Init/Logic/f_equal3.con";
1111 "cic:/Coq/Init/Logic/sym_eq.con";
1112 (* "cic:/Coq/Logic/Eqdep/UIP_refl.con"; *)
1113 (* "cic:/Coq/Init/Peano/mult_n_Sm.con"; *)
1115 (* ALB !!!! questo e` imbrogliare, ma x ora lo lasciamo cosi`...
1116 perche' questo cacchio di teorema rompe le scatole :'( *)
1117 "cic:/Rocq/SUBST/comparith/mult_n_2.con";
1121 let find_library_equalities ~(dbd:HMysql.dbd) context status maxmeta =
1122 let module C = Cic in
1123 let module S = CicSubstitution in
1124 let module T = CicTypeChecker in
1128 let suri = UriManager.string_of_uri uri in
1129 if UriManager.UriSet.mem uri equations_blacklist then
1132 let t = CicUtil.term_of_uri uri in
1134 CicTypeChecker.type_of_aux' [] context t CicUniv.empty_ugraph
1138 (MetadataQuery.equations_for_goal ~dbd status)
1140 let eq_uri1 = UriManager.uri_of_string HelmLibraryObjects.Logic.eq_XURI
1141 and eq_uri2 = HelmLibraryObjects.Logic.eq_URI in
1143 (UriManager.eq uri eq_uri1) || (UriManager.eq uri eq_uri2)
1145 let ok_types ty menv =
1146 List.for_all (fun (_, _, mt) -> mt = ty) menv
1148 let rec aux newmeta = function
1150 | (term, termty)::tl ->
1152 Printf.sprintf "Examining: %s (%s)"
1153 (UriManager.string_of_uri (CicUtil.uri_of_term term))(* (CicPp.ppterm term) *) (CicPp.ppterm termty)));
1156 | C.Prod (name, s, t) ->
1157 let head, newmetas, args, newmeta =
1158 ProofEngineHelpers.saturate_term newmeta [] context termty 0
1161 if List.length args = 0 then
1167 | C.Appl [C.MutInd (uri, _, _); ty; t1; t2]
1168 when (iseq uri) && (ok_types ty newmetas) ->
1170 Printf.sprintf "OK: %s" (CicPp.ppterm term)));
1171 let o = !Utils.compare_terms t1 t2 in
1172 let w = compute_equality_weight ty t1 t2 in
1173 let proof = BasicProof p in
1174 let e = (w, proof, (ty, t1, t2, o), newmetas, args) in
1176 | _ -> None, newmeta
1178 | C.Appl [C.MutInd (uri, _, _); ty; t1; t2] when iseq uri ->
1179 let o = !Utils.compare_terms t1 t2 in
1180 let w = compute_equality_weight ty t1 t2 in
1181 let e = (w, BasicProof term, (ty, t1, t2, o), [], []) in
1183 | _ -> None, newmeta
1187 let tl, newmeta' = aux newmeta tl in
1188 e::tl, max newmeta newmeta'
1192 let found, maxm = aux maxmeta candidates in
1195 if List.exists (meta_convertibility_eq e) l then (
1197 Printf.sprintf "NO!! %s already there!" (string_of_equality e)));
1205 let fix_metas newmeta ((w, p, (ty, left, right, o), menv, args) as equality) =
1206 (* print_endline ("fix_metas " ^ (string_of_int newmeta)); *)
1207 let table = Hashtbl.create (List.length args) in
1208 let is_this_case = ref false in
1209 let newargs, newmeta =
1211 (fun t (newargs, index) ->
1213 | Cic.Meta (i, l) ->
1214 if Hashtbl.mem table i then
1215 let idx = Hashtbl.find table i in
1216 ((Cic.Meta (idx, l))::newargs, index+1)
1218 let _ = Hashtbl.add table i index in
1219 ((Cic.Meta (index, l))::newargs, index+1)
1220 | _ -> assert false)
1221 args ([], newmeta+1)
1224 ProofEngineReduction.replace ~equality:(=) ~what:args ~with_what:newargs
1229 (fun (i, context, term) menv ->
1231 let index = Hashtbl.find table i in
1232 (index, context, term)::menv
1234 (i, context, term)::menv)
1238 and left = repl left
1239 and right = repl right in
1240 let metas = (metas_of_term left) @ (metas_of_term right) in
1241 let menv' = List.filter (fun (i, _, _) -> List.mem i metas) menv' in
1244 (function Cic.Meta (i, _) -> List.mem i metas | _ -> assert false) newargs
1246 let table' = Hashtbl.copy table in
1247 let first = List.hd metas in
1252 (function Cic.Meta (i, _) -> i = v | _ -> assert false)
1254 Hashtbl.replace table k first)
1258 (* (Printf.sprintf "args: %s\nnewargs: %s\n" *)
1259 (* (String.concat "; " (List.map CicPp.ppterm args)) *)
1260 (* (String.concat "; " (List.map CicPp.ppterm newargs))); *)
1262 let rec fix_proof = function
1263 | NoProof -> NoProof
1264 | BasicProof term ->
1265 (* let term' = repl term in *)
1267 (* (Printf.sprintf "term was: %s\nterm' is: %s\n" *)
1268 (* (CicPp.ppterm term) (CicPp.ppterm term')); *)
1269 BasicProof (repl term)
1270 | ProofBlock (subst, eq_URI, namety, bo(* t' *), (pos, eq), p) ->
1272 (* Printf.printf "fix_proof of equality %s, subst is:\n%s\n" *)
1273 (* (string_of_equality equality) (print_subst subst); *)
1275 (* debug_print "table is:"; *)
1277 (* (fun k v -> debug_print (Printf.sprintf "%d: %d" k v)) *)
1283 | Cic.Meta (i, l) -> (
1285 let j = Hashtbl.find table i in
1286 if List.mem_assoc i subst then
1289 let _, context, ty = CicUtil.lookup_meta i menv in
1290 (i, (context, Cic.Meta (j, l), ty))::s
1292 (* debug_print ("Not_found meta ?" ^ (string_of_int i)); *)
1295 | _ -> assert false)
1299 (* Printf.printf "subst' is:\n%s\n" (print_subst subst'); *)
1300 (* print_newline (); *)
1302 ProofBlock (subst' @ subst, eq_URI, namety, bo(* t' *), (pos, eq), p)
1305 let neweq = (w, fix_proof p, (ty, left, right, o), menv', newargs) in
1306 (newmeta + 1, neweq)
1310 let term_is_equality ?(eq_uri=HelmLibraryObjects.Logic.eq_URI) term =
1311 let iseq uri = UriManager.eq uri eq_uri in
1313 | Cic.Appl [Cic.MutInd (uri, _, _); _; _; _] when iseq uri -> true
1318 exception TermIsNotAnEquality;;
1320 let equality_of_term ?(eq_uri=HelmLibraryObjects.Logic.eq_URI) proof term =
1321 let iseq uri = UriManager.eq uri eq_uri in
1323 | Cic.Appl [Cic.MutInd (uri, _, _); ty; t1; t2] when iseq uri ->
1324 let o = !Utils.compare_terms t1 t2 in
1325 let w = compute_equality_weight ty t1 t2 in
1326 let e = (w, BasicProof proof, (ty, t1, t2, o), [], []) in
1328 (* (proof, (ty, t1, t2, o), [], []) *)
1330 raise TermIsNotAnEquality
1334 type environment = Cic.metasenv * Cic.context * CicUniv.universe_graph;;
1338 let superposition_left (metasenv, context, ugraph) target source =
1339 let module C = Cic in
1340 let module S = CicSubstitution in
1341 let module M = CicMetaSubst in
1342 let module HL = HelmLibraryObjects in
1343 let module CR = CicReduction in
1344 (* we assume that target is ground (does not contain metavariables): this
1345 * should always be the case (I hope, at least) *)
1346 let proof, (eq_ty, left, right, t_order), _, _ = target in
1347 let eqproof, (ty, t1, t2, s_order), newmetas, args = source in
1349 let compare_terms = !Utils.compare_terms in
1354 let where, is_left =
1355 match t_order (* compare_terms left right *) with
1356 | Lt -> right, false
1359 Printf.printf "????????? %s = %s" (CicPp.ppterm left)
1360 (CicPp.ppterm right);
1362 assert false (* again, for ground terms this shouldn't happen... *)
1365 let metasenv' = newmetas @ metasenv in
1366 let result = s_order (* compare_terms t1 t2 *) in
1369 | Gt -> (beta_expand t1 ty where context metasenv' ugraph), []
1370 | Lt -> [], (beta_expand t2 ty where context metasenv' ugraph)
1374 (fun (t, s, m, ug) ->
1375 compare_terms (M.apply_subst s t1) (M.apply_subst s t2) = Gt)
1376 (beta_expand t1 ty where context metasenv' ugraph)
1379 (fun (t, s, m, ug) ->
1380 compare_terms (M.apply_subst s t2) (M.apply_subst s t1) = Gt)
1381 (beta_expand t2 ty where context metasenv' ugraph)
1385 (* let what, other = *)
1386 (* if is_left then left, right *)
1387 (* else right, left *)
1389 let build_new what other eq_URI (t, s, m, ug) =
1390 let newgoal, newgoalproof =
1392 | C.Lambda (nn, ty, bo) ->
1393 let bo' = S.subst (M.apply_subst s other) bo in
1396 [C.MutInd (HL.Logic.eq_URI, 0, []);
1398 if is_left then [bo'; S.lift 1 right]
1399 else [S.lift 1 left; bo'])
1401 let t' = C.Lambda (nn, ty, bo'') in
1402 S.subst (M.apply_subst s other) bo,
1404 (C.Appl [C.Const (eq_URI, []); ty; what; t';
1405 proof; other; eqproof])
1409 if is_left then (eq_ty, newgoal, right, compare_terms newgoal right)
1410 else (eq_ty, left, newgoal, compare_terms left newgoal)
1412 (newgoalproof (* eqproof *), equation, [], [])
1414 let new1 = List.map (build_new t1 t2 HL.Logic.eq_ind_URI) res1
1415 and new2 = List.map (build_new t2 t1 HL.Logic.eq_ind_r_URI) res2 in
1420 let superposition_right newmeta (metasenv, context, ugraph) target source =
1421 let module C = Cic in
1422 let module S = CicSubstitution in
1423 let module M = CicMetaSubst in
1424 let module HL = HelmLibraryObjects in
1425 let module CR = CicReduction in
1426 let eqproof, (eq_ty, left, right, t_order), newmetas, args = target in
1427 let eqp', (ty', t1, t2, s_order), newm', args' = source in
1428 let maxmeta = ref newmeta in
1430 let compare_terms = !Utils.compare_terms in
1432 if eq_ty <> ty' then
1435 (* let ok term subst other other_eq_side ugraph = *)
1436 (* match term with *)
1437 (* | C.Lambda (nn, ty, bo) -> *)
1438 (* let bo' = S.subst (M.apply_subst subst other) bo in *)
1439 (* let res, _ = CR.are_convertible context bo' other_eq_side ugraph in *)
1441 (* | _ -> assert false *)
1443 let condition left right what other (t, s, m, ug) =
1444 let subst = M.apply_subst s in
1445 let cmp1 = compare_terms (subst what) (subst other) in
1446 let cmp2 = compare_terms (subst left) (subst right) in
1447 (* cmp1 = Gt && cmp2 = Gt *)
1448 cmp1 <> Lt && cmp1 <> Le && cmp2 <> Lt && cmp2 <> Le
1449 (* && (ok t s other right ug) *)
1451 let metasenv' = metasenv @ newmetas @ newm' in
1452 let beta_expand = beta_expand ~metas_ok:false in
1453 let cmp1 = t_order (* compare_terms left right *)
1454 and cmp2 = s_order (* compare_terms t1 t2 *) in
1455 let res1, res2, res3, res4 =
1459 (beta_expand s eq_ty l context metasenv' ugraph)
1461 match cmp1, cmp2 with
1463 (beta_expand t1 eq_ty left context metasenv' ugraph), [], [], []
1465 [], (beta_expand t2 eq_ty left context metasenv' ugraph), [], []
1467 [], [], (beta_expand t1 eq_ty right context metasenv' ugraph), []
1469 [], [], [], (beta_expand t2 eq_ty right context metasenv' ugraph)
1471 let res1 = res left right t1 t2
1472 and res2 = res left right t2 t1 in
1475 let res3 = res right left t1 t2
1476 and res4 = res right left t2 t1 in
1479 let res1 = res left right t1 t2
1480 and res3 = res right left t1 t2 in
1483 let res2 = res left right t2 t1
1484 and res4 = res right left t2 t1 in
1487 let res1 = res left right t1 t2
1488 and res2 = res left right t2 t1
1489 and res3 = res right left t1 t2
1490 and res4 = res right left t2 t1 in
1491 res1, res2, res3, res4
1493 let newmetas = newmetas @ newm' in
1494 let newargs = args @ args' in
1495 let build_new what other is_left eq_URI (t, s, m, ug) =
1496 (* let what, other = *)
1497 (* if is_left then left, right *)
1498 (* else right, left *)
1500 let newterm, neweqproof =
1502 | C.Lambda (nn, ty, bo) ->
1503 let bo' = M.apply_subst s (S.subst other bo) in
1506 [C.MutInd (HL.Logic.eq_URI, 0, []); S.lift 1 eq_ty] @
1507 if is_left then [bo'; S.lift 1 right]
1508 else [S.lift 1 left; bo'])
1510 let t' = C.Lambda (nn, ty, bo'') in
1513 (C.Appl [C.Const (eq_URI, []); ty; what; t';
1514 eqproof; other; eqp'])
1517 let newmeta, newequality =
1519 if is_left then (newterm, M.apply_subst s right)
1520 else (M.apply_subst s left, newterm) in
1521 let neworder = compare_terms left right in
1523 (neweqproof, (eq_ty, left, right, neworder), newmetas, newargs)
1528 let new1 = List.map (build_new t1 t2 true HL.Logic.eq_ind_URI) res1
1529 and new2 = List.map (build_new t2 t1 true HL.Logic.eq_ind_r_URI) res2
1530 and new3 = List.map (build_new t1 t2 false HL.Logic.eq_ind_URI) res3
1531 and new4 = List.map (build_new t2 t1 false HL.Logic.eq_ind_r_URI) res4 in
1533 | _, (_, left, right, _), _, _ ->
1534 not (fst (CR.are_convertible context left right ugraph))
1537 (List.filter ok (new1 @ new2 @ new3 @ new4)))
1542 let is_identity ((_, context, ugraph) as env) = function
1543 | ((_, _, (ty, left, right, _), _, _) as equality) ->
1545 (meta_convertibility left right) ||
1546 (fst (CicReduction.are_convertible context left right ugraph)))
1551 let demodulation newmeta (metasenv, context, ugraph) target source =
1552 let module C = Cic in
1553 let module S = CicSubstitution in
1554 let module M = CicMetaSubst in
1555 let module HL = HelmLibraryObjects in
1556 let module CR = CicReduction in
1558 let proof, (eq_ty, left, right, t_order), metas, args = target
1559 and proof', (ty, t1, t2, s_order), metas', args' = source in
1561 let compare_terms = !Utils.compare_terms in
1566 let first_step, get_params =
1567 match s_order (* compare_terms t1 t2 *) with
1568 | Gt -> 1, (function
1569 | 1 -> true, t1, t2, HL.Logic.eq_ind_URI
1570 | 0 -> false, t1, t2, HL.Logic.eq_ind_URI
1571 | _ -> assert false)
1572 | Lt -> 1, (function
1573 | 1 -> true, t2, t1, HL.Logic.eq_ind_r_URI
1574 | 0 -> false, t2, t1, HL.Logic.eq_ind_r_URI
1575 | _ -> assert false)
1577 let first_step = 3 in
1578 let get_params step =
1580 | 3 -> true, t1, t2, HL.Logic.eq_ind_URI
1581 | 2 -> false, t1, t2, HL.Logic.eq_ind_URI
1582 | 1 -> true, t2, t1, HL.Logic.eq_ind_r_URI
1583 | 0 -> false, t2, t1, HL.Logic.eq_ind_r_URI
1586 first_step, get_params
1588 let rec demodulate newmeta step metasenv target =
1589 let proof, (eq_ty, left, right, t_order), metas, args = target in
1590 let is_left, what, other, eq_URI = get_params step in
1592 let env = metasenv, context, ugraph in
1593 let names = names_of_context context in
1595 (* "demodulate\ntarget: %s\nwhat: %s\nother: %s\nis_left: %s\n" *)
1596 (* (string_of_equality ~env target) (CicPp.pp what names) *)
1597 (* (CicPp.pp other names) (string_of_bool is_left); *)
1598 (* Printf.printf "step: %d" step; *)
1599 (* print_newline (); *)
1601 let ok (t, s, m, ug) =
1602 compare_terms (M.apply_subst s what) (M.apply_subst s other) = Gt
1605 let r = (beta_expand ~metas_ok:false ~match_only:true
1606 what ty (if is_left then left else right)
1607 context (metasenv @ metas) ugraph)
1609 (* let m' = metas_of_term what *)
1610 (* and m'' = metas_of_term (if is_left then left else right) in *)
1611 (* if (List.mem 527 m'') && (List.mem 6 m') then ( *)
1613 (* "demodulate\ntarget: %s\nwhat: %s\nother: %s\nis_left: %s\n" *)
1614 (* (string_of_equality ~env target) (CicPp.pp what names) *)
1615 (* (CicPp.pp other names) (string_of_bool is_left); *)
1616 (* Printf.printf "step: %d" step; *)
1617 (* print_newline (); *)
1618 (* print_endline "res:"; *)
1619 (* List.iter (fun (t, s, m, ug) -> print_endline (CicPp.pp t names)) r; *)
1620 (* print_newline (); *)
1621 (* Printf.printf "metasenv:\n%s\n" (print_metasenv (metasenv @ metas)); *)
1622 (* print_newline (); *)
1628 if step = 0 then newmeta, target
1629 else demodulate newmeta (step-1) metasenv target
1630 | (t, s, m, ug)::_ ->
1631 let newterm, newproof =
1633 | C.Lambda (nn, ty, bo) ->
1634 (* let bo' = M.apply_subst s (S.subst other bo) in *)
1635 let bo' = S.subst (M.apply_subst s other) bo in
1638 [C.MutInd (HL.Logic.eq_URI, 0, []);
1640 if is_left then [bo'; S.lift 1 right]
1641 else [S.lift 1 left; bo'])
1643 let t' = C.Lambda (nn, ty, bo'') in
1644 (* M.apply_subst s (S.subst other bo), *)
1647 (C.Appl [C.Const (eq_URI, []); ty; what; t';
1648 proof; other; proof'])
1651 let newmeta, newtarget =
1653 (* if is_left then (newterm, M.apply_subst s right) *)
1654 (* else (M.apply_subst s left, newterm) in *)
1655 if is_left then newterm, right
1658 let neworder = compare_terms left right in
1659 (* let newmetasenv = metasenv @ metas in *)
1660 (* let newargs = args @ args' in *)
1661 (* fix_metas newmeta *)
1662 (* (newproof, (eq_ty, left, right), newmetasenv, newargs) *)
1663 let m = (metas_of_term left) @ (metas_of_term right) in
1664 let newmetasenv = List.filter (fun (i, _, _) -> List.mem i m) metas
1667 (function C.Meta (i, _) -> List.mem i m | _ -> assert false)
1671 (newproof, (eq_ty, left, right, neworder), newmetasenv, newargs)
1674 (* "demodulate, newtarget: %s\ntarget was: %s\n" *)
1675 (* (string_of_equality ~env newtarget) *)
1676 (* (string_of_equality ~env target); *)
1677 (* (\* let _, _, newm, newa = newtarget in *\) *)
1678 (* (\* Printf.printf "newmetasenv:\n%s\nnewargs:\n%s\n" *\) *)
1679 (* (\* (print_metasenv newm) *\) *)
1680 (* (\* (String.concat "\n" (List.map CicPp.ppterm newa)); *\) *)
1681 (* print_newline (); *)
1682 if is_identity env newtarget then
1685 demodulate newmeta first_step metasenv newtarget
1687 demodulate newmeta first_step (metasenv @ metas') target
1692 let demodulation newmeta env target source =
1698 let subsumption env target source =
1699 let _, (ty, tl, tr, _), tmetas, _ = target
1700 and _, (ty', sl, sr, _), smetas, _ = source in
1704 let metasenv, context, ugraph = env in
1705 let metasenv = metasenv @ tmetas @ smetas in
1706 let names = names_of_context context in
1707 let samesubst subst subst' =
1708 (* Printf.printf "samesubst:\nsubst: %s\nsubst': %s\n" *)
1709 (* (print_subst subst) (print_subst subst'); *)
1710 (* print_newline (); *)
1711 let tbl = Hashtbl.create (List.length subst) in
1712 List.iter (fun (m, (c, t1, t2)) -> Hashtbl.add tbl m (c, t1, t2)) subst;
1714 (fun (m, (c, t1, t2)) ->
1716 let c', t1', t2' = Hashtbl.find tbl m in
1717 if (c = c') && (t1 = t1') && (t2 = t2') then true
1723 let subsaux left right left' right' =
1725 let subst, menv, ug = matching metasenv context left left' ugraph
1726 and subst', menv', ug' = matching metasenv context right right' ugraph
1728 (* Printf.printf "left = right: %s = %s\n" *)
1729 (* (CicPp.pp left names) (CicPp.pp right names); *)
1730 (* Printf.printf "left' = right': %s = %s\n" *)
1731 (* (CicPp.pp left' names) (CicPp.pp right' names); *)
1732 samesubst subst subst'
1734 (* print_endline (Printexc.to_string e); *)
1738 if subsaux tl tr sl sr then true
1739 else subsaux tl tr sr sl
1742 Printf.printf "subsumption!:\ntarget: %s\nsource: %s\n"
1743 (string_of_equality ~env target) (string_of_equality ~env source);
1751 let extract_differing_subterms t1 t2 =
1752 let module C = Cic in
1755 | C.Appl l1, C.Appl l2 when (List.length l1) <> (List.length l2) ->
1757 | C.Appl (h1::tl1), C.Appl (h2::tl2) ->
1758 let res = List.concat (List.map2 aux tl1 tl2) in
1760 if res = [] then [(h1, h2)] else [(t1, t2)]
1762 if List.length res > 1 then [(t1, t2)] else res
1764 if t1 <> t2 then [(t1, t2)] else []
1766 let res = aux t1 t2 in