src/HOL/IMP/VCG.thy
changeset 68776 403dd13cf6e9
parent 67406 23307fd33906
equal deleted inserted replaced
68774:9fc50a3e07f6 68776:403dd13cf6e9
     1 (* Author: Tobias Nipkow *)
     1 (* Author: Tobias Nipkow *)
       
     2 
       
     3 subsection "Verification Condition Generation"
     2 
     4 
     3 theory VCG imports Hoare begin
     5 theory VCG imports Hoare begin
     4 
     6 
     5 subsection "Verification Conditions"
     7 subsubsection "Annotated Commands"
     6 
     8 
     7 text\<open>Annotated commands: commands where loops are annotated with
     9 text\<open>Commands where loops are annotated with invariants.\<close>
     8 invariants.\<close>
       
     9 
    10 
    10 datatype acom =
    11 datatype acom =
    11   Askip                  ("SKIP") |
    12   Askip                  ("SKIP") |
    12   Aassign vname aexp     ("(_ ::= _)" [1000, 61] 61) |
    13   Aassign vname aexp     ("(_ ::= _)" [1000, 61] 61) |
    13   Aseq   acom acom       ("_;;/ _"  [60, 61] 60) |
    14   Aseq   acom acom       ("_;;/ _"  [60, 61] 60) |
    23 "strip (x ::= a) = (x ::= a)" |
    24 "strip (x ::= a) = (x ::= a)" |
    24 "strip (C\<^sub>1;; C\<^sub>2) = (strip C\<^sub>1;; strip C\<^sub>2)" |
    25 "strip (C\<^sub>1;; C\<^sub>2) = (strip C\<^sub>1;; strip C\<^sub>2)" |
    25 "strip (IF b THEN C\<^sub>1 ELSE C\<^sub>2) = (IF b THEN strip C\<^sub>1 ELSE strip C\<^sub>2)" |
    26 "strip (IF b THEN C\<^sub>1 ELSE C\<^sub>2) = (IF b THEN strip C\<^sub>1 ELSE strip C\<^sub>2)" |
    26 "strip ({_} WHILE b DO C) = (WHILE b DO strip C)"
    27 "strip ({_} WHILE b DO C) = (WHILE b DO strip C)"
    27 
    28 
    28 text\<open>Weakest precondition from annotated commands:\<close>
    29 subsubsection "Weeakest Precondistion and Verification Condition"
       
    30 
       
    31 text\<open>Weakest precondition:\<close>
    29 
    32 
    30 fun pre :: "acom \<Rightarrow> assn \<Rightarrow> assn" where
    33 fun pre :: "acom \<Rightarrow> assn \<Rightarrow> assn" where
    31 "pre SKIP Q = Q" |
    34 "pre SKIP Q = Q" |
    32 "pre (x ::= a) Q = (\<lambda>s. Q(s(x := aval a s)))" |
    35 "pre (x ::= a) Q = (\<lambda>s. Q(s(x := aval a s)))" |
    33 "pre (C\<^sub>1;; C\<^sub>2) Q = pre C\<^sub>1 (pre C\<^sub>2 Q)" |
    36 "pre (C\<^sub>1;; C\<^sub>2) Q = pre C\<^sub>1 (pre C\<^sub>2 Q)" |
    46   ((\<forall>s. (I s \<and> bval b s \<longrightarrow> pre C I s) \<and>
    49   ((\<forall>s. (I s \<and> bval b s \<longrightarrow> pre C I s) \<and>
    47         (I s \<and> \<not> bval b s \<longrightarrow> Q s)) \<and>
    50         (I s \<and> \<not> bval b s \<longrightarrow> Q s)) \<and>
    48     vc C I)"
    51     vc C I)"
    49 
    52 
    50 
    53 
    51 text \<open>Soundness:\<close>
    54 subsubsection "Soundness"
    52 
    55 
    53 lemma vc_sound: "vc C Q \<Longrightarrow> \<turnstile> {pre C Q} strip C {Q}"
    56 lemma vc_sound: "vc C Q \<Longrightarrow> \<turnstile> {pre C Q} strip C {Q}"
    54 proof(induction C arbitrary: Q)
    57 proof(induction C arbitrary: Q)
    55   case (Awhile I b C)
    58   case (Awhile I b C)
    56   show ?case
    59   show ?case
    68 corollary vc_sound':
    71 corollary vc_sound':
    69   "\<lbrakk> vc C Q; \<forall>s. P s \<longrightarrow> pre C Q s \<rbrakk> \<Longrightarrow> \<turnstile> {P} strip C {Q}"
    72   "\<lbrakk> vc C Q; \<forall>s. P s \<longrightarrow> pre C Q s \<rbrakk> \<Longrightarrow> \<turnstile> {P} strip C {Q}"
    70 by (metis strengthen_pre vc_sound)
    73 by (metis strengthen_pre vc_sound)
    71 
    74 
    72 
    75 
    73 text\<open>Completeness:\<close>
    76 subsubsection "Completeness"
    74 
    77 
    75 lemma pre_mono:
    78 lemma pre_mono:
    76   "\<forall>s. P s \<longrightarrow> P' s \<Longrightarrow> pre C P s \<Longrightarrow> pre C P' s"
    79   "\<forall>s. P s \<longrightarrow> P' s \<Longrightarrow> pre C P s \<Longrightarrow> pre C P' s"
    77 proof (induction C arbitrary: P P' s)
    80 proof (induction C arbitrary: P P' s)
    78   case Aseq thus ?case by simp metis
    81   case Aseq thus ?case by simp metis