src/HOL/IMP/Sem_Equiv.thy
author wenzelm
Sun Nov 02 18:21:45 2014 +0100 (2014-11-02)
changeset 58889 5b7a9633cfa8
parent 55598 da35747597bd
child 67406 23307fd33906
permissions -rw-r--r--
modernized header uniformly as section;
     1 section "Constant Folding"
     2 
     3 theory Sem_Equiv
     4 imports Big_Step
     5 begin
     6 
     7 subsection "Semantic Equivalence up to a Condition"
     8 
     9 type_synonym assn = "state \<Rightarrow> bool"
    10 
    11 definition
    12   equiv_up_to :: "assn \<Rightarrow> com \<Rightarrow> com \<Rightarrow> bool" ("_ \<Turnstile> _ \<sim> _" [50,0,10] 50)
    13 where
    14   "(P \<Turnstile> c \<sim> c') = (\<forall>s s'. P s \<longrightarrow> (c,s) \<Rightarrow> s' \<longleftrightarrow> (c',s) \<Rightarrow> s')"
    15 
    16 definition
    17   bequiv_up_to :: "assn \<Rightarrow> bexp \<Rightarrow> bexp \<Rightarrow> bool" ("_ \<Turnstile> _ <\<sim>> _" [50,0,10] 50)
    18 where
    19   "(P \<Turnstile> b <\<sim>> b') = (\<forall>s. P s \<longrightarrow> bval b s = bval b' s)"
    20 
    21 lemma equiv_up_to_True:
    22   "((\<lambda>_. True) \<Turnstile> c \<sim> c') = (c \<sim> c')"
    23   by (simp add: equiv_def equiv_up_to_def)
    24 
    25 lemma equiv_up_to_weaken:
    26   "P \<Turnstile> c \<sim> c' \<Longrightarrow> (\<And>s. P' s \<Longrightarrow> P s) \<Longrightarrow> P' \<Turnstile> c \<sim> c'"
    27   by (simp add: equiv_up_to_def)
    28 
    29 lemma equiv_up_toI:
    30   "(\<And>s s'. P s \<Longrightarrow> (c, s) \<Rightarrow> s' = (c', s) \<Rightarrow> s') \<Longrightarrow> P \<Turnstile> c \<sim> c'"
    31   by (unfold equiv_up_to_def) blast
    32 
    33 lemma equiv_up_toD1:
    34   "P \<Turnstile> c \<sim> c' \<Longrightarrow> (c, s) \<Rightarrow> s' \<Longrightarrow> P s \<Longrightarrow> (c', s) \<Rightarrow> s'"
    35   by (unfold equiv_up_to_def) blast
    36 
    37 lemma equiv_up_toD2:
    38   "P \<Turnstile> c \<sim> c' \<Longrightarrow> (c', s) \<Rightarrow> s' \<Longrightarrow> P s \<Longrightarrow> (c, s) \<Rightarrow> s'"
    39   by (unfold equiv_up_to_def) blast
    40 
    41 
    42 lemma equiv_up_to_refl [simp, intro!]:
    43   "P \<Turnstile> c \<sim> c"
    44   by (auto simp: equiv_up_to_def)
    45 
    46 lemma equiv_up_to_sym:
    47   "(P \<Turnstile> c \<sim> c') = (P \<Turnstile> c' \<sim> c)"
    48   by (auto simp: equiv_up_to_def)
    49 
    50 lemma equiv_up_to_trans:
    51   "P \<Turnstile> c \<sim> c' \<Longrightarrow> P \<Turnstile> c' \<sim> c'' \<Longrightarrow> P \<Turnstile> c \<sim> c''"
    52   by (auto simp: equiv_up_to_def)
    53 
    54 
    55 lemma bequiv_up_to_refl [simp, intro!]:
    56   "P \<Turnstile> b <\<sim>> b"
    57   by (auto simp: bequiv_up_to_def)
    58 
    59 lemma bequiv_up_to_sym:
    60   "(P \<Turnstile> b <\<sim>> b') = (P \<Turnstile> b' <\<sim>> b)"
    61   by (auto simp: bequiv_up_to_def)
    62 
    63 lemma bequiv_up_to_trans:
    64   "P \<Turnstile> b <\<sim>> b' \<Longrightarrow> P \<Turnstile> b' <\<sim>> b'' \<Longrightarrow> P \<Turnstile> b <\<sim>> b''"
    65   by (auto simp: bequiv_up_to_def)
    66 
    67 lemma bequiv_up_to_subst:
    68   "P \<Turnstile> b <\<sim>> b' \<Longrightarrow> P s \<Longrightarrow> bval b s = bval b' s"
    69   by (simp add: bequiv_up_to_def)
    70 
    71 
    72 lemma equiv_up_to_seq:
    73   "P \<Turnstile> c \<sim> c' \<Longrightarrow> Q \<Turnstile> d \<sim> d' \<Longrightarrow>
    74   (\<And>s s'. (c,s) \<Rightarrow> s' \<Longrightarrow> P s \<Longrightarrow> Q s') \<Longrightarrow>
    75   P \<Turnstile> (c;; d) \<sim> (c';; d')"
    76   by (clarsimp simp: equiv_up_to_def) blast
    77 
    78 lemma equiv_up_to_while_lemma_weak:
    79   shows "(d,s) \<Rightarrow> s' \<Longrightarrow>
    80          P \<Turnstile> b <\<sim>> b' \<Longrightarrow>
    81          P \<Turnstile> c \<sim> c' \<Longrightarrow>
    82          (\<And>s s'. (c, s) \<Rightarrow> s' \<Longrightarrow> P s \<Longrightarrow> bval b s \<Longrightarrow> P s') \<Longrightarrow>
    83          P s \<Longrightarrow>
    84          d = WHILE b DO c \<Longrightarrow>
    85          (WHILE b' DO c', s) \<Rightarrow> s'"
    86 proof (induction rule: big_step_induct)
    87   case (WhileTrue b s1 c s2 s3)
    88   hence IH: "P s2 \<Longrightarrow> (WHILE b' DO c', s2) \<Rightarrow> s3" by auto
    89   from WhileTrue.prems
    90   have "P \<Turnstile> b <\<sim>> b'" by simp
    91   with `bval b s1` `P s1`
    92   have "bval b' s1" by (simp add: bequiv_up_to_def)
    93   moreover
    94   from WhileTrue.prems
    95   have "P \<Turnstile> c \<sim> c'" by simp
    96   with `bval b s1` `P s1` `(c, s1) \<Rightarrow> s2`
    97   have "(c', s1) \<Rightarrow> s2" by (simp add: equiv_up_to_def)
    98   moreover
    99   from WhileTrue.prems
   100   have "\<And>s s'. (c,s) \<Rightarrow> s' \<Longrightarrow> P s \<Longrightarrow> bval b s \<Longrightarrow> P s'" by simp
   101   with `P s1` `bval b s1` `(c, s1) \<Rightarrow> s2`
   102   have "P s2" by simp
   103   hence "(WHILE b' DO c', s2) \<Rightarrow> s3" by (rule IH)
   104   ultimately
   105   show ?case by blast
   106 next
   107   case WhileFalse
   108   thus ?case by (auto simp: bequiv_up_to_def)
   109 qed (fastforce simp: equiv_up_to_def bequiv_up_to_def)+
   110 
   111 lemma equiv_up_to_while_weak:
   112   assumes b: "P \<Turnstile> b <\<sim>> b'"
   113   assumes c: "P \<Turnstile> c \<sim> c'"
   114   assumes I: "\<And>s s'. (c, s) \<Rightarrow> s' \<Longrightarrow> P s \<Longrightarrow> bval b s \<Longrightarrow> P s'"
   115   shows "P \<Turnstile> WHILE b DO c \<sim> WHILE b' DO c'"
   116 proof -
   117   from b have b': "P \<Turnstile> b' <\<sim>> b" by (simp add: bequiv_up_to_sym)
   118 
   119   from c b have c': "P \<Turnstile> c' \<sim> c" by (simp add: equiv_up_to_sym)
   120 
   121   from I
   122   have I': "\<And>s s'. (c', s) \<Rightarrow> s' \<Longrightarrow> P s \<Longrightarrow> bval b' s \<Longrightarrow> P s'"
   123     by (auto dest!: equiv_up_toD1 [OF c'] simp: bequiv_up_to_subst [OF b'])
   124 
   125   note equiv_up_to_while_lemma_weak [OF _ b c]
   126        equiv_up_to_while_lemma_weak [OF _ b' c']
   127   thus ?thesis using I I' by (auto intro!: equiv_up_toI)
   128 qed
   129 
   130 lemma equiv_up_to_if_weak:
   131   "P \<Turnstile> b <\<sim>> b' \<Longrightarrow> P \<Turnstile> c \<sim> c' \<Longrightarrow> P \<Turnstile> d \<sim> d' \<Longrightarrow>
   132    P \<Turnstile> IF b THEN c ELSE d \<sim> IF b' THEN c' ELSE d'"
   133   by (auto simp: bequiv_up_to_def equiv_up_to_def)
   134 
   135 lemma equiv_up_to_if_True [intro!]:
   136   "(\<And>s. P s \<Longrightarrow> bval b s) \<Longrightarrow> P \<Turnstile> IF b THEN c1 ELSE c2 \<sim> c1"
   137   by (auto simp: equiv_up_to_def)
   138 
   139 lemma equiv_up_to_if_False [intro!]:
   140   "(\<And>s. P s \<Longrightarrow> \<not> bval b s) \<Longrightarrow> P \<Turnstile> IF b THEN c1 ELSE c2 \<sim> c2"
   141   by (auto simp: equiv_up_to_def)
   142 
   143 lemma equiv_up_to_while_False [intro!]:
   144   "(\<And>s. P s \<Longrightarrow> \<not> bval b s) \<Longrightarrow> P \<Turnstile> WHILE b DO c \<sim> SKIP"
   145   by (auto simp: equiv_up_to_def)
   146 
   147 lemma while_never: "(c, s) \<Rightarrow> u \<Longrightarrow> c \<noteq> WHILE (Bc True) DO c'"
   148  by (induct rule: big_step_induct) auto
   149 
   150 lemma equiv_up_to_while_True [intro!,simp]:
   151   "P \<Turnstile> WHILE Bc True DO c \<sim> WHILE Bc True DO SKIP"
   152   unfolding equiv_up_to_def
   153   by (blast dest: while_never)
   154 
   155 
   156 end