| author | blanchet | 
| Fri, 23 Aug 2013 16:02:32 +0200 | |
| changeset 53157 | c8369b691d04 | 
| parent 52728 | 470b579f35d2 | 
| child 54405 | 88f6d5b1422f | 
| permissions | -rw-r--r-- | 
| 40107 | 1 | (* Title: HOL/Tools/Function/partial_function.ML | 
| 2 | Author: Alexander Krauss, TU Muenchen | |
| 3 | ||
| 4 | Partial function definitions based on least fixed points in ccpos. | |
| 5 | *) | |
| 6 | ||
| 7 | signature PARTIAL_FUNCTION = | |
| 8 | sig | |
| 9 | val setup: theory -> theory | |
| 52728 
470b579f35d2
derive specialized version of full fixpoint induction (with admissibility)
 krauss parents: 
52727diff
changeset | 10 | val init: string -> term -> term -> thm -> thm -> thm option -> declaration | 
| 40107 | 11 | |
| 52727 | 12 | val mono_tac: Proof.context -> int -> tactic | 
| 13 | ||
| 40107 | 14 | val add_partial_function: string -> (binding * typ option * mixfix) list -> | 
| 15 | Attrib.binding * term -> local_theory -> local_theory | |
| 16 | ||
| 17 | val add_partial_function_cmd: string -> (binding * string option * mixfix) list -> | |
| 18 | Attrib.binding * string -> local_theory -> local_theory | |
| 19 | end; | |
| 20 | ||
| 21 | ||
| 22 | structure Partial_Function: PARTIAL_FUNCTION = | |
| 23 | struct | |
| 24 | ||
| 25 | (*** Context Data ***) | |
| 26 | ||
| 43080 | 27 | datatype setup_data = Setup_Data of | 
| 28 |  {fixp: term,
 | |
| 29 | mono: term, | |
| 30 | fixp_eq: thm, | |
| 52728 
470b579f35d2
derive specialized version of full fixpoint induction (with admissibility)
 krauss parents: 
52727diff
changeset | 31 | fixp_induct: thm, | 
| 
470b579f35d2
derive specialized version of full fixpoint induction (with admissibility)
 krauss parents: 
52727diff
changeset | 32 | fixp_induct_user: thm option}; | 
| 43080 | 33 | |
| 40107 | 34 | structure Modes = Generic_Data | 
| 35 | ( | |
| 43080 | 36 | type T = setup_data Symtab.table; | 
| 40107 | 37 | val empty = Symtab.empty; | 
| 38 | val extend = I; | |
| 41472 
f6ab14e61604
misc tuning and comments based on review of Theory_Data, Proof_Data, Generic_Data usage;
 wenzelm parents: 
41117diff
changeset | 39 | fun merge data = Symtab.merge (K true) data; | 
| 40107 | 40 | ) | 
| 41 | ||
| 52728 
470b579f35d2
derive specialized version of full fixpoint induction (with admissibility)
 krauss parents: 
52727diff
changeset | 42 | fun init mode fixp mono fixp_eq fixp_induct fixp_induct_user phi = | 
| 40107 | 43 | let | 
| 44 | val term = Morphism.term phi; | |
| 43080 | 45 | val thm = Morphism.thm phi; | 
| 46 | val data' = Setup_Data | |
| 47 |       {fixp=term fixp, mono=term mono, fixp_eq=thm fixp_eq,
 | |
| 52728 
470b579f35d2
derive specialized version of full fixpoint induction (with admissibility)
 krauss parents: 
52727diff
changeset | 48 | fixp_induct=thm fixp_induct, fixp_induct_user=Option.map thm fixp_induct_user}; | 
| 40107 | 49 | in | 
| 42949 
618adb3584e5
separate initializations for different modes of partial_function -- generation of induction rules will be non-uniform
 krauss parents: 
42495diff
changeset | 50 | Modes.map (Symtab.update (mode, data')) | 
| 40107 | 51 | end | 
| 52 | ||
| 53 | val known_modes = Symtab.keys o Modes.get o Context.Proof; | |
| 54 | val lookup_mode = Symtab.lookup o Modes.get o Context.Proof; | |
| 55 | ||
| 56 | ||
| 57 | structure Mono_Rules = Named_Thms | |
| 58 | ( | |
| 45294 | 59 |   val name = @{binding partial_function_mono};
 | 
| 40107 | 60 | val description = "monotonicity rules for partial function definitions"; | 
| 61 | ); | |
| 62 | ||
| 63 | ||
| 64 | (*** Automated monotonicity proofs ***) | |
| 65 | ||
| 66 | fun strip_cases ctac = ctac #> Seq.map snd; | |
| 67 | ||
| 68 | (*rewrite conclusion with k-th assumtion*) | |
| 69 | fun rewrite_with_asm_tac ctxt k = | |
| 45403 | 70 |   Subgoal.FOCUS (fn {context = ctxt', prems, ...} =>
 | 
| 40107 | 71 | Local_Defs.unfold_tac ctxt' [nth prems k]) ctxt; | 
| 72 | ||
| 73 | fun dest_case thy t = | |
| 74 | case strip_comb t of | |
| 75 | (Const (case_comb, _), args) => | |
| 76 | (case Datatype.info_of_case thy case_comb of | |
| 77 | NONE => NONE | |
| 78 |        | SOME {case_rewrites, ...} =>
 | |
| 79 | let | |
| 80 | val lhs = prop_of (hd case_rewrites) | |
| 81 | |> HOLogic.dest_Trueprop |> HOLogic.dest_eq |> fst; | |
| 82 | val arity = length (snd (strip_comb lhs)); | |
| 83 | val conv = funpow (length args - arity) Conv.fun_conv | |
| 84 | (Conv.rewrs_conv (map mk_meta_eq case_rewrites)); | |
| 85 | in | |
| 86 | SOME (nth args (arity - 1), conv) | |
| 87 | end) | |
| 88 | | _ => NONE; | |
| 89 | ||
| 90 | (*split on case expressions*) | |
| 91 | val split_cases_tac = Subgoal.FOCUS_PARAMS (fn {context=ctxt, ...} =>
 | |
| 92 | SUBGOAL (fn (t, i) => case t of | |
| 93 | _ $ (_ $ Abs (_, _, body)) => | |
| 42361 | 94 | (case dest_case (Proof_Context.theory_of ctxt) body of | 
| 40107 | 95 | NONE => no_tac | 
| 96 | | SOME (arg, conv) => | |
| 97 | let open Conv in | |
| 42083 
e1209fc7ecdc
added Term.is_open and Term.is_dependent convenience, to cover common situations of loose bounds;
 wenzelm parents: 
41472diff
changeset | 98 | if Term.is_open arg then no_tac | 
| 40107 | 99 | else ((DETERM o strip_cases o Induct.cases_tac ctxt false [[SOME arg]] NONE []) | 
| 100 | THEN_ALL_NEW (rewrite_with_asm_tac ctxt 0) | |
| 101 |                 THEN_ALL_NEW etac @{thm thin_rl}
 | |
| 102 | THEN_ALL_NEW (CONVERSION | |
| 103 | (params_conv ~1 (fn ctxt' => | |
| 104 | arg_conv (arg_conv (abs_conv (K conv) ctxt'))) ctxt))) i | |
| 105 | end) | |
| 106 | | _ => no_tac) 1); | |
| 107 | ||
| 108 | (*monotonicity proof: apply rules + split case expressions*) | |
| 109 | fun mono_tac ctxt = | |
| 110 |   K (Local_Defs.unfold_tac ctxt [@{thm curry_def}])
 | |
| 111 | THEN' (TRY o REPEAT_ALL_NEW | |
| 112 | (resolve_tac (Mono_Rules.get ctxt) | |
| 113 | ORELSE' split_cases_tac ctxt)); | |
| 114 | ||
| 115 | ||
| 116 | (*** Auxiliary functions ***) | |
| 117 | ||
| 118 | (*positional instantiation with computed type substitution. | |
| 119 | internal version of attribute "[of s t u]".*) | |
| 120 | fun cterm_instantiate' cts thm = | |
| 121 | let | |
| 122 | val thy = Thm.theory_of_thm thm; | |
| 123 | val vs = rev (Term.add_vars (prop_of thm) []) | |
| 124 | |> map (Thm.cterm_of thy o Var); | |
| 125 | in | |
| 126 | cterm_instantiate (zip_options vs cts) thm | |
| 127 | end; | |
| 128 | ||
| 129 | (*Returns t $ u, but instantiates the type of t to make the | |
| 130 | application type correct*) | |
| 131 | fun apply_inst ctxt t u = | |
| 132 | let | |
| 42361 | 133 | val thy = Proof_Context.theory_of ctxt; | 
| 40107 | 134 | val T = domain_type (fastype_of t); | 
| 135 | val T' = fastype_of u; | |
| 42388 | 136 | val subst = Sign.typ_match thy (T, T') Vartab.empty | 
| 40107 | 137 |       handle Type.TYPE_MATCH => raise TYPE ("apply_inst", [T, T'], [t, u])
 | 
| 138 | in | |
| 139 | map_types (Envir.norm_type subst) t $ u | |
| 140 | end; | |
| 141 | ||
| 142 | fun head_conv cv ct = | |
| 143 | if can Thm.dest_comb ct then Conv.fun_conv (head_conv cv) ct else cv ct; | |
| 144 | ||
| 145 | ||
| 146 | (*** currying transformation ***) | |
| 147 | ||
| 148 | fun curry_const (A, B, C) = | |
| 149 |   Const (@{const_name Product_Type.curry},
 | |
| 150 | [HOLogic.mk_prodT (A, B) --> C, A, B] ---> C); | |
| 151 | ||
| 152 | fun mk_curry f = | |
| 153 | case fastype_of f of | |
| 154 |     Type ("fun", [Type (_, [S, T]), U]) =>
 | |
| 155 | curry_const (S, T, U) $ f | |
| 156 |   | T => raise TYPE ("mk_curry", [T], [f]);
 | |
| 157 | ||
| 158 | (* iterated versions. Nonstandard left-nested tuples arise naturally | |
| 159 | from "split o split o split"*) | |
| 160 | fun curry_n arity = funpow (arity - 1) mk_curry; | |
| 161 | fun uncurry_n arity = funpow (arity - 1) HOLogic.mk_split; | |
| 162 | ||
| 51717 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51484diff
changeset | 163 | val curry_uncurry_ss = | 
| 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51484diff
changeset | 164 |   simpset_of (put_simpset HOL_basic_ss @{context}
 | 
| 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51484diff
changeset | 165 |     addsimps [@{thm Product_Type.curry_split}, @{thm Product_Type.split_curry}])
 | 
| 40107 | 166 | |
| 51717 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51484diff
changeset | 167 | val split_conv_ss = | 
| 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51484diff
changeset | 168 |   simpset_of (put_simpset HOL_basic_ss @{context}
 | 
| 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51484diff
changeset | 169 |     addsimps [@{thm Product_Type.split_conv}]);
 | 
| 43083 
df41a5762c3d
generate raw induction rule as instance of generic rule with careful treatment of currying
 krauss parents: 
43080diff
changeset | 170 | |
| 52728 
470b579f35d2
derive specialized version of full fixpoint induction (with admissibility)
 krauss parents: 
52727diff
changeset | 171 | |
| 
470b579f35d2
derive specialized version of full fixpoint induction (with admissibility)
 krauss parents: 
52727diff
changeset | 172 | (* instantiate generic fixpoint induction and eliminate the canonical assumptions; | 
| 
470b579f35d2
derive specialized version of full fixpoint induction (with admissibility)
 krauss parents: 
52727diff
changeset | 173 | curry induction predicate *) | 
| 
470b579f35d2
derive specialized version of full fixpoint induction (with admissibility)
 krauss parents: 
52727diff
changeset | 174 | fun specialize_fixp_induct ctxt args fT fT_uc F curry uncurry mono_thm f_def rule = | 
| 
470b579f35d2
derive specialized version of full fixpoint induction (with admissibility)
 krauss parents: 
52727diff
changeset | 175 | let | 
| 
470b579f35d2
derive specialized version of full fixpoint induction (with admissibility)
 krauss parents: 
52727diff
changeset | 176 | val cert = Thm.cterm_of (Proof_Context.theory_of ctxt) | 
| 
470b579f35d2
derive specialized version of full fixpoint induction (with admissibility)
 krauss parents: 
52727diff
changeset | 177 | val ([P], ctxt') = Variable.variant_fixes ["P"] ctxt | 
| 
470b579f35d2
derive specialized version of full fixpoint induction (with admissibility)
 krauss parents: 
52727diff
changeset | 178 |     val P_inst = Abs ("f", fT_uc, Free (P, fT --> HOLogic.boolT) $ (curry $ Bound 0))
 | 
| 
470b579f35d2
derive specialized version of full fixpoint induction (with admissibility)
 krauss parents: 
52727diff
changeset | 179 | in | 
| 
470b579f35d2
derive specialized version of full fixpoint induction (with admissibility)
 krauss parents: 
52727diff
changeset | 180 | rule | 
| 
470b579f35d2
derive specialized version of full fixpoint induction (with admissibility)
 krauss parents: 
52727diff
changeset | 181 | |> cterm_instantiate' [SOME (cert uncurry), NONE, SOME (cert curry), NONE, SOME (cert P_inst)] | 
| 
470b579f35d2
derive specialized version of full fixpoint induction (with admissibility)
 krauss parents: 
52727diff
changeset | 182 | |> Tactic.rule_by_tactic ctxt | 
| 
470b579f35d2
derive specialized version of full fixpoint induction (with admissibility)
 krauss parents: 
52727diff
changeset | 183 | (Simplifier.simp_tac (put_simpset curry_uncurry_ss ctxt) 3 (* discharge U (C f) = f *) | 
| 
470b579f35d2
derive specialized version of full fixpoint induction (with admissibility)
 krauss parents: 
52727diff
changeset | 184 | THEN Simplifier.full_simp_tac (put_simpset curry_uncurry_ss ctxt) 4) (* simplify induction step *) | 
| 
470b579f35d2
derive specialized version of full fixpoint induction (with admissibility)
 krauss parents: 
52727diff
changeset | 185 | |> (fn thm => thm OF [mono_thm, f_def]) | 
| 
470b579f35d2
derive specialized version of full fixpoint induction (with admissibility)
 krauss parents: 
52727diff
changeset | 186 | |> Conv.fconv_rule (Conv.concl_conv ~1 (* simplify conclusion *) | 
| 
470b579f35d2
derive specialized version of full fixpoint induction (with admissibility)
 krauss parents: 
52727diff
changeset | 187 |          (Raw_Simplifier.rewrite false [mk_meta_eq @{thm Product_Type.curry_split}])) 
 | 
| 
470b579f35d2
derive specialized version of full fixpoint induction (with admissibility)
 krauss parents: 
52727diff
changeset | 188 | |> singleton (Variable.export ctxt' ctxt) | 
| 
470b579f35d2
derive specialized version of full fixpoint induction (with admissibility)
 krauss parents: 
52727diff
changeset | 189 | end | 
| 
470b579f35d2
derive specialized version of full fixpoint induction (with admissibility)
 krauss parents: 
52727diff
changeset | 190 | |
| 52727 | 191 | fun mk_curried_induct args ctxt inst_rule = | 
| 43083 
df41a5762c3d
generate raw induction rule as instance of generic rule with careful treatment of currying
 krauss parents: 
43080diff
changeset | 192 | let | 
| 
df41a5762c3d
generate raw induction rule as instance of generic rule with careful treatment of currying
 krauss parents: 
43080diff
changeset | 193 | val cert = Thm.cterm_of (Proof_Context.theory_of ctxt) | 
| 
df41a5762c3d
generate raw induction rule as instance of generic rule with careful treatment of currying
 krauss parents: 
43080diff
changeset | 194 | val ([P], ctxt') = Variable.variant_fixes ["P"] ctxt | 
| 
df41a5762c3d
generate raw induction rule as instance of generic rule with careful treatment of currying
 krauss parents: 
43080diff
changeset | 195 | |
| 
df41a5762c3d
generate raw induction rule as instance of generic rule with careful treatment of currying
 krauss parents: 
43080diff
changeset | 196 | val split_paired_all_conv = | 
| 
df41a5762c3d
generate raw induction rule as instance of generic rule with careful treatment of currying
 krauss parents: 
43080diff
changeset | 197 |       Conv.every_conv (replicate (length args - 1) (Conv.rewr_conv @{thm split_paired_all}))
 | 
| 
df41a5762c3d
generate raw induction rule as instance of generic rule with careful treatment of currying
 krauss parents: 
43080diff
changeset | 198 | |
| 
df41a5762c3d
generate raw induction rule as instance of generic rule with careful treatment of currying
 krauss parents: 
43080diff
changeset | 199 | val split_params_conv = | 
| 
df41a5762c3d
generate raw induction rule as instance of generic rule with careful treatment of currying
 krauss parents: 
43080diff
changeset | 200 | Conv.params_conv ~1 (fn ctxt' => | 
| 
df41a5762c3d
generate raw induction rule as instance of generic rule with careful treatment of currying
 krauss parents: 
43080diff
changeset | 201 | Conv.implies_conv split_paired_all_conv Conv.all_conv) | 
| 
df41a5762c3d
generate raw induction rule as instance of generic rule with careful treatment of currying
 krauss parents: 
43080diff
changeset | 202 | |
| 52521 
a1c4f586e372
more robust instantiation: extract vars from conclusion instead of relying on fixed overall ordering
 krauss parents: 
52087diff
changeset | 203 | val (P_var, x_var) = | 
| 51484 
49eb8d73ae10
allow induction predicates with arbitrary arity (not just binary)
 krauss parents: 
46961diff
changeset | 204 | Thm.prop_of inst_rule |> Logic.strip_imp_concl |> HOLogic.dest_Trueprop | 
| 52521 
a1c4f586e372
more robust instantiation: extract vars from conclusion instead of relying on fixed overall ordering
 krauss parents: 
52087diff
changeset | 205 | |> strip_comb |> apsnd hd | 
| 
a1c4f586e372
more robust instantiation: extract vars from conclusion instead of relying on fixed overall ordering
 krauss parents: 
52087diff
changeset | 206 | val P_rangeT = range_type (snd (Term.dest_Var P_var)) | 
| 51484 
49eb8d73ae10
allow induction predicates with arbitrary arity (not just binary)
 krauss parents: 
46961diff
changeset | 207 | val PT = map (snd o dest_Free) args ---> P_rangeT | 
| 43083 
df41a5762c3d
generate raw induction rule as instance of generic rule with careful treatment of currying
 krauss parents: 
43080diff
changeset | 208 | val x_inst = cert (foldl1 HOLogic.mk_prod args) | 
| 
df41a5762c3d
generate raw induction rule as instance of generic rule with careful treatment of currying
 krauss parents: 
43080diff
changeset | 209 | val P_inst = cert (uncurry_n (length args) (Free (P, PT))) | 
| 
df41a5762c3d
generate raw induction rule as instance of generic rule with careful treatment of currying
 krauss parents: 
43080diff
changeset | 210 | |
| 
df41a5762c3d
generate raw induction rule as instance of generic rule with careful treatment of currying
 krauss parents: 
43080diff
changeset | 211 | val inst_rule' = inst_rule | 
| 
df41a5762c3d
generate raw induction rule as instance of generic rule with careful treatment of currying
 krauss parents: 
43080diff
changeset | 212 | |> Tactic.rule_by_tactic ctxt | 
| 51717 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51484diff
changeset | 213 | (Simplifier.simp_tac (put_simpset curry_uncurry_ss ctxt) 4 | 
| 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51484diff
changeset | 214 | THEN Simplifier.simp_tac (put_simpset curry_uncurry_ss ctxt) 3 | 
| 43083 
df41a5762c3d
generate raw induction rule as instance of generic rule with careful treatment of currying
 krauss parents: 
43080diff
changeset | 215 | THEN CONVERSION (split_params_conv ctxt | 
| 
df41a5762c3d
generate raw induction rule as instance of generic rule with careful treatment of currying
 krauss parents: 
43080diff
changeset | 216 | then_conv (Conv.forall_conv (K split_paired_all_conv) ctxt)) 3) | 
| 52521 
a1c4f586e372
more robust instantiation: extract vars from conclusion instead of relying on fixed overall ordering
 krauss parents: 
52087diff
changeset | 217 | |> Thm.instantiate ([], [(cert P_var, P_inst), (cert x_var, x_inst)]) | 
| 51717 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51484diff
changeset | 218 | |> Simplifier.full_simplify (put_simpset split_conv_ss ctxt) | 
| 43083 
df41a5762c3d
generate raw induction rule as instance of generic rule with careful treatment of currying
 krauss parents: 
43080diff
changeset | 219 | |> singleton (Variable.export ctxt' ctxt) | 
| 
df41a5762c3d
generate raw induction rule as instance of generic rule with careful treatment of currying
 krauss parents: 
43080diff
changeset | 220 | in | 
| 
df41a5762c3d
generate raw induction rule as instance of generic rule with careful treatment of currying
 krauss parents: 
43080diff
changeset | 221 | inst_rule' | 
| 
df41a5762c3d
generate raw induction rule as instance of generic rule with careful treatment of currying
 krauss parents: 
43080diff
changeset | 222 | end; | 
| 
df41a5762c3d
generate raw induction rule as instance of generic rule with careful treatment of currying
 krauss parents: 
43080diff
changeset | 223 | |
| 40107 | 224 | |
| 225 | (*** partial_function definition ***) | |
| 226 | ||
| 227 | fun gen_add_partial_function prep mode fixes_raw eqn_raw lthy = | |
| 228 | let | |
| 43080 | 229 | val setup_data = the (lookup_mode lthy mode) | 
| 40107 | 230 | handle Option.Option => error (cat_lines ["Unknown mode " ^ quote mode ^ ".", | 
| 231 | "Known modes are " ^ commas_quote (known_modes lthy) ^ "."]); | |
| 52728 
470b579f35d2
derive specialized version of full fixpoint induction (with admissibility)
 krauss parents: 
52727diff
changeset | 232 |     val Setup_Data {fixp, mono, fixp_eq, fixp_induct, fixp_induct_user} = setup_data;
 | 
| 40107 | 233 | |
| 234 | val ((fixes, [(eq_abinding, eqn)]), _) = prep fixes_raw [eqn_raw] lthy; | |
| 43083 
df41a5762c3d
generate raw induction rule as instance of generic rule with careful treatment of currying
 krauss parents: 
43080diff
changeset | 235 | val ((_, plain_eqn), args_ctxt) = Variable.focus eqn lthy; | 
| 40107 | 236 | |
| 237 | val ((f_binding, fT), mixfix) = the_single fixes; | |
| 238 | val fname = Binding.name_of f_binding; | |
| 239 | ||
| 42361 | 240 | val cert = cterm_of (Proof_Context.theory_of lthy); | 
| 40107 | 241 | val (lhs, rhs) = HOLogic.dest_eq (HOLogic.dest_Trueprop plain_eqn); | 
| 242 | val (head, args) = strip_comb lhs; | |
| 43083 
df41a5762c3d
generate raw induction rule as instance of generic rule with careful treatment of currying
 krauss parents: 
43080diff
changeset | 243 | val argnames = map (fst o dest_Free) args; | 
| 40107 | 244 | val F = fold_rev lambda (head :: args) rhs; | 
| 245 | ||
| 246 | val arity = length args; | |
| 247 | val (aTs, bTs) = chop arity (binder_types fT); | |
| 248 | ||
| 249 | val tupleT = foldl1 HOLogic.mk_prodT aTs; | |
| 250 | val fT_uc = tupleT :: bTs ---> body_type fT; | |
| 251 | val f_uc = Var ((fname, 0), fT_uc); | |
| 252 |     val x_uc = Var (("x", 0), tupleT);
 | |
| 253 | val uncurry = lambda head (uncurry_n arity head); | |
| 254 | val curry = lambda f_uc (curry_n arity f_uc); | |
| 255 | ||
| 256 | val F_uc = | |
| 257 | lambda f_uc (uncurry_n arity (F $ curry_n arity f_uc)); | |
| 258 | ||
| 259 | val mono_goal = apply_inst lthy mono (lambda f_uc (F_uc $ f_uc $ x_uc)) | |
| 260 | |> HOLogic.mk_Trueprop | |
| 261 | |> Logic.all x_uc; | |
| 262 | ||
| 263 | val mono_thm = Goal.prove_internal [] (cert mono_goal) | |
| 264 | (K (mono_tac lthy 1)) | |
| 52727 | 265 | val inst_mono_thm = Thm.forall_elim (cert x_uc) mono_thm | 
| 40107 | 266 | |
| 267 | val f_def_rhs = curry_n arity (apply_inst lthy fixp F_uc); | |
| 268 | val f_def_binding = Binding.conceal (Binding.name (Thm.def_name fname)); | |
| 269 | val ((f, (_, f_def)), lthy') = Local_Theory.define | |
| 270 | ((f_binding, mixfix), ((f_def_binding, []), f_def_rhs)) lthy; | |
| 271 | ||
| 272 | val eqn = HOLogic.mk_eq (list_comb (f, args), | |
| 273 | Term.betapplys (F, f :: args)) | |
| 274 | |> HOLogic.mk_Trueprop; | |
| 275 | ||
| 276 | val unfold = | |
| 277 | (cterm_instantiate' (map (SOME o cert) [uncurry, F, curry]) fixp_eq | |
| 52727 | 278 | OF [inst_mono_thm, f_def]) | 
| 52087 
f3075fc4f5f6
more precise treatment of theory vs. Proof.context;
 wenzelm parents: 
51717diff
changeset | 279 | |> Tactic.rule_by_tactic lthy' (Simplifier.simp_tac (put_simpset curry_uncurry_ss lthy') 1); | 
| 40107 | 280 | |
| 52728 
470b579f35d2
derive specialized version of full fixpoint induction (with admissibility)
 krauss parents: 
52727diff
changeset | 281 | val specialized_fixp_induct = | 
| 
470b579f35d2
derive specialized version of full fixpoint induction (with admissibility)
 krauss parents: 
52727diff
changeset | 282 | specialize_fixp_induct lthy' args fT fT_uc F curry uncurry inst_mono_thm f_def fixp_induct | 
| 
470b579f35d2
derive specialized version of full fixpoint induction (with admissibility)
 krauss parents: 
52727diff
changeset | 283 | |> Drule.rename_bvars' (map SOME (fname :: fname :: argnames)); | 
| 
470b579f35d2
derive specialized version of full fixpoint induction (with admissibility)
 krauss parents: 
52727diff
changeset | 284 | |
| 43083 
df41a5762c3d
generate raw induction rule as instance of generic rule with careful treatment of currying
 krauss parents: 
43080diff
changeset | 285 | val mk_raw_induct = | 
| 52727 | 286 | cterm_instantiate' [SOME (cert uncurry), NONE, SOME (cert curry)] | 
| 287 | #> mk_curried_induct args args_ctxt | |
| 52087 
f3075fc4f5f6
more precise treatment of theory vs. Proof.context;
 wenzelm parents: 
51717diff
changeset | 288 | #> singleton (Variable.export args_ctxt lthy') | 
| 52727 | 289 | #> (fn thm => cterm_instantiate' [SOME (cert F)] thm OF [inst_mono_thm, f_def]) | 
| 43083 
df41a5762c3d
generate raw induction rule as instance of generic rule with careful treatment of currying
 krauss parents: 
43080diff
changeset | 290 | #> Drule.rename_bvars' (map SOME (fname :: argnames @ argnames)) | 
| 
df41a5762c3d
generate raw induction rule as instance of generic rule with careful treatment of currying
 krauss parents: 
43080diff
changeset | 291 | |
| 52728 
470b579f35d2
derive specialized version of full fixpoint induction (with admissibility)
 krauss parents: 
52727diff
changeset | 292 | val raw_induct = Option.map mk_raw_induct fixp_induct_user | 
| 40107 | 293 | val rec_rule = let open Conv in | 
| 294 | Goal.prove lthy' (map (fst o dest_Free) args) [] eqn (fn _ => | |
| 295 | CONVERSION ((arg_conv o arg1_conv o head_conv o rewr_conv) (mk_meta_eq unfold)) 1 | |
| 296 |         THEN rtac @{thm refl} 1) end;
 | |
| 297 | in | |
| 298 | lthy' | |
| 299 | |> Local_Theory.note (eq_abinding, [rec_rule]) | |
| 300 | |-> (fn (_, rec') => | |
| 40180 | 301 | Spec_Rules.add Spec_Rules.Equational ([f], rec') | 
| 302 | #> Local_Theory.note ((Binding.qualify true fname (Binding.name "simps"), []), rec') #> snd) | |
| 52727 | 303 | |> (Local_Theory.note ((Binding.qualify true fname (Binding.name "mono"), []), [mono_thm]) #> snd) | 
| 43083 
df41a5762c3d
generate raw induction rule as instance of generic rule with careful treatment of currying
 krauss parents: 
43080diff
changeset | 304 | |> (case raw_induct of NONE => I | SOME thm => | 
| 
df41a5762c3d
generate raw induction rule as instance of generic rule with careful treatment of currying
 krauss parents: 
43080diff
changeset | 305 | Local_Theory.note ((Binding.qualify true fname (Binding.name "raw_induct"), []), [thm]) #> snd) | 
| 52728 
470b579f35d2
derive specialized version of full fixpoint induction (with admissibility)
 krauss parents: 
52727diff
changeset | 306 | |> (Local_Theory.note ((Binding.qualify true fname (Binding.name "fixp_induct"), []), [specialized_fixp_induct]) #> snd) | 
| 40107 | 307 | end; | 
| 308 | ||
| 309 | val add_partial_function = gen_add_partial_function Specification.check_spec; | |
| 310 | val add_partial_function_cmd = gen_add_partial_function Specification.read_spec; | |
| 311 | ||
| 46949 | 312 | val mode = @{keyword "("} |-- Parse.xname --| @{keyword ")"};
 | 
| 40107 | 313 | |
| 46961 
5c6955f487e5
outer syntax command definitions based on formal command_spec derived from theory header declarations;
 wenzelm parents: 
46949diff
changeset | 314 | val _ = | 
| 
5c6955f487e5
outer syntax command definitions based on formal command_spec derived from theory header declarations;
 wenzelm parents: 
46949diff
changeset | 315 |   Outer_Syntax.local_theory @{command_spec "partial_function"} "define partial function"
 | 
| 
5c6955f487e5
outer syntax command definitions based on formal command_spec derived from theory header declarations;
 wenzelm parents: 
46949diff
changeset | 316 | ((mode -- (Parse.fixes -- (Parse.where_ |-- Parse_Spec.spec))) | 
| 
5c6955f487e5
outer syntax command definitions based on formal command_spec derived from theory header declarations;
 wenzelm parents: 
46949diff
changeset | 317 | >> (fn (mode, (fixes, spec)) => add_partial_function_cmd mode fixes spec)); | 
| 40107 | 318 | |
| 319 | ||
| 320 | val setup = Mono_Rules.setup; | |
| 321 | ||
| 322 | end |