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 (* name, ty, eq_ty, left, right *)
20 (Cic.name * Cic.term * Cic.term * Cic.term * Cic.term) *
21 (Utils.pos * equality) * proof
22 | ProofGoalBlock of proof * equality
23 | ProofSymBlock of Cic.term Cic.explicit_named_substitution * proof
27 let string_of_equality ?env =
31 | w, _, (ty, left, right, o), _, _ ->
32 Printf.sprintf "Weight: %d, {%s}: %s =(%s) %s" w (CicPp.ppterm ty)
33 (CicPp.ppterm left) (string_of_comparison o) (CicPp.ppterm right)
35 | Some (_, context, _) -> (
36 let names = names_of_context context in
38 | w, _, (ty, left, right, o), _, _ ->
39 Printf.sprintf "Weight: %d, {%s}: %s =(%s) %s" w (CicPp.pp ty names)
40 (CicPp.pp left names) (string_of_comparison o)
41 (CicPp.pp right names)
46 let build_proof_term equality =
47 (* Printf.printf "build_term_proof %s" (string_of_equality equality); *)
48 (* print_newline (); *)
52 let rec do_build_proof proof =
55 Printf.fprintf stderr "WARNING: no proof!\n";
56 (* (string_of_equality equality); *)
58 | BasicProof term -> term
59 | ProofGoalBlock (proofbit, equality) ->
60 print_endline "found ProofGoalBlock, going up...";
61 let _, proof, _, _, _ = equality in
62 do_build_goal_proof proofbit proof
63 | ProofSymBlock (ens, proof) ->
64 let proof = do_build_proof proof in
66 Cic.Const (HelmLibraryObjects.Logic.sym_eq_URI, ens); (* symmetry *)
69 | ProofBlock (subst, eq_URI, t', (pos, eq), eqproof) ->
70 (* Printf.printf "\nsubst:\n%s\n" (print_subst subst); *)
71 (* print_newline (); *)
73 let name, ty, eq_ty, left, right = t' in
75 Cic.Appl [Cic.MutInd (HelmLibraryObjects.Logic.eq_URI, 0, []);
78 let t' = Cic.Lambda (name, ty, (* CicSubstitution.lift 1 *) bo) in
79 (* Printf.printf " ProofBlock: eq = %s, eq' = %s" *)
80 (* (string_of_equality eq) (string_of_equality eq'); *)
81 (* print_newline (); *)
83 (* let s = String.make !indent ' ' in *)
86 (* print_endline (s ^ "build proof'------------"); *)
89 let _, proof', _, _, _ = eq in
92 (* print_endline (s ^ "END proof'"); *)
94 (* print_endline (s ^ "build eqproof-----------"); *)
96 let eqproof = do_build_proof eqproof in
98 (* print_endline (s ^ "END eqproof"); *)
102 let _, _, (ty, what, other, _), menv', args' = eq in
104 if pos = Utils.Left then what, other else other, what
106 CicMetaSubst.apply_subst subst
107 (Cic.Appl [Cic.Const (eq_URI, []); ty;
108 what; t'; eqproof; other; proof'])
110 and do_build_goal_proof proofbit proof =
111 (* match proofbit with *)
112 (* | BasicProof _ -> do_build_proof proof *)
115 | ProofGoalBlock (pb, eq) ->
116 do_build_proof (ProofGoalBlock (replace_proof proofbit pb, eq))
117 (* let _, proof, _, _, _ = eq in *)
118 (* let newproof = replace_proof proofbit proof in *)
119 (* do_build_proof newproof *)
121 (* | ProofBlock (subst, eq_URI, t', poseq, eqproof) -> *)
122 (* let eqproof' = replace_proof proofbit eqproof in *)
123 (* do_build_proof (ProofBlock (subst, eq_URI, t', poseq, eqproof')) *)
124 | _ -> do_build_proof (replace_proof proofbit proof) (* assert false *)
126 and replace_proof newproof = function
127 | ProofBlock (subst, eq_URI, t', poseq, eqproof) ->
129 (* if eq_URI = HelmLibraryObjects.Logic.eq_ind_URI then *)
130 (* HelmLibraryObjects.Logic.eq_ind_r_URI *)
132 (* HelmLibraryObjects.Logic.eq_ind_URI *)
134 let eqproof' = replace_proof newproof eqproof in
135 ProofBlock (subst, uri(* eq_URI *), t', poseq, eqproof')
136 (* ProofBlock (subst, eq_URI, t', poseq, newproof) *)
137 | ProofGoalBlock (pb, equality) ->
138 let pb' = replace_proof newproof pb in
139 ProofGoalBlock (pb', equality)
140 (* let w, proof, t, menv, args = equality in *)
141 (* let proof' = replace_proof newproof proof in *)
142 (* ProofGoalBlock (pb, (w, proof', t, menv, args)) *)
143 | BasicProof _ -> newproof
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
378 let restore_metas (* context *) term =
379 let module C = Cic in
380 let rec aux = function
381 | C.Implicit (Some (`MetaInfo (i, c))) ->
383 (* CicMkImplicit.identity_relocation_list_for_metavariable context *)
386 (* let local_context:(C.term option) list = *)
387 (* Marshal.from_string mc 0 *)
389 (* C.Meta (i, local_context) *)
391 | C.Var (u, ens) -> C.Var (u, aux_ens ens)
392 | C.Const (u, ens) -> C.Const (u, aux_ens ens)
393 | C.Cast (s, t) -> C.Cast (aux s, aux t)
394 | C.Prod (name, s, t) -> C.Prod (name, aux s, aux t)
395 | C.Lambda (name, s, t) -> C.Lambda (name, aux s, aux t)
396 | C.LetIn (name, s, t) -> C.LetIn (name, aux s, aux t)
397 | C.Appl l -> C.Appl (List.map aux l)
398 | C.MutInd (uri, i, ens) -> C.MutInd (uri, i, aux_ens ens)
399 | C.MutConstruct (uri, i, j, ens) -> C.MutConstruct (uri, i, j, aux_ens ens)
400 | C.MutCase (uri, i, s, t, l) ->
401 C.MutCase (uri, i, aux s, aux t, List.map aux l)
404 List.map (fun (s, i, t1, t2) -> (s, i, aux t1, aux t2)) il in
408 List.map (fun (s, t1, t2) -> (s, aux t1, aux t2)) il in
412 List.map (fun (u, t) -> (u, aux t)) ens
418 let rec restore_subst (* context *) subst =
420 (fun (i, (c, t, ty)) ->
421 i, (c, restore_metas (* context *) t, ty))
427 let rec check_irl start = function
429 | None::tl -> check_irl (start+1) tl
430 | (Some (Cic.Rel x))::tl ->
431 if x = start then check_irl (start+1) tl else false
435 let rec is_simple_term = function
436 | Cic.Appl ((Cic.Meta _)::_) -> false
437 | Cic.Appl l -> List.for_all is_simple_term l
438 | Cic.Meta (i, l) -> check_irl 1 l
440 | Cic.Const _ -> true
445 let lookup_subst meta subst =
447 | Cic.Meta (i, _) -> (
448 try let _, (_, t, _) = List.find (fun (m, _) -> m = i) subst in t
449 with Not_found -> meta
455 let unification_simple metasenv context t1 t2 ugraph =
456 let module C = Cic in
457 let module M = CicMetaSubst in
458 let module U = CicUnification in
459 let lookup = lookup_subst in
460 let rec occurs_check subst what where =
462 | t when what = t -> true
463 | C.Appl l -> List.exists (occurs_check subst what) l
465 let t = lookup where subst in
466 if t <> where then occurs_check subst what t else false
469 let rec unif subst menv s t =
470 let s = match s with C.Meta _ -> lookup s subst | _ -> s
471 and t = match t with C.Meta _ -> lookup t subst | _ -> t
474 | s, t when s = t -> subst, menv
475 | C.Meta (i, _), C.Meta (j, _) when i > j ->
477 | C.Meta _, t when occurs_check subst s t ->
478 raise (U.UnificationFailure "Inference.unification.unif")
479 | C.Meta (i, l), t -> (
481 let _, _, ty = CicUtil.lookup_meta i menv in
483 if not (List.mem_assoc i subst) then (i, (context, t, ty))::subst
486 let menv = menv in (* List.filter (fun (m, _, _) -> i <> m) menv in *)
488 with CicUtil.Meta_not_found m ->
489 let names = names_of_context context in
491 Printf.sprintf "Meta_not_found %d!: %s %s\n%s\n\n%s" m
492 (CicPp.pp t1 names) (CicPp.pp t2 names)
493 (print_metasenv menv) (print_metasenv metasenv));
496 | _, C.Meta _ -> unif subst menv t s
497 | C.Appl (hds::_), C.Appl (hdt::_) when hds <> hdt ->
498 raise (U.UnificationFailure "Inference.unification.unif")
499 | C.Appl (hds::tls), C.Appl (hdt::tlt) -> (
502 (fun (subst', menv) s t -> unif subst' menv s t)
503 (subst, menv) tls tlt
504 with Invalid_argument _ ->
505 raise (U.UnificationFailure "Inference.unification.unif")
507 | _, _ -> raise (U.UnificationFailure "Inference.unification.unif")
509 let subst, menv = unif [] metasenv t1 t2 in
513 try let _ = List.find (fun (i, _) -> m = i) subst in false
514 with Not_found -> true)
517 List.rev subst, menv, ugraph
521 let unification metasenv context t1 t2 ugraph =
522 (* Printf.printf "| unification %s %s\n" (CicPp.ppterm t1) (CicPp.ppterm t2); *)
523 let subst, menv, ug =
524 if not (is_simple_term t1) || not (is_simple_term t2) then
525 CicUnification.fo_unif metasenv context t1 t2 ugraph
527 unification_simple metasenv context t1 t2 ugraph
529 let rec fix_term = function
530 | (Cic.Meta (i, l) as t) ->
531 let t' = lookup_subst t subst in
532 if t <> t' then fix_term t' else t
533 | Cic.Appl l -> Cic.Appl (List.map fix_term l)
536 let rec fix_subst = function
538 | (i, (c, t, ty))::tl -> (i, (c, fix_term t, fix_term ty))::(fix_subst tl)
540 (* Printf.printf "| subst: %s\n" (print_subst ~prefix:" ; " subst); *)
541 (* print_endline "|"; *)
542 fix_subst subst, menv, ug
546 (* let unification = CicUnification.fo_unif;; *)
548 exception MatchingFailure;;
551 let matching_simple metasenv context t1 t2 ugraph =
552 let module C = Cic in
553 let module M = CicMetaSubst in
554 let module U = CicUnification in
555 let lookup meta subst =
558 try let _, (_, t, _) = List.find (fun (m, _) -> m = i) subst in t
559 with Not_found -> meta
563 let rec do_match subst menv s t =
564 (* Printf.printf "do_match %s %s\n%s\n" (CicPp.ppterm s) (CicPp.ppterm t) *)
565 (* (print_subst subst); *)
566 (* print_newline (); *)
567 (* let s = match s with C.Meta _ -> lookup s subst | _ -> s *)
568 (* let t = match t with C.Meta _ -> lookup t subst | _ -> t in *)
569 (* Printf.printf "after apply_subst: %s %s\n%s" *)
570 (* (CicPp.ppterm s) (CicPp.ppterm t) (print_subst subst); *)
571 (* print_newline (); *)
573 | s, t when s = t -> subst, menv
574 (* | C.Meta (i, _), C.Meta (j, _) when i > j -> *)
575 (* do_match subst menv t s *)
576 (* | C.Meta _, t when occurs_check subst s t -> *)
577 (* raise MatchingFailure *)
578 (* | s, C.Meta _ when occurs_check subst t s -> *)
579 (* raise MatchingFailure *)
580 | s, C.Meta (i, l) ->
581 let filter_menv i menv =
582 List.filter (fun (m, _, _) -> i <> m) menv
585 let value = lookup t subst in
587 (* | C.Meta (i', l') when Hashtbl.mem table i' -> *)
588 (* (i', (context, s, ty))::subst, menv (\* filter_menv i' menv *\) *)
589 | value when value = t ->
590 let _, _, ty = CicUtil.lookup_meta i menv in
591 (i, (context, s, ty))::subst, filter_menv i menv
592 | value when value <> s ->
593 raise MatchingFailure
594 | value -> do_match subst menv s value
597 (* else if value <> s then *)
598 (* raise MatchingFailure *)
600 (* if not (List.mem_assoc i subst) then (i, (context, t, ty))::subst *)
603 (* let menv = List.filter (fun (m, _, _) -> i <> m) menv in *)
605 (* | _, C.Meta _ -> do_match subst menv t s *)
606 (* | C.Appl (hds::_), C.Appl (hdt::_) when hds <> hdt -> *)
607 (* raise MatchingFailure *)
608 | C.Appl ls, C.Appl lt -> (
611 (fun (subst, menv) s t -> do_match subst menv s t)
613 with Invalid_argument _ ->
614 (* print_endline (Printexc.to_string e); *)
615 (* Printf.printf "NO MATCH: %s %s\n" (CicPp.ppterm s) (CicPp.ppterm t); *)
616 (* print_newline (); *)
617 raise MatchingFailure
620 (* Printf.printf "NO MATCH: %s %s\n" (CicPp.ppterm s) (CicPp.ppterm t); *)
621 (* print_newline (); *)
622 raise MatchingFailure
624 let subst, menv = do_match [] metasenv t1 t2 in
625 (* Printf.printf "DONE!: subst = \n%s\n" (print_subst subst); *)
626 (* print_newline (); *)
631 let matching metasenv context t1 t2 ugraph =
632 (* if (is_simple_term t1) && (is_simple_term t2) then *)
633 (* let subst, menv, ug = *)
634 (* matching_simple metasenv context t1 t2 ugraph in *)
635 (* (\* Printf.printf "matching %s %s:\n%s\n" *\) *)
636 (* (\* (CicPp.ppterm t1) (CicPp.ppterm t2) (print_subst subst); *\) *)
637 (* (\* print_newline (); *\) *)
638 (* subst, menv, ug *)
640 (* Printf.printf "matching %s %s" (CicPp.ppterm t1) (CicPp.ppterm t2); *)
641 (* print_newline (); *)
643 let subst, metasenv, ugraph =
644 (* CicUnification.fo_unif metasenv context t1 t2 ugraph *)
645 unification metasenv context t1 t2 ugraph
647 let t' = CicMetaSubst.apply_subst subst t1 in
648 if not (meta_convertibility t1 t') then
649 raise MatchingFailure
651 let metas = metas_of_term t1 in
652 let fix_subst = function
653 | (i, (c, Cic.Meta (j, lc), ty)) when List.mem i metas ->
654 (j, (c, Cic.Meta (i, lc), ty))
657 let subst = List.map fix_subst subst in
659 (* Printf.printf "matching %s %s:\n%s\n" *)
660 (* (CicPp.ppterm t1) (CicPp.ppterm t2) (print_subst subst); *)
661 (* print_newline (); *)
663 subst, metasenv, ugraph
665 | CicUnification.UnificationFailure _
666 | CicUnification.Uncertain _ ->
667 (* Printf.printf "failed to match %s %s\n" *)
668 (* (CicPp.ppterm t1) (CicPp.ppterm t2); *)
669 (* print_endline (Printexc.to_string e); *)
670 raise MatchingFailure
674 (* let profile = CicUtil.profile "Inference.matching" in *)
675 (* (fun metasenv context t1 t2 ugraph -> *)
676 (* profile (matching metasenv context t1 t2) ugraph) *)
680 let beta_expand ?(metas_ok=true) ?(match_only=false)
681 what type_of_what where context metasenv ugraph =
682 let module S = CicSubstitution in
683 let module C = Cic in
686 (* let names = names_of_context context in *)
687 (* Printf.printf "beta_expand:\nwhat: %s, %s\nwhere: %s, %s\n" *)
688 (* (CicPp.pp what names) (CicPp.ppterm what) *)
689 (* (CicPp.pp where names) (CicPp.ppterm where); *)
690 (* print_newline (); *)
694 ((list of all possible beta expansions, subst, metasenv, ugraph),
697 let rec aux lift_amount term context metasenv subst ugraph =
698 (* Printf.printf "enter aux %s\n" (CicPp.ppterm term); *)
699 let res, lifted_term =
702 [], if m <= lift_amount then C.Rel m else C.Rel (m+1)
704 | C.Var (uri, exp_named_subst) ->
705 let ens', lifted_ens =
706 aux_ens lift_amount exp_named_subst context metasenv subst ugraph
710 (fun (e, s, m, ug) ->
711 (C.Var (uri, e), s, m, ug)) ens'
713 expansions, C.Var (uri, lifted_ens)
718 (fun arg (res, lifted_tl) ->
721 let arg_res, lifted_arg =
722 aux lift_amount arg context metasenv subst ugraph in
725 (fun (a, s, m, ug) -> (Some a)::lifted_tl, s, m, ug)
730 (fun (r, s, m, ug) -> (Some lifted_arg)::r, s, m, ug)
732 (Some lifted_arg)::lifted_tl)
735 (fun (r, s, m, ug) -> None::r, s, m, ug)
742 (fun (l, s, m, ug) ->
743 (C.Meta (i, l), s, m, ug)) l'
745 e, C.Meta (i, lifted_l)
748 | C.Implicit _ as t -> [], t
752 aux lift_amount s context metasenv subst ugraph in
754 aux lift_amount t context metasenv subst ugraph
758 (fun (t, s, m, ug) ->
759 C.Cast (t, lifted_t), s, m, ug) l1 in
762 (fun (t, s, m, ug) ->
763 C.Cast (lifted_s, t), s, m, ug) l2 in
764 l1'@l2', C.Cast (lifted_s, lifted_t)
766 | C.Prod (nn, s, t) ->
768 aux lift_amount s context metasenv subst ugraph in
770 aux (lift_amount+1) t ((Some (nn, C.Decl s))::context)
771 metasenv subst ugraph
775 (fun (t, s, m, ug) ->
776 C.Prod (nn, t, lifted_t), s, m, ug) l1 in
779 (fun (t, s, m, ug) ->
780 C.Prod (nn, lifted_s, t), s, m, ug) l2 in
781 l1'@l2', C.Prod (nn, lifted_s, lifted_t)
783 | C.Lambda (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.Lambda (nn, t, lifted_t), s, m, ug) l1 in
796 (fun (t, s, m, ug) ->
797 C.Lambda (nn, lifted_s, t), s, m, ug) l2 in
798 l1'@l2', C.Lambda (nn, lifted_s, lifted_t)
800 | C.LetIn (nn, s, t) ->
802 aux lift_amount s context metasenv subst ugraph in
804 aux (lift_amount+1) t ((Some (nn, C.Def (s, None)))::context)
805 metasenv subst ugraph
809 (fun (t, s, m, ug) ->
810 C.LetIn (nn, t, lifted_t), s, m, ug) l1 in
813 (fun (t, s, m, ug) ->
814 C.LetIn (nn, lifted_s, t), s, m, ug) l2 in
815 l1'@l2', C.LetIn (nn, lifted_s, lifted_t)
819 aux_list lift_amount l context metasenv subst ugraph
821 (List.map (fun (l, s, m, ug) -> (C.Appl l, s, m, ug)) l',
824 | C.Const (uri, exp_named_subst) ->
825 let ens', lifted_ens =
826 aux_ens lift_amount exp_named_subst context metasenv subst ugraph
830 (fun (e, s, m, ug) ->
831 (C.Const (uri, e), s, m, ug)) ens'
833 (expansions, C.Const (uri, lifted_ens))
835 | C.MutInd (uri, i ,exp_named_subst) ->
836 let ens', lifted_ens =
837 aux_ens lift_amount exp_named_subst context metasenv subst ugraph
841 (fun (e, s, m, ug) ->
842 (C.MutInd (uri, i, e), s, m, ug)) ens'
844 (expansions, C.MutInd (uri, i, lifted_ens))
846 | C.MutConstruct (uri, i, j, exp_named_subst) ->
847 let ens', lifted_ens =
848 aux_ens lift_amount exp_named_subst context metasenv subst ugraph
852 (fun (e, s, m, ug) ->
853 (C.MutConstruct (uri, i, j, e), s, m, ug)) ens'
855 (expansions, C.MutConstruct (uri, i, j, lifted_ens))
857 | C.MutCase (sp, i, outt, t, pl) ->
858 let pl_res, lifted_pl =
859 aux_list lift_amount pl context metasenv subst ugraph
861 let l1, lifted_outt =
862 aux lift_amount outt context metasenv subst ugraph in
864 aux lift_amount t context metasenv subst ugraph in
868 (fun (outt, s, m, ug) ->
869 C.MutCase (sp, i, outt, lifted_t, lifted_pl), s, m, ug) l1 in
872 (fun (t, s, m, ug) ->
873 C.MutCase (sp, i, lifted_outt, t, lifted_pl), s, m, ug) l2 in
876 (fun (pl, s, m, ug) ->
877 C.MutCase (sp, i, lifted_outt, lifted_t, pl), s, m, ug) pl_res
879 (l1'@l2'@l3', C.MutCase (sp, i, lifted_outt, lifted_t, lifted_pl))
882 let len = List.length fl in
885 (fun (nm, idx, ty, bo) (res, lifted_tl) ->
886 let lifted_ty = S.lift lift_amount ty in
887 let bo_res, lifted_bo =
888 aux (lift_amount+len) bo context metasenv subst ugraph in
891 (fun (a, s, m, ug) ->
892 (nm, idx, lifted_ty, a)::lifted_tl, s, m, ug)
897 (fun (r, s, m, ug) ->
898 (nm, idx, lifted_ty, lifted_bo)::r, s, m, ug) res),
899 (nm, idx, lifted_ty, lifted_bo)::lifted_tl)
903 (fun (fl, s, m, ug) -> C.Fix (i, fl), s, m, ug) fl',
904 C.Fix (i, lifted_fl))
907 let len = List.length fl in
910 (fun (nm, ty, bo) (res, lifted_tl) ->
911 let lifted_ty = S.lift lift_amount ty in
912 let bo_res, lifted_bo =
913 aux (lift_amount+len) bo context metasenv subst ugraph in
916 (fun (a, s, m, ug) ->
917 (nm, lifted_ty, a)::lifted_tl, s, m, ug)
922 (fun (r, s, m, ug) ->
923 (nm, lifted_ty, lifted_bo)::r, s, m, ug) res),
924 (nm, lifted_ty, lifted_bo)::lifted_tl)
928 (fun (fl, s, m, ug) -> C.CoFix (i, fl), s, m, ug) fl',
929 C.CoFix (i, lifted_fl))
933 | C.Meta _ when (not metas_ok) ->
937 (* if match_only then replace_metas context term *)
941 let subst', metasenv', ugraph' =
942 (* Printf.printf "provo a unificare %s e %s\n" *)
943 (* (CicPp.ppterm (S.lift lift_amount what)) (CicPp.ppterm term); *)
945 matching metasenv context term (S.lift lift_amount what) ugraph
947 CicUnification.fo_unif metasenv context
948 (S.lift lift_amount what) term ugraph
950 (* Printf.printf "Ok, trovato: %s\n\nwhat: %s" (CicPp.ppterm term) *)
951 (* (CicPp.ppterm (S.lift lift_amount what)); *)
952 (* Printf.printf "substitution:\n%s\n\n" (print_subst subst'); *)
953 (* Printf.printf "metasenv': %s\n" (print_metasenv metasenv'); *)
954 (* Printf.printf "metasenv: %s\n\n" (print_metasenv metasenv); *)
955 (* if match_only then *)
956 (* let t' = CicMetaSubst.apply_subst subst' term in *)
957 (* if not (meta_convertibility term t') then ( *)
958 (* res, lifted_term *)
960 (* let metas = metas_of_term term in *)
961 (* let fix_subst = function *)
962 (* | (i, (c, C.Meta (j, lc), ty)) when List.mem i metas -> *)
963 (* (j, (c, C.Meta (i, lc), ty)) *)
966 (* let subst' = List.map fix_subst subst' in *)
967 (* ((C.Rel (1 + lift_amount), subst', metasenv', ugraph')::res, *)
971 ((C.Rel (1 + lift_amount), subst', metasenv', ugraph')::res,
975 | CicUnification.UnificationFailure _
976 | CicUnification.Uncertain _ ->
979 (* Printf.printf "exit aux\n"; *)
982 and aux_list lift_amount l context metasenv subst ugraph =
984 (fun arg (res, lifted_tl) ->
985 let arg_res, lifted_arg =
986 aux lift_amount arg context metasenv subst ugraph in
988 (fun (a, s, m, ug) -> a::lifted_tl, s, m, ug) arg_res
991 (fun (r, s, m, ug) -> lifted_arg::r, s, m, ug) res),
992 lifted_arg::lifted_tl)
995 and aux_ens lift_amount exp_named_subst context metasenv subst ugraph =
997 (fun (u, arg) (res, lifted_tl) ->
998 let arg_res, lifted_arg =
999 aux lift_amount arg context metasenv subst ugraph in
1002 (fun (a, s, m, ug) -> (u, a)::lifted_tl, s, m, ug) arg_res
1004 (l1 @ (List.map (fun (r, s, m, ug) ->
1005 (u, lifted_arg)::r, s, m, ug) res),
1006 (u, lifted_arg)::lifted_tl)
1007 ) exp_named_subst ([], [])
1012 (* if match_only then replace_metas (\* context *\) where *)
1015 aux 0 where context metasenv [] ugraph
1018 (* if match_only then *)
1019 (* (fun (term, subst, metasenv, ugraph) -> *)
1021 (* C.Lambda (C.Anonymous, type_of_what, restore_metas term) *)
1022 (* and subst = restore_subst subst in *)
1023 (* (term', subst, metasenv, ugraph)) *)
1025 (fun (term, subst, metasenv, ugraph) ->
1026 let term' = C.Lambda (C.Anonymous, type_of_what, term) in
1027 (term', subst, metasenv, ugraph))
1029 List.map mapfun expansions
1033 let find_equalities ?(eq_uri=HelmLibraryObjects.Logic.eq_URI) context proof =
1034 let module C = Cic in
1035 let module S = CicSubstitution in
1036 let module T = CicTypeChecker in
1037 let newmeta = ProofEngineHelpers.new_meta_of_proof ~proof in
1038 let ok_types ty menv =
1039 List.for_all (fun (_, _, mt) -> mt = ty) menv
1041 let rec aux index newmeta = function
1043 | (Some (_, C.Decl (term)))::tl ->
1044 let do_find context term =
1046 | C.Prod (name, s, t) ->
1047 (* let newmeta = ProofEngineHelpers.new_meta_of_proof ~proof in *)
1048 let (head, newmetas, args, newmeta) =
1049 ProofEngineHelpers.saturate_term newmeta []
1050 context (S.lift index term)
1053 if List.length args = 0 then
1056 C.Appl ((C.Rel index)::args)
1059 | C.Appl [C.MutInd (uri, _, _); ty; t1; t2]
1060 when (UriManager.eq uri eq_uri) && (ok_types ty newmetas) ->
1062 Printf.sprintf "OK: %s" (CicPp.ppterm term));
1064 (* Printf.sprintf "args: %s\n" *)
1065 (* (String.concat ", " (List.map CicPp.ppterm args))); *)
1067 (* Printf.sprintf "newmetas:\n%s\n" *)
1068 (* (print_metasenv newmetas)); *)
1069 let o = !Utils.compare_terms t1 t2 in
1070 let w = compute_equality_weight ty t1 t2 in
1071 let proof = BasicProof p in
1072 let e = (w, proof, (ty, t1, t2, o), newmetas, args) in
1074 | _ -> None, newmeta
1076 | C.Appl [C.MutInd (uri, _, _); ty; t1; t2]
1077 when UriManager.eq uri eq_uri ->
1078 let t1 = S.lift index t1
1079 and t2 = S.lift index t2 in
1080 let o = !Utils.compare_terms t1 t2 in
1081 let w = compute_equality_weight ty t1 t2 in
1082 let e = (w, BasicProof (C.Rel index), (ty, t1, t2, o), [], []) in
1084 | _ -> None, newmeta
1086 match do_find context term with
1087 | Some p, newmeta ->
1088 let tl, newmeta' = (aux (index+1) newmeta tl) in
1089 p::tl, max newmeta newmeta'
1091 aux (index+1) newmeta tl
1094 aux (index+1) newmeta tl
1096 aux 1 newmeta context
1100 let equations_blacklist =
1102 (fun s u -> UriManager.UriSet.add (UriManager.uri_of_string u) s)
1103 UriManager.UriSet.empty [
1104 "cic:/Coq/Init/Logic/eq.ind#xpointer(1/1/1)";
1105 "cic:/Coq/Init/Logic/trans_eq.con";
1106 "cic:/Coq/Init/Logic/f_equal.con";
1107 "cic:/Coq/Init/Logic/f_equal2.con";
1108 "cic:/Coq/Init/Logic/f_equal3.con";
1109 "cic:/Coq/Init/Logic/sym_eq.con";
1110 (* "cic:/Coq/Logic/Eqdep/UIP_refl.con"; *)
1111 (* "cic:/Coq/Init/Peano/mult_n_Sm.con"; *)
1115 let find_library_equalities ~(dbd:Mysql.dbd) context status maxmeta =
1116 let module C = Cic in
1117 let module S = CicSubstitution in
1118 let module T = CicTypeChecker in
1122 if UriManager.UriSet.mem uri equations_blacklist then
1125 let t = CicUtil.term_of_uri uri in
1127 CicTypeChecker.type_of_aux' [] context t CicUniv.empty_ugraph
1131 (MetadataQuery.equations_for_goal ~dbd status)
1133 let eq_uri1 = UriManager.uri_of_string HelmLibraryObjects.Logic.eq_XURI
1134 and eq_uri2 = HelmLibraryObjects.Logic.eq_URI in
1136 (UriManager.eq uri eq_uri1) || (UriManager.eq uri eq_uri2)
1138 let ok_types ty menv =
1139 List.for_all (fun (_, _, mt) -> mt = ty) menv
1141 let rec aux newmeta = function
1143 | (term, termty)::tl ->
1146 | C.Prod (name, s, t) ->
1147 let head, newmetas, args, newmeta =
1148 ProofEngineHelpers.saturate_term newmeta [] context termty
1151 if List.length args = 0 then
1157 | C.Appl [C.MutInd (uri, _, _); ty; t1; t2]
1158 when (iseq uri) && (ok_types ty newmetas) ->
1160 Printf.sprintf "OK: %s" (CicPp.ppterm term));
1161 let o = !Utils.compare_terms t1 t2 in
1162 let w = compute_equality_weight ty t1 t2 in
1163 let proof = BasicProof p in
1164 let e = (w, proof, (ty, t1, t2, o), newmetas, args) in
1166 | _ -> None, newmeta
1168 | C.Appl [C.MutInd (uri, _, _); ty; t1; t2] when iseq uri ->
1169 let o = !Utils.compare_terms t1 t2 in
1170 let w = compute_equality_weight ty t1 t2 in
1171 let e = (w, BasicProof term, (ty, t1, t2, o), [], []) in
1173 | _ -> None, newmeta
1177 let tl, newmeta' = aux newmeta tl in
1178 e::tl, max newmeta newmeta'
1182 aux maxmeta candidates
1186 let fix_metas newmeta ((w, p, (ty, left, right, o), menv, args) as equality) =
1187 (* print_endline ("fix_metas " ^ (string_of_int newmeta)); *)
1188 let table = Hashtbl.create (List.length args) in
1189 let is_this_case = ref false in
1190 let newargs, newmeta =
1192 (fun t (newargs, index) ->
1194 | Cic.Meta (i, l) ->
1195 Hashtbl.add table i index;
1196 (* if index = 5469 then ( *)
1197 (* Printf.printf "?5469 COMES FROM (%d): %s\n" *)
1198 (* i (string_of_equality equality); *)
1199 (* print_newline (); *)
1200 (* is_this_case := true *)
1202 ((Cic.Meta (index, l))::newargs, index+1)
1203 | _ -> assert false)
1204 args ([], newmeta+1)
1207 ProofEngineReduction.replace ~equality:(=) ~what:args ~with_what:newargs
1212 (fun (i, context, term) menv ->
1214 let index = Hashtbl.find table i in
1215 (index, context, term)::menv
1217 (i, context, term)::menv)
1221 and left = repl left
1222 and right = repl right in
1223 let metas = (metas_of_term left) @ (metas_of_term right) in
1224 let menv' = List.filter (fun (i, _, _) -> List.mem i metas) menv'
1227 (function Cic.Meta (i, _) -> List.mem i metas | _ -> assert false) newargs
1229 let rec fix_proof = function
1230 | NoProof -> NoProof
1231 | BasicProof term -> BasicProof (repl term)
1232 | ProofBlock (subst, eq_URI, t', (pos, eq), p) ->
1234 (* Printf.printf "fix_proof of equality %s, subst is:\n%s\n" *)
1235 (* (string_of_equality equality) (print_subst subst); *)
1241 | Cic.Meta (i, l) -> (
1243 let j = Hashtbl.find table i in
1244 if List.mem_assoc i subst then
1247 (* let _, context, ty = CicUtil.lookup_meta j menv' in *)
1248 (* (i, (context, Cic.Meta (j, l), ty))::s *)
1249 let _, context, ty = CicUtil.lookup_meta i menv in
1250 (i, (context, Cic.Meta (j, l), ty))::s
1253 | _ -> assert false)
1258 (* (fun (i, e) -> *)
1259 (* try let j = Hashtbl.find table i in (j, e) *)
1260 (* with _ -> (i, e)) subst *)
1263 (* Printf.printf "subst' is:\n%s\n" (print_subst subst'); *)
1264 (* print_newline (); *)
1266 ProofBlock (subst' @ subst, eq_URI, t', (pos, eq), p)
1267 (* | ProofSymBlock (ens, p) -> *)
1268 (* let ens' = List.map (fun (u, t) -> (u, repl t)) ens in *)
1269 (* ProofSymBlock (ens', fix_proof p) *)
1272 (* (newmeta + (List.length newargs) + 2, *)
1273 let neweq = (w, fix_proof p, (ty, left, right, o), menv', newargs) in
1274 (* if !is_this_case then ( *)
1275 (* print_endline "\nTHIS IS THE TROUBLE!!!"; *)
1276 (* let pt = build_proof_term neweq in *)
1277 (* Printf.printf "equality: %s\nproof: %s\n" *)
1278 (* (string_of_equality neweq) (CicPp.ppterm pt); *)
1279 (* print_endline (String.make 79 '-'); *)
1281 (newmeta + 1, neweq)
1282 (* (w, fix_proof p, (ty, left, right, o), menv', newargs)) *)
1286 let term_is_equality ?(eq_uri=HelmLibraryObjects.Logic.eq_URI) term =
1287 let iseq uri = UriManager.eq uri eq_uri in
1289 | Cic.Appl [Cic.MutInd (uri, _, _); _; _; _] when iseq uri -> true
1294 exception TermIsNotAnEquality;;
1296 let equality_of_term ?(eq_uri=HelmLibraryObjects.Logic.eq_URI) proof term =
1297 let iseq uri = UriManager.eq uri eq_uri in
1299 | Cic.Appl [Cic.MutInd (uri, _, _); ty; t1; t2] when iseq uri ->
1300 let o = !Utils.compare_terms t1 t2 in
1301 let w = compute_equality_weight ty t1 t2 in
1302 let e = (w, BasicProof proof, (ty, t1, t2, o), [], []) in
1304 (* (proof, (ty, t1, t2, o), [], []) *)
1306 raise TermIsNotAnEquality
1310 type environment = Cic.metasenv * Cic.context * CicUniv.universe_graph;;
1314 let superposition_left (metasenv, context, ugraph) target source =
1315 let module C = Cic in
1316 let module S = CicSubstitution in
1317 let module M = CicMetaSubst in
1318 let module HL = HelmLibraryObjects in
1319 let module CR = CicReduction in
1320 (* we assume that target is ground (does not contain metavariables): this
1321 * should always be the case (I hope, at least) *)
1322 let proof, (eq_ty, left, right, t_order), _, _ = target in
1323 let eqproof, (ty, t1, t2, s_order), newmetas, args = source in
1325 let compare_terms = !Utils.compare_terms in
1330 let where, is_left =
1331 match t_order (* compare_terms left right *) with
1332 | Lt -> right, false
1335 Printf.printf "????????? %s = %s" (CicPp.ppterm left)
1336 (CicPp.ppterm right);
1338 assert false (* again, for ground terms this shouldn't happen... *)
1341 let metasenv' = newmetas @ metasenv in
1342 let result = s_order (* compare_terms t1 t2 *) in
1345 | Gt -> (beta_expand t1 ty where context metasenv' ugraph), []
1346 | Lt -> [], (beta_expand t2 ty where context metasenv' ugraph)
1350 (fun (t, s, m, ug) ->
1351 compare_terms (M.apply_subst s t1) (M.apply_subst s t2) = Gt)
1352 (beta_expand t1 ty where context metasenv' ugraph)
1355 (fun (t, s, m, ug) ->
1356 compare_terms (M.apply_subst s t2) (M.apply_subst s t1) = Gt)
1357 (beta_expand t2 ty where context metasenv' ugraph)
1361 (* let what, other = *)
1362 (* if is_left then left, right *)
1363 (* else right, left *)
1365 let build_new what other eq_URI (t, s, m, ug) =
1366 let newgoal, newgoalproof =
1368 | C.Lambda (nn, ty, bo) ->
1369 let bo' = S.subst (M.apply_subst s other) bo in
1372 [C.MutInd (HL.Logic.eq_URI, 0, []);
1374 if is_left then [bo'; S.lift 1 right]
1375 else [S.lift 1 left; bo'])
1377 let t' = C.Lambda (nn, ty, bo'') in
1378 S.subst (M.apply_subst s other) bo,
1380 (C.Appl [C.Const (eq_URI, []); ty; what; t';
1381 proof; other; eqproof])
1385 if is_left then (eq_ty, newgoal, right, compare_terms newgoal right)
1386 else (eq_ty, left, newgoal, compare_terms left newgoal)
1388 (newgoalproof (* eqproof *), equation, [], [])
1390 let new1 = List.map (build_new t1 t2 HL.Logic.eq_ind_URI) res1
1391 and new2 = List.map (build_new t2 t1 HL.Logic.eq_ind_r_URI) res2 in
1396 let superposition_right newmeta (metasenv, context, ugraph) target source =
1397 let module C = Cic in
1398 let module S = CicSubstitution in
1399 let module M = CicMetaSubst in
1400 let module HL = HelmLibraryObjects in
1401 let module CR = CicReduction in
1402 let eqproof, (eq_ty, left, right, t_order), newmetas, args = target in
1403 let eqp', (ty', t1, t2, s_order), newm', args' = source in
1404 let maxmeta = ref newmeta in
1406 let compare_terms = !Utils.compare_terms in
1408 if eq_ty <> ty' then
1411 (* let ok term subst other other_eq_side ugraph = *)
1412 (* match term with *)
1413 (* | C.Lambda (nn, ty, bo) -> *)
1414 (* let bo' = S.subst (M.apply_subst subst other) bo in *)
1415 (* let res, _ = CR.are_convertible context bo' other_eq_side ugraph in *)
1417 (* | _ -> assert false *)
1419 let condition left right what other (t, s, m, ug) =
1420 let subst = M.apply_subst s in
1421 let cmp1 = compare_terms (subst what) (subst other) in
1422 let cmp2 = compare_terms (subst left) (subst right) in
1423 (* cmp1 = Gt && cmp2 = Gt *)
1424 cmp1 <> Lt && cmp1 <> Le && cmp2 <> Lt && cmp2 <> Le
1425 (* && (ok t s other right ug) *)
1427 let metasenv' = metasenv @ newmetas @ newm' in
1428 let beta_expand = beta_expand ~metas_ok:false in
1429 let cmp1 = t_order (* compare_terms left right *)
1430 and cmp2 = s_order (* compare_terms t1 t2 *) in
1431 let res1, res2, res3, res4 =
1435 (beta_expand s eq_ty l context metasenv' ugraph)
1437 match cmp1, cmp2 with
1439 (beta_expand t1 eq_ty left context metasenv' ugraph), [], [], []
1441 [], (beta_expand t2 eq_ty left context metasenv' ugraph), [], []
1443 [], [], (beta_expand t1 eq_ty right context metasenv' ugraph), []
1445 [], [], [], (beta_expand t2 eq_ty right context metasenv' ugraph)
1447 let res1 = res left right t1 t2
1448 and res2 = res left right t2 t1 in
1451 let res3 = res right left t1 t2
1452 and res4 = res right left t2 t1 in
1455 let res1 = res left right t1 t2
1456 and res3 = res right left t1 t2 in
1459 let res2 = res left right t2 t1
1460 and res4 = res right left t2 t1 in
1463 let res1 = res left right t1 t2
1464 and res2 = res left right t2 t1
1465 and res3 = res right left t1 t2
1466 and res4 = res right left t2 t1 in
1467 res1, res2, res3, res4
1469 let newmetas = newmetas @ newm' in
1470 let newargs = args @ args' in
1471 let build_new what other is_left eq_URI (t, s, m, ug) =
1472 (* let what, other = *)
1473 (* if is_left then left, right *)
1474 (* else right, left *)
1476 let newterm, neweqproof =
1478 | C.Lambda (nn, ty, bo) ->
1479 let bo' = M.apply_subst s (S.subst other bo) in
1482 [C.MutInd (HL.Logic.eq_URI, 0, []); S.lift 1 eq_ty] @
1483 if is_left then [bo'; S.lift 1 right]
1484 else [S.lift 1 left; bo'])
1486 let t' = C.Lambda (nn, ty, bo'') in
1489 (C.Appl [C.Const (eq_URI, []); ty; what; t';
1490 eqproof; other; eqp'])
1493 let newmeta, newequality =
1495 if is_left then (newterm, M.apply_subst s right)
1496 else (M.apply_subst s left, newterm) in
1497 let neworder = compare_terms left right in
1499 (neweqproof, (eq_ty, left, right, neworder), newmetas, newargs)
1504 let new1 = List.map (build_new t1 t2 true HL.Logic.eq_ind_URI) res1
1505 and new2 = List.map (build_new t2 t1 true HL.Logic.eq_ind_r_URI) res2
1506 and new3 = List.map (build_new t1 t2 false HL.Logic.eq_ind_URI) res3
1507 and new4 = List.map (build_new t2 t1 false HL.Logic.eq_ind_r_URI) res4 in
1509 | _, (_, left, right, _), _, _ ->
1510 not (fst (CR.are_convertible context left right ugraph))
1513 (List.filter ok (new1 @ new2 @ new3 @ new4)))
1518 let is_identity ((_, context, ugraph) as env) = function
1519 | ((_, _, (ty, left, right, _), _, _) as equality) ->
1521 (fst (CicReduction.are_convertible context left right ugraph)))
1526 let demodulation newmeta (metasenv, context, ugraph) target source =
1527 let module C = Cic in
1528 let module S = CicSubstitution in
1529 let module M = CicMetaSubst in
1530 let module HL = HelmLibraryObjects in
1531 let module CR = CicReduction in
1533 let proof, (eq_ty, left, right, t_order), metas, args = target
1534 and proof', (ty, t1, t2, s_order), metas', args' = source in
1536 let compare_terms = !Utils.compare_terms in
1541 let first_step, get_params =
1542 match s_order (* compare_terms t1 t2 *) with
1543 | Gt -> 1, (function
1544 | 1 -> true, t1, t2, HL.Logic.eq_ind_URI
1545 | 0 -> false, t1, t2, HL.Logic.eq_ind_URI
1546 | _ -> assert false)
1547 | Lt -> 1, (function
1548 | 1 -> true, t2, t1, HL.Logic.eq_ind_r_URI
1549 | 0 -> false, t2, t1, HL.Logic.eq_ind_r_URI
1550 | _ -> assert false)
1552 let first_step = 3 in
1553 let get_params step =
1555 | 3 -> true, t1, t2, HL.Logic.eq_ind_URI
1556 | 2 -> false, t1, t2, HL.Logic.eq_ind_URI
1557 | 1 -> true, t2, t1, HL.Logic.eq_ind_r_URI
1558 | 0 -> false, t2, t1, HL.Logic.eq_ind_r_URI
1561 first_step, get_params
1563 let rec demodulate newmeta step metasenv target =
1564 let proof, (eq_ty, left, right, t_order), metas, args = target in
1565 let is_left, what, other, eq_URI = get_params step in
1567 let env = metasenv, context, ugraph in
1568 let names = names_of_context context in
1570 (* "demodulate\ntarget: %s\nwhat: %s\nother: %s\nis_left: %s\n" *)
1571 (* (string_of_equality ~env target) (CicPp.pp what names) *)
1572 (* (CicPp.pp other names) (string_of_bool is_left); *)
1573 (* Printf.printf "step: %d" step; *)
1574 (* print_newline (); *)
1576 let ok (t, s, m, ug) =
1577 compare_terms (M.apply_subst s what) (M.apply_subst s other) = Gt
1580 let r = (beta_expand ~metas_ok:false ~match_only:true
1581 what ty (if is_left then left else right)
1582 context (metasenv @ metas) ugraph)
1584 (* let m' = metas_of_term what *)
1585 (* and m'' = metas_of_term (if is_left then left else right) in *)
1586 (* if (List.mem 527 m'') && (List.mem 6 m') then ( *)
1588 (* "demodulate\ntarget: %s\nwhat: %s\nother: %s\nis_left: %s\n" *)
1589 (* (string_of_equality ~env target) (CicPp.pp what names) *)
1590 (* (CicPp.pp other names) (string_of_bool is_left); *)
1591 (* Printf.printf "step: %d" step; *)
1592 (* print_newline (); *)
1593 (* print_endline "res:"; *)
1594 (* List.iter (fun (t, s, m, ug) -> print_endline (CicPp.pp t names)) r; *)
1595 (* print_newline (); *)
1596 (* Printf.printf "metasenv:\n%s\n" (print_metasenv (metasenv @ metas)); *)
1597 (* print_newline (); *)
1603 if step = 0 then newmeta, target
1604 else demodulate newmeta (step-1) metasenv target
1605 | (t, s, m, ug)::_ ->
1606 let newterm, newproof =
1608 | C.Lambda (nn, ty, bo) ->
1609 (* let bo' = M.apply_subst s (S.subst other bo) in *)
1610 let bo' = S.subst (M.apply_subst s other) bo in
1613 [C.MutInd (HL.Logic.eq_URI, 0, []);
1615 if is_left then [bo'; S.lift 1 right]
1616 else [S.lift 1 left; bo'])
1618 let t' = C.Lambda (nn, ty, bo'') in
1619 (* M.apply_subst s (S.subst other bo), *)
1622 (C.Appl [C.Const (eq_URI, []); ty; what; t';
1623 proof; other; proof'])
1626 let newmeta, newtarget =
1628 (* if is_left then (newterm, M.apply_subst s right) *)
1629 (* else (M.apply_subst s left, newterm) in *)
1630 if is_left then newterm, right
1633 let neworder = compare_terms left right in
1634 (* let newmetasenv = metasenv @ metas in *)
1635 (* let newargs = args @ args' in *)
1636 (* fix_metas newmeta *)
1637 (* (newproof, (eq_ty, left, right), newmetasenv, newargs) *)
1638 let m = (metas_of_term left) @ (metas_of_term right) in
1639 let newmetasenv = List.filter (fun (i, _, _) -> List.mem i m) metas
1642 (function C.Meta (i, _) -> List.mem i m | _ -> assert false)
1646 (newproof, (eq_ty, left, right, neworder), newmetasenv, newargs)
1649 (* "demodulate, newtarget: %s\ntarget was: %s\n" *)
1650 (* (string_of_equality ~env newtarget) *)
1651 (* (string_of_equality ~env target); *)
1652 (* (\* let _, _, newm, newa = newtarget in *\) *)
1653 (* (\* Printf.printf "newmetasenv:\n%s\nnewargs:\n%s\n" *\) *)
1654 (* (\* (print_metasenv newm) *\) *)
1655 (* (\* (String.concat "\n" (List.map CicPp.ppterm newa)); *\) *)
1656 (* print_newline (); *)
1657 if is_identity env newtarget then
1660 demodulate newmeta first_step metasenv newtarget
1662 demodulate newmeta first_step (metasenv @ metas') target
1667 let demodulation newmeta env target source =
1673 let subsumption env target source =
1674 let _, (ty, tl, tr, _), tmetas, _ = target
1675 and _, (ty', sl, sr, _), smetas, _ = source in
1679 let metasenv, context, ugraph = env in
1680 let metasenv = metasenv @ tmetas @ smetas in
1681 let names = names_of_context context in
1682 let samesubst subst subst' =
1683 (* Printf.printf "samesubst:\nsubst: %s\nsubst': %s\n" *)
1684 (* (print_subst subst) (print_subst subst'); *)
1685 (* print_newline (); *)
1686 let tbl = Hashtbl.create (List.length subst) in
1687 List.iter (fun (m, (c, t1, t2)) -> Hashtbl.add tbl m (c, t1, t2)) subst;
1689 (fun (m, (c, t1, t2)) ->
1691 let c', t1', t2' = Hashtbl.find tbl m in
1692 if (c = c') && (t1 = t1') && (t2 = t2') then true
1698 let subsaux left right left' right' =
1700 let subst, menv, ug = matching metasenv context left left' ugraph
1701 and subst', menv', ug' = matching metasenv context right right' ugraph
1703 (* Printf.printf "left = right: %s = %s\n" *)
1704 (* (CicPp.pp left names) (CicPp.pp right names); *)
1705 (* Printf.printf "left' = right': %s = %s\n" *)
1706 (* (CicPp.pp left' names) (CicPp.pp right' names); *)
1707 samesubst subst subst'
1709 (* print_endline (Printexc.to_string e); *)
1713 if subsaux tl tr sl sr then true
1714 else subsaux tl tr sr sl
1717 Printf.printf "subsumption!:\ntarget: %s\nsource: %s\n"
1718 (string_of_equality ~env target) (string_of_equality ~env source);
1726 let extract_differing_subterms t1 t2 =
1727 let module C = Cic in
1730 | C.Appl l1, C.Appl l2 when (List.length l1) <> (List.length l2) ->
1732 | C.Appl (h1::tl1), C.Appl (h2::tl2) ->
1733 let res = List.concat (List.map2 aux tl1 tl2) in
1735 if res = [] then [(h1, h2)] else [(t1, t2)]
1737 if List.length res > 1 then [(t1, t2)] else res
1739 if t1 <> t2 then [(t1, t2)] else []
1741 let res = aux t1 t2 in