src/HOL/IMP/Hoare.thy
author bulwahn
Fri Oct 21 11:17:14 2011 +0200 (2011-10-21)
changeset 45231 d85a2fdc586c
parent 45212 e87feee00a4c
child 47818 151d137f1095
permissions -rw-r--r--
replacing code_inline by code_unfold, removing obsolete code_unfold, code_inline del now that the ancient code generator is removed
kleing@43158
     1
(* Author: Tobias Nipkow *)
kleing@43158
     2
kleing@43158
     3
header "Hoare Logic"
kleing@43158
     4
kleing@43158
     5
theory Hoare imports Big_Step begin
kleing@43158
     6
kleing@43158
     7
subsection "Hoare Logic for Partial Correctness"
kleing@43158
     8
kleing@43158
     9
type_synonym assn = "state \<Rightarrow> bool"
kleing@43158
    10
nipkow@45212
    11
abbreviation state_subst :: "state \<Rightarrow> aexp \<Rightarrow> vname \<Rightarrow> state"
kleing@43158
    12
  ("_[_'/_]" [1000,0,0] 999)
kleing@43158
    13
where "s[a/x] == s(x := aval a s)"
kleing@43158
    14
kleing@43158
    15
inductive
kleing@43158
    16
  hoare :: "assn \<Rightarrow> com \<Rightarrow> assn \<Rightarrow> bool" ("\<turnstile> ({(1_)}/ (_)/ {(1_)})" 50)
kleing@43158
    17
where
kleing@43158
    18
Skip: "\<turnstile> {P} SKIP {P}"  |
kleing@43158
    19
kleing@43158
    20
Assign:  "\<turnstile> {\<lambda>s. P(s[a/x])} x::=a {P}"  |
kleing@43158
    21
kleing@43158
    22
Semi: "\<lbrakk> \<turnstile> {P} c\<^isub>1 {Q};  \<turnstile> {Q} c\<^isub>2 {R} \<rbrakk>
kleing@43158
    23
       \<Longrightarrow> \<turnstile> {P} c\<^isub>1;c\<^isub>2 {R}"  |
kleing@43158
    24
kleing@43158
    25
If: "\<lbrakk> \<turnstile> {\<lambda>s. P s \<and> bval b s} c\<^isub>1 {Q};  \<turnstile> {\<lambda>s. P s \<and> \<not> bval b s} c\<^isub>2 {Q} \<rbrakk>
kleing@43158
    26
     \<Longrightarrow> \<turnstile> {P} IF b THEN c\<^isub>1 ELSE c\<^isub>2 {Q}"  |
kleing@43158
    27
kleing@43158
    28
While: "\<turnstile> {\<lambda>s. P s \<and> bval b s} c {P} \<Longrightarrow>
kleing@43158
    29
        \<turnstile> {P} WHILE b DO c {\<lambda>s. P s \<and> \<not> bval b s}"  |
kleing@43158
    30
kleing@43158
    31
conseq: "\<lbrakk> \<forall>s. P' s \<longrightarrow> P s;  \<turnstile> {P} c {Q};  \<forall>s. Q s \<longrightarrow> Q' s \<rbrakk>
kleing@43158
    32
        \<Longrightarrow> \<turnstile> {P'} c {Q'}"
kleing@43158
    33
kleing@43158
    34
lemmas [simp] = hoare.Skip hoare.Assign hoare.Semi If
kleing@43158
    35
kleing@43158
    36
lemmas [intro!] = hoare.Skip hoare.Assign hoare.Semi hoare.If
kleing@43158
    37
kleing@43158
    38
lemma strengthen_pre:
kleing@43158
    39
  "\<lbrakk> \<forall>s. P' s \<longrightarrow> P s;  \<turnstile> {P} c {Q} \<rbrakk> \<Longrightarrow> \<turnstile> {P'} c {Q}"
kleing@43158
    40
by (blast intro: conseq)
kleing@43158
    41
kleing@43158
    42
lemma weaken_post:
kleing@43158
    43
  "\<lbrakk> \<turnstile> {P} c {Q};  \<forall>s. Q s \<longrightarrow> Q' s \<rbrakk> \<Longrightarrow>  \<turnstile> {P} c {Q'}"
kleing@43158
    44
by (blast intro: conseq)
kleing@43158
    45
kleing@43158
    46
text{* The assignment and While rule are awkward to use in actual proofs
kleing@43158
    47
because their pre and postcondition are of a very special form and the actual
kleing@43158
    48
goal would have to match this form exactly. Therefore we derive two variants
kleing@43158
    49
with arbitrary pre and postconditions. *}
kleing@43158
    50
kleing@43158
    51
lemma Assign': "\<forall>s. P s \<longrightarrow> Q(s[a/x]) \<Longrightarrow> \<turnstile> {P} x ::= a {Q}"
kleing@43158
    52
by (simp add: strengthen_pre[OF _ Assign])
kleing@43158
    53
kleing@43158
    54
lemma While':
kleing@43158
    55
assumes "\<turnstile> {\<lambda>s. P s \<and> bval b s} c {P}" and "\<forall>s. P s \<and> \<not> bval b s \<longrightarrow> Q s"
kleing@43158
    56
shows "\<turnstile> {P} WHILE b DO c {Q}"
kleing@43158
    57
by(rule weaken_post[OF While[OF assms(1)] assms(2)])
kleing@43158
    58
kleing@43158
    59
end