(*rewrite conclusion with k-th assumtion*) fun rewrite_with_asm_tac ctxt k =
Subgoal.FOCUS (fn {context = ctxt', prems, ...} =>
Local_Defs.unfold0_tac ctxt' [nth prems k]) ctxt;
fun dest_case ctxt t = case strip_comb t of
(Const (case_comb, _), args) =>
(case Ctr_Sugar.ctr_sugar_of_case ctxt case_comb of
NONE => NONE
| SOME {case_thms, ...} => let val lhs = Thm.prop_of (hd case_thms)
|> HOLogic.dest_Trueprop |> HOLogic.dest_eq |> fst; val arity = length (snd (strip_comb lhs)); val conv = funpow (length args - arity) Conv.fun_conv
(Conv.rewrs_conv (map mk_meta_eq case_thms)); in
SOME (nth args (arity - 1), conv) end)
| _ => NONE;
(*split on case expressions*) val split_cases_tac = Subgoal.FOCUS_PARAMS (fn {context = ctxt, ...} =>
SUBGOAL (fn (t, i) => case t of
_ $ (_ $ Abs (_, _, body)) =>
(case dest_case ctxt body of
NONE => no_tac
| SOME (arg, conv) => letopen Conv in if Term.is_open arg then no_tac else ((DETERM o Induct.cases_tac ctxt false [[SOME arg]] NONE [])
THEN_ALL_NEW (rewrite_with_asm_tac ctxt 0)
THEN_ALL_NEW eresolve_tac ctxt @{thms thin_rl}
THEN_ALL_NEW (CONVERSION
(params_conv ~1 (fn ctxt' =>
arg_conv (arg_conv (abs_conv (K conv) ctxt'))) ctxt))) i end)
| _ => no_tac) 1);
(*monotonicity proof: apply rules + split case expressions*) fun mono_tac ctxt =
K (Local_Defs.unfold0_tac ctxt [@{thm curry_def}]) THEN' (TRY o REPEAT_ALL_NEW
(resolve_tac ctxt (rev (Named_Theorems.get ctxt \<^named_theorems>\<open>partial_function_mono\<close>))
ORELSE' split_cases_tac ctxt));
(*** Auxiliary functions ***)
(*Returns t $ u, but instantiates the type of t to make the
application type correct*) fun apply_inst ctxt t u = let val thy = Proof_Context.theory_of ctxt; val T = domain_type (fastype_of t); val T' = fastype_of u; val subst = Sign.typ_match thy (T, T') Vartab.empty handleType.TYPE_MATCH => raiseTYPE ("apply_inst", [T, T'], [t, u]) in
map_types (Envir.norm_type subst) t $ u end;
fun head_conv cv ct = if can Thm.dest_fun ct then Conv.fun_conv (head_conv cv) ct else cv ct;
(*** currying transformation ***)
fun curry_const (A, B, C) = Const (\<^const_name>\<open>Product_Type.curry\<close>,
[HOLogic.mk_prodT (A, B) --> C, A, B] ---> C);
fun mk_curry f = case fastype_of f of Type ("fun", [Type (_, [S, T]), U]) =>
curry_const (S, T, U) $ f
| T => raiseTYPE ("mk_curry", [T], [f]);
(* iterated versions. Nonstandard left-nested tuples arise naturally
from "split o split o split"*) fun curry_n arity = funpow (arity - 1) mk_curry; fun uncurry_n arity = funpow (arity - 1) HOLogic.mk_case_prod;
val (lhs, rhs) = HOLogic.dest_eq (HOLogic.dest_Trueprop plain_eqn); val (head, args) = strip_comb lhs; val argnames = map (fst o dest_Free) args; val F = fold_rev lambda (head :: args) rhs;
val arity = length args; val (aTs, bTs) = chop arity (binder_types fT);
val tupleT = foldl1 HOLogic.mk_prodT aTs; val fT_uc = tupleT :: bTs ---> body_type fT; val f_uc = Var ((f_bname, 0), fT_uc); val x_uc = Var (("x", 1), tupleT); val uncurry = lambda head (uncurry_n arity head); val curry = lambda f_uc (curry_n arity f_uc);
val f_def_rhs = curry_n arity (apply_inst lthy fixp F_uc); val f_def_binding =
Thm.make_def_binding (Config.get lthy Function_Lib.function_internals) f_binding val ((f, (_, f_def)), lthy') =
Local_Theory.define ((f_binding, mixfix), ((f_def_binding, []), f_def_rhs)) lthy;
val eqn = HOLogic.mk_eq (list_comb (f, args),
Term.betapplys (F, f :: args))
|> HOLogic.mk_Trueprop;
val unfold =
(infer_instantiate' lthy' (map (SOME o Thm.cterm_of lthy') [uncurry, F, curry]) fixp_eq OF [inst_mono_thm, f_def])
|> Tactic.rule_by_tactic lthy' (Simplifier.simp_tac (put_simpset curry_uncurry_ss lthy') 1);
val specialized_fixp_induct =
specialize_fixp_induct lthy' fT fT_uc curry uncurry inst_mono_thm f_def fixp_induct
|> Drule.rename_bvars' (map SOME (f_bname :: f_bname :: argnames));
val mk_raw_induct =
infer_instantiate' args_ctxt
((map o Option.map) (Thm.cterm_of args_ctxt) [SOME uncurry, NONE, SOME curry])
#> mk_curried_induct args args_ctxt
#> singleton (Variable.export args_ctxt lthy')
#> (fn thm => infer_instantiate' lthy'
[SOME (Thm.cterm_of lthy' F)] thm OF [inst_mono_thm, f_def])
#> Drule.rename_bvars' (map SOME (f_bname :: argnames @ argnames))
val raw_induct = Option.map mk_raw_induct fixp_induct_user val rec_rule = letopen Conv in
Goal.prove lthy' (map (fst o dest_Free) args) [] eqn (fn _ =>
CONVERSION ((arg_conv o arg1_conv o head_conv o rewr_conv) (mk_meta_eq unfold)) 1 THEN resolve_tac lthy' @{thms refl} 1) end; val ((_, [rec_rule']), lthy'') = lthy' |> Local_Theory.note (eq_abinding, [rec_rule]) in
lthy''
|> Spec_Rules.add Binding.empty Spec_Rules.equational_recdef [f] [rec_rule']
|> note_qualified ("simps", [rec_rule'])
|> note_qualified ("mono", [mono_thm])
|> (case raw_induct of NONE => I | SOME thm => note_qualified ("raw_induct", [thm]))
|> note_qualified ("fixp_induct", [specialized_fixp_induct])
|> pair (f, rec_rule') end;
val add_partial_function = gen_add_partial_function Specification.check_multi_specs; val add_partial_function_cmd = gen_add_partial_function Specification.read_multi_specs;
val mode = \<^keyword>\<open>(\<close> |-- Parse.name --| \<^keyword>\<open>)\<close>;
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung ist noch experimentell.