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