210
|
1 |
theory Ind_General_Scheme
|
|
2 |
imports Simple_Inductive_Package Ind_Prelims
|
32
|
3 |
begin
|
|
4 |
|
210
|
5 |
(*<*)
|
|
6 |
datatype trm =
|
|
7 |
Var "string"
|
|
8 |
| App "trm" "trm"
|
|
9 |
| Lam "string" "trm"
|
|
10 |
|
|
11 |
simple_inductive
|
|
12 |
fresh :: "string \<Rightarrow> trm \<Rightarrow> bool"
|
|
13 |
where
|
|
14 |
fresh_var: "a\<noteq>b \<Longrightarrow> fresh a (Var b)"
|
|
15 |
| fresh_app: "\<lbrakk>fresh a t; fresh a s\<rbrakk> \<Longrightarrow> fresh a (App t s)"
|
|
16 |
| fresh_lam1: "fresh a (Lam a t)"
|
|
17 |
| fresh_lam2: "\<lbrakk>a\<noteq>b; fresh a t\<rbrakk> \<Longrightarrow> fresh a (Lam b t)"
|
|
18 |
(*>*)
|
|
19 |
|
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
|
20 |
section {* The Code in a Nutshell\label{sec:nutshell} *}
|
32
|
21 |
|
|
22 |
text {*
|
210
|
23 |
(FIXME: perhaps move somewhere else)
|
|
24 |
|
127
|
25 |
The point of these examples is to get a feeling what the automatic proofs
|
|
26 |
should do in order to solve all inductive definitions we throw at them. For this
|
|
27 |
it is instructive to look at the general construction principle
|
|
28 |
of inductive definitions, which we shall do in the next section.
|
32
|
29 |
*}
|
|
30 |
|
210
|
31 |
text {*
|
|
32 |
The inductive package will generate the reasoning infrastructure
|
|
33 |
for mutually recursive predicates @{text "pred\<^isub>1\<dots>pred\<^isub>n"}. In what
|
|
34 |
follows we will have the convention that various, possibly empty collections of
|
|
35 |
``things'' are indicated either by adding an @{text "s"} or by adding
|
|
36 |
a superscript @{text [quotes] "\<^isup>*"}. The shorthand for the predicates
|
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
|
37 |
will therefore be @{text "preds"} or @{text "pred\<^sup>*"}. In the case 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
|
38 |
predicates there must be, of course, at least a single one in order to obtain a
|
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
|
39 |
meaningful definition.
|
210
|
40 |
|
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
|
41 |
The input for the inductive package will be some @{text "preds"} with possible
|
210
|
42 |
typing and syntax annotations, and also some introduction rules. We call below the
|
|
43 |
introduction rules short as @{text "rules"}. Borrowing some idealised Isabelle
|
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
|
44 |
notation, one such @{text "rule"} is assumed to be of the form
|
210
|
45 |
|
|
46 |
\begin{isabelle}
|
|
47 |
@{text "rule ::=
|
|
48 |
\<And>xs. \<^raw:$\underbrace{\mbox{>As\<^raw:}}_{\text{\makebox[0mm]{\rm non-recursive premises}}}$> \<Longrightarrow>
|
|
49 |
\<^raw:$\underbrace{\mbox{>(\<And>ys. Bs \<Longrightarrow> pred ss)\<^isup>*\<^raw:}}_{\text{\rm recursive premises}}$>
|
|
50 |
\<Longrightarrow> pred ts"}
|
|
51 |
\end{isabelle}
|
|
52 |
|
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
|
53 |
For the purposes here, we will assume the @{text rules} have
|
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
|
54 |
this format and omit any code that actually tests this. Therefore ``things''
|
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
|
55 |
can go horribly wrong, if the @{text "rules"} are not of this
|
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
|
56 |
form.\footnote{FIXME: Exercise to test this format.} The @{text As} and
|
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
|
57 |
@{text Bs} in a @{text "rule"} are formulae not involving the inductive
|
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
|
58 |
predicates @{text "preds"}; the instances @{text "pred ss"} and @{text "pred
|
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 |
ts"} can stand for different predicates, like @{text "pred\<^isub>1 ss"} and
|
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
|
60 |
@{text "pred\<^isub>2 ts"}; @{text ss} and @{text ts} are 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
|
61 |
arguments of the predicates. Every formula left of @{text [quotes]
|
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
|
62 |
"\<Longrightarrow> pred ts"} is a premise of the rule. The outermost quantified
|
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
|
63 |
variables @{text "xs"} are usually omitted in the user's input. 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
|
64 |
quantification for the variables @{text "ys"} is local with respect to
|
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
|
65 |
one recursive premise and must be given. Some examples of @{text "rule"}s
|
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
|
66 |
are
|
210
|
67 |
|
|
68 |
@{thm [display] fresh_var[no_vars]}
|
|
69 |
|
|
70 |
which has only a single non-recursive premise, whereas
|
|
71 |
|
|
72 |
@{thm [display] evenS[no_vars]}
|
|
73 |
|
|
74 |
has a single recursive premise; the rule
|
|
75 |
|
|
76 |
@{thm [display] accpartI[no_vars]}
|
|
77 |
|
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
|
78 |
has a recursive premise that has a precondition. As usual all
|
210
|
79 |
rules are stated without the leading meta-quantification @{text "\<And>xs"}.
|
|
80 |
|
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
|
81 |
The code of the inductive package falls roughly in tree parts: 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
|
82 |
deals with the definitions, the second with the induction principles and
|
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
|
83 |
the third with the introduction rules.
|
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
|
84 |
|
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
|
85 |
For the definitions we need to have the @{text rules} in a form where
|
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
|
86 |
the meta-quantifiers and meta-implications are replaced by their object
|
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
|
87 |
logic equivalents. Therefore an @{text "orule"} is of the form
|
210
|
88 |
|
|
89 |
@{text [display] "orule ::= \<forall>xs. As \<longrightarrow> (\<forall>ys. Bs \<longrightarrow> pred ss)\<^isup>* \<longrightarrow> pred ts"}
|
|
90 |
|
|
91 |
A definition for the predicate @{text "pred"} has then the form
|
|
92 |
|
|
93 |
@{text [display] "def ::= pred \<equiv> \<lambda>zs. \<forall>preds. orules \<longrightarrow> pred zs"}
|
|
94 |
|
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
|
95 |
The induction principles for every predicate @{text "pred"} are of the
|
210
|
96 |
form
|
|
97 |
|
|
98 |
@{text [display] "ind ::= pred ?zs \<Longrightarrow> rules[preds := ?Ps] \<Longrightarrow> ?P ?zs"}
|
|
99 |
|
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
|
100 |
where in the @{text "rules"} every @{text pred} is replaced by a fresh
|
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 |
meta-variable @{text "?P"}.
|
210
|
102 |
|
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
|
103 |
In order to derive an induction principle for the predicate @{text "pred"},
|
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
|
104 |
we first transform @{text ind} into the object logic and fix the meta-variables.
|
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
|
105 |
Hence we have to prove a formula of the form
|
210
|
106 |
|
|
107 |
@{text [display] "pred zs \<longrightarrow> orules[preds := Ps] \<longrightarrow> P zs"}
|
|
108 |
|
|
109 |
If we assume @{text "pred zs"} and unfold its definition, then we have
|
|
110 |
|
|
111 |
@{text [display] "\<forall>preds. orules \<longrightarrow> pred zs"}
|
|
112 |
|
|
113 |
and must prove
|
|
114 |
|
|
115 |
@{text [display] "orules[preds := Ps] \<longrightarrow> P zs"}
|
|
116 |
|
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
|
117 |
This can be done by instantiating the @{text "\<forall>preds"}-quantification
|
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 |
with the @{text "Ps"}. Then we are done since we are left with a simple
|
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
|
119 |
identity.
|
210
|
120 |
|
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
|
121 |
Although the user declares introduction rules @{text rules}, they must
|
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
|
122 |
be derived from the @{text defs}. These derivations are a bit involved.
|
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
|
123 |
Assuming we want to prove the introduction rule
|
210
|
124 |
|
|
125 |
@{text [display] "\<And>xs. As \<Longrightarrow> (\<And>ys. Bs \<Longrightarrow> pred ss)\<^isup>* \<Longrightarrow> pred ts"}
|
|
126 |
|
|
127 |
then we can assume
|
|
128 |
|
|
129 |
\begin{isabelle}
|
|
130 |
(i)~~@{text "As"}\\
|
|
131 |
(ii)~@{text "(\<And>ys. Bs \<Longrightarrow> pred ss)\<^isup>*"}
|
|
132 |
\end{isabelle}
|
|
133 |
|
|
134 |
and must show
|
|
135 |
|
|
136 |
@{text [display] "pred ts"}
|
|
137 |
|
|
138 |
If we now unfold the definitions for the @{text preds}, we have
|
|
139 |
|
|
140 |
\begin{isabelle}
|
|
141 |
(i)~~~@{text "As"}\\
|
|
142 |
(ii)~~@{text "(\<And>ys. Bs \<Longrightarrow> \<forall>preds. orules \<longrightarrow> pred ss)\<^isup>*"}\\
|
|
143 |
(iii)~@{text "orules"}
|
|
144 |
\end{isabelle}
|
|
145 |
|
|
146 |
and need to show
|
|
147 |
|
|
148 |
@{text [display] "pred ts"}
|
|
149 |
|
|
150 |
In the last step we removed some quantifiers and moved the precondition @{text "orules"}
|
|
151 |
into the assumtion. The @{text "orules"} stand for all introduction rules that are given
|
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
|
152 |
by the user. We apply the @{text orule} that corresponds to introduction rule we are
|
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
|
153 |
proving. This introduction rule must necessarily be of the form
|
210
|
154 |
|
|
155 |
@{text [display] "As \<Longrightarrow> (\<And>ys. Bs \<Longrightarrow> pred ss)\<^isup>* \<Longrightarrow> pred ts"}
|
|
156 |
|
|
157 |
When we apply this rule we end up in the goal state where we have to prove
|
|
158 |
|
|
159 |
\begin{isabelle}
|
|
160 |
(a)~@{text "As"}\\
|
|
161 |
(b)~@{text "(\<And>ys. Bs \<Longrightarrow> pred ss)\<^isup>*"}
|
|
162 |
\end{isabelle}
|
|
163 |
|
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
|
164 |
We can immediately discharge the goals @{text "As"} using the assumption in
|
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
|
165 |
@{text "(i)"}. The goals in @{text "(b)"} can be discharged as follows: we
|
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
|
166 |
assume the @{text "Bs"} and prove @{text "pred ss"}. For this we resolve 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
|
167 |
@{text "Bs"} with the assumptions in @{text "(ii)"}. This gives us 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
|
168 |
assumptions
|
210
|
169 |
|
|
170 |
@{text [display] "(\<forall>preds. orules \<longrightarrow> pred ss)\<^isup>*"}
|
|
171 |
|
|
172 |
Instantiating the universal quantifiers and then resolving with the assumptions
|
|
173 |
in @{text "(iii)"} gives us @{text "pred ss"}, which is the goal we are after.
|
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
|
174 |
This completes the proof for introduction rules.
|
210
|
175 |
|
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
|
176 |
What remains is to implement the reasoning outlined above. We do this in
|
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
|
177 |
the next section. For building testcases, we use the shorthands for
|
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
|
178 |
@{text "even/odd"}, @{term "fresh"} and @{term "accpart"}
|
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
|
179 |
given in Figure~\ref{fig:shorthands}.
|
210
|
180 |
*}
|
|
181 |
|
|
182 |
|
|
183 |
text_raw{*
|
|
184 |
\begin{figure}[p]
|
|
185 |
\begin{minipage}{\textwidth}
|
|
186 |
\begin{isabelle}*}
|
|
187 |
ML{*(* even-odd example *)
|
|
188 |
val eo_defs = [@{thm even_def}, @{thm odd_def}]
|
|
189 |
|
|
190 |
val eo_rules =
|
|
191 |
[@{prop "even 0"},
|
|
192 |
@{prop "\<And>n. odd n \<Longrightarrow> even (Suc n)"},
|
|
193 |
@{prop "\<And>n. even n \<Longrightarrow> odd (Suc n)"}]
|
|
194 |
|
|
195 |
val eo_orules =
|
|
196 |
[@{prop "even 0"},
|
|
197 |
@{prop "\<forall>n. odd n \<longrightarrow> even (Suc n)"},
|
|
198 |
@{prop "\<forall>n. even n \<longrightarrow> odd (Suc n)"}]
|
|
199 |
|
|
200 |
val eo_preds = [@{term "even::nat\<Rightarrow>bool"}, @{term "odd::nat\<Rightarrow>bool"}]
|
|
201 |
val eo_prednames = [@{binding "even"}, @{binding "odd"}]
|
|
202 |
val eo_syns = [NoSyn, NoSyn]
|
|
203 |
val eo_arg_tyss = [[@{typ "nat"}], [@{typ "nat"}]]
|
|
204 |
val e_pred = @{term "even::nat\<Rightarrow>bool"}
|
|
205 |
val e_arg_tys = [@{typ "nat"}]
|
|
206 |
|
|
207 |
|
|
208 |
|
|
209 |
(* freshness example *)
|
|
210 |
val fresh_rules =
|
|
211 |
[@{prop "\<And>a b. a \<noteq> b \<Longrightarrow> fresh a (Var b)"},
|
|
212 |
@{prop "\<And>a s t. fresh a t \<Longrightarrow> fresh a s \<Longrightarrow> fresh a (App t s)"},
|
|
213 |
@{prop "\<And>a t. fresh a (Lam a t)"},
|
|
214 |
@{prop "\<And>a b t. a \<noteq> b \<Longrightarrow> fresh a t \<Longrightarrow> fresh a (Lam b t)"}]
|
|
215 |
|
|
216 |
val fresh_orules =
|
|
217 |
[@{prop "\<forall>a b. a \<noteq> b \<longrightarrow> fresh a (Var b)"},
|
|
218 |
@{prop "\<forall>a s t. fresh a t \<longrightarrow> fresh a s \<longrightarrow> fresh a (App t s)"},
|
|
219 |
@{prop "\<forall>a t. fresh a (Lam a t)"},
|
|
220 |
@{prop "\<forall>a b t. a \<noteq> b \<longrightarrow> fresh a t \<longrightarrow> fresh a (Lam b t)"}]
|
|
221 |
|
|
222 |
val fresh_pred = @{term "fresh::string\<Rightarrow>trm\<Rightarrow>bool"}
|
|
223 |
val fresh_arg_tys = [@{typ "string"}, @{typ "trm"}]
|
|
224 |
|
|
225 |
|
|
226 |
|
|
227 |
(* accessible-part example *)
|
|
228 |
val acc_rules =
|
|
229 |
[@{prop "\<And>R x. (\<And>y. R y x \<Longrightarrow> accpart R y) \<Longrightarrow> accpart R x"}]
|
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
|
230 |
val acc_pred = @{term "accpart::('a \<Rightarrow>'a\<Rightarrow>bool)\<Rightarrow>'a \<Rightarrow>bool"}*}
|
210
|
231 |
text_raw{*
|
|
232 |
\end{isabelle}
|
|
233 |
\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
|
234 |
\caption{Shorthands for the inductive predicates @{text "even"}-@{text "odd"},
|
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
|
235 |
@{text "fresh"} and @{text "accpart"}. The names of these shorthands follow
|
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
|
236 |
the convention @{text "rules"}, @{text "orules"}, @{text "preds"} and so on.
|
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
|
237 |
The purpose of these shorthands is to simplify the construction of testcases
|
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
|
238 |
in Section~\ref{sec:code}.\label{fig:shorthands}}
|
210
|
239 |
\end{figure}
|
|
240 |
*}
|
|
241 |
|
|
242 |
|
|
243 |
|
32
|
244 |
end
|