91
667a0943c40b
added a section that will eventually describe the code
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
+ − 1
theory Ind_Code
224
+ − 2
imports "../Base" "../FirstSteps" Ind_General_Scheme
91
667a0943c40b
added a section that will eventually describe the code
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
+ − 3
begin
667a0943c40b
added a section that will eventually describe the code
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
+ − 4
211
d5accbc67e1b
more work on simple inductive and marked all sections that are still seriously incomplete with TBD
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 5
section {* The Gory Details\label{sec:code} *}
d5accbc67e1b
more work on simple inductive and marked all sections that are still seriously incomplete with TBD
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 6
d5accbc67e1b
more work on simple inductive and marked all sections that are still seriously incomplete with TBD
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 7
text {*
237
+ − 8
As mentioned before the code falls roughly into three parts: the code that deals
+ − 9
with the definitions, with the induction principles and with the introduction
+ − 10
rules. In addition there are some administrative functions that string everything
+ − 11
together.
211
d5accbc67e1b
more work on simple inductive and marked all sections that are still seriously incomplete with TBD
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 12
*}
208
+ − 13
+ − 14
subsection {* Definitions *}
+ − 15
+ − 16
text {*
237
+ − 17
We first have to produce for each predicate the user specifies an appropriate
+ − 18
definition, whose general form is
178
fb8f22dd8ad0
adapted to latest Attrib.setup changes and more work on the simple induct chapter
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 19
fb8f22dd8ad0
adapted to latest Attrib.setup changes and more work on the simple induct chapter
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 20
@{text [display] "pred \<equiv> \<lambda>zs. \<forall>preds. orules \<longrightarrow> pred zs"}
fb8f22dd8ad0
adapted to latest Attrib.setup changes and more work on the simple induct chapter
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 21
208
+ − 22
and then ``register'' the definition inside a local theory.
237
+ − 23
To do the latter, we use the following wrapper for the function
316
+ − 24
@{ML_ind define in LocalTheory}. The wrapper takes a predicate name, a syntax
178
fb8f22dd8ad0
adapted to latest Attrib.setup changes and more work on the simple induct chapter
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 25
annotation and a term representing the right-hand side of the definition.
fb8f22dd8ad0
adapted to latest Attrib.setup changes and more work on the simple induct chapter
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 26
*}
fb8f22dd8ad0
adapted to latest Attrib.setup changes and more work on the simple induct chapter
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 27
237
+ − 28
ML %linenosgray{*fun make_defn ((predname, mx), trm) lthy =
164
+ − 29
let
237
+ − 30
val arg = ((predname, mx), (Attrib.empty_binding, trm))
176
+ − 31
val ((_, (_ , thm)), lthy') = LocalTheory.define Thm.internalK arg lthy
164
+ − 32
in
176
+ − 33
(thm, lthy')
164
+ − 34
end*}
+ − 35
+ − 36
text {*
237
+ − 37
It returns the definition (as a theorem) and the local theory in which the
316
+ − 38
definition has been made. In Line 4, @{ML_ind internalK in Thm} is a flag
+ − 39
attached to the theorem (other possibile flags are @{ML_ind definitionK in Thm}
+ − 40
and @{ML_ind axiomK in Thm}). These flags just classify theorems and have no
237
+ − 41
significant meaning, except for tools that, for example, find theorems in
+ − 42
the theorem database.\footnote{FIXME: put in the section about theorems.} We
316
+ − 43
also use @{ML_ind empty_binding in Attrib} in Line 3, since the definitions for
237
+ − 44
our inductive predicates are not meant to be seen by the user and therefore
+ − 45
do not need to have any theorem attributes. A testcase for this function is
164
+ − 46
*}
+ − 47
178
fb8f22dd8ad0
adapted to latest Attrib.setup changes and more work on the simple induct chapter
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 48
local_setup %gray {* fn lthy =>
fb8f22dd8ad0
adapted to latest Attrib.setup changes and more work on the simple induct chapter
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 49
let
237
+ − 50
val arg = ((@{binding "My_True"}, NoSyn), @{term True})
210
+ − 51
val (def, lthy') = make_defn arg lthy
178
fb8f22dd8ad0
adapted to latest Attrib.setup changes and more work on the simple induct chapter
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 52
in
301
+ − 53
tracing (string_of_thm_no_vars lthy' def); lthy'
178
fb8f22dd8ad0
adapted to latest Attrib.setup changes and more work on the simple induct chapter
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 54
end *}
164
+ − 55
+ − 56
text {*
237
+ − 57
which introduces the definition @{thm My_True_def} and then prints it out.
208
+ − 58
Since we are testing the function inside \isacommand{local\_setup}, i.e., make
211
d5accbc67e1b
more work on simple inductive and marked all sections that are still seriously incomplete with TBD
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 59
actual changes to the ambient theory, we can query the definition with the usual
183
+ − 60
command \isacommand{thm}:
179
+ − 61
+ − 62
\begin{isabelle}
237
+ − 63
\isacommand{thm}~@{thm [source] "My_True_def"}\\
+ − 64
@{text ">"}~@{thm "My_True_def"}
179
+ − 65
\end{isabelle}
164
+ − 66
208
+ − 67
The next two functions construct the right-hand sides of the definitions,
237
+ − 68
which are terms whose general form is:
179
+ − 69
190
+ − 70
@{text [display] "\<lambda>zs. \<forall>preds. orules \<longrightarrow> pred zs"}
179
+ − 71
237
+ − 72
When constructing these terms, the variables @{text "zs"} need to be chosen so
+ − 73
that they do not occur in the @{text orules} and also be distinct from the
+ − 74
@{text "preds"}.
208
+ − 75
183
+ − 76
211
d5accbc67e1b
more work on simple inductive and marked all sections that are still seriously incomplete with TBD
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 77
The first function, named @{text defn_aux}, constructs the term for one
d5accbc67e1b
more work on simple inductive and marked all sections that are still seriously incomplete with TBD
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 78
particular predicate (the argument @{text "pred"} in the code below). The
d5accbc67e1b
more work on simple inductive and marked all sections that are still seriously incomplete with TBD
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 79
number of arguments of this predicate is determined by the number of
d5accbc67e1b
more work on simple inductive and marked all sections that are still seriously incomplete with TBD
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 80
argument types given in @{text "arg_tys"}. The other arguments of the
d5accbc67e1b
more work on simple inductive and marked all sections that are still seriously incomplete with TBD
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 81
function are the @{text orules} and all the @{text "preds"}.
164
+ − 82
*}
+ − 83
210
+ − 84
ML %linenosgray{*fun defn_aux lthy orules preds (pred, arg_tys) =
164
+ − 85
let
+ − 86
fun mk_all x P = HOLogic.all_const (fastype_of x) $ lambda x P
+ − 87
+ − 88
val fresh_args =
176
+ − 89
arg_tys
164
+ − 90
|> map (pair "z")
179
+ − 91
|> Variable.variant_frees lthy (preds @ orules)
164
+ − 92
|> map Free
+ − 93
in
+ − 94
list_comb (pred, fresh_args)
+ − 95
|> fold_rev (curry HOLogic.mk_imp) orules
+ − 96
|> fold_rev mk_all preds
+ − 97
|> fold_rev lambda fresh_args
+ − 98
end*}
+ − 99
+ − 100
text {*
211
d5accbc67e1b
more work on simple inductive and marked all sections that are still seriously incomplete with TBD
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 101
The function @{text mk_all} in Line 3 is just a helper function for constructing
d5accbc67e1b
more work on simple inductive and marked all sections that are still seriously incomplete with TBD
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 102
universal quantifications. The code in Lines 5 to 9 produces the fresh @{text
190
+ − 103
"zs"}. For this it pairs every argument type with the string
184
+ − 104
@{text [quotes] "z"} (Line 7); then generates variants for all these strings
190
+ − 105
so that they are unique w.r.t.~to the predicates and @{text "orules"} (Line 8);
184
+ − 106
in Line 9 it generates the corresponding variable terms for the unique
+ − 107
strings.
164
+ − 108
211
d5accbc67e1b
more work on simple inductive and marked all sections that are still seriously incomplete with TBD
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 109
The unique variables are applied to the predicate in Line 11 using the
183
+ − 110
function @{ML list_comb}; then the @{text orules} are prefixed (Line 12); in
+ − 111
Line 13 we quantify over all predicates; and in line 14 we just abstract
208
+ − 112
over all the @{text "zs"}, i.e., the fresh arguments of the
190
+ − 113
predicate. A testcase for this function is
164
+ − 114
*}
+ − 115
295
24c68350d059
polished the package chapter used FOCUS to explain the subproofs
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 116
local_setup %gray {* fn lthy =>
176
+ − 117
let
211
d5accbc67e1b
more work on simple inductive and marked all sections that are still seriously incomplete with TBD
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 118
val def = defn_aux lthy eo_orules eo_preds (e_pred, e_arg_tys)
173
+ − 119
in
329
+ − 120
tracing (string_of_term lthy def); lthy
179
+ − 121
end *}
173
+ − 122
91
667a0943c40b
added a section that will eventually describe the code
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
+ − 123
text {*
211
d5accbc67e1b
more work on simple inductive and marked all sections that are still seriously incomplete with TBD
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 124
where we use the shorthands defined in Figure~\ref{fig:shorthands}.
d5accbc67e1b
more work on simple inductive and marked all sections that are still seriously incomplete with TBD
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 125
The testcase calls @{ML defn_aux} for the predicate @{text "even"} and prints
208
+ − 126
out the generated definition. So we obtain as printout
179
+ − 127
+ − 128
@{text [display]
+ − 129
"\<lambda>z. \<forall>even odd. (even 0) \<longrightarrow> (\<forall>n. odd n \<longrightarrow> even (Suc n))
+ − 130
\<longrightarrow> (\<forall>n. even n \<longrightarrow> odd (Suc n)) \<longrightarrow> even z"}
+ − 131
211
d5accbc67e1b
more work on simple inductive and marked all sections that are still seriously incomplete with TBD
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 132
If we try out the function with the rules for freshness
210
+ − 133
*}
+ − 134
295
24c68350d059
polished the package chapter used FOCUS to explain the subproofs
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 135
local_setup %gray {* fn lthy =>
24c68350d059
polished the package chapter used FOCUS to explain the subproofs
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 136
let
24c68350d059
polished the package chapter used FOCUS to explain the subproofs
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 137
val def = defn_aux lthy fresh_orules
24c68350d059
polished the package chapter used FOCUS to explain the subproofs
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 138
[fresh_pred] (fresh_pred, fresh_arg_tys)
24c68350d059
polished the package chapter used FOCUS to explain the subproofs
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 139
in
329
+ − 140
tracing (string_of_term lthy def); lthy
295
24c68350d059
polished the package chapter used FOCUS to explain the subproofs
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 141
end *}
24c68350d059
polished the package chapter used FOCUS to explain the subproofs
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 142
210
+ − 143
+ − 144
text {*
+ − 145
we obtain
+ − 146
+ − 147
@{term [display]
+ − 148
"\<lambda>z za. \<forall>fresh. (\<forall>a b. \<not> a = b \<longrightarrow> fresh a (Var b)) \<longrightarrow>
+ − 149
(\<forall>a s t. fresh a t \<longrightarrow> fresh a s \<longrightarrow> fresh a (App t s)) \<longrightarrow>
+ − 150
(\<forall>a t. fresh a (Lam a t)) \<longrightarrow>
+ − 151
(\<forall>a b t. \<not> a = b \<longrightarrow> fresh a t \<longrightarrow> fresh a (Lam b t)) \<longrightarrow> fresh z za"}
+ − 152
+ − 153
237
+ − 154
The second function, named @{text defns}, has to iterate the function
210
+ − 155
@{ML defn_aux} over all predicates. The argument @{text "preds"} is again
237
+ − 156
the list of predicates as @{ML_type term}s; the argument @{text
+ − 157
"prednames"} is the list of binding names of the predicates; @{text mxs}
+ − 158
are the list of syntax, or mixfix, annotations for the predicates;
+ − 159
@{text "arg_tyss"} is the list of argument-type-lists.
164
+ − 160
*}
+ − 161
237
+ − 162
ML %linenosgray{*fun defns rules preds prednames mxs arg_typss lthy =
164
+ − 163
let
+ − 164
val thy = ProofContext.theory_of lthy
+ − 165
val orules = map (ObjectLogic.atomize_term thy) rules
210
+ − 166
val defs = map (defn_aux lthy orules preds) (preds ~~ arg_typss)
164
+ − 167
in
237
+ − 168
fold_map make_defn (prednames ~~ mxs ~~ defs) lthy
164
+ − 169
end*}
+ − 170
+ − 171
text {*
184
+ − 172
The user will state the introduction rules using meta-implications and
237
+ − 173
meta-quanti\-fications. In Line 4, we transform these introduction rules
+ − 174
into the object logic (since definitions cannot be stated with
184
+ − 175
meta-connectives). To do this transformation we have to obtain the theory
316
+ − 176
behind the local theory using the function @{ML_ind theory_of in ProofContext}
295
24c68350d059
polished the package chapter used FOCUS to explain the subproofs
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 177
(Line 3); with this theory we can use the function
316
+ − 178
@{ML_ind atomize_term in ObjectLogic} to make the transformation (Line 4). The call
210
+ − 179
to @{ML defn_aux} in Line 5 produces all right-hand sides of the
237
+ − 180
definitions. The actual definitions are then made in Line 7. The result of
+ − 181
the function is a list of theorems and a local theory (the theorems are
+ − 182
registered with the local theory). A testcase for this function is
164
+ − 183
*}
+ − 184
179
+ − 185
local_setup %gray {* fn lthy =>
178
fb8f22dd8ad0
adapted to latest Attrib.setup changes and more work on the simple induct chapter
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 186
let
208
+ − 187
val (defs, lthy') =
237
+ − 188
defns eo_rules eo_preds eo_prednames eo_mxs eo_arg_tyss lthy
178
fb8f22dd8ad0
adapted to latest Attrib.setup changes and more work on the simple induct chapter
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 189
in
301
+ − 190
tracing (string_of_thms_no_vars lthy' defs); lthy
179
+ − 191
end *}
178
fb8f22dd8ad0
adapted to latest Attrib.setup changes and more work on the simple induct chapter
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 192
179
+ − 193
text {*
211
d5accbc67e1b
more work on simple inductive and marked all sections that are still seriously incomplete with TBD
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 194
where we feed into the function all parameters corresponding to
295
24c68350d059
polished the package chapter used FOCUS to explain the subproofs
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 195
the @{text even}/@{text odd} example. The definitions we obtain
184
+ − 196
are:
179
+ − 197
210
+ − 198
@{text [display, break]
+ − 199
"even \<equiv> \<lambda>z. \<forall>even odd. (even 0) \<longrightarrow> (\<forall>n. odd n \<longrightarrow> even (Suc n))
+ − 200
\<longrightarrow> (\<forall>n. even n \<longrightarrow> odd (Suc n)) \<longrightarrow> even z,
+ − 201
odd \<equiv> \<lambda>z. \<forall>even odd. (even 0) \<longrightarrow> (\<forall>n. odd n \<longrightarrow> even (Suc n))
+ − 202
\<longrightarrow> (\<forall>n. even n \<longrightarrow> odd (Suc n)) \<longrightarrow> odd z"}
178
fb8f22dd8ad0
adapted to latest Attrib.setup changes and more work on the simple induct chapter
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 203
208
+ − 204
Note that in the testcase we return the local theory @{text lthy}
+ − 205
(not the modified @{text lthy'}). As a result the test case has no effect
210
+ − 206
on the ambient theory. The reason is that if we introduce the
295
24c68350d059
polished the package chapter used FOCUS to explain the subproofs
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 207
definition again, we pollute the name space with two versions of
24c68350d059
polished the package chapter used FOCUS to explain the subproofs
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 208
@{text "even"} and @{text "odd"}. We want to avoid this here.
164
+ − 209
210
+ − 210
This completes the code for introducing the definitions. Next we deal with
208
+ − 211
the induction principles.
+ − 212
*}
+ − 213
210
+ − 214
subsection {* Induction Principles *}
208
+ − 215
+ − 216
text {*
215
+ − 217
Recall that the manual proof for the induction principle
+ − 218
of @{text "even"} was:
179
+ − 219
*}
176
+ − 220
210
+ − 221
lemma manual_ind_prin_even:
209
+ − 222
assumes prem: "even z"
+ − 223
shows "P 0 \<Longrightarrow> (\<And>m. Q m \<Longrightarrow> P (Suc m)) \<Longrightarrow> (\<And>m. P m \<Longrightarrow> Q (Suc m)) \<Longrightarrow> P z"
176
+ − 224
apply(atomize (full))
209
+ − 225
apply(cut_tac prem)
176
+ − 226
apply(unfold even_def)
+ − 227
apply(drule spec[where x=P])
+ − 228
apply(drule spec[where x=Q])
+ − 229
apply(assumption)
+ − 230
done
+ − 231
179
+ − 232
text {*
190
+ − 233
The code for automating such induction principles has to accomplish two tasks:
184
+ − 234
constructing the induction principles from the given introduction
190
+ − 235
rules and then automatically generating proofs for them using a tactic.
183
+ − 236
+ − 237
The tactic will use the following helper function for instantiating universal
184
+ − 238
quantifiers.
179
+ − 239
*}
176
+ − 240
179
+ − 241
ML{*fun inst_spec ctrm =
323
+ − 242
Drule.instantiate' [SOME (ctyp_of_term ctrm)] [NONE, SOME ctrm]
+ − 243
@{thm spec}*}
164
+ − 244
+ − 245
text {*
323
+ − 246
This helper function uses the function @{ML_ind instantiate' in Drule}
+ − 247
and instantiates the @{text "?x"} in the theorem @{thm spec} with a given
+ − 248
@{ML_type cterm}. We call this helper function in the following
+ − 249
tactic.\label{fun:instspectac}.
164
+ − 250
*}
+ − 251
183
+ − 252
ML{*fun inst_spec_tac ctrms =
+ − 253
EVERY' (map (dtac o inst_spec) ctrms)*}
+ − 254
+ − 255
text {*
212
+ − 256
This tactic expects a list of @{ML_type cterm}s. It allows us in the
+ − 257
proof below to instantiate the three quantifiers in the assumption.
184
+ − 258
*}
183
+ − 259
184
+ − 260
lemma
224
+ − 261
fixes P::"nat \<Rightarrow> nat \<Rightarrow> nat \<Rightarrow> bool"
+ − 262
shows "\<forall>x y z. P x y z \<Longrightarrow> True"
176
+ − 263
apply (tactic {*
212
+ − 264
inst_spec_tac [@{cterm "a::nat"},@{cterm "b::nat"},@{cterm "c::nat"}] 1 *})
179
+ − 265
txt {*
183
+ − 266
We obtain the goal state
179
+ − 267
+ − 268
\begin{minipage}{\textwidth}
+ − 269
@{subgoals}
+ − 270
\end{minipage}*}
164
+ − 271
(*<*)oops(*>*)
+ − 272
+ − 273
text {*
211
d5accbc67e1b
more work on simple inductive and marked all sections that are still seriously incomplete with TBD
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 274
The complete tactic for proving the induction principles can now
183
+ − 275
be implemented as follows:
163
2319cff107f0
removed rep_ss, and used dest_ss instead; some very slight changes to simple_inductive
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 276
*}
2319cff107f0
removed rep_ss, and used dest_ss instead; some very slight changes to simple_inductive
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 277
210
+ − 278
ML %linenosgray{*fun ind_tac defs prem insts =
176
+ − 279
EVERY1 [ObjectLogic.full_atomize_tac,
208
+ − 280
cut_facts_tac prem,
164
+ − 281
K (rewrite_goals_tac defs),
183
+ − 282
inst_spec_tac insts,
164
+ − 283
assume_tac]*}
+ − 284
179
+ − 285
text {*
215
+ − 286
We have to give it as arguments the definitions, the premise (a list of
+ − 287
formulae) and the instantiations. The premise is @{text "even n"} in lemma
295
24c68350d059
polished the package chapter used FOCUS to explain the subproofs
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 288
@{thm [source] manual_ind_prin_even} shown above; in our code it will always be a list
215
+ − 289
consisting of a single formula. Compare this tactic with the manual proof
+ − 290
for the lemma @{thm [source] manual_ind_prin_even}: as you can see there is
+ − 291
almost a one-to-one correspondence between the \isacommand{apply}-script and
295
24c68350d059
polished the package chapter used FOCUS to explain the subproofs
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 292
the @{ML ind_tac}. We first rewrite the goal to use only object connectives (Line 2),
24c68350d059
polished the package chapter used FOCUS to explain the subproofs
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 293
"cut in" the premise (Line 3), unfold the definitions (Line 4), instantiate
24c68350d059
polished the package chapter used FOCUS to explain the subproofs
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 294
the assumptions of the goal (Line 5) and then conclude with @{ML assume_tac}.
24c68350d059
polished the package chapter used FOCUS to explain the subproofs
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 295
24c68350d059
polished the package chapter used FOCUS to explain the subproofs
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 296
Two testcases for this tactic are:
183
+ − 297
*}
+ − 298
210
+ − 299
lemma automatic_ind_prin_even:
208
+ − 300
assumes prem: "even z"
+ − 301
shows "P 0 \<Longrightarrow> (\<And>m. Q m \<Longrightarrow> P (Suc m)) \<Longrightarrow> (\<And>m. P m \<Longrightarrow> Q (Suc m)) \<Longrightarrow> P z"
210
+ − 302
by (tactic {* ind_tac eo_defs @{thms prem}
+ − 303
[@{cterm "P::nat\<Rightarrow>bool"}, @{cterm "Q::nat\<Rightarrow>bool"}] *})
+ − 304
+ − 305
lemma automatic_ind_prin_fresh:
+ − 306
assumes prem: "fresh z za"
+ − 307
shows "(\<And>a b. a \<noteq> b \<Longrightarrow> P a (Var b)) \<Longrightarrow>
+ − 308
(\<And>a t s. \<lbrakk>P a t; P a s\<rbrakk> \<Longrightarrow> P a (App t s)) \<Longrightarrow>
+ − 309
(\<And>a t. P a (Lam a t)) \<Longrightarrow>
+ − 310
(\<And>a b t. \<lbrakk>a \<noteq> b; P a t\<rbrakk> \<Longrightarrow> P a (Lam b t)) \<Longrightarrow> P z za"
+ − 311
by (tactic {* ind_tac @{thms fresh_def} @{thms prem}
211
d5accbc67e1b
more work on simple inductive and marked all sections that are still seriously incomplete with TBD
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 312
[@{cterm "P::string\<Rightarrow>trm\<Rightarrow>bool"}] *})
210
+ − 313
164
+ − 314
165
+ − 315
text {*
211
d5accbc67e1b
more work on simple inductive and marked all sections that are still seriously incomplete with TBD
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 316
While the tactic for proving the induction principles is relatively simple,
237
+ − 317
it will be a bit more work to construct the goals from the introduction rules
211
d5accbc67e1b
more work on simple inductive and marked all sections that are still seriously incomplete with TBD
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 318
the user provides. Therefore let us have a closer look at the first
d5accbc67e1b
more work on simple inductive and marked all sections that are still seriously incomplete with TBD
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 319
proved theorem:
209
+ − 320
+ − 321
\begin{isabelle}
210
+ − 322
\isacommand{thm}~@{thm [source] automatic_ind_prin_even}\\
+ − 323
@{text "> "}~@{thm automatic_ind_prin_even}
209
+ − 324
\end{isabelle}
+ − 325
210
+ − 326
The variables @{text "z"}, @{text "P"} and @{text "Q"} are schematic
215
+ − 327
variables (since they are not quantified in the lemma). These
+ − 328
variables must be schematic, otherwise they cannot be instantiated
+ − 329
by the user. To generate these schematic variables we use a common trick
+ − 330
in Isabelle programming: we first declare them as \emph{free},
+ − 331
\emph{but fixed}, and then use the infrastructure to turn them into
+ − 332
schematic variables.
+ − 333
+ − 334
In general we have to construct for each predicate @{text "pred"} a goal
+ − 335
of the form
179
+ − 336
+ − 337
@{text [display]
208
+ − 338
"pred ?zs \<Longrightarrow> rules[preds := ?Ps] \<Longrightarrow> ?P ?zs"}
180
+ − 339
211
d5accbc67e1b
more work on simple inductive and marked all sections that are still seriously incomplete with TBD
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 340
where the predicates @{text preds} are replaced in @{text rules} by new
d5accbc67e1b
more work on simple inductive and marked all sections that are still seriously incomplete with TBD
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 341
distinct variables @{text "?Ps"}. We also need to generate fresh arguments
d5accbc67e1b
more work on simple inductive and marked all sections that are still seriously incomplete with TBD
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 342
@{text "?zs"} for the predicate @{text "pred"} and the @{text "?P"} in
215
+ − 343
the conclusion.
190
+ − 344
211
d5accbc67e1b
more work on simple inductive and marked all sections that are still seriously incomplete with TBD
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 345
We generate these goals in two steps. The first function, named @{text prove_ind},
d5accbc67e1b
more work on simple inductive and marked all sections that are still seriously incomplete with TBD
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 346
expects that the introduction rules are already appropriately substituted. The argument
208
+ − 347
@{text "srules"} stands for these substituted rules; @{text cnewpreds} are
+ − 348
the certified terms coresponding to the variables @{text "?Ps"}; @{text
210
+ − 349
"pred"} is the predicate for which we prove the induction principle;
208
+ − 350
@{text "newpred"} is its replacement and @{text "arg_tys"} are the argument
+ − 351
types of this predicate.
165
+ − 352
*}
+ − 353
210
+ − 354
ML %linenosgray{*fun prove_ind lthy defs srules cnewpreds ((pred, newpred), arg_tys) =
178
fb8f22dd8ad0
adapted to latest Attrib.setup changes and more work on the simple induct chapter
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 355
let
190
+ − 356
val zs = replicate (length arg_tys) "z"
179
+ − 357
val (newargnames, lthy') = Variable.variant_fixes zs lthy;
190
+ − 358
val newargs = map Free (newargnames ~~ arg_tys)
178
fb8f22dd8ad0
adapted to latest Attrib.setup changes and more work on the simple induct chapter
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 359
fb8f22dd8ad0
adapted to latest Attrib.setup changes and more work on the simple induct chapter
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 360
val prem = HOLogic.mk_Trueprop (list_comb (pred, newargs))
fb8f22dd8ad0
adapted to latest Attrib.setup changes and more work on the simple induct chapter
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 361
val goal = Logic.list_implies
183
+ − 362
(srules, HOLogic.mk_Trueprop (list_comb (newpred, newargs)))
178
fb8f22dd8ad0
adapted to latest Attrib.setup changes and more work on the simple induct chapter
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 363
in
179
+ − 364
Goal.prove lthy' [] [prem] goal
210
+ − 365
(fn {prems, ...} => ind_tac defs prems cnewpreds)
179
+ − 366
|> singleton (ProofContext.export lthy' lthy)
+ − 367
end *}
+ − 368
180
+ − 369
text {*
190
+ − 370
In Line 3 we produce names @{text "zs"} for each type in the
183
+ − 371
argument type list. Line 4 makes these names unique and declares them as
215
+ − 372
free, but fixed, variables in the local theory @{text "lthy'"}.
212
+ − 373
That means they are not schematic variables (yet).
210
+ − 374
In Line 5 we construct the terms corresponding to these variables.
208
+ − 375
The variables are applied to the predicate in Line 7 (this corresponds
190
+ − 376
to the first premise @{text "pred zs"} of the induction principle).
+ − 377
In Line 8 and 9, we first construct the term @{text "P zs"}
212
+ − 378
and then add the (substituted) introduction rules as preconditions. In
+ − 379
case that no introduction rules are given, the conclusion of this
+ − 380
implication needs to be wrapped inside a @{term Trueprop}, otherwise
+ − 381
the Isabelle's goal mechanism will fail.\footnote{FIXME: check with
+ − 382
Stefan...is this so?}
180
+ − 383
316
+ − 384
In Line 11 we set up the goal to be proved using the function @{ML_ind
295
24c68350d059
polished the package chapter used FOCUS to explain the subproofs
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 385
prove in Goal}; in the next line we call the tactic for proving the
24c68350d059
polished the package chapter used FOCUS to explain the subproofs
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 386
induction principle. As mentioned before, this tactic expects the
24c68350d059
polished the package chapter used FOCUS to explain the subproofs
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 387
definitions, the premise and the (certified) predicates with which the
24c68350d059
polished the package chapter used FOCUS to explain the subproofs
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 388
introduction rules have been substituted. The code in these two lines will
24c68350d059
polished the package chapter used FOCUS to explain the subproofs
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 389
return a theorem. However, it is a theorem proved inside the local theory
24c68350d059
polished the package chapter used FOCUS to explain the subproofs
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 390
@{text "lthy'"}, where the variables @{text "zs"} are free, but fixed (see
24c68350d059
polished the package chapter used FOCUS to explain the subproofs
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 391
Line 4). By exporting this theorem from @{text "lthy'"} (which contains the
24c68350d059
polished the package chapter used FOCUS to explain the subproofs
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 392
@{text "zs"} as free variables) to @{text "lthy"} (which does not), we
24c68350d059
polished the package chapter used FOCUS to explain the subproofs
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 393
obtain the desired schematic variables @{text "?zs"}. A testcase for this
24c68350d059
polished the package chapter used FOCUS to explain the subproofs
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 394
function is
190
+ − 395
*}
180
+ − 396
295
24c68350d059
polished the package chapter used FOCUS to explain the subproofs
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 397
local_setup %gray {* fn lthy =>
190
+ − 398
let
295
24c68350d059
polished the package chapter used FOCUS to explain the subproofs
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 399
val newpreds = [@{term "P::nat \<Rightarrow> bool"}, @{term "Q::nat \<Rightarrow> bool"}]
24c68350d059
polished the package chapter used FOCUS to explain the subproofs
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 400
val cnewpreds = [@{cterm "P::nat \<Rightarrow> bool"}, @{cterm "Q::nat \<Rightarrow> bool"}]
24c68350d059
polished the package chapter used FOCUS to explain the subproofs
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 401
val newpred = @{term "P::nat \<Rightarrow> bool"}
210
+ − 402
val srules = map (subst_free (eo_preds ~~ newpreds)) eo_rules
190
+ − 403
val intro =
211
d5accbc67e1b
more work on simple inductive and marked all sections that are still seriously incomplete with TBD
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 404
prove_ind lthy eo_defs srules cnewpreds ((e_pred, newpred), e_arg_tys)
190
+ − 405
in
301
+ − 406
tracing (string_of_thm lthy intro); lthy
210
+ − 407
end *}
184
+ − 408
190
+ − 409
text {*
210
+ − 410
This prints out the theorem:
190
+ − 411
+ − 412
@{text [display]
+ − 413
" \<lbrakk>even ?z; P 0; \<And>n. Q n \<Longrightarrow> P (Suc n); \<And>n. P n \<Longrightarrow> Q (Suc n)\<rbrakk> \<Longrightarrow> P ?z"}
+ − 414
210
+ − 415
The export from @{text lthy'} to @{text lthy} in Line 13 above
+ − 416
has correctly turned the free, but fixed, @{text "z"} into a schematic
209
+ − 417
variable @{text "?z"}; the variables @{text "P"} and @{text "Q"} are not yet
208
+ − 418
schematic.
190
+ − 419
+ − 420
We still have to produce the new predicates with which the introduction
210
+ − 421
rules are substituted and iterate @{ML prove_ind} over all
211
d5accbc67e1b
more work on simple inductive and marked all sections that are still seriously incomplete with TBD
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 422
predicates. This is what the second function, named @{text inds} does.
180
+ − 423
*}
165
+ − 424
210
+ − 425
ML %linenosgray{*fun inds rules defs preds arg_tyss lthy =
164
+ − 426
let
+ − 427
val Ps = replicate (length preds) "P"
183
+ − 428
val (newprednames, lthy') = Variable.variant_fixes Ps lthy
164
+ − 429
183
+ − 430
val thy = ProofContext.theory_of lthy'
164
+ − 431
184
+ − 432
val tyss' = map (fn tys => tys ---> HOLogic.boolT) arg_tyss
165
+ − 433
val newpreds = map Free (newprednames ~~ tyss')
164
+ − 434
val cnewpreds = map (cterm_of thy) newpreds
184
+ − 435
val srules = map (subst_free (preds ~~ newpreds)) rules
164
+ − 436
+ − 437
in
210
+ − 438
map (prove_ind lthy' defs srules cnewpreds)
184
+ − 439
(preds ~~ newpreds ~~ arg_tyss)
183
+ − 440
|> ProofContext.export lthy' lthy
165
+ − 441
end*}
+ − 442
184
+ − 443
text {*
208
+ − 444
In Line 3, we generate a string @{text [quotes] "P"} for each predicate.
184
+ − 445
In Line 4, we use the same trick as in the previous function, that is making the
211
d5accbc67e1b
more work on simple inductive and marked all sections that are still seriously incomplete with TBD
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 446
@{text "Ps"} fresh and declaring them as free, but fixed, in
184
+ − 447
the new local theory @{text "lthy'"}. From the local theory we extract
+ − 448
the ambient theory in Line 6. We need this theory in order to certify
208
+ − 449
the new predicates. In Line 8, we construct the types of these new predicates
190
+ − 450
using the given argument types. Next we turn them into terms and subsequently
+ − 451
certify them (Line 9 and 10). We can now produce the substituted introduction rules
316
+ − 452
(Line 11) using the function @{ML_ind subst_free}. Line 14 and 15 just iterate
190
+ − 453
the proofs for all predicates.
184
+ − 454
From this we obtain a list of theorems. Finally we need to export the
208
+ − 455
fixed variables @{text "Ps"} to obtain the schematic variables @{text "?Ps"}
184
+ − 456
(Line 16).
+ − 457
+ − 458
A testcase for this function is
+ − 459
*}
+ − 460
+ − 461
local_setup %gray {* fn lthy =>
+ − 462
let
210
+ − 463
val ind_thms = inds eo_rules eo_defs eo_preds eo_arg_tyss lthy
184
+ − 464
in
301
+ − 465
tracing (string_of_thms lthy ind_thms); lthy
190
+ − 466
end *}
165
+ − 467
176
+ − 468
184
+ − 469
text {*
+ − 470
which prints out
+ − 471
+ − 472
@{text [display]
210
+ − 473
"even ?z \<Longrightarrow> ?P1 0 \<Longrightarrow>
+ − 474
(\<And>m. ?Pa1 m \<Longrightarrow> ?P1 (Suc m)) \<Longrightarrow> (\<And>m. ?P1 m \<Longrightarrow> ?Pa1 (Suc m)) \<Longrightarrow> ?P1 ?z,
+ − 475
odd ?z \<Longrightarrow> ?P1 0 \<Longrightarrow>
+ − 476
(\<And>m. ?Pa1 m \<Longrightarrow> ?P1 (Suc m)) \<Longrightarrow> (\<And>m. ?P1 m \<Longrightarrow> ?Pa1 (Suc m)) \<Longrightarrow> ?Pa1 ?z"}
184
+ − 477
208
+ − 478
Note that now both, the @{text "?Ps"} and the @{text "?zs"}, are schematic
210
+ − 479
variables. The numbers attached to these variables have been introduced by
+ − 480
the pretty-printer and are \emph{not} important for the user.
184
+ − 481
210
+ − 482
This completes the code for the induction principles. The final peice
+ − 483
of reasoning infrastructure we need are the introduction rules.
208
+ − 484
*}
+ − 485
+ − 486
subsection {* Introduction Rules *}
+ − 487
+ − 488
text {*
212
+ − 489
Constructing the goals for the introduction rules is easy: they
+ − 490
are just the rules given by the user. However, their proofs are
+ − 491
quite a bit more involved than the ones for the induction principles.
+ − 492
To explain the general method, our running example will be
+ − 493
the introduction rule
208
+ − 494
212
+ − 495
\begin{isabelle}
+ − 496
@{prop "\<And>a b t. \<lbrakk>a \<noteq> b; fresh a t\<rbrakk> \<Longrightarrow> fresh a (Lam b t)"}
+ − 497
\end{isabelle}
+ − 498
+ − 499
about freshness for lambdas. In order to ease somewhat
+ − 500
our work here, we use the following two helper functions.
184
+ − 501
*}
+ − 502
165
+ − 503
ML{*val all_elims = fold (fn ct => fn th => th RS inst_spec ct)
+ − 504
val imp_elims = fold (fn th => fn th' => [th', th] MRS @{thm mp})*}
+ − 505
190
+ − 506
text {*
212
+ − 507
To see what these functions do, let us suppose we have the following three
190
+ − 508
theorems.
+ − 509
*}
+ − 510
+ − 511
lemma all_elims_test:
224
+ − 512
fixes P::"nat \<Rightarrow> nat \<Rightarrow> nat \<Rightarrow> bool"
+ − 513
shows "\<forall>x y z. P x y z" sorry
190
+ − 514
+ − 515
lemma imp_elims_test:
224
+ − 516
shows "A \<longrightarrow> B \<longrightarrow> C" sorry
190
+ − 517
+ − 518
lemma imp_elims_test':
224
+ − 519
shows "A" "B" sorry
190
+ − 520
+ − 521
text {*
+ − 522
The function @{ML all_elims} takes a list of (certified) terms and instantiates
+ − 523
theorems of the form @{thm [source] all_elims_test}. For example we can instantiate
210
+ − 524
the quantifiers in this theorem with @{term a}, @{term b} and @{term c} as follows:
190
+ − 525
+ − 526
@{ML_response_fake [display, gray]
+ − 527
"let
+ − 528
val ctrms = [@{cterm \"a::nat\"}, @{cterm \"b::nat\"}, @{cterm \"c::nat\"}]
+ − 529
val new_thm = all_elims ctrms @{thm all_elims_test}
+ − 530
in
301
+ − 531
tracing (string_of_thm_no_vars @{context} new_thm)
190
+ − 532
end"
+ − 533
"P a b c"}
+ − 534
215
+ − 535
Note the difference with @{ML inst_spec_tac} from Page~\pageref{fun:instspectac}:
+ − 536
@{ML inst_spec_tac} is a tactic which operates on a goal state; in contrast
+ − 537
@{ML all_elims} operates on theorems.
+ − 538
190
+ − 539
Similarly, the function @{ML imp_elims} eliminates preconditions from implications.
210
+ − 540
For example we can eliminate the preconditions @{text "A"} and @{text "B"} from
+ − 541
@{thm [source] imp_elims_test}:
190
+ − 542
+ − 543
@{ML_response_fake [display, gray]
295
24c68350d059
polished the package chapter used FOCUS to explain the subproofs
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 544
"let
24c68350d059
polished the package chapter used FOCUS to explain the subproofs
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 545
val res = imp_elims @{thms imp_elims_test'} @{thm imp_elims_test}
24c68350d059
polished the package chapter used FOCUS to explain the subproofs
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 546
in
301
+ − 547
tracing (string_of_thm_no_vars @{context} res)
295
24c68350d059
polished the package chapter used FOCUS to explain the subproofs
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 548
end"
190
+ − 549
"C"}
+ − 550
212
+ − 551
Now we set up the proof for the introduction rule as follows:
190
+ − 552
*}
+ − 553
211
d5accbc67e1b
more work on simple inductive and marked all sections that are still seriously incomplete with TBD
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 554
lemma fresh_Lam:
224
+ − 555
shows "\<And>a b t. \<lbrakk>a \<noteq> b; fresh a t\<rbrakk> \<Longrightarrow> fresh a (Lam b t)"
210
+ − 556
(*<*)oops(*>*)
+ − 557
+ − 558
text {*
212
+ − 559
The first step in the proof will be to expand the definitions of freshness
210
+ − 560
and then introduce quantifiers and implications. For this we
+ − 561
will use the tactic
+ − 562
*}
+ − 563
212
+ − 564
ML %linenosgray{*fun expand_tac defs =
210
+ − 565
ObjectLogic.rulify_tac 1
+ − 566
THEN rewrite_goals_tac defs
+ − 567
THEN (REPEAT (resolve_tac [@{thm allI}, @{thm impI}] 1)) *}
+ − 568
+ − 569
text {*
295
24c68350d059
polished the package chapter used FOCUS to explain the subproofs
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 570
The function in Line 2 ``rulifies'' the lemma.\footnote{FIXME: explain this better}
24c68350d059
polished the package chapter used FOCUS to explain the subproofs
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 571
This will turn out to
215
+ − 572
be important later on. Applying this tactic in our proof of @{text "fresh_Lem"}
210
+ − 573
*}
+ − 574
+ − 575
(*<*)
211
d5accbc67e1b
more work on simple inductive and marked all sections that are still seriously incomplete with TBD
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 576
lemma fresh_Lam:
224
+ − 577
shows "\<And>a b t. \<lbrakk>a \<noteq> b; fresh a t\<rbrakk> \<Longrightarrow> fresh a (Lam b t)"
210
+ − 578
(*>*)
+ − 579
apply(tactic {* expand_tac @{thms fresh_def} *})
209
+ − 580
+ − 581
txt {*
215
+ − 582
gives us the goal state
210
+ − 583
209
+ − 584
\begin{isabelle}
210
+ − 585
@{subgoals [display]}
209
+ − 586
\end{isabelle}
210
+ − 587
215
+ − 588
As you can see, there are parameters (namely @{text "a"}, @{text "b"} and
+ − 589
@{text "t"}) which come from the introduction rule and parameters (in the
+ − 590
case above only @{text "fresh"}) which come from the universal
+ − 591
quantification in the definition @{term "fresh a (App t s)"}. Similarly,
+ − 592
there are assumptions that come from the premises of the rule (namely the
+ − 593
first two) and assumptions from the definition of the predicate (assumption
+ − 594
three to six). We need to treat these parameters and assumptions
+ − 595
differently. In the code below we will therefore separate them into @{text
+ − 596
"params1"} and @{text params2}, respectively @{text "prems1"} and @{text
+ − 597
"prems2"}. To do this separation, it is best to open a subproof with the
316
+ − 598
tactic @{ML_ind SUBPROOF}, since this tactic provides us with the parameters (as
215
+ − 599
list of @{ML_type cterm}s) and the assumptions (as list of @{ML_type thm}s).
295
24c68350d059
polished the package chapter used FOCUS to explain the subproofs
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 600
The problem with @{ML SUBPROOF}, however, is that it always expects us to
24c68350d059
polished the package chapter used FOCUS to explain the subproofs
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 601
completely discharge the goal (see Section~\ref{sec:simpletacs}). This is
24c68350d059
polished the package chapter used FOCUS to explain the subproofs
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 602
a bit inconvenient for our gradual explanation of the proof here. Therefore
316
+ − 603
we use first the function @{ML_ind FOCUS in Subgoal}, which does s
299
d0b81d6e1b28
updated to Isabelle changes and merged sections in the FirstSteps chapter
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 604
ame as @{ML SUBPROOF}
295
24c68350d059
polished the package chapter used FOCUS to explain the subproofs
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 605
but does not require us to completely discharge the goal.
210
+ − 606
*}
+ − 607
(*<*)oops(*>*)
+ − 608
text_raw {*
+ − 609
\begin{figure}[t]
+ − 610
\begin{minipage}{\textwidth}
+ − 611
\begin{isabelle}
+ − 612
*}
+ − 613
ML{*fun chop_print params1 params2 prems1 prems2 ctxt =
+ − 614
let
250
ab9e09076462
some polishing; added together with Jasmin more examples to the pretty printing section
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 615
val s = ["Params1 from the rule:", string_of_cterms ctxt params1]
ab9e09076462
some polishing; added together with Jasmin more examples to the pretty printing section
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 616
@ ["Params2 from the predicate:", string_of_cterms ctxt params2]
ab9e09076462
some polishing; added together with Jasmin more examples to the pretty printing section
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 617
@ ["Prems1 from the rule:"] @ (map (string_of_thm ctxt) prems1)
ab9e09076462
some polishing; added together with Jasmin more examples to the pretty printing section
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 618
@ ["Prems2 from the predicate:"] @ (map (string_of_thm ctxt) prems2)
210
+ − 619
in
305
2ac9dc1a95b4
added a comment for printing out information and tuned some examples accordingly
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 620
s |> cat_lines
295
24c68350d059
polished the package chapter used FOCUS to explain the subproofs
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 621
|> tracing
210
+ − 622
end*}
+ − 623
text_raw{*
+ − 624
\end{isabelle}
+ − 625
\end{minipage}
211
d5accbc67e1b
more work on simple inductive and marked all sections that are still seriously incomplete with TBD
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 626
\caption{A helper function that prints out the parameters and premises that
d5accbc67e1b
more work on simple inductive and marked all sections that are still seriously incomplete with TBD
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 627
need to be treated differently.\label{fig:chopprint}}
210
+ − 628
\end{figure}
+ − 629
*}
+ − 630
+ − 631
text {*
+ − 632
First we calculate the values for @{text "params1/2"} and @{text "prems1/2"}
212
+ − 633
from @{text "params"} and @{text "prems"}, respectively. To better see what is
+ − 634
going in our example, we will print out these values using the printing
299
d0b81d6e1b28
updated to Isabelle changes and merged sections in the FirstSteps chapter
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 635
function in Figure~\ref{fig:chopprint}. Since @{ML FOCUS in Subgoal} will
210
+ − 636
supply us the @{text "params"} and @{text "prems"} as lists, we can
316
+ − 637
separate them using the function @{ML_ind chop}.
210
+ − 638
*}
+ − 639
295
24c68350d059
polished the package chapter used FOCUS to explain the subproofs
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 640
ML %linenosgray{*fun chop_test_tac preds rules =
299
d0b81d6e1b28
updated to Isabelle changes and merged sections in the FirstSteps chapter
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 641
Subgoal.FOCUS (fn {params, prems, context, ...} =>
210
+ − 642
let
295
24c68350d059
polished the package chapter used FOCUS to explain the subproofs
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 643
val cparams = map snd params
24c68350d059
polished the package chapter used FOCUS to explain the subproofs
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 644
val (params1, params2) = chop (length cparams - length preds) cparams
210
+ − 645
val (prems1, prems2) = chop (length prems - length rules) prems
+ − 646
in
295
24c68350d059
polished the package chapter used FOCUS to explain the subproofs
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 647
chop_print params1 params2 prems1 prems2 context; all_tac
210
+ − 648
end) *}
+ − 649
+ − 650
text {*
212
+ − 651
For the separation we can rely on the fact that Isabelle deterministically
+ − 652
produces parameters and premises in a goal state. The last parameters
+ − 653
that were introduced come from the quantifications in the definitions
+ − 654
(see the tactic @{ML expand_tac}).
295
24c68350d059
polished the package chapter used FOCUS to explain the subproofs
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 655
Therefore we only have to subtract in Line 5 the number of predicates (in this
211
d5accbc67e1b
more work on simple inductive and marked all sections that are still seriously incomplete with TBD
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 656
case only @{text "1"}) from the lenghts of all parameters. Similarly
295
24c68350d059
polished the package chapter used FOCUS to explain the subproofs
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 657
with the @{text "prems"} in line 6: the last premises in the goal state come from
211
d5accbc67e1b
more work on simple inductive and marked all sections that are still seriously incomplete with TBD
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 658
unfolding the definition of the predicate in the conclusion. So we can
d5accbc67e1b
more work on simple inductive and marked all sections that are still seriously incomplete with TBD
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 659
just subtract the number of rules from the number of all premises.
295
24c68350d059
polished the package chapter used FOCUS to explain the subproofs
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 660
To check our calculations we print them out in Line 8 using the
24c68350d059
polished the package chapter used FOCUS to explain the subproofs
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 661
function @{ML chop_print} from Figure~\ref{fig:chopprint} and then
24c68350d059
polished the package chapter used FOCUS to explain the subproofs
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 662
just do nothing, that is @{ML all_tac}. Applying this tactic in our example
209
+ − 663
*}
+ − 664
210
+ − 665
(*<*)
211
d5accbc67e1b
more work on simple inductive and marked all sections that are still seriously incomplete with TBD
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 666
lemma fresh_Lam:
224
+ − 667
shows "\<And>a b t. \<lbrakk>a \<noteq> b; fresh a t\<rbrakk> \<Longrightarrow> fresh a (Lam b t)"
210
+ − 668
apply(tactic {* expand_tac @{thms fresh_def} *})
+ − 669
(*>*)
295
24c68350d059
polished the package chapter used FOCUS to explain the subproofs
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 670
apply(tactic {* chop_test_tac [fresh_pred] fresh_rules @{context} 1 *})
210
+ − 671
(*<*)oops(*>*)
+ − 672
+ − 673
text {*
+ − 674
gives
209
+ − 675
210
+ − 676
\begin{isabelle}
+ − 677
@{text "Params1 from the rule:"}\\
+ − 678
@{text "a, b, t"}\\
+ − 679
@{text "Params2 from the predicate:"}\\
+ − 680
@{text "fresh"}\\
+ − 681
@{text "Prems1 from the rule:"}\\
+ − 682
@{term "a \<noteq> b"}\\
+ − 683
@{text [break]
+ − 684
"\<forall>fresh.
+ − 685
(\<forall>a b. a \<noteq> b \<longrightarrow> fresh a (Var b)) \<longrightarrow>
+ − 686
(\<forall>a t s. fresh a t \<longrightarrow> fresh a s \<longrightarrow> fresh a (App t s)) \<longrightarrow>
+ − 687
(\<forall>a t. fresh a (Lam a t)) \<longrightarrow>
+ − 688
(\<forall>a b t. a \<noteq> b \<longrightarrow> fresh a t \<longrightarrow> fresh a (Lam b t)) \<longrightarrow> fresh a t"}\\
+ − 689
@{text "Prems2 from the predicate:"}\\
+ − 690
@{term "\<forall>a b. a \<noteq> b \<longrightarrow> fresh a (Var b)"}\\
+ − 691
@{term "\<forall>a t s. fresh a t \<longrightarrow> fresh a s \<longrightarrow> fresh a (App t s)"}\\
+ − 692
@{term "\<forall>a t. fresh a (Lam a t)"}\\
+ − 693
@{term "\<forall>a b t. a \<noteq> b \<longrightarrow> fresh a t \<longrightarrow> fresh a (Lam b t)"}
+ − 694
\end{isabelle}
208
+ − 695
192
+ − 696
210
+ − 697
We now have to select from @{text prems2} the premise
+ − 698
that corresponds to the introduction rule we prove, namely:
+ − 699
212
+ − 700
@{term [display] "\<forall>a b t. a \<noteq> b \<longrightarrow> fresh a t \<longrightarrow> fresh a (Lam a t)"}
210
+ − 701
+ − 702
To use this premise with @{ML rtac}, we need to instantiate its
211
d5accbc67e1b
more work on simple inductive and marked all sections that are still seriously incomplete with TBD
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 703
quantifiers (with @{text params1}) and transform it into rule
316
+ − 704
format (using @{ML_ind rulify in ObjectLogic}). So we can modify the
295
24c68350d059
polished the package chapter used FOCUS to explain the subproofs
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 705
code as follows:
210
+ − 706
*}
+ − 707
212
+ − 708
ML %linenosgray{*fun apply_prem_tac i preds rules =
299
d0b81d6e1b28
updated to Isabelle changes and merged sections in the FirstSteps chapter
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 709
Subgoal.FOCUS (fn {params, prems, context, ...} =>
210
+ − 710
let
295
24c68350d059
polished the package chapter used FOCUS to explain the subproofs
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 711
val cparams = map snd params
24c68350d059
polished the package chapter used FOCUS to explain the subproofs
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 712
val (params1, params2) = chop (length cparams - length preds) cparams
210
+ − 713
val (prems1, prems2) = chop (length prems - length rules) prems
+ − 714
in
+ − 715
rtac (ObjectLogic.rulify (all_elims params1 (nth prems2 i))) 1
+ − 716
end) *}
+ − 717
211
d5accbc67e1b
more work on simple inductive and marked all sections that are still seriously incomplete with TBD
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 718
text {*
d5accbc67e1b
more work on simple inductive and marked all sections that are still seriously incomplete with TBD
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 719
The argument @{text i} corresponds to the number of the
215
+ − 720
introduction we want to prove. We will later on let it range
212
+ − 721
from @{text 0} to the number of @{text "rules - 1"}.
+ − 722
Below we apply this function with @{text 3}, since
211
d5accbc67e1b
more work on simple inductive and marked all sections that are still seriously incomplete with TBD
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 723
we are proving the fourth introduction rule.
d5accbc67e1b
more work on simple inductive and marked all sections that are still seriously incomplete with TBD
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 724
*}
210
+ − 725
+ − 726
(*<*)
211
d5accbc67e1b
more work on simple inductive and marked all sections that are still seriously incomplete with TBD
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 727
lemma fresh_Lam:
224
+ − 728
shows "\<And>a b t. \<lbrakk>a \<noteq> b; fresh a t\<rbrakk> \<Longrightarrow> fresh a (Lam b t)"
210
+ − 729
apply(tactic {* expand_tac @{thms fresh_def} *})
+ − 730
(*>*)
295
24c68350d059
polished the package chapter used FOCUS to explain the subproofs
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 731
apply(tactic {* apply_prem_tac 3 [fresh_pred] fresh_rules @{context} 1 *})
210
+ − 732
(*<*)oops(*>*)
+ − 733
+ − 734
text {*
295
24c68350d059
polished the package chapter used FOCUS to explain the subproofs
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 735
The goal state we obtain is:
210
+ − 736
+ − 737
\begin{isabelle}
295
24c68350d059
polished the package chapter used FOCUS to explain the subproofs
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 738
@{text "1."}~@{text "\<dots> \<Longrightarrow> "}~@{prop "a \<noteq> b"}\\
24c68350d059
polished the package chapter used FOCUS to explain the subproofs
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 739
@{text "2."}~@{text "\<dots> \<Longrightarrow> "}~@{prop "fresh a t"}
210
+ − 740
\end{isabelle}
+ − 741
215
+ − 742
As expected there are two subgoals, where the first comes from the
212
+ − 743
non-recursive premise of the introduction rule and the second comes
215
+ − 744
from the recursive one. The first goal can be solved immediately
212
+ − 745
by @{text "prems1"}. The second needs more work. It can be solved
+ − 746
with the other premise in @{text "prems1"}, namely
+ − 747
210
+ − 748
+ − 749
@{term [break,display]
+ − 750
"\<forall>fresh.
+ − 751
(\<forall>a b. a \<noteq> b \<longrightarrow> fresh a (Var b)) \<longrightarrow>
+ − 752
(\<forall>a t s. fresh a t \<longrightarrow> fresh a s \<longrightarrow> fresh a (App t s)) \<longrightarrow>
+ − 753
(\<forall>a t. fresh a (Lam a t)) \<longrightarrow>
+ − 754
(\<forall>a b t. a \<noteq> b \<longrightarrow> fresh a t \<longrightarrow> fresh a (Lam b t)) \<longrightarrow> fresh a t"}
+ − 755
+ − 756
but we have to instantiate it appropriately. These instantiations
+ − 757
come from @{text "params1"} and @{text "prems2"}. We can determine
+ − 758
whether we are in the simple or complicated case by checking whether
211
d5accbc67e1b
more work on simple inductive and marked all sections that are still seriously incomplete with TBD
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 759
the topmost connective is an @{text "\<forall>"}. The premises in the simple
212
+ − 760
case cannot have such a quantification, since the first step
+ − 761
of @{ML "expand_tac"} was to ``rulify'' the lemma.
211
d5accbc67e1b
more work on simple inductive and marked all sections that are still seriously incomplete with TBD
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 762
The premise of the complicated case must have at least one @{text "\<forall>"}
d5accbc67e1b
more work on simple inductive and marked all sections that are still seriously incomplete with TBD
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 763
coming from the quantification over the @{text preds}. So
210
+ − 764
we can implement the following function
+ − 765
*}
+ − 766
+ − 767
ML{*fun prepare_prem params2 prems2 prem =
+ − 768
rtac (case prop_of prem of
165
+ − 769
_ $ (Const (@{const_name All}, _) $ _) =>
210
+ − 770
prem |> all_elims params2
+ − 771
|> imp_elims prems2
+ − 772
| _ => prem) *}
+ − 773
+ − 774
text {*
211
d5accbc67e1b
more work on simple inductive and marked all sections that are still seriously incomplete with TBD
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 775
which either applies the premise outright (the default case) or if
d5accbc67e1b
more work on simple inductive and marked all sections that are still seriously incomplete with TBD
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 776
it has an outermost universial quantification, instantiates it first
d5accbc67e1b
more work on simple inductive and marked all sections that are still seriously incomplete with TBD
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 777
with @{text "params1"} and then @{text "prems1"}. The following
d5accbc67e1b
more work on simple inductive and marked all sections that are still seriously incomplete with TBD
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 778
tactic will therefore prove the lemma completely.
210
+ − 779
*}
+ − 780
+ − 781
ML{*fun prove_intro_tac i preds rules =
211
d5accbc67e1b
more work on simple inductive and marked all sections that are still seriously incomplete with TBD
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 782
SUBPROOF (fn {params, prems, ...} =>
210
+ − 783
let
295
24c68350d059
polished the package chapter used FOCUS to explain the subproofs
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 784
val cparams = map snd params
24c68350d059
polished the package chapter used FOCUS to explain the subproofs
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 785
val (params1, params2) = chop (length cparams - length preds) cparams
210
+ − 786
val (prems1, prems2) = chop (length prems - length rules) prems
+ − 787
in
+ − 788
rtac (ObjectLogic.rulify (all_elims params1 (nth prems2 i))) 1
+ − 789
THEN EVERY1 (map (prepare_prem params2 prems2) prems1)
+ − 790
end) *}
+ − 791
+ − 792
text {*
299
d0b81d6e1b28
updated to Isabelle changes and merged sections in the FirstSteps chapter
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 793
Note that the tactic is now @{ML SUBPROOF}, not @{ML FOCUS in Subgoal} anymore.
215
+ − 794
The full proof of the introduction rule is as follows:
210
+ − 795
*}
+ − 796
211
d5accbc67e1b
more work on simple inductive and marked all sections that are still seriously incomplete with TBD
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 797
lemma fresh_Lam:
224
+ − 798
shows "\<And>a b t. \<lbrakk>a \<noteq> b; fresh a t\<rbrakk> \<Longrightarrow> fresh a (Lam b t)"
210
+ − 799
apply(tactic {* expand_tac @{thms fresh_def} *})
+ − 800
apply(tactic {* prove_intro_tac 3 [fresh_pred] fresh_rules @{context} 1 *})
+ − 801
done
+ − 802
+ − 803
text {*
295
24c68350d059
polished the package chapter used FOCUS to explain the subproofs
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 804
Phew!\ldots
24c68350d059
polished the package chapter used FOCUS to explain the subproofs
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 805
24c68350d059
polished the package chapter used FOCUS to explain the subproofs
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 806
Unfortunately, not everything is done yet. If you look closely
212
+ − 807
at the general principle outlined for the introduction rules in
+ − 808
Section~\ref{sec:nutshell}, we have not yet dealt with the case where
+ − 809
recursive premises have preconditions. The introduction rule
211
d5accbc67e1b
more work on simple inductive and marked all sections that are still seriously incomplete with TBD
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 810
of the accessible part is such a rule.
210
+ − 811
*}
+ − 812
+ − 813
lemma accpartI:
224
+ − 814
shows "\<And>R x. (\<And>y. R y x \<Longrightarrow> accpart R y) \<Longrightarrow> accpart R x"
210
+ − 815
apply(tactic {* expand_tac @{thms accpart_def} *})
295
24c68350d059
polished the package chapter used FOCUS to explain the subproofs
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 816
apply(tactic {* chop_test_tac [acc_pred] acc_rules @{context} 1 *})
24c68350d059
polished the package chapter used FOCUS to explain the subproofs
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 817
apply(tactic {* apply_prem_tac 0 [acc_pred] acc_rules @{context} 1 *})
210
+ − 818
+ − 819
txt {*
211
d5accbc67e1b
more work on simple inductive and marked all sections that are still seriously incomplete with TBD
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 820
Here @{ML chop_test_tac} prints out the following
d5accbc67e1b
more work on simple inductive and marked all sections that are still seriously incomplete with TBD
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 821
values for @{text "params1/2"} and @{text "prems1/2"}
210
+ − 822
+ − 823
\begin{isabelle}
+ − 824
@{text "Params1 from the rule:"}\\
+ − 825
@{text "x"}\\
+ − 826
@{text "Params2 from the predicate:"}\\
+ − 827
@{text "P"}\\
+ − 828
@{text "Prems1 from the rule:"}\\
+ − 829
@{text "R ?y x \<Longrightarrow> \<forall>P. (\<forall>x. (\<forall>y. R y x \<longrightarrow> P y) \<longrightarrow> P x) \<longrightarrow> P ?y"}\\
+ − 830
@{text "Prems2 from the predicate:"}\\
+ − 831
@{term "\<forall>x. (\<forall>y. R y x \<longrightarrow> P y) \<longrightarrow> P x"}\\
+ − 832
\end{isabelle}
+ − 833
211
d5accbc67e1b
more work on simple inductive and marked all sections that are still seriously incomplete with TBD
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 834
and after application of the introduction rule
d5accbc67e1b
more work on simple inductive and marked all sections that are still seriously incomplete with TBD
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 835
using @{ML apply_prem_tac}, we are in the goal state
d5accbc67e1b
more work on simple inductive and marked all sections that are still seriously incomplete with TBD
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 836
d5accbc67e1b
more work on simple inductive and marked all sections that are still seriously incomplete with TBD
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 837
\begin{isabelle}
d5accbc67e1b
more work on simple inductive and marked all sections that are still seriously incomplete with TBD
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 838
@{text "1."}~@{term "\<And>y. R y x \<Longrightarrow> P y"}
d5accbc67e1b
more work on simple inductive and marked all sections that are still seriously incomplete with TBD
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 839
\end{isabelle}
d5accbc67e1b
more work on simple inductive and marked all sections that are still seriously incomplete with TBD
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 840
d5accbc67e1b
more work on simple inductive and marked all sections that are still seriously incomplete with TBD
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 841
d5accbc67e1b
more work on simple inductive and marked all sections that are still seriously incomplete with TBD
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 842
*}(*<*)oops(*>*)
210
+ − 843
211
d5accbc67e1b
more work on simple inductive and marked all sections that are still seriously incomplete with TBD
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 844
text {*
212
+ − 845
In order to make progress, we have to use the precondition
211
d5accbc67e1b
more work on simple inductive and marked all sections that are still seriously incomplete with TBD
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 846
@{text "R y x"} (in general there can be many of them). The best way
d5accbc67e1b
more work on simple inductive and marked all sections that are still seriously incomplete with TBD
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 847
to get a handle on these preconditions is to open up another subproof,
212
+ − 848
since the preconditions will then be bound to @{text prems}. Therfore we
211
d5accbc67e1b
more work on simple inductive and marked all sections that are still seriously incomplete with TBD
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 849
modify the function @{ML prepare_prem} as follows
d5accbc67e1b
more work on simple inductive and marked all sections that are still seriously incomplete with TBD
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 850
*}
210
+ − 851
211
d5accbc67e1b
more work on simple inductive and marked all sections that are still seriously incomplete with TBD
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 852
ML %linenosgray{*fun prepare_prem params2 prems2 ctxt prem =
210
+ − 853
SUBPROOF (fn {prems, ...} =>
+ − 854
let
+ − 855
val prem' = prems MRS prem
+ − 856
in
+ − 857
rtac (case prop_of prem' of
+ − 858
_ $ (Const (@{const_name All}, _) $ _) =>
+ − 859
prem' |> all_elims params2
+ − 860
|> imp_elims prems2
+ − 861
| _ => prem') 1
+ − 862
end) ctxt *}
+ − 863
211
d5accbc67e1b
more work on simple inductive and marked all sections that are still seriously incomplete with TBD
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 864
text {*
d5accbc67e1b
more work on simple inductive and marked all sections that are still seriously incomplete with TBD
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 865
In Line 4 we use the @{text prems} from the @{ML SUBPROOF} and resolve
212
+ − 866
them with @{text prem}. In the simple cases, that is where the @{text prem}
211
d5accbc67e1b
more work on simple inductive and marked all sections that are still seriously incomplete with TBD
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 867
comes from a non-recursive premise of the rule, @{text prems} will be
316
+ − 868
just the empty list and the function @{ML_ind MRS} does nothing. Similarly, in the
211
d5accbc67e1b
more work on simple inductive and marked all sections that are still seriously incomplete with TBD
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 869
cases where the recursive premises of the rule do not have preconditions.
212
+ − 870
In case there are preconditions, then Line 4 discharges them. After
+ − 871
that we can proceed as before, i.e., check whether the outermost
+ − 872
connective is @{text "\<forall>"}.
211
d5accbc67e1b
more work on simple inductive and marked all sections that are still seriously incomplete with TBD
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 873
212
+ − 874
The function @{ML prove_intro_tac} only needs to be changed so that it
+ − 875
gives the context to @{ML prepare_prem} (Line 8). The modified version
+ − 876
is below.
211
d5accbc67e1b
more work on simple inductive and marked all sections that are still seriously incomplete with TBD
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 877
*}
d5accbc67e1b
more work on simple inductive and marked all sections that are still seriously incomplete with TBD
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 878
d5accbc67e1b
more work on simple inductive and marked all sections that are still seriously incomplete with TBD
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 879
ML %linenosgray{*fun prove_intro_tac i preds rules =
210
+ − 880
SUBPROOF (fn {params, prems, context, ...} =>
+ − 881
let
295
24c68350d059
polished the package chapter used FOCUS to explain the subproofs
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 882
val cparams = map snd params
24c68350d059
polished the package chapter used FOCUS to explain the subproofs
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 883
val (params1, params2) = chop (length cparams - length preds) cparams
210
+ − 884
val (prems1, prems2) = chop (length prems - length rules) prems
+ − 885
in
+ − 886
rtac (ObjectLogic.rulify (all_elims params1 (nth prems2 i))) 1
+ − 887
THEN EVERY1 (map (prepare_prem params2 prems2 context) prems1)
+ − 888
end) *}
+ − 889
211
d5accbc67e1b
more work on simple inductive and marked all sections that are still seriously incomplete with TBD
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 890
text {*
212
+ − 891
With these two functions we can now also prove the introduction
211
d5accbc67e1b
more work on simple inductive and marked all sections that are still seriously incomplete with TBD
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 892
rule for the accessible part.
d5accbc67e1b
more work on simple inductive and marked all sections that are still seriously incomplete with TBD
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 893
*}
d5accbc67e1b
more work on simple inductive and marked all sections that are still seriously incomplete with TBD
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 894
210
+ − 895
lemma accpartI:
224
+ − 896
shows "\<And>R x. (\<And>y. R y x \<Longrightarrow> accpart R y) \<Longrightarrow> accpart R x"
210
+ − 897
apply(tactic {* expand_tac @{thms accpart_def} *})
+ − 898
apply(tactic {* prove_intro_tac 0 [acc_pred] acc_rules @{context} 1 *})
+ − 899
done
+ − 900
190
+ − 901
text {*
211
d5accbc67e1b
more work on simple inductive and marked all sections that are still seriously incomplete with TBD
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 902
Finally we need two functions that string everything together. The first
d5accbc67e1b
more work on simple inductive and marked all sections that are still seriously incomplete with TBD
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 903
function is the tactic that performs the proofs.
190
+ − 904
*}
+ − 905
211
d5accbc67e1b
more work on simple inductive and marked all sections that are still seriously incomplete with TBD
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 906
ML %linenosgray{*fun intro_tac defs rules preds i ctxt =
165
+ − 907
EVERY1 [ObjectLogic.rulify_tac,
+ − 908
K (rewrite_goals_tac defs),
184
+ − 909
REPEAT o (resolve_tac [@{thm allI}, @{thm impI}]),
210
+ − 910
prove_intro_tac i preds rules ctxt]*}
165
+ − 911
190
+ − 912
text {*
215
+ − 913
Lines 2 to 4 in this tactic correspond to the function @{ML expand_tac}.
+ − 914
Some testcases for this tactic are:
190
+ − 915
*}
+ − 916
211
d5accbc67e1b
more work on simple inductive and marked all sections that are still seriously incomplete with TBD
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 917
lemma even0_intro:
224
+ − 918
shows "even 0"
211
d5accbc67e1b
more work on simple inductive and marked all sections that are still seriously incomplete with TBD
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 919
by (tactic {* intro_tac eo_defs eo_rules eo_preds 0 @{context} *})
d5accbc67e1b
more work on simple inductive and marked all sections that are still seriously incomplete with TBD
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 920
d5accbc67e1b
more work on simple inductive and marked all sections that are still seriously incomplete with TBD
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 921
lemma evenS_intro:
224
+ − 922
shows "\<And>m. odd m \<Longrightarrow> even (Suc m)"
211
d5accbc67e1b
more work on simple inductive and marked all sections that are still seriously incomplete with TBD
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 923
by (tactic {* intro_tac eo_defs eo_rules eo_preds 1 @{context} *})
d5accbc67e1b
more work on simple inductive and marked all sections that are still seriously incomplete with TBD
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 924
d5accbc67e1b
more work on simple inductive and marked all sections that are still seriously incomplete with TBD
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 925
lemma fresh_App:
224
+ − 926
shows "\<And>a t s. \<lbrakk>fresh a t; fresh a s\<rbrakk> \<Longrightarrow> fresh a (App t s)"
211
d5accbc67e1b
more work on simple inductive and marked all sections that are still seriously incomplete with TBD
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 927
by (tactic {*
d5accbc67e1b
more work on simple inductive and marked all sections that are still seriously incomplete with TBD
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 928
intro_tac @{thms fresh_def} fresh_rules [fresh_pred] 1 @{context} *})
190
+ − 929
211
d5accbc67e1b
more work on simple inductive and marked all sections that are still seriously incomplete with TBD
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 930
text {*
215
+ − 931
The second function sets up in Line 4 the goals to be proved (this is easy
212
+ − 932
for the introduction rules since they are exactly the rules
+ − 933
given by the user) and iterates @{ML intro_tac} over all
+ − 934
introduction rules.
211
d5accbc67e1b
more work on simple inductive and marked all sections that are still seriously incomplete with TBD
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 935
*}
173
+ − 936
211
d5accbc67e1b
more work on simple inductive and marked all sections that are still seriously incomplete with TBD
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 937
ML %linenosgray{*fun intros rules preds defs lthy =
165
+ − 938
let
211
d5accbc67e1b
more work on simple inductive and marked all sections that are still seriously incomplete with TBD
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 939
fun intros_aux (i, goal) =
165
+ − 940
Goal.prove lthy [] [] goal
211
d5accbc67e1b
more work on simple inductive and marked all sections that are still seriously incomplete with TBD
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 941
(fn {context, ...} => intro_tac defs rules preds i context)
165
+ − 942
in
211
d5accbc67e1b
more work on simple inductive and marked all sections that are still seriously incomplete with TBD
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 943
map_index intros_aux rules
164
+ − 944
end*}
+ − 945
212
+ − 946
text {*
316
+ − 947
The iteration is done with the function @{ML_ind map_index} since we
212
+ − 948
need the introduction rule together with its number (counted from
+ − 949
@{text 0}). This completes the code for the functions deriving the
+ − 950
reasoning infrastructure. It remains to implement some administrative
+ − 951
code that strings everything together.
+ − 952
*}
+ − 953
215
+ − 954
subsection {* Administrative Functions *}
+ − 955
+ − 956
text {*
+ − 957
We have produced various theorems (definitions, induction principles and
+ − 958
introduction rules), but apart from the definitions, we have not yet
+ − 959
registered them with the theorem database. This is what the functions
316
+ − 960
@{ML_ind note in LocalTheory} does.
215
+ − 961
+ − 962
+ − 963
For convenience, we use the following
+ − 964
three wrappers this function:
+ − 965
*}
211
d5accbc67e1b
more work on simple inductive and marked all sections that are still seriously incomplete with TBD
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 966
295
24c68350d059
polished the package chapter used FOCUS to explain the subproofs
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 967
ML{*fun note_many qname ((name, attrs), thms) =
215
+ − 968
LocalTheory.note Thm.theoremK
+ − 969
((Binding.qualify false qname name, attrs), thms)
+ − 970
295
24c68350d059
polished the package chapter used FOCUS to explain the subproofs
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 971
fun note_single1 qname ((name, attrs), thm) =
24c68350d059
polished the package chapter used FOCUS to explain the subproofs
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 972
note_many qname ((name, attrs), [thm])
176
+ − 973
295
24c68350d059
polished the package chapter used FOCUS to explain the subproofs
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 974
fun note_single2 name attrs (qname, thm) =
24c68350d059
polished the package chapter used FOCUS to explain the subproofs
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 975
note_many (Binding.name_of qname) ((name, attrs), [thm]) *}
211
d5accbc67e1b
more work on simple inductive and marked all sections that are still seriously incomplete with TBD
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 976
215
+ − 977
text {*
+ − 978
The function that ``holds everything together'' is @{text "add_inductive"}.
+ − 979
Its arguments are the specification of the predicates @{text "pred_specs"}
+ − 980
and the introduction rules @{text "rule_spec"}.
+ − 981
*}
211
d5accbc67e1b
more work on simple inductive and marked all sections that are still seriously incomplete with TBD
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 982
186
371e4375c994
made the Ackermann function example safer and included suggestions from MW
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 983
ML %linenosgray{*fun add_inductive pred_specs rule_specs lthy =
165
+ − 984
let
237
+ − 985
val mxs = map snd pred_specs
165
+ − 986
val pred_specs' = map fst pred_specs
+ − 987
val prednames = map fst pred_specs'
+ − 988
val preds = map (fn (p, ty) => Free (Binding.name_of p, ty)) pred_specs'
215
+ − 989
val tyss = map (binder_types o fastype_of) preds
163
2319cff107f0
removed rep_ss, and used dest_ss instead; some very slight changes to simple_inductive
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 990
215
+ − 991
val (namesattrs, rules) = split_list rule_specs
165
+ − 992
237
+ − 993
val (defs, lthy') = defns rules preds prednames mxs tyss lthy
+ − 994
val ind_prins = inds rules defs preds tyss lthy'
210
+ − 995
val intro_rules = intros rules preds defs lthy'
91
667a0943c40b
added a section that will eventually describe the code
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
+ − 996
165
+ − 997
val mut_name = space_implode "_" (map Binding.name_of prednames)
215
+ − 998
val case_names = map (Binding.name_of o fst) namesattrs
165
+ − 999
in
295
24c68350d059
polished the package chapter used FOCUS to explain the subproofs
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 1000
lthy' |> note_many mut_name ((@{binding "intros"}, []), intro_rules)
24c68350d059
polished the package chapter used FOCUS to explain the subproofs
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 1001
||>> note_many mut_name ((@{binding "inducts"}, []), ind_prins)
24c68350d059
polished the package chapter used FOCUS to explain the subproofs
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 1002
||>> fold_map (note_single1 mut_name) (namesattrs ~~ intro_rules)
24c68350d059
polished the package chapter used FOCUS to explain the subproofs
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 1003
||>> fold_map (note_single2 @{binding "induct"}
215
+ − 1004
[Attrib.internal (K (RuleCases.case_names case_names)),
+ − 1005
Attrib.internal (K (RuleCases.consumes 1)),
+ − 1006
Attrib.internal (K (Induct.induct_pred ""))])
237
+ − 1007
(prednames ~~ ind_prins)
215
+ − 1008
|> snd
165
+ − 1009
end*}
91
667a0943c40b
added a section that will eventually describe the code
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
+ − 1010
215
+ − 1011
text {*
+ − 1012
In Line 3 the function extracts the syntax annotations from the predicates.
+ − 1013
Lines 4 to 6 extract the names of the predicates and generate
+ − 1014
the variables terms (with types) corresponding to the predicates.
+ − 1015
Line 7 produces the argument types for each predicate.
+ − 1016
+ − 1017
Line 9 extracts the introduction rules from the specifications
+ − 1018
and stores also in @{text namesattrs} the names and attributes the
+ − 1019
user may have attached to these rules.
+ − 1020
+ − 1021
Line 11 produces the definitions and also registers the definitions
+ − 1022
in the local theory @{text "lthy'"}. The next two lines produce
+ − 1023
the induction principles and the introduction rules (all of them
+ − 1024
as theorems). Both need the local theory @{text lthy'} in which
+ − 1025
the definitions have been registered.
+ − 1026
+ − 1027
Lines 15 produces the name that is used to register the introduction
+ − 1028
rules. It is costum to collect all introduction rules under
+ − 1029
@{text "string.intros"}, whereby @{text "string"} stands for the
+ − 1030
@{text [quotes] "_"}-separated list of predicate names (for example
+ − 1031
@{text "even_odd"}. Also by custom, the case names in intuction
+ − 1032
proofs correspond to the names of the introduction rules. These
+ − 1033
are generated in Line 16.
+ − 1034
237
+ − 1035
Lines 18 and 19 now add to @{text "lthy'"} all the introduction rules
+ − 1036
und induction principles under the name @{text "mut_name.intros"} and
+ − 1037
@{text "mut_name.inducts"}, respectively (see previous paragraph).
+ − 1038
+ − 1039
Line 20 add further every introduction rule under its own name
215
+ − 1040
(given by the user).\footnote{FIXME: what happens if the user did not give
237
+ − 1041
any name.} Line 21 registers the induction principles. For this we have
316
+ − 1042
to use some specific attributes. The first @{ML_ind case_names in RuleCases}
215
+ − 1043
corresponds to the case names that are used by Isar to reference the proof
+ − 1044
obligations in the induction. The second @{ML "consumes 1" in RuleCases}
+ − 1045
indicates that the first premise of the induction principle (namely
+ − 1046
the predicate over which the induction proceeds) is eliminated.
+ − 1047
+ − 1048
This completes all the code and fits in with the ``front end'' described
237
+ − 1049
in Section~\ref{sec:interface}.\footnote{FIXME: Describe @{ML Induct.induct_pred}.
+ − 1050
Why the mut-name?
224
+ − 1051
What does @{ML Binding.qualify} do?}
124
+ − 1052
*}
219
+ − 1053
(*<*)end(*>*)