src/HOL/Tools/Function/function_core.ML
changeset 61340 ce74c00de6b7
parent 61121 efe8b18306b7
child 62093 bd73a2279fcd
equal deleted inserted replaced
61339:93883c825062 61340:ce74c00de6b7
    84 val ex1_implies_un = @{thm Fun_Def.fundef_ex1_uniqueness}
    84 val ex1_implies_un = @{thm Fun_Def.fundef_ex1_uniqueness}
    85 val ex1_implies_iff = @{thm Fun_Def.fundef_ex1_iff}
    85 val ex1_implies_iff = @{thm Fun_Def.fundef_ex1_iff}
    86 
    86 
    87 val acc_downward = @{thm accp_downward}
    87 val acc_downward = @{thm accp_downward}
    88 val accI = @{thm accp.accI}
    88 val accI = @{thm accp.accI}
    89 val case_split = @{thm HOL.case_split}
       
    90 val fundef_default_value = @{thm Fun_Def.fundef_default_value}
       
    91 val not_acc_down = @{thm not_accp_down}
       
    92 
       
    93 
    89 
    94 
    90 
    95 fun find_calls tree =
    91 fun find_calls tree =
    96   let
    92   let
    97     fun add_Ri (fixes,assumes) (_ $ arg) _ (_, xs) =
    93     fun add_Ri (fixes,assumes) (_ $ arg) _ (_, xs) =
   257   end
   253   end
   258 
   254 
   259 (* Generates the replacement lemma in fully quantified form. *)
   255 (* Generates the replacement lemma in fully quantified form. *)
   260 fun mk_replacement_lemma ctxt h ih_elim clause =
   256 fun mk_replacement_lemma ctxt h ih_elim clause =
   261   let
   257   let
   262     val ClauseInfo {cdata=ClauseContext {qs, lhs, cqs, ags, case_hyp, ...},
   258     val ClauseInfo {cdata=ClauseContext {qs, cqs, ags, case_hyp, ...}, RCs, tree, ...} = clause
   263       RCs, tree, ...} = clause
       
   264     local open Conv in
   259     local open Conv in
   265       val ih_conv = arg1_conv o arg_conv o arg_conv
   260       val ih_conv = arg1_conv o arg_conv o arg_conv
   266     end
   261     end
   267 
   262 
   268     val ih_elim_case =
   263     val ih_elim_case =
   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 =