src/HOL/Hoare/Hoare_Logic_Abort.thy
changeset 74503 403ce50e6a2a
parent 73655 26a1d66b9077
equal deleted inserted replaced
74500:40f03761f77f 74503:403ce50e6a2a
    17 datatype 'a com =
    17 datatype 'a com =
    18   Basic "'a \<Rightarrow> 'a"
    18   Basic "'a \<Rightarrow> 'a"
    19 | Abort
    19 | Abort
    20 | Seq "'a com" "'a com"
    20 | Seq "'a com" "'a com"
    21 | Cond "'a bexp" "'a com" "'a com"
    21 | Cond "'a bexp" "'a com" "'a com"
    22 | While "'a bexp" "'a assn" "'a var" "'a com"
    22 | While "'a bexp" "'a com"
    23 
    23 
    24 abbreviation annskip ("SKIP") where "SKIP == Basic id"
    24 abbreviation annskip ("SKIP") where "SKIP == Basic id"
    25 
    25 
    26 type_synonym 'a sem = "'a option => 'a option => bool"
    26 type_synonym 'a sem = "'a option => 'a option => bool"
    27 
    27 
    32 | "Sem Abort s None"
    32 | "Sem Abort s None"
    33 | "Sem c1 s s'' \<Longrightarrow> Sem c2 s'' s' \<Longrightarrow> Sem (Seq c1 c2) s s'"
    33 | "Sem c1 s s'' \<Longrightarrow> Sem c2 s'' s' \<Longrightarrow> Sem (Seq c1 c2) s s'"
    34 | "Sem (Cond b c1 c2) None None"
    34 | "Sem (Cond b c1 c2) None None"
    35 | "s \<in> b \<Longrightarrow> Sem c1 (Some s) s' \<Longrightarrow> Sem (Cond b c1 c2) (Some s) s'"
    35 | "s \<in> b \<Longrightarrow> Sem c1 (Some s) s' \<Longrightarrow> Sem (Cond b c1 c2) (Some s) s'"
    36 | "s \<notin> b \<Longrightarrow> Sem c2 (Some s) s' \<Longrightarrow> Sem (Cond b c1 c2) (Some s) s'"
    36 | "s \<notin> b \<Longrightarrow> Sem c2 (Some s) s' \<Longrightarrow> Sem (Cond b c1 c2) (Some s) s'"
    37 | "Sem (While b x y c) None None"
    37 | "Sem (While b c) None None"
    38 | "s \<notin> b \<Longrightarrow> Sem (While b x y c) (Some s) (Some s)"
    38 | "s \<notin> b \<Longrightarrow> Sem (While b c) (Some s) (Some s)"
    39 | "s \<in> b \<Longrightarrow> Sem c (Some s) s'' \<Longrightarrow> Sem (While b x y c) s'' s' \<Longrightarrow>
    39 | "s \<in> b \<Longrightarrow> Sem c (Some s) s'' \<Longrightarrow> Sem (While b c) s'' s' \<Longrightarrow>
    40    Sem (While b x y c) (Some s) s'"
    40    Sem (While b c) (Some s) s'"
    41 
    41 
    42 inductive_cases [elim!]:
    42 inductive_cases [elim!]:
    43   "Sem (Basic f) s s'" "Sem (Seq c1 c2) s s'"
    43   "Sem (Basic f) s s'" "Sem (Seq c1 c2) s s'"
    44   "Sem (Cond b c1 c2) s s'"
    44   "Sem (Cond b c1 c2) s s'"
    45 
    45 
    52     by (induct rule: Sem.induct) (subst Sem.simps, blast)+
    52     by (induct rule: Sem.induct) (subst Sem.simps, blast)+
    53   thus ?thesis
    53   thus ?thesis
    54     using assms by simp
    54     using assms by simp
    55 qed
    55 qed
    56 
    56 
    57 definition Valid :: "'a bexp \<Rightarrow> 'a com \<Rightarrow> 'a bexp \<Rightarrow> bool"
    57 definition Valid :: "'a bexp \<Rightarrow> 'a com \<Rightarrow> 'a anno \<Rightarrow> 'a bexp \<Rightarrow> bool"
    58   where "Valid p c q \<equiv> \<forall>s s'. Sem c s s' \<longrightarrow> s \<in> Some ` p \<longrightarrow> s' \<in> Some ` q"
    58   where "Valid p c a q \<equiv> \<forall>s s'. Sem c s s' \<longrightarrow> s \<in> Some ` p \<longrightarrow> s' \<in> Some ` q"
    59 
    59 
    60 definition ValidTC :: "'a bexp \<Rightarrow> 'a com \<Rightarrow> 'a bexp \<Rightarrow> bool"
    60 definition ValidTC :: "'a bexp \<Rightarrow> 'a com \<Rightarrow> 'a anno \<Rightarrow> 'a bexp \<Rightarrow> bool"
    61   where "ValidTC p c q \<equiv> \<forall>s . s \<in> p \<longrightarrow> (\<exists>t . Sem c (Some s) (Some t) \<and> t \<in> q)"
    61   where "ValidTC p c a q \<equiv> \<forall>s . s \<in> p \<longrightarrow> (\<exists>t . Sem c (Some s) (Some t) \<and> t \<in> q)"
    62 
    62 
    63 lemma tc_implies_pc:
    63 lemma tc_implies_pc:
    64   "ValidTC p c q \<Longrightarrow> Valid p c q"
    64   "ValidTC p c a q \<Longrightarrow> Valid p c a q"
    65   by (smt (verit) Sem_deterministic ValidTC_def Valid_def image_iff)
    65   by (smt (verit) Sem_deterministic ValidTC_def Valid_def image_iff)
    66 
    66 
    67 lemma tc_extract_function:
    67 lemma tc_extract_function:
    68   "ValidTC p c q \<Longrightarrow> \<exists>f . \<forall>s . s \<in> p \<longrightarrow> f s \<in> q"
    68   "ValidTC p c a q \<Longrightarrow> \<exists>f . \<forall>s . s \<in> p \<longrightarrow> f s \<in> q"
    69   by (meson ValidTC_def)
    69   by (meson ValidTC_def)
    70 
    70 
    71 text \<open>The proof rules for partial correctness\<close>
    71 text \<open>The proof rules for partial correctness\<close>
    72 
    72 
    73 lemma SkipRule: "p \<subseteq> q \<Longrightarrow> Valid p (Basic id) q"
    73 lemma SkipRule: "p \<subseteq> q \<Longrightarrow> Valid p (Basic id) a q"
    74 by (auto simp:Valid_def)
    74 by (auto simp:Valid_def)
    75 
    75 
    76 lemma BasicRule: "p \<subseteq> {s. f s \<in> q} \<Longrightarrow> Valid p (Basic f) q"
    76 lemma BasicRule: "p \<subseteq> {s. f s \<in> q} \<Longrightarrow> Valid p (Basic f) a q"
    77 by (auto simp:Valid_def)
    77 by (auto simp:Valid_def)
    78 
    78 
    79 lemma SeqRule: "Valid P c1 Q \<Longrightarrow> Valid Q c2 R \<Longrightarrow> Valid P (Seq c1 c2) R"
    79 lemma SeqRule: "Valid P c1 a1 Q \<Longrightarrow> Valid Q c2 a2 R \<Longrightarrow> Valid P (Seq c1 c2) (Aseq a1 a2) R"
    80 by (auto simp:Valid_def)
    80 by (auto simp:Valid_def)
    81 
    81 
    82 lemma CondRule:
    82 lemma CondRule:
    83  "p \<subseteq> {s. (s \<in> b \<longrightarrow> s \<in> w) \<and> (s \<notin> b \<longrightarrow> s \<in> w')}
    83  "p \<subseteq> {s. (s \<in> b \<longrightarrow> s \<in> w) \<and> (s \<notin> b \<longrightarrow> s \<in> w')}
    84   \<Longrightarrow> Valid w c1 q \<Longrightarrow> Valid w' c2 q \<Longrightarrow> Valid p (Cond b c1 c2) q"
    84   \<Longrightarrow> Valid w c1 a1 q \<Longrightarrow> Valid w' c2 a2 q \<Longrightarrow> Valid p (Cond b c1 c2) (Acond a1 a2) q"
    85 by (fastforce simp:Valid_def image_def)
    85 by (fastforce simp:Valid_def image_def)
    86 
    86 
    87 lemma While_aux:
    87 lemma While_aux:
    88   assumes "Sem (While b i v c) s s'"
    88   assumes "Sem (While b c) s s'"
    89   shows "\<forall>s s'. Sem c s s' \<longrightarrow> s \<in> Some ` (I \<inter> b) \<longrightarrow> s' \<in> Some ` I \<Longrightarrow>
    89   shows "\<forall>s s'. Sem c s s' \<longrightarrow> s \<in> Some ` (I \<inter> b) \<longrightarrow> s' \<in> Some ` I \<Longrightarrow>
    90     s \<in> Some ` I \<Longrightarrow> s' \<in> Some ` (I \<inter> -b)"
    90     s \<in> Some ` I \<Longrightarrow> s' \<in> Some ` (I \<inter> -b)"
    91   using assms
    91   using assms
    92   by (induct "While b i v c" s s') auto
    92   by (induct "While b c" s s') auto
    93 
    93 
    94 lemma WhileRule:
    94 lemma WhileRule:
    95  "p \<subseteq> i \<Longrightarrow> Valid (i \<inter> b) c i \<Longrightarrow> i \<inter> (-b) \<subseteq> q \<Longrightarrow> Valid p (While b i v c) q"
    95  "p \<subseteq> i \<Longrightarrow> Valid (i \<inter> b) c (A 0) i \<Longrightarrow> i \<inter> (-b) \<subseteq> q \<Longrightarrow> Valid p (While b c) (Awhile i v A) q"
    96 apply (clarsimp simp:Valid_def)
    96 apply (clarsimp simp:Valid_def)
    97 apply(drule While_aux)
    97 apply(drule While_aux)
    98   apply assumption
    98   apply assumption
    99  apply blast
    99  apply blast
   100 apply blast
   100 apply blast
   101 done
   101 done
   102 
   102 
   103 lemma AbortRule: "p \<subseteq> {s. False} \<Longrightarrow> Valid p Abort q"
   103 lemma AbortRule: "p \<subseteq> {s. False} \<Longrightarrow> Valid p Abort a q"
   104 by(auto simp:Valid_def)
   104 by(auto simp:Valid_def)
   105 
   105 
   106 text \<open>The proof rules for total correctness\<close>
   106 text \<open>The proof rules for total correctness\<close>
   107 
   107 
   108 lemma SkipRuleTC:
   108 lemma SkipRuleTC:
   109   assumes "p \<subseteq> q"
   109   assumes "p \<subseteq> q"
   110     shows "ValidTC p (Basic id) q"
   110     shows "ValidTC p (Basic id) a q"
   111   by (metis Sem.intros(2) ValidTC_def assms id_def subsetD)
   111   by (metis Sem.intros(2) ValidTC_def assms id_def subsetD)
   112 
   112 
   113 lemma BasicRuleTC:
   113 lemma BasicRuleTC:
   114   assumes "p \<subseteq> {s. f s \<in> q}"
   114   assumes "p \<subseteq> {s. f s \<in> q}"
   115     shows "ValidTC p (Basic f) q"
   115     shows "ValidTC p (Basic f) a q"
   116   by (metis Ball_Collect Sem.intros(2) ValidTC_def assms)
   116   by (metis Ball_Collect Sem.intros(2) ValidTC_def assms)
   117 
   117 
   118 lemma SeqRuleTC:
   118 lemma SeqRuleTC:
   119   assumes "ValidTC p c1 q"
   119   assumes "ValidTC p c1 a1 q"
   120       and "ValidTC q c2 r"
   120       and "ValidTC q c2 a2 r"
   121     shows "ValidTC p (Seq c1 c2) r"
   121     shows "ValidTC p (Seq c1 c2) (Aseq a1 a2) r"
   122   by (meson assms Sem.intros(4) ValidTC_def)
   122   by (meson assms Sem.intros(4) ValidTC_def)
   123 
   123 
   124 lemma CondRuleTC:
   124 lemma CondRuleTC:
   125  assumes "p \<subseteq> {s. (s \<in> b \<longrightarrow> s \<in> w) \<and> (s \<notin> b \<longrightarrow> s \<in> w')}"
   125  assumes "p \<subseteq> {s. (s \<in> b \<longrightarrow> s \<in> w) \<and> (s \<notin> b \<longrightarrow> s \<in> w')}"
   126      and "ValidTC w c1 q"
   126      and "ValidTC w c1 a1 q"
   127      and "ValidTC w' c2 q"
   127      and "ValidTC w' c2 a2 q"
   128    shows "ValidTC p (Cond b c1 c2) q"
   128    shows "ValidTC p (Cond b c1 c2) (Acons a1 a2)  q"
   129 proof (unfold ValidTC_def, rule allI)
   129 proof (unfold ValidTC_def, rule allI)
   130   fix s
   130   fix s
   131   show "s \<in> p \<longrightarrow> (\<exists>t . Sem (Cond b c1 c2) (Some s) (Some t) \<and> t \<in> q)"
   131   show "s \<in> p \<longrightarrow> (\<exists>t . Sem (Cond b c1 c2) (Some s) (Some t) \<and> t \<in> q)"
   132     apply (cases "s \<in> b")
   132     apply (cases "s \<in> b")
   133     apply (metis (mono_tags, lifting) Ball_Collect Sem.intros(6) ValidTC_def assms(1,2))
   133     apply (metis (mono_tags, lifting) Ball_Collect Sem.intros(6) ValidTC_def assms(1,2))
   134     by (metis (mono_tags, lifting) Ball_Collect Sem.intros(7) ValidTC_def assms(1,3))
   134     by (metis (mono_tags, lifting) Ball_Collect Sem.intros(7) ValidTC_def assms(1,3))
   135 qed
   135 qed
   136 
   136 
   137 lemma WhileRuleTC:
   137 lemma WhileRuleTC:
   138   assumes "p \<subseteq> i"
   138   assumes "p \<subseteq> i"
   139       and "\<And>n::nat . ValidTC (i \<inter> b \<inter> {s . v s = n}) c (i \<inter> {s . v s < n})"
   139       and "\<And>n::nat . ValidTC (i \<inter> b \<inter> {s . v s = n}) c (A n) (i \<inter> {s . v s < n})"
   140       and "i \<inter> uminus b \<subseteq> q"
   140       and "i \<inter> uminus b \<subseteq> q"
   141     shows "ValidTC p (While b i v c) q"
   141     shows "ValidTC p (While b c) (Awhile i v A) q"
   142 proof -
   142 proof -
   143   {
   143   {
   144     fix s n
   144     fix s n
   145     have "s \<in> i \<and> v s = n \<longrightarrow> (\<exists>t . Sem (While b i v c) (Some s) (Some t) \<and> t \<in> q)"
   145     have "s \<in> i \<and> v s = n \<longrightarrow> (\<exists>t . Sem (While b c) (Some s) (Some t) \<and> t \<in> q)"
   146     proof (induction "n" arbitrary: s rule: less_induct)
   146     proof (induction "n" arbitrary: s rule: less_induct)
   147       fix n :: nat
   147       fix n :: nat
   148       fix s :: 'a
   148       fix s :: 'a
   149       assume 1: "\<And>(m::nat) s::'a . m < n \<Longrightarrow> s \<in> i \<and> v s = m \<longrightarrow> (\<exists>t . Sem (While b i v c) (Some s) (Some t) \<and> t \<in> q)"
   149       assume 1: "\<And>(m::nat) s::'a . m < n \<Longrightarrow> s \<in> i \<and> v s = m \<longrightarrow> (\<exists>t . Sem (While b c) (Some s) (Some t) \<and> t \<in> q)"
   150       show "s \<in> i \<and> v s = n \<longrightarrow> (\<exists>t . Sem (While b i v c) (Some s) (Some t) \<and> t \<in> q)"
   150       show "s \<in> i \<and> v s = n \<longrightarrow> (\<exists>t . Sem (While b c) (Some s) (Some t) \<and> t \<in> q)"
   151       proof (rule impI, cases "s \<in> b")
   151       proof (rule impI, cases "s \<in> b")
   152         assume 2: "s \<in> b" and "s \<in> i \<and> v s = n"
   152         assume 2: "s \<in> b" and "s \<in> i \<and> v s = n"
   153         hence "s \<in> i \<inter> b \<inter> {s . v s = n}"
   153         hence "s \<in> i \<inter> b \<inter> {s . v s = n}"
   154           using assms(1) by auto
   154           using assms(1) by auto
   155         hence "\<exists>t . Sem c (Some s) (Some t) \<and> t \<in> i \<inter> {s . v s < n}"
   155         hence "\<exists>t . Sem c (Some s) (Some t) \<and> t \<in> i \<inter> {s . v s < n}"
   156           by (metis assms(2) ValidTC_def)
   156           by (metis assms(2) ValidTC_def)
   157         from this obtain t where 3: "Sem c (Some s) (Some t) \<and> t \<in> i \<inter> {s . v s < n}"
   157         from this obtain t where 3: "Sem c (Some s) (Some t) \<and> t \<in> i \<inter> {s . v s < n}"
   158           by auto
   158           by auto
   159         hence "\<exists>u . Sem (While b i v c) (Some t) (Some u) \<and> u \<in> q"
   159         hence "\<exists>u . Sem (While b c) (Some t) (Some u) \<and> u \<in> q"
   160           using 1 by auto
   160           using 1 by auto
   161         thus "\<exists>t . Sem (While b i v c) (Some s) (Some t) \<and> t \<in> q"
   161         thus "\<exists>t . Sem (While b c) (Some s) (Some t) \<and> t \<in> q"
   162           using 2 3 Sem.intros(10) by force
   162           using 2 3 Sem.intros(10) by force
   163       next
   163       next
   164         assume "s \<notin> b" and "s \<in> i \<and> v s = n"
   164         assume "s \<notin> b" and "s \<in> i \<and> v s = n"
   165         thus "\<exists>t . Sem (While b i v c) (Some s) (Some t) \<and> t \<in> q"
   165         thus "\<exists>t . Sem (While b c) (Some s) (Some t) \<and> t \<in> q"
   166           using Sem.intros(9) assms(3) by fastforce
   166           using Sem.intros(9) assms(3) by fastforce
   167       qed
   167       qed
   168     qed
   168     qed
   169   }
   169   }
   170   thus ?thesis
   170   thus ?thesis