src/HOL/Hoare/Examples.ML
author wenzelm
Sat, 06 Oct 2001 00:02:46 +0200
changeset 11704 3c50a2cd6f00
parent 11701 3d51fbf81c17
child 12486 0ed8bdd883e0
permissions -rw-r--r--
* sane numerals (stage 2): plain "num" syntax (removed "#");
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
9393
c97111953a66 corrected header
oheimb
parents: 9147
diff changeset
     1
(*  Title:      HOL/Hoare/Examples.ML
1335
5e1c0540f285 New directory.
nipkow
parents:
diff changeset
     2
    ID:         $Id$
5646
7c2ddbaf8b8c New many-sorted version.
nipkow
parents: 5591
diff changeset
     3
    Author:     Norbert Galm & Tobias Nipkow
7c2ddbaf8b8c New many-sorted version.
nipkow
parents: 5591
diff changeset
     4
    Copyright   1998 TUM
1335
5e1c0540f285 New directory.
nipkow
parents:
diff changeset
     5
*)
5e1c0540f285 New directory.
nipkow
parents:
diff changeset
     6
5646
7c2ddbaf8b8c New many-sorted version.
nipkow
parents: 5591
diff changeset
     7
(*** ARITHMETIC ***)
1335
5e1c0540f285 New directory.
nipkow
parents:
diff changeset
     8
6802
655a16e16c01 added square root example.
nipkow
parents: 6162
diff changeset
     9
(** multiplication by successive addition **)
1335
5e1c0540f285 New directory.
nipkow
parents:
diff changeset
    10
9147
9a64807da023 corrected specifications and simplified proofs
oheimb
parents: 8791
diff changeset
    11
Goal "|- VARS m s a b. \
9a64807da023 corrected specifications and simplified proofs
oheimb
parents: 8791
diff changeset
    12
\  {a=A & b=B} \
9a64807da023 corrected specifications and simplified proofs
oheimb
parents: 8791
diff changeset
    13
\  m := 0; s := 0; \
5646
7c2ddbaf8b8c New many-sorted version.
nipkow
parents: 5591
diff changeset
    14
\  WHILE m~=a \
9147
9a64807da023 corrected specifications and simplified proofs
oheimb
parents: 8791
diff changeset
    15
\  INV {s=m*b & a=A & b=B} \  
11701
3d51fbf81c17 sane numerals (stage 1): added generic 1, removed 1' and 2 on nat,
wenzelm
parents: 11464
diff changeset
    16
\  DO s := s+b; m := m+(1::nat) OD \
9147
9a64807da023 corrected specifications and simplified proofs
oheimb
parents: 8791
diff changeset
    17
\  {s = A*B}"; 
6162
484adda70b65 expandshort
paulson
parents: 5983
diff changeset
    18
by (hoare_tac (Asm_full_simp_tac) 1);
1335
5e1c0540f285 New directory.
nipkow
parents:
diff changeset
    19
qed "multiply_by_add";
5e1c0540f285 New directory.
nipkow
parents:
diff changeset
    20
6802
655a16e16c01 added square root example.
nipkow
parents: 6162
diff changeset
    21
(** Euclid's algorithm for GCD **)
1335
5e1c0540f285 New directory.
nipkow
parents:
diff changeset
    22
5646
7c2ddbaf8b8c New many-sorted version.
nipkow
parents: 5591
diff changeset
    23
Goal "|- VARS a b. \
9147
9a64807da023 corrected specifications and simplified proofs
oheimb
parents: 8791
diff changeset
    24
\ {0<A & 0<B} \
9a64807da023 corrected specifications and simplified proofs
oheimb
parents: 8791
diff changeset
    25
\ a := A; b := B; \
5646
7c2ddbaf8b8c New many-sorted version.
nipkow
parents: 5591
diff changeset
    26
\ WHILE  a~=b  \
7c2ddbaf8b8c New many-sorted version.
nipkow
parents: 5591
diff changeset
    27
\ INV {0<a & 0<b & gcd A B = gcd a b} \
7c2ddbaf8b8c New many-sorted version.
nipkow
parents: 5591
diff changeset
    28
\ DO IF a<b THEN b := b-a ELSE a := a-b FI OD \
1335
5e1c0540f285 New directory.
nipkow
parents:
diff changeset
    29
\ {a = gcd A B}";
5646
7c2ddbaf8b8c New many-sorted version.
nipkow
parents: 5591
diff changeset
    30
by (hoare_tac (K all_tac) 1);
3372
6e472c8f0011 Replacement of "divides" by "dvd" from Divides.thy, and updating of proofs
paulson
parents: 2031
diff changeset
    31
(*Now prove the verification conditions*)
9147
9a64807da023 corrected specifications and simplified proofs
oheimb
parents: 8791
diff changeset
    32
by   Auto_tac;
9a64807da023 corrected specifications and simplified proofs
oheimb
parents: 8791
diff changeset
    33
by    (etac gcd_nnn 4);
9a64807da023 corrected specifications and simplified proofs
oheimb
parents: 8791
diff changeset
    34
by   (asm_full_simp_tac (simpset() addsimps [not_less_iff_le, gcd_diff_l]) 3);
9a64807da023 corrected specifications and simplified proofs
oheimb
parents: 8791
diff changeset
    35
by  (force_tac (claset(),
5646
7c2ddbaf8b8c New many-sorted version.
nipkow
parents: 5591
diff changeset
    36
               simpset() addsimps [not_less_iff_le, le_eq_less_or_eq]) 2);
7c2ddbaf8b8c New many-sorted version.
nipkow
parents: 5591
diff changeset
    37
by (asm_simp_tac (simpset() addsimps [gcd_diff_r,less_imp_le]) 1);
1335
5e1c0540f285 New directory.
nipkow
parents:
diff changeset
    38
qed "Euclid_GCD";
5e1c0540f285 New directory.
nipkow
parents:
diff changeset
    39
10123
9469c039ff57 *** empty log message ***
nipkow
parents: 9393
diff changeset
    40
(** Dijkstra's extension of Euclid's algorithm for simultaneous GCD and SCM **)
9469c039ff57 *** empty log message ***
nipkow
parents: 9393
diff changeset
    41
(* From E.W. Disjkstra. Selected Writings on Computing, p 98 (EWD474),
9469c039ff57 *** empty log message ***
nipkow
parents: 9393
diff changeset
    42
   where it is given without the invariant. Instead of defining scm
9469c039ff57 *** empty log message ***
nipkow
parents: 9393
diff changeset
    43
   explicitly we have used the theorem scm x y = x*y/gcd x y and avoided
9469c039ff57 *** empty log message ***
nipkow
parents: 9393
diff changeset
    44
   division by mupltiplying with gcd x y.
9469c039ff57 *** empty log message ***
nipkow
parents: 9393
diff changeset
    45
*)
9469c039ff57 *** empty log message ***
nipkow
parents: 9393
diff changeset
    46
9469c039ff57 *** empty log message ***
nipkow
parents: 9393
diff changeset
    47
val distribs =
9469c039ff57 *** empty log message ***
nipkow
parents: 9393
diff changeset
    48
  [diff_mult_distrib,diff_mult_distrib2,add_mult_distrib,add_mult_distrib2];
9469c039ff57 *** empty log message ***
nipkow
parents: 9393
diff changeset
    49
9469c039ff57 *** empty log message ***
nipkow
parents: 9393
diff changeset
    50
Goal "|- VARS a b x y. \
9469c039ff57 *** empty log message ***
nipkow
parents: 9393
diff changeset
    51
\ {0<A & 0<B & a=A & b=B & x=B & y=A} \
9469c039ff57 *** empty log message ***
nipkow
parents: 9393
diff changeset
    52
\ WHILE  a ~= b  \
11704
3c50a2cd6f00 * sane numerals (stage 2): plain "num" syntax (removed "#");
wenzelm
parents: 11701
diff changeset
    53
\ INV {0<a & 0<b & gcd A B = gcd a b & 2*A*B = a*x + b*y} \
10123
9469c039ff57 *** empty log message ***
nipkow
parents: 9393
diff changeset
    54
\ DO IF a<b THEN (b := b-a; x := x+y) ELSE (a := a-b; y := y+x) FI OD \
11704
3c50a2cd6f00 * sane numerals (stage 2): plain "num" syntax (removed "#");
wenzelm
parents: 11701
diff changeset
    55
\ {a = gcd A B & 2*A*B = a*(x+y)}";
10123
9469c039ff57 *** empty log message ***
nipkow
parents: 9393
diff changeset
    56
by (hoare_tac (K all_tac) 1);
9469c039ff57 *** empty log message ***
nipkow
parents: 9393
diff changeset
    57
by(Asm_simp_tac 1);
9469c039ff57 *** empty log message ***
nipkow
parents: 9393
diff changeset
    58
by(asm_simp_tac (simpset() addsimps
9469c039ff57 *** empty log message ***
nipkow
parents: 9393
diff changeset
    59
    (distribs @ [gcd_diff_r,not_less_iff_le, gcd_diff_l])) 1);
9469c039ff57 *** empty log message ***
nipkow
parents: 9393
diff changeset
    60
by(arith_tac 1);
9469c039ff57 *** empty log message ***
nipkow
parents: 9393
diff changeset
    61
by(asm_full_simp_tac (simpset() addsimps (distribs @ [gcd_nnn])) 1);
9469c039ff57 *** empty log message ***
nipkow
parents: 9393
diff changeset
    62
qed "gcd_scm";
9469c039ff57 *** empty log message ***
nipkow
parents: 9393
diff changeset
    63
6802
655a16e16c01 added square root example.
nipkow
parents: 6162
diff changeset
    64
(** Power by iterated squaring and multiplication **)
1335
5e1c0540f285 New directory.
nipkow
parents:
diff changeset
    65
5646
7c2ddbaf8b8c New many-sorted version.
nipkow
parents: 5591
diff changeset
    66
Goal "|- VARS a b c. \
7c2ddbaf8b8c New many-sorted version.
nipkow
parents: 5591
diff changeset
    67
\ {a=A & b=B} \
11701
3d51fbf81c17 sane numerals (stage 1): added generic 1, removed 1' and 2 on nat,
wenzelm
parents: 11464
diff changeset
    68
\ c := (1::nat); \
5646
7c2ddbaf8b8c New many-sorted version.
nipkow
parents: 5591
diff changeset
    69
\ WHILE b ~= 0 \
7c2ddbaf8b8c New many-sorted version.
nipkow
parents: 5591
diff changeset
    70
\ INV {A^B = c * a^b} \
11704
3c50a2cd6f00 * sane numerals (stage 2): plain "num" syntax (removed "#");
wenzelm
parents: 11701
diff changeset
    71
\ DO  WHILE b mod 2 = 0 \
5646
7c2ddbaf8b8c New many-sorted version.
nipkow
parents: 5591
diff changeset
    72
\     INV {A^B = c * a^b} \
11704
3c50a2cd6f00 * sane numerals (stage 2): plain "num" syntax (removed "#");
wenzelm
parents: 11701
diff changeset
    73
\     DO  a := a*a; b := b div 2 OD; \
11701
3d51fbf81c17 sane numerals (stage 1): added generic 1, removed 1' and 2 on nat,
wenzelm
parents: 11464
diff changeset
    74
\     c := c*a; b := b - 1 \
5646
7c2ddbaf8b8c New many-sorted version.
nipkow
parents: 5591
diff changeset
    75
\ OD \
4359
6f2986464280 Simplified proofs.
nipkow
parents: 4356
diff changeset
    76
\ {c = A^B}";
6162
484adda70b65 expandshort
paulson
parents: 5983
diff changeset
    77
by (hoare_tac (Asm_full_simp_tac) 1);
8442
96023903c2df case_tac now subsumes both boolean and datatype cases;
wenzelm
parents: 8423
diff changeset
    78
by (case_tac "b" 1);
9147
9a64807da023 corrected specifications and simplified proofs
oheimb
parents: 8791
diff changeset
    79
by  (asm_full_simp_tac (simpset() addsimps [mod_less]) 1);
8791
50b650d19641 changed 2 to #2
paulson
parents: 8442
diff changeset
    80
by (Asm_simp_tac 1);
1335
5e1c0540f285 New directory.
nipkow
parents:
diff changeset
    81
qed "power_by_mult";
5e1c0540f285 New directory.
nipkow
parents:
diff changeset
    82
6802
655a16e16c01 added square root example.
nipkow
parents: 6162
diff changeset
    83
(** Factorial **)
655a16e16c01 added square root example.
nipkow
parents: 6162
diff changeset
    84
5646
7c2ddbaf8b8c New many-sorted version.
nipkow
parents: 5591
diff changeset
    85
Goal "|- VARS a b. \
7c2ddbaf8b8c New many-sorted version.
nipkow
parents: 5591
diff changeset
    86
\ {a=A} \
7c2ddbaf8b8c New many-sorted version.
nipkow
parents: 5591
diff changeset
    87
\ b := 1; \
7c2ddbaf8b8c New many-sorted version.
nipkow
parents: 5591
diff changeset
    88
\ WHILE a ~= 0 \
7c2ddbaf8b8c New many-sorted version.
nipkow
parents: 5591
diff changeset
    89
\ INV {fac A = b * fac a} \
11701
3d51fbf81c17 sane numerals (stage 1): added generic 1, removed 1' and 2 on nat,
wenzelm
parents: 11464
diff changeset
    90
\ DO b := b*a; a := a - 1 OD \
1335
5e1c0540f285 New directory.
nipkow
parents:
diff changeset
    91
\ {b = fac A}";
10700
b18f417d0b62 cancel-factor simproc allows shorter proofs
paulson
parents: 10123
diff changeset
    92
by (hoare_tac (asm_full_simp_tac (simpset() addsplits [nat_diff_split])) 1);
b18f417d0b62 cancel-factor simproc allows shorter proofs
paulson
parents: 10123
diff changeset
    93
by Auto_tac;  
9147
9a64807da023 corrected specifications and simplified proofs
oheimb
parents: 8791
diff changeset
    94
qed "factorial";
1335
5e1c0540f285 New directory.
nipkow
parents:
diff changeset
    95
6802
655a16e16c01 added square root example.
nipkow
parents: 6162
diff changeset
    96
(** Square root **)
655a16e16c01 added square root example.
nipkow
parents: 6162
diff changeset
    97
6816
4267539284be unclosed comment.
nipkow
parents: 6802
diff changeset
    98
(* the easy way: *)
6802
655a16e16c01 added square root example.
nipkow
parents: 6162
diff changeset
    99
9147
9a64807da023 corrected specifications and simplified proofs
oheimb
parents: 8791
diff changeset
   100
Goal "|- VARS r x. \
9a64807da023 corrected specifications and simplified proofs
oheimb
parents: 8791
diff changeset
   101
\ {True} \
11701
3d51fbf81c17 sane numerals (stage 1): added generic 1, removed 1' and 2 on nat,
wenzelm
parents: 11464
diff changeset
   102
\ x := X; r := (0::nat); \
6802
655a16e16c01 added square root example.
nipkow
parents: 6162
diff changeset
   103
\ WHILE (r+1)*(r+1) <= x \
9147
9a64807da023 corrected specifications and simplified proofs
oheimb
parents: 8791
diff changeset
   104
\ INV {r*r <= x & x=X} \
6802
655a16e16c01 added square root example.
nipkow
parents: 6162
diff changeset
   105
\ DO r := r+1 OD \
9147
9a64807da023 corrected specifications and simplified proofs
oheimb
parents: 8791
diff changeset
   106
\ {r*r <= X & X < (r+1)*(r+1)}";
9a64807da023 corrected specifications and simplified proofs
oheimb
parents: 8791
diff changeset
   107
by (hoare_tac (SELECT_GOAL Auto_tac) 1);
6802
655a16e16c01 added square root example.
nipkow
parents: 6162
diff changeset
   108
qed "sqrt";
655a16e16c01 added square root example.
nipkow
parents: 6162
diff changeset
   109
655a16e16c01 added square root example.
nipkow
parents: 6162
diff changeset
   110
(* without multiplication *)
655a16e16c01 added square root example.
nipkow
parents: 6162
diff changeset
   111
9147
9a64807da023 corrected specifications and simplified proofs
oheimb
parents: 8791
diff changeset
   112
Goal "|- VARS u w r x. \
9a64807da023 corrected specifications and simplified proofs
oheimb
parents: 8791
diff changeset
   113
\ {True} \
11701
3d51fbf81c17 sane numerals (stage 1): added generic 1, removed 1' and 2 on nat,
wenzelm
parents: 11464
diff changeset
   114
\ x := X; u := 1; w := 1; r := (0::nat); \
6802
655a16e16c01 added square root example.
nipkow
parents: 6162
diff changeset
   115
\ WHILE w <= x \
9147
9a64807da023 corrected specifications and simplified proofs
oheimb
parents: 8791
diff changeset
   116
\ INV {u = r+r+1 & w = (r+1)*(r+1) & r*r <= x & x=X} \
11704
3c50a2cd6f00 * sane numerals (stage 2): plain "num" syntax (removed "#");
wenzelm
parents: 11701
diff changeset
   117
\ DO r := r + 1; w := w + u + 2; u := u + 2 OD \
9147
9a64807da023 corrected specifications and simplified proofs
oheimb
parents: 8791
diff changeset
   118
\ {r*r <= X & X < (r+1)*(r+1)}";
9a64807da023 corrected specifications and simplified proofs
oheimb
parents: 8791
diff changeset
   119
by (hoare_tac (SELECT_GOAL Auto_tac) 1);
6802
655a16e16c01 added square root example.
nipkow
parents: 6162
diff changeset
   120
qed "sqrt_without_multiplication";
655a16e16c01 added square root example.
nipkow
parents: 6162
diff changeset
   121
655a16e16c01 added square root example.
nipkow
parents: 6162
diff changeset
   122
5646
7c2ddbaf8b8c New many-sorted version.
nipkow
parents: 5591
diff changeset
   123
(*** LISTS ***)
7c2ddbaf8b8c New many-sorted version.
nipkow
parents: 5591
diff changeset
   124
7c2ddbaf8b8c New many-sorted version.
nipkow
parents: 5591
diff changeset
   125
Goal "|- VARS y x. \
7c2ddbaf8b8c New many-sorted version.
nipkow
parents: 5591
diff changeset
   126
\ {x=X} \
7c2ddbaf8b8c New many-sorted version.
nipkow
parents: 5591
diff changeset
   127
\ y:=[]; \
7c2ddbaf8b8c New many-sorted version.
nipkow
parents: 5591
diff changeset
   128
\ WHILE x ~= [] \
7c2ddbaf8b8c New many-sorted version.
nipkow
parents: 5591
diff changeset
   129
\ INV {rev(x)@y = rev(X)} \
7c2ddbaf8b8c New many-sorted version.
nipkow
parents: 5591
diff changeset
   130
\ DO y := (hd x # y); x := tl x OD \
7c2ddbaf8b8c New many-sorted version.
nipkow
parents: 5591
diff changeset
   131
\ {y=rev(X)}";
7c2ddbaf8b8c New many-sorted version.
nipkow
parents: 5591
diff changeset
   132
by (hoare_tac Asm_full_simp_tac 1);
9147
9a64807da023 corrected specifications and simplified proofs
oheimb
parents: 8791
diff changeset
   133
by  (asm_full_simp_tac (simpset() addsimps [neq_Nil_conv]) 1);
9a64807da023 corrected specifications and simplified proofs
oheimb
parents: 8791
diff changeset
   134
by  Auto_tac;
5646
7c2ddbaf8b8c New many-sorted version.
nipkow
parents: 5591
diff changeset
   135
qed "imperative_reverse";
7c2ddbaf8b8c New many-sorted version.
nipkow
parents: 5591
diff changeset
   136
7c2ddbaf8b8c New many-sorted version.
nipkow
parents: 5591
diff changeset
   137
Goal
7c2ddbaf8b8c New many-sorted version.
nipkow
parents: 5591
diff changeset
   138
"|- VARS x y. \
7c2ddbaf8b8c New many-sorted version.
nipkow
parents: 5591
diff changeset
   139
\ {x=X & y=Y} \
7c2ddbaf8b8c New many-sorted version.
nipkow
parents: 5591
diff changeset
   140
\ x := rev(x); \
7c2ddbaf8b8c New many-sorted version.
nipkow
parents: 5591
diff changeset
   141
\ WHILE x~=[] \
7c2ddbaf8b8c New many-sorted version.
nipkow
parents: 5591
diff changeset
   142
\ INV {rev(x)@y = X@Y} \
7c2ddbaf8b8c New many-sorted version.
nipkow
parents: 5591
diff changeset
   143
\ DO y := (hd x # y); \
7c2ddbaf8b8c New many-sorted version.
nipkow
parents: 5591
diff changeset
   144
\    x := tl x \
7c2ddbaf8b8c New many-sorted version.
nipkow
parents: 5591
diff changeset
   145
\ OD \
7c2ddbaf8b8c New many-sorted version.
nipkow
parents: 5591
diff changeset
   146
\ {y = X@Y}";
7c2ddbaf8b8c New many-sorted version.
nipkow
parents: 5591
diff changeset
   147
by (hoare_tac Asm_full_simp_tac 1);
9147
9a64807da023 corrected specifications and simplified proofs
oheimb
parents: 8791
diff changeset
   148
by  (asm_full_simp_tac (simpset() addsimps [neq_Nil_conv]) 1);
9a64807da023 corrected specifications and simplified proofs
oheimb
parents: 8791
diff changeset
   149
by  Auto_tac;
5646
7c2ddbaf8b8c New many-sorted version.
nipkow
parents: 5591
diff changeset
   150
qed "imperative_append";
7c2ddbaf8b8c New many-sorted version.
nipkow
parents: 5591
diff changeset
   151
7c2ddbaf8b8c New many-sorted version.
nipkow
parents: 5591
diff changeset
   152
7c2ddbaf8b8c New many-sorted version.
nipkow
parents: 5591
diff changeset
   153
(*** ARRAYS ***)
7c2ddbaf8b8c New many-sorted version.
nipkow
parents: 5591
diff changeset
   154
7c2ddbaf8b8c New many-sorted version.
nipkow
parents: 5591
diff changeset
   155
(* Search for 0 *)
7c2ddbaf8b8c New many-sorted version.
nipkow
parents: 5591
diff changeset
   156
Goal
7c2ddbaf8b8c New many-sorted version.
nipkow
parents: 5591
diff changeset
   157
"|- VARS A i. \
7c2ddbaf8b8c New many-sorted version.
nipkow
parents: 5591
diff changeset
   158
\ {True} \
7c2ddbaf8b8c New many-sorted version.
nipkow
parents: 5591
diff changeset
   159
\ i := 0; \
7c2ddbaf8b8c New many-sorted version.
nipkow
parents: 5591
diff changeset
   160
\ WHILE i < length A & A!i ~= 0 \
7c2ddbaf8b8c New many-sorted version.
nipkow
parents: 5591
diff changeset
   161
\ INV {!j. j<i --> A!j ~= 0} \
7c2ddbaf8b8c New many-sorted version.
nipkow
parents: 5591
diff changeset
   162
\ DO i := i+1 OD \
7c2ddbaf8b8c New many-sorted version.
nipkow
parents: 5591
diff changeset
   163
\ {(i < length A --> A!i = 0) & \
7c2ddbaf8b8c New many-sorted version.
nipkow
parents: 5591
diff changeset
   164
\  (i = length A --> (!j. j < length A --> A!j ~= 0))}";
7c2ddbaf8b8c New many-sorted version.
nipkow
parents: 5591
diff changeset
   165
by (hoare_tac Asm_full_simp_tac 1);
6162
484adda70b65 expandshort
paulson
parents: 5983
diff changeset
   166
by (blast_tac (claset() addSEs [less_SucE]) 1);
5646
7c2ddbaf8b8c New many-sorted version.
nipkow
parents: 5591
diff changeset
   167
qed "zero_search";
7c2ddbaf8b8c New many-sorted version.
nipkow
parents: 5591
diff changeset
   168
7c2ddbaf8b8c New many-sorted version.
nipkow
parents: 5591
diff changeset
   169
(* 
7c2ddbaf8b8c New many-sorted version.
nipkow
parents: 5591
diff changeset
   170
The `partition' procedure for quicksort.
7c2ddbaf8b8c New many-sorted version.
nipkow
parents: 5591
diff changeset
   171
`A' is the array to be sorted (modelled as a list).
7c2ddbaf8b8c New many-sorted version.
nipkow
parents: 5591
diff changeset
   172
Elements of A must be of class order to infer at the end
7c2ddbaf8b8c New many-sorted version.
nipkow
parents: 5591
diff changeset
   173
that the elements between u and l are equal to pivot.
7c2ddbaf8b8c New many-sorted version.
nipkow
parents: 5591
diff changeset
   174
7c2ddbaf8b8c New many-sorted version.
nipkow
parents: 5591
diff changeset
   175
Ambiguity warnings of parser are due to := being used
7c2ddbaf8b8c New many-sorted version.
nipkow
parents: 5591
diff changeset
   176
both for assignment and list update.
7c2ddbaf8b8c New many-sorted version.
nipkow
parents: 5591
diff changeset
   177
*)
11701
3d51fbf81c17 sane numerals (stage 1): added generic 1, removed 1' and 2 on nat,
wenzelm
parents: 11464
diff changeset
   178
Goal "m - Suc 0 < n ==> m < Suc n";
10962
cda180b1e2e0 deleted several obsolete lemmas from NatArith.ML
paulson
parents: 10700
diff changeset
   179
by (arith_tac 1);
cda180b1e2e0 deleted several obsolete lemmas from NatArith.ML
paulson
parents: 10700
diff changeset
   180
qed "lemma";
cda180b1e2e0 deleted several obsolete lemmas from NatArith.ML
paulson
parents: 10700
diff changeset
   181
5646
7c2ddbaf8b8c New many-sorted version.
nipkow
parents: 5591
diff changeset
   182
Goal
7c2ddbaf8b8c New many-sorted version.
nipkow
parents: 5591
diff changeset
   183
"[| leq == %A i. !k. k<i --> A!k <= pivot; \
7c2ddbaf8b8c New many-sorted version.
nipkow
parents: 5591
diff changeset
   184
\   geq == %A i. !k. i<k & k<length A --> pivot <= A!k |] ==> \
7c2ddbaf8b8c New many-sorted version.
nipkow
parents: 5591
diff changeset
   185
\ |- VARS A u l.\
7c2ddbaf8b8c New many-sorted version.
nipkow
parents: 5591
diff changeset
   186
\ {0 < length(A::('a::order)list)} \
11701
3d51fbf81c17 sane numerals (stage 1): added generic 1, removed 1' and 2 on nat,
wenzelm
parents: 11464
diff changeset
   187
\ l := 0; u := length A - Suc 0; \
5646
7c2ddbaf8b8c New many-sorted version.
nipkow
parents: 5591
diff changeset
   188
\ WHILE l <= u \
7c2ddbaf8b8c New many-sorted version.
nipkow
parents: 5591
diff changeset
   189
\  INV {leq A l & geq A u & u<length A & l<=length A} \
7c2ddbaf8b8c New many-sorted version.
nipkow
parents: 5591
diff changeset
   190
\  DO WHILE l < length A & A!l <= pivot \
7c2ddbaf8b8c New many-sorted version.
nipkow
parents: 5591
diff changeset
   191
\      INV {leq A l & geq A u & u<length A & l<=length A} \
7c2ddbaf8b8c New many-sorted version.
nipkow
parents: 5591
diff changeset
   192
\      DO l := l+1 OD; \
7c2ddbaf8b8c New many-sorted version.
nipkow
parents: 5591
diff changeset
   193
\     WHILE 0 < u & pivot <= A!u \
7c2ddbaf8b8c New many-sorted version.
nipkow
parents: 5591
diff changeset
   194
\      INV {leq A l & geq A u  & u<length A & l<=length A} \
11701
3d51fbf81c17 sane numerals (stage 1): added generic 1, removed 1' and 2 on nat,
wenzelm
parents: 11464
diff changeset
   195
\      DO u := u - 1 OD; \
5646
7c2ddbaf8b8c New many-sorted version.
nipkow
parents: 5591
diff changeset
   196
\     IF l <= u THEN A := A[l := A!u, u := A!l] ELSE SKIP FI \
7c2ddbaf8b8c New many-sorted version.
nipkow
parents: 5591
diff changeset
   197
\  OD \
7c2ddbaf8b8c New many-sorted version.
nipkow
parents: 5591
diff changeset
   198
\ {leq A u & (!k. u<k & k<l --> A!k = pivot) & geq A l}";
7c2ddbaf8b8c New many-sorted version.
nipkow
parents: 5591
diff changeset
   199
(* expand and delete abbreviations first *)
6162
484adda70b65 expandshort
paulson
parents: 5983
diff changeset
   200
by (asm_simp_tac HOL_basic_ss 1);
9147
9a64807da023 corrected specifications and simplified proofs
oheimb
parents: 8791
diff changeset
   201
by (REPEAT (etac thin_rl 1));
5646
7c2ddbaf8b8c New many-sorted version.
nipkow
parents: 5591
diff changeset
   202
by (hoare_tac Asm_full_simp_tac 1);
10700
b18f417d0b62 cancel-factor simproc allows shorter proofs
paulson
parents: 10123
diff changeset
   203
    by (force_tac (claset(), simpset() addsimps [neq_Nil_conv]) 1);
6162
484adda70b65 expandshort
paulson
parents: 5983
diff changeset
   204
   by (blast_tac (claset() addSEs [less_SucE] addIs [Suc_leI]) 1);
10962
cda180b1e2e0 deleted several obsolete lemmas from NatArith.ML
paulson
parents: 10700
diff changeset
   205
  by (blast_tac (claset() addSEs [less_SucE] 
cda180b1e2e0 deleted several obsolete lemmas from NatArith.ML
paulson
parents: 10700
diff changeset
   206
                          addIs [less_imp_diff_less] addDs [lemma]) 1);
10700
b18f417d0b62 cancel-factor simproc allows shorter proofs
paulson
parents: 10123
diff changeset
   207
 by (force_tac (claset(), simpset() addsimps [nth_list_update]) 1);
b18f417d0b62 cancel-factor simproc allows shorter proofs
paulson
parents: 10123
diff changeset
   208
by (auto_tac (claset() addIs [order_antisym], simpset()));
5646
7c2ddbaf8b8c New many-sorted version.
nipkow
parents: 5591
diff changeset
   209
qed "Partition";