author | haftmann |
Fri, 20 Apr 2007 11:21:35 +0200 | |
changeset 22737 | d87ccbcc2702 |
parent 22507 | 3572bc633d9a |
child 22845 | 5f9138bcb3d7 |
permissions | -rw-r--r-- |
17024 | 1 |
(* Title: HOL/Extraction/Pigeonhole.thy |
2 |
ID: $Id$ |
|
3 |
Author: Stefan Berghofer, TU Muenchen |
|
4 |
*) |
|
5 |
||
6 |
header {* The pigeonhole principle *} |
|
7 |
||
22737 | 8 |
theory Pigeonhole |
9 |
imports EfficientNat |
|
10 |
begin |
|
17024 | 11 |
|
12 |
text {* |
|
13 |
We formalize two proofs of the pigeonhole principle, which lead |
|
14 |
to extracted programs of quite different complexity. The original |
|
15 |
formalization of these proofs in {\sc Nuprl} is due to |
|
16 |
Aleksey Nogin \cite{Nogin-ENTCS-2000}. |
|
17 |
||
18 |
We need decidability of equality on natural numbers: |
|
19 |
*} |
|
20 |
||
21127 | 21 |
lemma nat_eq_dec: "(m\<Colon>nat) = n \<or> m \<noteq> n" |
22 |
apply (induct m arbitrary: n) |
|
17024 | 23 |
apply (case_tac n) |
24 |
apply (case_tac [3] n) |
|
17604 | 25 |
apply (simp only: nat.simps, iprover?)+ |
17024 | 26 |
done |
27 |
||
28 |
text {* |
|
17027 | 29 |
We can decide whether an array @{term "f"} of length @{term "l"} |
17024 | 30 |
contains an element @{term "x"}. |
31 |
*} |
|
32 |
||
33 |
lemma search: "(\<exists>j<(l::nat). (x::nat) = f j) \<or> \<not> (\<exists>j<l. x = f j)" |
|
34 |
proof (induct l) |
|
35 |
case 0 |
|
36 |
have "\<not> (\<exists>j<0. x = f j)" |
|
37 |
proof |
|
38 |
assume "\<exists>j<0. x = f j" |
|
17604 | 39 |
then obtain j where j: "j < (0::nat)" by iprover |
17024 | 40 |
thus "False" by simp |
41 |
qed |
|
42 |
thus ?case .. |
|
43 |
next |
|
44 |
case (Suc l) |
|
45 |
thus ?case |
|
46 |
proof |
|
47 |
assume "\<exists>j<l. x = f j" |
|
48 |
then obtain j where j: "j < l" |
|
17604 | 49 |
and eq: "x = f j" by iprover |
17024 | 50 |
from j have "j < Suc l" by simp |
17604 | 51 |
with eq show ?case by iprover |
17024 | 52 |
next |
53 |
assume nex: "\<not> (\<exists>j<l. x = f j)" |
|
54 |
from nat_eq_dec show ?case |
|
55 |
proof |
|
56 |
assume eq: "x = f l" |
|
57 |
have "l < Suc l" by simp |
|
17604 | 58 |
with eq show ?case by iprover |
17024 | 59 |
next |
60 |
assume neq: "x \<noteq> f l" |
|
61 |
have "\<not> (\<exists>j<Suc l. x = f j)" |
|
62 |
proof |
|
63 |
assume "\<exists>j<Suc l. x = f j" |
|
64 |
then obtain j where j: "j < Suc l" |
|
17604 | 65 |
and eq: "x = f j" by iprover |
17024 | 66 |
show False |
67 |
proof cases |
|
68 |
assume "j = l" |
|
69 |
with eq have "x = f l" by simp |
|
70 |
with neq show False .. |
|
71 |
next |
|
72 |
assume "j \<noteq> l" |
|
73 |
with j have "j < l" by simp |
|
17604 | 74 |
with nex and eq show False by iprover |
17024 | 75 |
qed |
76 |
qed |
|
77 |
thus ?case .. |
|
78 |
qed |
|
79 |
qed |
|
80 |
qed |
|
81 |
||
82 |
text {* |
|
83 |
This proof yields a polynomial program. |
|
84 |
*} |
|
85 |
||
86 |
theorem pigeonhole: |
|
87 |
"\<And>f. (\<And>i. i \<le> Suc n \<Longrightarrow> f i \<le> n) \<Longrightarrow> \<exists>i j. i \<le> Suc n \<and> j < i \<and> f i = f j" |
|
88 |
proof (induct n) |
|
89 |
case 0 |
|
90 |
hence "Suc 0 \<le> Suc 0 \<and> 0 < Suc 0 \<and> f (Suc 0) = f 0" by simp |
|
17604 | 91 |
thus ?case by iprover |
17024 | 92 |
next |
93 |
case (Suc n) |
|
94 |
{ |
|
95 |
fix k |
|
96 |
have |
|
97 |
"k \<le> Suc (Suc n) \<Longrightarrow> |
|
98 |
(\<And>i j. Suc k \<le> i \<Longrightarrow> i \<le> Suc (Suc n) \<Longrightarrow> j < i \<Longrightarrow> f i \<noteq> f j) \<Longrightarrow> |
|
99 |
(\<exists>i j. i \<le> k \<and> j < i \<and> f i = f j)" |
|
100 |
proof (induct k) |
|
101 |
case 0 |
|
102 |
let ?f = "\<lambda>i. if f i = Suc n then f (Suc (Suc n)) else f i" |
|
103 |
have "\<not> (\<exists>i j. i \<le> Suc n \<and> j < i \<and> ?f i = ?f j)" |
|
104 |
proof |
|
105 |
assume "\<exists>i j. i \<le> Suc n \<and> j < i \<and> ?f i = ?f j" |
|
106 |
then obtain i j where i: "i \<le> Suc n" and j: "j < i" |
|
17604 | 107 |
and f: "?f i = ?f j" by iprover |
17024 | 108 |
from j have i_nz: "Suc 0 \<le> i" by simp |
109 |
from i have iSSn: "i \<le> Suc (Suc n)" by simp |
|
110 |
have S0SSn: "Suc 0 \<le> Suc (Suc n)" by simp |
|
111 |
show False |
|
112 |
proof cases |
|
113 |
assume fi: "f i = Suc n" |
|
114 |
show False |
|
115 |
proof cases |
|
116 |
assume fj: "f j = Suc n" |
|
117 |
from i_nz and iSSn and j have "f i \<noteq> f j" by (rule 0) |
|
118 |
moreover from fi have "f i = f j" |
|
119 |
by (simp add: fj [symmetric]) |
|
120 |
ultimately show ?thesis .. |
|
121 |
next |
|
122 |
from i and j have "j < Suc (Suc n)" by simp |
|
123 |
with S0SSn and le_refl have "f (Suc (Suc n)) \<noteq> f j" |
|
124 |
by (rule 0) |
|
125 |
moreover assume "f j \<noteq> Suc n" |
|
126 |
with fi and f have "f (Suc (Suc n)) = f j" by simp |
|
127 |
ultimately show False .. |
|
128 |
qed |
|
129 |
next |
|
130 |
assume fi: "f i \<noteq> Suc n" |
|
131 |
show False |
|
132 |
proof cases |
|
133 |
from i have "i < Suc (Suc n)" by simp |
|
134 |
with S0SSn and le_refl have "f (Suc (Suc n)) \<noteq> f i" |
|
135 |
by (rule 0) |
|
136 |
moreover assume "f j = Suc n" |
|
137 |
with fi and f have "f (Suc (Suc n)) = f i" by simp |
|
138 |
ultimately show False .. |
|
139 |
next |
|
140 |
from i_nz and iSSn and j |
|
141 |
have "f i \<noteq> f j" by (rule 0) |
|
142 |
moreover assume "f j \<noteq> Suc n" |
|
143 |
with fi and f have "f i = f j" by simp |
|
144 |
ultimately show False .. |
|
145 |
qed |
|
146 |
qed |
|
147 |
qed |
|
148 |
moreover have "\<And>i. i \<le> Suc n \<Longrightarrow> ?f i \<le> n" |
|
149 |
proof - |
|
150 |
fix i assume "i \<le> Suc n" |
|
151 |
hence i: "i < Suc (Suc n)" by simp |
|
152 |
have "f (Suc (Suc n)) \<noteq> f i" |
|
153 |
by (rule 0) (simp_all add: i) |
|
154 |
moreover have "f (Suc (Suc n)) \<le> Suc n" |
|
155 |
by (rule Suc) simp |
|
156 |
moreover from i have "i \<le> Suc (Suc n)" by simp |
|
157 |
hence "f i \<le> Suc n" by (rule Suc) |
|
158 |
ultimately show "?thesis i" |
|
159 |
by simp |
|
160 |
qed |
|
161 |
hence "\<exists>i j. i \<le> Suc n \<and> j < i \<and> ?f i = ?f j" |
|
162 |
by (rule Suc) |
|
163 |
ultimately show ?case .. |
|
164 |
next |
|
165 |
case (Suc k) |
|
166 |
from search show ?case |
|
167 |
proof |
|
168 |
assume "\<exists>j<Suc k. f (Suc k) = f j" |
|
17604 | 169 |
thus ?case by (iprover intro: le_refl) |
17024 | 170 |
next |
171 |
assume nex: "\<not> (\<exists>j<Suc k. f (Suc k) = f j)" |
|
172 |
have "\<exists>i j. i \<le> k \<and> j < i \<and> f i = f j" |
|
173 |
proof (rule Suc) |
|
174 |
from Suc show "k \<le> Suc (Suc n)" by simp |
|
175 |
fix i j assume k: "Suc k \<le> i" and i: "i \<le> Suc (Suc n)" |
|
176 |
and j: "j < i" |
|
177 |
show "f i \<noteq> f j" |
|
178 |
proof cases |
|
179 |
assume eq: "i = Suc k" |
|
180 |
show ?thesis |
|
181 |
proof |
|
182 |
assume "f i = f j" |
|
183 |
hence "f (Suc k) = f j" by (simp add: eq) |
|
17604 | 184 |
with nex and j and eq show False by iprover |
17024 | 185 |
qed |
186 |
next |
|
187 |
assume "i \<noteq> Suc k" |
|
188 |
with k have "Suc (Suc k) \<le> i" by simp |
|
189 |
thus ?thesis using i and j by (rule Suc) |
|
190 |
qed |
|
191 |
qed |
|
17604 | 192 |
thus ?thesis by (iprover intro: le_SucI) |
17024 | 193 |
qed |
194 |
qed |
|
195 |
} |
|
196 |
note r = this |
|
197 |
show ?case by (rule r) simp_all |
|
198 |
qed |
|
199 |
||
200 |
text {* |
|
201 |
The following proof, although quite elegant from a mathematical point of view, |
|
202 |
leads to an exponential program: |
|
203 |
*} |
|
204 |
||
205 |
theorem pigeonhole_slow: |
|
206 |
"\<And>f. (\<And>i. i \<le> Suc n \<Longrightarrow> f i \<le> n) \<Longrightarrow> \<exists>i j. i \<le> Suc n \<and> j < i \<and> f i = f j" |
|
207 |
proof (induct n) |
|
208 |
case 0 |
|
209 |
have "Suc 0 \<le> Suc 0" .. |
|
210 |
moreover have "0 < Suc 0" .. |
|
211 |
moreover from 0 have "f (Suc 0) = f 0" by simp |
|
17604 | 212 |
ultimately show ?case by iprover |
17024 | 213 |
next |
214 |
case (Suc n) |
|
215 |
from search show ?case |
|
216 |
proof |
|
217 |
assume "\<exists>j < Suc (Suc n). f (Suc (Suc n)) = f j" |
|
17604 | 218 |
thus ?case by (iprover intro: le_refl) |
17024 | 219 |
next |
220 |
assume "\<not> (\<exists>j < Suc (Suc n). f (Suc (Suc n)) = f j)" |
|
17604 | 221 |
hence nex: "\<forall>j < Suc (Suc n). f (Suc (Suc n)) \<noteq> f j" by iprover |
17024 | 222 |
let ?f = "\<lambda>i. if f i = Suc n then f (Suc (Suc n)) else f i" |
223 |
have "\<And>i. i \<le> Suc n \<Longrightarrow> ?f i \<le> n" |
|
224 |
proof - |
|
225 |
fix i assume i: "i \<le> Suc n" |
|
226 |
show "?thesis i" |
|
227 |
proof (cases "f i = Suc n") |
|
228 |
case True |
|
229 |
from i and nex have "f (Suc (Suc n)) \<noteq> f i" by simp |
|
230 |
with True have "f (Suc (Suc n)) \<noteq> Suc n" by simp |
|
231 |
moreover from Suc have "f (Suc (Suc n)) \<le> Suc n" by simp |
|
232 |
ultimately have "f (Suc (Suc n)) \<le> n" by simp |
|
233 |
with True show ?thesis by simp |
|
234 |
next |
|
235 |
case False |
|
236 |
from Suc and i have "f i \<le> Suc n" by simp |
|
237 |
with False show ?thesis by simp |
|
238 |
qed |
|
239 |
qed |
|
240 |
hence "\<exists>i j. i \<le> Suc n \<and> j < i \<and> ?f i = ?f j" by (rule Suc) |
|
241 |
then obtain i j where i: "i \<le> Suc n" and ji: "j < i" and f: "?f i = ?f j" |
|
17604 | 242 |
by iprover |
17024 | 243 |
have "f i = f j" |
244 |
proof (cases "f i = Suc n") |
|
245 |
case True |
|
246 |
show ?thesis |
|
247 |
proof (cases "f j = Suc n") |
|
248 |
assume "f j = Suc n" |
|
249 |
with True show ?thesis by simp |
|
250 |
next |
|
251 |
assume "f j \<noteq> Suc n" |
|
252 |
moreover from i ji nex have "f (Suc (Suc n)) \<noteq> f j" by simp |
|
253 |
ultimately show ?thesis using True f by simp |
|
254 |
qed |
|
255 |
next |
|
256 |
case False |
|
257 |
show ?thesis |
|
258 |
proof (cases "f j = Suc n") |
|
259 |
assume "f j = Suc n" |
|
260 |
moreover from i nex have "f (Suc (Suc n)) \<noteq> f i" by simp |
|
261 |
ultimately show ?thesis using False f by simp |
|
262 |
next |
|
263 |
assume "f j \<noteq> Suc n" |
|
264 |
with False f show ?thesis by simp |
|
265 |
qed |
|
266 |
qed |
|
267 |
moreover from i have "i \<le> Suc (Suc n)" by simp |
|
17604 | 268 |
ultimately show ?thesis using ji by iprover |
17024 | 269 |
qed |
270 |
qed |
|
271 |
||
272 |
extract pigeonhole pigeonhole_slow |
|
273 |
||
274 |
text {* |
|
275 |
The programs extracted from the above proofs look as follows: |
|
276 |
@{thm [display] pigeonhole_def} |
|
277 |
@{thm [display] pigeonhole_slow_def} |
|
278 |
The program for searching for an element in an array is |
|
279 |
@{thm [display,eta_contract=false] search_def} |
|
280 |
The correctness statement for @{term "pigeonhole"} is |
|
281 |
@{thm [display] pigeonhole_correctness [no_vars]} |
|
282 |
||
283 |
In order to analyze the speed of the above programs, |
|
284 |
we generate ML code from them. |
|
285 |
*} |
|
286 |
||
20837 | 287 |
definition |
288 |
"test n = pigeonhole n (\<lambda>m. m - 1)" |
|
21404
eb85850d3eb7
more robust syntax for definition/abbreviation/notation;
wenzelm
parents:
21127
diff
changeset
|
289 |
definition |
20837 | 290 |
"test' n = pigeonhole_slow n (\<lambda>m. m - 1)" |
22507 | 291 |
definition |
292 |
"test'' u = pigeonhole 8 (op ! [0, 1, 2, 3, 4, 5, 6, 3, 7, 8])" |
|
20837 | 293 |
|
22507 | 294 |
|
295 |
consts_code |
|
296 |
arbitrary :: "nat \<times> nat" ("{* (0::nat, 0::nat) *}") |
|
297 |
||
298 |
definition |
|
299 |
arbitrary_nat_pair :: "nat \<times> nat" where |
|
300 |
[symmetric, code inline]: "arbitrary_nat_pair = arbitrary" |
|
301 |
||
302 |
code_const arbitrary_nat_pair (SML "(~1, ~1)") |
|
303 |
(* this is justified since for valid inputs this "arbitrary" will be dropped |
|
304 |
in the next recursion step in pigeonhole_def *) |
|
20837 | 305 |
|
22507 | 306 |
code_module PH |
307 |
contains |
|
308 |
test = test |
|
309 |
test' = test' |
|
310 |
test'' = test'' |
|
311 |
||
312 |
code_gen test test' test'' (SML #) |
|
313 |
||
314 |
ML "timeit (fn () => PH.test 10)" |
|
20837 | 315 |
ML "timeit (fn () => ROOT.Pigeonhole.test 10)" |
22507 | 316 |
|
317 |
ML "timeit (fn () => PH.test' 10)" |
|
20837 | 318 |
ML "timeit (fn () => ROOT.Pigeonhole.test' 10)" |
22507 | 319 |
|
320 |
ML "timeit (fn () => PH.test 20)" |
|
20837 | 321 |
ML "timeit (fn () => ROOT.Pigeonhole.test 20)" |
22507 | 322 |
|
323 |
ML "timeit (fn () => PH.test' 20)" |
|
20837 | 324 |
ML "timeit (fn () => ROOT.Pigeonhole.test' 20)" |
22507 | 325 |
|
326 |
ML "timeit (fn () => PH.test 25)" |
|
20837 | 327 |
ML "timeit (fn () => ROOT.Pigeonhole.test 25)" |
22507 | 328 |
|
329 |
ML "timeit (fn () => PH.test' 25)" |
|
20837 | 330 |
ML "timeit (fn () => ROOT.Pigeonhole.test' 25)" |
22507 | 331 |
|
332 |
ML "timeit (fn () => PH.test 500)" |
|
20837 | 333 |
ML "timeit (fn () => ROOT.Pigeonhole.test 500)" |
334 |
||
22507 | 335 |
ML "timeit PH.test''" |
336 |
ML "timeit ROOT.Pigeonhole.test''" |
|
20837 | 337 |
|
17024 | 338 |
end |