
kleing@44070: header "Semantic Equivalence up to a Condition"
kleing@44070: 
kleing@44070: theory Sem_Equiv
kleing@44070: imports Hoare_Sound_Complete
kleing@44070: begin
kleing@44070: 
kleing@44070: definition
kleing@44070:   equiv_up_to :: "assn \<Rightarrow> com \<Rightarrow> com \<Rightarrow> bool" ("_ \<Turnstile> _ \<sim> _" [60,0,10] 60)
kleing@44070: where
kleing@44070:   "P \<Turnstile> c \<sim> c' \<equiv> \<forall>s s'. P s \<longrightarrow> (c,s) \<Rightarrow> s' \<longleftrightarrow> (c',s) \<Rightarrow> s'"
kleing@44070: 
kleing@44070: definition 
kleing@44070:   bequiv_up_to :: "assn \<Rightarrow> bexp \<Rightarrow> bexp \<Rightarrow> bool" ("_ \<Turnstile> _ <\<sim>> _" [60,0,10] 60)
kleing@44070: where 
kleing@44070:   "P \<Turnstile> b <\<sim>> b' \<equiv> \<forall>s. P s \<longrightarrow> bval b s = bval b' s"
kleing@44070: 
kleing@44070: lemma equiv_up_to_True:
kleing@44070:   "((\<lambda>_. True) \<Turnstile> c \<sim> c') = (c \<sim> c')"
kleing@44070:   by (simp add: equiv_def equiv_up_to_def)
kleing@44070: 
kleing@44070: lemma equiv_up_to_weaken:
kleing@44070:   "P \<Turnstile> c \<sim> c' \<Longrightarrow> (\<And>s. P' s \<Longrightarrow> P s) \<Longrightarrow> P' \<Turnstile> c \<sim> c'"
kleing@44070:   by (simp add: equiv_up_to_def)
kleing@44070: 
kleing@44070: lemma equiv_up_toI:
kleing@44070:   "(\<And>s s'. P s \<Longrightarrow> (c, s) \<Rightarrow> s' = (c', s) \<Rightarrow> s') \<Longrightarrow> P \<Turnstile> c \<sim> c'"
kleing@44070:   by (unfold equiv_up_to_def) blast
kleing@44070: 
kleing@44070: lemma equiv_up_toD1:
kleing@44070:   "P \<Turnstile> c \<sim> c' \<Longrightarrow> P s \<Longrightarrow> (c, s) \<Rightarrow> s' \<Longrightarrow> (c', s) \<Rightarrow> s'"
kleing@44070:   by (unfold equiv_up_to_def) blast
kleing@44070: 
kleing@44070: lemma equiv_up_toD2:
kleing@44070:   "P \<Turnstile> c \<sim> c' \<Longrightarrow> P s \<Longrightarrow> (c', s) \<Rightarrow> s' \<Longrightarrow> (c, s) \<Rightarrow> s'"
kleing@44070:   by (unfold equiv_up_to_def) blast
kleing@44070: 
kleing@44070: 
kleing@44070: lemma equiv_up_to_refl [simp, intro!]:
kleing@44070:   "P \<Turnstile> c \<sim> c"
kleing@44070:   by (auto simp: equiv_up_to_def)
kleing@44070: 
kleing@44070: lemma equiv_up_to_sym:
kleing@44070:   "(P \<Turnstile> c \<sim> c') = (P \<Turnstile> c' \<sim> c)"
kleing@44070:   by (auto simp: equiv_up_to_def)
kleing@44070: 
kleing@45218: lemma equiv_up_to_trans:
kleing@44070:   "P \<Turnstile> c \<sim> c' \<Longrightarrow> P \<Turnstile> c' \<sim> c'' \<Longrightarrow> P \<Turnstile> c \<sim> c''"
kleing@44070:   by (auto simp: equiv_up_to_def)
kleing@44070: 
kleing@44070: 
kleing@44070: lemma bequiv_up_to_refl [simp, intro!]:
kleing@44070:   "P \<Turnstile> b <\<sim>> b"
kleing@44070:   by (auto simp: bequiv_up_to_def)
kleing@44070: 
kleing@44070: lemma bequiv_up_to_sym:
kleing@44070:   "(P \<Turnstile> b <\<sim>> b') = (P \<Turnstile> b' <\<sim>> b)"
kleing@44070:   by (auto simp: bequiv_up_to_def)
kleing@44070: 
kleing@45218: lemma bequiv_up_to_trans:
kleing@44070:   "P \<Turnstile> b <\<sim>> b' \<Longrightarrow> P \<Turnstile> b' <\<sim>> b'' \<Longrightarrow> P \<Turnstile> b <\<sim>> b''"
kleing@44070:   by (auto simp: bequiv_up_to_def)
kleing@44070: 
kleing@44070: 
kleing@44070: lemma equiv_up_to_hoare:
kleing@44070:   "P' \<Turnstile> c \<sim> c' \<Longrightarrow> (\<And>s. P s \<Longrightarrow> P' s) \<Longrightarrow> (\<Turnstile> {P} c {Q}) = (\<Turnstile> {P} c' {Q})"
kleing@44070:   unfolding hoare_valid_def equiv_up_to_def by blast
kleing@44070: 
kleing@44070: lemma equiv_up_to_hoare_eq:
kleing@44070:   "P \<Turnstile> c \<sim> c' \<Longrightarrow> (\<Turnstile> {P} c {Q}) = (\<Turnstile> {P} c' {Q})"
kleing@44070:   by (rule equiv_up_to_hoare)
kleing@44070: 
kleing@44070: 
kleing@44070: lemma equiv_up_to_semi:
kleing@44070:   "P \<Turnstile> c \<sim> c' \<Longrightarrow> Q \<Turnstile> d \<sim> d' \<Longrightarrow> \<Turnstile> {P} c {Q} \<Longrightarrow>
kleing@44070:   P \<Turnstile> (c; d) \<sim> (c'; d')"
kleing@44070:   by (clarsimp simp: equiv_up_to_def hoare_valid_def) blast
kleing@44070: 
kleing@44070: lemma equiv_up_to_while_lemma:
kleing@44070:   shows "(d,s) \<Rightarrow> s' \<Longrightarrow> 
kleing@44070:          P \<Turnstile> b <\<sim>> b' \<Longrightarrow>
kleing@44070:          (\<lambda>s. P s \<and> bval b s) \<Turnstile> c \<sim> c' \<Longrightarrow> 
kleing@44070:          \<Turnstile> {\<lambda>s. P s \<and> bval b s} c {P} \<Longrightarrow> 
kleing@44070:          P s \<Longrightarrow> 
kleing@44070:          d = WHILE b DO c \<Longrightarrow> 
kleing@44070:          (WHILE b' DO c', s) \<Rightarrow> s'"  
nipkow@45015: proof (induction rule: big_step_induct)
kleing@44070:   case (WhileTrue b s1 c s2 s3)
nipkow@45015:   note IH = WhileTrue.IH(2) [OF WhileTrue.prems(1-3) _ WhileTrue.prems(5)]
kleing@44070:   from WhileTrue.prems
kleing@44070:   have "P \<Turnstile> b <\<sim>> b'" by simp
kleing@44070:   with `bval b s1` `P s1`
kleing@44070:   have "bval b' s1" by (simp add: bequiv_up_to_def)
kleing@44070:   moreover
kleing@44070:   from WhileTrue.prems
kleing@44070:   have "(\<lambda>s. P s \<and> bval b s) \<Turnstile> c \<sim> c'" by simp
kleing@44070:   with `bval b s1` `P s1` `(c, s1) \<Rightarrow> s2`
kleing@44070:   have "(c', s1) \<Rightarrow> s2" by (simp add: equiv_up_to_def)
kleing@44070:   moreover
kleing@44070:   from WhileTrue.prems
kleing@44070:   have "\<Turnstile> {\<lambda>s. P s \<and> bval b s} c {P}" by simp
kleing@44070:   with `P s1` `bval b s1` `(c, s1) \<Rightarrow> s2`
kleing@44070:   have "P s2" by (simp add: hoare_valid_def)
kleing@44070:   hence "(WHILE b' DO c', s2) \<Rightarrow> s3" by (rule IH)
kleing@44070:   ultimately 
kleing@44070:   show ?case by blast
kleing@44070: next
kleing@44070:   case WhileFalse
kleing@44070:   thus ?case by (auto simp: bequiv_up_to_def)
nipkow@44890: qed (fastforce simp: equiv_up_to_def bequiv_up_to_def hoare_valid_def)+
kleing@44070: 
kleing@44070: lemma bequiv_context_subst:
kleing@44070:   "P \<Turnstile> b <\<sim>> b' \<Longrightarrow> (P s \<and> bval b s) = (P s \<and> bval b' s)"
kleing@44070:   by (auto simp: bequiv_up_to_def)
kleing@44070: 
kleing@44070: lemma equiv_up_to_while:
kleing@44070:   "P \<Turnstile> b <\<sim>> b' \<Longrightarrow> (\<lambda>s. P s \<and> bval b s) \<Turnstile> c \<sim> c' \<Longrightarrow> 
kleing@44070:    \<Turnstile> {\<lambda>s. P s \<and> bval b s} c {P} \<Longrightarrow> 
kleing@44070:    P \<Turnstile> WHILE b DO c \<sim> WHILE b' DO c'"
kleing@44070:   apply (safe intro!: equiv_up_toI)
kleing@44070:    apply (auto intro: equiv_up_to_while_lemma)[1]
kleing@44070:   apply (simp add: equiv_up_to_hoare_eq bequiv_context_subst)
kleing@44070:   apply (drule equiv_up_to_sym [THEN iffD1])
kleing@44070:   apply (drule bequiv_up_to_sym [THEN iffD1])
kleing@44070:   apply (auto intro: equiv_up_to_while_lemma)[1]
kleing@44070:   done
kleing@44070: 
kleing@44070: lemma equiv_up_to_while_weak:
kleing@44070:   "P \<Turnstile> b <\<sim>> b' \<Longrightarrow> P \<Turnstile> c \<sim> c' \<Longrightarrow> \<Turnstile> {P} c {P} \<Longrightarrow> 
kleing@44070:    P \<Turnstile> WHILE b DO c \<sim> WHILE b' DO c'"
nipkow@44890:   by (fastforce elim!: equiv_up_to_while equiv_up_to_weaken 
kleing@44070:                simp: hoare_valid_def)
kleing@44070: 
kleing@44070: lemma equiv_up_to_if:
huffman@44261:   "P \<Turnstile> b <\<sim>> b' \<Longrightarrow> (\<lambda>s. P s \<and> bval b s) \<Turnstile> c \<sim> c' \<Longrightarrow> (\<lambda>s. P s \<and> \<not>bval b s) \<Turnstile> d \<sim> d' \<Longrightarrow>
kleing@44070:    P \<Turnstile> IF b THEN c ELSE d \<sim> IF b' THEN c' ELSE d'"
kleing@44070:   by (auto simp: bequiv_up_to_def equiv_up_to_def)
kleing@44070: 
kleing@44070: lemma equiv_up_to_if_weak:
kleing@44070:   "P \<Turnstile> b <\<sim>> b' \<Longrightarrow> P \<Turnstile> c \<sim> c' \<Longrightarrow> P \<Turnstile> d \<sim> d' \<Longrightarrow>
kleing@44070:    P \<Turnstile> IF b THEN c ELSE d \<sim> IF b' THEN c' ELSE d'"
nipkow@44890:   by (fastforce elim!: equiv_up_to_if equiv_up_to_weaken)
kleing@44070: 
kleing@44070: lemma equiv_up_to_if_True [intro!]:
kleing@44070:   "(\<And>s. P s \<Longrightarrow> bval b s) \<Longrightarrow> P \<Turnstile> IF b THEN c1 ELSE c2 \<sim> c1"
kleing@44070:   by (auto simp: equiv_up_to_def) 
kleing@44070: 
kleing@44070: lemma equiv_up_to_if_False [intro!]:
kleing@44070:   "(\<And>s. P s \<Longrightarrow> \<not> bval b s) \<Longrightarrow> P \<Turnstile> IF b THEN c1 ELSE c2 \<sim> c2"
kleing@44070:   by (auto simp: equiv_up_to_def)
kleing@44070: 
kleing@44070: lemma equiv_up_to_while_False [intro!]:
kleing@44070:   "(\<And>s. P s \<Longrightarrow> \<not> bval b s) \<Longrightarrow> P \<Turnstile> WHILE b DO c \<sim> SKIP"
kleing@44070:   by (auto simp: equiv_up_to_def)
kleing@44070: 
nipkow@45200: lemma while_never: "(c, s) \<Rightarrow> u \<Longrightarrow> c \<noteq> WHILE (Bc True) DO c'"
kleing@44070:  by (induct rule: big_step_induct) auto
kleing@44070:   
kleing@44070: lemma equiv_up_to_while_True [intro!,simp]:
nipkow@45200:   "P \<Turnstile> WHILE Bc True DO c \<sim> WHILE Bc True DO SKIP"
kleing@44070:   unfolding equiv_up_to_def
kleing@44070:   by (blast dest: while_never)
kleing@44070: 
kleing@44070: 
huffman@44261: end




