src/HOL/IMP/Hoare_Examples.thy
author nipkow
Wed, 22 May 2013 08:46:39 +0200
changeset 52108 06db08182c4b
parent 52046 bc01725d7918
child 52149 32b1dbda331c
permissions -rw-r--r--
simplified example and proof
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
43158
686fa0a0696e imported rest of new IMP
kleing
parents:
diff changeset
     1
(* Author: Tobias Nipkow *)
686fa0a0696e imported rest of new IMP
kleing
parents:
diff changeset
     2
44177
b4b5cbca2519 IMP/Util distinguishes between sets and functions again; imported only where used.
kleing
parents: 43158
diff changeset
     3
theory Hoare_Examples imports Hoare begin
43158
686fa0a0696e imported rest of new IMP
kleing
parents:
diff changeset
     4
52108
06db08182c4b simplified example and proof
nipkow
parents: 52046
diff changeset
     5
text{* Summing up the first @{text x} natural numbers in variable @{text y}. *}
43158
686fa0a0696e imported rest of new IMP
kleing
parents:
diff changeset
     6
52108
06db08182c4b simplified example and proof
nipkow
parents: 52046
diff changeset
     7
abbreviation "wsum ==
06db08182c4b simplified example and proof
nipkow
parents: 52046
diff changeset
     8
  WHILE Less (N 0) (V ''x'')
06db08182c4b simplified example and proof
nipkow
parents: 52046
diff changeset
     9
  DO (''y'' ::= Plus (V ''y'') (V ''x'');; ''x'' ::= Plus (V ''x'') (N -1))"
43158
686fa0a0696e imported rest of new IMP
kleing
parents:
diff changeset
    10
686fa0a0696e imported rest of new IMP
kleing
parents:
diff changeset
    11
text{* For this example we make use of some predefined functions.  Function
686fa0a0696e imported rest of new IMP
kleing
parents:
diff changeset
    12
@{const Setsum}, also written @{text"\<Sum>"}, sums up the elements of a set. The
686fa0a0696e imported rest of new IMP
kleing
parents:
diff changeset
    13
set of numbers from @{text m} to @{text n} is written @{term "{m .. n}"}. *}
686fa0a0696e imported rest of new IMP
kleing
parents:
diff changeset
    14
686fa0a0696e imported rest of new IMP
kleing
parents:
diff changeset
    15
subsubsection{* Proof by Operational Semantics *}
686fa0a0696e imported rest of new IMP
kleing
parents:
diff changeset
    16
686fa0a0696e imported rest of new IMP
kleing
parents:
diff changeset
    17
text{* The behaviour of the loop is proved by induction: *}
686fa0a0696e imported rest of new IMP
kleing
parents:
diff changeset
    18
52108
06db08182c4b simplified example and proof
nipkow
parents: 52046
diff changeset
    19
lemma set_ivl_lemma:
06db08182c4b simplified example and proof
nipkow
parents: 52046
diff changeset
    20
  "i \<le> j \<Longrightarrow> {i..j} = insert j {i..j - (1::int)}"
06db08182c4b simplified example and proof
nipkow
parents: 52046
diff changeset
    21
apply(simp add:atLeastAtMost_def atLeast_def atMost_def del: simp_from_to)
06db08182c4b simplified example and proof
nipkow
parents: 52046
diff changeset
    22
apply(fastforce)
06db08182c4b simplified example and proof
nipkow
parents: 52046
diff changeset
    23
done
06db08182c4b simplified example and proof
nipkow
parents: 52046
diff changeset
    24
43158
686fa0a0696e imported rest of new IMP
kleing
parents:
diff changeset
    25
lemma while_sum:
52108
06db08182c4b simplified example and proof
nipkow
parents: 52046
diff changeset
    26
  "(wsum, s) \<Rightarrow> t \<Longrightarrow> t ''y'' = s ''y'' + \<Sum> {1 .. s ''x''}"
06db08182c4b simplified example and proof
nipkow
parents: 52046
diff changeset
    27
apply(induction wsum s t rule: big_step_induct)
06db08182c4b simplified example and proof
nipkow
parents: 52046
diff changeset
    28
apply(auto simp add: set_ivl_lemma)
43158
686fa0a0696e imported rest of new IMP
kleing
parents:
diff changeset
    29
done
686fa0a0696e imported rest of new IMP
kleing
parents:
diff changeset
    30
686fa0a0696e imported rest of new IMP
kleing
parents:
diff changeset
    31
text{* We were lucky that the proof was practically automatic, except for the
686fa0a0696e imported rest of new IMP
kleing
parents:
diff changeset
    32
induction. In general, such proofs will not be so easy. The automation is
686fa0a0696e imported rest of new IMP
kleing
parents:
diff changeset
    33
partly due to the right inversion rules that we set up as automatic
686fa0a0696e imported rest of new IMP
kleing
parents:
diff changeset
    34
elimination rules that decompose big-step premises.
686fa0a0696e imported rest of new IMP
kleing
parents:
diff changeset
    35
686fa0a0696e imported rest of new IMP
kleing
parents:
diff changeset
    36
Now we prefix the loop with the necessary initialization: *}
686fa0a0696e imported rest of new IMP
kleing
parents:
diff changeset
    37
686fa0a0696e imported rest of new IMP
kleing
parents:
diff changeset
    38
lemma sum_via_bigstep:
52108
06db08182c4b simplified example and proof
nipkow
parents: 52046
diff changeset
    39
assumes "(''y'' ::= N 0;; wsum, s) \<Rightarrow> t"
06db08182c4b simplified example and proof
nipkow
parents: 52046
diff changeset
    40
shows "t ''y'' = \<Sum> {1 .. s ''x''}"
43158
686fa0a0696e imported rest of new IMP
kleing
parents:
diff changeset
    41
proof -
52108
06db08182c4b simplified example and proof
nipkow
parents: 52046
diff changeset
    42
  from assms have "(wsum,s(''y'':=0)) \<Rightarrow> t" by auto
43158
686fa0a0696e imported rest of new IMP
kleing
parents:
diff changeset
    43
  from while_sum[OF this] show ?thesis by simp
686fa0a0696e imported rest of new IMP
kleing
parents:
diff changeset
    44
qed
686fa0a0696e imported rest of new IMP
kleing
parents:
diff changeset
    45
686fa0a0696e imported rest of new IMP
kleing
parents:
diff changeset
    46
686fa0a0696e imported rest of new IMP
kleing
parents:
diff changeset
    47
subsubsection{* Proof by Hoare Logic *}
686fa0a0696e imported rest of new IMP
kleing
parents:
diff changeset
    48
686fa0a0696e imported rest of new IMP
kleing
parents:
diff changeset
    49
text{* Note that we deal with sequences of commands from right to left,
686fa0a0696e imported rest of new IMP
kleing
parents:
diff changeset
    50
pulling back the postcondition towards the precondition. *}
686fa0a0696e imported rest of new IMP
kleing
parents:
diff changeset
    51
52108
06db08182c4b simplified example and proof
nipkow
parents: 52046
diff changeset
    52
lemma "\<turnstile> {\<lambda>s. s ''x'' = n} ''y'' ::= N 0;; wsum {\<lambda>s. s ''y'' = \<Sum> {1 .. n}}"
47818
151d137f1095 renamed Semi to Seq
nipkow
parents: 45015
diff changeset
    53
apply(rule hoare.Seq)
43158
686fa0a0696e imported rest of new IMP
kleing
parents:
diff changeset
    54
prefer 2
52108
06db08182c4b simplified example and proof
nipkow
parents: 52046
diff changeset
    55
apply(rule While' [where P = "\<lambda>s. (s ''y'' = \<Sum> {1..n} - \<Sum> {1 .. s ''x''})"])
47818
151d137f1095 renamed Semi to Seq
nipkow
parents: 45015
diff changeset
    56
apply(rule Seq)
43158
686fa0a0696e imported rest of new IMP
kleing
parents:
diff changeset
    57
prefer 2
686fa0a0696e imported rest of new IMP
kleing
parents:
diff changeset
    58
apply(rule Assign)
686fa0a0696e imported rest of new IMP
kleing
parents:
diff changeset
    59
apply(rule Assign')
52108
06db08182c4b simplified example and proof
nipkow
parents: 52046
diff changeset
    60
apply simp
06db08182c4b simplified example and proof
nipkow
parents: 52046
diff changeset
    61
apply(simp add: algebra_simps set_ivl_lemma minus_numeral_simps(1)[symmetric]
06db08182c4b simplified example and proof
nipkow
parents: 52046
diff changeset
    62
  del: minus_numeral_simps)
06db08182c4b simplified example and proof
nipkow
parents: 52046
diff changeset
    63
apply(simp)
43158
686fa0a0696e imported rest of new IMP
kleing
parents:
diff changeset
    64
apply(rule Assign')
686fa0a0696e imported rest of new IMP
kleing
parents:
diff changeset
    65
apply simp
686fa0a0696e imported rest of new IMP
kleing
parents:
diff changeset
    66
done
686fa0a0696e imported rest of new IMP
kleing
parents:
diff changeset
    67
686fa0a0696e imported rest of new IMP
kleing
parents:
diff changeset
    68
text{* The proof is intentionally an apply skript because it merely composes
686fa0a0696e imported rest of new IMP
kleing
parents:
diff changeset
    69
the rules of Hoare logic. Of course, in a few places side conditions have to
686fa0a0696e imported rest of new IMP
kleing
parents:
diff changeset
    70
be proved. But since those proofs are 1-liners, a structured proof is
686fa0a0696e imported rest of new IMP
kleing
parents:
diff changeset
    71
overkill. In fact, we shall learn later that the application of the Hoare
686fa0a0696e imported rest of new IMP
kleing
parents:
diff changeset
    72
rules can be automated completely and all that is left for the user is to
686fa0a0696e imported rest of new IMP
kleing
parents:
diff changeset
    73
provide the loop invariants and prove the side-conditions. *}
686fa0a0696e imported rest of new IMP
kleing
parents:
diff changeset
    74
686fa0a0696e imported rest of new IMP
kleing
parents:
diff changeset
    75
end