| author | wenzelm |
| Wed, 12 Oct 2016 15:48:05 +0200 | |
| changeset 64168 | e573b985390c |
| parent 63106 | 412140038d3c |
| child 67410 | 64d928bacddd |
| permissions | -rw-r--r-- |
| 1476 | 1 |
(* Title: HOL/Hoare/Examples.thy |
2 |
Author: Norbert Galm |
|
| 5646 | 3 |
Copyright 1998 TUM |
| 1335 | 4 |
|
| 5646 | 5 |
Various examples. |
| 1335 | 6 |
*) |
7 |
||
|
35316
870dfea4f9c0
dropped axclass; dropped Id; session theory Hoare.thy
haftmann
parents:
16796
diff
changeset
|
8 |
theory Examples imports Hoare_Logic Arith2 begin |
| 13682 | 9 |
|
10 |
(*** ARITHMETIC ***) |
|
11 |
||
12 |
(** multiplication by successive addition **) |
|
13 |
||
| 13737 | 14 |
lemma multiply_by_add: "VARS m s a b |
| 13682 | 15 |
{a=A & b=B}
|
16 |
m := 0; s := 0; |
|
17 |
WHILE m~=a |
|
18 |
INV {s=m*b & a=A & b=B}
|
|
19 |
DO s := s+b; m := m+(1::nat) OD |
|
20 |
{s = A*B}"
|
|
21 |
by vcg_simp |
|
22 |
||
| 63106 | 23 |
lemma multiply_by_add_time: "VARS m s a b t |
24 |
{a=A & b=B & t=0}
|
|
25 |
m := 0; t := t+1; s := 0; t := t+1; |
|
26 |
WHILE m~=a |
|
27 |
INV {s=m*b & a=A & b=B & t = 2*m + 2}
|
|
28 |
DO s := s+b; t := t+1; m := m+(1::nat); t := t+1 OD |
|
29 |
{s = A*B \<and> t = 2*A + 2}"
|
|
30 |
by vcg_simp |
|
31 |
||
32 |
lemma multiply_by_add2: "VARS M N P :: int |
|
| 13789 | 33 |
{m=M & n=N}
|
34 |
IF M < 0 THEN M := -M; N := -N ELSE SKIP FI; |
|
35 |
P := 0; |
|
36 |
WHILE 0 < M |
|
37 |
INV {0 <= M & (EX p. p = (if m<0 then -m else m) & p*N = m*n & P = (p-M)*N)}
|
|
38 |
DO P := P+N; M := M - 1 OD |
|
39 |
{P = m*n}"
|
|
40 |
apply vcg_simp |
|
| 63106 | 41 |
apply (auto simp add:int_distrib) |
42 |
done |
|
43 |
||
44 |
lemma multiply_by_add2_time: "VARS M N P t :: int |
|
45 |
{m=M & n=N & t=0}
|
|
46 |
IF M < 0 THEN M := -M; t := t+1; N := -N; t := t+1 ELSE SKIP FI; |
|
47 |
P := 0; t := t+1; |
|
48 |
WHILE 0 < M |
|
49 |
INV {0 \<le> M & (EX p. p = (if m<0 then -m else m) & p*N = m*n & P = (p-M)*N & t \<ge> 0 & t \<le> 2*(p-M)+3)}
|
|
50 |
DO P := P+N; t := t+1; M := M - 1; t := t+1 OD |
|
51 |
{P = m*n & t \<le> 2*abs m + 3}"
|
|
52 |
apply vcg_simp |
|
53 |
apply (auto simp add:int_distrib) |
|
| 13789 | 54 |
done |
| 13682 | 55 |
|
56 |
(** Euclid's algorithm for GCD **) |
|
57 |
||
| 13737 | 58 |
lemma Euclid_GCD: "VARS a b |
| 13682 | 59 |
{0<A & 0<B}
|
60 |
a := A; b := B; |
|
| 13857 | 61 |
WHILE a \<noteq> b |
| 13682 | 62 |
INV {0<a & 0<b & gcd A B = gcd a b}
|
63 |
DO IF a<b THEN b := b-a ELSE a := a-b FI OD |
|
64 |
{a = gcd A B}"
|
|
65 |
apply vcg |
|
66 |
(*Now prove the verification conditions*) |
|
67 |
apply auto |
|
68 |
apply(simp add: gcd_diff_r less_imp_le) |
|
| 16796 | 69 |
apply(simp add: linorder_not_less gcd_diff_l) |
| 13682 | 70 |
apply(erule gcd_nnn) |
71 |
done |
|
72 |
||
| 63106 | 73 |
lemma Euclid_GCD_time: "VARS a b t |
74 |
{0<A & 0<B & t=0}
|
|
75 |
a := A; t := t+1; b := B; t := t+1; |
|
76 |
WHILE a \<noteq> b |
|
77 |
INV {0<a & 0<b & gcd A B = gcd a b & a\<le>A & b\<le>B & t \<le> max A B - max a b + 2}
|
|
78 |
DO IF a<b THEN b := b-a; t := t+1 ELSE a := a-b; t := t+1 FI OD |
|
79 |
{a = gcd A B & t \<le> max A B + 2}"
|
|
80 |
apply vcg |
|
81 |
(*Now prove the verification conditions*) |
|
82 |
apply auto |
|
83 |
apply(simp add: gcd_diff_r less_imp_le) |
|
84 |
apply(simp add: linorder_not_less gcd_diff_l) |
|
85 |
apply(erule gcd_nnn) |
|
86 |
done |
|
87 |
||
| 13682 | 88 |
(** Dijkstra's extension of Euclid's algorithm for simultaneous GCD and SCM **) |
89 |
(* From E.W. Disjkstra. Selected Writings on Computing, p 98 (EWD474), |
|
90 |
where it is given without the invariant. Instead of defining scm |
|
91 |
explicitly we have used the theorem scm x y = x*y/gcd x y and avoided |
|
92 |
division by mupltiplying with gcd x y. |
|
93 |
*) |
|
94 |
||
95 |
lemmas distribs = |
|
96 |
diff_mult_distrib diff_mult_distrib2 add_mult_distrib add_mult_distrib2 |
|
97 |
||
| 13737 | 98 |
lemma gcd_scm: "VARS a b x y |
| 13682 | 99 |
{0<A & 0<B & a=A & b=B & x=B & y=A}
|
100 |
WHILE a ~= b |
|
101 |
INV {0<a & 0<b & gcd A B = gcd a b & 2*A*B = a*x + b*y}
|
|
102 |
DO IF a<b THEN (b := b-a; x := x+y) ELSE (a := a-b; y := y+x) FI OD |
|
103 |
{a = gcd A B & 2*A*B = a*(x+y)}"
|
|
104 |
apply vcg |
|
105 |
apply simp |
|
| 16796 | 106 |
apply(simp add: distribs gcd_diff_r linorder_not_less gcd_diff_l) |
| 13682 | 107 |
apply(simp add: distribs gcd_nnn) |
108 |
done |
|
109 |
||
110 |
(** Power by iterated squaring and multiplication **) |
|
111 |
||
| 13737 | 112 |
lemma power_by_mult: "VARS a b c |
| 13682 | 113 |
{a=A & b=B}
|
114 |
c := (1::nat); |
|
115 |
WHILE b ~= 0 |
|
116 |
INV {A^B = c * a^b}
|
|
117 |
DO WHILE b mod 2 = 0 |
|
118 |
INV {A^B = c * a^b}
|
|
119 |
DO a := a*a; b := b div 2 OD; |
|
120 |
c := c*a; b := b - 1 |
|
121 |
OD |
|
122 |
{c = A^B}"
|
|
123 |
apply vcg_simp |
|
124 |
apply(case_tac "b") |
|
| 42154 | 125 |
apply simp |
| 13682 | 126 |
apply simp |
127 |
done |
|
128 |
||
129 |
(** Factorial **) |
|
130 |
||
| 13737 | 131 |
lemma factorial: "VARS a b |
| 13682 | 132 |
{a=A}
|
133 |
b := 1; |
|
| 63106 | 134 |
WHILE a > 0 |
| 13682 | 135 |
INV {fac A = b * fac a}
|
136 |
DO b := b*a; a := a - 1 OD |
|
137 |
{b = fac A}"
|
|
138 |
apply vcg_simp |
|
139 |
apply(clarsimp split: nat_diff_split) |
|
140 |
done |
|
141 |
||
| 63106 | 142 |
lemma factorial_time: "VARS a b t |
143 |
{a=A & t=0}
|
|
144 |
b := 1; t := t+1; |
|
145 |
WHILE a > 0 |
|
146 |
INV {fac A = b * fac a & a \<le> A & t = 2*(A-a)+1}
|
|
147 |
DO b := b*a; t := t+1; a := a - 1; t := t+1 OD |
|
148 |
{b = fac A & t = 2*A + 1}"
|
|
149 |
apply vcg_simp |
|
150 |
apply(clarsimp split: nat_diff_split) |
|
151 |
done |
|
152 |
||
| 13684 | 153 |
lemma [simp]: "1 \<le> i \<Longrightarrow> fac (i - Suc 0) * i = fac i" |
154 |
by(induct i, simp_all) |
|
155 |
||
| 63106 | 156 |
lemma factorial2: "VARS i f |
| 13684 | 157 |
{True}
|
158 |
i := (1::nat); f := 1; |
|
159 |
WHILE i <= n INV {f = fac(i - 1) & 1 <= i & i <= n+1}
|
|
160 |
DO f := f*i; i := i+1 OD |
|
161 |
{f = fac n}"
|
|
162 |
apply vcg_simp |
|
163 |
apply(subgoal_tac "i = Suc n") |
|
164 |
apply simp |
|
165 |
apply arith |
|
166 |
done |
|
| 13682 | 167 |
|
| 63106 | 168 |
lemma factorial2_time: "VARS i f t |
169 |
{t=0}
|
|
170 |
i := (1::nat); t := t+1; f := 1; t := t+1; |
|
171 |
WHILE i \<le> n INV {f = fac(i - 1) & 1 \<le> i & i \<le> n+1 & t = 2*(i-1)+2}
|
|
172 |
DO f := f*i; t := t+1; i := i+1; t := t+1 OD |
|
173 |
{f = fac n & t = 2*n+2}"
|
|
174 |
apply vcg_simp |
|
175 |
apply auto |
|
176 |
apply(subgoal_tac "i = Suc n") |
|
177 |
apply simp |
|
178 |
apply arith |
|
179 |
done |
|
180 |
||
| 13682 | 181 |
(** Square root **) |
182 |
||
183 |
(* the easy way: *) |
|
184 |
||
| 13737 | 185 |
lemma sqrt: "VARS r x |
| 13682 | 186 |
{True}
|
| 63106 | 187 |
r := (0::nat); |
188 |
WHILE (r+1)*(r+1) <= X |
|
189 |
INV {r*r \<le> X}
|
|
| 13682 | 190 |
DO r := r+1 OD |
191 |
{r*r <= X & X < (r+1)*(r+1)}"
|
|
192 |
apply vcg_simp |
|
193 |
done |
|
194 |
||
| 63106 | 195 |
lemma sqrt_time: "VARS r t |
196 |
{t=0}
|
|
197 |
r := (0::nat); t := t+1; |
|
198 |
WHILE (r+1)*(r+1) <= X |
|
199 |
INV {r*r \<le> X & t = r+1}
|
|
200 |
DO r := r+1; t := t+1 OD |
|
201 |
{r*r <= X & X < (r+1)*(r+1) & (t-1)*(t-1) \<le> X}"
|
|
202 |
apply vcg_simp |
|
203 |
done |
|
204 |
||
| 13682 | 205 |
(* without multiplication *) |
206 |
||
| 63106 | 207 |
lemma sqrt_without_multiplication: "VARS u w r |
208 |
{x=X}
|
|
209 |
u := 1; w := 1; r := (0::nat); |
|
210 |
WHILE w <= X |
|
211 |
INV {u = r+r+1 & w = (r+1)*(r+1) & r*r <= X}
|
|
| 13682 | 212 |
DO r := r + 1; w := w + u + 2; u := u + 2 OD |
213 |
{r*r <= X & X < (r+1)*(r+1)}"
|
|
214 |
apply vcg_simp |
|
215 |
done |
|
216 |
||
217 |
||
218 |
(*** LISTS ***) |
|
219 |
||
| 13737 | 220 |
lemma imperative_reverse: "VARS y x |
| 13682 | 221 |
{x=X}
|
222 |
y:=[]; |
|
223 |
WHILE x ~= [] |
|
224 |
INV {rev(x)@y = rev(X)}
|
|
225 |
DO y := (hd x # y); x := tl x OD |
|
226 |
{y=rev(X)}"
|
|
227 |
apply vcg_simp |
|
228 |
apply(simp add: neq_Nil_conv) |
|
229 |
apply auto |
|
230 |
done |
|
231 |
||
| 63106 | 232 |
lemma imperative_reverse_time: "VARS y x t |
233 |
{x=X & t=0}
|
|
234 |
y:=[]; t := t+1; |
|
235 |
WHILE x ~= [] |
|
236 |
INV {rev(x)@y = rev(X) & t = 2*(length y) + 1}
|
|
237 |
DO y := (hd x # y); t := t+1; x := tl x; t := t+1 OD |
|
238 |
{y=rev(X) & t = 2*length X + 1}"
|
|
239 |
apply vcg_simp |
|
240 |
apply(simp add: neq_Nil_conv) |
|
241 |
apply auto |
|
242 |
done |
|
243 |
||
| 13737 | 244 |
lemma imperative_append: "VARS x y |
| 13682 | 245 |
{x=X & y=Y}
|
246 |
x := rev(x); |
|
247 |
WHILE x~=[] |
|
248 |
INV {rev(x)@y = X@Y}
|
|
249 |
DO y := (hd x # y); |
|
250 |
x := tl x |
|
251 |
OD |
|
252 |
{y = X@Y}"
|
|
253 |
apply vcg_simp |
|
254 |
apply(simp add: neq_Nil_conv) |
|
255 |
apply auto |
|
256 |
done |
|
257 |
||
| 63106 | 258 |
lemma imperative_append_time_no_rev: "VARS x y t |
259 |
{x=X & y=Y}
|
|
260 |
x := rev(x); t := 0; |
|
261 |
WHILE x~=[] |
|
262 |
INV {rev(x)@y = X@Y & length x \<le> length X & t = 2 * (length X - length x)}
|
|
263 |
DO y := (hd x # y); t := t+1; |
|
264 |
x := tl x; t := t+1 |
|
265 |
OD |
|
266 |
{y = X@Y & t = 2 * length X}"
|
|
267 |
apply vcg_simp |
|
268 |
apply(simp add: neq_Nil_conv) |
|
269 |
apply auto |
|
270 |
done |
|
271 |
||
| 13682 | 272 |
|
273 |
(*** ARRAYS ***) |
|
274 |
||
275 |
(* Search for a key *) |
|
| 13737 | 276 |
lemma zero_search: "VARS A i |
| 13682 | 277 |
{True}
|
278 |
i := 0; |
|
279 |
WHILE i < length A & A!i ~= key |
|
280 |
INV {!j. j<i --> A!j ~= key}
|
|
281 |
DO i := i+1 OD |
|
282 |
{(i < length A --> A!i = key) &
|
|
283 |
(i = length A --> (!j. j < length A --> A!j ~= key))}" |
|
284 |
apply vcg_simp |
|
285 |
apply(blast elim!: less_SucE) |
|
286 |
done |
|
287 |
||
| 63106 | 288 |
lemma zero_search_time: "VARS A i t |
289 |
{t=0}
|
|
290 |
i := 0; t := t+1; |
|
291 |
WHILE i < length A & A!i ~= key |
|
292 |
INV {(\<forall>j. j<i --> A!j ~= key) & i \<le> length A & t = i+1}
|
|
293 |
DO i := i+1; t := t+1 OD |
|
294 |
{(i < length A --> A!i = key) &
|
|
295 |
(i = length A --> (!j. j < length A --> A!j ~= key)) & t \<le> length A + 1}" |
|
296 |
apply vcg_simp |
|
297 |
apply(blast elim!: less_SucE) |
|
298 |
done |
|
299 |
||
| 13682 | 300 |
(* |
301 |
The `partition' procedure for quicksort. |
|
302 |
`A' is the array to be sorted (modelled as a list). |
|
303 |
Elements of A must be of class order to infer at the end |
|
304 |
that the elements between u and l are equal to pivot. |
|
305 |
||
306 |
Ambiguity warnings of parser are due to := being used |
|
307 |
both for assignment and list update. |
|
308 |
*) |
|
309 |
lemma lem: "m - Suc 0 < n ==> m < Suc n" |
|
310 |
by arith |
|
311 |
||
312 |
||
313 |
lemma Partition: |
|
314 |
"[| leq == %A i. !k. k<i --> A!k <= pivot; |
|
315 |
geq == %A i. !k. i<k & k<length A --> pivot <= A!k |] ==> |
|
| 13737 | 316 |
VARS A u l |
| 13682 | 317 |
{0 < length(A::('a::order)list)}
|
318 |
l := 0; u := length A - Suc 0; |
|
319 |
WHILE l <= u |
|
320 |
INV {leq A l & geq A u & u<length A & l<=length A}
|
|
321 |
DO WHILE l < length A & A!l <= pivot |
|
322 |
INV {leq A l & geq A u & u<length A & l<=length A}
|
|
323 |
DO l := l+1 OD; |
|
324 |
WHILE 0 < u & pivot <= A!u |
|
325 |
INV {leq A l & geq A u & u<length A & l<=length A}
|
|
326 |
DO u := u - 1 OD; |
|
327 |
IF l <= u THEN A := A[l := A!u, u := A!l] ELSE SKIP FI |
|
328 |
OD |
|
329 |
{leq A u & (!k. u<k & k<l --> A!k = pivot) & geq A l}"
|
|
330 |
(* expand and delete abbreviations first *) |
|
| 58860 | 331 |
apply (simp) |
| 13682 | 332 |
apply (erule thin_rl)+ |
333 |
apply vcg_simp |
|
|
16733
236dfafbeb63
linear arithmetic now takes "&" in assumptions apart.
nipkow
parents:
16417
diff
changeset
|
334 |
apply (force simp: neq_Nil_conv) |
|
236dfafbeb63
linear arithmetic now takes "&" in assumptions apart.
nipkow
parents:
16417
diff
changeset
|
335 |
apply (blast elim!: less_SucE intro: Suc_leI) |
|
236dfafbeb63
linear arithmetic now takes "&" in assumptions apart.
nipkow
parents:
16417
diff
changeset
|
336 |
apply (blast elim!: less_SucE intro: less_imp_diff_less dest: lem) |
|
236dfafbeb63
linear arithmetic now takes "&" in assumptions apart.
nipkow
parents:
16417
diff
changeset
|
337 |
apply (force simp: nth_list_update) |
| 13682 | 338 |
done |
339 |
||
| 62390 | 340 |
end |