author | wenzelm |
Thu, 01 Sep 2005 00:46:14 +0200 | |
changeset 17215 | 8b969275a5d2 |
parent 16761 | 99549528ce76 |
child 17604 | 5f30179fbf44 |
permissions | -rw-r--r-- |
13405 | 1 |
(* Title: HOL/Extraction/Warshall.thy |
2 |
ID: $Id$ |
|
3 |
Author: Stefan Berghofer, TU Muenchen |
|
4 |
*) |
|
5 |
||
6 |
header {* Warshall's algorithm *} |
|
7 |
||
16761 | 8 |
theory Warshall |
9 |
imports Main |
|
10 |
begin |
|
13405 | 11 |
|
12 |
text {* |
|
13 |
Derivation of Warshall's algorithm using program extraction, |
|
14 |
based on Berger, Schwichtenberg and Seisenberger \cite{Berger-JAR-2001}. |
|
15 |
*} |
|
16 |
||
17 |
datatype b = T | F |
|
18 |
||
19 |
consts |
|
20 |
is_path' :: "('a \<Rightarrow> 'a \<Rightarrow> b) \<Rightarrow> 'a \<Rightarrow> 'a list \<Rightarrow> 'a \<Rightarrow> bool" |
|
21 |
||
22 |
primrec |
|
23 |
"is_path' r x [] z = (r x z = T)" |
|
24 |
"is_path' r x (y # ys) z = (r x y = T \<and> is_path' r y ys z)" |
|
25 |
||
26 |
constdefs |
|
27 |
is_path :: "(nat \<Rightarrow> nat \<Rightarrow> b) \<Rightarrow> (nat * nat list * nat) \<Rightarrow> |
|
28 |
nat \<Rightarrow> nat \<Rightarrow> nat \<Rightarrow> bool" |
|
29 |
"is_path r p i j k == fst p = j \<and> snd (snd p) = k \<and> |
|
30 |
list_all (\<lambda>x. x < i) (fst (snd p)) \<and> |
|
31 |
is_path' r (fst p) (fst (snd p)) (snd (snd p))" |
|
32 |
||
33 |
conc :: "('a * 'a list * 'a) \<Rightarrow> ('a * 'a list * 'a) \<Rightarrow> ('a * 'a list * 'a)" |
|
34 |
"conc p q == (fst p, fst (snd p) @ fst q # fst (snd q), snd (snd q))" |
|
35 |
||
36 |
theorem is_path'_snoc [simp]: |
|
37 |
"\<And>x. is_path' r x (ys @ [y]) z = (is_path' r x ys y \<and> r y z = T)" |
|
38 |
by (induct ys) simp+ |
|
39 |
||
40 |
theorem list_all_scoc [simp]: "list_all P (xs @ [x]) = (P x \<and> list_all P xs)" |
|
41 |
by (induct xs, simp+, rules) |
|
42 |
||
43 |
theorem list_all_lemma: |
|
44 |
"list_all P xs \<Longrightarrow> (\<And>x. P x \<Longrightarrow> Q x) \<Longrightarrow> list_all Q xs" |
|
45 |
proof - |
|
46 |
assume PQ: "\<And>x. P x \<Longrightarrow> Q x" |
|
47 |
show "list_all P xs \<Longrightarrow> list_all Q xs" |
|
48 |
proof (induct xs) |
|
49 |
case Nil |
|
50 |
show ?case by simp |
|
51 |
next |
|
52 |
case (Cons y ys) |
|
53 |
hence Py: "P y" by simp |
|
54 |
from Cons have Pys: "list_all P ys" by simp |
|
55 |
show ?case |
|
56 |
by simp (rule conjI PQ Py Cons Pys)+ |
|
57 |
qed |
|
58 |
qed |
|
59 |
||
60 |
theorem lemma1: "\<And>p. is_path r p i j k \<Longrightarrow> is_path r p (Suc i) j k" |
|
61 |
apply (unfold is_path_def) |
|
62 |
apply (simp cong add: conj_cong add: split_paired_all) |
|
63 |
apply (erule conjE)+ |
|
64 |
apply (erule list_all_lemma) |
|
65 |
apply simp |
|
66 |
done |
|
67 |
||
68 |
theorem lemma2: "\<And>p. is_path r p 0 j k \<Longrightarrow> r j k = T" |
|
69 |
apply (unfold is_path_def) |
|
70 |
apply (simp cong add: conj_cong add: split_paired_all) |
|
71 |
apply (case_tac "aa") |
|
72 |
apply simp+ |
|
73 |
done |
|
74 |
||
75 |
theorem is_path'_conc: "is_path' r j xs i \<Longrightarrow> is_path' r i ys k \<Longrightarrow> |
|
76 |
is_path' r j (xs @ i # ys) k" |
|
77 |
proof - |
|
78 |
assume pys: "is_path' r i ys k" |
|
79 |
show "\<And>j. is_path' r j xs i \<Longrightarrow> is_path' r j (xs @ i # ys) k" |
|
80 |
proof (induct xs) |
|
81 |
case (Nil j) |
|
82 |
hence "r j i = T" by simp |
|
83 |
with pys show ?case by simp |
|
84 |
next |
|
85 |
case (Cons z zs j) |
|
86 |
hence jzr: "r j z = T" by simp |
|
87 |
from Cons have pzs: "is_path' r z zs i" by simp |
|
88 |
show ?case |
|
89 |
by simp (rule conjI jzr Cons pzs)+ |
|
90 |
qed |
|
91 |
qed |
|
92 |
||
93 |
theorem lemma3: |
|
94 |
"\<And>p q. is_path r p i j i \<Longrightarrow> is_path r q i i k \<Longrightarrow> |
|
95 |
is_path r (conc p q) (Suc i) j k" |
|
96 |
apply (unfold is_path_def conc_def) |
|
97 |
apply (simp cong add: conj_cong add: split_paired_all) |
|
98 |
apply (erule conjE)+ |
|
99 |
apply (rule conjI) |
|
100 |
apply (erule list_all_lemma) |
|
101 |
apply simp |
|
102 |
apply (rule conjI) |
|
103 |
apply (erule list_all_lemma) |
|
104 |
apply simp |
|
105 |
apply (rule is_path'_conc) |
|
106 |
apply assumption+ |
|
107 |
done |
|
108 |
||
109 |
theorem lemma5: |
|
110 |
"\<And>p. is_path r p (Suc i) j k \<Longrightarrow> ~ is_path r p i j k \<Longrightarrow> |
|
111 |
(\<exists>q. is_path r q i j i) \<and> (\<exists>q'. is_path r q' i i k)" |
|
112 |
proof (simp cong add: conj_cong add: split_paired_all is_path_def, (erule conjE)+) |
|
113 |
fix xs |
|
114 |
assume "list_all (\<lambda>x. x < Suc i) xs" |
|
115 |
assume "is_path' r j xs k" |
|
116 |
assume "\<not> list_all (\<lambda>x. x < i) xs" |
|
117 |
show "(\<exists>ys. list_all (\<lambda>x. x < i) ys \<and> is_path' r j ys i) \<and> |
|
118 |
(\<exists>ys. list_all (\<lambda>x. x < i) ys \<and> is_path' r i ys k)" |
|
119 |
proof |
|
120 |
show "\<And>j. list_all (\<lambda>x. x < Suc i) xs \<Longrightarrow> is_path' r j xs k \<Longrightarrow> |
|
121 |
\<not> list_all (\<lambda>x. x < i) xs \<Longrightarrow> |
|
122 |
\<exists>ys. list_all (\<lambda>x. x < i) ys \<and> is_path' r j ys i" (is "PROP ?ih xs") |
|
123 |
proof (induct xs) |
|
124 |
case Nil |
|
125 |
thus ?case by simp |
|
126 |
next |
|
127 |
case (Cons a as j) |
|
128 |
show ?case |
|
129 |
proof (cases "a=i") |
|
130 |
case True |
|
131 |
show ?thesis |
|
132 |
proof |
|
133 |
from True and Cons have "r j i = T" by simp |
|
134 |
thus "list_all (\<lambda>x. x < i) [] \<and> is_path' r j [] i" by simp |
|
135 |
qed |
|
136 |
next |
|
137 |
case False |
|
138 |
have "PROP ?ih as" by (rule Cons) |
|
139 |
then obtain ys where ys: "list_all (\<lambda>x. x < i) ys \<and> is_path' r a ys i" |
|
140 |
proof |
|
141 |
from Cons show "list_all (\<lambda>x. x < Suc i) as" by simp |
|
142 |
from Cons show "is_path' r a as k" by simp |
|
16761 | 143 |
from Cons and False show "\<not> list_all (\<lambda>x. x < i) as" by (simp) |
13405 | 144 |
qed |
145 |
show ?thesis |
|
146 |
proof |
|
147 |
from Cons False ys |
|
16761 | 148 |
show "list_all (\<lambda>x. x<i) (a#ys) \<and> is_path' r j (a#ys) i" by simp |
13405 | 149 |
qed |
150 |
qed |
|
151 |
qed |
|
152 |
show "\<And>k. list_all (\<lambda>x. x < Suc i) xs \<Longrightarrow> is_path' r j xs k \<Longrightarrow> |
|
153 |
\<not> list_all (\<lambda>x. x < i) xs \<Longrightarrow> |
|
154 |
\<exists>ys. list_all (\<lambda>x. x < i) ys \<and> is_path' r i ys k" (is "PROP ?ih xs") |
|
155 |
proof (induct xs rule: rev_induct) |
|
156 |
case Nil |
|
157 |
thus ?case by simp |
|
158 |
next |
|
159 |
case (snoc a as k) |
|
160 |
show ?case |
|
161 |
proof (cases "a=i") |
|
162 |
case True |
|
163 |
show ?thesis |
|
164 |
proof |
|
165 |
from True and snoc have "r i k = T" by simp |
|
166 |
thus "list_all (\<lambda>x. x < i) [] \<and> is_path' r i [] k" by simp |
|
167 |
qed |
|
168 |
next |
|
169 |
case False |
|
170 |
have "PROP ?ih as" by (rule snoc) |
|
171 |
then obtain ys where ys: "list_all (\<lambda>x. x < i) ys \<and> is_path' r i ys a" |
|
172 |
proof |
|
173 |
from snoc show "list_all (\<lambda>x. x < Suc i) as" by simp |
|
174 |
from snoc show "is_path' r j as a" by simp |
|
16761 | 175 |
from snoc and False show "\<not> list_all (\<lambda>x. x < i) as" by simp |
13405 | 176 |
qed |
177 |
show ?thesis |
|
178 |
proof |
|
179 |
from snoc False ys |
|
180 |
show "list_all (\<lambda>x. x < i) (ys @ [a]) \<and> is_path' r i (ys @ [a]) k" |
|
16761 | 181 |
by simp |
13405 | 182 |
qed |
183 |
qed |
|
184 |
qed |
|
185 |
qed |
|
186 |
qed |
|
187 |
||
188 |
theorem lemma5': |
|
189 |
"\<And>p. is_path r p (Suc i) j k \<Longrightarrow> \<not> is_path r p i j k \<Longrightarrow> |
|
190 |
\<not> (\<forall>q. \<not> is_path r q i j i) \<and> \<not> (\<forall>q'. \<not> is_path r q' i i k)" |
|
191 |
by (rules dest: lemma5) |
|
192 |
||
193 |
theorem warshall: |
|
194 |
"\<And>j k. \<not> (\<exists>p. is_path r p i j k) \<or> (\<exists>p. is_path r p i j k)" |
|
195 |
proof (induct i) |
|
196 |
case (0 j k) |
|
197 |
show ?case |
|
198 |
proof (cases "r j k") |
|
199 |
assume "r j k = T" |
|
200 |
hence "is_path r (j, [], k) 0 j k" |
|
201 |
by (simp add: is_path_def) |
|
202 |
hence "\<exists>p. is_path r p 0 j k" .. |
|
203 |
thus ?thesis .. |
|
204 |
next |
|
205 |
assume "r j k = F" |
|
206 |
hence "r j k ~= T" by simp |
|
207 |
hence "\<not> (\<exists>p. is_path r p 0 j k)" |
|
208 |
by (rules dest: lemma2) |
|
209 |
thus ?thesis .. |
|
210 |
qed |
|
211 |
next |
|
212 |
case (Suc i j k) |
|
213 |
thus ?case |
|
214 |
proof |
|
215 |
assume h1: "\<not> (\<exists>p. is_path r p i j k)" |
|
216 |
from Suc show ?case |
|
217 |
proof |
|
218 |
assume "\<not> (\<exists>p. is_path r p i j i)" |
|
219 |
with h1 have "\<not> (\<exists>p. is_path r p (Suc i) j k)" |
|
220 |
by (rules dest: lemma5') |
|
221 |
thus ?case .. |
|
222 |
next |
|
223 |
assume "\<exists>p. is_path r p i j i" |
|
224 |
then obtain p where h2: "is_path r p i j i" .. |
|
225 |
from Suc show ?case |
|
226 |
proof |
|
227 |
assume "\<not> (\<exists>p. is_path r p i i k)" |
|
228 |
with h1 have "\<not> (\<exists>p. is_path r p (Suc i) j k)" |
|
229 |
by (rules dest: lemma5') |
|
230 |
thus ?case .. |
|
231 |
next |
|
232 |
assume "\<exists>q. is_path r q i i k" |
|
233 |
then obtain q where "is_path r q i i k" .. |
|
234 |
with h2 have "is_path r (conc p q) (Suc i) j k" |
|
235 |
by (rule lemma3) |
|
236 |
hence "\<exists>pq. is_path r pq (Suc i) j k" .. |
|
237 |
thus ?case .. |
|
238 |
qed |
|
239 |
qed |
|
240 |
next |
|
241 |
assume "\<exists>p. is_path r p i j k" |
|
242 |
hence "\<exists>p. is_path r p (Suc i) j k" |
|
243 |
by (rules intro: lemma1) |
|
244 |
thus ?case .. |
|
245 |
qed |
|
246 |
qed |
|
247 |
||
248 |
extract warshall |
|
249 |
||
250 |
text {* |
|
251 |
The program extracted from the above proof looks as follows |
|
13671
eec2582923f6
Eta contraction is now switched off when printing extracted program.
berghofe
parents:
13471
diff
changeset
|
252 |
@{thm [display, eta_contract=false] warshall_def [no_vars]} |
13405 | 253 |
The corresponding correctness theorem is |
254 |
@{thm [display] warshall_correctness [no_vars]} |
|
255 |
*} |
|
256 |
||
257 |
end |