| author | wenzelm | 
| Sun, 06 Mar 2016 13:19:19 +0100 | |
| changeset 62528 | c8c532b22947 | 
| parent 62093 | bd73a2279fcd | 
| child 62969 | 9f394a16c557 | 
| 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 | |
| 52728 
470b579f35d2
derive specialized version of full fixpoint induction (with admissibility)
 krauss parents: 
52727diff
changeset | 9 | val init: string -> term -> term -> thm -> thm -> thm option -> declaration | 
| 52727 | 10 | val mono_tac: Proof.context -> int -> tactic | 
| 40107 | 11 | val add_partial_function: string -> (binding * typ option * mixfix) list -> | 
| 12 | Attrib.binding * term -> local_theory -> local_theory | |
| 13 | val add_partial_function_cmd: string -> (binding * string option * mixfix) list -> | |
| 14 | Attrib.binding * string -> local_theory -> local_theory | |
| 15 | end; | |
| 16 | ||
| 17 | structure Partial_Function: PARTIAL_FUNCTION = | |
| 18 | struct | |
| 19 | ||
| 20 | (*** Context Data ***) | |
| 21 | ||
| 61113 | 22 | datatype setup_data = Setup_Data of | 
| 43080 | 23 |  {fixp: term,
 | 
| 24 | mono: term, | |
| 25 | fixp_eq: thm, | |
| 52728 
470b579f35d2
derive specialized version of full fixpoint induction (with admissibility)
 krauss parents: 
52727diff
changeset | 26 | fixp_induct: thm, | 
| 
470b579f35d2
derive specialized version of full fixpoint induction (with admissibility)
 krauss parents: 
52727diff
changeset | 27 | fixp_induct_user: thm option}; | 
| 43080 | 28 | |
| 61113 | 29 | fun transform_setup_data phi (Setup_Data {fixp, mono, fixp_eq, fixp_induct, fixp_induct_user}) =
 | 
| 30 | let | |
| 31 | val term = Morphism.term phi; | |
| 32 | val thm = Morphism.thm phi; | |
| 33 | in | |
| 34 | Setup_Data | |
| 35 |      {fixp = term fixp, mono = term mono, fixp_eq = thm fixp_eq,
 | |
| 36 | fixp_induct = thm fixp_induct, fixp_induct_user = Option.map thm fixp_induct_user} | |
| 37 | end; | |
| 38 | ||
| 40107 | 39 | structure Modes = Generic_Data | 
| 40 | ( | |
| 43080 | 41 | type T = setup_data Symtab.table; | 
| 40107 | 42 | val empty = Symtab.empty; | 
| 43 | val extend = I; | |
| 41472 
f6ab14e61604
misc tuning and comments based on review of Theory_Data, Proof_Data, Generic_Data usage;
 wenzelm parents: 
41117diff
changeset | 44 | fun merge data = Symtab.merge (K true) data; | 
| 40107 | 45 | ) | 
| 46 | ||
| 52728 
470b579f35d2
derive specialized version of full fixpoint induction (with admissibility)
 krauss parents: 
52727diff
changeset | 47 | fun init mode fixp mono fixp_eq fixp_induct fixp_induct_user phi = | 
| 40107 | 48 | let | 
| 61113 | 49 | val data' = | 
| 50 | Setup_Data | |
| 51 |        {fixp = fixp, mono = mono, fixp_eq = fixp_eq,
 | |
| 52 | fixp_induct = fixp_induct, fixp_induct_user = fixp_induct_user} | |
| 53 | |> transform_setup_data (phi $> Morphism.trim_context_morphism); | |
| 54 | in Modes.map (Symtab.update (mode, data')) end; | |
| 40107 | 55 | |
| 56 | val known_modes = Symtab.keys o Modes.get o Context.Proof; | |
| 61113 | 57 | |
| 58 | fun lookup_mode ctxt = | |
| 59 | Symtab.lookup (Modes.get (Context.Proof ctxt)) | |
| 60 | #> Option.map (transform_setup_data (Morphism.transfer_morphism (Proof_Context.theory_of ctxt))); | |
| 40107 | 61 | |
| 62 | ||
| 63 | (*** Automated monotonicity proofs ***) | |
| 64 | ||
| 65 | (*rewrite conclusion with k-th assumtion*) | |
| 66 | fun rewrite_with_asm_tac ctxt k = | |
| 45403 | 67 |   Subgoal.FOCUS (fn {context = ctxt', prems, ...} =>
 | 
| 40107 | 68 | Local_Defs.unfold_tac ctxt' [nth prems k]) ctxt; | 
| 69 | ||
| 54405 
88f6d5b1422f
ported 'partial_function' to 'Ctr_Sugar' abstraction
 blanchet parents: 
52728diff
changeset | 70 | fun dest_case ctxt t = | 
| 40107 | 71 | case strip_comb t of | 
| 72 | (Const (case_comb, _), args) => | |
| 54405 
88f6d5b1422f
ported 'partial_function' to 'Ctr_Sugar' abstraction
 blanchet parents: 
52728diff
changeset | 73 | (case Ctr_Sugar.ctr_sugar_of_case ctxt case_comb of | 
| 40107 | 74 | NONE => NONE | 
| 54405 
88f6d5b1422f
ported 'partial_function' to 'Ctr_Sugar' abstraction
 blanchet parents: 
52728diff
changeset | 75 |        | SOME {case_thms, ...} =>
 | 
| 40107 | 76 | let | 
| 59582 | 77 | val lhs = Thm.prop_of (hd case_thms) | 
| 40107 | 78 | |> HOLogic.dest_Trueprop |> HOLogic.dest_eq |> fst; | 
| 79 | val arity = length (snd (strip_comb lhs)); | |
| 80 | val conv = funpow (length args - arity) Conv.fun_conv | |
| 54405 
88f6d5b1422f
ported 'partial_function' to 'Ctr_Sugar' abstraction
 blanchet parents: 
52728diff
changeset | 81 | (Conv.rewrs_conv (map mk_meta_eq case_thms)); | 
| 40107 | 82 | in | 
| 83 | SOME (nth args (arity - 1), conv) | |
| 84 | end) | |
| 85 | | _ => NONE; | |
| 86 | ||
| 87 | (*split on case expressions*) | |
| 59498 
50b60f501b05
proper context for resolve_tac, eresolve_tac, dresolve_tac, forward_tac etc.;
 wenzelm parents: 
58839diff
changeset | 88 | val split_cases_tac = Subgoal.FOCUS_PARAMS (fn {context = ctxt, ...} =>
 | 
| 40107 | 89 | SUBGOAL (fn (t, i) => case t of | 
| 90 | _ $ (_ $ Abs (_, _, body)) => | |
| 54405 
88f6d5b1422f
ported 'partial_function' to 'Ctr_Sugar' abstraction
 blanchet parents: 
52728diff
changeset | 91 | (case dest_case ctxt body of | 
| 40107 | 92 | NONE => no_tac | 
| 93 | | SOME (arg, conv) => | |
| 94 | 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 | 95 | if Term.is_open arg then no_tac | 
| 61844 | 96 | else ((DETERM o Induct.cases_tac ctxt false [[SOME arg]] NONE []) | 
| 40107 | 97 | THEN_ALL_NEW (rewrite_with_asm_tac ctxt 0) | 
| 59498 
50b60f501b05
proper context for resolve_tac, eresolve_tac, dresolve_tac, forward_tac etc.;
 wenzelm parents: 
58839diff
changeset | 98 |                 THEN_ALL_NEW eresolve_tac ctxt @{thms thin_rl}
 | 
| 40107 | 99 | THEN_ALL_NEW (CONVERSION | 
| 100 | (params_conv ~1 (fn ctxt' => | |
| 101 | arg_conv (arg_conv (abs_conv (K conv) ctxt'))) ctxt))) i | |
| 102 | end) | |
| 103 | | _ => no_tac) 1); | |
| 104 | ||
| 105 | (*monotonicity proof: apply rules + split case expressions*) | |
| 106 | fun mono_tac ctxt = | |
| 107 |   K (Local_Defs.unfold_tac ctxt [@{thm curry_def}])
 | |
| 108 | THEN' (TRY o REPEAT_ALL_NEW | |
| 59498 
50b60f501b05
proper context for resolve_tac, eresolve_tac, dresolve_tac, forward_tac etc.;
 wenzelm parents: 
58839diff
changeset | 109 |    (resolve_tac ctxt (rev (Named_Theorems.get ctxt @{named_theorems partial_function_mono}))
 | 
| 40107 | 110 | ORELSE' split_cases_tac ctxt)); | 
| 111 | ||
| 112 | ||
| 113 | (*** Auxiliary functions ***) | |
| 114 | ||
| 115 | (*Returns t $ u, but instantiates the type of t to make the | |
| 116 | application type correct*) | |
| 117 | fun apply_inst ctxt t u = | |
| 118 | let | |
| 42361 | 119 | val thy = Proof_Context.theory_of ctxt; | 
| 40107 | 120 | val T = domain_type (fastype_of t); | 
| 121 | val T' = fastype_of u; | |
| 42388 | 122 | val subst = Sign.typ_match thy (T, T') Vartab.empty | 
| 40107 | 123 |       handle Type.TYPE_MATCH => raise TYPE ("apply_inst", [T, T'], [t, u])
 | 
| 124 | in | |
| 125 | map_types (Envir.norm_type subst) t $ u | |
| 126 | end; | |
| 127 | ||
| 128 | fun head_conv cv ct = | |
| 129 | if can Thm.dest_comb ct then Conv.fun_conv (head_conv cv) ct else cv ct; | |
| 130 | ||
| 131 | ||
| 132 | (*** currying transformation ***) | |
| 133 | ||
| 134 | fun curry_const (A, B, C) = | |
| 135 |   Const (@{const_name Product_Type.curry},
 | |
| 136 | [HOLogic.mk_prodT (A, B) --> C, A, B] ---> C); | |
| 137 | ||
| 138 | fun mk_curry f = | |
| 139 | case fastype_of f of | |
| 140 |     Type ("fun", [Type (_, [S, T]), U]) =>
 | |
| 141 | curry_const (S, T, U) $ f | |
| 142 |   | T => raise TYPE ("mk_curry", [T], [f]);
 | |
| 143 | ||
| 144 | (* iterated versions. Nonstandard left-nested tuples arise naturally | |
| 145 | from "split o split o split"*) | |
| 146 | fun curry_n arity = funpow (arity - 1) mk_curry; | |
| 61424 
c3658c18b7bc
prod_case as canonical name for product type eliminator
 haftmann parents: 
61121diff
changeset | 147 | fun uncurry_n arity = funpow (arity - 1) HOLogic.mk_case_prod; | 
| 40107 | 148 | |
| 51717 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51484diff
changeset | 149 | val curry_uncurry_ss = | 
| 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51484diff
changeset | 150 |   simpset_of (put_simpset HOL_basic_ss @{context}
 | 
| 61424 
c3658c18b7bc
prod_case as canonical name for product type eliminator
 haftmann parents: 
61121diff
changeset | 151 |     addsimps [@{thm Product_Type.curry_case_prod}, @{thm Product_Type.case_prod_curry}])
 | 
| 40107 | 152 | |
| 51717 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51484diff
changeset | 153 | val split_conv_ss = | 
| 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51484diff
changeset | 154 |   simpset_of (put_simpset HOL_basic_ss @{context}
 | 
| 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51484diff
changeset | 155 |     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 | 156 | |
| 54630 
9061af4d5ebc
restrict admissibility to non-empty chains to allow more syntax-directed proof rules
 Andreas Lochbihler parents: 
54405diff
changeset | 157 | val curry_K_ss = | 
| 
9061af4d5ebc
restrict admissibility to non-empty chains to allow more syntax-directed proof rules
 Andreas Lochbihler parents: 
54405diff
changeset | 158 |   simpset_of (put_simpset HOL_basic_ss @{context}
 | 
| 
9061af4d5ebc
restrict admissibility to non-empty chains to allow more syntax-directed proof rules
 Andreas Lochbihler parents: 
54405diff
changeset | 159 |     addsimps [@{thm Product_Type.curry_K}]);
 | 
| 52728 
470b579f35d2
derive specialized version of full fixpoint induction (with admissibility)
 krauss parents: 
52727diff
changeset | 160 | |
| 
470b579f35d2
derive specialized version of full fixpoint induction (with admissibility)
 krauss parents: 
52727diff
changeset | 161 | (* instantiate generic fixpoint induction and eliminate the canonical assumptions; | 
| 
470b579f35d2
derive specialized version of full fixpoint induction (with admissibility)
 krauss parents: 
52727diff
changeset | 162 | curry induction predicate *) | 
| 
470b579f35d2
derive specialized version of full fixpoint induction (with admissibility)
 krauss parents: 
52727diff
changeset | 163 | 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 | 164 | let | 
| 
470b579f35d2
derive specialized version of full fixpoint induction (with admissibility)
 krauss parents: 
52727diff
changeset | 165 | val ([P], ctxt') = Variable.variant_fixes ["P"] ctxt | 
| 
470b579f35d2
derive specialized version of full fixpoint induction (with admissibility)
 krauss parents: 
52727diff
changeset | 166 |     val P_inst = Abs ("f", fT_uc, Free (P, fT --> HOLogic.boolT) $ (curry $ Bound 0))
 | 
| 61113 | 167 | in | 
| 54742 
7a86358a3c0b
proper context for basic Simplifier operations: rewrite_rule, rewrite_goals_rule, rewrite_goals_tac etc.;
 wenzelm parents: 
54630diff
changeset | 168 | (* FIXME ctxt vs. ctxt' (!?) *) | 
| 52728 
470b579f35d2
derive specialized version of full fixpoint induction (with admissibility)
 krauss parents: 
52727diff
changeset | 169 | rule | 
| 60784 | 170 | |> infer_instantiate' ctxt | 
| 171 | ((map o Option.map) (Thm.cterm_of ctxt) [SOME uncurry, NONE, SOME curry, NONE, SOME P_inst]) | |
| 52728 
470b579f35d2
derive specialized version of full fixpoint induction (with admissibility)
 krauss parents: 
52727diff
changeset | 172 | |> Tactic.rule_by_tactic ctxt | 
| 
470b579f35d2
derive specialized version of full fixpoint induction (with admissibility)
 krauss parents: 
52727diff
changeset | 173 | (Simplifier.simp_tac (put_simpset curry_uncurry_ss ctxt) 3 (* discharge U (C f) = f *) | 
| 54630 
9061af4d5ebc
restrict admissibility to non-empty chains to allow more syntax-directed proof rules
 Andreas Lochbihler parents: 
54405diff
changeset | 174 | THEN Simplifier.simp_tac (put_simpset curry_K_ss ctxt) 4 (* simplify bot case *) | 
| 
9061af4d5ebc
restrict admissibility to non-empty chains to allow more syntax-directed proof rules
 Andreas Lochbihler parents: 
54405diff
changeset | 175 | THEN Simplifier.full_simp_tac (put_simpset curry_uncurry_ss ctxt) 5) (* simplify induction step *) | 
| 52728 
470b579f35d2
derive specialized version of full fixpoint induction (with admissibility)
 krauss parents: 
52727diff
changeset | 176 | |> (fn thm => thm OF [mono_thm, f_def]) | 
| 
470b579f35d2
derive specialized version of full fixpoint induction (with admissibility)
 krauss parents: 
52727diff
changeset | 177 | |> Conv.fconv_rule (Conv.concl_conv ~1 (* simplify conclusion *) | 
| 61424 
c3658c18b7bc
prod_case as canonical name for product type eliminator
 haftmann parents: 
61121diff
changeset | 178 |          (Raw_Simplifier.rewrite ctxt false [mk_meta_eq @{thm Product_Type.curry_case_prod}]))
 | 
| 52728 
470b579f35d2
derive specialized version of full fixpoint induction (with admissibility)
 krauss parents: 
52727diff
changeset | 179 | |> singleton (Variable.export ctxt' ctxt) | 
| 
470b579f35d2
derive specialized version of full fixpoint induction (with admissibility)
 krauss parents: 
52727diff
changeset | 180 | end | 
| 
470b579f35d2
derive specialized version of full fixpoint induction (with admissibility)
 krauss parents: 
52727diff
changeset | 181 | |
| 52727 | 182 | 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 | 183 | let | 
| 59621 
291934bac95e
Thm.cterm_of and Thm.ctyp_of operate on local context;
 wenzelm parents: 
59582diff
changeset | 184 | val cert = Thm.cterm_of ctxt | 
| 43083 
df41a5762c3d
generate raw induction rule as instance of generic rule with careful treatment of currying
 krauss parents: 
43080diff
changeset | 185 | 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 | 186 | |
| 
df41a5762c3d
generate raw induction rule as instance of generic rule with careful treatment of currying
 krauss parents: 
43080diff
changeset | 187 | val split_paired_all_conv = | 
| 
df41a5762c3d
generate raw induction rule as instance of generic rule with careful treatment of currying
 krauss parents: 
43080diff
changeset | 188 |       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 | 189 | |
| 61113 | 190 | val split_params_conv = | 
| 43083 
df41a5762c3d
generate raw induction rule as instance of generic rule with careful treatment of currying
 krauss parents: 
43080diff
changeset | 191 | 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 | 192 | 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 | 193 | |
| 52521 
a1c4f586e372
more robust instantiation: extract vars from conclusion instead of relying on fixed overall ordering
 krauss parents: 
52087diff
changeset | 194 | val (P_var, x_var) = | 
| 51484 
49eb8d73ae10
allow induction predicates with arbitrary arity (not just binary)
 krauss parents: 
46961diff
changeset | 195 | 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 | 196 | |> strip_comb |> apsnd hd | 
| 60642 
48dd1cefb4ae
simplified Thm.instantiate and derivatives: the LHS refers to non-certified variables -- this merely serves as index into already certified structures (or is ignored);
 wenzelm parents: 
59936diff
changeset | 197 | |> apply2 dest_Var | 
| 
48dd1cefb4ae
simplified Thm.instantiate and derivatives: the LHS refers to non-certified variables -- this merely serves as index into already certified structures (or is ignored);
 wenzelm parents: 
59936diff
changeset | 198 | val P_rangeT = range_type (snd P_var) | 
| 51484 
49eb8d73ae10
allow induction predicates with arbitrary arity (not just binary)
 krauss parents: 
46961diff
changeset | 199 | 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 | 200 | 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 | 201 | 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 | 202 | |
| 
df41a5762c3d
generate raw induction rule as instance of generic rule with careful treatment of currying
 krauss parents: 
43080diff
changeset | 203 | val inst_rule' = inst_rule | 
| 
df41a5762c3d
generate raw induction rule as instance of generic rule with careful treatment of currying
 krauss parents: 
43080diff
changeset | 204 | |> Tactic.rule_by_tactic ctxt | 
| 51717 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51484diff
changeset | 205 | (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 | 206 | 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 | 207 | 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 | 208 | then_conv (Conv.forall_conv (K split_paired_all_conv) ctxt)) 3) | 
| 60642 
48dd1cefb4ae
simplified Thm.instantiate and derivatives: the LHS refers to non-certified variables -- this merely serves as index into already certified structures (or is ignored);
 wenzelm parents: 
59936diff
changeset | 209 | |> Thm.instantiate ([], [(P_var, P_inst), (x_var, x_inst)]) | 
| 51717 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51484diff
changeset | 210 | |> 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 | 211 | |> singleton (Variable.export ctxt' ctxt) | 
| 
df41a5762c3d
generate raw induction rule as instance of generic rule with careful treatment of currying
 krauss parents: 
43080diff
changeset | 212 | in | 
| 
df41a5762c3d
generate raw induction rule as instance of generic rule with careful treatment of currying
 krauss parents: 
43080diff
changeset | 213 | inst_rule' | 
| 
df41a5762c3d
generate raw induction rule as instance of generic rule with careful treatment of currying
 krauss parents: 
43080diff
changeset | 214 | end; | 
| 61113 | 215 | |
| 40107 | 216 | |
| 217 | (*** partial_function definition ***) | |
| 218 | ||
| 219 | fun gen_add_partial_function prep mode fixes_raw eqn_raw lthy = | |
| 220 | let | |
| 43080 | 221 | val setup_data = the (lookup_mode lthy mode) | 
| 40107 | 222 | handle Option.Option => error (cat_lines ["Unknown mode " ^ quote mode ^ ".", | 
| 223 | "Known modes are " ^ commas_quote (known_modes lthy) ^ "."]); | |
| 52728 
470b579f35d2
derive specialized version of full fixpoint induction (with admissibility)
 krauss parents: 
52727diff
changeset | 224 |     val Setup_Data {fixp, mono, fixp_eq, fixp_induct, fixp_induct_user} = setup_data;
 | 
| 40107 | 225 | |
| 226 | val ((fixes, [(eq_abinding, eqn)]), _) = prep fixes_raw [eqn_raw] lthy; | |
| 60695 
757549b4bbe6
Variable.focus etc.: optional bindings provided by user;
 wenzelm parents: 
60642diff
changeset | 227 | val ((_, plain_eqn), args_ctxt) = Variable.focus NONE eqn lthy; | 
| 40107 | 228 | |
| 229 | val ((f_binding, fT), mixfix) = the_single fixes; | |
| 230 | val fname = Binding.name_of f_binding; | |
| 231 | ||
| 232 | val (lhs, rhs) = HOLogic.dest_eq (HOLogic.dest_Trueprop plain_eqn); | |
| 233 | 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 | 234 | val argnames = map (fst o dest_Free) args; | 
| 40107 | 235 | val F = fold_rev lambda (head :: args) rhs; | 
| 236 | ||
| 237 | val arity = length args; | |
| 238 | val (aTs, bTs) = chop arity (binder_types fT); | |
| 239 | ||
| 240 | val tupleT = foldl1 HOLogic.mk_prodT aTs; | |
| 241 | val fT_uc = tupleT :: bTs ---> body_type fT; | |
| 242 | val f_uc = Var ((fname, 0), fT_uc); | |
| 243 |     val x_uc = Var (("x", 0), tupleT);
 | |
| 244 | val uncurry = lambda head (uncurry_n arity head); | |
| 245 | val curry = lambda f_uc (curry_n arity f_uc); | |
| 246 | ||
| 247 | val F_uc = | |
| 248 | lambda f_uc (uncurry_n arity (F $ curry_n arity f_uc)); | |
| 249 | ||
| 250 | val mono_goal = apply_inst lthy mono (lambda f_uc (F_uc $ f_uc $ x_uc)) | |
| 251 | |> HOLogic.mk_Trueprop | |
| 252 | |> Logic.all x_uc; | |
| 253 | ||
| 60784 | 254 | val mono_thm = Goal.prove_internal lthy [] (Thm.cterm_of lthy mono_goal) | 
| 40107 | 255 | (K (mono_tac lthy 1)) | 
| 60784 | 256 | val inst_mono_thm = Thm.forall_elim (Thm.cterm_of lthy x_uc) mono_thm | 
| 40107 | 257 | |
| 258 | val f_def_rhs = curry_n arity (apply_inst lthy fixp F_uc); | |
| 61121 
efe8b18306b7
do not expose low-level "_def" facts of 'function' definitions, to avoid potential confusion with the situation of plain 'definition';
 wenzelm parents: 
61113diff
changeset | 259 | val f_def_binding = | 
| 62093 | 260 | if Config.get lthy Function_Lib.function_internals | 
| 261 | then (Binding.name (Thm.def_name fname), []) | |
| 61121 
efe8b18306b7
do not expose low-level "_def" facts of 'function' definitions, to avoid potential confusion with the situation of plain 'definition';
 wenzelm parents: 
61113diff
changeset | 262 | else Attrib.empty_binding; | 
| 40107 | 263 | val ((f, (_, f_def)), lthy') = Local_Theory.define | 
| 61121 
efe8b18306b7
do not expose low-level "_def" facts of 'function' definitions, to avoid potential confusion with the situation of plain 'definition';
 wenzelm parents: 
61113diff
changeset | 264 | ((f_binding, mixfix), (f_def_binding, f_def_rhs)) lthy; | 
| 40107 | 265 | |
| 266 | val eqn = HOLogic.mk_eq (list_comb (f, args), | |
| 267 | Term.betapplys (F, f :: args)) | |
| 268 | |> HOLogic.mk_Trueprop; | |
| 269 | ||
| 270 | val unfold = | |
| 60784 | 271 | (infer_instantiate' lthy' (map (SOME o Thm.cterm_of lthy') [uncurry, F, curry]) fixp_eq | 
| 52727 | 272 | OF [inst_mono_thm, f_def]) | 
| 52087 
f3075fc4f5f6
more precise treatment of theory vs. Proof.context;
 wenzelm parents: 
51717diff
changeset | 273 | |> Tactic.rule_by_tactic lthy' (Simplifier.simp_tac (put_simpset curry_uncurry_ss lthy') 1); | 
| 40107 | 274 | |
| 61113 | 275 | val specialized_fixp_induct = | 
| 52728 
470b579f35d2
derive specialized version of full fixpoint induction (with admissibility)
 krauss parents: 
52727diff
changeset | 276 | 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 | 277 | |> Drule.rename_bvars' (map SOME (fname :: fname :: argnames)); | 
| 
470b579f35d2
derive specialized version of full fixpoint induction (with admissibility)
 krauss parents: 
52727diff
changeset | 278 | |
| 43083 
df41a5762c3d
generate raw induction rule as instance of generic rule with careful treatment of currying
 krauss parents: 
43080diff
changeset | 279 | val mk_raw_induct = | 
| 60784 | 280 | infer_instantiate' args_ctxt | 
| 281 | ((map o Option.map) (Thm.cterm_of args_ctxt) [SOME uncurry, NONE, SOME curry]) | |
| 52727 | 282 | #> mk_curried_induct args args_ctxt | 
| 52087 
f3075fc4f5f6
more precise treatment of theory vs. Proof.context;
 wenzelm parents: 
51717diff
changeset | 283 | #> singleton (Variable.export args_ctxt lthy') | 
| 60784 | 284 | #> (fn thm => infer_instantiate' lthy' | 
| 285 | [SOME (Thm.cterm_of lthy' 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 | 286 | #> 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 | 287 | |
| 52728 
470b579f35d2
derive specialized version of full fixpoint induction (with admissibility)
 krauss parents: 
52727diff
changeset | 288 | val raw_induct = Option.map mk_raw_induct fixp_induct_user | 
| 40107 | 289 | val rec_rule = let open Conv in | 
| 290 | Goal.prove lthy' (map (fst o dest_Free) args) [] eqn (fn _ => | |
| 291 | CONVERSION ((arg_conv o arg1_conv o head_conv o rewr_conv) (mk_meta_eq unfold)) 1 | |
| 59498 
50b60f501b05
proper context for resolve_tac, eresolve_tac, dresolve_tac, forward_tac etc.;
 wenzelm parents: 
58839diff
changeset | 292 |         THEN resolve_tac lthy' @{thms refl} 1) end;
 | 
| 40107 | 293 | in | 
| 294 | lthy' | |
| 295 | |> Local_Theory.note (eq_abinding, [rec_rule]) | |
| 296 | |-> (fn (_, rec') => | |
| 40180 | 297 | Spec_Rules.add Spec_Rules.Equational ([f], rec') | 
| 298 | #> Local_Theory.note ((Binding.qualify true fname (Binding.name "simps"), []), rec') #> snd) | |
| 61113 | 299 | |> (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 | 300 | |> (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 | 301 | Local_Theory.note ((Binding.qualify true fname (Binding.name "raw_induct"), []), [thm]) #> snd) | 
| 61113 | 302 | |> (Local_Theory.note ((Binding.qualify true fname (Binding.name "fixp_induct"), []), [specialized_fixp_induct]) #> snd) | 
| 40107 | 303 | end; | 
| 304 | ||
| 305 | val add_partial_function = gen_add_partial_function Specification.check_spec; | |
| 306 | val add_partial_function_cmd = gen_add_partial_function Specification.read_spec; | |
| 307 | ||
| 46949 | 308 | val mode = @{keyword "("} |-- Parse.xname --| @{keyword ")"};
 | 
| 40107 | 309 | |
| 46961 
5c6955f487e5
outer syntax command definitions based on formal command_spec derived from theory header declarations;
 wenzelm parents: 
46949diff
changeset | 310 | val _ = | 
| 59936 
b8ffc3dc9e24
@{command_spec} is superseded by @{command_keyword};
 wenzelm parents: 
59859diff
changeset | 311 |   Outer_Syntax.local_theory @{command_keyword partial_function} "define partial function"
 | 
| 46961 
5c6955f487e5
outer syntax command definitions based on formal command_spec derived from theory header declarations;
 wenzelm parents: 
46949diff
changeset | 312 | ((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 | 313 | >> (fn (mode, (fixes, spec)) => add_partial_function_cmd mode fixes spec)); | 
| 40107 | 314 | |
| 61113 | 315 | end; |