130 | _ => ct); |
130 | _ => ct); |
131 |
131 |
132 (*The premises of a theorem, as a cterm list*) |
132 (*The premises of a theorem, as a cterm list*) |
133 val cprems_of = strip_imp_prems o Thm.cprop_of; |
133 val cprems_of = strip_imp_prems o Thm.cprop_of; |
134 |
134 |
135 fun cterm_fun f ct = Thm.cterm_of (Thm.theory_of_cterm ct) (f (Thm.term_of ct)); |
135 fun cterm_fun f ct = Thm.global_cterm_of (Thm.theory_of_cterm ct) (f (Thm.term_of ct)); |
136 fun ctyp_fun f cT = Thm.ctyp_of (Thm.theory_of_ctyp cT) (f (Thm.typ_of cT)); |
136 fun ctyp_fun f cT = Thm.global_ctyp_of (Thm.theory_of_ctyp cT) (f (Thm.typ_of cT)); |
137 |
137 |
138 fun certify t = Thm.cterm_of (Context.the_theory (Context.the_thread_data ())) t; |
138 fun certify t = Thm.global_cterm_of (Context.the_theory (Context.the_thread_data ())) t; |
139 |
139 |
140 val implies = certify Logic.implies; |
140 val implies = certify Logic.implies; |
141 fun mk_implies (A, B) = Thm.apply (Thm.apply implies A) B; |
141 fun mk_implies (A, B) = Thm.apply (Thm.apply implies A) B; |
142 |
142 |
143 (*cterm version of list_implies: [A1,...,An], B goes to [|A1;==>;An|]==>B *) |
143 (*cterm version of list_implies: [A1,...,An], B goes to [|A1;==>;An|]==>B *) |
199 val forall_intr_list = fold_rev Thm.forall_intr; |
199 val forall_intr_list = fold_rev Thm.forall_intr; |
200 |
200 |
201 (*Generalization over Vars -- canonical order*) |
201 (*Generalization over Vars -- canonical order*) |
202 fun forall_intr_vars th = |
202 fun forall_intr_vars th = |
203 fold Thm.forall_intr |
203 fold Thm.forall_intr |
204 (map (Thm.cterm_of (Thm.theory_of_thm th) o Var) (Thm.fold_terms Term.add_vars th [])) th; |
204 (map (Thm.global_cterm_of (Thm.theory_of_thm th) o Var) (Thm.fold_terms Term.add_vars th [])) th; |
205 |
205 |
206 fun outer_params t = |
206 fun outer_params t = |
207 let val vs = Term.strip_all_vars t |
207 let val vs = Term.strip_all_vars t |
208 in Name.variant_list [] (map (Name.clean o #1) vs) ~~ map #2 vs end; |
208 in Name.variant_list [] (map (Name.clean o #1) vs) ~~ map #2 vs end; |
209 |
209 |
210 (*generalize outermost parameters*) |
210 (*generalize outermost parameters*) |
211 fun gen_all th = |
211 fun gen_all th = |
212 let |
212 let |
213 val {thy, prop, maxidx, ...} = Thm.rep_thm th; |
213 val {thy, prop, maxidx, ...} = Thm.rep_thm th; |
214 fun elim (x, T) = Thm.forall_elim (Thm.cterm_of thy (Var ((x, maxidx + 1), T))); |
214 fun elim (x, T) = Thm.forall_elim (Thm.global_cterm_of thy (Var ((x, maxidx + 1), T))); |
215 in fold elim (outer_params prop) th end; |
215 in fold elim (outer_params prop) th end; |
216 |
216 |
217 (*lift vars wrt. outermost goal parameters |
217 (*lift vars wrt. outermost goal parameters |
218 -- reverses the effect of gen_all modulo higher-order unification*) |
218 -- reverses the effect of gen_all modulo higher-order unification*) |
219 fun lift_all goal th = |
219 fun lift_all goal th = |
222 val maxidx = Thm.maxidx_of th; |
222 val maxidx = Thm.maxidx_of th; |
223 val ps = outer_params (Thm.term_of goal) |
223 val ps = outer_params (Thm.term_of goal) |
224 |> map (fn (x, T) => Var ((x, maxidx + 1), Logic.incr_tvar (maxidx + 1) T)); |
224 |> map (fn (x, T) => Var ((x, maxidx + 1), Logic.incr_tvar (maxidx + 1) T)); |
225 val Ts = map Term.fastype_of ps; |
225 val Ts = map Term.fastype_of ps; |
226 val inst = Thm.fold_terms Term.add_vars th [] |> map (fn (xi, T) => |
226 val inst = Thm.fold_terms Term.add_vars th [] |> map (fn (xi, T) => |
227 (Thm.cterm_of thy (Var (xi, T)), Thm.cterm_of thy (Term.list_comb (Var (xi, Ts ---> T), ps)))); |
227 (Thm.global_cterm_of thy (Var (xi, T)), Thm.global_cterm_of thy (Term.list_comb (Var (xi, Ts ---> T), ps)))); |
228 in |
228 in |
229 th |> Thm.instantiate ([], inst) |
229 th |> Thm.instantiate ([], inst) |
230 |> fold_rev (Thm.forall_intr o Thm.cterm_of thy) ps |
230 |> fold_rev (Thm.forall_intr o Thm.global_cterm_of thy) ps |
231 end; |
231 end; |
232 |
232 |
233 (*direct generalization*) |
233 (*direct generalization*) |
234 fun generalize names th = Thm.generalize names (Thm.maxidx_of th + 1) th; |
234 fun generalize names th = Thm.generalize names (Thm.maxidx_of th + 1) th; |
235 |
235 |
246 fun zero_var_indexes_list [] = [] |
246 fun zero_var_indexes_list [] = [] |
247 | zero_var_indexes_list ths = |
247 | zero_var_indexes_list ths = |
248 let |
248 let |
249 val thy = Theory.merge_list (map Thm.theory_of_thm ths); |
249 val thy = Theory.merge_list (map Thm.theory_of_thm ths); |
250 val (instT, inst) = Term_Subst.zero_var_indexes_inst (map Thm.full_prop_of ths); |
250 val (instT, inst) = Term_Subst.zero_var_indexes_inst (map Thm.full_prop_of ths); |
251 val cinstT = map (fn (v, T) => (Thm.ctyp_of thy (TVar v), Thm.ctyp_of thy T)) instT; |
251 val cinstT = map (fn (v, T) => (Thm.global_ctyp_of thy (TVar v), Thm.global_ctyp_of thy T)) instT; |
252 val cinst = map (fn (v, t) => (Thm.cterm_of thy (Var v), Thm.cterm_of thy t)) inst; |
252 val cinst = map (fn (v, t) => (Thm.global_cterm_of thy (Var v), Thm.global_cterm_of thy t)) inst; |
253 in map (Thm.adjust_maxidx_thm ~1 o Thm.instantiate (cinstT, cinst)) ths end; |
253 in map (Thm.adjust_maxidx_thm ~1 o Thm.instantiate (cinstT, cinst)) ths end; |
254 |
254 |
255 val zero_var_indexes = singleton zero_var_indexes_list; |
255 val zero_var_indexes = singleton zero_var_indexes_list; |
256 |
256 |
257 |
257 |
642 |
642 |
643 fun mk_term ct = |
643 fun mk_term ct = |
644 let |
644 let |
645 val thy = Thm.theory_of_cterm ct; |
645 val thy = Thm.theory_of_cterm ct; |
646 val T = Thm.typ_of_cterm ct; |
646 val T = Thm.typ_of_cterm ct; |
647 val a = Thm.ctyp_of thy (TVar (("'a", 0), [])); |
647 val a = Thm.global_ctyp_of thy (TVar (("'a", 0), [])); |
648 val x = Thm.cterm_of thy (Var (("x", 0), T)); |
648 val x = Thm.global_cterm_of thy (Var (("x", 0), T)); |
649 in Thm.instantiate ([(a, Thm.ctyp_of thy T)], [(x, ct)]) termI end; |
649 in Thm.instantiate ([(a, Thm.global_ctyp_of thy T)], [(x, ct)]) termI end; |
650 |
650 |
651 fun dest_term th = |
651 fun dest_term th = |
652 let val cprop = strip_imp_concl (Thm.cprop_of th) in |
652 let val cprop = strip_imp_concl (Thm.cprop_of th) in |
653 if can Logic.dest_term (Thm.term_of cprop) then |
653 if can Logic.dest_term (Thm.term_of cprop) then |
654 Thm.dest_arg cprop |
654 Thm.dest_arg cprop |
787 | cterm_instantiate ctpairs th = |
787 | cterm_instantiate ctpairs th = |
788 let |
788 let |
789 val (thy, tye, _) = fold_rev add_types ctpairs (Thm.theory_of_thm th, Vartab.empty, 0); |
789 val (thy, tye, _) = fold_rev add_types ctpairs (Thm.theory_of_thm th, Vartab.empty, 0); |
790 val instT = |
790 val instT = |
791 Vartab.fold (fn (xi, (S, T)) => |
791 Vartab.fold (fn (xi, (S, T)) => |
792 cons (Thm.ctyp_of thy (TVar (xi, S)), Thm.ctyp_of thy (Envir.norm_type tye T))) tye []; |
792 cons (Thm.global_ctyp_of thy (TVar (xi, S)), Thm.global_ctyp_of thy (Envir.norm_type tye T))) tye []; |
793 val inst = map (apply2 (Thm.instantiate_cterm (instT, []))) ctpairs; |
793 val inst = map (apply2 (Thm.instantiate_cterm (instT, []))) ctpairs; |
794 in instantiate_normalize (instT, inst) th end |
794 in instantiate_normalize (instT, inst) th end |
795 handle TERM (msg, _) => raise THM (msg, 0, [th]) |
795 handle TERM (msg, _) => raise THM (msg, 0, [th]) |
796 | TYPE (msg, _, _) => raise THM (msg, 0, [th]); |
796 | TYPE (msg, _, _) => raise THM (msg, 0, [th]); |
797 end; |
797 end; |
805 raise TYPE ("instantiate': " ^ msg, |
805 raise TYPE ("instantiate': " ^ msg, |
806 map_filter (Option.map Thm.typ_of) cTs, |
806 map_filter (Option.map Thm.typ_of) cTs, |
807 map_filter (Option.map Thm.term_of) cts); |
807 map_filter (Option.map Thm.term_of) cts); |
808 |
808 |
809 fun inst_of (v, ct) = |
809 fun inst_of (v, ct) = |
810 (Thm.cterm_of (Thm.theory_of_cterm ct) (Var v), ct) |
810 (Thm.global_cterm_of (Thm.theory_of_cterm ct) (Var v), ct) |
811 handle TYPE (msg, _, _) => err msg; |
811 handle TYPE (msg, _, _) => err msg; |
812 |
812 |
813 fun tyinst_of (v, cT) = |
813 fun tyinst_of (v, cT) = |
814 (Thm.ctyp_of (Thm.theory_of_ctyp cT) (TVar v), cT) |
814 (Thm.global_ctyp_of (Thm.theory_of_ctyp cT) (TVar v), cT) |
815 handle TYPE (msg, _, _) => err msg; |
815 handle TYPE (msg, _, _) => err msg; |
816 |
816 |
817 fun zip_vars xs ys = |
817 fun zip_vars xs ys = |
818 zip_options xs ys handle ListPair.UnequalLengths => |
818 zip_options xs ys handle ListPair.UnequalLengths => |
819 err "more instantiations than variables in thm"; |
819 err "more instantiations than variables in thm"; |
841 let |
841 let |
842 val thy = Thm.theory_of_thm thm; |
842 val thy = Thm.theory_of_thm thm; |
843 fun ren (Abs (x, T, t)) = Abs (AList.lookup (op =) vs x |> the_default x, T, ren t) |
843 fun ren (Abs (x, T, t)) = Abs (AList.lookup (op =) vs x |> the_default x, T, ren t) |
844 | ren (t $ u) = ren t $ ren u |
844 | ren (t $ u) = ren t $ ren u |
845 | ren t = t; |
845 | ren t = t; |
846 in Thm.equal_elim (Thm.reflexive (Thm.cterm_of thy (ren (Thm.prop_of thm)))) thm end; |
846 in Thm.equal_elim (Thm.reflexive (Thm.global_cterm_of thy (ren (Thm.prop_of thm)))) thm end; |
847 |
847 |
848 |
848 |
849 (* renaming in left-to-right order *) |
849 (* renaming in left-to-right order *) |
850 |
850 |
851 fun rename_bvars' xs thm = |
851 fun rename_bvars' xs thm = |
862 val (xs'', u') = rename xs' u |
862 val (xs'', u') = rename xs' u |
863 in (xs'', t' $ u') end |
863 in (xs'', t' $ u') end |
864 | rename xs t = (xs, t); |
864 | rename xs t = (xs, t); |
865 in |
865 in |
866 (case rename xs prop of |
866 (case rename xs prop of |
867 ([], prop') => Thm.equal_elim (Thm.reflexive (Thm.cterm_of thy prop')) thm |
867 ([], prop') => Thm.equal_elim (Thm.reflexive (Thm.global_cterm_of thy prop')) thm |
868 | _ => error "More names than abstractions in theorem") |
868 | _ => error "More names than abstractions in theorem") |
869 end; |
869 end; |
870 |
870 |
871 end; |
871 end; |
872 |
872 |