| author | krauss | 
| Tue, 20 Sep 2011 01:32:04 +0200 | |
| changeset 45008 | 8b74cfea913a | 
| parent 44239 | 47ecd30e018d | 
| child 45290 | f599ac41e7f5 | 
| permissions | -rw-r--r-- | 
| 37744 | 1 | (* Title: HOL/Tools/Function/function.ML | 
| 33099 
b8cdd3d73022
function package: more standard names for structures and files
 krauss parents: diff
changeset | 2 | Author: Alexander Krauss, TU Muenchen | 
| 
b8cdd3d73022
function package: more standard names for structures and files
 krauss parents: diff
changeset | 3 | |
| 41114 | 4 | Main entry points to the function package. | 
| 33099 
b8cdd3d73022
function package: more standard names for structures and files
 krauss parents: diff
changeset | 5 | *) | 
| 
b8cdd3d73022
function package: more standard names for structures and files
 krauss parents: diff
changeset | 6 | |
| 
b8cdd3d73022
function package: more standard names for structures and files
 krauss parents: diff
changeset | 7 | signature FUNCTION = | 
| 
b8cdd3d73022
function package: more standard names for structures and files
 krauss parents: diff
changeset | 8 | sig | 
| 34232 
36a2a3029fd3
new year's resolution: reindented code in function package
 krauss parents: 
34231diff
changeset | 9 | include FUNCTION_DATA | 
| 
36a2a3029fd3
new year's resolution: reindented code in function package
 krauss parents: 
34231diff
changeset | 10 | |
| 36520 | 11 | val add_function: (binding * typ option * mixfix) list -> | 
| 12 | (Attrib.binding * term) list -> Function_Common.function_config -> | |
| 36522 
e80a95279ef6
return info record (relative to auxiliary context!)
 krauss parents: 
36520diff
changeset | 13 | (Proof.context -> tactic) -> local_theory -> info * local_theory | 
| 36520 | 14 | |
| 15 | val add_function_cmd: (binding * string option * mixfix) list -> | |
| 16 | (Attrib.binding * string) list -> Function_Common.function_config -> | |
| 44239 
47ecd30e018d
less verbosity for 'function' and 'fun': observe "int" flag more carefully (cf. a32ca9165928);
 wenzelm parents: 
44192diff
changeset | 17 | (Proof.context -> tactic) -> bool -> local_theory -> info * local_theory | 
| 36520 | 18 | |
| 36519 
46bf776a81e0
ML interface uses plain command names, following conventions from typedef
 krauss parents: 
36518diff
changeset | 19 | val function: (binding * typ option * mixfix) list -> | 
| 34232 
36a2a3029fd3
new year's resolution: reindented code in function package
 krauss parents: 
34231diff
changeset | 20 | (Attrib.binding * term) list -> Function_Common.function_config -> | 
| 
36a2a3029fd3
new year's resolution: reindented code in function package
 krauss parents: 
34231diff
changeset | 21 | local_theory -> Proof.state | 
| 34230 | 22 | |
| 36519 
46bf776a81e0
ML interface uses plain command names, following conventions from typedef
 krauss parents: 
36518diff
changeset | 23 | val function_cmd: (binding * string option * mixfix) list -> | 
| 34232 
36a2a3029fd3
new year's resolution: reindented code in function package
 krauss parents: 
34231diff
changeset | 24 | (Attrib.binding * string) list -> Function_Common.function_config -> | 
| 44239 
47ecd30e018d
less verbosity for 'function' and 'fun': observe "int" flag more carefully (cf. a32ca9165928);
 wenzelm parents: 
44192diff
changeset | 25 | bool -> local_theory -> Proof.state | 
| 33099 
b8cdd3d73022
function package: more standard names for structures and files
 krauss parents: diff
changeset | 26 | |
| 36547 
2a9d0ec8c10d
return updated info record after termination proof
 krauss parents: 
36522diff
changeset | 27 | val prove_termination: term option -> tactic -> local_theory -> | 
| 
2a9d0ec8c10d
return updated info record after termination proof
 krauss parents: 
36522diff
changeset | 28 | info * local_theory | 
| 
2a9d0ec8c10d
return updated info record after termination proof
 krauss parents: 
36522diff
changeset | 29 | val prove_termination_cmd: string option -> tactic -> local_theory -> | 
| 
2a9d0ec8c10d
return updated info record after termination proof
 krauss parents: 
36522diff
changeset | 30 | info * local_theory | 
| 36520 | 31 | |
| 36519 
46bf776a81e0
ML interface uses plain command names, following conventions from typedef
 krauss parents: 
36518diff
changeset | 32 | val termination : term option -> local_theory -> Proof.state | 
| 
46bf776a81e0
ML interface uses plain command names, following conventions from typedef
 krauss parents: 
36518diff
changeset | 33 | val termination_cmd : string option -> local_theory -> Proof.state | 
| 33099 
b8cdd3d73022
function package: more standard names for structures and files
 krauss parents: diff
changeset | 34 | |
| 34232 
36a2a3029fd3
new year's resolution: reindented code in function package
 krauss parents: 
34231diff
changeset | 35 | val setup : theory -> theory | 
| 
36a2a3029fd3
new year's resolution: reindented code in function package
 krauss parents: 
34231diff
changeset | 36 | val get_congs : Proof.context -> thm list | 
| 34230 | 37 | |
| 34232 
36a2a3029fd3
new year's resolution: reindented code in function package
 krauss parents: 
34231diff
changeset | 38 | val get_info : Proof.context -> term -> info | 
| 33099 
b8cdd3d73022
function package: more standard names for structures and files
 krauss parents: diff
changeset | 39 | end | 
| 
b8cdd3d73022
function package: more standard names for structures and files
 krauss parents: diff
changeset | 40 | |
| 
b8cdd3d73022
function package: more standard names for structures and files
 krauss parents: diff
changeset | 41 | |
| 
b8cdd3d73022
function package: more standard names for structures and files
 krauss parents: diff
changeset | 42 | structure Function : FUNCTION = | 
| 
b8cdd3d73022
function package: more standard names for structures and files
 krauss parents: diff
changeset | 43 | struct | 
| 
b8cdd3d73022
function package: more standard names for structures and files
 krauss parents: diff
changeset | 44 | |
| 
b8cdd3d73022
function package: more standard names for structures and files
 krauss parents: diff
changeset | 45 | open Function_Lib | 
| 
b8cdd3d73022
function package: more standard names for structures and files
 krauss parents: diff
changeset | 46 | open Function_Common | 
| 
b8cdd3d73022
function package: more standard names for structures and files
 krauss parents: diff
changeset | 47 | |
| 
b8cdd3d73022
function package: more standard names for structures and files
 krauss parents: diff
changeset | 48 | val simp_attribs = map (Attrib.internal o K) | 
| 34232 
36a2a3029fd3
new year's resolution: reindented code in function package
 krauss parents: 
34231diff
changeset | 49 | [Simplifier.simp_add, | 
| 
36a2a3029fd3
new year's resolution: reindented code in function package
 krauss parents: 
34231diff
changeset | 50 | Code.add_default_eqn_attribute, | 
| 
36a2a3029fd3
new year's resolution: reindented code in function package
 krauss parents: 
34231diff
changeset | 51 | Nitpick_Simps.add] | 
| 33099 
b8cdd3d73022
function package: more standard names for structures and files
 krauss parents: diff
changeset | 52 | |
| 
b8cdd3d73022
function package: more standard names for structures and files
 krauss parents: diff
changeset | 53 | val psimp_attribs = map (Attrib.internal o K) | 
| 39754 | 54 | [Nitpick_Psimps.add] | 
| 33099 
b8cdd3d73022
function package: more standard names for structures and files
 krauss parents: diff
changeset | 55 | |
| 
b8cdd3d73022
function package: more standard names for structures and files
 krauss parents: diff
changeset | 56 | fun mk_defname fixes = fixes |> map (fst o fst) |> space_implode "_" | 
| 
b8cdd3d73022
function package: more standard names for structures and files
 krauss parents: diff
changeset | 57 | |
| 34232 
36a2a3029fd3
new year's resolution: reindented code in function package
 krauss parents: 
34231diff
changeset | 58 | fun add_simps fnames post sort extra_qualify label mod_binding moreatts | 
| 
36a2a3029fd3
new year's resolution: reindented code in function package
 krauss parents: 
34231diff
changeset | 59 | simps lthy = | 
| 
36a2a3029fd3
new year's resolution: reindented code in function package
 krauss parents: 
34231diff
changeset | 60 | let | 
| 
36a2a3029fd3
new year's resolution: reindented code in function package
 krauss parents: 
34231diff
changeset | 61 | val spec = post simps | 
| 
36a2a3029fd3
new year's resolution: reindented code in function package
 krauss parents: 
34231diff
changeset | 62 | |> map (apfst (apsnd (fn ats => moreatts @ ats))) | 
| 
36a2a3029fd3
new year's resolution: reindented code in function package
 krauss parents: 
34231diff
changeset | 63 | |> map (apfst (apfst extra_qualify)) | 
| 33099 
b8cdd3d73022
function package: more standard names for structures and files
 krauss parents: diff
changeset | 64 | |
| 34232 
36a2a3029fd3
new year's resolution: reindented code in function package
 krauss parents: 
34231diff
changeset | 65 | val (saved_spec_simps, lthy) = | 
| 
36a2a3029fd3
new year's resolution: reindented code in function package
 krauss parents: 
34231diff
changeset | 66 | fold_map Local_Theory.note spec lthy | 
| 33099 
b8cdd3d73022
function package: more standard names for structures and files
 krauss parents: diff
changeset | 67 | |
| 34232 
36a2a3029fd3
new year's resolution: reindented code in function package
 krauss parents: 
34231diff
changeset | 68 | val saved_simps = maps snd saved_spec_simps | 
| 
36a2a3029fd3
new year's resolution: reindented code in function package
 krauss parents: 
34231diff
changeset | 69 | val simps_by_f = sort saved_simps | 
| 33099 
b8cdd3d73022
function package: more standard names for structures and files
 krauss parents: diff
changeset | 70 | |
| 34232 
36a2a3029fd3
new year's resolution: reindented code in function package
 krauss parents: 
34231diff
changeset | 71 | fun add_for_f fname simps = | 
| 
36a2a3029fd3
new year's resolution: reindented code in function package
 krauss parents: 
34231diff
changeset | 72 | Local_Theory.note | 
| 
36a2a3029fd3
new year's resolution: reindented code in function package
 krauss parents: 
34231diff
changeset | 73 | ((mod_binding (Binding.qualify true fname (Binding.name label)), []), simps) | 
| 
36a2a3029fd3
new year's resolution: reindented code in function package
 krauss parents: 
34231diff
changeset | 74 | #> snd | 
| 
36a2a3029fd3
new year's resolution: reindented code in function package
 krauss parents: 
34231diff
changeset | 75 | in | 
| 
36a2a3029fd3
new year's resolution: reindented code in function package
 krauss parents: 
34231diff
changeset | 76 | (saved_simps, fold2 add_for_f fnames simps_by_f lthy) | 
| 
36a2a3029fd3
new year's resolution: reindented code in function package
 krauss parents: 
34231diff
changeset | 77 | end | 
| 33099 
b8cdd3d73022
function package: more standard names for structures and files
 krauss parents: diff
changeset | 78 | |
| 44239 
47ecd30e018d
less verbosity for 'function' and 'fun': observe "int" flag more carefully (cf. a32ca9165928);
 wenzelm parents: 
44192diff
changeset | 79 | fun prepare_function do_print prep default_constraint fixspec eqns config lthy = | 
| 34232 
36a2a3029fd3
new year's resolution: reindented code in function package
 krauss parents: 
34231diff
changeset | 80 | let | 
| 
36a2a3029fd3
new year's resolution: reindented code in function package
 krauss parents: 
34231diff
changeset | 81 | val constrn_fxs = map (fn (b, T, mx) => (b, SOME (the_default default_constraint T), mx)) | 
| 
36a2a3029fd3
new year's resolution: reindented code in function package
 krauss parents: 
34231diff
changeset | 82 | val ((fixes0, spec0), ctxt') = prep (constrn_fxs fixspec) eqns lthy | 
| 
36a2a3029fd3
new year's resolution: reindented code in function package
 krauss parents: 
34231diff
changeset | 83 | val fixes = map (apfst (apfst Binding.name_of)) fixes0; | 
| 
36a2a3029fd3
new year's resolution: reindented code in function package
 krauss parents: 
34231diff
changeset | 84 | val spec = map (fn (bnd, prop) => (bnd, [prop])) spec0; | 
| 
36a2a3029fd3
new year's resolution: reindented code in function package
 krauss parents: 
34231diff
changeset | 85 | val (eqs, post, sort_cont, cnames) = get_preproc lthy config ctxt' fixes spec | 
| 33099 
b8cdd3d73022
function package: more standard names for structures and files
 krauss parents: diff
changeset | 86 | |
| 34232 
36a2a3029fd3
new year's resolution: reindented code in function package
 krauss parents: 
34231diff
changeset | 87 | val defname = mk_defname fixes | 
| 41846 
b368a7aee46a
removed support for tail-recursion from function package (now implemented by partial_function)
 krauss parents: 
41417diff
changeset | 88 |     val FunctionConfig {partials, default, ...} = config
 | 
| 41417 | 89 | val _ = | 
| 44052 | 90 | if is_some default | 
| 91 | then legacy_feature "\"function (default)\" -- use 'partial_function' instead" | |
| 41417 | 92 | else () | 
| 33099 
b8cdd3d73022
function package: more standard names for structures and files
 krauss parents: diff
changeset | 93 | |
| 36519 
46bf776a81e0
ML interface uses plain command names, following conventions from typedef
 krauss parents: 
36518diff
changeset | 94 | val ((goal_state, cont), lthy') = | 
| 34232 
36a2a3029fd3
new year's resolution: reindented code in function package
 krauss parents: 
34231diff
changeset | 95 | Function_Mutual.prepare_function_mutual config defname fixes eqs lthy | 
| 33099 
b8cdd3d73022
function package: more standard names for structures and files
 krauss parents: diff
changeset | 96 | |
| 34232 
36a2a3029fd3
new year's resolution: reindented code in function package
 krauss parents: 
34231diff
changeset | 97 | fun afterqed [[proof]] lthy = | 
| 
36a2a3029fd3
new year's resolution: reindented code in function package
 krauss parents: 
34231diff
changeset | 98 | let | 
| 41846 
b368a7aee46a
removed support for tail-recursion from function package (now implemented by partial_function)
 krauss parents: 
41417diff
changeset | 99 |         val FunctionResult {fs, R, psimps, simple_pinducts,
 | 
| 34232 
36a2a3029fd3
new year's resolution: reindented code in function package
 krauss parents: 
34231diff
changeset | 100 | termination, domintros, cases, ...} = | 
| 33099 
b8cdd3d73022
function package: more standard names for structures and files
 krauss parents: diff
changeset | 101 | cont (Thm.close_derivation proof) | 
| 
b8cdd3d73022
function package: more standard names for structures and files
 krauss parents: diff
changeset | 102 | |
| 34232 
36a2a3029fd3
new year's resolution: reindented code in function package
 krauss parents: 
34231diff
changeset | 103 | val fnames = map (fst o fst) fixes | 
| 
36a2a3029fd3
new year's resolution: reindented code in function package
 krauss parents: 
34231diff
changeset | 104 | fun qualify n = Binding.name n | 
| 
36a2a3029fd3
new year's resolution: reindented code in function package
 krauss parents: 
34231diff
changeset | 105 | |> Binding.qualify true defname | 
| 
36a2a3029fd3
new year's resolution: reindented code in function package
 krauss parents: 
34231diff
changeset | 106 | val conceal_partial = if partials then I else Binding.conceal | 
| 33394 
9c6980f2eb39
conceal "termination" rule, used only by special tools
 krauss parents: 
33369diff
changeset | 107 | |
| 34232 
36a2a3029fd3
new year's resolution: reindented code in function package
 krauss parents: 
34231diff
changeset | 108 | val addsmps = add_simps fnames post sort_cont | 
| 33099 
b8cdd3d73022
function package: more standard names for structures and files
 krauss parents: diff
changeset | 109 | |
| 34232 
36a2a3029fd3
new year's resolution: reindented code in function package
 krauss parents: 
34231diff
changeset | 110 | val (((psimps', pinducts'), (_, [termination'])), lthy) = | 
| 
36a2a3029fd3
new year's resolution: reindented code in function package
 krauss parents: 
34231diff
changeset | 111 | lthy | 
| 
36a2a3029fd3
new year's resolution: reindented code in function package
 krauss parents: 
34231diff
changeset | 112 | |> addsmps (conceal_partial o Binding.qualify false "partial") | 
| 
36a2a3029fd3
new year's resolution: reindented code in function package
 krauss parents: 
34231diff
changeset | 113 | "psimps" conceal_partial psimp_attribs psimps | 
| 
36a2a3029fd3
new year's resolution: reindented code in function package
 krauss parents: 
34231diff
changeset | 114 | ||>> Local_Theory.note ((conceal_partial (qualify "pinduct"), | 
| 
36a2a3029fd3
new year's resolution: reindented code in function package
 krauss parents: 
34231diff
changeset | 115 | [Attrib.internal (K (Rule_Cases.case_names cnames)), | 
| 
36a2a3029fd3
new year's resolution: reindented code in function package
 krauss parents: 
34231diff
changeset | 116 | Attrib.internal (K (Rule_Cases.consumes 1)), | 
| 
36a2a3029fd3
new year's resolution: reindented code in function package
 krauss parents: 
34231diff
changeset | 117 | Attrib.internal (K (Induct.induct_pred ""))]), simple_pinducts) | 
| 
36a2a3029fd3
new year's resolution: reindented code in function package
 krauss parents: 
34231diff
changeset | 118 | ||>> Local_Theory.note ((Binding.conceal (qualify "termination"), []), [termination]) | 
| 
36a2a3029fd3
new year's resolution: reindented code in function package
 krauss parents: 
34231diff
changeset | 119 | ||> (snd o Local_Theory.note ((qualify "cases", | 
| 
36a2a3029fd3
new year's resolution: reindented code in function package
 krauss parents: 
34231diff
changeset | 120 | [Attrib.internal (K (Rule_Cases.case_names cnames))]), [cases])) | 
| 40076 | 121 | ||> (case domintros of NONE => I | SOME thms => | 
| 122 | Local_Theory.note ((qualify "domintros", []), thms) #> snd) | |
| 33099 
b8cdd3d73022
function package: more standard names for structures and files
 krauss parents: diff
changeset | 123 | |
| 34232 
36a2a3029fd3
new year's resolution: reindented code in function package
 krauss parents: 
34231diff
changeset | 124 |         val info = { add_simps=addsmps, case_names=cnames, psimps=psimps',
 | 
| 
36a2a3029fd3
new year's resolution: reindented code in function package
 krauss parents: 
34231diff
changeset | 125 | pinducts=snd pinducts', simps=NONE, inducts=NONE, termination=termination', | 
| 
36a2a3029fd3
new year's resolution: reindented code in function package
 krauss parents: 
34231diff
changeset | 126 | fs=fs, R=R, defname=defname, is_partial=true } | 
| 34230 | 127 | |
| 44239 
47ecd30e018d
less verbosity for 'function' and 'fun': observe "int" flag more carefully (cf. a32ca9165928);
 wenzelm parents: 
44192diff
changeset | 128 | val _ = Proof_Display.print_consts do_print lthy (K false) (map fst fixes) | 
| 34232 
36a2a3029fd3
new year's resolution: reindented code in function package
 krauss parents: 
34231diff
changeset | 129 | in | 
| 44239 
47ecd30e018d
less verbosity for 'function' and 'fun': observe "int" flag more carefully (cf. a32ca9165928);
 wenzelm parents: 
44192diff
changeset | 130 | (info, | 
| 36522 
e80a95279ef6
return info record (relative to auxiliary context!)
 krauss parents: 
36520diff
changeset | 131 | lthy |> Local_Theory.declaration false (add_function_data o morph_function_data info)) | 
| 34232 
36a2a3029fd3
new year's resolution: reindented code in function package
 krauss parents: 
34231diff
changeset | 132 | end | 
| 36520 | 133 | in | 
| 36519 
46bf776a81e0
ML interface uses plain command names, following conventions from typedef
 krauss parents: 
36518diff
changeset | 134 | ((goal_state, afterqed), lthy') | 
| 36518 
a33b986f2e22
function: better separate Isar integration from actual functionality
 krauss parents: 
36323diff
changeset | 135 | end | 
| 
a33b986f2e22
function: better separate Isar integration from actual functionality
 krauss parents: 
36323diff
changeset | 136 | |
| 44239 
47ecd30e018d
less verbosity for 'function' and 'fun': observe "int" flag more carefully (cf. a32ca9165928);
 wenzelm parents: 
44192diff
changeset | 137 | fun gen_add_function do_print prep default_constraint fixspec eqns config tac lthy = | 
| 36520 | 138 | let | 
| 139 | val ((goal_state, afterqed), lthy') = | |
| 44239 
47ecd30e018d
less verbosity for 'function' and 'fun': observe "int" flag more carefully (cf. a32ca9165928);
 wenzelm parents: 
44192diff
changeset | 140 | prepare_function do_print prep default_constraint fixspec eqns config lthy | 
| 36520 | 141 | val pattern_thm = | 
| 142 | case SINGLE (tac lthy') goal_state of | |
| 143 | NONE => error "pattern completeness and compatibility proof failed" | |
| 144 | | SOME st => Goal.finish lthy' st | |
| 145 | in | |
| 146 | lthy' | |
| 147 | |> afterqed [[pattern_thm]] | |
| 148 | end | |
| 149 | ||
| 150 | val add_function = | |
| 37145 
01aa36932739
renamed structure TypeInfer to Type_Infer, keeping the old name as legacy alias for some time;
 wenzelm parents: 
36960diff
changeset | 151 | gen_add_function false Specification.check_spec (Type_Infer.anyT HOLogic.typeS) | 
| 44239 
47ecd30e018d
less verbosity for 'function' and 'fun': observe "int" flag more carefully (cf. a32ca9165928);
 wenzelm parents: 
44192diff
changeset | 152 | fun add_function_cmd a b c d int = gen_add_function int Specification.read_spec "_::type" a b c d | 
| 36520 | 153 | |
| 44239 
47ecd30e018d
less verbosity for 'function' and 'fun': observe "int" flag more carefully (cf. a32ca9165928);
 wenzelm parents: 
44192diff
changeset | 154 | fun gen_function do_print prep default_constraint fixspec eqns config lthy = | 
| 36518 
a33b986f2e22
function: better separate Isar integration from actual functionality
 krauss parents: 
36323diff
changeset | 155 | let | 
| 36520 | 156 | val ((goal_state, afterqed), lthy') = | 
| 44239 
47ecd30e018d
less verbosity for 'function' and 'fun': observe "int" flag more carefully (cf. a32ca9165928);
 wenzelm parents: 
44192diff
changeset | 157 | prepare_function do_print prep default_constraint fixspec eqns config lthy | 
| 34232 
36a2a3029fd3
new year's resolution: reindented code in function package
 krauss parents: 
34231diff
changeset | 158 | in | 
| 36518 
a33b986f2e22
function: better separate Isar integration from actual functionality
 krauss parents: 
36323diff
changeset | 159 | lthy' | 
| 36522 
e80a95279ef6
return info record (relative to auxiliary context!)
 krauss parents: 
36520diff
changeset | 160 | |> Proof.theorem NONE (snd oo afterqed) [[(Logic.unprotect (concl_of goal_state), [])]] | 
| 36519 
46bf776a81e0
ML interface uses plain command names, following conventions from typedef
 krauss parents: 
36518diff
changeset | 161 | |> Proof.refine (Method.primitive_text (K goal_state)) |> Seq.hd | 
| 34232 
36a2a3029fd3
new year's resolution: reindented code in function package
 krauss parents: 
34231diff
changeset | 162 | end | 
| 33099 
b8cdd3d73022
function package: more standard names for structures and files
 krauss parents: diff
changeset | 163 | |
| 36519 
46bf776a81e0
ML interface uses plain command names, following conventions from typedef
 krauss parents: 
36518diff
changeset | 164 | val function = | 
| 37145 
01aa36932739
renamed structure TypeInfer to Type_Infer, keeping the old name as legacy alias for some time;
 wenzelm parents: 
36960diff
changeset | 165 | gen_function false Specification.check_spec (Type_Infer.anyT HOLogic.typeS) | 
| 44239 
47ecd30e018d
less verbosity for 'function' and 'fun': observe "int" flag more carefully (cf. a32ca9165928);
 wenzelm parents: 
44192diff
changeset | 166 | fun function_cmd a b c int = gen_function int Specification.read_spec "_::type" a b c | 
| 36520 | 167 | |
| 36518 
a33b986f2e22
function: better separate Isar integration from actual functionality
 krauss parents: 
36323diff
changeset | 168 | fun prepare_termination_proof prep_term raw_term_opt lthy = | 
| 34232 
36a2a3029fd3
new year's resolution: reindented code in function package
 krauss parents: 
34231diff
changeset | 169 | let | 
| 
36a2a3029fd3
new year's resolution: reindented code in function package
 krauss parents: 
34231diff
changeset | 170 | val term_opt = Option.map (prep_term lthy) raw_term_opt | 
| 
36a2a3029fd3
new year's resolution: reindented code in function package
 krauss parents: 
34231diff
changeset | 171 | val info = the (case term_opt of | 
| 
36a2a3029fd3
new year's resolution: reindented code in function package
 krauss parents: 
34231diff
changeset | 172 | SOME t => (import_function_data t lthy | 
| 
36a2a3029fd3
new year's resolution: reindented code in function package
 krauss parents: 
34231diff
changeset | 173 | handle Option.Option => | 
| 
36a2a3029fd3
new year's resolution: reindented code in function package
 krauss parents: 
34231diff
changeset | 174 |                           error ("Not a function: " ^ quote (Syntax.string_of_term lthy t)))
 | 
| 
36a2a3029fd3
new year's resolution: reindented code in function package
 krauss parents: 
34231diff
changeset | 175 | | NONE => (import_last_function lthy handle Option.Option => error "Not a function")) | 
| 33099 
b8cdd3d73022
function package: more standard names for structures and files
 krauss parents: diff
changeset | 176 | |
| 34232 
36a2a3029fd3
new year's resolution: reindented code in function package
 krauss parents: 
34231diff
changeset | 177 |       val { termination, fs, R, add_simps, case_names, psimps,
 | 
| 
36a2a3029fd3
new year's resolution: reindented code in function package
 krauss parents: 
34231diff
changeset | 178 | pinducts, defname, ...} = info | 
| 
36a2a3029fd3
new year's resolution: reindented code in function package
 krauss parents: 
34231diff
changeset | 179 | val domT = domain_type (fastype_of R) | 
| 
36a2a3029fd3
new year's resolution: reindented code in function package
 krauss parents: 
34231diff
changeset | 180 | val goal = HOLogic.mk_Trueprop | 
| 
36a2a3029fd3
new year's resolution: reindented code in function package
 krauss parents: 
34231diff
changeset | 181 |                    (HOLogic.mk_all ("x", domT, mk_acc domT R $ Free ("x", domT)))
 | 
| 
36a2a3029fd3
new year's resolution: reindented code in function package
 krauss parents: 
34231diff
changeset | 182 | fun afterqed [[totality]] lthy = | 
| 
36a2a3029fd3
new year's resolution: reindented code in function package
 krauss parents: 
34231diff
changeset | 183 | let | 
| 
36a2a3029fd3
new year's resolution: reindented code in function package
 krauss parents: 
34231diff
changeset | 184 | val totality = Thm.close_derivation totality | 
| 
36a2a3029fd3
new year's resolution: reindented code in function package
 krauss parents: 
34231diff
changeset | 185 | val remove_domain_condition = | 
| 35410 | 186 |             full_simplify (HOL_basic_ss addsimps [totality, @{thm True_implies_equals}])
 | 
| 34232 
36a2a3029fd3
new year's resolution: reindented code in function package
 krauss parents: 
34231diff
changeset | 187 | val tsimps = map remove_domain_condition psimps | 
| 
36a2a3029fd3
new year's resolution: reindented code in function package
 krauss parents: 
34231diff
changeset | 188 | val tinduct = map remove_domain_condition pinducts | 
| 34230 | 189 | |
| 34232 
36a2a3029fd3
new year's resolution: reindented code in function package
 krauss parents: 
34231diff
changeset | 190 | fun qualify n = Binding.name n | 
| 
36a2a3029fd3
new year's resolution: reindented code in function package
 krauss parents: 
34231diff
changeset | 191 | |> Binding.qualify true defname | 
| 
36a2a3029fd3
new year's resolution: reindented code in function package
 krauss parents: 
34231diff
changeset | 192 | in | 
| 
36a2a3029fd3
new year's resolution: reindented code in function package
 krauss parents: 
34231diff
changeset | 193 | lthy | 
| 
36a2a3029fd3
new year's resolution: reindented code in function package
 krauss parents: 
34231diff
changeset | 194 | |> add_simps I "simps" I simp_attribs tsimps | 
| 
36a2a3029fd3
new year's resolution: reindented code in function package
 krauss parents: 
34231diff
changeset | 195 | ||>> Local_Theory.note | 
| 
36a2a3029fd3
new year's resolution: reindented code in function package
 krauss parents: 
34231diff
changeset | 196 | ((qualify "induct", | 
| 
36a2a3029fd3
new year's resolution: reindented code in function package
 krauss parents: 
34231diff
changeset | 197 | [Attrib.internal (K (Rule_Cases.case_names case_names))]), | 
| 
36a2a3029fd3
new year's resolution: reindented code in function package
 krauss parents: 
34231diff
changeset | 198 | tinduct) | 
| 36547 
2a9d0ec8c10d
return updated info record after termination proof
 krauss parents: 
36522diff
changeset | 199 | |-> (fn (simps, (_, inducts)) => fn lthy => | 
| 34232 
36a2a3029fd3
new year's resolution: reindented code in function package
 krauss parents: 
34231diff
changeset | 200 |             let val info' = { is_partial=false, defname=defname, add_simps=add_simps,
 | 
| 
36a2a3029fd3
new year's resolution: reindented code in function package
 krauss parents: 
34231diff
changeset | 201 | case_names=case_names, fs=fs, R=R, psimps=psimps, pinducts=pinducts, | 
| 
36a2a3029fd3
new year's resolution: reindented code in function package
 krauss parents: 
34231diff
changeset | 202 | simps=SOME simps, inducts=SOME inducts, termination=termination } | 
| 
36a2a3029fd3
new year's resolution: reindented code in function package
 krauss parents: 
34231diff
changeset | 203 | in | 
| 36547 
2a9d0ec8c10d
return updated info record after termination proof
 krauss parents: 
36522diff
changeset | 204 | (info', | 
| 
2a9d0ec8c10d
return updated info record after termination proof
 krauss parents: 
36522diff
changeset | 205 | lthy | 
| 
2a9d0ec8c10d
return updated info record after termination proof
 krauss parents: 
36522diff
changeset | 206 | |> Local_Theory.declaration false (add_function_data o morph_function_data info') | 
| 
2a9d0ec8c10d
return updated info record after termination proof
 krauss parents: 
36522diff
changeset | 207 | |> Spec_Rules.add Spec_Rules.Equational (fs, tsimps)) | 
| 34232 
36a2a3029fd3
new year's resolution: reindented code in function package
 krauss parents: 
34231diff
changeset | 208 | end) | 
| 
36a2a3029fd3
new year's resolution: reindented code in function package
 krauss parents: 
34231diff
changeset | 209 | end | 
| 
36a2a3029fd3
new year's resolution: reindented code in function package
 krauss parents: 
34231diff
changeset | 210 | in | 
| 36518 
a33b986f2e22
function: better separate Isar integration from actual functionality
 krauss parents: 
36323diff
changeset | 211 | (goal, afterqed, termination) | 
| 
a33b986f2e22
function: better separate Isar integration from actual functionality
 krauss parents: 
36323diff
changeset | 212 | end | 
| 
a33b986f2e22
function: better separate Isar integration from actual functionality
 krauss parents: 
36323diff
changeset | 213 | |
| 36520 | 214 | fun gen_prove_termination prep_term raw_term_opt tac lthy = | 
| 215 | let | |
| 216 | val (goal, afterqed, termination) = | |
| 217 | prepare_termination_proof prep_term raw_term_opt lthy | |
| 218 | ||
| 219 | val totality = Goal.prove lthy [] [] goal (K tac) | |
| 220 | in | |
| 221 | afterqed [[totality]] lthy | |
| 222 | end | |
| 223 | ||
| 224 | val prove_termination = gen_prove_termination Syntax.check_term | |
| 225 | val prove_termination_cmd = gen_prove_termination Syntax.read_term | |
| 226 | ||
| 36519 
46bf776a81e0
ML interface uses plain command names, following conventions from typedef
 krauss parents: 
36518diff
changeset | 227 | fun gen_termination prep_term raw_term_opt lthy = | 
| 36518 
a33b986f2e22
function: better separate Isar integration from actual functionality
 krauss parents: 
36323diff
changeset | 228 | let | 
| 
a33b986f2e22
function: better separate Isar integration from actual functionality
 krauss parents: 
36323diff
changeset | 229 | val (goal, afterqed, termination) = prepare_termination_proof prep_term raw_term_opt lthy | 
| 
a33b986f2e22
function: better separate Isar integration from actual functionality
 krauss parents: 
36323diff
changeset | 230 | in | 
| 34232 
36a2a3029fd3
new year's resolution: reindented code in function package
 krauss parents: 
34231diff
changeset | 231 | lthy | 
| 42361 | 232 | |> Proof_Context.note_thmss "" | 
| 34232 
36a2a3029fd3
new year's resolution: reindented code in function package
 krauss parents: 
34231diff
changeset | 233 | [((Binding.empty, [Context_Rules.rule_del]), [([allI], [])])] |> snd | 
| 42361 | 234 | |> Proof_Context.note_thmss "" | 
| 34232 
36a2a3029fd3
new year's resolution: reindented code in function package
 krauss parents: 
34231diff
changeset | 235 | [((Binding.empty, [Context_Rules.intro_bang (SOME 1)]), [([allI], [])])] |> snd | 
| 42361 | 236 | |> Proof_Context.note_thmss "" | 
| 34232 
36a2a3029fd3
new year's resolution: reindented code in function package
 krauss parents: 
34231diff
changeset | 237 | [((Binding.name "termination", [Context_Rules.intro_bang (SOME 0)]), | 
| 
36a2a3029fd3
new year's resolution: reindented code in function package
 krauss parents: 
34231diff
changeset | 238 | [([Goal.norm_result termination], [])])] |> snd | 
| 36547 
2a9d0ec8c10d
return updated info record after termination proof
 krauss parents: 
36522diff
changeset | 239 | |> Proof.theorem NONE (snd oo afterqed) [[(goal, [])]] | 
| 34232 
36a2a3029fd3
new year's resolution: reindented code in function package
 krauss parents: 
34231diff
changeset | 240 | end | 
| 33099 
b8cdd3d73022
function package: more standard names for structures and files
 krauss parents: diff
changeset | 241 | |
| 36519 
46bf776a81e0
ML interface uses plain command names, following conventions from typedef
 krauss parents: 
36518diff
changeset | 242 | val termination = gen_termination Syntax.check_term | 
| 
46bf776a81e0
ML interface uses plain command names, following conventions from typedef
 krauss parents: 
36518diff
changeset | 243 | val termination_cmd = gen_termination Syntax.read_term | 
| 33099 
b8cdd3d73022
function package: more standard names for structures and files
 krauss parents: diff
changeset | 244 | |
| 
b8cdd3d73022
function package: more standard names for structures and files
 krauss parents: diff
changeset | 245 | |
| 
b8cdd3d73022
function package: more standard names for structures and files
 krauss parents: diff
changeset | 246 | (* Datatype hook to declare datatype congs as "function_congs" *) | 
| 
b8cdd3d73022
function package: more standard names for structures and files
 krauss parents: diff
changeset | 247 | |
| 
b8cdd3d73022
function package: more standard names for structures and files
 krauss parents: diff
changeset | 248 | |
| 
b8cdd3d73022
function package: more standard names for structures and files
 krauss parents: diff
changeset | 249 | fun add_case_cong n thy = | 
| 34232 
36a2a3029fd3
new year's resolution: reindented code in function package
 krauss parents: 
34231diff
changeset | 250 | let | 
| 
36a2a3029fd3
new year's resolution: reindented code in function package
 krauss parents: 
34231diff
changeset | 251 | val cong = #case_cong (Datatype.the_info thy n) | 
| 
36a2a3029fd3
new year's resolution: reindented code in function package
 krauss parents: 
34231diff
changeset | 252 | |> safe_mk_meta_eq | 
| 
36a2a3029fd3
new year's resolution: reindented code in function package
 krauss parents: 
34231diff
changeset | 253 | in | 
| 
36a2a3029fd3
new year's resolution: reindented code in function package
 krauss parents: 
34231diff
changeset | 254 | Context.theory_map | 
| 
36a2a3029fd3
new year's resolution: reindented code in function package
 krauss parents: 
34231diff
changeset | 255 | (Function_Ctx_Tree.map_function_congs (Thm.add_thm cong)) thy | 
| 
36a2a3029fd3
new year's resolution: reindented code in function package
 krauss parents: 
34231diff
changeset | 256 | end | 
| 33099 
b8cdd3d73022
function package: more standard names for structures and files
 krauss parents: diff
changeset | 257 | |
| 
b8cdd3d73022
function package: more standard names for structures and files
 krauss parents: diff
changeset | 258 | val setup_case_cong = Datatype.interpretation (K (fold add_case_cong)) | 
| 
b8cdd3d73022
function package: more standard names for structures and files
 krauss parents: diff
changeset | 259 | |
| 
b8cdd3d73022
function package: more standard names for structures and files
 krauss parents: diff
changeset | 260 | |
| 
b8cdd3d73022
function package: more standard names for structures and files
 krauss parents: diff
changeset | 261 | (* setup *) | 
| 
b8cdd3d73022
function package: more standard names for structures and files
 krauss parents: diff
changeset | 262 | |
| 
b8cdd3d73022
function package: more standard names for structures and files
 krauss parents: diff
changeset | 263 | val setup = | 
| 
b8cdd3d73022
function package: more standard names for structures and files
 krauss parents: diff
changeset | 264 |   Attrib.setup @{binding fundef_cong}
 | 
| 
b8cdd3d73022
function package: more standard names for structures and files
 krauss parents: diff
changeset | 265 | (Attrib.add_del Function_Ctx_Tree.cong_add Function_Ctx_Tree.cong_del) | 
| 
b8cdd3d73022
function package: more standard names for structures and files
 krauss parents: diff
changeset | 266 | "declaration of congruence rule for function definitions" | 
| 
b8cdd3d73022
function package: more standard names for structures and files
 krauss parents: diff
changeset | 267 | #> setup_case_cong | 
| 
b8cdd3d73022
function package: more standard names for structures and files
 krauss parents: diff
changeset | 268 | #> Function_Relation.setup | 
| 
b8cdd3d73022
function package: more standard names for structures and files
 krauss parents: diff
changeset | 269 | #> Function_Common.Termination_Simps.setup | 
| 
b8cdd3d73022
function package: more standard names for structures and files
 krauss parents: diff
changeset | 270 | |
| 
b8cdd3d73022
function package: more standard names for structures and files
 krauss parents: diff
changeset | 271 | val get_congs = Function_Ctx_Tree.get_function_congs | 
| 
b8cdd3d73022
function package: more standard names for structures and files
 krauss parents: diff
changeset | 272 | |
| 34230 | 273 | fun get_info ctxt t = Item_Net.retrieve (get_function ctxt) t | 
| 274 | |> the_single |> snd | |
| 33099 
b8cdd3d73022
function package: more standard names for structures and files
 krauss parents: diff
changeset | 275 | |
| 36960 
01594f816e3a
prefer structure Keyword, Parse, Parse_Spec, Outer_Syntax;
 wenzelm parents: 
36547diff
changeset | 276 | |
| 33099 
b8cdd3d73022
function package: more standard names for structures and files
 krauss parents: diff
changeset | 277 | (* outer syntax *) | 
| 
b8cdd3d73022
function package: more standard names for structures and files
 krauss parents: diff
changeset | 278 | |
| 
b8cdd3d73022
function package: more standard names for structures and files
 krauss parents: diff
changeset | 279 | val _ = | 
| 44239 
47ecd30e018d
less verbosity for 'function' and 'fun': observe "int" flag more carefully (cf. a32ca9165928);
 wenzelm parents: 
44192diff
changeset | 280 | Outer_Syntax.local_theory_to_proof' "function" "define general recursive functions" | 
| 36960 
01594f816e3a
prefer structure Keyword, Parse, Parse_Spec, Outer_Syntax;
 wenzelm parents: 
36547diff
changeset | 281 | Keyword.thy_goal | 
| 33099 
b8cdd3d73022
function package: more standard names for structures and files
 krauss parents: diff
changeset | 282 | (function_parser default_config | 
| 36519 
46bf776a81e0
ML interface uses plain command names, following conventions from typedef
 krauss parents: 
36518diff
changeset | 283 | >> (fn ((config, fixes), statements) => function_cmd fixes statements config)) | 
| 33099 
b8cdd3d73022
function package: more standard names for structures and files
 krauss parents: diff
changeset | 284 | |
| 
b8cdd3d73022
function package: more standard names for structures and files
 krauss parents: diff
changeset | 285 | val _ = | 
| 36960 
01594f816e3a
prefer structure Keyword, Parse, Parse_Spec, Outer_Syntax;
 wenzelm parents: 
36547diff
changeset | 286 | Outer_Syntax.local_theory_to_proof "termination" "prove termination of a recursive function" | 
| 
01594f816e3a
prefer structure Keyword, Parse, Parse_Spec, Outer_Syntax;
 wenzelm parents: 
36547diff
changeset | 287 | Keyword.thy_goal | 
| 
01594f816e3a
prefer structure Keyword, Parse, Parse_Spec, Outer_Syntax;
 wenzelm parents: 
36547diff
changeset | 288 | (Scan.option Parse.term >> termination_cmd) | 
| 33099 
b8cdd3d73022
function package: more standard names for structures and files
 krauss parents: diff
changeset | 289 | |
| 
b8cdd3d73022
function package: more standard names for structures and files
 krauss parents: diff
changeset | 290 | |
| 
b8cdd3d73022
function package: more standard names for structures and files
 krauss parents: diff
changeset | 291 | end |