-
Notifications
You must be signed in to change notification settings - Fork 1
/
Isar_Demo.thy
264 lines (206 loc) · 5.62 KB
/
Isar_Demo.thy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
theory Isar_Demo
imports Main
begin
section{* An introductory example *}
lemma "\<not> surj(f :: 'a \<Rightarrow> 'a set)"
proof
assume 0: "surj f"
from 0 have 1: "\<forall>A. \<exists>a. A = f a" by(simp add: surj_def)
from 1 have 2: "\<exists>a. {x. x \<notin> f x} = f a" by blast
from 2 show "False" by blast
qed
text{* A bit shorter: *}
lemma "\<not> surj(f :: 'a \<Rightarrow> 'a set)"
proof
assume 0: "surj f"
from 0 have 1: "\<exists>a. {x. x \<notin> f x} = f a" by(auto simp: surj_def)
from 1 show "False" by blast
qed
subsection{* this, then, hence and thus *}
text{* Avoid labels, use this: *}
lemma "\<not> surj(f :: 'a \<Rightarrow> 'a set)"
proof
assume "surj f"
from this have "\<exists>a. {x. x \<notin> f x} = f a" by(auto simp: surj_def)
from this show "False" by blast
qed
text{* then = from this *}
lemma "\<not> surj(f :: 'a \<Rightarrow> 'a set)"
proof
assume "surj f"
then have "\<exists>a. {x. x \<notin> f x} = f a" by(auto simp: surj_def)
then show "False" by blast
qed
text{* hence = then have, thus = then show *}
lemma "\<not> surj(f :: 'a \<Rightarrow> 'a set)"
proof
assume "surj f"
hence "\<exists>a. {x. x \<notin> f x} = f a" by(auto simp: surj_def)
thus "False" by blast
qed
subsection{* Structured statements: fixes, assumes, shows *}
lemma
fixes f :: "'a \<Rightarrow> 'a set"
assumes s: "surj f"
shows "False"
proof - (* no automatic proof step! *)
have "\<exists> a. {x. x \<notin> f x} = f a" using s
by(auto simp: surj_def)
thus "False" by blast
qed
section{* Proof patterns *}
lemma "P \<longleftrightarrow> Q"
proof
assume "P"
show "Q" sorry
next
assume "Q"
show "P" sorry
qed
lemma "A = (B::'a set)"
proof
show "A \<subseteq> B" sorry
next
show "B \<subseteq> A" sorry
qed
lemma "A \<subseteq> B"
proof
fix a
assume "a \<in> A"
show "a \<in> B" sorry
qed
text{* Contradiction *}
lemma P
proof (rule ccontr)
assume "\<not>P"
show "False" sorry
qed
text{* Case distinction *}
lemma "R"
proof cases
assume "P"
show "R" sorry
next
assume "\<not> P"
show "R" sorry
qed
lemma "R"
proof -
have "P \<or> Q" sorry
then show "R"
proof
assume "P"
show "R" sorry
next
assume "Q"
show "R" sorry
qed
qed
text{* obtain example *}
lemma "\<not> surj(f :: 'a \<Rightarrow> 'a set)"
proof
assume "surj f"
hence "\<exists>a. {x. x \<notin> f x} = f a" by(auto simp: surj_def)
then obtain a where "{x. x \<notin> f x} = f a" by blast
hence "a \<notin> f a \<longleftrightarrow> a \<in> f a" by blast
thus "False" by blast
qed
text{* Interactive exercise: *}
lemma assumes "EX x. ALL y. P x y" shows "ALL y. EX x. P x y"
oops (* solution at the bottom *)
section{* Streamlining proofs *}
subsection{* Pattern matching and ?-variables *}
text{* Show EX *}
lemma "EX xs. length xs = 0" (is "EX xs. ?P xs")
proof
show "?P([])" by simp
qed
text{* Multiple EX easier with forward proof: *}
lemma "EX x y :: int. x < z & z < y" (is "EX x y. ?P x y")
proof -
have "?P (z - 1) (z + 1)" by arith
thus ?thesis by blast
qed
subsection{* Quoting facts: *}
lemma assumes "x < (0::int)" shows "x*x > 0"
proof -
from `x<0` show ?thesis by(metis mult_neg_neg)
qed
subsection {* Example: Top Down Proof Development *}
text{* The key idea: case distinction on length: *}
lemma "(EX ys zs. xs = ys @ zs \<and> length ys = length zs) |
(EX ys zs. xs = ys @ zs & length ys = length zs + 1)"
proof cases
assume "EX n. length xs = n+n"
show ?thesis sorry
next
assume "\<not> (EX n. length xs = n+n)"
show ?thesis sorry
qed
text{* A proof skeleton: *}
lemma "(EX ys zs. xs = ys @ zs \<and> length ys = length zs) |
(EX ys zs. xs = ys @ zs & length ys = length zs + 1)"
proof cases
assume "EX n. length xs = n+n"
then obtain n where "length xs = n+n" by blast
let ?ys = "take n xs"
let ?zs = "take n (drop n xs)"
have "xs = ?ys @ ?zs \<and> length ?ys = length ?zs" sorry
thus ?thesis by blast
next
assume "\<not> (EX n. length xs = n+n)"
then obtain n where "length xs = Suc(n+n)" sorry
let ?ys = "take (Suc n) xs"
let ?zs = "take n (drop (Suc n) xs)"
have "xs = ?ys @ ?zs \<and> length ?ys = length ?zs + 1" sorry
then show ?thesis by blast
qed
text{* The complete proof: *}
lemma "(EX ys zs. xs = ys @ zs \<and> length ys = length zs) |
(EX ys zs. xs = ys @ zs & length ys = length zs + 1)"
proof cases
assume "EX n. length xs = n+n"
then obtain n where "length xs = n+n" by blast
let ?ys = "take n xs"
let ?zs = "take n (drop n xs)"
have "xs = ?ys @ ?zs \<and> length ?ys = length ?zs"
by (simp add: `length xs = n + n`)
thus ?thesis by blast
next
assume "\<not> (EX n. length xs = n+n)"
hence "EX n. length xs = Suc(n+n)" by arith
then obtain n where l: "length xs = Suc(n+n)" by blast
let ?ys = "take (Suc n) xs"
let ?zs = "take n (drop (Suc n) xs)"
have "xs = ?ys @ ?zs \<and> length ?ys = length ?zs + 1" by (simp add: l)
thus ?thesis by blast
qed
subsection {* moreover *}
lemma assumes "A \<and> B" shows "B \<and> A"
proof -
from `A \<and> B` have "A" by auto
moreover
from `A \<and> B` have "B" by auto
ultimately show "B \<and> A" by auto
qed
subsection{* Raw proof blocks *}
lemma fixes k :: int assumes "k dvd (n+k)" shows "k dvd n"
proof -
{ fix a assume a: "n+k = k*a"
have "EX b. n = k*b"
proof
show "n = k*(a - 1)" using a by(simp add: algebra_simps)
qed }
with assms show ?thesis by (auto simp add: dvd_def)
qed
text{* Solution to interactive exercise: *}
lemma assumes "EX x. ALL y. P x y" shows "ALL y. EX x. P x y"
proof
fix b
from assms obtain a where 0: "ALL y. P a y" by blast
show "EX x. P x b"
proof
show "P a b" using 0 by blast
qed
qed
end