| 1 |
% This software is licensed to you under the terms of a BSD-style license, |
| 2 |
% see bsdlic.txt for details. |
| 3 |
\input dotnot |
| 4 |
\headline{\ifnum\count0=1\hfil\else\centerline\title\fi} |
| 5 |
\def\title{A Short Program Whose Construction Isn't} |
| 6 |
\def\out{{\it out:\/}} |
| 7 |
\centerline{\bf \title} |
| 8 |
|
| 9 |
\beginsection Introduction |
| 10 |
|
| 11 |
This is a construction of a {\it Simple Program Whose Proof Isn't}, |
| 12 |
as D.~E.~Knuth titled his contribution to {\it Beauty Is Our |
| 13 |
Business, A Birthday Salute to Edsger W. Dijkstra}. Knuth's program |
| 14 |
is module 103 from \TeX. Given integer $n$ it prints a decimal |
| 15 |
fraction that approximates $n/2^{16}$ using just enough digits such |
| 16 |
that the fraction determines $n$ uniquely. Both David Gries (in |
| 17 |
{\it Binary to Decimal, One More Time}) and Knuth presented proofs |
| 18 |
in the same volume that use unique properties of $2^{16}$. But this |
| 19 |
program provides a nontrivial generalization, it complies to the |
| 20 |
requirements for any $q > n \ge 0$. We call it ``\out''. |
| 21 |
|
| 22 |
\beginsection Notation |
| 23 |
|
| 24 |
For program construction, that is developing the program {\it and\/} its |
| 25 |
proof hand in hand, Dijkstra proposed both a notation (see EWD |
| 26 |
1300) and a programming language (see {\it A Discipline Of |
| 27 |
Programming}) that we are going to use here---however, slightly |
| 28 |
modified, blocks are deliminated by $[$ and $]$. |
| 29 |
A dot denotes function application, like ``$f.x$'' for ``$f(x)$''. |
| 30 |
Dot and colon have the same precedence and are left associative; |
| 31 |
e.g., ``$d:hiext.i$'' means ``$(d:hiext).i$''. The precedence is |
| 32 |
higher than that of any other operator, e.g., ``$\neg(d \:= a).R0$'' |
| 33 |
means ``$\neg\bigl((d \:= a).R0\bigr)$''. |
| 34 |
|
| 35 |
For expressions $E$, $F$, and $G$ we notate substitution of $E$ by |
| 36 |
$F$ in $G$ as ``$(E \:= F).G$'', e.g., |
| 37 |
\f (x \:= x+y).(x > 0) \meq x+y > 0\qquad,\hbox{or}\\ |
| 38 |
\f \bigl((10+5) \:= 7\bigr).\bigl((10+5) * 2\bigr)\qquad=\qquad 14\qquad.\\ |
| 39 |
The last example shows that we don't constraint the |
| 40 |
substituion function to predicates but apply it to any expression, |
| 41 |
in this case to an integer expression, and that we allow any |
| 42 |
expression left of $\:=$, not only program variables. |
| 43 |
|
| 44 |
We denote a conventionally written set $\{t.x \mid r.x\}$ as $\<x : |
| 45 |
r.x : t.x\>$. The three colon separated fields denote the dummy |
| 46 |
variable ($x$), the range of the dummy ($r.x$) and the terms ($t.x$), |
| 47 |
that is the elements of the set. Quantified operators applied to |
| 48 |
a set are written just after the $\langle$, e. g., $\<\mx i : i < |
| 49 |
10 : i\>$ is the maximum integer exceeded by ten, and $\<\nbr i : |
| 50 |
0 \le i < 10 : i\>$ is the number of naturals exceeded by 10. |
| 51 |
|
| 52 |
Throughout this paper, the type of $m$, $n$, $i$, $j$, and $p$ is |
| 53 |
integer and of $q$ positive integer and of $r$ rational. The |
| 54 |
variables $d$ and $a$ are of type integer array with the lower |
| 55 |
bound of its domain being one. |
| 56 |
|
| 57 |
\beginsection Integer Artithmetic |
| 58 |
|
| 59 |
This construction of \out\ is based on |
| 60 |
\proclaim Theorem 0. |
| 61 |
A half open interval of length $j$ contains exactly $j$ integers. |
| 62 |
|
| 63 |
As programmers we all know this theorem for intervals with integral |
| 64 |
end points but its extension to arbitray end points might come as |
| 65 |
a surprise. We'll use the following corollaries in this paper. |
| 66 |
|
| 67 |
\proclaim Corollary 0. |
| 68 |
$m = \< \mx i : i \le p/q : i\>\meq p/q-1 < m \le p/q$ |
| 69 |
|
| 70 |
\proclaim Corollary 1. |
| 71 |
$m = \< \mx i : i < p/q : i\>\meq p/q-1 \le m < p/q$ |
| 72 |
|
| 73 |
\proclaim Corollary 2. |
| 74 |
$ p\div q = \< \mx i : i*q \le p : i\>$ |
| 75 |
|
| 76 |
\proclaim Corollary 3. |
| 77 |
$(p-1)\div q = \< \mx i : i*q < p : i\>$ |
| 78 |
|
| 79 |
\beginsection Specification of \out |
| 80 |
|
| 81 |
Given integers $n$, $q$ with $0 \le n < q$, \out\ computes a decimal |
| 82 |
representation of $n/q$. It stores the digits in an array $d$ with |
| 83 |
$d.lob = 1$. For any integer array $a$ with $a.lob=1$ let $a.s$ |
| 84 |
be the sum |
| 85 |
\f a.s = \< \su i : a.lob \le i \le a.hib : a.i/10^i\>\\ |
| 86 |
|
| 87 |
{\it d.s is close enough to $n/q$\/}: |
| 88 |
\nf R0: (n-1/2)/q \le d.s < (n+1/2)/q\\ |
| 89 |
|
| 90 |
{\it The length of $d$ is minimal\/}: |
| 91 |
\nf R1: \< \fa a : a.hib < d.hib : \neg(d \:= a).R0 \>\\ |
| 92 |
|
| 93 |
{\it |
| 94 |
If there is more than one approximation that is close enough to |
| 95 |
$n/q$ and yielded by an array of minimal length, $d.s$ is the closest one\/}: |
| 96 |
\nf R2: \<\nbr a : (d \:= a).R0 \mand a.hib = d.hib : a.s \> > 1 |
| 97 |
\mimpl |n/q - d.s| \le | n/q - a.s|\\ |
| 98 |
|
| 99 |
{\it d contains decimal digits\/}: |
| 100 |
\nf R3: \< \fa i : d.lob \le i \le d.hib : 0 \le d.i < 10\>\\ |
| 101 |
|
| 102 |
\beginsection Construction of \out |
| 103 |
|
| 104 |
For brevity, we define expressions that depend implicitly on $d$, the only |
| 105 |
variable in the state space of \out. |
| 106 |
\dnf L: d.hib\\Q: 10^L\!/q\\ |
| 107 |
\dnf N: 10^L*d.s\\J: \<r : (n-1/2)*Q \le r < (n+1/2)*Q : r\>\\ |
| 108 |
|
| 109 |
We introduced $N$ because it is an integer and $J$ because it is |
| 110 |
a half open interval of length $Q$. They are related to $R0$ by |
| 111 |
\nrf0 R0 \meq N \in J\qquad.\\ |
| 112 |
|
| 113 |
For the proof of (0) we observe |
| 114 |
\f R0 \\ |
| 115 |
\heq {multiply by $10^L$, definition of $N$ and $Q$} |
| 116 |
\f (n-1/2)*Q \le N < (n+1/2)*Q\\ |
| 117 |
\heq {Def. of $J$} |
| 118 |
\f N \in J\\ |
| 119 |
|
| 120 |
We construct a loop that starts with an empty array $d$, appends an |
| 121 |
integer to $d$ in each iteration and terminates |
| 122 |
with $R0 \mand R1$. |
| 123 |
|
| 124 |
Applying the Linear Search Theorem to find the minimal length |
| 125 |
we introduce the loop guard |
| 126 |
\nf G0: \< \fa a : a.hib = L : \neg(d \:= a).R0 \>\qquad.\\ |
| 127 |
|
| 128 |
Since $N$ is an integer and $J$ depends on $L$ only, not on the entries |
| 129 |
of $d$, (0) lets us conclude |
| 130 |
\f G0 \mff \<\nbr i : i \in J : i\> = 0\qquad.\\ |
| 131 |
\filbreak |
| 132 |
To test for $\<\nbr i : i \in J : i\> > 0$, we use a wittness $N$ |
| 133 |
that is in $J$ whenever $J$ contains an integer at all. Two candidates for |
| 134 |
$N$ come to mind: |
| 135 |
\df N = \< \mx i : i < (n+1/2)*Q : i\>\\ N = \< \mn i : (n-1/2)*Q\le i : i\>\\ |
| 136 |
\df \;\mand\\ \;\mand \\ |
| 137 |
\df \<\nbr i : i \in J : i\> = 0\\ \<\nbr i : i \in J : i\> = 0\\ |
| 138 |
\dh\eq {arithmetic} \eq {arithmetic} |
| 139 |
\df N < (n-1/2)*Q \\ (n+1/2)*Q \le N \\ |
| 140 |
\filbreak |
| 141 |
We decide for the left alternative. It leads to a nondecreasing |
| 142 |
sequence of the intermediate sums $d.s$, and that is equivalent to |
| 143 |
the entries in $d$ being naturals. We take |
| 144 |
|
| 145 |
\nf P0: N = \< \mx i : i < (n+1/2)*Q : i \> \\ |
| 146 |
\unparskip |
| 147 |
as a loop invariant and replace $G0$ by the loop guard |
| 148 |
\nf G1: N < (n-1/2)*Q\qquad.\\ |
| 149 |
\filbreak |
| 150 |
The program reads---with $E$ chosen such that $wp.``d:hiext.E".P0$ holds: |
| 151 |
\bblock |
| 152 |
d \vir int \array \:= (1) \co{R1; P0} |
| 153 |
;\do G1 \-> d:hiext.E \co{R1; P0} \od |
| 154 |
\cofl{\neg G1; R1; P0} |
| 155 |
\eblock |
| 156 |
We are going to prove that $P0$ and $R1$ are loop invariants. |
| 157 |
\doparskip |
| 158 |
\dnf P0 \hbox{ is established by the initialization}:\\ |
| 159 |
P0 \hbox{ is maintained}:\\ |
| 160 |
\df wp.``d \:= (1)".P0\\ |
| 161 |
wp.``d:hiext.E".P0\\ |
| 162 |
\dh\eq {$N, L \:= 0,0$} |
| 163 |
\eq {choice of $E$} |
| 164 |
\df 0 = \<\mx i : i < (n+1/2)/q : i\>\\ |
| 165 |
true\\ |
| 166 |
\heq {Corollary 1} |
| 167 |
\f (n+1/2)/q - 1 \le 0 < (n + 1/2)/q\\ |
| 168 |
\heq {$0 \le n$ implies the right $<$} |
| 169 |
\f n+1/2 \le q\\ |
| 170 |
\heq {$n$, $q$ are integers} |
| 171 |
\f n < q\\ |
| 172 |
\heq {precondition of \out} |
| 173 |
\f true\\ |
| 174 |
\doparskip |
| 175 |
\dnf R1\hbox{ is established by the initialization}:\\ |
| 176 |
R1\hbox{ is maintained}:\\ |
| 177 |
\df wp.``d \:= (1)".R1\\ |
| 178 |
wp.``d:hiext.E".R1\\ |
| 179 |
\dh\eq {$L \:= 0$} |
| 180 |
\eq {$L \:= L+1$} |
| 181 |
\df \<\fa i : 0 \le i < 0 : \neg(L \:= i).R0 \>\\ |
| 182 |
\< \fa i : 0 \le i < L+1 : \neg(L \:= i).R0 \>\\ |
| 183 |
\dh\eq {predicate calculus: empty range of $\fa$} |
| 184 |
\eq {predicate calculus: split range} |
| 185 |
\df true\\ |
| 186 |
\< \fa i : 0 \le i < L : \neg(L \:= i).R0 \> \mand \neg(L \:= L).R0\\ |
| 187 |
\dh {} {} \eq {Def. of $R1$} |
| 188 |
\df \\ R1 \mand \neg R0\\ |
| 189 |
\dh{} {} \eq {$R1$ is precondition} |
| 190 |
\df \\ \neg R0\\ |
| 191 |
\dh {} {} \ff {construction of $G1$} |
| 192 |
\df \\ G1\\ |
| 193 |
\dh {} {} \eq {$G1$ is precondition} |
| 194 |
\df \\ true\\ |
| 195 |
|
| 196 |
For the termination proof, we note that $L$ is incremented in each iteration |
| 197 |
and bounded from above: |
| 198 |
\f 10^L < q\\ |
| 199 |
\heq {Def. of $Q$, $q > 0$} |
| 200 |
\f Q < 1\\ |
| 201 |
\h\ff {$J$ is an half open interval of length $Q$; Theorem 0} |
| 202 |
\f \<\nbr i : i \in J : i\> = 0\\ |
| 203 |
\heq {$G1 \mand P0$} |
| 204 |
\f true\\ |
| 205 |
|
| 206 |
That finishes the proof of the above program and we are ready to turn to |
| 207 |
\nf R2: \<\nbr a : (d \:= a).R0 \mand a.hib = L : a.s \> > 1 |
| 208 |
\mimpl |n/q - d.s| \le | n/q - a.s|\qquad.\\ |
| 209 |
|
| 210 |
$R2$'s antecedent reduces to $\<\nbr i : i \in J : i\> > 1$, since |
| 211 |
for any $a$ with $a.hib = L$ the number $a.s*10^L$ is an integer, |
| 212 |
which is contained in $J$ precisely when $(d \:= a).R0$ holds. |
| 213 |
|
| 214 |
$R2$'s consequence is equivalent to |
| 215 |
\f n*Q - 1/2 \le N \le n*Q+1/2\qquad.\\ |
| 216 |
Since this condition does not always determine a unique solution |
| 217 |
for $N$, we impose the slightly stronger one constraining $N$ to |
| 218 |
a half open interval of length 1. |
| 219 |
|
| 220 |
\nf R2C: n*Q - 1/2 < N \le n*Q+1/2\qquad.\\ |
| 221 |
\filbreak |
| 222 |
$R2C$ looks quite different from $P0$. To avoid appending a "wrong" |
| 223 |
digit according to $P0$, we take a further loop invariant that |
| 224 |
implies the negation of $R2$'s antecedent. |
| 225 |
|
| 226 |
\f \<\nbr i : i \in J : i\> \le 1\\ |
| 227 |
\h\ff {$J$ is an half open interval of length $Q$; Theorem 0} |
| 228 |
\nf P1: Q \le 1\\ |
| 229 |
|
| 230 |
$P1$ is established by initialization: |
| 231 |
\f wp.``d \:= (1)".P1 \\ |
| 232 |
\heq {$Q \:= 1/q$} |
| 233 |
\f 1 \le q\\ |
| 234 |
\heq {$0 \le n < q$} |
| 235 |
\f true\\ |
| 236 |
|
| 237 |
We calculate guard $G2$ such that $P1$ is maintained: |
| 238 |
\f wp.``d:hiext.E".P1\\ |
| 239 |
\heq {$Q \:= 10*Q$} |
| 240 |
\nf G2: 10*Q \le 1\\ |
| 241 |
|
| 242 |
With $G2$, the loop might terminate while $G1$ is still true, that is without |
| 243 |
reaching $R0$. In that case, we compute the last digit according to $R2C$. |
| 244 |
|
| 245 |
The program now reads---with $E$ chosen such that $wp.``d:hiext.E".P0$ holds |
| 246 |
and $F$ chosen such that $wp.``d:hiext.F".R2C$ holds: |
| 247 |
|
| 248 |
\bblock |
| 249 |
d \vir int \array \:= (1) \co{P0\and R1\and P1} |
| 250 |
; \do G1 \mand G2 \-> d:hiext.E \co{P0\and R1\and P1} \od |
| 251 |
\cofl{\neg (G1 \and G2) \and P0 \and R1 \and P1} |
| 252 |
; \IF G1 \-> d:hiext.F \co{R2C} \| \neg G1 \-> skip\FI |
| 253 |
\cofl{R0\and R1\and R2} |
| 254 |
\eblock |
| 255 |
We will prove that the if-statement establishes the postconditions. |
| 256 |
We already have proven that the loop terminates and establishes |
| 257 |
its postconditions. According to the guards of the if-statement, |
| 258 |
our proof takes two cases: |
| 259 |
|
| 260 |
Case zero: The loop terminates with $\neg G1$: |
| 261 |
|
| 262 |
Here the if-statement selects the skip statement. Its predicate |
| 263 |
transformer, $wp.skip$, is the identity function. |
| 264 |
|
| 265 |
$R0$ is reached: |
| 266 |
\df (n-1/2)*Q \le N \\ N < (n+1/2)*Q \\ |
| 267 |
\dh\eq {$\neg G1$} \eq {$P0$} |
| 268 |
\df true \\ true \\ |
| 269 |
|
| 270 |
$R1$ is reached: $R1$ is a precondition of the if-statement. |
| 271 |
|
| 272 |
$R2$ is reached: $P1$ implies the negation of $R2$'s antecedent |
| 273 |
and is a precondition of the if-statement. |
| 274 |
|
| 275 |
This ends the proof for case zero. |
| 276 |
\filbreak |
| 277 |
Case one: The loop terminates with $G1$: |
| 278 |
|
| 279 |
$R0$ is reached: We show that $R0$ holds in the state after |
| 280 |
``d:hiext.F''. |
| 281 |
\nf R0: (n-1/2)*Q \le N < (n+1/2)*Q\\ |
| 282 |
\heq {towards $R2C$} |
| 283 |
\f n*Q -(1/2)*Q \le N < n*Q + (1/2)*Q\\ |
| 284 |
\h \ff {$1<Q$, see (**) below; transitiviy of $<$ and $=$} |
| 285 |
\f n*Q -1/2 \le N \le n*Q + 1/2\\ |
| 286 |
\heq {$F$ chosen sucht that $R2C$ holds} |
| 287 |
\f true\\ |
| 288 |
|
| 289 |
(**)$1<Q$ holds because |
| 290 |
\f wp.``d:hiext.F".(1<Q)\\ |
| 291 |
\heq {$Q \:= 10*Q$} |
| 292 |
\f 1 < 10*Q\\ |
| 293 |
\heq {Definition of $G2$} |
| 294 |
\f \neg G2\\ |
| 295 |
\heq {$G1 \mand (\neg G1 \mor \neg G2)$} |
| 296 |
\f true\\ |
| 297 |
|
| 298 |
$R1$ is reached: The proof for the invariance of $R1$ can be taken |
| 299 |
verbatim, since here we have the same preconditions ($G1; R1; P0$). |
| 300 |
|
| 301 |
$R2$ is reached: Its consequence $R2C$ is established by the choice of $F$. |
| 302 |
|
| 303 |
That finishes the proof for $R0$, $R1$, and $R2$. So far, we |
| 304 |
ignored $R3$. In fact, the final value of $d.s$ and $L$ is uniquely |
| 305 |
defined by the other three conditions. But the intermediate values |
| 306 |
of $d.s$ and---equivalently the entries of $d$---are not. We can |
| 307 |
only hope that \out\ delivers decimal digits. |
| 308 |
|
| 309 |
We show $0 \le E < 10$ in the state before |
| 310 |
``$d:hiext.E$'', where $wp.``d:hiext.E".P0$ holds. |
| 311 |
\f wp.``d:hiext.E".P0\\ |
| 312 |
\heq {$N, Q \:= 10*N+E, 10*Q$} |
| 313 |
\f 10*N + E = \<\mx i : i < (n+1/2)*10*Q : i \>\\ |
| 314 |
\heq {simplify} |
| 315 |
\f E = \<\mx i : i < \bigl((n+1/2)*Q - N\bigr)*10 : i \>\\ |
| 316 |
\heq {Corollary 1} |
| 317 |
\f \bigl((n+1/2)*Q - N\bigr)*10 - 1 \le E < \bigl((n+1/2)*Q-N\bigr)*10 \\ |
| 318 |
|
| 319 |
We observe: |
| 320 |
\f 0 \le E \\ |
| 321 |
\heq {trick, 0 and $E$ are integers} |
| 322 |
\df -1 < E \\ |
| 323 |
E < 10 \\ |
| 324 |
\dh\ff {above, left end} |
| 325 |
\ff {above, right end} |
| 326 |
\df -1 < \bigl((n+1/2)*Q - N\bigr)*10 - 1 \\ |
| 327 |
\bigl((n+1/2)*Q - N\bigr)*10 \le 10 \\ |
| 328 |
\dh \eq {simplify} |
| 329 |
\eq {simplify} |
| 330 |
\df N < (n+1/2)*Q\\ |
| 331 |
(n+1/2)*Q \le N + 1\\ |
| 332 |
\dh \eq {$P0$, right end} |
| 333 |
\eq {$P0$, left end} |
| 334 |
\df true\\ |
| 335 |
true\\ |
| 336 |
\filbreak |
| 337 |
So far so good! Now $F$. |
| 338 |
We show $0 \le F < 10$ in the state before |
| 339 |
``$d:hiext.F$'', where $wp.``d:hiext.F".R2C$ holds. |
| 340 |
\f wp.``d:hiext.F".R2C \\ |
| 341 |
\heq {$N, Q \:= 10*N+F, 10*Q$} |
| 342 |
\f (n*Q-N)*10 - 1/2 < F \le (n*Q-N)*10 + 1/2\\ |
| 343 |
\filbreak |
| 344 |
We observe: |
| 345 |
\df 0 \le F \\ |
| 346 |
F < 10 \\ |
| 347 |
\dh \eq {trick, 0 and $F$ are integers} |
| 348 |
\ff {see above, right end} |
| 349 |
\df -1 < F\\ |
| 350 |
(n*Q-N)*10 + 1/2 < 10\\ |
| 351 |
\dh \ff {see above, left end} |
| 352 |
\eq {heading for $P0$, left end} |
| 353 |
\df -1 \le (n*Q-N)*10 - 1/2\\ |
| 354 |
(n*Q-1)*10 + 1/2 < N*10\\ |
| 355 |
\dh \eq {simplify} |
| 356 |
\ff {$P0$, left end} |
| 357 |
\df -1/2 \le (n*Q-N)*10\\ |
| 358 |
(n*Q-1)*10 + 1/2 < (n+1/2)*Q*10-10\\ |
| 359 |
\dh \ff {arithmetic} |
| 360 |
\eq {simplify} |
| 361 |
\df N < n*Q\\ |
| 362 |
1/2 < (1/2)*Q*10\\ |
| 363 |
\dh\eq {$G1$} |
| 364 |
\eq {$\neg G2$} |
| 365 |
\df true\\ |
| 366 |
true\\ |
| 367 |
\filbreak |
| 368 |
|
| 369 |
Now that \out\ is proved correct---lucky as we are, $R3$ holds---, |
| 370 |
we reduce the expressions to integer arithmetics. We start with |
| 371 |
\nf G2: 10*Q \le 1\\ |
| 372 |
\heq {$Q=10^L\!/q$} |
| 373 |
\f 10^{L+1} \le q\\ |
| 374 |
\heq {new variable $u$ with invariant $u = 10^{L+1}$} |
| 375 |
\f u \le q\\ |
| 376 |
|
| 377 |
The invariant of $u$ is established by $u \:= 10$ and maintained |
| 378 |
by $u \:= 10*u$. |
| 379 |
\filbreak |
| 380 |
Next, we reduce |
| 381 |
\nf G1: N < (n-1/2)*Q \\ |
| 382 |
\heq {expand with $q$, reuse $u$} |
| 383 |
\f N*q*10 < (n-1/2)*u\\ |
| 384 |
\heq {think positive} |
| 385 |
\f u/2 < n*u - N*q*10\\ |
| 386 |
\heq {$u = 10^{L+1}$ is even} |
| 387 |
\f u \div 2 < n*u - N*q*10\\ |
| 388 |
\heq {new variable $v$ with invariant $v=n*u-N*q*10$} |
| 389 |
\f u \div 2 < v\\ |
| 390 |
\filbreak |
| 391 |
The expressions $n*u$ and $N*q*10$ might take large values. To avoid integer |
| 392 |
overflow, we store their difference in $v$. The invariant of $v$ is established |
| 393 |
by $v \:= 10*n$. |
| 394 |
We calculate $V$ such that $v$'s invariant is maintained by $v \:= V$. |
| 395 |
\f wp.``d:hiext.E; u, v \:= 10*u, V".(v = n*u - N*q*10)\\ |
| 396 |
\heq {semantics of the semicolon} |
| 397 |
\f wp.``d:hiext.E".\bigl(wp.``u, v \:= 10*u, V".(v = n*u - N*q*10)\bigr)\\ |
| 398 |
\heq {semantics of the assignment} |
| 399 |
\f wp.``d:hiext.E".(V = n*10*u - N*q*10)\\ |
| 400 |
\heq {array semantics: $N \:= 10*N+E$} |
| 401 |
\f V = n*10*u - (10*N+E)*q*10\\ |
| 402 |
\heq {simplify} |
| 403 |
\f V = (n*u - 10*N*q-E*q)*10\\ |
| 404 |
\heq {invariant for $v$} |
| 405 |
\f V = (v-E*q)*10\\ |
| 406 |
\filbreak |
| 407 |
Reducing $E$ and $F$ to $\div$ is straight forward by the well |
| 408 |
known Corollary 2 and the slightly less well known Corollary 3. |
| 409 |
|
| 410 |
\dnf E: \<\mx i : i < \bigl((n+1/2)*Q - N\bigr)*10 : i \>\\ |
| 411 |
F: \<\mx i : i \le (n*Q - N)*10 + 1/2 : i \>\\ |
| 412 |
\dh= {expand with $q$} |
| 413 |
= {expand with $q$, reuse $u$} |
| 414 |
\df \<\mx i : i*q < (n+1/2)*10^{L+1} - N*q*10 : i \>\\ |
| 415 |
\<\mx i : i*q \le n*u - N*q*10 + q/2 : i \>\\ |
| 416 |
\dh= {reuse $u$} |
| 417 |
= {reuse $v$} |
| 418 |
\df \<\mx i : i*q < n*u+ u/2 - N*q*10 : i \>\\ |
| 419 |
\<\mx i : i*q \le v + q/2 : i \>\\ |
| 420 |
\dh= {reuse $v$ , $u$ is even} |
| 421 |
= {see (*)} |
| 422 |
\df \<\mx i : i*q < v + u \div 2 : i \>\\ |
| 423 |
\<\mx i : i*q \le v + q \div 2 : i \>\\ |
| 424 |
\dh= {Corollary 3} |
| 425 |
= {Corollary 2} |
| 426 |
\df (v + u \div 2 - 1) \div q\\ |
| 427 |
(v + q \div 2) \div q\\ |
| 428 |
\filbreak |
| 429 |
The equation at (*) holds when $q$ is even, since then $q/2 = q \div 2$. |
| 430 |
And when $q$ is odd, we have $q/2 = q \div 2 + 1/2$ and we observe |
| 431 |
\f i*q \le v + q/2\\ |
| 432 |
\heq {$q$ is odd} |
| 433 |
\f i*q \le v + q \div 2 + 1/2\\ |
| 434 |
\heq {$i*q$ and $v + q \div 2$ are integers} |
| 435 |
\f i*q \le v + q \div 2\\ |
| 436 |
\filbreak |
| 437 |
This ends the reduction of \out\ to integer arithmetic and the construction |
| 438 |
of our program. |
| 439 |
\bblock |
| 440 |
\out\; \[\glocon n, q; \virvar d \co {0 \le n < q} |
| 441 |
; \privar u, v |
| 442 |
; d \vir int \array, u \vir int, v \vir int \:= (1), 10, n*10 |
| 443 |
; \do u \div 2 < v \mand u \le q \->\&% |
| 444 |
d:hiext.\bigl((v + u \div 2 - 1) \div q\bigr) |
| 445 |
; u, v \:= 10*u, (v - d.high*q)*10 \decind |
| 446 |
\od |
| 447 |
; \IF u \div 2 < v \-> d:hiext.\bigl((v + q \div 2) \div q\bigr) |
| 448 |
\| u \div 2 \ge v \-> skip |
| 449 |
\FI |
| 450 |
\] \co {R0 \mand R1 \mand R2 \mand R3} |
| 451 |
\eblock |
| 452 |
That's it. The program is only proved correct, not tested. The |
| 453 |
proof and/or the program might contain errors. Please don't hesitate |
| 454 |
to contact me if you found one, or if you know a better way to |
| 455 |
apply Dijkstra's technique to a problem that turned out to be |
| 456 |
surprisingly hard. |
| 457 |
\vfill |
| 458 |
\bi |
| 459 |
Wolfgang Helbig\hfill {\tt [email protected]}\qquad\bi |
| 460 |
Stauferstr. 22\hfill {\tt wwwlehre.ba-stuttgart.de/\~{}helbig/}\qquad\bi |
| 461 |
71334 Waiblingen\hfill October 2008\qquad\hfill |
| 462 |
\eject |
| 463 |
\end |