src/HOL/Hoare/Examples.thy
 author nipkow Mon Oct 28 14:29:51 2002 +0100 (2002-10-28) changeset 13682 91674c8a008b parent 5646 7c2ddbaf8b8c child 13684 48bfc2cc0938 permissions -rw-r--r--
conversion ML -> thy
```     1 (*  Title:      HOL/Hoare/Examples.thy
```
```     2     ID:         \$Id\$
```
```     3     Author:     Norbert Galm
```
```     4     Copyright   1998 TUM
```
```     5
```
```     6 Various examples.
```
```     7 *)
```
```     8
```
```     9 theory Examples = Hoare + Arith2:
```
```    10
```
```    11 (*** ARITHMETIC ***)
```
```    12
```
```    13 (** multiplication by successive addition **)
```
```    14
```
```    15 lemma multiply_by_add: "|- VARS m s a b.
```
```    16   {a=A & b=B}
```
```    17   m := 0; s := 0;
```
```    18   WHILE m~=a
```
```    19   INV {s=m*b & a=A & b=B}
```
```    20   DO s := s+b; m := m+(1::nat) OD
```
```    21   {s = A*B}"
```
```    22 by vcg_simp
```
```    23
```
```    24
```
```    25 (** Euclid's algorithm for GCD **)
```
```    26
```
```    27 lemma Euclid_GCD: "|- VARS a b.
```
```    28  {0<A & 0<B}
```
```    29  a := A; b := B;
```
```    30  WHILE  a~=b
```
```    31  INV {0<a & 0<b & gcd A B = gcd a b}
```
```    32  DO IF a<b THEN b := b-a ELSE a := a-b FI OD
```
```    33  {a = gcd A B}"
```
```    34 apply vcg
```
```    35 (*Now prove the verification conditions*)
```
```    36   apply auto
```
```    37   apply(simp add: gcd_diff_r less_imp_le)
```
```    38  apply(simp add: not_less_iff_le gcd_diff_l)
```
```    39 apply(erule gcd_nnn)
```
```    40 done
```
```    41
```
```    42 (** Dijkstra's extension of Euclid's algorithm for simultaneous GCD and SCM **)
```
```    43 (* From E.W. Disjkstra. Selected Writings on Computing, p 98 (EWD474),
```
```    44    where it is given without the invariant. Instead of defining scm
```
```    45    explicitly we have used the theorem scm x y = x*y/gcd x y and avoided
```
```    46    division by mupltiplying with gcd x y.
```
```    47 *)
```
```    48
```
```    49 lemmas distribs =
```
```    50   diff_mult_distrib diff_mult_distrib2 add_mult_distrib add_mult_distrib2
```
```    51
```
```    52 lemma gcd_scm: "|- VARS a b x y.
```
```    53  {0<A & 0<B & a=A & b=B & x=B & y=A}
```
```    54  WHILE  a ~= b
```
```    55  INV {0<a & 0<b & gcd A B = gcd a b & 2*A*B = a*x + b*y}
```
```    56  DO IF a<b THEN (b := b-a; x := x+y) ELSE (a := a-b; y := y+x) FI OD
```
```    57  {a = gcd A B & 2*A*B = a*(x+y)}"
```
```    58 apply vcg
```
```    59   apply simp
```
```    60  apply(simp add: distribs gcd_diff_r not_less_iff_le gcd_diff_l)
```
```    61  apply arith
```
```    62 apply(simp add: distribs gcd_nnn)
```
```    63 done
```
```    64
```
```    65 (** Power by iterated squaring and multiplication **)
```
```    66
```
```    67 lemma power_by_mult: "|- VARS a b c.
```
```    68  {a=A & b=B}
```
```    69  c := (1::nat);
```
```    70  WHILE b ~= 0
```
```    71  INV {A^B = c * a^b}
```
```    72  DO  WHILE b mod 2 = 0
```
```    73      INV {A^B = c * a^b}
```
```    74      DO  a := a*a; b := b div 2 OD;
```
```    75      c := c*a; b := b - 1
```
```    76  OD
```
```    77  {c = A^B}"
```
```    78 apply vcg_simp
```
```    79 apply(case_tac "b")
```
```    80  apply(simp add: mod_less)
```
```    81 apply simp
```
```    82 done
```
```    83
```
```    84 (** Factorial **)
```
```    85
```
```    86 lemma factorial: "|- VARS a b.
```
```    87  {a=A}
```
```    88  b := 1;
```
```    89  WHILE a ~= 0
```
```    90  INV {fac A = b * fac a}
```
```    91  DO b := b*a; a := a - 1 OD
```
```    92  {b = fac A}"
```
```    93 apply vcg_simp
```
```    94 apply(clarsimp split: nat_diff_split)
```
```    95 done
```
```    96
```
```    97
```
```    98 (** Square root **)
```
```    99
```
```   100 (* the easy way: *)
```
```   101
```
```   102 lemma sqrt: "|- VARS r x.
```
```   103  {True}
```
```   104  x := X; r := (0::nat);
```
```   105  WHILE (r+1)*(r+1) <= x
```
```   106  INV {r*r <= x & x=X}
```
```   107  DO r := r+1 OD
```
```   108  {r*r <= X & X < (r+1)*(r+1)}"
```
```   109 apply vcg_simp
```
```   110 apply auto
```
```   111 done
```
```   112
```
```   113 (* without multiplication *)
```
```   114
```
```   115 lemma sqrt_without_multiplication: "|- VARS u w r x.
```
```   116  {True}
```
```   117  x := X; u := 1; w := 1; r := (0::nat);
```
```   118  WHILE w <= x
```
```   119  INV {u = r+r+1 & w = (r+1)*(r+1) & r*r <= x & x=X}
```
```   120  DO r := r + 1; w := w + u + 2; u := u + 2 OD
```
```   121  {r*r <= X & X < (r+1)*(r+1)}"
```
```   122 apply vcg_simp
```
```   123 apply auto
```
```   124 done
```
```   125
```
```   126
```
```   127 (*** LISTS ***)
```
```   128
```
```   129 lemma imperative_reverse: "|- VARS y x.
```
```   130  {x=X}
```
```   131  y:=[];
```
```   132  WHILE x ~= []
```
```   133  INV {rev(x)@y = rev(X)}
```
```   134  DO y := (hd x # y); x := tl x OD
```
```   135  {y=rev(X)}"
```
```   136 apply vcg_simp
```
```   137  apply(simp add: neq_Nil_conv)
```
```   138  apply auto
```
```   139 done
```
```   140
```
```   141 lemma imperative_append: "|- VARS x y.
```
```   142  {x=X & y=Y}
```
```   143  x := rev(x);
```
```   144  WHILE x~=[]
```
```   145  INV {rev(x)@y = X@Y}
```
```   146  DO y := (hd x # y);
```
```   147     x := tl x
```
```   148  OD
```
```   149  {y = X@Y}"
```
```   150 apply vcg_simp
```
```   151 apply(simp add: neq_Nil_conv)
```
```   152 apply auto
```
```   153 done
```
```   154
```
```   155
```
```   156 (*** ARRAYS ***)
```
```   157
```
```   158 (* Search for a key *)
```
```   159 lemma zero_search: "|- VARS A i.
```
```   160  {True}
```
```   161  i := 0;
```
```   162  WHILE i < length A & A!i ~= key
```
```   163  INV {!j. j<i --> A!j ~= key}
```
```   164  DO i := i+1 OD
```
```   165  {(i < length A --> A!i = key) &
```
```   166   (i = length A --> (!j. j < length A --> A!j ~= key))}"
```
```   167 apply vcg_simp
```
```   168 apply(blast elim!: less_SucE)
```
```   169 done
```
```   170
```
```   171 (*
```
```   172 The `partition' procedure for quicksort.
```
```   173 `A' is the array to be sorted (modelled as a list).
```
```   174 Elements of A must be of class order to infer at the end
```
```   175 that the elements between u and l are equal to pivot.
```
```   176
```
```   177 Ambiguity warnings of parser are due to := being used
```
```   178 both for assignment and list update.
```
```   179 *)
```
```   180 lemma lem: "m - Suc 0 < n ==> m < Suc n"
```
```   181 by arith
```
```   182
```
```   183
```
```   184 lemma Partition:
```
```   185 "[| leq == %A i. !k. k<i --> A!k <= pivot;
```
```   186     geq == %A i. !k. i<k & k<length A --> pivot <= A!k |] ==>
```
```   187  |- VARS A u l.
```
```   188  {0 < length(A::('a::order)list)}
```
```   189  l := 0; u := length A - Suc 0;
```
```   190  WHILE l <= u
```
```   191   INV {leq A l & geq A u & u<length A & l<=length A}
```
```   192   DO WHILE l < length A & A!l <= pivot
```
```   193      INV {leq A l & geq A u & u<length A & l<=length A}
```
```   194      DO l := l+1 OD;
```
```   195      WHILE 0 < u & pivot <= A!u
```
```   196      INV {leq A l & geq A u  & u<length A & l<=length A}
```
```   197      DO u := u - 1 OD;
```
```   198      IF l <= u THEN A := A[l := A!u, u := A!l] ELSE SKIP FI
```
```   199   OD
```
```   200  {leq A u & (!k. u<k & k<l --> A!k = pivot) & geq A l}"
```
```   201 (* expand and delete abbreviations first *)
```
```   202 apply (simp);
```
```   203 apply (erule thin_rl)+
```
```   204 apply vcg_simp
```
```   205     apply (force simp: neq_Nil_conv)
```
```   206    apply (blast elim!: less_SucE intro: Suc_leI)
```
```   207   apply (blast elim!: less_SucE intro: less_imp_diff_less dest: lem)
```
```   208  apply (force simp: nth_list_update)
```
```   209 apply (auto intro: order_antisym)
```
```   210 done
```
```   211
```
`   212 end`