src/HOL/NanoJava/Equivalence.thy
author blanchet
Tue Sep 09 20:51:36 2014 +0200 (2014-09-09)
changeset 58262 85b13d75b2e4
parent 51717 9e7d1c139569
child 58889 5b7a9633cfa8
permissions -rw-r--r--
rename_tac'd scrips
     1 (*  Title:      HOL/NanoJava/Equivalence.thy
     2     Author:     David von Oheimb
     3     Copyright   2001 Technische Universitaet Muenchen
     4 *)
     5 
     6 header "Equivalence of Operational and Axiomatic Semantics"
     7 
     8 theory Equivalence imports OpSem AxSem begin
     9 
    10 subsection "Validity"
    11 
    12 definition valid :: "[assn,stmt, assn] => bool" ("|= {(1_)}/ (_)/ {(1_)}" [3,90,3] 60) where
    13  "|=  {P} c {Q} \<equiv> \<forall>s   t. P s --> (\<exists>n. s -c  -n\<rightarrow> t) --> Q   t"
    14 
    15 definition evalid   :: "[assn,expr,vassn] => bool" ("|=e {(1_)}/ (_)/ {(1_)}" [3,90,3] 60) where
    16  "|=e {P} e {Q} \<equiv> \<forall>s v t. P s --> (\<exists>n. s -e\<succ>v-n\<rightarrow> t) --> Q v t"
    17 
    18 definition nvalid   :: "[nat, triple    ] => bool" ("|=_: _"  [61,61] 60) where
    19  "|=n:  t \<equiv> let (P,c,Q) = t in \<forall>s   t. s -c  -n\<rightarrow> t --> P s --> Q   t"
    20 
    21 definition envalid   :: "[nat,etriple    ] => bool" ("|=_:e _" [61,61] 60) where
    22  "|=n:e t \<equiv> let (P,e,Q) = t in \<forall>s v t. s -e\<succ>v-n\<rightarrow> t --> P s --> Q v t"
    23 
    24 definition nvalids :: "[nat,       triple set] => bool" ("||=_: _" [61,61] 60) where
    25  "||=n: T \<equiv> \<forall>t\<in>T. |=n: t"
    26 
    27 definition cnvalids :: "[triple set,triple set] => bool" ("_ ||=/ _"  [61,61] 60) where
    28  "A ||=  C \<equiv> \<forall>n. ||=n: A --> ||=n: C"
    29 
    30 definition cenvalid  :: "[triple set,etriple   ] => bool" ("_ ||=e/ _" [61,61] 60) where
    31  "A ||=e t \<equiv> \<forall>n. ||=n: A --> |=n:e t"
    32 
    33 notation (xsymbols)
    34   valid  ("\<Turnstile> {(1_)}/ (_)/ {(1_)}" [3,90,3] 60) and
    35   evalid  ("\<Turnstile>\<^sub>e {(1_)}/ (_)/ {(1_)}" [3,90,3] 60) and
    36   nvalid  ("\<Turnstile>_: _" [61,61] 60) and
    37   envalid  ("\<Turnstile>_:\<^sub>e _" [61,61] 60) and
    38   nvalids  ("|\<Turnstile>_: _" [61,61] 60) and
    39   cnvalids  ("_ |\<Turnstile>/ _" [61,61] 60) and
    40   cenvalid  ("_ |\<Turnstile>\<^sub>e/ _"[61,61] 60)
    41 
    42 
    43 lemma nvalid_def2: "\<Turnstile>n: (P,c,Q) \<equiv> \<forall>s t. s -c-n\<rightarrow> t \<longrightarrow> P s \<longrightarrow> Q t"
    44 by (simp add: nvalid_def Let_def)
    45 
    46 lemma valid_def2: "\<Turnstile> {P} c {Q} = (\<forall>n. \<Turnstile>n: (P,c,Q))"
    47 apply (simp add: valid_def nvalid_def2)
    48 apply blast
    49 done
    50 
    51 lemma envalid_def2: "\<Turnstile>n:\<^sub>e (P,e,Q) \<equiv> \<forall>s v t. s -e\<succ>v-n\<rightarrow> t \<longrightarrow> P s \<longrightarrow> Q v t"
    52 by (simp add: envalid_def Let_def)
    53 
    54 lemma evalid_def2: "\<Turnstile>\<^sub>e {P} e {Q} = (\<forall>n. \<Turnstile>n:\<^sub>e (P,e,Q))"
    55 apply (simp add: evalid_def envalid_def2)
    56 apply blast
    57 done
    58 
    59 lemma cenvalid_def2: 
    60   "A|\<Turnstile>\<^sub>e (P,e,Q) = (\<forall>n. |\<Turnstile>n: A \<longrightarrow> (\<forall>s v t. s -e\<succ>v-n\<rightarrow> t \<longrightarrow> P s \<longrightarrow> Q v t))"
    61 by(simp add: cenvalid_def envalid_def2) 
    62 
    63 
    64 subsection "Soundness"
    65 
    66 declare exec_elim_cases [elim!] eval_elim_cases [elim!]
    67 
    68 lemma Impl_nvalid_0: "\<Turnstile>0: (P,Impl M,Q)"
    69 by (clarsimp simp add: nvalid_def2)
    70 
    71 lemma Impl_nvalid_Suc: "\<Turnstile>n: (P,body M,Q) \<Longrightarrow> \<Turnstile>Suc n: (P,Impl M,Q)"
    72 by (clarsimp simp add: nvalid_def2)
    73 
    74 lemma nvalid_SucD: "\<And>t. \<Turnstile>Suc n:t \<Longrightarrow> \<Turnstile>n:t"
    75 by (force simp add: split_paired_all nvalid_def2 intro: exec_mono)
    76 
    77 lemma nvalids_SucD: "Ball A (nvalid (Suc n)) \<Longrightarrow>  Ball A (nvalid n)"
    78 by (fast intro: nvalid_SucD)
    79 
    80 lemma Loop_sound_lemma [rule_format (no_asm)]: 
    81 "\<forall>s t. s -c-n\<rightarrow> t \<longrightarrow> P s \<and> s<x> \<noteq> Null \<longrightarrow> P t \<Longrightarrow> 
    82   (s -c0-n0\<rightarrow> t \<longrightarrow> P s \<longrightarrow> c0 = While (x) c \<longrightarrow> n0 = n \<longrightarrow> P t \<and> t<x> = Null)"
    83 apply (rule_tac ?P2.1="%s e v n t. True" in exec_eval.induct [THEN conjunct1])
    84 apply clarsimp+
    85 done
    86 
    87 lemma Impl_sound_lemma: 
    88 "\<lbrakk>\<forall>z n. Ball (A \<union> B) (nvalid n) \<longrightarrow> Ball (f z ` Ms) (nvalid n); 
    89   Cm\<in>Ms; Ball A (nvalid na); Ball B (nvalid na)\<rbrakk> \<Longrightarrow> nvalid na (f z Cm)"
    90 by blast
    91 
    92 lemma all_conjunct2: "\<forall>l. P' l \<and> P l \<Longrightarrow> \<forall>l. P l"
    93 by fast
    94 
    95 lemma all3_conjunct2: 
    96   "\<forall>a p l. (P' a p l \<and> P a p l) \<Longrightarrow> \<forall>a p l. P a p l"
    97 by fast
    98 
    99 lemma cnvalid1_eq: 
   100   "A |\<Turnstile> {(P,c,Q)} \<equiv> \<forall>n. |\<Turnstile>n: A \<longrightarrow> (\<forall>s t. s -c-n\<rightarrow> t \<longrightarrow> P s \<longrightarrow> Q t)"
   101 by(simp add: cnvalids_def nvalids_def nvalid_def2)
   102 
   103 lemma hoare_sound_main:"\<And>t. (A |\<turnstile> C \<longrightarrow> A |\<Turnstile> C) \<and> (A |\<turnstile>\<^sub>e t \<longrightarrow> A |\<Turnstile>\<^sub>e t)"
   104 apply (tactic "split_all_tac @{context} 1", rename_tac P e Q)
   105 apply (rule hoare_ehoare.induct)
   106 (*18*)
   107 apply (tactic {* ALLGOALS (REPEAT o dresolve_tac [@{thm all_conjunct2}, @{thm all3_conjunct2}]) *})
   108 apply (tactic {* ALLGOALS (REPEAT o thin_tac @{context} "hoare ?x ?y") *})
   109 apply (tactic {* ALLGOALS (REPEAT o thin_tac @{context} "ehoare ?x ?y") *})
   110 apply (simp_all only: cnvalid1_eq cenvalid_def2)
   111                  apply fast
   112                 apply fast
   113                apply fast
   114               apply (clarify,tactic "smp_tac 1 1",erule(2) Loop_sound_lemma,(rule HOL.refl)+)
   115              apply fast
   116             apply fast
   117            apply fast
   118           apply fast
   119          apply fast
   120         apply fast
   121        apply (clarsimp del: Meth_elim_cases) (* Call *)
   122       apply (force del: Impl_elim_cases)
   123      defer
   124      prefer 4 apply blast (*  Conseq *)
   125     prefer 4 apply blast (* eConseq *)
   126    apply (simp_all (no_asm_use) only: cnvalids_def nvalids_def)
   127    apply blast
   128   apply blast
   129  apply blast
   130 apply (rule allI)
   131 apply (rule_tac x=Z in spec)
   132 apply (induct_tac "n")
   133  apply  (clarify intro!: Impl_nvalid_0)
   134 apply (clarify  intro!: Impl_nvalid_Suc)
   135 apply (drule nvalids_SucD)
   136 apply (simp only: HOL.all_simps)
   137 apply (erule (1) impE)
   138 apply (drule (2) Impl_sound_lemma)
   139  apply  blast
   140 apply assumption
   141 done
   142 
   143 theorem hoare_sound: "{} \<turnstile> {P} c {Q} \<Longrightarrow> \<Turnstile> {P} c {Q}"
   144 apply (simp only: valid_def2)
   145 apply (drule hoare_sound_main [THEN conjunct1, rule_format])
   146 apply (unfold cnvalids_def nvalids_def)
   147 apply fast
   148 done
   149 
   150 theorem ehoare_sound: "{} \<turnstile>\<^sub>e {P} e {Q} \<Longrightarrow> \<Turnstile>\<^sub>e {P} e {Q}"
   151 apply (simp only: evalid_def2)
   152 apply (drule hoare_sound_main [THEN conjunct2, rule_format])
   153 apply (unfold cenvalid_def nvalids_def)
   154 apply fast
   155 done
   156 
   157 
   158 subsection "(Relative) Completeness"
   159 
   160 definition MGT :: "stmt => state => triple" where
   161          "MGT  c Z \<equiv> (\<lambda>s. Z = s, c, \<lambda>  t. \<exists>n. Z -c-  n\<rightarrow> t)"
   162 
   163 definition MGTe   :: "expr => state => etriple" where
   164          "MGTe e Z \<equiv> (\<lambda>s. Z = s, e, \<lambda>v t. \<exists>n. Z -e\<succ>v-n\<rightarrow> t)"
   165 
   166 notation (xsymbols)
   167   MGTe  ("MGT\<^sub>e")
   168 notation (HTML output)
   169   MGTe  ("MGT\<^sub>e")
   170 
   171 lemma MGF_implies_complete:
   172  "\<forall>Z. {} |\<turnstile> { MGT c Z} \<Longrightarrow> \<Turnstile>  {P} c {Q} \<Longrightarrow> {} \<turnstile>  {P} c {Q}"
   173 apply (simp only: valid_def2)
   174 apply (unfold MGT_def)
   175 apply (erule hoare_ehoare.Conseq)
   176 apply (clarsimp simp add: nvalid_def2)
   177 done
   178 
   179 lemma eMGF_implies_complete:
   180  "\<forall>Z. {} |\<turnstile>\<^sub>e MGT\<^sub>e e Z \<Longrightarrow> \<Turnstile>\<^sub>e {P} e {Q} \<Longrightarrow> {} \<turnstile>\<^sub>e {P} e {Q}"
   181 apply (simp only: evalid_def2)
   182 apply (unfold MGTe_def)
   183 apply (erule hoare_ehoare.eConseq)
   184 apply (clarsimp simp add: envalid_def2)
   185 done
   186 
   187 declare exec_eval.intros[intro!]
   188 
   189 lemma MGF_Loop: "\<forall>Z. A \<turnstile> {op = Z} c {\<lambda>t. \<exists>n. Z -c-n\<rightarrow> t} \<Longrightarrow> 
   190   A \<turnstile> {op = Z} While (x) c {\<lambda>t. \<exists>n. Z -While (x) c-n\<rightarrow> t}"
   191 apply (rule_tac P' = "\<lambda>Z s. (Z,s) \<in> ({(s,t). \<exists>n. s<x> \<noteq> Null \<and> s -c-n\<rightarrow> t})^*"
   192        in hoare_ehoare.Conseq)
   193 apply  (rule allI)
   194 apply  (rule hoare_ehoare.Loop)
   195 apply  (erule hoare_ehoare.Conseq)
   196 apply  clarsimp
   197 apply  (blast intro:rtrancl_into_rtrancl)
   198 apply (erule thin_rl)
   199 apply clarsimp
   200 apply (erule_tac x = Z in allE)
   201 apply clarsimp
   202 apply (erule converse_rtrancl_induct)
   203 apply  blast
   204 apply clarsimp
   205 apply (drule (1) exec_exec_max)
   206 apply (blast del: exec_elim_cases)
   207 done
   208 
   209 lemma MGF_lemma: "\<forall>M Z. A |\<turnstile> {MGT (Impl M) Z} \<Longrightarrow> 
   210  (\<forall>Z. A |\<turnstile> {MGT c Z}) \<and> (\<forall>Z. A |\<turnstile>\<^sub>e MGT\<^sub>e e Z)"
   211 apply (simp add: MGT_def MGTe_def)
   212 apply (rule stmt_expr.induct)
   213 apply (rule_tac [!] allI)
   214 
   215 apply (rule Conseq1 [OF hoare_ehoare.Skip])
   216 apply blast
   217 
   218 apply (rule hoare_ehoare.Comp)
   219 apply  (erule spec)
   220 apply (erule hoare_ehoare.Conseq)
   221 apply clarsimp
   222 apply (drule (1) exec_exec_max)
   223 apply blast
   224 
   225 apply (erule thin_rl)
   226 apply (rule hoare_ehoare.Cond)
   227 apply  (erule spec)
   228 apply (rule allI)
   229 apply (simp)
   230 apply (rule conjI)
   231 apply  (rule impI, erule hoare_ehoare.Conseq, clarsimp, drule (1) eval_exec_max,
   232         erule thin_rl, erule thin_rl, force)+
   233 
   234 apply (erule MGF_Loop)
   235 
   236 apply (erule hoare_ehoare.eConseq [THEN hoare_ehoare.LAss])
   237 apply fast
   238 
   239 apply (erule thin_rl)
   240 apply (rename_tac expr1 u v Z, rule_tac Q = "\<lambda>a s. \<exists>n. Z -expr1\<succ>Addr a-n\<rightarrow> s" in hoare_ehoare.FAss)
   241 apply  (drule spec)
   242 apply  (erule eConseq2)
   243 apply  fast
   244 apply (rule allI)
   245 apply (erule hoare_ehoare.eConseq)
   246 apply clarsimp
   247 apply (drule (1) eval_eval_max)
   248 apply blast
   249 
   250 apply (simp only: split_paired_all)
   251 apply (rule hoare_ehoare.Meth)
   252 apply (rule allI)
   253 apply (drule spec, drule spec, erule hoare_ehoare.Conseq)
   254 apply blast
   255 
   256 apply (simp add: split_paired_all)
   257 
   258 apply (rule eConseq1 [OF hoare_ehoare.NewC])
   259 apply blast
   260 
   261 apply (erule hoare_ehoare.eConseq [THEN hoare_ehoare.Cast])
   262 apply fast
   263 
   264 apply (rule eConseq1 [OF hoare_ehoare.LAcc])
   265 apply blast
   266 
   267 apply (erule hoare_ehoare.eConseq [THEN hoare_ehoare.FAcc])
   268 apply fast
   269 
   270 apply (rename_tac expr1 u expr2 Z)
   271 apply (rule_tac R = "\<lambda>a v s. \<exists>n1 n2 t. Z -expr1\<succ>a-n1\<rightarrow> t \<and> t -expr2\<succ>v-n2\<rightarrow> s" in
   272                 hoare_ehoare.Call)
   273 apply   (erule spec)
   274 apply  (rule allI)
   275 apply  (erule hoare_ehoare.eConseq)
   276 apply  clarsimp
   277 apply  blast
   278 apply (rule allI)+
   279 apply (rule hoare_ehoare.Meth)
   280 apply (rule allI)
   281 apply (drule spec, drule spec, erule hoare_ehoare.Conseq)
   282 apply (erule thin_rl, erule thin_rl)
   283 apply (clarsimp del: Impl_elim_cases)
   284 apply (drule (2) eval_eval_exec_max)
   285 apply (force del: Impl_elim_cases)
   286 done
   287 
   288 lemma MGF_Impl: "{} |\<turnstile> {MGT (Impl M) Z}"
   289 apply (unfold MGT_def)
   290 apply (rule Impl1')
   291 apply  (rule_tac [2] UNIV_I)
   292 apply clarsimp
   293 apply (rule hoare_ehoare.ConjI)
   294 apply clarsimp
   295 apply (rule ssubst [OF Impl_body_eq])
   296 apply (fold MGT_def)
   297 apply (rule MGF_lemma [THEN conjunct1, rule_format])
   298 apply (rule hoare_ehoare.Asm)
   299 apply force
   300 done
   301 
   302 theorem hoare_relative_complete: "\<Turnstile> {P} c {Q} \<Longrightarrow> {} \<turnstile> {P} c {Q}"
   303 apply (rule MGF_implies_complete)
   304 apply  (erule_tac [2] asm_rl)
   305 apply (rule allI)
   306 apply (rule MGF_lemma [THEN conjunct1, rule_format])
   307 apply (rule MGF_Impl)
   308 done
   309 
   310 theorem ehoare_relative_complete: "\<Turnstile>\<^sub>e {P} e {Q} \<Longrightarrow> {} \<turnstile>\<^sub>e {P} e {Q}"
   311 apply (rule eMGF_implies_complete)
   312 apply  (erule_tac [2] asm_rl)
   313 apply (rule allI)
   314 apply (rule MGF_lemma [THEN conjunct2, rule_format])
   315 apply (rule MGF_Impl)
   316 done
   317 
   318 lemma cFalse: "A \<turnstile> {\<lambda>s. False} c {Q}"
   319 apply (rule cThin)
   320 apply (rule hoare_relative_complete)
   321 apply (auto simp add: valid_def)
   322 done
   323 
   324 lemma eFalse: "A \<turnstile>\<^sub>e {\<lambda>s. False} e {Q}"
   325 apply (rule eThin)
   326 apply (rule ehoare_relative_complete)
   327 apply (auto simp add: evalid_def)
   328 done
   329 
   330 end