src/HOL/IMP/Fold.thy
author wenzelm
Thu, 18 Apr 2013 17:07:01 +0200
changeset 51717 9e7d1c139569
parent 51573 acc4bd79e2e9
child 52046 bc01725d7918
permissions -rw-r--r--
simplifier uses proper Proof.context instead of historic type simpset;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
44070
cebb7abb54b1 import constant folding theory into IMP
kleing
parents:
diff changeset
     1
header "Constant Folding"
cebb7abb54b1 import constant folding theory into IMP
kleing
parents:
diff changeset
     2
cebb7abb54b1 import constant folding theory into IMP
kleing
parents:
diff changeset
     3
theory Fold imports Sem_Equiv begin
cebb7abb54b1 import constant folding theory into IMP
kleing
parents:
diff changeset
     4
44850
a6095c96a89b tuned headers
nipkow
parents: 44070
diff changeset
     5
subsection "Simple folding of arithmetic expressions"
44070
cebb7abb54b1 import constant folding theory into IMP
kleing
parents:
diff changeset
     6
45134
9b02f6665fc8 discontinued obsolete 'types' command;
wenzelm
parents: 45015
diff changeset
     7
type_synonym
45212
e87feee00a4c renamed name -> vname
nipkow
parents: 45200
diff changeset
     8
  tab = "vname \<Rightarrow> val option"
44070
cebb7abb54b1 import constant folding theory into IMP
kleing
parents:
diff changeset
     9
cebb7abb54b1 import constant folding theory into IMP
kleing
parents:
diff changeset
    10
(* maybe better as the composition of substitution and the existing simp_const(0) *)
51514
9bed72e55475 simp_const -> afold; bfold -> fold'; bsimp_const -> bfold
kleing
parents: 51506
diff changeset
    11
fun afold :: "aexp \<Rightarrow> tab \<Rightarrow> aexp" where
9bed72e55475 simp_const -> afold; bfold -> fold'; bsimp_const -> bfold
kleing
parents: 51506
diff changeset
    12
"afold (N n) _ = N n" |
9bed72e55475 simp_const -> afold; bfold -> fold'; bsimp_const -> bfold
kleing
parents: 51506
diff changeset
    13
"afold (V x) t = (case t x of None \<Rightarrow> V x | Some k \<Rightarrow> N k)" |
9bed72e55475 simp_const -> afold; bfold -> fold'; bsimp_const -> bfold
kleing
parents: 51506
diff changeset
    14
"afold (Plus e1 e2) t = (case (afold e1 t, afold e2 t) of
48909
b2dea007e55e remove Hoare dependency from Fold.thy
Gerwin Klein <gerwin.klein@nicta.com.au>
parents: 47818
diff changeset
    15
  (N n1, N n2) \<Rightarrow> N(n1+n2) | (e1',e2') \<Rightarrow> Plus e1' e2')"
44070
cebb7abb54b1 import constant folding theory into IMP
kleing
parents:
diff changeset
    16
cebb7abb54b1 import constant folding theory into IMP
kleing
parents:
diff changeset
    17
definition "approx t s \<longleftrightarrow> (ALL x k. t x = Some k \<longrightarrow> s x = k)"
cebb7abb54b1 import constant folding theory into IMP
kleing
parents:
diff changeset
    18
51514
9bed72e55475 simp_const -> afold; bfold -> fold'; bsimp_const -> bfold
kleing
parents: 51506
diff changeset
    19
theorem aval_afold[simp]:
44070
cebb7abb54b1 import constant folding theory into IMP
kleing
parents:
diff changeset
    20
assumes "approx t s"
51514
9bed72e55475 simp_const -> afold; bfold -> fold'; bsimp_const -> bfold
kleing
parents: 51506
diff changeset
    21
shows "aval (afold a t) s = aval a s"
48909
b2dea007e55e remove Hoare dependency from Fold.thy
Gerwin Klein <gerwin.klein@nicta.com.au>
parents: 47818
diff changeset
    22
  using assms
44070
cebb7abb54b1 import constant folding theory into IMP
kleing
parents:
diff changeset
    23
  by (induct a) (auto simp: approx_def split: aexp.split option.split)
cebb7abb54b1 import constant folding theory into IMP
kleing
parents:
diff changeset
    24
51514
9bed72e55475 simp_const -> afold; bfold -> fold'; bsimp_const -> bfold
kleing
parents: 51506
diff changeset
    25
theorem aval_afold_N:
44070
cebb7abb54b1 import constant folding theory into IMP
kleing
parents:
diff changeset
    26
assumes "approx t s"
51514
9bed72e55475 simp_const -> afold; bfold -> fold'; bsimp_const -> bfold
kleing
parents: 51506
diff changeset
    27
shows "afold a t = N n \<Longrightarrow> aval a s = n"
51573
acc4bd79e2e9 replace induction by hammer
kleing
parents: 51514
diff changeset
    28
  by (metis assms aval.simps(1) aval_afold)
44070
cebb7abb54b1 import constant folding theory into IMP
kleing
parents:
diff changeset
    29
cebb7abb54b1 import constant folding theory into IMP
kleing
parents:
diff changeset
    30
definition
cebb7abb54b1 import constant folding theory into IMP
kleing
parents:
diff changeset
    31
  "merge t1 t2 = (\<lambda>m. if t1 m = t2 m then t1 m else None)"
cebb7abb54b1 import constant folding theory into IMP
kleing
parents:
diff changeset
    32
45212
e87feee00a4c renamed name -> vname
nipkow
parents: 45200
diff changeset
    33
primrec lnames :: "com \<Rightarrow> vname set" where
44070
cebb7abb54b1 import constant folding theory into IMP
kleing
parents:
diff changeset
    34
"lnames SKIP = {}" |
cebb7abb54b1 import constant folding theory into IMP
kleing
parents:
diff changeset
    35
"lnames (x ::= a) = {x}" |
cebb7abb54b1 import constant folding theory into IMP
kleing
parents:
diff changeset
    36
"lnames (c1; c2) = lnames c1 \<union> lnames c2" |
cebb7abb54b1 import constant folding theory into IMP
kleing
parents:
diff changeset
    37
"lnames (IF b THEN c1 ELSE c2) = lnames c1 \<union> lnames c2" |
cebb7abb54b1 import constant folding theory into IMP
kleing
parents:
diff changeset
    38
"lnames (WHILE b DO c) = lnames c"
cebb7abb54b1 import constant folding theory into IMP
kleing
parents:
diff changeset
    39
cebb7abb54b1 import constant folding theory into IMP
kleing
parents:
diff changeset
    40
primrec "defs" :: "com \<Rightarrow> tab \<Rightarrow> tab" where
cebb7abb54b1 import constant folding theory into IMP
kleing
parents:
diff changeset
    41
"defs SKIP t = t" |
cebb7abb54b1 import constant folding theory into IMP
kleing
parents:
diff changeset
    42
"defs (x ::= a) t =
51514
9bed72e55475 simp_const -> afold; bfold -> fold'; bsimp_const -> bfold
kleing
parents: 51506
diff changeset
    43
  (case afold a t of N k \<Rightarrow> t(x \<mapsto> k) | _ \<Rightarrow> t(x:=None))" |
44070
cebb7abb54b1 import constant folding theory into IMP
kleing
parents:
diff changeset
    44
"defs (c1;c2) t = (defs c2 o defs c1) t" |
cebb7abb54b1 import constant folding theory into IMP
kleing
parents:
diff changeset
    45
"defs (IF b THEN c1 ELSE c2) t = merge (defs c1 t) (defs c2 t)" |
48909
b2dea007e55e remove Hoare dependency from Fold.thy
Gerwin Klein <gerwin.klein@nicta.com.au>
parents: 47818
diff changeset
    46
"defs (WHILE b DO c) t = t |` (-lnames c)"
44070
cebb7abb54b1 import constant folding theory into IMP
kleing
parents:
diff changeset
    47
cebb7abb54b1 import constant folding theory into IMP
kleing
parents:
diff changeset
    48
primrec fold where
cebb7abb54b1 import constant folding theory into IMP
kleing
parents:
diff changeset
    49
"fold SKIP _ = SKIP" |
51514
9bed72e55475 simp_const -> afold; bfold -> fold'; bsimp_const -> bfold
kleing
parents: 51506
diff changeset
    50
"fold (x ::= a) t = (x ::= (afold a t))" |
44070
cebb7abb54b1 import constant folding theory into IMP
kleing
parents:
diff changeset
    51
"fold (c1;c2) t = (fold c1 t; fold c2 (defs c1 t))" |
cebb7abb54b1 import constant folding theory into IMP
kleing
parents:
diff changeset
    52
"fold (IF b THEN c1 ELSE c2) t = IF b THEN fold c1 t ELSE fold c2 t" |
cebb7abb54b1 import constant folding theory into IMP
kleing
parents:
diff changeset
    53
"fold (WHILE b DO c) t = WHILE b DO fold c (t |` (-lnames c))"
cebb7abb54b1 import constant folding theory into IMP
kleing
parents:
diff changeset
    54
cebb7abb54b1 import constant folding theory into IMP
kleing
parents:
diff changeset
    55
lemma approx_merge:
cebb7abb54b1 import constant folding theory into IMP
kleing
parents:
diff changeset
    56
  "approx t1 s \<or> approx t2 s \<Longrightarrow> approx (merge t1 t2) s"
44890
22f665a2e91c new fastforce replacing fastsimp - less confusing name
nipkow
parents: 44850
diff changeset
    57
  by (fastforce simp: merge_def approx_def)
44070
cebb7abb54b1 import constant folding theory into IMP
kleing
parents:
diff changeset
    58
cebb7abb54b1 import constant folding theory into IMP
kleing
parents:
diff changeset
    59
lemma approx_map_le:
cebb7abb54b1 import constant folding theory into IMP
kleing
parents:
diff changeset
    60
  "approx t2 s \<Longrightarrow> t1 \<subseteq>\<^sub>m t2 \<Longrightarrow> approx t1 s"
cebb7abb54b1 import constant folding theory into IMP
kleing
parents:
diff changeset
    61
  by (clarsimp simp: approx_def map_le_def dom_def)
cebb7abb54b1 import constant folding theory into IMP
kleing
parents:
diff changeset
    62
cebb7abb54b1 import constant folding theory into IMP
kleing
parents:
diff changeset
    63
lemma restrict_map_le [intro!, simp]: "t |` S \<subseteq>\<^sub>m t"
cebb7abb54b1 import constant folding theory into IMP
kleing
parents:
diff changeset
    64
  by (clarsimp simp: restrict_map_def map_le_def)
cebb7abb54b1 import constant folding theory into IMP
kleing
parents:
diff changeset
    65
cebb7abb54b1 import constant folding theory into IMP
kleing
parents:
diff changeset
    66
lemma merge_restrict:
cebb7abb54b1 import constant folding theory into IMP
kleing
parents:
diff changeset
    67
  assumes "t1 |` S = t |` S"
cebb7abb54b1 import constant folding theory into IMP
kleing
parents:
diff changeset
    68
  assumes "t2 |` S = t |` S"
cebb7abb54b1 import constant folding theory into IMP
kleing
parents:
diff changeset
    69
  shows "merge t1 t2 |` S = t |` S"
cebb7abb54b1 import constant folding theory into IMP
kleing
parents:
diff changeset
    70
proof -
cebb7abb54b1 import constant folding theory into IMP
kleing
parents:
diff changeset
    71
  from assms
48909
b2dea007e55e remove Hoare dependency from Fold.thy
Gerwin Klein <gerwin.klein@nicta.com.au>
parents: 47818
diff changeset
    72
  have "\<forall>x. (t1 |` S) x = (t |` S) x"
44070
cebb7abb54b1 import constant folding theory into IMP
kleing
parents:
diff changeset
    73
   and "\<forall>x. (t2 |` S) x = (t |` S) x" by auto
cebb7abb54b1 import constant folding theory into IMP
kleing
parents:
diff changeset
    74
  thus ?thesis
48909
b2dea007e55e remove Hoare dependency from Fold.thy
Gerwin Klein <gerwin.klein@nicta.com.au>
parents: 47818
diff changeset
    75
    by (auto simp: merge_def restrict_map_def
51506
cd573f1a82b2 removed obsolete uses of ext
kleing
parents: 48909
diff changeset
    76
             split: if_splits)
44070
cebb7abb54b1 import constant folding theory into IMP
kleing
parents:
diff changeset
    77
qed
cebb7abb54b1 import constant folding theory into IMP
kleing
parents:
diff changeset
    78
cebb7abb54b1 import constant folding theory into IMP
kleing
parents:
diff changeset
    79
cebb7abb54b1 import constant folding theory into IMP
kleing
parents:
diff changeset
    80
lemma defs_restrict:
cebb7abb54b1 import constant folding theory into IMP
kleing
parents:
diff changeset
    81
  "defs c t |` (- lnames c) = t |` (- lnames c)"
45015
fdac1e9880eb Updated IMP to use new induction method
nipkow
parents: 44890
diff changeset
    82
proof (induction c arbitrary: t)
47818
151d137f1095 renamed Semi to Seq
nipkow
parents: 45212
diff changeset
    83
  case (Seq c1 c2)
48909
b2dea007e55e remove Hoare dependency from Fold.thy
Gerwin Klein <gerwin.klein@nicta.com.au>
parents: 47818
diff changeset
    84
  hence "defs c1 t |` (- lnames c1) = t |` (- lnames c1)"
44070
cebb7abb54b1 import constant folding theory into IMP
kleing
parents:
diff changeset
    85
    by simp
48909
b2dea007e55e remove Hoare dependency from Fold.thy
Gerwin Klein <gerwin.klein@nicta.com.au>
parents: 47818
diff changeset
    86
  hence "defs c1 t |` (- lnames c1) |` (-lnames c2) =
44070
cebb7abb54b1 import constant folding theory into IMP
kleing
parents:
diff changeset
    87
         t |` (- lnames c1) |` (-lnames c2)" by simp
cebb7abb54b1 import constant folding theory into IMP
kleing
parents:
diff changeset
    88
  moreover
47818
151d137f1095 renamed Semi to Seq
nipkow
parents: 45212
diff changeset
    89
  from Seq
48909
b2dea007e55e remove Hoare dependency from Fold.thy
Gerwin Klein <gerwin.klein@nicta.com.au>
parents: 47818
diff changeset
    90
  have "defs c2 (defs c1 t) |` (- lnames c2) =
44070
cebb7abb54b1 import constant folding theory into IMP
kleing
parents:
diff changeset
    91
        defs c1 t |` (- lnames c2)"
cebb7abb54b1 import constant folding theory into IMP
kleing
parents:
diff changeset
    92
    by simp
cebb7abb54b1 import constant folding theory into IMP
kleing
parents:
diff changeset
    93
  hence "defs c2 (defs c1 t) |` (- lnames c2) |` (- lnames c1) =
cebb7abb54b1 import constant folding theory into IMP
kleing
parents:
diff changeset
    94
         defs c1 t |` (- lnames c2) |` (- lnames c1)"
cebb7abb54b1 import constant folding theory into IMP
kleing
parents:
diff changeset
    95
    by simp
cebb7abb54b1 import constant folding theory into IMP
kleing
parents:
diff changeset
    96
  ultimately
cebb7abb54b1 import constant folding theory into IMP
kleing
parents:
diff changeset
    97
  show ?case by (clarsimp simp: Int_commute)
cebb7abb54b1 import constant folding theory into IMP
kleing
parents:
diff changeset
    98
next
cebb7abb54b1 import constant folding theory into IMP
kleing
parents:
diff changeset
    99
  case (If b c1 c2)
cebb7abb54b1 import constant folding theory into IMP
kleing
parents:
diff changeset
   100
  hence "defs c1 t |` (- lnames c1) = t |` (- lnames c1)" by simp
48909
b2dea007e55e remove Hoare dependency from Fold.thy
Gerwin Klein <gerwin.klein@nicta.com.au>
parents: 47818
diff changeset
   101
  hence "defs c1 t |` (- lnames c1) |` (-lnames c2) =
44070
cebb7abb54b1 import constant folding theory into IMP
kleing
parents:
diff changeset
   102
         t |` (- lnames c1) |` (-lnames c2)" by simp
cebb7abb54b1 import constant folding theory into IMP
kleing
parents:
diff changeset
   103
  moreover
cebb7abb54b1 import constant folding theory into IMP
kleing
parents:
diff changeset
   104
  from If
cebb7abb54b1 import constant folding theory into IMP
kleing
parents:
diff changeset
   105
  have "defs c2 t |` (- lnames c2) = t |` (- lnames c2)" by simp
48909
b2dea007e55e remove Hoare dependency from Fold.thy
Gerwin Klein <gerwin.klein@nicta.com.au>
parents: 47818
diff changeset
   106
  hence "defs c2 t |` (- lnames c2) |` (-lnames c1) =
44070
cebb7abb54b1 import constant folding theory into IMP
kleing
parents:
diff changeset
   107
         t |` (- lnames c2) |` (-lnames c1)" by simp
cebb7abb54b1 import constant folding theory into IMP
kleing
parents:
diff changeset
   108
  ultimately
cebb7abb54b1 import constant folding theory into IMP
kleing
parents:
diff changeset
   109
  show ?case by (auto simp: Int_commute intro: merge_restrict)
cebb7abb54b1 import constant folding theory into IMP
kleing
parents:
diff changeset
   110
qed (auto split: aexp.split)
cebb7abb54b1 import constant folding theory into IMP
kleing
parents:
diff changeset
   111
cebb7abb54b1 import constant folding theory into IMP
kleing
parents:
diff changeset
   112
cebb7abb54b1 import constant folding theory into IMP
kleing
parents:
diff changeset
   113
lemma big_step_pres_approx:
cebb7abb54b1 import constant folding theory into IMP
kleing
parents:
diff changeset
   114
  "(c,s) \<Rightarrow> s' \<Longrightarrow> approx t s \<Longrightarrow> approx (defs c t) s'"
45015
fdac1e9880eb Updated IMP to use new induction method
nipkow
parents: 44890
diff changeset
   115
proof (induction arbitrary: t rule: big_step_induct)
44070
cebb7abb54b1 import constant folding theory into IMP
kleing
parents:
diff changeset
   116
  case Skip thus ?case by simp
cebb7abb54b1 import constant folding theory into IMP
kleing
parents:
diff changeset
   117
next
cebb7abb54b1 import constant folding theory into IMP
kleing
parents:
diff changeset
   118
  case Assign
cebb7abb54b1 import constant folding theory into IMP
kleing
parents:
diff changeset
   119
  thus ?case
51514
9bed72e55475 simp_const -> afold; bfold -> fold'; bsimp_const -> bfold
kleing
parents: 51506
diff changeset
   120
    by (clarsimp simp: aval_afold_N approx_def split: aexp.split)
44070
cebb7abb54b1 import constant folding theory into IMP
kleing
parents:
diff changeset
   121
next
47818
151d137f1095 renamed Semi to Seq
nipkow
parents: 45212
diff changeset
   122
  case (Seq c1 s1 s2 c2 s3)
151d137f1095 renamed Semi to Seq
nipkow
parents: 45212
diff changeset
   123
  have "approx (defs c1 t) s2" by (rule Seq.IH(1)[OF Seq.prems])
151d137f1095 renamed Semi to Seq
nipkow
parents: 45212
diff changeset
   124
  hence "approx (defs c2 (defs c1 t)) s3" by (rule Seq.IH(2))
44070
cebb7abb54b1 import constant folding theory into IMP
kleing
parents:
diff changeset
   125
  thus ?case by simp
cebb7abb54b1 import constant folding theory into IMP
kleing
parents:
diff changeset
   126
next
cebb7abb54b1 import constant folding theory into IMP
kleing
parents:
diff changeset
   127
  case (IfTrue b s c1 s')
cebb7abb54b1 import constant folding theory into IMP
kleing
parents:
diff changeset
   128
  hence "approx (defs c1 t) s'" by simp
cebb7abb54b1 import constant folding theory into IMP
kleing
parents:
diff changeset
   129
  thus ?case by (simp add: approx_merge)
cebb7abb54b1 import constant folding theory into IMP
kleing
parents:
diff changeset
   130
next
cebb7abb54b1 import constant folding theory into IMP
kleing
parents:
diff changeset
   131
  case (IfFalse b s c2 s')
cebb7abb54b1 import constant folding theory into IMP
kleing
parents:
diff changeset
   132
  hence "approx (defs c2 t) s'" by simp
cebb7abb54b1 import constant folding theory into IMP
kleing
parents:
diff changeset
   133
  thus ?case by (simp add: approx_merge)
cebb7abb54b1 import constant folding theory into IMP
kleing
parents:
diff changeset
   134
next
cebb7abb54b1 import constant folding theory into IMP
kleing
parents:
diff changeset
   135
  case WhileFalse
cebb7abb54b1 import constant folding theory into IMP
kleing
parents:
diff changeset
   136
  thus ?case by (simp add: approx_def restrict_map_def)
cebb7abb54b1 import constant folding theory into IMP
kleing
parents:
diff changeset
   137
next
cebb7abb54b1 import constant folding theory into IMP
kleing
parents:
diff changeset
   138
  case (WhileTrue b s1 c s2 s3)
cebb7abb54b1 import constant folding theory into IMP
kleing
parents:
diff changeset
   139
  hence "approx (defs c t) s2" by simp
cebb7abb54b1 import constant folding theory into IMP
kleing
parents:
diff changeset
   140
  with WhileTrue
cebb7abb54b1 import constant folding theory into IMP
kleing
parents:
diff changeset
   141
  have "approx (defs c t |` (-lnames c)) s3" by simp
cebb7abb54b1 import constant folding theory into IMP
kleing
parents:
diff changeset
   142
  thus ?case by (simp add: defs_restrict)
cebb7abb54b1 import constant folding theory into IMP
kleing
parents:
diff changeset
   143
qed
cebb7abb54b1 import constant folding theory into IMP
kleing
parents:
diff changeset
   144
cebb7abb54b1 import constant folding theory into IMP
kleing
parents:
diff changeset
   145
cebb7abb54b1 import constant folding theory into IMP
kleing
parents:
diff changeset
   146
lemma big_step_pres_approx_restrict:
cebb7abb54b1 import constant folding theory into IMP
kleing
parents:
diff changeset
   147
  "(c,s) \<Rightarrow> s' \<Longrightarrow> approx (t |` (-lnames c)) s \<Longrightarrow> approx (t |` (-lnames c)) s'"
45015
fdac1e9880eb Updated IMP to use new induction method
nipkow
parents: 44890
diff changeset
   148
proof (induction arbitrary: t rule: big_step_induct)
44070
cebb7abb54b1 import constant folding theory into IMP
kleing
parents:
diff changeset
   149
  case Assign
cebb7abb54b1 import constant folding theory into IMP
kleing
parents:
diff changeset
   150
  thus ?case by (clarsimp simp: approx_def)
cebb7abb54b1 import constant folding theory into IMP
kleing
parents:
diff changeset
   151
next
47818
151d137f1095 renamed Semi to Seq
nipkow
parents: 45212
diff changeset
   152
  case (Seq c1 s1 s2 c2 s3)
48909
b2dea007e55e remove Hoare dependency from Fold.thy
Gerwin Klein <gerwin.klein@nicta.com.au>
parents: 47818
diff changeset
   153
  hence "approx (t |` (-lnames c2) |` (-lnames c1)) s1"
44070
cebb7abb54b1 import constant folding theory into IMP
kleing
parents:
diff changeset
   154
    by (simp add: Int_commute)
cebb7abb54b1 import constant folding theory into IMP
kleing
parents:
diff changeset
   155
  hence "approx (t |` (-lnames c2) |` (-lnames c1)) s2"
47818
151d137f1095 renamed Semi to Seq
nipkow
parents: 45212
diff changeset
   156
    by (rule Seq)
44070
cebb7abb54b1 import constant folding theory into IMP
kleing
parents:
diff changeset
   157
  hence "approx (t |` (-lnames c1) |` (-lnames c2)) s2"
cebb7abb54b1 import constant folding theory into IMP
kleing
parents:
diff changeset
   158
    by (simp add: Int_commute)
cebb7abb54b1 import constant folding theory into IMP
kleing
parents:
diff changeset
   159
  hence "approx (t |` (-lnames c1) |` (-lnames c2)) s3"
47818
151d137f1095 renamed Semi to Seq
nipkow
parents: 45212
diff changeset
   160
    by (rule Seq)
44070
cebb7abb54b1 import constant folding theory into IMP
kleing
parents:
diff changeset
   161
  thus ?case by simp
cebb7abb54b1 import constant folding theory into IMP
kleing
parents:
diff changeset
   162
next
cebb7abb54b1 import constant folding theory into IMP
kleing
parents:
diff changeset
   163
  case (IfTrue b s c1 s' c2)
48909
b2dea007e55e remove Hoare dependency from Fold.thy
Gerwin Klein <gerwin.klein@nicta.com.au>
parents: 47818
diff changeset
   164
  hence "approx (t |` (-lnames c2) |` (-lnames c1)) s"
44070
cebb7abb54b1 import constant folding theory into IMP
kleing
parents:
diff changeset
   165
    by (simp add: Int_commute)
48909
b2dea007e55e remove Hoare dependency from Fold.thy
Gerwin Klein <gerwin.klein@nicta.com.au>
parents: 47818
diff changeset
   166
  hence "approx (t |` (-lnames c2) |` (-lnames c1)) s'"
44070
cebb7abb54b1 import constant folding theory into IMP
kleing
parents:
diff changeset
   167
    by (rule IfTrue)
48909
b2dea007e55e remove Hoare dependency from Fold.thy
Gerwin Klein <gerwin.klein@nicta.com.au>
parents: 47818
diff changeset
   168
  thus ?case by (simp add: Int_commute)
44070
cebb7abb54b1 import constant folding theory into IMP
kleing
parents:
diff changeset
   169
next
cebb7abb54b1 import constant folding theory into IMP
kleing
parents:
diff changeset
   170
  case (IfFalse b s c2 s' c1)
48909
b2dea007e55e remove Hoare dependency from Fold.thy
Gerwin Klein <gerwin.klein@nicta.com.au>
parents: 47818
diff changeset
   171
  hence "approx (t |` (-lnames c1) |` (-lnames c2)) s"
44070
cebb7abb54b1 import constant folding theory into IMP
kleing
parents:
diff changeset
   172
    by simp
48909
b2dea007e55e remove Hoare dependency from Fold.thy
Gerwin Klein <gerwin.klein@nicta.com.au>
parents: 47818
diff changeset
   173
  hence "approx (t |` (-lnames c1) |` (-lnames c2)) s'"
44070
cebb7abb54b1 import constant folding theory into IMP
kleing
parents:
diff changeset
   174
    by (rule IfFalse)
cebb7abb54b1 import constant folding theory into IMP
kleing
parents:
diff changeset
   175
  thus ?case by simp
cebb7abb54b1 import constant folding theory into IMP
kleing
parents:
diff changeset
   176
qed auto
cebb7abb54b1 import constant folding theory into IMP
kleing
parents:
diff changeset
   177
cebb7abb54b1 import constant folding theory into IMP
kleing
parents:
diff changeset
   178
cebb7abb54b1 import constant folding theory into IMP
kleing
parents:
diff changeset
   179
declare assign_simp [simp]
cebb7abb54b1 import constant folding theory into IMP
kleing
parents:
diff changeset
   180
cebb7abb54b1 import constant folding theory into IMP
kleing
parents:
diff changeset
   181
lemma approx_eq:
cebb7abb54b1 import constant folding theory into IMP
kleing
parents:
diff changeset
   182
  "approx t \<Turnstile> c \<sim> fold c t"
45015
fdac1e9880eb Updated IMP to use new induction method
nipkow
parents: 44890
diff changeset
   183
proof (induction c arbitrary: t)
44070
cebb7abb54b1 import constant folding theory into IMP
kleing
parents:
diff changeset
   184
  case SKIP show ?case by simp
cebb7abb54b1 import constant folding theory into IMP
kleing
parents:
diff changeset
   185
next
cebb7abb54b1 import constant folding theory into IMP
kleing
parents:
diff changeset
   186
  case Assign
cebb7abb54b1 import constant folding theory into IMP
kleing
parents:
diff changeset
   187
  show ?case by (simp add: equiv_up_to_def)
cebb7abb54b1 import constant folding theory into IMP
kleing
parents:
diff changeset
   188
next
48909
b2dea007e55e remove Hoare dependency from Fold.thy
Gerwin Klein <gerwin.klein@nicta.com.au>
parents: 47818
diff changeset
   189
  case Seq
b2dea007e55e remove Hoare dependency from Fold.thy
Gerwin Klein <gerwin.klein@nicta.com.au>
parents: 47818
diff changeset
   190
  thus ?case by (auto intro!: equiv_up_to_seq big_step_pres_approx)
44070
cebb7abb54b1 import constant folding theory into IMP
kleing
parents:
diff changeset
   191
next
cebb7abb54b1 import constant folding theory into IMP
kleing
parents:
diff changeset
   192
  case If
cebb7abb54b1 import constant folding theory into IMP
kleing
parents:
diff changeset
   193
  thus ?case by (auto intro!: equiv_up_to_if_weak)
cebb7abb54b1 import constant folding theory into IMP
kleing
parents:
diff changeset
   194
next
cebb7abb54b1 import constant folding theory into IMP
kleing
parents:
diff changeset
   195
  case (While b c)
48909
b2dea007e55e remove Hoare dependency from Fold.thy
Gerwin Klein <gerwin.klein@nicta.com.au>
parents: 47818
diff changeset
   196
  hence "approx (t |` (- lnames c)) \<Turnstile>
44070
cebb7abb54b1 import constant folding theory into IMP
kleing
parents:
diff changeset
   197
         WHILE b DO c \<sim> WHILE b DO fold c (t |` (- lnames c))"
48909
b2dea007e55e remove Hoare dependency from Fold.thy
Gerwin Klein <gerwin.klein@nicta.com.au>
parents: 47818
diff changeset
   198
    by (auto intro: equiv_up_to_while_weak big_step_pres_approx_restrict)
b2dea007e55e remove Hoare dependency from Fold.thy
Gerwin Klein <gerwin.klein@nicta.com.au>
parents: 47818
diff changeset
   199
  thus ?case
44070
cebb7abb54b1 import constant folding theory into IMP
kleing
parents:
diff changeset
   200
    by (auto intro: equiv_up_to_weaken approx_map_le)
cebb7abb54b1 import constant folding theory into IMP
kleing
parents:
diff changeset
   201
qed
48909
b2dea007e55e remove Hoare dependency from Fold.thy
Gerwin Klein <gerwin.klein@nicta.com.au>
parents: 47818
diff changeset
   202
44070
cebb7abb54b1 import constant folding theory into IMP
kleing
parents:
diff changeset
   203
48909
b2dea007e55e remove Hoare dependency from Fold.thy
Gerwin Klein <gerwin.klein@nicta.com.au>
parents: 47818
diff changeset
   204
lemma approx_empty [simp]:
44070
cebb7abb54b1 import constant folding theory into IMP
kleing
parents:
diff changeset
   205
  "approx empty = (\<lambda>_. True)"
51506
cd573f1a82b2 removed obsolete uses of ext
kleing
parents: 48909
diff changeset
   206
  by (auto simp: approx_def)
44070
cebb7abb54b1 import constant folding theory into IMP
kleing
parents:
diff changeset
   207
cebb7abb54b1 import constant folding theory into IMP
kleing
parents:
diff changeset
   208
lemma equiv_sym:
cebb7abb54b1 import constant folding theory into IMP
kleing
parents:
diff changeset
   209
  "c \<sim> c' \<longleftrightarrow> c' \<sim> c"
cebb7abb54b1 import constant folding theory into IMP
kleing
parents:
diff changeset
   210
  by (auto simp add: equiv_def)
cebb7abb54b1 import constant folding theory into IMP
kleing
parents:
diff changeset
   211
cebb7abb54b1 import constant folding theory into IMP
kleing
parents:
diff changeset
   212
theorem constant_folding_equiv:
cebb7abb54b1 import constant folding theory into IMP
kleing
parents:
diff changeset
   213
  "fold c empty \<sim> c"
cebb7abb54b1 import constant folding theory into IMP
kleing
parents:
diff changeset
   214
  using approx_eq [of empty c]
cebb7abb54b1 import constant folding theory into IMP
kleing
parents:
diff changeset
   215
  by (simp add: equiv_up_to_True equiv_sym)
cebb7abb54b1 import constant folding theory into IMP
kleing
parents:
diff changeset
   216
cebb7abb54b1 import constant folding theory into IMP
kleing
parents:
diff changeset
   217
cebb7abb54b1 import constant folding theory into IMP
kleing
parents:
diff changeset
   218
44850
a6095c96a89b tuned headers
nipkow
parents: 44070
diff changeset
   219
subsection {* More ambitious folding including boolean expressions *}
44070
cebb7abb54b1 import constant folding theory into IMP
kleing
parents:
diff changeset
   220
cebb7abb54b1 import constant folding theory into IMP
kleing
parents:
diff changeset
   221
51514
9bed72e55475 simp_const -> afold; bfold -> fold'; bsimp_const -> bfold
kleing
parents: 51506
diff changeset
   222
fun bfold :: "bexp \<Rightarrow> tab \<Rightarrow> bexp" where
9bed72e55475 simp_const -> afold; bfold -> fold'; bsimp_const -> bfold
kleing
parents: 51506
diff changeset
   223
"bfold (Less a1 a2) t = less (afold a1 t) (afold a2 t)" |
9bed72e55475 simp_const -> afold; bfold -> fold'; bsimp_const -> bfold
kleing
parents: 51506
diff changeset
   224
"bfold (And b1 b2) t = and (bfold b1 t) (bfold b2 t)" |
9bed72e55475 simp_const -> afold; bfold -> fold'; bsimp_const -> bfold
kleing
parents: 51506
diff changeset
   225
"bfold (Not b) t = not(bfold b t)" |
9bed72e55475 simp_const -> afold; bfold -> fold'; bsimp_const -> bfold
kleing
parents: 51506
diff changeset
   226
"bfold (Bc bc) _ = Bc bc"
44070
cebb7abb54b1 import constant folding theory into IMP
kleing
parents:
diff changeset
   227
51514
9bed72e55475 simp_const -> afold; bfold -> fold'; bsimp_const -> bfold
kleing
parents: 51506
diff changeset
   228
theorem bval_bfold [simp]:
44070
cebb7abb54b1 import constant folding theory into IMP
kleing
parents:
diff changeset
   229
  assumes "approx t s"
51514
9bed72e55475 simp_const -> afold; bfold -> fold'; bsimp_const -> bfold
kleing
parents: 51506
diff changeset
   230
  shows "bval (bfold b t) s = bval b s"
44070
cebb7abb54b1 import constant folding theory into IMP
kleing
parents:
diff changeset
   231
  using assms by (induct b) auto
cebb7abb54b1 import constant folding theory into IMP
kleing
parents:
diff changeset
   232
45200
1f1897ac7877 renamed B to Bc
nipkow
parents: 45134
diff changeset
   233
lemma not_Bc [simp]: "not (Bc v) = Bc (\<not>v)"
44070
cebb7abb54b1 import constant folding theory into IMP
kleing
parents:
diff changeset
   234
  by (cases v) auto
cebb7abb54b1 import constant folding theory into IMP
kleing
parents:
diff changeset
   235
45200
1f1897ac7877 renamed B to Bc
nipkow
parents: 45134
diff changeset
   236
lemma not_Bc_eq [simp]: "(not b = Bc v) = (b = Bc (\<not>v))"
44070
cebb7abb54b1 import constant folding theory into IMP
kleing
parents:
diff changeset
   237
  by (cases b) auto
cebb7abb54b1 import constant folding theory into IMP
kleing
parents:
diff changeset
   238
48909
b2dea007e55e remove Hoare dependency from Fold.thy
Gerwin Klein <gerwin.klein@nicta.com.au>
parents: 47818
diff changeset
   239
lemma and_Bc_eq:
45200
1f1897ac7877 renamed B to Bc
nipkow
parents: 45134
diff changeset
   240
  "(and a b = Bc v) =
48909
b2dea007e55e remove Hoare dependency from Fold.thy
Gerwin Klein <gerwin.klein@nicta.com.au>
parents: 47818
diff changeset
   241
   (a = Bc False \<and> \<not>v  \<or>  b = Bc False \<and> \<not>v \<or>
45200
1f1897ac7877 renamed B to Bc
nipkow
parents: 45134
diff changeset
   242
    (\<exists>v1 v2. a = Bc v1 \<and> b = Bc v2 \<and> v = v1 \<and> v2))"
44070
cebb7abb54b1 import constant folding theory into IMP
kleing
parents:
diff changeset
   243
  by (rule and.induct) auto
cebb7abb54b1 import constant folding theory into IMP
kleing
parents:
diff changeset
   244
45200
1f1897ac7877 renamed B to Bc
nipkow
parents: 45134
diff changeset
   245
lemma less_Bc_eq [simp]:
1f1897ac7877 renamed B to Bc
nipkow
parents: 45134
diff changeset
   246
  "(less a b = Bc v) = (\<exists>n1 n2. a = N n1 \<and> b = N n2 \<and> v = (n1 < n2))"
44070
cebb7abb54b1 import constant folding theory into IMP
kleing
parents:
diff changeset
   247
  by (rule less.induct) auto
48909
b2dea007e55e remove Hoare dependency from Fold.thy
Gerwin Klein <gerwin.klein@nicta.com.au>
parents: 47818
diff changeset
   248
51514
9bed72e55475 simp_const -> afold; bfold -> fold'; bsimp_const -> bfold
kleing
parents: 51506
diff changeset
   249
theorem bval_bfold_Bc:
44070
cebb7abb54b1 import constant folding theory into IMP
kleing
parents:
diff changeset
   250
assumes "approx t s"
51514
9bed72e55475 simp_const -> afold; bfold -> fold'; bsimp_const -> bfold
kleing
parents: 51506
diff changeset
   251
shows "bfold b t = Bc v \<Longrightarrow> bval b s = v"
44070
cebb7abb54b1 import constant folding theory into IMP
kleing
parents:
diff changeset
   252
  using assms
cebb7abb54b1 import constant folding theory into IMP
kleing
parents:
diff changeset
   253
  by (induct b arbitrary: v)
51514
9bed72e55475 simp_const -> afold; bfold -> fold'; bsimp_const -> bfold
kleing
parents: 51506
diff changeset
   254
     (auto simp: approx_def and_Bc_eq aval_afold_N
44070
cebb7abb54b1 import constant folding theory into IMP
kleing
parents:
diff changeset
   255
           split: bexp.splits bool.splits)
cebb7abb54b1 import constant folding theory into IMP
kleing
parents:
diff changeset
   256
cebb7abb54b1 import constant folding theory into IMP
kleing
parents:
diff changeset
   257
cebb7abb54b1 import constant folding theory into IMP
kleing
parents:
diff changeset
   258
primrec "bdefs" :: "com \<Rightarrow> tab \<Rightarrow> tab" where
cebb7abb54b1 import constant folding theory into IMP
kleing
parents:
diff changeset
   259
"bdefs SKIP t = t" |
cebb7abb54b1 import constant folding theory into IMP
kleing
parents:
diff changeset
   260
"bdefs (x ::= a) t =
51514
9bed72e55475 simp_const -> afold; bfold -> fold'; bsimp_const -> bfold
kleing
parents: 51506
diff changeset
   261
  (case afold a t of N k \<Rightarrow> t(x \<mapsto> k) | _ \<Rightarrow> t(x:=None))" |
44070
cebb7abb54b1 import constant folding theory into IMP
kleing
parents:
diff changeset
   262
"bdefs (c1;c2) t = (bdefs c2 o bdefs c1) t" |
51514
9bed72e55475 simp_const -> afold; bfold -> fold'; bsimp_const -> bfold
kleing
parents: 51506
diff changeset
   263
"bdefs (IF b THEN c1 ELSE c2) t = (case bfold b t of
45200
1f1897ac7877 renamed B to Bc
nipkow
parents: 45134
diff changeset
   264
    Bc True \<Rightarrow> bdefs c1 t
1f1897ac7877 renamed B to Bc
nipkow
parents: 45134
diff changeset
   265
  | Bc False \<Rightarrow> bdefs c2 t
44070
cebb7abb54b1 import constant folding theory into IMP
kleing
parents:
diff changeset
   266
  | _ \<Rightarrow> merge (bdefs c1 t) (bdefs c2 t))" |
48909
b2dea007e55e remove Hoare dependency from Fold.thy
Gerwin Klein <gerwin.klein@nicta.com.au>
parents: 47818
diff changeset
   267
"bdefs (WHILE b DO c) t = t |` (-lnames c)"
44070
cebb7abb54b1 import constant folding theory into IMP
kleing
parents:
diff changeset
   268
cebb7abb54b1 import constant folding theory into IMP
kleing
parents:
diff changeset
   269
51514
9bed72e55475 simp_const -> afold; bfold -> fold'; bsimp_const -> bfold
kleing
parents: 51506
diff changeset
   270
primrec fold' where
9bed72e55475 simp_const -> afold; bfold -> fold'; bsimp_const -> bfold
kleing
parents: 51506
diff changeset
   271
"fold' SKIP _ = SKIP" |
9bed72e55475 simp_const -> afold; bfold -> fold'; bsimp_const -> bfold
kleing
parents: 51506
diff changeset
   272
"fold' (x ::= a) t = (x ::= (afold a t))" |
9bed72e55475 simp_const -> afold; bfold -> fold'; bsimp_const -> bfold
kleing
parents: 51506
diff changeset
   273
"fold' (c1;c2) t = (fold' c1 t; fold' c2 (bdefs c1 t))" |
9bed72e55475 simp_const -> afold; bfold -> fold'; bsimp_const -> bfold
kleing
parents: 51506
diff changeset
   274
"fold' (IF b THEN c1 ELSE c2) t = (case bfold b t of
9bed72e55475 simp_const -> afold; bfold -> fold'; bsimp_const -> bfold
kleing
parents: 51506
diff changeset
   275
    Bc True \<Rightarrow> fold' c1 t
9bed72e55475 simp_const -> afold; bfold -> fold'; bsimp_const -> bfold
kleing
parents: 51506
diff changeset
   276
  | Bc False \<Rightarrow> fold' c2 t
9bed72e55475 simp_const -> afold; bfold -> fold'; bsimp_const -> bfold
kleing
parents: 51506
diff changeset
   277
  | _ \<Rightarrow> IF bfold b t THEN fold' c1 t ELSE fold' c2 t)" |
9bed72e55475 simp_const -> afold; bfold -> fold'; bsimp_const -> bfold
kleing
parents: 51506
diff changeset
   278
"fold' (WHILE b DO c) t = (case bfold b t of
45200
1f1897ac7877 renamed B to Bc
nipkow
parents: 45134
diff changeset
   279
    Bc False \<Rightarrow> SKIP
51514
9bed72e55475 simp_const -> afold; bfold -> fold'; bsimp_const -> bfold
kleing
parents: 51506
diff changeset
   280
  | _ \<Rightarrow> WHILE bfold b (t |` (-lnames c)) DO fold' c (t |` (-lnames c)))"
44070
cebb7abb54b1 import constant folding theory into IMP
kleing
parents:
diff changeset
   281
cebb7abb54b1 import constant folding theory into IMP
kleing
parents:
diff changeset
   282
cebb7abb54b1 import constant folding theory into IMP
kleing
parents:
diff changeset
   283
lemma bdefs_restrict:
cebb7abb54b1 import constant folding theory into IMP
kleing
parents:
diff changeset
   284
  "bdefs c t |` (- lnames c) = t |` (- lnames c)"
45015
fdac1e9880eb Updated IMP to use new induction method
nipkow
parents: 44890
diff changeset
   285
proof (induction c arbitrary: t)
47818
151d137f1095 renamed Semi to Seq
nipkow
parents: 45212
diff changeset
   286
  case (Seq c1 c2)
48909
b2dea007e55e remove Hoare dependency from Fold.thy
Gerwin Klein <gerwin.klein@nicta.com.au>
parents: 47818
diff changeset
   287
  hence "bdefs c1 t |` (- lnames c1) = t |` (- lnames c1)"
44070
cebb7abb54b1 import constant folding theory into IMP
kleing
parents:
diff changeset
   288
    by simp
48909
b2dea007e55e remove Hoare dependency from Fold.thy
Gerwin Klein <gerwin.klein@nicta.com.au>
parents: 47818
diff changeset
   289
  hence "bdefs c1 t |` (- lnames c1) |` (-lnames c2) =
44070
cebb7abb54b1 import constant folding theory into IMP
kleing
parents:
diff changeset
   290
         t |` (- lnames c1) |` (-lnames c2)" by simp
cebb7abb54b1 import constant folding theory into IMP
kleing
parents:
diff changeset
   291
  moreover
47818
151d137f1095 renamed Semi to Seq
nipkow
parents: 45212
diff changeset
   292
  from Seq
48909
b2dea007e55e remove Hoare dependency from Fold.thy
Gerwin Klein <gerwin.klein@nicta.com.au>
parents: 47818
diff changeset
   293
  have "bdefs c2 (bdefs c1 t) |` (- lnames c2) =
44070
cebb7abb54b1 import constant folding theory into IMP
kleing
parents:
diff changeset
   294
        bdefs c1 t |` (- lnames c2)"
cebb7abb54b1 import constant folding theory into IMP
kleing
parents:
diff changeset
   295
    by simp
cebb7abb54b1 import constant folding theory into IMP
kleing
parents:
diff changeset
   296
  hence "bdefs c2 (bdefs c1 t) |` (- lnames c2) |` (- lnames c1) =
cebb7abb54b1 import constant folding theory into IMP
kleing
parents:
diff changeset
   297
         bdefs c1 t |` (- lnames c2) |` (- lnames c1)"
cebb7abb54b1 import constant folding theory into IMP
kleing
parents:
diff changeset
   298
    by simp
cebb7abb54b1 import constant folding theory into IMP
kleing
parents:
diff changeset
   299
  ultimately
cebb7abb54b1 import constant folding theory into IMP
kleing
parents:
diff changeset
   300
  show ?case by (clarsimp simp: Int_commute)
cebb7abb54b1 import constant folding theory into IMP
kleing
parents:
diff changeset
   301
next
cebb7abb54b1 import constant folding theory into IMP
kleing
parents:
diff changeset
   302
  case (If b c1 c2)
cebb7abb54b1 import constant folding theory into IMP
kleing
parents:
diff changeset
   303
  hence "bdefs c1 t |` (- lnames c1) = t |` (- lnames c1)" by simp
48909
b2dea007e55e remove Hoare dependency from Fold.thy
Gerwin Klein <gerwin.klein@nicta.com.au>
parents: 47818
diff changeset
   304
  hence "bdefs c1 t |` (- lnames c1) |` (-lnames c2) =
44070
cebb7abb54b1 import constant folding theory into IMP
kleing
parents:
diff changeset
   305
         t |` (- lnames c1) |` (-lnames c2)" by simp
cebb7abb54b1 import constant folding theory into IMP
kleing
parents:
diff changeset
   306
  moreover
cebb7abb54b1 import constant folding theory into IMP
kleing
parents:
diff changeset
   307
  from If
cebb7abb54b1 import constant folding theory into IMP
kleing
parents:
diff changeset
   308
  have "bdefs c2 t |` (- lnames c2) = t |` (- lnames c2)" by simp
48909
b2dea007e55e remove Hoare dependency from Fold.thy
Gerwin Klein <gerwin.klein@nicta.com.au>
parents: 47818
diff changeset
   309
  hence "bdefs c2 t |` (- lnames c2) |` (-lnames c1) =
44070
cebb7abb54b1 import constant folding theory into IMP
kleing
parents:
diff changeset
   310
         t |` (- lnames c2) |` (-lnames c1)" by simp
cebb7abb54b1 import constant folding theory into IMP
kleing
parents:
diff changeset
   311
  ultimately
48909
b2dea007e55e remove Hoare dependency from Fold.thy
Gerwin Klein <gerwin.klein@nicta.com.au>
parents: 47818
diff changeset
   312
  show ?case
b2dea007e55e remove Hoare dependency from Fold.thy
Gerwin Klein <gerwin.klein@nicta.com.au>
parents: 47818
diff changeset
   313
    by (auto simp: Int_commute intro: merge_restrict
44070
cebb7abb54b1 import constant folding theory into IMP
kleing
parents:
diff changeset
   314
             split: bexp.splits bool.splits)
cebb7abb54b1 import constant folding theory into IMP
kleing
parents:
diff changeset
   315
qed (auto split: aexp.split bexp.split bool.split)
cebb7abb54b1 import constant folding theory into IMP
kleing
parents:
diff changeset
   316
cebb7abb54b1 import constant folding theory into IMP
kleing
parents:
diff changeset
   317
cebb7abb54b1 import constant folding theory into IMP
kleing
parents:
diff changeset
   318
lemma big_step_pres_approx_b:
48909
b2dea007e55e remove Hoare dependency from Fold.thy
Gerwin Klein <gerwin.klein@nicta.com.au>
parents: 47818
diff changeset
   319
  "(c,s) \<Rightarrow> s' \<Longrightarrow> approx t s \<Longrightarrow> approx (bdefs c t) s'"
45015
fdac1e9880eb Updated IMP to use new induction method
nipkow
parents: 44890
diff changeset
   320
proof (induction arbitrary: t rule: big_step_induct)
44070
cebb7abb54b1 import constant folding theory into IMP
kleing
parents:
diff changeset
   321
  case Skip thus ?case by simp
cebb7abb54b1 import constant folding theory into IMP
kleing
parents:
diff changeset
   322
next
cebb7abb54b1 import constant folding theory into IMP
kleing
parents:
diff changeset
   323
  case Assign
cebb7abb54b1 import constant folding theory into IMP
kleing
parents:
diff changeset
   324
  thus ?case
51514
9bed72e55475 simp_const -> afold; bfold -> fold'; bsimp_const -> bfold
kleing
parents: 51506
diff changeset
   325
    by (clarsimp simp: aval_afold_N approx_def split: aexp.split)
44070
cebb7abb54b1 import constant folding theory into IMP
kleing
parents:
diff changeset
   326
next
47818
151d137f1095 renamed Semi to Seq
nipkow
parents: 45212
diff changeset
   327
  case (Seq c1 s1 s2 c2 s3)
151d137f1095 renamed Semi to Seq
nipkow
parents: 45212
diff changeset
   328
  have "approx (bdefs c1 t) s2" by (rule Seq.IH(1)[OF Seq.prems])
151d137f1095 renamed Semi to Seq
nipkow
parents: 45212
diff changeset
   329
  hence "approx (bdefs c2 (bdefs c1 t)) s3" by (rule Seq.IH(2))
44070
cebb7abb54b1 import constant folding theory into IMP
kleing
parents:
diff changeset
   330
  thus ?case by simp
cebb7abb54b1 import constant folding theory into IMP
kleing
parents:
diff changeset
   331
next
cebb7abb54b1 import constant folding theory into IMP
kleing
parents:
diff changeset
   332
  case (IfTrue b s c1 s')
cebb7abb54b1 import constant folding theory into IMP
kleing
parents:
diff changeset
   333
  hence "approx (bdefs c1 t) s'" by simp
cebb7abb54b1 import constant folding theory into IMP
kleing
parents:
diff changeset
   334
  thus ?case using `bval b s` `approx t s`
51514
9bed72e55475 simp_const -> afold; bfold -> fold'; bsimp_const -> bfold
kleing
parents: 51506
diff changeset
   335
    by (clarsimp simp: approx_merge bval_bfold_Bc
44070
cebb7abb54b1 import constant folding theory into IMP
kleing
parents:
diff changeset
   336
                 split: bexp.splits bool.splits)
cebb7abb54b1 import constant folding theory into IMP
kleing
parents:
diff changeset
   337
next
cebb7abb54b1 import constant folding theory into IMP
kleing
parents:
diff changeset
   338
  case (IfFalse b s c2 s')
cebb7abb54b1 import constant folding theory into IMP
kleing
parents:
diff changeset
   339
  hence "approx (bdefs c2 t) s'" by simp
cebb7abb54b1 import constant folding theory into IMP
kleing
parents:
diff changeset
   340
  thus ?case using `\<not>bval b s` `approx t s`
51514
9bed72e55475 simp_const -> afold; bfold -> fold'; bsimp_const -> bfold
kleing
parents: 51506
diff changeset
   341
    by (clarsimp simp: approx_merge bval_bfold_Bc
44070
cebb7abb54b1 import constant folding theory into IMP
kleing
parents:
diff changeset
   342
                 split: bexp.splits bool.splits)
cebb7abb54b1 import constant folding theory into IMP
kleing
parents:
diff changeset
   343
next
cebb7abb54b1 import constant folding theory into IMP
kleing
parents:
diff changeset
   344
  case WhileFalse
48909
b2dea007e55e remove Hoare dependency from Fold.thy
Gerwin Klein <gerwin.klein@nicta.com.au>
parents: 47818
diff changeset
   345
  thus ?case
51514
9bed72e55475 simp_const -> afold; bfold -> fold'; bsimp_const -> bfold
kleing
parents: 51506
diff changeset
   346
    by (clarsimp simp: bval_bfold_Bc approx_def restrict_map_def
44070
cebb7abb54b1 import constant folding theory into IMP
kleing
parents:
diff changeset
   347
                 split: bexp.splits bool.splits)
cebb7abb54b1 import constant folding theory into IMP
kleing
parents:
diff changeset
   348
next
cebb7abb54b1 import constant folding theory into IMP
kleing
parents:
diff changeset
   349
  case (WhileTrue b s1 c s2 s3)
cebb7abb54b1 import constant folding theory into IMP
kleing
parents:
diff changeset
   350
  hence "approx (bdefs c t) s2" by simp
cebb7abb54b1 import constant folding theory into IMP
kleing
parents:
diff changeset
   351
  with WhileTrue
cebb7abb54b1 import constant folding theory into IMP
kleing
parents:
diff changeset
   352
  have "approx (bdefs c t |` (- lnames c)) s3"
cebb7abb54b1 import constant folding theory into IMP
kleing
parents:
diff changeset
   353
    by simp
48909
b2dea007e55e remove Hoare dependency from Fold.thy
Gerwin Klein <gerwin.klein@nicta.com.au>
parents: 47818
diff changeset
   354
  thus ?case
44070
cebb7abb54b1 import constant folding theory into IMP
kleing
parents:
diff changeset
   355
    by (simp add: bdefs_restrict)
cebb7abb54b1 import constant folding theory into IMP
kleing
parents:
diff changeset
   356
qed
cebb7abb54b1 import constant folding theory into IMP
kleing
parents:
diff changeset
   357
51514
9bed72e55475 simp_const -> afold; bfold -> fold'; bsimp_const -> bfold
kleing
parents: 51506
diff changeset
   358
lemma fold'_equiv:
9bed72e55475 simp_const -> afold; bfold -> fold'; bsimp_const -> bfold
kleing
parents: 51506
diff changeset
   359
  "approx t \<Turnstile> c \<sim> fold' c t"
45015
fdac1e9880eb Updated IMP to use new induction method
nipkow
parents: 44890
diff changeset
   360
proof (induction c arbitrary: t)
44070
cebb7abb54b1 import constant folding theory into IMP
kleing
parents:
diff changeset
   361
  case SKIP show ?case by simp
cebb7abb54b1 import constant folding theory into IMP
kleing
parents:
diff changeset
   362
next
cebb7abb54b1 import constant folding theory into IMP
kleing
parents:
diff changeset
   363
  case Assign
cebb7abb54b1 import constant folding theory into IMP
kleing
parents:
diff changeset
   364
  thus ?case by (simp add: equiv_up_to_def)
cebb7abb54b1 import constant folding theory into IMP
kleing
parents:
diff changeset
   365
next
47818
151d137f1095 renamed Semi to Seq
nipkow
parents: 45212
diff changeset
   366
  case Seq
48909
b2dea007e55e remove Hoare dependency from Fold.thy
Gerwin Klein <gerwin.klein@nicta.com.au>
parents: 47818
diff changeset
   367
  thus ?case by (auto intro!: equiv_up_to_seq big_step_pres_approx_b)
44070
cebb7abb54b1 import constant folding theory into IMP
kleing
parents:
diff changeset
   368
next
cebb7abb54b1 import constant folding theory into IMP
kleing
parents:
diff changeset
   369
  case (If b c1 c2)
48909
b2dea007e55e remove Hoare dependency from Fold.thy
Gerwin Klein <gerwin.klein@nicta.com.au>
parents: 47818
diff changeset
   370
  hence "approx t \<Turnstile> IF b THEN c1 ELSE c2 \<sim>
51514
9bed72e55475 simp_const -> afold; bfold -> fold'; bsimp_const -> bfold
kleing
parents: 51506
diff changeset
   371
                   IF bfold b t THEN fold' c1 t ELSE fold' c2 t"
48909
b2dea007e55e remove Hoare dependency from Fold.thy
Gerwin Klein <gerwin.klein@nicta.com.au>
parents: 47818
diff changeset
   372
    by (auto intro: equiv_up_to_if_weak simp: bequiv_up_to_def)
44070
cebb7abb54b1 import constant folding theory into IMP
kleing
parents:
diff changeset
   373
  thus ?case using If
51514
9bed72e55475 simp_const -> afold; bfold -> fold'; bsimp_const -> bfold
kleing
parents: 51506
diff changeset
   374
    by (fastforce simp: bval_bfold_Bc split: bexp.splits bool.splits
44070
cebb7abb54b1 import constant folding theory into IMP
kleing
parents:
diff changeset
   375
                 intro: equiv_up_to_trans)
cebb7abb54b1 import constant folding theory into IMP
kleing
parents:
diff changeset
   376
  next
cebb7abb54b1 import constant folding theory into IMP
kleing
parents:
diff changeset
   377
  case (While b c)
48909
b2dea007e55e remove Hoare dependency from Fold.thy
Gerwin Klein <gerwin.klein@nicta.com.au>
parents: 47818
diff changeset
   378
  hence "approx (t |` (- lnames c)) \<Turnstile>
44070
cebb7abb54b1 import constant folding theory into IMP
kleing
parents:
diff changeset
   379
                   WHILE b DO c \<sim>
51514
9bed72e55475 simp_const -> afold; bfold -> fold'; bsimp_const -> bfold
kleing
parents: 51506
diff changeset
   380
                   WHILE bfold b (t |` (- lnames c))
9bed72e55475 simp_const -> afold; bfold -> fold'; bsimp_const -> bfold
kleing
parents: 51506
diff changeset
   381
                      DO fold' c (t |` (- lnames c))" (is "_ \<Turnstile> ?W \<sim> ?W'")
48909
b2dea007e55e remove Hoare dependency from Fold.thy
Gerwin Klein <gerwin.klein@nicta.com.au>
parents: 47818
diff changeset
   382
    by (auto intro: equiv_up_to_while_weak big_step_pres_approx_restrict
44070
cebb7abb54b1 import constant folding theory into IMP
kleing
parents:
diff changeset
   383
             simp: bequiv_up_to_def)
cebb7abb54b1 import constant folding theory into IMP
kleing
parents:
diff changeset
   384
  hence "approx t \<Turnstile> ?W \<sim> ?W'"
cebb7abb54b1 import constant folding theory into IMP
kleing
parents:
diff changeset
   385
    by (auto intro: equiv_up_to_weaken approx_map_le)
cebb7abb54b1 import constant folding theory into IMP
kleing
parents:
diff changeset
   386
  thus ?case
48909
b2dea007e55e remove Hoare dependency from Fold.thy
Gerwin Klein <gerwin.klein@nicta.com.au>
parents: 47818
diff changeset
   387
    by (auto split: bexp.splits bool.splits
b2dea007e55e remove Hoare dependency from Fold.thy
Gerwin Klein <gerwin.klein@nicta.com.au>
parents: 47818
diff changeset
   388
             intro: equiv_up_to_while_False
51514
9bed72e55475 simp_const -> afold; bfold -> fold'; bsimp_const -> bfold
kleing
parents: 51506
diff changeset
   389
             simp: bval_bfold_Bc)
44070
cebb7abb54b1 import constant folding theory into IMP
kleing
parents:
diff changeset
   390
qed
cebb7abb54b1 import constant folding theory into IMP
kleing
parents:
diff changeset
   391
cebb7abb54b1 import constant folding theory into IMP
kleing
parents:
diff changeset
   392
51514
9bed72e55475 simp_const -> afold; bfold -> fold'; bsimp_const -> bfold
kleing
parents: 51506
diff changeset
   393
theorem constant_folding_equiv':
9bed72e55475 simp_const -> afold; bfold -> fold'; bsimp_const -> bfold
kleing
parents: 51506
diff changeset
   394
  "fold' c empty \<sim> c"
9bed72e55475 simp_const -> afold; bfold -> fold'; bsimp_const -> bfold
kleing
parents: 51506
diff changeset
   395
  using fold'_equiv [of empty c]
44070
cebb7abb54b1 import constant folding theory into IMP
kleing
parents:
diff changeset
   396
  by (simp add: equiv_up_to_True equiv_sym)
cebb7abb54b1 import constant folding theory into IMP
kleing
parents:
diff changeset
   397
cebb7abb54b1 import constant folding theory into IMP
kleing
parents:
diff changeset
   398
cebb7abb54b1 import constant folding theory into IMP
kleing
parents:
diff changeset
   399
end