3 begin |
3 begin |
4 |
4 |
5 section{* Preliminaries *} |
5 section{* Preliminaries *} |
6 |
6 |
7 text {* |
7 text {* |
8 The user will just give a specification of an inductive predicate and |
8 The user will just give a specification of inductive predicate(s) and |
9 expects from the package to produce a convenient reasoning |
9 expects from the package to produce a convenient reasoning |
10 infrastructure. This infrastructure needs to be derived from the definition |
10 infrastructure. This infrastructure needs to be derived from the definition |
11 that correspond to the specified predicate. This will roughly mean that the |
11 that correspond to the specified predicate(s). Before we start with |
12 package has three main parts, namely: |
12 explaining all parts of the package, let us first give four examples showing |
13 |
13 how to define inductive predicates by hand and then also how to prove by |
14 |
14 hand properties about them. See Figure \ref{fig:paperpreds} for their usual |
15 \begin{itemize} |
15 ``pencil-and-paper'' definitions. From these examples, we will |
16 \item parsing the specification and typing the parsed input, |
|
17 \item making the definitions and deriving the reasoning infrastructure, and |
|
18 \item storing the results in the theory. |
|
19 \end{itemize} |
|
20 |
|
21 Before we start with explaining all parts, let us first give three examples |
|
22 showing how to define inductive predicates by hand and then also how to |
|
23 prove by hand important properties about them. From these examples, we will |
|
24 figure out a general method for defining inductive predicates. The aim in |
16 figure out a general method for defining inductive predicates. The aim in |
25 this section is \emph{not} to write proofs that are as beautiful as |
17 this section is \emph{not} to write proofs that are as beautiful as |
26 possible, but as close as possible to the ML-code we will develop in later |
18 possible, but as close as possible to the ML-code we will develop in later |
27 sections. |
19 sections. |
28 |
20 |
29 |
21 \begin{figure}[t] |
30 We first consider the transitive closure of a relation @{text R}. It is |
22 \begin{boxedminipage}{\textwidth} |
31 an inductive predicate characterised by the two introduction rules: |
|
32 |
|
33 \begin{center}\small |
23 \begin{center}\small |
34 @{prop[mode=Axiom] "trcl R x x"} \hspace{5mm} |
24 @{prop[mode=Axiom] "trcl R x x"} \hspace{5mm} |
35 @{prop[mode=Rule] "R x y \<Longrightarrow> trcl R y z \<Longrightarrow> trcl R x z"} |
25 @{prop[mode=Rule] "R x y \<Longrightarrow> trcl R y z \<Longrightarrow> trcl R x z"} |
36 \end{center} |
26 \end{center} |
37 |
27 \begin{center}\small |
38 In Isabelle, the user will state for @{term trcl\<iota>} the specification: |
28 @{prop[mode=Axiom] "even (0::nat)"} \hspace{5mm} |
|
29 @{prop[mode=Rule] "odd n \<Longrightarrow> even (Suc n)"} \hspace{5mm} |
|
30 @{prop[mode=Rule] "even n \<Longrightarrow> odd (Suc n)"} |
|
31 \end{center} |
|
32 \begin{center}\small |
|
33 \mbox{\inferrule{@{term "\<And>y. R y x \<Longrightarrow> accpart R y"}}{@{term "accpart R x"}}} |
|
34 \end{center} |
|
35 \begin{center}\small |
|
36 @{prop[mode=Rule] "a\<noteq>b \<Longrightarrow> fresh a (Var b)"}\hspace{5mm} |
|
37 @{prop[mode=Rule] "\<lbrakk>fresh a t; fresh a s\<rbrakk> \<Longrightarrow> fresh a (App t s)"}\\[2mm] |
|
38 @{prop[mode=Axiom] "fresh a (Lam a t)"}\hspace{5mm} |
|
39 @{prop[mode=Rule] "\<lbrakk>a\<noteq>b; fresh a t\<rbrakk> \<Longrightarrow> fresh a (Lam b t)"} |
|
40 \end{center} |
|
41 \end{boxedminipage} |
|
42 \caption{Examples of four ``Pencil-and-paper'' definitions of inductively defined |
|
43 predicates. In formal reasoning with Isabelle, the user just wants to give such |
|
44 definitions and expects that the reasoning structure is derived automatically. |
|
45 For this definitional packages need to be implemented.\label{fig:paperpreds}} |
|
46 \end{figure} |
|
47 |
|
48 We first consider the transitive closure of a relation @{text R}. The user will |
|
49 state for @{term trcl\<iota>} the specification: |
39 *} |
50 *} |
40 |
51 |
41 simple_inductive |
52 simple_inductive |
42 trcl\<iota> :: "('a \<Rightarrow> 'a \<Rightarrow> bool) \<Rightarrow> 'a \<Rightarrow> 'a \<Rightarrow> bool" |
53 trcl\<iota> :: "('a \<Rightarrow> 'a \<Rightarrow> bool) \<Rightarrow> 'a \<Rightarrow> 'a \<Rightarrow> bool" |
43 where |
54 where |
44 base: "trcl\<iota> R x x" |
55 base: "trcl\<iota> R x x" |
45 | step: "trcl\<iota> R x y \<Longrightarrow> R y z \<Longrightarrow> trcl\<iota> R x z" |
56 | step: "trcl\<iota> R x y \<Longrightarrow> R y z \<Longrightarrow> trcl\<iota> R x z" |
46 |
57 |
47 text {* |
58 text {* |
48 As said above the package has to make an appropriate definition and provide |
59 The package has to make an appropriate definition and provide |
49 lemmas to reason about the predicate @{term trcl\<iota>}. Since an inductively |
60 lemmas to reason about the predicate @{term trcl\<iota>}. Since an inductively |
50 defined predicate is the least predicate closed under a collection of |
61 defined predicate is the least predicate closed under a collection of |
51 introduction rules, the predicate @{text "trcl R x y"} can be defined so |
62 introduction rules, the predicate @{text "trcl R x y"} can be defined so |
52 that it holds if and only if @{text "P x y"} holds for every predicate |
63 that it holds if and only if @{text "P x y"} holds for every predicate |
53 @{text P} closed under the rules above. This gives rise to the definition |
64 @{text P} closed under the rules above. This gives rise to the definition |
56 definition "trcl \<equiv> |
67 definition "trcl \<equiv> |
57 \<lambda>R x y. \<forall>P. (\<forall>x. P x x) |
68 \<lambda>R x y. \<forall>P. (\<forall>x. P x x) |
58 \<longrightarrow> (\<forall>x y z. R x y \<longrightarrow> P y z \<longrightarrow> P x z) \<longrightarrow> P x y" |
69 \<longrightarrow> (\<forall>x y z. R x y \<longrightarrow> P y z \<longrightarrow> P x z) \<longrightarrow> P x y" |
59 |
70 |
60 text {* |
71 text {* |
61 where we quantify over the predicate @{text P}. We have to use the |
72 We have to use the object implication @{text "\<longrightarrow>"} and object quantification |
62 object implication @{text "\<longrightarrow>"} and object quantification @{text "\<forall>"} for |
73 @{text "\<forall>"} for stating this definition (there is no other way for |
63 stating this definition (there is no other way for definitions in |
74 definitions in HOL). However, the introduction rules and induction |
64 HOL). However, the introduction rules and induction principles |
75 principles associated with the transitive closure should use the meta-connectives, |
65 should use the meta-connectives since they simplify the |
76 since they simplify the reasoning for the user. |
66 reasoning for the user. |
77 |
67 |
78 |
68 With this definition, the proof of the induction principle for @{term trcl} |
79 With this definition, the proof of the induction principle for @{term trcl} |
69 is almost immediate. It suffices to convert all the meta-level |
80 is almost immediate. It suffices to convert all the meta-level |
70 connectives in the lemma to object-level connectives using the |
81 connectives in the lemma to object-level connectives using the |
71 proof method @{text atomize} (Line 4), expand the definition of @{term trcl} |
82 proof method @{text atomize} (Line 4), expand the definition of @{term trcl} |
72 (Line 5 and 6), eliminate the universal quantifier contained in it (Line~7), |
83 (Line 5 and 6), eliminate the universal quantifier contained in it (Line~7), |
73 and then solve the goal by assumption (Line 8). |
84 and then solve the goal by @{text assumption} (Line 8). |
74 |
85 |
75 *} |
86 *} |
76 |
87 |
77 lemma %linenos trcl_induct: |
88 lemma %linenos trcl_induct: |
78 assumes "trcl R x y" |
89 assumes "trcl R x y" |
79 shows "(\<And>x. P x x) \<Longrightarrow> (\<And>x y z. R x y \<Longrightarrow> P y z \<Longrightarrow> P x z) \<Longrightarrow> P x y" |
90 shows "(\<And>x. P x x) \<Longrightarrow> (\<And>x y z. R x y \<Longrightarrow> P y z \<Longrightarrow> P x z) \<Longrightarrow> P x y" |
80 apply(atomize (full)) |
91 apply(atomize (full)) |
81 apply(cut_tac prems) |
92 apply(cut_tac prems) |
82 apply(unfold trcl_def) |
93 apply(unfold trcl_def) |
83 apply(drule spec[where x=P]) |
94 apply(drule spec[where x=P]) |
84 apply(assumption) |
95 apply(assumption) |
108 apply (rule allI impI)+(*>*) |
119 apply (rule allI impI)+(*>*) |
109 txt {* @{subgoals [display]} *} |
120 txt {* @{subgoals [display]} *} |
110 (*<*)oops(*>*) |
121 (*<*)oops(*>*) |
111 |
122 |
112 text {* |
123 text {* |
113 The two assumptions correspond to the introduction rules. Thus, all we have |
124 The two assumptions come from the definition of @{term trcl} and correspond |
114 to do is to eliminate the universal quantifier in front of the first |
125 to the introduction rules. Thus, all we have to do is to eliminate the |
115 assumption (Line 5), and then solve the goal by assumption (Line 6). |
126 universal quantifier in front of the first assumption (Line 5), and then |
|
127 solve the goal by assumption (Line 6). |
116 *} |
128 *} |
117 |
129 |
118 text {* |
130 text {* |
119 Next we have to show that the second introduction rule also follows from the |
131 Next we have to show that the second introduction rule also follows from the |
120 definition. Since this rule has premises, the proof is a bit more |
132 definition. Since this rule has premises, the proof is a bit more |
121 involved. After unfolding the definitions and applying the introduction |
133 involved. After unfolding the definitions and applying the introduction |
122 rules for @{text "\<forall>"} and @{text "\<longrightarrow>"} |
134 rules for @{text "\<forall>"} and @{text "\<longrightarrow>"} |
123 *} |
135 *} |
124 |
136 |
125 lemma trcl_step: |
137 lemma trcl_step: |
126 shows "R x y \<Longrightarrow> trcl R y z \<Longrightarrow> trcl R x z" |
138 shows "R x y \<Longrightarrow> trcl R y z \<Longrightarrow> trcl R x z" |
127 apply (unfold trcl_def) |
139 apply (unfold trcl_def) |
128 apply (rule allI impI)+ |
140 apply (rule allI impI)+ |
129 |
141 |
130 txt {* |
142 txt {* |
131 we obtain the goal state |
143 we obtain the goal state |
145 have r2: "\<forall>x y z. R x y \<longrightarrow> P y z \<longrightarrow> P x z" by fact |
157 have r2: "\<forall>x y z. R x y \<longrightarrow> P y z \<longrightarrow> P x z" by fact |
146 show "P x z" |
158 show "P x z" |
147 |
159 |
148 txt {* |
160 txt {* |
149 The assumptions @{text "p1"} and @{text "p2"} correspond to the premises of |
161 The assumptions @{text "p1"} and @{text "p2"} correspond to the premises of |
150 the second introduction rule; the assumptions @{text "r1"} and @{text "r2"} |
162 the second introduction rule (unfolded); the assumptions @{text "r1"} and @{text "r2"} |
151 correspond to the introduction rules. We apply @{text "r2"} to the goal |
163 come from the definition of @{term trcl} . We apply @{text "r2"} to the goal |
152 @{term "P x z"}. In order for the assumption to be applicable as a rule, we |
164 @{term "P x z"}. In order for the assumption to be applicable as a rule, we |
153 have to eliminate the universal quantifier and turn the object-level |
165 have to eliminate the universal quantifier and turn the object-level |
154 implications into meta-level ones. This can be accomplished using the @{text |
166 implications into meta-level ones. This can be accomplished using the @{text |
155 rule_format} attribute. So we continue the proof with: |
167 rule_format} attribute. So we continue the proof with: |
156 |
168 |
193 @{text blast}, @{text auto} and the like in automated proofs. The reason is |
205 @{text blast}, @{text auto} and the like in automated proofs. The reason is |
194 that you do not have precise control over them (the user can, for example, |
206 that you do not have precise control over them (the user can, for example, |
195 declare new intro- or simplification rules that can throw automatic tactics |
207 declare new intro- or simplification rules that can throw automatic tactics |
196 off course) and also it is very hard to debug proofs involving automatic |
208 off course) and also it is very hard to debug proofs involving automatic |
197 tactics whenever something goes wrong. Therefore if possible, automatic |
209 tactics whenever something goes wrong. Therefore if possible, automatic |
198 tactics should be avoided or sufficiently constrained. |
210 tactics should be avoided or be constrained sufficiently. |
199 |
211 |
200 The method of defining inductive predicates by impredicative quantification |
212 The method of defining inductive predicates by impredicative quantification |
201 also generalises to mutually inductive predicates. The next example defines |
213 also generalises to mutually inductive predicates. The next example defines |
202 the predicates @{text even} and @{text odd} characterised by the following |
214 the predicates @{text even} and @{text odd}. The user will state for this |
203 rules: |
215 inductive definition the specification: |
204 |
|
205 \begin{center}\small |
|
206 @{prop[mode=Axiom] "even (0::nat)"} \hspace{5mm} |
|
207 @{prop[mode=Rule] "odd n \<Longrightarrow> even (Suc n)"} \hspace{5mm} |
|
208 @{prop[mode=Rule] "even n \<Longrightarrow> odd (Suc n)"} |
|
209 \end{center} |
|
210 |
|
211 The user will state for this inductive definition the specification: |
|
212 *} |
216 *} |
213 |
217 |
214 simple_inductive |
218 simple_inductive |
215 even and odd |
219 even and odd |
216 where |
220 where |
236 For proving the induction principles, we use exactly the same technique |
240 For proving the induction principles, we use exactly the same technique |
237 as in the transitive closure example, namely: |
241 as in the transitive closure example, namely: |
238 *} |
242 *} |
239 |
243 |
240 lemma even_induct: |
244 lemma even_induct: |
241 assumes "even n" |
245 assumes "even n" |
242 shows "P 0 \<Longrightarrow> |
246 shows "P 0 \<Longrightarrow> (\<And>m. Q m \<Longrightarrow> P (Suc m)) \<Longrightarrow> (\<And>m. P m \<Longrightarrow> Q (Suc m)) \<Longrightarrow> P n" |
243 (\<And>m. Q m \<Longrightarrow> P (Suc m)) \<Longrightarrow> (\<And>m. P m \<Longrightarrow> Q (Suc m)) \<Longrightarrow> P n" |
|
244 apply(atomize (full)) |
247 apply(atomize (full)) |
245 apply(cut_tac prems) |
248 apply(cut_tac prems) |
246 apply(unfold even_def) |
249 apply(unfold even_def) |
247 apply(drule spec[where x=P]) |
250 apply(drule spec[where x=P]) |
248 apply(drule spec[where x=Q]) |
251 apply(drule spec[where x=Q]) |
250 done |
253 done |
251 |
254 |
252 text {* |
255 text {* |
253 The only difference with the proof @{text "trcl_induct"} is that we have to |
256 The only difference with the proof @{text "trcl_induct"} is that we have to |
254 instantiate here two universal quantifiers. We omit the other induction |
257 instantiate here two universal quantifiers. We omit the other induction |
255 principle that has @{term "Q n"} as conclusion. The proofs of the |
258 principle that has @{prop "even n"} as premise and @{term "Q n"} as conclusion. |
256 introduction rules are also very similar to the ones in the @{text |
259 The proofs of the introduction rules are also very similar to the ones in |
257 "trcl"}-example. We only show the proof of the second introduction rule. |
260 the @{text "trcl"}-example. We only show the proof of the second introduction |
258 |
261 rule. |
259 *} |
262 *} |
260 |
263 |
261 lemma %linenos evenS: |
264 lemma %linenos evenS: |
262 shows "odd m \<Longrightarrow> even (Suc m)" |
265 shows "odd m \<Longrightarrow> even (Suc m)" |
263 apply (unfold odd_def even_def) |
266 apply (unfold odd_def even_def) |
264 apply (rule allI impI)+ |
267 apply (rule allI impI)+ |
265 proof - |
268 proof - |
266 case (goal1 P Q) |
269 case (goal1 P Q) |
267 have p1: "\<forall>P Q. P 0 \<longrightarrow> (\<forall>m. Q m \<longrightarrow> P (Suc m)) |
270 have p1: "\<forall>P Q. P 0 \<longrightarrow> (\<forall>m. Q m \<longrightarrow> P (Suc m)) |
275 THEN mp, THEN mp, THEN mp, OF r1, OF r2, OF r3]) |
278 THEN mp, THEN mp, THEN mp, OF r1, OF r2, OF r3]) |
276 done |
279 done |
277 qed |
280 qed |
278 |
281 |
279 text {* |
282 text {* |
|
283 The interesting lines are 7 to 15. The assumptions fall into to categories: |
|
284 @{text p1} corresponds to the premise of the introduction rule; @{text "r1"} |
|
285 to @{text "r3"} come from the definition of @{text "even"}. |
280 In Line 13, we apply the assumption @{text "r2"} (since we prove the second |
286 In Line 13, we apply the assumption @{text "r2"} (since we prove the second |
281 introduction rule). In Lines 14 and 15 we apply assumption @{text "p1"} (if |
287 introduction rule). In Lines 14 and 15 we apply assumption @{text "p1"} (if |
282 the second introduction rule had more premises we have to do that for all |
288 the second introduction rule had more premises we have to do that for all |
283 of them). In order for this assumption to be applicable, the quantifiers |
289 of them). In order for this assumption to be applicable, the quantifiers |
284 need to be instantiated and then also the implications need to be resolved |
290 need to be instantiated and then also the implications need to be resolved |
285 with the other rules. |
291 with the other rules. |
286 |
292 |
287 |
293 As a final example, we define the accessible part of a relation @{text R} |
288 As a final example, we define the accessible part of a relation @{text R} characterised |
294 (see Figure~\ref{fig:paperpreds}). There the premsise of the introduction |
289 by the introduction rule |
295 rule involves a universal quantifier and an implication. The |
290 |
|
291 \begin{center}\small |
|
292 \mbox{\inferrule{@{term "\<And>y. R y x \<Longrightarrow> accpart R y"}}{@{term "accpart R x"}}} |
|
293 \end{center} |
|
294 |
|
295 whose premise involves a universal quantifier and an implication. The |
|
296 definition of @{text accpart} is: |
296 definition of @{text accpart} is: |
297 *} |
297 *} |
298 |
298 |
299 definition "accpart \<equiv> \<lambda>R x. \<forall>P. (\<forall>x. (\<forall>y. R y x \<longrightarrow> P y) \<longrightarrow> P x) \<longrightarrow> P x" |
299 definition "accpart \<equiv> \<lambda>R x. \<forall>P. (\<forall>x. (\<forall>y. R y x \<longrightarrow> P y) \<longrightarrow> P x) \<longrightarrow> P x" |
300 |
300 |
301 text {* |
301 text {* |
302 The proof of the induction principle is again straightforward. |
302 The proof of the induction principle is again straightforward and omitted. |
303 *} |
303 Proving the introduction rule is a little more complicated, because the |
304 |
304 quantifier and the implication in the premise. The proof is as follows. |
305 lemma accpart_induct: |
|
306 assumes "accpart R x" |
|
307 shows "(\<And>x. (\<And>y. R y x \<Longrightarrow> P y) \<Longrightarrow> P x) \<Longrightarrow> P x" |
|
308 apply(atomize (full)) |
|
309 apply(cut_tac prems) |
|
310 apply(unfold accpart_def) |
|
311 apply(drule spec[where x=P]) |
|
312 apply(assumption) |
|
313 done |
|
314 |
|
315 text {* |
|
316 Proving the introduction rule is a little more complicated, because the quantifier |
|
317 and the implication in the premise. The proof is as follows. |
|
318 *} |
305 *} |
319 |
306 |
320 lemma %linenos accpartI: |
307 lemma %linenos accpartI: |
321 shows "(\<And>y. R y x \<Longrightarrow> accpart R y) \<Longrightarrow> accpart R x" |
308 shows "(\<And>y. R y x \<Longrightarrow> accpart R y) \<Longrightarrow> accpart R x" |
322 apply (unfold accpart_def) |
309 apply (unfold accpart_def) |
323 apply (rule allI impI)+ |
310 apply (rule allI impI)+ |
324 proof - |
311 proof - |
325 case (goal1 P) |
312 case (goal1 P) |
326 have p1: "\<And>y. R y x \<Longrightarrow> |
313 have p1: "\<And>y. R y x \<Longrightarrow> |
336 done |
323 done |
337 qed |
324 qed |
338 qed |
325 qed |
339 |
326 |
340 text {* |
327 text {* |
341 In Line 11, applying the assumption @{text "r1"} generates a goal state with |
328 There are now two subproofs. The assumptions fall again into two categories (Lines |
342 the new local assumption @{term "R y x"}, named @{text "r1_prem"} in the |
329 7 to 9). In Line 11, applying the assumption @{text "r1"} generates a goal state |
343 proof above (Line 14). This local assumption is used to solve |
330 with the new local assumption @{term "R y x"}, named @{text "r1_prem"} in the |
|
331 proof (Line 14). This local assumption is used to solve |
344 the goal @{term "P y"} with the help of assumption @{text "p1"}. |
332 the goal @{term "P y"} with the help of assumption @{text "p1"}. |
345 |
333 |
346 The point of these examples is to get a feeling what the automatic proofs |
334 The point of these examples is to get a feeling what the automatic proofs |
347 should do in order to solve all inductive definitions we throw at them. |
335 should do in order to solve all inductive definitions we throw at them. |
348 This is usually the first step in writing a package. We next explain |
336 This is usually the first step in writing a package. We next explain |