author paulson <>
Mon May 23 15:33:24 2016 +0100 (2016-05-23)
changeset 63114 27afe7af7379
parent 58889 5b7a9633cfa8
child 67406 23307fd33906
permissions -rw-r--r--
Lots of new material for multivariate analysis
     1 section "Constant Folding"
     3 theory Sem_Equiv
     4 imports Big_Step
     5 begin
     7 subsection "Semantic Equivalence up to a Condition"
     9 type_synonym assn = "state \<Rightarrow> bool"
    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')"
    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)"
    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)
    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)
    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
    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
    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
    42 lemma equiv_up_to_refl [simp, intro!]:
    43   "P \<Turnstile> c \<sim> c"
    44   by (auto simp: equiv_up_to_def)
    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)
    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)
    55 lemma bequiv_up_to_refl [simp, intro!]:
    56   "P \<Turnstile> b <\<sim>> b"
    57   by (auto simp: bequiv_up_to_def)
    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)
    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)
    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)
    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
    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)+
   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)
   119   from c b have c': "P \<Turnstile> c' \<sim> c" by (simp add: equiv_up_to_sym)
   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'])
   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
   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)
   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)
   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)
   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)
   147 lemma while_never: "(c, s) \<Rightarrow> u \<Longrightarrow> c \<noteq> WHILE (Bc True) DO c'"
   148  by (induct rule: big_step_induct) auto
   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)
   156 end