src/HOL/Hoare/ExamplesTC.thy
author nipkow
Fri, 04 Dec 2020 15:16:03 +0100
changeset 72807 ea189da0ff60
child 72990 db8f94656024
permissions -rw-r--r--
Total correctness examples by Walter Guttmann
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
72807
ea189da0ff60 Total correctness examples by Walter Guttmann
nipkow
parents:
diff changeset
     1
(* Title:      Examples using Hoare Logic for Total Correctness
ea189da0ff60 Total correctness examples by Walter Guttmann
nipkow
parents:
diff changeset
     2
   Author:     Walter Guttmann
ea189da0ff60 Total correctness examples by Walter Guttmann
nipkow
parents:
diff changeset
     3
*)
ea189da0ff60 Total correctness examples by Walter Guttmann
nipkow
parents:
diff changeset
     4
ea189da0ff60 Total correctness examples by Walter Guttmann
nipkow
parents:
diff changeset
     5
section \<open>Examples using Hoare Logic for Total Correctness\<close>
ea189da0ff60 Total correctness examples by Walter Guttmann
nipkow
parents:
diff changeset
     6
ea189da0ff60 Total correctness examples by Walter Guttmann
nipkow
parents:
diff changeset
     7
theory ExamplesTC
ea189da0ff60 Total correctness examples by Walter Guttmann
nipkow
parents:
diff changeset
     8
ea189da0ff60 Total correctness examples by Walter Guttmann
nipkow
parents:
diff changeset
     9
imports Hoare_Logic
ea189da0ff60 Total correctness examples by Walter Guttmann
nipkow
parents:
diff changeset
    10
ea189da0ff60 Total correctness examples by Walter Guttmann
nipkow
parents:
diff changeset
    11
begin
ea189da0ff60 Total correctness examples by Walter Guttmann
nipkow
parents:
diff changeset
    12
ea189da0ff60 Total correctness examples by Walter Guttmann
nipkow
parents:
diff changeset
    13
text \<open>
ea189da0ff60 Total correctness examples by Walter Guttmann
nipkow
parents:
diff changeset
    14
This theory demonstrates a few simple partial- and total-correctness proofs.
ea189da0ff60 Total correctness examples by Walter Guttmann
nipkow
parents:
diff changeset
    15
The first example is taken from HOL/Hoare/Examples.thy written by N. Galm.
ea189da0ff60 Total correctness examples by Walter Guttmann
nipkow
parents:
diff changeset
    16
We have added the invariant \<open>m \<le> a\<close>.
ea189da0ff60 Total correctness examples by Walter Guttmann
nipkow
parents:
diff changeset
    17
\<close>
ea189da0ff60 Total correctness examples by Walter Guttmann
nipkow
parents:
diff changeset
    18
ea189da0ff60 Total correctness examples by Walter Guttmann
nipkow
parents:
diff changeset
    19
lemma multiply_by_add: "VARS m s a b
ea189da0ff60 Total correctness examples by Walter Guttmann
nipkow
parents:
diff changeset
    20
  {a=A \<and> b=B}
ea189da0ff60 Total correctness examples by Walter Guttmann
nipkow
parents:
diff changeset
    21
  m := 0; s := 0;
ea189da0ff60 Total correctness examples by Walter Guttmann
nipkow
parents:
diff changeset
    22
  WHILE m\<noteq>a
ea189da0ff60 Total correctness examples by Walter Guttmann
nipkow
parents:
diff changeset
    23
  INV {s=m*b \<and> a=A \<and> b=B \<and> m\<le>a}
ea189da0ff60 Total correctness examples by Walter Guttmann
nipkow
parents:
diff changeset
    24
  DO s := s+b; m := m+(1::nat) OD
ea189da0ff60 Total correctness examples by Walter Guttmann
nipkow
parents:
diff changeset
    25
  {s = A*B}"
ea189da0ff60 Total correctness examples by Walter Guttmann
nipkow
parents:
diff changeset
    26
  by vcg_simp
ea189da0ff60 Total correctness examples by Walter Guttmann
nipkow
parents:
diff changeset
    27
ea189da0ff60 Total correctness examples by Walter Guttmann
nipkow
parents:
diff changeset
    28
text \<open>
ea189da0ff60 Total correctness examples by Walter Guttmann
nipkow
parents:
diff changeset
    29
Here is the total-correctness proof for the same program.
ea189da0ff60 Total correctness examples by Walter Guttmann
nipkow
parents:
diff changeset
    30
It needs the additional invariant \<open>m \<le> a\<close>.
ea189da0ff60 Total correctness examples by Walter Guttmann
nipkow
parents:
diff changeset
    31
\<close>
ea189da0ff60 Total correctness examples by Walter Guttmann
nipkow
parents:
diff changeset
    32
ea189da0ff60 Total correctness examples by Walter Guttmann
nipkow
parents:
diff changeset
    33
lemma multiply_by_add_tc: "VARS m s a b
ea189da0ff60 Total correctness examples by Walter Guttmann
nipkow
parents:
diff changeset
    34
  [a=A \<and> b=B]
ea189da0ff60 Total correctness examples by Walter Guttmann
nipkow
parents:
diff changeset
    35
  m := 0; s := 0;
ea189da0ff60 Total correctness examples by Walter Guttmann
nipkow
parents:
diff changeset
    36
  WHILE m\<noteq>a
ea189da0ff60 Total correctness examples by Walter Guttmann
nipkow
parents:
diff changeset
    37
  INV {s=m*b \<and> a=A \<and> b=B \<and> m\<le>a}
ea189da0ff60 Total correctness examples by Walter Guttmann
nipkow
parents:
diff changeset
    38
  VAR {a-m}
ea189da0ff60 Total correctness examples by Walter Guttmann
nipkow
parents:
diff changeset
    39
  DO s := s+b; m := m+(1::nat) OD
ea189da0ff60 Total correctness examples by Walter Guttmann
nipkow
parents:
diff changeset
    40
  [s = A*B]"
ea189da0ff60 Total correctness examples by Walter Guttmann
nipkow
parents:
diff changeset
    41
  apply vcg_tc_simp
ea189da0ff60 Total correctness examples by Walter Guttmann
nipkow
parents:
diff changeset
    42
  by auto
ea189da0ff60 Total correctness examples by Walter Guttmann
nipkow
parents:
diff changeset
    43
ea189da0ff60 Total correctness examples by Walter Guttmann
nipkow
parents:
diff changeset
    44
text \<open>
ea189da0ff60 Total correctness examples by Walter Guttmann
nipkow
parents:
diff changeset
    45
Next, we prove partial correctness of a program that computes powers.
ea189da0ff60 Total correctness examples by Walter Guttmann
nipkow
parents:
diff changeset
    46
\<close>
ea189da0ff60 Total correctness examples by Walter Guttmann
nipkow
parents:
diff changeset
    47
ea189da0ff60 Total correctness examples by Walter Guttmann
nipkow
parents:
diff changeset
    48
lemma power: "VARS (p::int) i
ea189da0ff60 Total correctness examples by Walter Guttmann
nipkow
parents:
diff changeset
    49
  { True }
ea189da0ff60 Total correctness examples by Walter Guttmann
nipkow
parents:
diff changeset
    50
  p := 1;
ea189da0ff60 Total correctness examples by Walter Guttmann
nipkow
parents:
diff changeset
    51
  i := 0;
ea189da0ff60 Total correctness examples by Walter Guttmann
nipkow
parents:
diff changeset
    52
  WHILE i < n
ea189da0ff60 Total correctness examples by Walter Guttmann
nipkow
parents:
diff changeset
    53
    INV { p = x^i \<and> i \<le> n }
ea189da0ff60 Total correctness examples by Walter Guttmann
nipkow
parents:
diff changeset
    54
     DO p := p * x;
ea189da0ff60 Total correctness examples by Walter Guttmann
nipkow
parents:
diff changeset
    55
        i := i + 1
ea189da0ff60 Total correctness examples by Walter Guttmann
nipkow
parents:
diff changeset
    56
     OD
ea189da0ff60 Total correctness examples by Walter Guttmann
nipkow
parents:
diff changeset
    57
  { p = x^n }"
ea189da0ff60 Total correctness examples by Walter Guttmann
nipkow
parents:
diff changeset
    58
  apply vcg_simp
ea189da0ff60 Total correctness examples by Walter Guttmann
nipkow
parents:
diff changeset
    59
  by auto
ea189da0ff60 Total correctness examples by Walter Guttmann
nipkow
parents:
diff changeset
    60
ea189da0ff60 Total correctness examples by Walter Guttmann
nipkow
parents:
diff changeset
    61
text \<open>
ea189da0ff60 Total correctness examples by Walter Guttmann
nipkow
parents:
diff changeset
    62
Here is its total-correctness proof.
ea189da0ff60 Total correctness examples by Walter Guttmann
nipkow
parents:
diff changeset
    63
\<close>
ea189da0ff60 Total correctness examples by Walter Guttmann
nipkow
parents:
diff changeset
    64
ea189da0ff60 Total correctness examples by Walter Guttmann
nipkow
parents:
diff changeset
    65
lemma power_tc: "VARS (p::int) i
ea189da0ff60 Total correctness examples by Walter Guttmann
nipkow
parents:
diff changeset
    66
  [ True ]
ea189da0ff60 Total correctness examples by Walter Guttmann
nipkow
parents:
diff changeset
    67
  p := 1;
ea189da0ff60 Total correctness examples by Walter Guttmann
nipkow
parents:
diff changeset
    68
  i := 0;
ea189da0ff60 Total correctness examples by Walter Guttmann
nipkow
parents:
diff changeset
    69
  WHILE i < n
ea189da0ff60 Total correctness examples by Walter Guttmann
nipkow
parents:
diff changeset
    70
    INV { p = x^i \<and> i \<le> n }
ea189da0ff60 Total correctness examples by Walter Guttmann
nipkow
parents:
diff changeset
    71
    VAR { n - i }
ea189da0ff60 Total correctness examples by Walter Guttmann
nipkow
parents:
diff changeset
    72
     DO p := p * x;
ea189da0ff60 Total correctness examples by Walter Guttmann
nipkow
parents:
diff changeset
    73
        i := i + 1
ea189da0ff60 Total correctness examples by Walter Guttmann
nipkow
parents:
diff changeset
    74
     OD
ea189da0ff60 Total correctness examples by Walter Guttmann
nipkow
parents:
diff changeset
    75
  [ p = x^n ]"
ea189da0ff60 Total correctness examples by Walter Guttmann
nipkow
parents:
diff changeset
    76
  apply vcg_tc
ea189da0ff60 Total correctness examples by Walter Guttmann
nipkow
parents:
diff changeset
    77
  by auto
ea189da0ff60 Total correctness examples by Walter Guttmann
nipkow
parents:
diff changeset
    78
ea189da0ff60 Total correctness examples by Walter Guttmann
nipkow
parents:
diff changeset
    79
text \<open>
ea189da0ff60 Total correctness examples by Walter Guttmann
nipkow
parents:
diff changeset
    80
The last example is again taken from HOL/Hoare/Examples.thy.
ea189da0ff60 Total correctness examples by Walter Guttmann
nipkow
parents:
diff changeset
    81
We have modified it to integers so it requires precondition \<open>0 \<le> x\<close>.
ea189da0ff60 Total correctness examples by Walter Guttmann
nipkow
parents:
diff changeset
    82
\<close>
ea189da0ff60 Total correctness examples by Walter Guttmann
nipkow
parents:
diff changeset
    83
ea189da0ff60 Total correctness examples by Walter Guttmann
nipkow
parents:
diff changeset
    84
lemma sqrt_tc: "VARS r
ea189da0ff60 Total correctness examples by Walter Guttmann
nipkow
parents:
diff changeset
    85
  [0 \<le> (x::int)]
ea189da0ff60 Total correctness examples by Walter Guttmann
nipkow
parents:
diff changeset
    86
  r := 0;
ea189da0ff60 Total correctness examples by Walter Guttmann
nipkow
parents:
diff changeset
    87
  WHILE (r+1)*(r+1) <= x
ea189da0ff60 Total correctness examples by Walter Guttmann
nipkow
parents:
diff changeset
    88
  INV {r*r \<le> x}
ea189da0ff60 Total correctness examples by Walter Guttmann
nipkow
parents:
diff changeset
    89
  VAR {nat (x-r)}
ea189da0ff60 Total correctness examples by Walter Guttmann
nipkow
parents:
diff changeset
    90
  DO r := r+1 OD
ea189da0ff60 Total correctness examples by Walter Guttmann
nipkow
parents:
diff changeset
    91
  [r*r \<le> x \<and> x < (r+1)*(r+1)]"
ea189da0ff60 Total correctness examples by Walter Guttmann
nipkow
parents:
diff changeset
    92
  apply vcg_tc_simp
ea189da0ff60 Total correctness examples by Walter Guttmann
nipkow
parents:
diff changeset
    93
  by (smt (verit) div_pos_pos_trivial mult_less_0_iff nonzero_mult_div_cancel_left)
ea189da0ff60 Total correctness examples by Walter Guttmann
nipkow
parents:
diff changeset
    94
ea189da0ff60 Total correctness examples by Walter Guttmann
nipkow
parents:
diff changeset
    95
text \<open>
ea189da0ff60 Total correctness examples by Walter Guttmann
nipkow
parents:
diff changeset
    96
A total-correctness proof allows us to extract a function for further use.
ea189da0ff60 Total correctness examples by Walter Guttmann
nipkow
parents:
diff changeset
    97
For every input satisfying the precondition the function returns an output satisfying the postcondition.
ea189da0ff60 Total correctness examples by Walter Guttmann
nipkow
parents:
diff changeset
    98
\<close>
ea189da0ff60 Total correctness examples by Walter Guttmann
nipkow
parents:
diff changeset
    99
ea189da0ff60 Total correctness examples by Walter Guttmann
nipkow
parents:
diff changeset
   100
lemma sqrt_exists:
ea189da0ff60 Total correctness examples by Walter Guttmann
nipkow
parents:
diff changeset
   101
  "0 \<le> (x::int) \<Longrightarrow> \<exists>r' . r'*r' \<le> x \<and> x < (r'+1)*(r'+1)"
ea189da0ff60 Total correctness examples by Walter Guttmann
nipkow
parents:
diff changeset
   102
  using tc_extract_function sqrt_tc by blast
ea189da0ff60 Total correctness examples by Walter Guttmann
nipkow
parents:
diff changeset
   103
ea189da0ff60 Total correctness examples by Walter Guttmann
nipkow
parents:
diff changeset
   104
definition "sqrt (x::int) \<equiv> (SOME r' . r'*r' \<le> x \<and> x < (r'+1)*(r'+1))"
ea189da0ff60 Total correctness examples by Walter Guttmann
nipkow
parents:
diff changeset
   105
ea189da0ff60 Total correctness examples by Walter Guttmann
nipkow
parents:
diff changeset
   106
lemma sqrt_function:
ea189da0ff60 Total correctness examples by Walter Guttmann
nipkow
parents:
diff changeset
   107
  assumes "0 \<le> (x::int)"
ea189da0ff60 Total correctness examples by Walter Guttmann
nipkow
parents:
diff changeset
   108
    and "r' = sqrt x"
ea189da0ff60 Total correctness examples by Walter Guttmann
nipkow
parents:
diff changeset
   109
  shows "r'*r' \<le> x \<and> x < (r'+1)*(r'+1)"
ea189da0ff60 Total correctness examples by Walter Guttmann
nipkow
parents:
diff changeset
   110
proof -
ea189da0ff60 Total correctness examples by Walter Guttmann
nipkow
parents:
diff changeset
   111
  let ?P = "\<lambda>r' . r'*r' \<le> x \<and> x < (r'+1)*(r'+1)"
ea189da0ff60 Total correctness examples by Walter Guttmann
nipkow
parents:
diff changeset
   112
  have "?P (SOME z . ?P z)"
ea189da0ff60 Total correctness examples by Walter Guttmann
nipkow
parents:
diff changeset
   113
    by (metis (mono_tags, lifting) assms(1) sqrt_exists some_eq_imp)
ea189da0ff60 Total correctness examples by Walter Guttmann
nipkow
parents:
diff changeset
   114
  thus ?thesis
ea189da0ff60 Total correctness examples by Walter Guttmann
nipkow
parents:
diff changeset
   115
    using assms(2) sqrt_def by auto
ea189da0ff60 Total correctness examples by Walter Guttmann
nipkow
parents:
diff changeset
   116
qed
ea189da0ff60 Total correctness examples by Walter Guttmann
nipkow
parents:
diff changeset
   117
ea189da0ff60 Total correctness examples by Walter Guttmann
nipkow
parents:
diff changeset
   118
end