author | krauss |
Tue, 16 Oct 2007 14:11:06 +0200 | |
changeset 25045 | 12386aefe9ac |
parent 24977 | 9f98751c9628 |
child 25088 | 9a13ab12b174 |
permissions | -rw-r--r-- |
19564
d3e2f532459a
First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff
changeset
|
1 |
(* Title: HOL/Tools/function_package/fundef_package.ML |
d3e2f532459a
First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff
changeset
|
2 |
ID: $Id$ |
d3e2f532459a
First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff
changeset
|
3 |
Author: Alexander Krauss, TU Muenchen |
d3e2f532459a
First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff
changeset
|
4 |
|
20363
f34c5dbe74d5
global goals/qeds: after_qed operates on Proof.context (potentially local_theory);
wenzelm
parents:
20338
diff
changeset
|
5 |
A package for general recursive function definitions. |
19564
d3e2f532459a
First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff
changeset
|
6 |
Isar commands. |
d3e2f532459a
First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff
changeset
|
7 |
|
d3e2f532459a
First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff
changeset
|
8 |
*) |
d3e2f532459a
First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff
changeset
|
9 |
|
20363
f34c5dbe74d5
global goals/qeds: after_qed operates on Proof.context (potentially local_theory);
wenzelm
parents:
20338
diff
changeset
|
10 |
signature FUNDEF_PACKAGE = |
19564
d3e2f532459a
First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff
changeset
|
11 |
sig |
20877
368b997ad67e
removed with_local_path -- LocalTheory.note admits qualified names;
wenzelm
parents:
20654
diff
changeset
|
12 |
val add_fundef : (string * string option * mixfix) list |
23203
a5026e73cfcf
"function (sequential)" and "fun" now handle incomplete patterns silently by adding "undefined" cases.
krauss
parents:
23189
diff
changeset
|
13 |
-> ((bstring * Attrib.src list) * string) list |
20877
368b997ad67e
removed with_local_path -- LocalTheory.note admits qualified names;
wenzelm
parents:
20654
diff
changeset
|
14 |
-> FundefCommon.fundef_config |
23203
a5026e73cfcf
"function (sequential)" and "fun" now handle incomplete patterns silently by adding "undefined" cases.
krauss
parents:
23189
diff
changeset
|
15 |
-> bool list |
20877
368b997ad67e
removed with_local_path -- LocalTheory.note admits qualified names;
wenzelm
parents:
20654
diff
changeset
|
16 |
-> local_theory |
22733
0b14bb35be90
definition lookup via terms, not names. Methods "relation" and "lexicographic_order"
krauss
parents:
22725
diff
changeset
|
17 |
-> Proof.state |
20523
36a59e5d0039
Major update to function package, including new syntax and the (only theoretical)
krauss
parents:
20363
diff
changeset
|
18 |
|
20877
368b997ad67e
removed with_local_path -- LocalTheory.note admits qualified names;
wenzelm
parents:
20654
diff
changeset
|
19 |
val add_fundef_i: (string * typ option * mixfix) list |
23203
a5026e73cfcf
"function (sequential)" and "fun" now handle incomplete patterns silently by adding "undefined" cases.
krauss
parents:
23189
diff
changeset
|
20 |
-> ((bstring * Attrib.src list) * term) list |
22170 | 21 |
-> FundefCommon.fundef_config |
23203
a5026e73cfcf
"function (sequential)" and "fun" now handle incomplete patterns silently by adding "undefined" cases.
krauss
parents:
23189
diff
changeset
|
22 |
-> bool list |
20877
368b997ad67e
removed with_local_path -- LocalTheory.note admits qualified names;
wenzelm
parents:
20654
diff
changeset
|
23 |
-> local_theory |
22733
0b14bb35be90
definition lookup via terms, not names. Methods "relation" and "lexicographic_order"
krauss
parents:
22725
diff
changeset
|
24 |
-> Proof.state |
21211
5370cfbf3070
Preparations for making "lexicographic_order" part of "fun"
krauss
parents:
21051
diff
changeset
|
25 |
|
5370cfbf3070
Preparations for making "lexicographic_order" part of "fun"
krauss
parents:
21051
diff
changeset
|
26 |
val setup_termination_proof : string option -> local_theory -> Proof.state |
19564
d3e2f532459a
First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff
changeset
|
27 |
|
d3e2f532459a
First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff
changeset
|
28 |
val setup : theory -> theory |
19770
be5c23ebe1eb
HOL/Tools/function_package: Added support for mutual recursive definitions.
krauss
parents:
19617
diff
changeset
|
29 |
val get_congs : theory -> thm list |
19564
d3e2f532459a
First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff
changeset
|
30 |
end |
d3e2f532459a
First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff
changeset
|
31 |
|
d3e2f532459a
First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff
changeset
|
32 |
|
23203
a5026e73cfcf
"function (sequential)" and "fun" now handle incomplete patterns silently by adding "undefined" cases.
krauss
parents:
23189
diff
changeset
|
33 |
structure FundefPackage : FUNDEF_PACKAGE = |
19564
d3e2f532459a
First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff
changeset
|
34 |
struct |
d3e2f532459a
First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff
changeset
|
35 |
|
21051
c49467a9c1e1
Switched function package to use the new package for inductive predicates.
krauss
parents:
21000
diff
changeset
|
36 |
open FundefLib |
19564
d3e2f532459a
First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff
changeset
|
37 |
open FundefCommon |
d3e2f532459a
First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff
changeset
|
38 |
|
21435 | 39 |
val note_theorem = LocalTheory.note Thm.theoremK |
40 |
||
22166 | 41 |
fun mk_defname fixes = fixes |> map (fst o fst) |> space_implode "_" |
42 |
||
23819 | 43 |
fun add_simps fnames post sort label moreatts simps lthy = |
19564
d3e2f532459a
First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff
changeset
|
44 |
let |
23819 | 45 |
val atts = Attrib.internal (K Simplifier.simp_add) :: moreatts |
46 |
val spec = post simps |
|
47 |
|> map (apfst (apsnd (append atts))) |
|
21255
617fdb08abe9
added profiling code, improved handling of proof terms, generation of domain
krauss
parents:
21240
diff
changeset
|
48 |
|
22166 | 49 |
val (saved_spec_simps, lthy) = |
23819 | 50 |
fold_map note_theorem spec lthy |
51 |
||
22498
62cdd4b3e96b
made function syntax strict, requiring | to separate equations; cleanup
krauss
parents:
22325
diff
changeset
|
52 |
val saved_simps = flat (map snd saved_spec_simps) |
22166 | 53 |
val simps_by_f = sort saved_simps |
19564
d3e2f532459a
First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff
changeset
|
54 |
|
22166 | 55 |
fun add_for_f fname simps = |
23819 | 56 |
note_theorem ((NameSpace.qualified fname label, []), simps) #> snd |
19564
d3e2f532459a
First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff
changeset
|
57 |
in |
22166 | 58 |
(saved_simps, |
59 |
fold2 add_for_f fnames simps_by_f lthy) |
|
19564
d3e2f532459a
First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff
changeset
|
60 |
end |
d3e2f532459a
First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff
changeset
|
61 |
|
23203
a5026e73cfcf
"function (sequential)" and "fun" now handle incomplete patterns silently by adding "undefined" cases.
krauss
parents:
23189
diff
changeset
|
62 |
fun fundef_afterqed config fixes post defname cont sort_cont [[proof]] lthy = |
20523
36a59e5d0039
Major update to function package, including new syntax and the (only theoretical)
krauss
parents:
20363
diff
changeset
|
63 |
let |
22733
0b14bb35be90
definition lookup via terms, not names. Methods "relation" and "lexicographic_order"
krauss
parents:
22725
diff
changeset
|
64 |
val FundefResult {fs, R, psimps, trsimps, subset_pinducts, simple_pinducts, termination, domintros, cases, ...} = |
22166 | 65 |
cont (Goal.close_result proof) |
66 |
||
23819 | 67 |
val fnames = map (fst o fst) fixes |
21391
a8809f46bd7f
replaced NameSpace.append by NameSpace.qualified, which handles empty names as expected;
wenzelm
parents:
21359
diff
changeset
|
68 |
val qualify = NameSpace.qualified defname |
23819 | 69 |
val addsmps = add_simps fnames post sort_cont |
21602 | 70 |
|
71 |
val (((psimps', pinducts'), (_, [termination'])), lthy) = |
|
21255
617fdb08abe9
added profiling code, improved handling of proof terms, generation of domain
krauss
parents:
21240
diff
changeset
|
72 |
lthy |
22170 | 73 |
|> addsmps "psimps" [] psimps |
74 |
||> fold_option (snd oo addsmps "simps" []) trsimps |
|
22166 | 75 |
||>> note_theorem ((qualify "pinduct", |
24861
cc669ca5f382
tuned Induct interface: prefer pred'' over set'';
wenzelm
parents:
24830
diff
changeset
|
76 |
[Attrib.internal (K (Induct.induct_pred ""))]), simple_pinducts) |
22166 | 77 |
||>> note_theorem ((qualify "termination", []), [termination]) |
78 |
||> (snd o note_theorem ((qualify "cases", []), [cases])) |
|
79 |
||> fold_option (snd oo curry note_theorem (qualify "domintros", [])) domintros |
|
21255
617fdb08abe9
added profiling code, improved handling of proof terms, generation of domain
krauss
parents:
21240
diff
changeset
|
80 |
|
22170 | 81 |
val cdata = FundefCtxData { add_simps=addsmps, psimps=psimps', |
22733
0b14bb35be90
definition lookup via terms, not names. Methods "relation" and "lexicographic_order"
krauss
parents:
22725
diff
changeset
|
82 |
pinducts=snd pinducts', termination=termination', fs=fs, R=R, defname=defname } |
22668
8183ee579232
data declaration: removed obsolete target_morphism (still required for local data!?);
wenzelm
parents:
22635
diff
changeset
|
83 |
val cdata' = cdata |> morph_fundef_data (LocalTheory.target_morphism lthy); (* FIXME !? *) |
20523
36a59e5d0039
Major update to function package, including new syntax and the (only theoretical)
krauss
parents:
20363
diff
changeset
|
84 |
in |
22623 | 85 |
lthy |
22733
0b14bb35be90
definition lookup via terms, not names. Methods "relation" and "lexicographic_order"
krauss
parents:
22725
diff
changeset
|
86 |
|> LocalTheory.declaration (fn phi => add_fundef_data (morph_fundef_data phi cdata)) (* save in target *) |
0b14bb35be90
definition lookup via terms, not names. Methods "relation" and "lexicographic_order"
krauss
parents:
22725
diff
changeset
|
87 |
|> Context.proof_map (add_fundef_data cdata') (* also save in local context *) |
20523
36a59e5d0039
Major update to function package, including new syntax and the (only theoretical)
krauss
parents:
20363
diff
changeset
|
88 |
end (* FIXME: Add cases for induct and cases thm *) |
36a59e5d0039
Major update to function package, including new syntax and the (only theoretical)
krauss
parents:
20363
diff
changeset
|
89 |
|
36a59e5d0039
Major update to function package, including new syntax and the (only theoretical)
krauss
parents:
20363
diff
changeset
|
90 |
|
24722
59fd5cceccd7
minimal adaptions for Specification.read/check_specification;
wenzelm
parents:
24711
diff
changeset
|
91 |
fun prepare_spec prep fixspec eqnss lthy = (* FIXME: obsolete; use Specification.read/check_specification *) |
19564
d3e2f532459a
First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff
changeset
|
92 |
let |
23203
a5026e73cfcf
"function (sequential)" and "fun" now handle incomplete patterns silently by adding "undefined" cases.
krauss
parents:
23189
diff
changeset
|
93 |
val eqns = map (apsnd single) eqnss |
22498
62cdd4b3e96b
made function syntax strict, requiring | to separate equations; cleanup
krauss
parents:
22325
diff
changeset
|
94 |
|
23203
a5026e73cfcf
"function (sequential)" and "fun" now handle incomplete patterns silently by adding "undefined" cases.
krauss
parents:
23189
diff
changeset
|
95 |
val ((fixes, _), ctxt') = prep fixspec [] lthy |
24722
59fd5cceccd7
minimal adaptions for Specification.read/check_specification;
wenzelm
parents:
24711
diff
changeset
|
96 |
fun prep_eqn e = the_single (snd (fst (prep [] [[e]] ctxt'))) |
23189 | 97 |
|
23203
a5026e73cfcf
"function (sequential)" and "fun" now handle incomplete patterns silently by adding "undefined" cases.
krauss
parents:
23189
diff
changeset
|
98 |
val spec = map prep_eqn eqns |
a5026e73cfcf
"function (sequential)" and "fun" now handle incomplete patterns silently by adding "undefined" cases.
krauss
parents:
23189
diff
changeset
|
99 |
|> map (apsnd (map (fn t => fold_rev (mk_forall o Free) (frees_in_term ctxt' t) t))) (* Add quantifiers *) |
20523
36a59e5d0039
Major update to function package, including new syntax and the (only theoretical)
krauss
parents:
20363
diff
changeset
|
100 |
in |
36a59e5d0039
Major update to function package, including new syntax and the (only theoretical)
krauss
parents:
20363
diff
changeset
|
101 |
((fixes, spec), ctxt') |
36a59e5d0039
Major update to function package, including new syntax and the (only theoretical)
krauss
parents:
20363
diff
changeset
|
102 |
end |
36a59e5d0039
Major update to function package, including new syntax and the (only theoretical)
krauss
parents:
20363
diff
changeset
|
103 |
|
23203
a5026e73cfcf
"function (sequential)" and "fun" now handle incomplete patterns silently by adding "undefined" cases.
krauss
parents:
23189
diff
changeset
|
104 |
fun gen_add_fundef prep fixspec eqnss config flags lthy = |
20523
36a59e5d0039
Major update to function package, including new syntax and the (only theoretical)
krauss
parents:
20363
diff
changeset
|
105 |
let |
23203
a5026e73cfcf
"function (sequential)" and "fun" now handle incomplete patterns silently by adding "undefined" cases.
krauss
parents:
23189
diff
changeset
|
106 |
val ((fixes, spec), ctxt') = prepare_spec prep fixspec eqnss lthy |
23819 | 107 |
val (eqs, post, sort_cont) = FundefCommon.get_preproc lthy config flags ctxt' fixes spec |
22166 | 108 |
|
109 |
val defname = mk_defname fixes |
|
110 |
||
23819 | 111 |
val ((goalstate, cont), lthy) = |
23203
a5026e73cfcf
"function (sequential)" and "fun" now handle incomplete patterns silently by adding "undefined" cases.
krauss
parents:
23189
diff
changeset
|
112 |
FundefMutual.prepare_fundef_mutual config defname fixes eqs lthy |
20523
36a59e5d0039
Major update to function package, including new syntax and the (only theoretical)
krauss
parents:
20363
diff
changeset
|
113 |
|
23203
a5026e73cfcf
"function (sequential)" and "fun" now handle incomplete patterns silently by adding "undefined" cases.
krauss
parents:
23189
diff
changeset
|
114 |
val afterqed = fundef_afterqed config fixes post defname cont sort_cont |
20523
36a59e5d0039
Major update to function package, including new syntax and the (only theoretical)
krauss
parents:
20363
diff
changeset
|
115 |
in |
22733
0b14bb35be90
definition lookup via terms, not names. Methods "relation" and "lexicographic_order"
krauss
parents:
22725
diff
changeset
|
116 |
lthy |
0b14bb35be90
definition lookup via terms, not names. Methods "relation" and "lexicographic_order"
krauss
parents:
22725
diff
changeset
|
117 |
|> Proof.theorem_i NONE afterqed [[(Logic.unprotect (concl_of goalstate), [])]] |
0b14bb35be90
definition lookup via terms, not names. Methods "relation" and "lexicographic_order"
krauss
parents:
22725
diff
changeset
|
118 |
|> Proof.refine (Method.primitive_text (fn _ => goalstate)) |> Seq.hd |
20523
36a59e5d0039
Major update to function package, including new syntax and the (only theoretical)
krauss
parents:
20363
diff
changeset
|
119 |
end |
36a59e5d0039
Major update to function package, including new syntax and the (only theoretical)
krauss
parents:
20363
diff
changeset
|
120 |
|
22733
0b14bb35be90
definition lookup via terms, not names. Methods "relation" and "lexicographic_order"
krauss
parents:
22725
diff
changeset
|
121 |
fun total_termination_afterqed data [[totality]] lthy = |
20523
36a59e5d0039
Major update to function package, including new syntax and the (only theoretical)
krauss
parents:
20363
diff
changeset
|
122 |
let |
22733
0b14bb35be90
definition lookup via terms, not names. Methods "relation" and "lexicographic_order"
krauss
parents:
22725
diff
changeset
|
123 |
val FundefCtxData { add_simps, psimps, pinducts, defname, ... } = data |
21255
617fdb08abe9
added profiling code, improved handling of proof terms, generation of domain
krauss
parents:
21240
diff
changeset
|
124 |
|
22166 | 125 |
val totality = Goal.close_result totality |
19564
d3e2f532459a
First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff
changeset
|
126 |
|
21255
617fdb08abe9
added profiling code, improved handling of proof terms, generation of domain
krauss
parents:
21240
diff
changeset
|
127 |
val remove_domain_condition = full_simplify (HOL_basic_ss addsimps [totality, True_implies_equals]) |
19564
d3e2f532459a
First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff
changeset
|
128 |
|
22166 | 129 |
val tsimps = map remove_domain_condition psimps |
130 |
val tinduct = map remove_domain_condition pinducts |
|
19770
be5c23ebe1eb
HOL/Tools/function_package: Added support for mutual recursive definitions.
krauss
parents:
19617
diff
changeset
|
131 |
|
21511
16c62deb1adf
The function package declares the [code] attribute automatically again.
krauss
parents:
21491
diff
changeset
|
132 |
val has_guards = exists ((fn (Const ("Trueprop", _) $ _) => false | _ => true) o prop_of) tsimps |
24624
b8383b1bbae3
distinction between regular and default code theorems
haftmann
parents:
24508
diff
changeset
|
133 |
val allatts = if has_guards then [] else [Attrib.internal (K RecfunCodegen.add_default)] |
21602 | 134 |
|
21511
16c62deb1adf
The function package declares the [code] attribute automatically again.
krauss
parents:
21491
diff
changeset
|
135 |
val qualify = NameSpace.qualified defname; |
19564
d3e2f532459a
First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff
changeset
|
136 |
in |
21511
16c62deb1adf
The function package declares the [code] attribute automatically again.
krauss
parents:
21491
diff
changeset
|
137 |
lthy |
22166 | 138 |
|> add_simps "simps" allatts tsimps |> snd |
139 |
|> note_theorem ((qualify "induct", []), tinduct) |> snd |
|
20363
f34c5dbe74d5
global goals/qeds: after_qed operates on Proof.context (potentially local_theory);
wenzelm
parents:
20338
diff
changeset
|
140 |
end |
19564
d3e2f532459a
First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff
changeset
|
141 |
|
d3e2f532459a
First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff
changeset
|
142 |
|
22733
0b14bb35be90
definition lookup via terms, not names. Methods "relation" and "lexicographic_order"
krauss
parents:
22725
diff
changeset
|
143 |
fun setup_termination_proof term_opt lthy = |
20523
36a59e5d0039
Major update to function package, including new syntax and the (only theoretical)
krauss
parents:
20363
diff
changeset
|
144 |
let |
22733
0b14bb35be90
definition lookup via terms, not names. Methods "relation" and "lexicographic_order"
krauss
parents:
22725
diff
changeset
|
145 |
val data = the (case term_opt of |
24508
c8b82fec6447
replaced ProofContext.read_term/prop by general Syntax.read_term/prop;
wenzelm
parents:
24168
diff
changeset
|
146 |
SOME t => import_fundef_data (Syntax.read_term lthy t) (Context.Proof lthy) |
22733
0b14bb35be90
definition lookup via terms, not names. Methods "relation" and "lexicographic_order"
krauss
parents:
22725
diff
changeset
|
147 |
| NONE => import_last_fundef (Context.Proof lthy)) |
0b14bb35be90
definition lookup via terms, not names. Methods "relation" and "lexicographic_order"
krauss
parents:
22725
diff
changeset
|
148 |
handle Option.Option => raise ERROR ("Not a function: " ^ quote (the_default "" term_opt)) |
20523
36a59e5d0039
Major update to function package, including new syntax and the (only theoretical)
krauss
parents:
20363
diff
changeset
|
149 |
|
22166 | 150 |
val FundefCtxData {termination, R, ...} = data |
22325 | 151 |
val domT = domain_type (fastype_of R) |
152 |
val goal = HOLogic.mk_Trueprop (HOLogic.mk_all ("x", domT, mk_acc domT R $ Free ("x", domT))) |
|
20523
36a59e5d0039
Major update to function package, including new syntax and the (only theoretical)
krauss
parents:
20363
diff
changeset
|
153 |
in |
20877
368b997ad67e
removed with_local_path -- LocalTheory.note admits qualified names;
wenzelm
parents:
20654
diff
changeset
|
154 |
lthy |
22325 | 155 |
|> ProofContext.note_thmss_i "" [(("", [ContextRules.rule_del]), [([allI], [])])] |> snd |
156 |
|> ProofContext.note_thmss_i "" [(("", [ContextRules.intro_bang (SOME 1)]), [([allI], [])])] |> snd |
|
21602 | 157 |
|> ProofContext.note_thmss_i "" |
22325 | 158 |
[(("termination", [ContextRules.intro_bang (SOME 0)]), |
21602 | 159 |
[([Goal.norm_result termination], [])])] |> snd |
22733
0b14bb35be90
definition lookup via terms, not names. Methods "relation" and "lexicographic_order"
krauss
parents:
22725
diff
changeset
|
160 |
|> Proof.theorem_i NONE (total_termination_afterqed data) [[(goal, [])]] |
20523
36a59e5d0039
Major update to function package, including new syntax and the (only theoretical)
krauss
parents:
20363
diff
changeset
|
161 |
end |
36a59e5d0039
Major update to function package, including new syntax and the (only theoretical)
krauss
parents:
20363
diff
changeset
|
162 |
|
36a59e5d0039
Major update to function package, including new syntax and the (only theoretical)
krauss
parents:
20363
diff
changeset
|
163 |
|
36a59e5d0039
Major update to function package, including new syntax and the (only theoretical)
krauss
parents:
20363
diff
changeset
|
164 |
val add_fundef = gen_add_fundef Specification.read_specification |
24722
59fd5cceccd7
minimal adaptions for Specification.read/check_specification;
wenzelm
parents:
24711
diff
changeset
|
165 |
val add_fundef_i = gen_add_fundef Specification.check_specification |
19611
da2a0014c461
Function Package: Quick-and-dirty-fixed strange "Proved a different theorem bug"
krauss
parents:
19585
diff
changeset
|
166 |
|
19564
d3e2f532459a
First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff
changeset
|
167 |
|
21235
674e2731b519
Added datatype hook to declare all case_congs as "fundef_cong" automatically.
krauss
parents:
21211
diff
changeset
|
168 |
(* Datatype hook to declare datatype congs as "fundef_congs" *) |
674e2731b519
Added datatype hook to declare all case_congs as "fundef_cong" automatically.
krauss
parents:
21211
diff
changeset
|
169 |
|
674e2731b519
Added datatype hook to declare all case_congs as "fundef_cong" automatically.
krauss
parents:
21211
diff
changeset
|
170 |
|
21602 | 171 |
fun add_case_cong n thy = |
24168
86a03a092062
simplified internal interfaces; cong rules are now handled directly by "context_tree.ML"
krauss
parents:
24039
diff
changeset
|
172 |
Context.theory_map (FundefCtxTree.map_fundef_congs (Thm.add_thm |
21235
674e2731b519
Added datatype hook to declare all case_congs as "fundef_cong" automatically.
krauss
parents:
21211
diff
changeset
|
173 |
(DatatypePackage.get_datatype thy n |> the |
674e2731b519
Added datatype hook to declare all case_congs as "fundef_cong" automatically.
krauss
parents:
21211
diff
changeset
|
174 |
|> #case_cong |
21602 | 175 |
|> safe_mk_meta_eq))) |
21235
674e2731b519
Added datatype hook to declare all case_congs as "fundef_cong" automatically.
krauss
parents:
21211
diff
changeset
|
176 |
thy |
674e2731b519
Added datatype hook to declare all case_congs as "fundef_cong" automatically.
krauss
parents:
21211
diff
changeset
|
177 |
|
24626
85eceef2edc7
introduced generic concepts for theory interpretators
haftmann
parents:
24624
diff
changeset
|
178 |
val case_cong = fold add_case_cong |
21235
674e2731b519
Added datatype hook to declare all case_congs as "fundef_cong" automatically.
krauss
parents:
21211
diff
changeset
|
179 |
|
24711 | 180 |
val setup_case_cong = DatatypePackage.interpretation case_cong |
24626
85eceef2edc7
introduced generic concepts for theory interpretators
haftmann
parents:
24624
diff
changeset
|
181 |
|
19564
d3e2f532459a
First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff
changeset
|
182 |
|
23819 | 183 |
|
23473
997bca36d4fe
new method "elim_to_cases" provides ad-hoc conversion of obtain-style
krauss
parents:
23203
diff
changeset
|
184 |
(* ad-hoc method to convert elimination-style goals to existential statements *) |
997bca36d4fe
new method "elim_to_cases" provides ad-hoc conversion of obtain-style
krauss
parents:
23203
diff
changeset
|
185 |
|
997bca36d4fe
new method "elim_to_cases" provides ad-hoc conversion of obtain-style
krauss
parents:
23203
diff
changeset
|
186 |
fun insert_int_goal thy subg st = |
997bca36d4fe
new method "elim_to_cases" provides ad-hoc conversion of obtain-style
krauss
parents:
23203
diff
changeset
|
187 |
let |
997bca36d4fe
new method "elim_to_cases" provides ad-hoc conversion of obtain-style
krauss
parents:
23203
diff
changeset
|
188 |
val goal = hd (prems_of st) |
997bca36d4fe
new method "elim_to_cases" provides ad-hoc conversion of obtain-style
krauss
parents:
23203
diff
changeset
|
189 |
val (ps, imp) = dest_all_all goal |
997bca36d4fe
new method "elim_to_cases" provides ad-hoc conversion of obtain-style
krauss
parents:
23203
diff
changeset
|
190 |
val cps = map (cterm_of thy) ps |
997bca36d4fe
new method "elim_to_cases" provides ad-hoc conversion of obtain-style
krauss
parents:
23203
diff
changeset
|
191 |
|
997bca36d4fe
new method "elim_to_cases" provides ad-hoc conversion of obtain-style
krauss
parents:
23203
diff
changeset
|
192 |
val imp_subg = fold (fn p => fn t => betapply (t,p)) ps subg |
997bca36d4fe
new method "elim_to_cases" provides ad-hoc conversion of obtain-style
krauss
parents:
23203
diff
changeset
|
193 |
val new_subg = implies $ imp_subg $ imp |
997bca36d4fe
new method "elim_to_cases" provides ad-hoc conversion of obtain-style
krauss
parents:
23203
diff
changeset
|
194 |
|> fold_rev mk_forall ps |
997bca36d4fe
new method "elim_to_cases" provides ad-hoc conversion of obtain-style
krauss
parents:
23203
diff
changeset
|
195 |
|> cterm_of thy |
997bca36d4fe
new method "elim_to_cases" provides ad-hoc conversion of obtain-style
krauss
parents:
23203
diff
changeset
|
196 |
|> assume |
997bca36d4fe
new method "elim_to_cases" provides ad-hoc conversion of obtain-style
krauss
parents:
23203
diff
changeset
|
197 |
|
997bca36d4fe
new method "elim_to_cases" provides ad-hoc conversion of obtain-style
krauss
parents:
23203
diff
changeset
|
198 |
val sg2 = imp_subg |
997bca36d4fe
new method "elim_to_cases" provides ad-hoc conversion of obtain-style
krauss
parents:
23203
diff
changeset
|
199 |
|> fold_rev mk_forall ps |
997bca36d4fe
new method "elim_to_cases" provides ad-hoc conversion of obtain-style
krauss
parents:
23203
diff
changeset
|
200 |
|> cterm_of thy |
997bca36d4fe
new method "elim_to_cases" provides ad-hoc conversion of obtain-style
krauss
parents:
23203
diff
changeset
|
201 |
|> assume |
997bca36d4fe
new method "elim_to_cases" provides ad-hoc conversion of obtain-style
krauss
parents:
23203
diff
changeset
|
202 |
|
997bca36d4fe
new method "elim_to_cases" provides ad-hoc conversion of obtain-style
krauss
parents:
23203
diff
changeset
|
203 |
val t' = new_subg |
997bca36d4fe
new method "elim_to_cases" provides ad-hoc conversion of obtain-style
krauss
parents:
23203
diff
changeset
|
204 |
|> fold forall_elim cps |
24977
9f98751c9628
replaced (flip Thm.implies_elim) by Thm.elim_implies;
wenzelm
parents:
24867
diff
changeset
|
205 |
|> Thm.elim_implies (fold forall_elim cps sg2) |
23473
997bca36d4fe
new method "elim_to_cases" provides ad-hoc conversion of obtain-style
krauss
parents:
23203
diff
changeset
|
206 |
|> fold_rev forall_intr cps |
997bca36d4fe
new method "elim_to_cases" provides ad-hoc conversion of obtain-style
krauss
parents:
23203
diff
changeset
|
207 |
|
997bca36d4fe
new method "elim_to_cases" provides ad-hoc conversion of obtain-style
krauss
parents:
23203
diff
changeset
|
208 |
val st' = implies_elim st t' |
997bca36d4fe
new method "elim_to_cases" provides ad-hoc conversion of obtain-style
krauss
parents:
23203
diff
changeset
|
209 |
|> implies_intr (cprop_of sg2) |
997bca36d4fe
new method "elim_to_cases" provides ad-hoc conversion of obtain-style
krauss
parents:
23203
diff
changeset
|
210 |
|> implies_intr (cprop_of new_subg) |
997bca36d4fe
new method "elim_to_cases" provides ad-hoc conversion of obtain-style
krauss
parents:
23203
diff
changeset
|
211 |
in |
997bca36d4fe
new method "elim_to_cases" provides ad-hoc conversion of obtain-style
krauss
parents:
23203
diff
changeset
|
212 |
Seq.single st' |
997bca36d4fe
new method "elim_to_cases" provides ad-hoc conversion of obtain-style
krauss
parents:
23203
diff
changeset
|
213 |
end |
997bca36d4fe
new method "elim_to_cases" provides ad-hoc conversion of obtain-style
krauss
parents:
23203
diff
changeset
|
214 |
|
997bca36d4fe
new method "elim_to_cases" provides ad-hoc conversion of obtain-style
krauss
parents:
23203
diff
changeset
|
215 |
fun mk_cases_statement thy t = |
997bca36d4fe
new method "elim_to_cases" provides ad-hoc conversion of obtain-style
krauss
parents:
23203
diff
changeset
|
216 |
let |
997bca36d4fe
new method "elim_to_cases" provides ad-hoc conversion of obtain-style
krauss
parents:
23203
diff
changeset
|
217 |
fun mk_clause t = |
997bca36d4fe
new method "elim_to_cases" provides ad-hoc conversion of obtain-style
krauss
parents:
23203
diff
changeset
|
218 |
let |
997bca36d4fe
new method "elim_to_cases" provides ad-hoc conversion of obtain-style
krauss
parents:
23203
diff
changeset
|
219 |
val (qs, imp) = dest_all_all t |
997bca36d4fe
new method "elim_to_cases" provides ad-hoc conversion of obtain-style
krauss
parents:
23203
diff
changeset
|
220 |
in |
997bca36d4fe
new method "elim_to_cases" provides ad-hoc conversion of obtain-style
krauss
parents:
23203
diff
changeset
|
221 |
Logic.strip_imp_prems imp |
997bca36d4fe
new method "elim_to_cases" provides ad-hoc conversion of obtain-style
krauss
parents:
23203
diff
changeset
|
222 |
|> map (ObjectLogic.atomize_term thy) |
997bca36d4fe
new method "elim_to_cases" provides ad-hoc conversion of obtain-style
krauss
parents:
23203
diff
changeset
|
223 |
|> foldr1 HOLogic.mk_conj |
997bca36d4fe
new method "elim_to_cases" provides ad-hoc conversion of obtain-style
krauss
parents:
23203
diff
changeset
|
224 |
|> fold_rev (fn Free (v,T) => fn t => HOLogic.mk_exists (v,T,t)) qs |
997bca36d4fe
new method "elim_to_cases" provides ad-hoc conversion of obtain-style
krauss
parents:
23203
diff
changeset
|
225 |
end |
997bca36d4fe
new method "elim_to_cases" provides ad-hoc conversion of obtain-style
krauss
parents:
23203
diff
changeset
|
226 |
|
997bca36d4fe
new method "elim_to_cases" provides ad-hoc conversion of obtain-style
krauss
parents:
23203
diff
changeset
|
227 |
val (ps, imp) = dest_all_all t |
997bca36d4fe
new method "elim_to_cases" provides ad-hoc conversion of obtain-style
krauss
parents:
23203
diff
changeset
|
228 |
in |
997bca36d4fe
new method "elim_to_cases" provides ad-hoc conversion of obtain-style
krauss
parents:
23203
diff
changeset
|
229 |
Logic.strip_imp_prems imp |
997bca36d4fe
new method "elim_to_cases" provides ad-hoc conversion of obtain-style
krauss
parents:
23203
diff
changeset
|
230 |
|> map mk_clause |
997bca36d4fe
new method "elim_to_cases" provides ad-hoc conversion of obtain-style
krauss
parents:
23203
diff
changeset
|
231 |
|> foldr1 HOLogic.mk_disj |
997bca36d4fe
new method "elim_to_cases" provides ad-hoc conversion of obtain-style
krauss
parents:
23203
diff
changeset
|
232 |
|> HOLogic.mk_Trueprop |
997bca36d4fe
new method "elim_to_cases" provides ad-hoc conversion of obtain-style
krauss
parents:
23203
diff
changeset
|
233 |
|> fold_rev lambda ps |
997bca36d4fe
new method "elim_to_cases" provides ad-hoc conversion of obtain-style
krauss
parents:
23203
diff
changeset
|
234 |
end |
997bca36d4fe
new method "elim_to_cases" provides ad-hoc conversion of obtain-style
krauss
parents:
23203
diff
changeset
|
235 |
|
997bca36d4fe
new method "elim_to_cases" provides ad-hoc conversion of obtain-style
krauss
parents:
23203
diff
changeset
|
236 |
fun elim_to_cases1 ctxt st = |
997bca36d4fe
new method "elim_to_cases" provides ad-hoc conversion of obtain-style
krauss
parents:
23203
diff
changeset
|
237 |
let |
997bca36d4fe
new method "elim_to_cases" provides ad-hoc conversion of obtain-style
krauss
parents:
23203
diff
changeset
|
238 |
val thy = theory_of_thm st |
997bca36d4fe
new method "elim_to_cases" provides ad-hoc conversion of obtain-style
krauss
parents:
23203
diff
changeset
|
239 |
val [subg] = prems_of st |
997bca36d4fe
new method "elim_to_cases" provides ad-hoc conversion of obtain-style
krauss
parents:
23203
diff
changeset
|
240 |
val cex = mk_cases_statement thy subg |
997bca36d4fe
new method "elim_to_cases" provides ad-hoc conversion of obtain-style
krauss
parents:
23203
diff
changeset
|
241 |
in |
997bca36d4fe
new method "elim_to_cases" provides ad-hoc conversion of obtain-style
krauss
parents:
23203
diff
changeset
|
242 |
(insert_int_goal thy cex |
997bca36d4fe
new method "elim_to_cases" provides ad-hoc conversion of obtain-style
krauss
parents:
23203
diff
changeset
|
243 |
THEN REPEAT_ALL_NEW (Tactic.ematch_tac [disjE, exE, conjE]) 1 |
997bca36d4fe
new method "elim_to_cases" provides ad-hoc conversion of obtain-style
krauss
parents:
23203
diff
changeset
|
244 |
THEN REPEAT (Goal.assume_rule_tac ctxt 1) |
997bca36d4fe
new method "elim_to_cases" provides ad-hoc conversion of obtain-style
krauss
parents:
23203
diff
changeset
|
245 |
(* THEN REPEAT (etac thin_rl 1)*)) st |
997bca36d4fe
new method "elim_to_cases" provides ad-hoc conversion of obtain-style
krauss
parents:
23203
diff
changeset
|
246 |
end |
997bca36d4fe
new method "elim_to_cases" provides ad-hoc conversion of obtain-style
krauss
parents:
23203
diff
changeset
|
247 |
|
997bca36d4fe
new method "elim_to_cases" provides ad-hoc conversion of obtain-style
krauss
parents:
23203
diff
changeset
|
248 |
fun elim_to_cases_tac ctxt = SELECT_GOAL (elim_to_cases1 ctxt) |
997bca36d4fe
new method "elim_to_cases" provides ad-hoc conversion of obtain-style
krauss
parents:
23203
diff
changeset
|
249 |
|
997bca36d4fe
new method "elim_to_cases" provides ad-hoc conversion of obtain-style
krauss
parents:
23203
diff
changeset
|
250 |
val elim_to_cases_setup = Method.add_methods |
997bca36d4fe
new method "elim_to_cases" provides ad-hoc conversion of obtain-style
krauss
parents:
23203
diff
changeset
|
251 |
[("elim_to_cases", Method.ctxt_args (Method.SIMPLE_METHOD' o elim_to_cases_tac), |
997bca36d4fe
new method "elim_to_cases" provides ad-hoc conversion of obtain-style
krauss
parents:
23203
diff
changeset
|
252 |
"convert elimination-style goal to a disjunction of existentials")] |
997bca36d4fe
new method "elim_to_cases" provides ad-hoc conversion of obtain-style
krauss
parents:
23203
diff
changeset
|
253 |
|
19564
d3e2f532459a
First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff
changeset
|
254 |
(* setup *) |
d3e2f532459a
First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff
changeset
|
255 |
|
21602 | 256 |
val setup = |
22846 | 257 |
Attrib.add_attributes |
24168
86a03a092062
simplified internal interfaces; cong rules are now handled directly by "context_tree.ML"
krauss
parents:
24039
diff
changeset
|
258 |
[("fundef_cong", Attrib.add_del_args FundefCtxTree.cong_add FundefCtxTree.cong_del, |
22846 | 259 |
"declaration of congruence rule for function definitions")] |
24626
85eceef2edc7
introduced generic concepts for theory interpretators
haftmann
parents:
24624
diff
changeset
|
260 |
#> setup_case_cong |
22846 | 261 |
#> FundefRelation.setup |
23473
997bca36d4fe
new method "elim_to_cases" provides ad-hoc conversion of obtain-style
krauss
parents:
23203
diff
changeset
|
262 |
#> elim_to_cases_setup |
19564
d3e2f532459a
First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff
changeset
|
263 |
|
24168
86a03a092062
simplified internal interfaces; cong rules are now handled directly by "context_tree.ML"
krauss
parents:
24039
diff
changeset
|
264 |
val get_congs = FundefCtxTree.get_fundef_congs o Context.Theory |
19770
be5c23ebe1eb
HOL/Tools/function_package: Added support for mutual recursive definitions.
krauss
parents:
19617
diff
changeset
|
265 |
|
21235
674e2731b519
Added datatype hook to declare all case_congs as "fundef_cong" automatically.
krauss
parents:
21211
diff
changeset
|
266 |
|
19564
d3e2f532459a
First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff
changeset
|
267 |
(* outer syntax *) |
d3e2f532459a
First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff
changeset
|
268 |
|
d3e2f532459a
First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff
changeset
|
269 |
local structure P = OuterParse and K = OuterKeyword in |
d3e2f532459a
First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff
changeset
|
270 |
|
25045
12386aefe9ac
"sequential" is no longer a keyword. It is still used as before, but as a normal
krauss
parents:
24977
diff
changeset
|
271 |
val _ = OuterSyntax.keywords ["otherwise"]; |
24867 | 272 |
|
273 |
val _ = |
|
19564
d3e2f532459a
First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff
changeset
|
274 |
OuterSyntax.command "function" "define general recursive functions" K.thy_goal |
22498
62cdd4b3e96b
made function syntax strict, requiring | to separate equations; cleanup
krauss
parents:
22325
diff
changeset
|
275 |
(fundef_parser default_config |
23203
a5026e73cfcf
"function (sequential)" and "fun" now handle incomplete patterns silently by adding "undefined" cases.
krauss
parents:
23189
diff
changeset
|
276 |
>> (fn ((config, fixes), (flags, statements)) => |
a5026e73cfcf
"function (sequential)" and "fun" now handle incomplete patterns silently by adding "undefined" cases.
krauss
parents:
23189
diff
changeset
|
277 |
Toplevel.local_theory_to_proof (target_of config) (add_fundef fixes statements config flags) |
21211
5370cfbf3070
Preparations for making "lexicographic_order" part of "fun"
krauss
parents:
21051
diff
changeset
|
278 |
#> Toplevel.print)); |
20523
36a59e5d0039
Major update to function package, including new syntax and the (only theoretical)
krauss
parents:
20363
diff
changeset
|
279 |
|
24867 | 280 |
val _ = |
19564
d3e2f532459a
First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff
changeset
|
281 |
OuterSyntax.command "termination" "prove termination of a recursive function" K.thy_goal |
22733
0b14bb35be90
definition lookup via terms, not names. Methods "relation" and "lexicographic_order"
krauss
parents:
22725
diff
changeset
|
282 |
(P.opt_target -- Scan.option P.term |
21000 | 283 |
>> (fn (target, name) => |
284 |
Toplevel.print o |
|
21211
5370cfbf3070
Preparations for making "lexicographic_order" part of "fun"
krauss
parents:
21051
diff
changeset
|
285 |
Toplevel.local_theory_to_proof target (setup_termination_proof name))); |
19564
d3e2f532459a
First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff
changeset
|
286 |
|
d3e2f532459a
First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff
changeset
|
287 |
end; |
d3e2f532459a
First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff
changeset
|
288 |
|
d3e2f532459a
First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff
changeset
|
289 |
|
19585 | 290 |
end |