782 fun mk_nest_term_rule ctxt globals R R_cases clauses = |
777 fun mk_nest_term_rule ctxt globals R R_cases clauses = |
783 let |
778 let |
784 val Globals { domT, x, z, ... } = globals |
779 val Globals { domT, x, z, ... } = globals |
785 val acc_R = mk_acc domT R |
780 val acc_R = mk_acc domT R |
786 |
781 |
787 val R' = Free ("R", fastype_of R) |
782 val ([Rn], ctxt') = Variable.variant_fixes ["R"] ctxt |
788 |
783 val R' = Free (Rn, fastype_of R) |
789 val Rrel = Free ("R", HOLogic.mk_setT (HOLogic.mk_prodT (domT, domT))) |
784 |
|
785 val Rrel = Free (Rn, HOLogic.mk_setT (HOLogic.mk_prodT (domT, domT))) |
790 val inrel_R = Const (@{const_name Fun_Def.in_rel}, |
786 val inrel_R = Const (@{const_name Fun_Def.in_rel}, |
791 HOLogic.mk_setT (HOLogic.mk_prodT (domT, domT)) --> fastype_of R) $ Rrel |
787 HOLogic.mk_setT (HOLogic.mk_prodT (domT, domT)) --> fastype_of R) $ Rrel |
792 |
788 |
793 val wfR' = HOLogic.mk_Trueprop (Const (@{const_name Wellfounded.wfP}, |
789 val wfR' = HOLogic.mk_Trueprop (Const (@{const_name Wellfounded.wfP}, |
794 (domT --> domT --> boolT) --> boolT) $ R') |
790 (domT --> domT --> boolT) --> boolT) $ R') |
795 |> Thm.cterm_of ctxt (* "wf R'" *) |
791 |> Thm.cterm_of ctxt' (* "wf R'" *) |
796 |
792 |
797 (* Inductive Hypothesis: !!z. (z,x):R' ==> z : acc R *) |
793 (* Inductive Hypothesis: !!z. (z,x):R' ==> z : acc R *) |
798 val ihyp = Logic.all_const domT $ Abs ("z", domT, |
794 val ihyp = Logic.all_const domT $ Abs ("z", domT, |
799 Logic.mk_implies (HOLogic.mk_Trueprop (R' $ Bound 0 $ x), |
795 Logic.mk_implies (HOLogic.mk_Trueprop (R' $ Bound 0 $ x), |
800 HOLogic.mk_Trueprop (acc_R $ Bound 0))) |
796 HOLogic.mk_Trueprop (acc_R $ Bound 0))) |
801 |> Thm.cterm_of ctxt |
797 |> Thm.cterm_of ctxt' |
802 |
798 |
803 val ihyp_a = Thm.assume ihyp |> Thm.forall_elim_vars 0 |
799 val ihyp_a = Thm.assume ihyp |> Thm.forall_elim_vars 0 |
804 |
800 |
805 val R_z_x = Thm.cterm_of ctxt (HOLogic.mk_Trueprop (R $ z $ x)) |
801 val R_z_x = Thm.cterm_of ctxt' (HOLogic.mk_Trueprop (R $ z $ x)) |
806 |
802 |
807 val (hyps, cases) = fold (mk_nest_term_case ctxt globals R' ihyp_a) clauses ([], []) |
803 val (hyps, cases) = fold (mk_nest_term_case ctxt' globals R' ihyp_a) clauses ([], []) |
808 in |
804 in |
809 R_cases |
805 R_cases |
810 |> Thm.forall_elim (Thm.cterm_of ctxt z) |
806 |> Thm.forall_elim (Thm.cterm_of ctxt' z) |
811 |> Thm.forall_elim (Thm.cterm_of ctxt x) |
807 |> Thm.forall_elim (Thm.cterm_of ctxt' x) |
812 |> Thm.forall_elim (Thm.cterm_of ctxt (acc_R $ z)) |
808 |> Thm.forall_elim (Thm.cterm_of ctxt' (acc_R $ z)) |
813 |> curry op COMP (Thm.assume R_z_x) |
809 |> curry op COMP (Thm.assume R_z_x) |
814 |> fold_rev (curry op COMP) cases |
810 |> fold_rev (curry op COMP) cases |
815 |> Thm.implies_intr R_z_x |
811 |> Thm.implies_intr R_z_x |
816 |> Thm.forall_intr (Thm.cterm_of ctxt z) |
812 |> Thm.forall_intr (Thm.cterm_of ctxt' z) |
817 |> (fn it => it COMP accI) |
813 |> (fn it => it COMP accI) |
818 |> Thm.implies_intr ihyp |
814 |> Thm.implies_intr ihyp |
819 |> Thm.forall_intr (Thm.cterm_of ctxt x) |
815 |> Thm.forall_intr (Thm.cterm_of ctxt' x) |
820 |> (fn it => Drule.compose (it, 2, wf_induct_rule)) |
816 |> (fn it => Drule.compose (it, 2, wf_induct_rule)) |
821 |> curry op RS (Thm.assume wfR') |
817 |> curry op RS (Thm.assume wfR') |
822 |> forall_intr_vars |
818 |> forall_intr_vars |
823 |> (fn it => it COMP allI) |
819 |> (fn it => it COMP allI) |
824 |> fold Thm.implies_intr hyps |
820 |> fold Thm.implies_intr hyps |
825 |> Thm.implies_intr wfR' |
821 |> Thm.implies_intr wfR' |
826 |> Thm.forall_intr (Thm.cterm_of ctxt R') |
822 |> Thm.forall_intr (Thm.cterm_of ctxt' R') |
827 |> Thm.forall_elim (Thm.cterm_of ctxt (inrel_R)) |
823 |> Thm.forall_elim (Thm.cterm_of ctxt' inrel_R) |
828 |> curry op RS wf_in_rel |
824 |> curry op RS wf_in_rel |
829 |> full_simplify (put_simpset HOL_basic_ss ctxt addsimps [in_rel_def]) |
825 |> full_simplify (put_simpset HOL_basic_ss ctxt' addsimps [in_rel_def]) |
830 |> Thm.forall_intr (Thm.cterm_of ctxt Rrel) |
826 |> Thm.forall_intr_name ("R", Thm.cterm_of ctxt' Rrel) |
831 end |
827 end |
832 |
828 |
833 |
829 |
834 |
830 |
835 fun prepare_function config defname [((fname, fT), mixfix)] abstract_qglrs lthy = |
831 fun prepare_function config defname [((fname, fT), mixfix)] abstract_qglrs lthy = |