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