src/HOL/IMP/Comp_Rev.thy
author kleing
Sun, 14 Apr 2013 21:54:45 +1000
changeset 52842 3d213f39d83c
parent 52396 1491459df114
permissions -rw-r--r--
added another definition snipped
kleing@52396
     1
(* Author: Gerwin Klein *)
kleing@52396
     2
kleing@44296
     3
theory Comp_Rev
kleing@44296
     4
imports Compiler
kleing@44296
     5
begin
kleing@44296
     6
nipkow@51076
     7
section {* Compiler Correctness, Reverse Direction *}
kleing@44296
     8
kleing@44296
     9
subsection {* Definitions *}
kleing@44296
    10
kleing@44296
    11
text {* Execution in @{term n} steps for simpler induction *}
kleing@44296
    12
primrec 
kleing@44296
    13
  exec_n :: "instr list \<Rightarrow> config \<Rightarrow> nat \<Rightarrow> config \<Rightarrow> bool" 
kleing@44871
    14
  ("_/ \<turnstile> (_ \<rightarrow>^_/ _)" [65,0,1000,55] 55)
kleing@44296
    15
where 
kleing@44296
    16
  "P \<turnstile> c \<rightarrow>^0 c' = (c'=c)" |
kleing@44296
    17
  "P \<turnstile> c \<rightarrow>^(Suc n) c'' = (\<exists>c'. (P \<turnstile> c \<rightarrow> c') \<and> P \<turnstile> c' \<rightarrow>^n c'')"
kleing@44296
    18
kleing@52396
    19
text {* The possible successor PCs of an instruction at position @{term n} *}
kleing@52842
    20
text_raw{*\snip{isuccsdef}{0}{1}{% *}
kleing@44296
    21
definition
kleing@44296
    22
  "isuccs i n \<equiv> case i of 
kleing@44296
    23
     JMP j \<Rightarrow> {n + 1 + j}
kleing@46193
    24
   | JMPLESS j \<Rightarrow> {n + 1 + j, n + 1}
kleing@46193
    25
   | JMPGE j \<Rightarrow> {n + 1 + j, n + 1}
kleing@44296
    26
   | _ \<Rightarrow> {n +1}"
kleing@52842
    27
text_raw{*}%endsnip*}
kleing@44296
    28
kleing@52396
    29
text {* The possible successors PCs of an instruction list *}
kleing@44296
    30
definition
kleing@52396
    31
  succs :: "instr list \<Rightarrow> int \<Rightarrow> int set" where
kleing@52396
    32
  "succs P n = {s. \<exists>i::int. 0 \<le> i \<and> i < size P \<and> s \<in> isuccs (P!!i) (n+i)}" 
kleing@44296
    33
kleing@52396
    34
text {* Possible exit PCs of a program *}
kleing@44296
    35
definition
kleing@52396
    36
  "exits P = succs P 0 - {0..< size P}"
kleing@44296
    37
kleing@44296
    38
  
kleing@44296
    39
subsection {* Basic properties of @{term exec_n} *}
kleing@44296
    40
kleing@44296
    41
lemma exec_n_exec:
kleing@44296
    42
  "P \<turnstile> c \<rightarrow>^n c' \<Longrightarrow> P \<turnstile> c \<rightarrow>* c'"
nipkow@51075
    43
  by (induct n arbitrary: c) (auto simp del: exec1_def)
kleing@44296
    44
kleing@44296
    45
lemma exec_0 [intro!]: "P \<turnstile> c \<rightarrow>^0 c" by simp
kleing@44296
    46
kleing@46089
    47
lemma exec_Suc:
kleing@44871
    48
  "\<lbrakk> P \<turnstile> c \<rightarrow> c'; P \<turnstile> c' \<rightarrow>^n c'' \<rbrakk> \<Longrightarrow> P \<turnstile> c \<rightarrow>^(Suc n) c''" 
nipkow@45761
    49
  by (fastforce simp del: split_paired_Ex)
kleing@44296
    50
kleing@44296
    51
lemma exec_exec_n:
kleing@44296
    52
  "P \<turnstile> c \<rightarrow>* c' \<Longrightarrow> \<exists>n. P \<turnstile> c \<rightarrow>^n c'"
nipkow@51075
    53
  by (induct rule: exec.induct) (auto simp del: exec1_def intro: exec_Suc)
kleing@44296
    54
    
kleing@44296
    55
lemma exec_eq_exec_n:
kleing@44296
    56
  "(P \<turnstile> c \<rightarrow>* c') = (\<exists>n. P \<turnstile> c \<rightarrow>^n c')"
kleing@44296
    57
  by (blast intro: exec_exec_n exec_n_exec)
kleing@44296
    58
kleing@44296
    59
lemma exec_n_Nil [simp]:
kleing@44296
    60
  "[] \<turnstile> c \<rightarrow>^k c' = (c' = c \<and> k = 0)"
kleing@44296
    61
  by (induct k) auto
kleing@44296
    62
kleing@44875
    63
lemma exec1_exec_n [intro!]:
kleing@44296
    64
  "P \<turnstile> c \<rightarrow> c' \<Longrightarrow> P \<turnstile> c \<rightarrow>^1 c'"
kleing@44296
    65
  by (cases c') simp
kleing@44296
    66
kleing@44296
    67
kleing@44296
    68
subsection {* Concrete symbolic execution steps *}
kleing@44296
    69
kleing@44296
    70
lemma exec_n_step:
kleing@44296
    71
  "n \<noteq> n' \<Longrightarrow> 
kleing@44296
    72
  P \<turnstile> (n,stk,s) \<rightarrow>^k (n',stk',s') = 
kleing@44296
    73
  (\<exists>c. P \<turnstile> (n,stk,s) \<rightarrow> c \<and> P \<turnstile> c \<rightarrow>^(k - 1) (n',stk',s') \<and> 0 < k)"
kleing@44296
    74
  by (cases k) auto
kleing@44296
    75
kleing@44296
    76
lemma exec1_end:
kleing@52396
    77
  "size P <= fst c \<Longrightarrow> \<not> P \<turnstile> c \<rightarrow> c'"
kleing@44296
    78
  by auto
kleing@44296
    79
kleing@44296
    80
lemma exec_n_end:
kleing@52396
    81
  "size P <= (n::int) \<Longrightarrow> 
kleing@44296
    82
  P \<turnstile> (n,s,stk) \<rightarrow>^k (n',s',stk') = (n' = n \<and> stk'=stk \<and> s'=s \<and> k =0)"
kleing@44296
    83
  by (cases k) (auto simp: exec1_end)
kleing@44296
    84
kleing@44296
    85
lemmas exec_n_simps = exec_n_step exec_n_end
kleing@44296
    86
kleing@44296
    87
kleing@44296
    88
subsection {* Basic properties of @{term succs} *}
kleing@44296
    89
kleing@44296
    90
lemma succs_simps [simp]: 
kleing@44296
    91
  "succs [ADD] n = {n + 1}"
kleing@44296
    92
  "succs [LOADI v] n = {n + 1}"
kleing@44296
    93
  "succs [LOAD x] n = {n + 1}"
kleing@44296
    94
  "succs [STORE x] n = {n + 1}"
kleing@44296
    95
  "succs [JMP i] n = {n + 1 + i}"
kleing@46193
    96
  "succs [JMPGE i] n = {n + 1 + i, n + 1}"
kleing@46193
    97
  "succs [JMPLESS i] n = {n + 1 + i, n + 1}"
kleing@44296
    98
  by (auto simp: succs_def isuccs_def)
kleing@44296
    99
kleing@44296
   100
lemma succs_empty [iff]: "succs [] n = {}"
kleing@44296
   101
  by (simp add: succs_def)
kleing@44296
   102
kleing@44296
   103
lemma succs_Cons:
kleing@44296
   104
  "succs (x#xs) n = isuccs x n \<union> succs xs (1+n)" (is "_ = ?x \<union> ?xs")
kleing@44296
   105
proof 
kleing@52396
   106
  let ?isuccs = "\<lambda>p P n i::int. 0 \<le> i \<and> i < size P \<and> p \<in> isuccs (P!!i) (n+i)"
kleing@44296
   107
  { fix p assume "p \<in> succs (x#xs) n"
kleing@52396
   108
    then obtain i::int where isuccs: "?isuccs p (x#xs) n i"
kleing@44296
   109
      unfolding succs_def by auto     
kleing@44296
   110
    have "p \<in> ?x \<union> ?xs" 
kleing@44296
   111
    proof cases
kleing@44296
   112
      assume "i = 0" with isuccs show ?thesis by simp
kleing@44296
   113
    next
kleing@44296
   114
      assume "i \<noteq> 0" 
kleing@44296
   115
      with isuccs 
kleing@44296
   116
      have "?isuccs p xs (1+n) (i - 1)" by auto
kleing@44296
   117
      hence "p \<in> ?xs" unfolding succs_def by blast
kleing@44296
   118
      thus ?thesis .. 
kleing@44296
   119
    qed
kleing@44296
   120
  } 
kleing@44296
   121
  thus "succs (x#xs) n \<subseteq> ?x \<union> ?xs" ..
kleing@44296
   122
  
kleing@44296
   123
  { fix p assume "p \<in> ?x \<or> p \<in> ?xs"
kleing@44296
   124
    hence "p \<in> succs (x#xs) n"
kleing@44296
   125
    proof
nipkow@45761
   126
      assume "p \<in> ?x" thus ?thesis by (fastforce simp: succs_def)
kleing@44296
   127
    next
kleing@44296
   128
      assume "p \<in> ?xs"
kleing@44296
   129
      then obtain i where "?isuccs p xs (1+n) i"
kleing@44296
   130
        unfolding succs_def by auto
kleing@44296
   131
      hence "?isuccs p (x#xs) n (1+i)"
kleing@44296
   132
        by (simp add: algebra_simps)
kleing@44296
   133
      thus ?thesis unfolding succs_def by blast
kleing@44296
   134
    qed
kleing@44296
   135
  }  
kleing@44296
   136
  thus "?x \<union> ?xs \<subseteq> succs (x#xs) n" by blast
kleing@44296
   137
qed
kleing@44296
   138
kleing@44296
   139
lemma succs_iexec1:
kleing@52396
   140
  assumes "c' = iexec (P!!i) (i,s,stk)" "0 \<le> i" "i < size P"
kleing@44875
   141
  shows "fst c' \<in> succs P 0"
nipkow@51075
   142
  using assms by (auto simp: succs_def isuccs_def split: instr.split)
kleing@44296
   143
kleing@44296
   144
lemma succs_shift:
kleing@44296
   145
  "(p - n \<in> succs P 0) = (p \<in> succs P n)" 
nipkow@45761
   146
  by (fastforce simp: succs_def isuccs_def split: instr.split)
kleing@44296
   147
  
kleing@44296
   148
lemma inj_op_plus [simp]:
kleing@44296
   149
  "inj (op + (i::int))"
kleing@44296
   150
  by (metis add_minus_cancel inj_on_inverseI)
kleing@44296
   151
kleing@44296
   152
lemma succs_set_shift [simp]:
kleing@44296
   153
  "op + i ` succs xs 0 = succs xs i"
kleing@44296
   154
  by (force simp: succs_shift [where n=i, symmetric] intro: set_eqI)
kleing@44296
   155
kleing@44296
   156
lemma succs_append [simp]:
kleing@52396
   157
  "succs (xs @ ys) n = succs xs n \<union> succs ys (n + size xs)"
kleing@44296
   158
  by (induct xs arbitrary: n) (auto simp: succs_Cons algebra_simps)
kleing@44296
   159
kleing@44296
   160
kleing@44296
   161
lemma exits_append [simp]:
kleing@52396
   162
  "exits (xs @ ys) = exits xs \<union> (op + (size xs)) ` exits ys - 
kleing@52396
   163
                     {0..<size xs + size ys}" 
kleing@44296
   164
  by (auto simp: exits_def image_set_diff)
kleing@44296
   165
  
kleing@44296
   166
lemma exits_single:
kleing@44296
   167
  "exits [x] = isuccs x 0 - {0}"
kleing@44296
   168
  by (auto simp: exits_def succs_def)
kleing@44296
   169
  
kleing@44296
   170
lemma exits_Cons:
kleing@44296
   171
  "exits (x # xs) = (isuccs x 0 - {0}) \<union> (op + 1) ` exits xs - 
kleing@52396
   172
                     {0..<1 + size xs}" 
kleing@44296
   173
  using exits_append [of "[x]" xs]
kleing@44296
   174
  by (simp add: exits_single)
kleing@44296
   175
kleing@44296
   176
lemma exits_empty [iff]: "exits [] = {}" by (simp add: exits_def)
kleing@44296
   177
kleing@44296
   178
lemma exits_simps [simp]:
kleing@44296
   179
  "exits [ADD] = {1}"
kleing@44296
   180
  "exits [LOADI v] = {1}"
kleing@44296
   181
  "exits [LOAD x] = {1}"
kleing@44296
   182
  "exits [STORE x] = {1}"
kleing@44296
   183
  "i \<noteq> -1 \<Longrightarrow> exits [JMP i] = {1 + i}"
kleing@46193
   184
  "i \<noteq> -1 \<Longrightarrow> exits [JMPGE i] = {1 + i, 1}"
kleing@46193
   185
  "i \<noteq> -1 \<Longrightarrow> exits [JMPLESS i] = {1 + i, 1}"
kleing@44296
   186
  by (auto simp: exits_def)
kleing@44296
   187
kleing@44296
   188
lemma acomp_succs [simp]:
kleing@52396
   189
  "succs (acomp a) n = {n + 1 .. n + size (acomp a)}"
kleing@44296
   190
  by (induct a arbitrary: n) auto
kleing@44296
   191
kleing@44296
   192
lemma acomp_size:
kleing@52396
   193
  "(1::int) \<le> size (acomp a)"
kleing@44296
   194
  by (induct a) auto
kleing@44296
   195
kleing@44881
   196
lemma acomp_exits [simp]:
kleing@52396
   197
  "exits (acomp a) = {size (acomp a)}"
kleing@44296
   198
  by (auto simp: exits_def acomp_size)
kleing@44296
   199
kleing@44296
   200
lemma bcomp_succs:
kleing@44296
   201
  "0 \<le> i \<Longrightarrow>
kleing@52396
   202
  succs (bcomp b c i) n \<subseteq> {n .. n + size (bcomp b c i)}
kleing@52396
   203
                           \<union> {n + i + size (bcomp b c i)}" 
nipkow@45886
   204
proof (induction b arbitrary: c i n)
kleing@44296
   205
  case (And b1 b2)
kleing@44296
   206
  from And.prems
kleing@44296
   207
  show ?case 
kleing@44296
   208
    by (cases c)
nipkow@45886
   209
       (auto dest: And.IH(1) [THEN subsetD, rotated] 
nipkow@45886
   210
                   And.IH(2) [THEN subsetD, rotated])
kleing@44296
   211
qed auto
kleing@44296
   212
kleing@44296
   213
lemmas bcomp_succsD [dest!] = bcomp_succs [THEN subsetD, rotated]
kleing@44296
   214
kleing@44296
   215
lemma bcomp_exits:
kleing@52396
   216
  fixes i :: int
kleing@52396
   217
  shows
kleing@44296
   218
  "0 \<le> i \<Longrightarrow>
kleing@52396
   219
  exits (bcomp b c i) \<subseteq> {size (bcomp b c i), i + size (bcomp b c i)}" 
kleing@44296
   220
  by (auto simp: exits_def)
kleing@44296
   221
  
kleing@44296
   222
lemma bcomp_exitsD [dest!]:
kleing@44296
   223
  "p \<in> exits (bcomp b c i) \<Longrightarrow> 0 \<le> i \<Longrightarrow> 
kleing@52396
   224
  p = size (bcomp b c i) \<or> p = i + size (bcomp b c i)"
kleing@44296
   225
  using bcomp_exits by auto
kleing@44296
   226
kleing@44296
   227
lemma ccomp_succs:
kleing@52396
   228
  "succs (ccomp c) n \<subseteq> {n..n + size (ccomp c)}"
nipkow@45886
   229
proof (induction c arbitrary: n)
kleing@44296
   230
  case SKIP thus ?case by simp
kleing@44296
   231
next
kleing@44296
   232
  case Assign thus ?case by simp
kleing@44296
   233
next
nipkow@48689
   234
  case (Seq c1 c2)
nipkow@48689
   235
  from Seq.prems
kleing@44296
   236
  show ?case 
nipkow@48689
   237
    by (fastforce dest: Seq.IH [THEN subsetD])
kleing@44296
   238
next
kleing@44296
   239
  case (If b c1 c2)
kleing@44296
   240
  from If.prems
kleing@44296
   241
  show ?case
nipkow@45886
   242
    by (auto dest!: If.IH [THEN subsetD] simp: isuccs_def succs_Cons)
kleing@44296
   243
next
kleing@44296
   244
  case (While b c)
kleing@44296
   245
  from While.prems
nipkow@45886
   246
  show ?case by (auto dest!: While.IH [THEN subsetD])
kleing@44296
   247
qed
kleing@44296
   248
kleing@44296
   249
lemma ccomp_exits:
kleing@52396
   250
  "exits (ccomp c) \<subseteq> {size (ccomp c)}"
kleing@44296
   251
  using ccomp_succs [of c 0] by (auto simp: exits_def)
kleing@44296
   252
kleing@44296
   253
lemma ccomp_exitsD [dest!]:
kleing@52396
   254
  "p \<in> exits (ccomp c) \<Longrightarrow> p = size (ccomp c)"
kleing@44296
   255
  using ccomp_exits by auto
kleing@44296
   256
kleing@44296
   257
kleing@44296
   258
subsection {* Splitting up machine executions *}
kleing@44296
   259
kleing@44296
   260
lemma exec1_split:
kleing@52396
   261
  fixes i j :: int
kleing@52396
   262
  shows
kleing@52396
   263
  "P @ c @ P' \<turnstile> (size P + i, s) \<rightarrow> (j,s') \<Longrightarrow> 0 \<le> i \<Longrightarrow> i < size c \<Longrightarrow> 
kleing@52396
   264
  c \<turnstile> (i,s) \<rightarrow> (j - size P, s')"
nipkow@51075
   265
  by (auto split: instr.splits)
kleing@44296
   266
kleing@44296
   267
lemma exec_n_split:
kleing@52396
   268
  fixes i j :: int
kleing@52396
   269
  assumes "P @ c @ P' \<turnstile> (size P + i, s) \<rightarrow>^n (j, s')"
kleing@52396
   270
          "0 \<le> i" "i < size c" 
kleing@52396
   271
          "j \<notin> {size P ..< size P + size c}"
kleing@52396
   272
  shows "\<exists>s'' (i'::int) k m. 
kleing@44296
   273
                   c \<turnstile> (i, s) \<rightarrow>^k (i', s'') \<and>
kleing@44296
   274
                   i' \<in> exits c \<and> 
kleing@52396
   275
                   P @ c @ P' \<turnstile> (size P + i', s'') \<rightarrow>^m (j, s') \<and>
kleing@44296
   276
                   n = k + m" 
nipkow@45886
   277
using assms proof (induction n arbitrary: i j s)
kleing@44296
   278
  case 0
kleing@44296
   279
  thus ?case by simp
kleing@44296
   280
next
kleing@44296
   281
  case (Suc n)
kleing@52396
   282
  have i: "0 \<le> i" "i < size c" by fact+
kleing@44296
   283
  from Suc.prems
kleing@52396
   284
  have j: "\<not> (size P \<le> j \<and> j < size P + size c)" by simp
kleing@44296
   285
  from Suc.prems 
kleing@44296
   286
  obtain i0 s0 where
kleing@52396
   287
    step: "P @ c @ P' \<turnstile> (size P + i, s) \<rightarrow> (i0,s0)" and
kleing@44296
   288
    rest: "P @ c @ P' \<turnstile> (i0,s0) \<rightarrow>^n (j, s')"
kleing@44296
   289
    by clarsimp
kleing@44296
   290
kleing@44296
   291
  from step i
kleing@52396
   292
  have c: "c \<turnstile> (i,s) \<rightarrow> (i0 - size P, s0)" by (rule exec1_split)
kleing@44296
   293
kleing@52396
   294
  have "i0 = size P + (i0 - size P) " by simp
kleing@52396
   295
  then obtain j0::int where j0: "i0 = size P + j0"  ..
kleing@44296
   296
kleing@44296
   297
  note split_paired_Ex [simp del]
kleing@44296
   298
kleing@52396
   299
  { assume "j0 \<in> {0 ..< size c}"
kleing@44296
   300
    with j0 j rest c
kleing@44296
   301
    have ?case
nipkow@45886
   302
      by (fastforce dest!: Suc.IH intro!: exec_Suc)
kleing@44296
   303
  } moreover {
kleing@52396
   304
    assume "j0 \<notin> {0 ..< size c}"
kleing@44296
   305
    moreover
kleing@44296
   306
    from c j0 have "j0 \<in> succs c 0"
nipkow@51075
   307
      by (auto dest: succs_iexec1 simp del: iexec.simps)
kleing@44296
   308
    ultimately
kleing@44296
   309
    have "j0 \<in> exits c" by (simp add: exits_def)
kleing@44296
   310
    with c j0 rest
nipkow@45761
   311
    have ?case by fastforce
kleing@44296
   312
  }
kleing@44296
   313
  ultimately
kleing@44296
   314
  show ?case by cases
kleing@44296
   315
qed
kleing@44296
   316
kleing@44296
   317
lemma exec_n_drop_right:
kleing@52396
   318
  fixes j :: int
kleing@52396
   319
  assumes "c @ P' \<turnstile> (0, s) \<rightarrow>^n (j, s')" "j \<notin> {0..<size c}"
kleing@44875
   320
  shows "\<exists>s'' i' k m. 
kleing@44875
   321
          (if c = [] then s'' = s \<and> i' = 0 \<and> k = 0
kleing@44875
   322
           else c \<turnstile> (0, s) \<rightarrow>^k (i', s'') \<and>
kleing@44875
   323
           i' \<in> exits c) \<and> 
kleing@44875
   324
           c @ P' \<turnstile> (i', s'') \<rightarrow>^m (j, s') \<and>
kleing@44875
   325
           n = k + m"
kleing@44875
   326
  using assms
kleing@44296
   327
  by (cases "c = []")
kleing@44296
   328
     (auto dest: exec_n_split [where P="[]", simplified])
kleing@44296
   329
  
kleing@44296
   330
kleing@44296
   331
text {*
kleing@44296
   332
  Dropping the left context of a potentially incomplete execution of @{term c}.
kleing@44296
   333
*}
kleing@44296
   334
kleing@44296
   335
lemma exec1_drop_left:
kleing@52396
   336
  fixes i n :: int
kleing@52396
   337
  assumes "P1 @ P2 \<turnstile> (i, s, stk) \<rightarrow> (n, s', stk')" and "size P1 \<le> i"
kleing@52396
   338
  shows "P2 \<turnstile> (i - size P1, s, stk) \<rightarrow> (n - size P1, s', stk')"
kleing@44296
   339
proof -
kleing@52396
   340
  have "i = size P1 + (i - size P1)" by simp 
kleing@52396
   341
  then obtain i' :: int where "i = size P1 + i'" ..
kleing@44296
   342
  moreover
kleing@52396
   343
  have "n = size P1 + (n - size P1)" by simp 
kleing@52396
   344
  then obtain n' :: int where "n = size P1 + n'" ..
kleing@44296
   345
  ultimately 
nipkow@51075
   346
  show ?thesis using assms by (clarsimp simp del: iexec.simps)
kleing@44296
   347
qed
kleing@44296
   348
kleing@44296
   349
lemma exec_n_drop_left:
kleing@52396
   350
  fixes i n :: int
kleing@44875
   351
  assumes "P @ P' \<turnstile> (i, s, stk) \<rightarrow>^k (n, s', stk')"
kleing@52396
   352
          "size P \<le> i" "exits P' \<subseteq> {0..}"
kleing@52396
   353
  shows "P' \<turnstile> (i - size P, s, stk) \<rightarrow>^k (n - size P, s', stk')"
nipkow@45886
   354
using assms proof (induction k arbitrary: i s stk)
kleing@44296
   355
  case 0 thus ?case by simp
kleing@44296
   356
next
kleing@44296
   357
  case (Suc k)
kleing@44296
   358
  from Suc.prems
kleing@44296
   359
  obtain i' s'' stk'' where
kleing@44296
   360
    step: "P @ P' \<turnstile> (i, s, stk) \<rightarrow> (i', s'', stk'')" and
kleing@44296
   361
    rest: "P @ P' \<turnstile> (i', s'', stk'') \<rightarrow>^k (n, s', stk')"
nipkow@51075
   362
    by (auto simp del: exec1_def)
kleing@52396
   363
  from step `size P \<le> i`
kleing@52396
   364
  have "P' \<turnstile> (i - size P, s, stk) \<rightarrow> (i' - size P, s'', stk'')" 
kleing@44296
   365
    by (rule exec1_drop_left)
kleing@46089
   366
  moreover
kleing@52396
   367
  then have "i' - size P \<in> succs P' 0"
nipkow@51075
   368
    by (fastforce dest!: succs_iexec1 simp del: iexec.simps)
kleing@44296
   369
  with `exits P' \<subseteq> {0..}`
kleing@52396
   370
  have "size P \<le> i'" by (auto simp: exits_def)
kleing@44296
   371
  from rest this `exits P' \<subseteq> {0..}`     
kleing@52396
   372
  have "P' \<turnstile> (i' - size P, s'', stk'') \<rightarrow>^k (n - size P, s', stk')"
nipkow@45886
   373
    by (rule Suc.IH)
kleing@46089
   374
  ultimately
kleing@46089
   375
  show ?case by auto
kleing@44296
   376
qed
kleing@44296
   377
kleing@44296
   378
lemmas exec_n_drop_Cons = 
wenzelm@46476
   379
  exec_n_drop_left [where P="[instr]", simplified] for instr
kleing@44296
   380
kleing@44296
   381
definition
kleing@52396
   382
  "closed P \<longleftrightarrow> exits P \<subseteq> {size P}" 
kleing@44296
   383
kleing@44296
   384
lemma ccomp_closed [simp, intro!]: "closed (ccomp c)"
kleing@44296
   385
  using ccomp_exits by (auto simp: closed_def)
kleing@44296
   386
kleing@44296
   387
lemma acomp_closed [simp, intro!]: "closed (acomp c)"
kleing@44296
   388
  by (simp add: closed_def)
kleing@44296
   389
kleing@44296
   390
lemma exec_n_split_full:
kleing@52396
   391
  fixes j :: int
kleing@44296
   392
  assumes exec: "P @ P' \<turnstile> (0,s,stk) \<rightarrow>^k (j, s', stk')"
kleing@52396
   393
  assumes P: "size P \<le> j" 
kleing@44296
   394
  assumes closed: "closed P"
kleing@44296
   395
  assumes exits: "exits P' \<subseteq> {0..}"
kleing@52396
   396
  shows "\<exists>k1 k2 s'' stk''. P \<turnstile> (0,s,stk) \<rightarrow>^k1 (size P, s'', stk'') \<and> 
kleing@52396
   397
                           P' \<turnstile> (0,s'',stk'') \<rightarrow>^k2 (j - size P, s', stk')"
kleing@44296
   398
proof (cases "P")
kleing@44296
   399
  case Nil with exec
nipkow@45761
   400
  show ?thesis by fastforce
kleing@44296
   401
next
kleing@44296
   402
  case Cons
kleing@52396
   403
  hence "0 < size P" by simp
kleing@44296
   404
  with exec P closed
kleing@44296
   405
  obtain k1 k2 s'' stk'' where
kleing@52396
   406
    1: "P \<turnstile> (0,s,stk) \<rightarrow>^k1 (size P, s'', stk'')" and
kleing@52396
   407
    2: "P @ P' \<turnstile> (size P,s'',stk'') \<rightarrow>^k2 (j, s', stk')"
kleing@44296
   408
    by (auto dest!: exec_n_split [where P="[]" and i=0, simplified] 
kleing@44296
   409
             simp: closed_def)
kleing@44296
   410
  moreover
kleing@52396
   411
  have "j = size P + (j - size P)" by simp
kleing@52396
   412
  then obtain j0 :: int where "j = size P + j0" ..
kleing@44296
   413
  ultimately
kleing@44296
   414
  show ?thesis using exits
nipkow@45761
   415
    by (fastforce dest: exec_n_drop_left)
kleing@44296
   416
qed
kleing@44296
   417
kleing@44296
   418
kleing@44296
   419
subsection {* Correctness theorem *}
kleing@44296
   420
kleing@44296
   421
lemma acomp_neq_Nil [simp]:
kleing@44296
   422
  "acomp a \<noteq> []"
kleing@44296
   423
  by (induct a) auto
kleing@44296
   424
kleing@44296
   425
lemma acomp_exec_n [dest!]:
kleing@52396
   426
  "acomp a \<turnstile> (0,s,stk) \<rightarrow>^n (size (acomp a),s',stk') \<Longrightarrow> 
kleing@44296
   427
  s' = s \<and> stk' = aval a s#stk"
nipkow@45886
   428
proof (induction a arbitrary: n s' stk stk')
kleing@44296
   429
  case (Plus a1 a2)
kleing@52396
   430
  let ?sz = "size (acomp a1) + (size (acomp a2) + 1)"
kleing@44296
   431
  from Plus.prems
kleing@44296
   432
  have "acomp a1 @ acomp a2 @ [ADD] \<turnstile> (0,s,stk) \<rightarrow>^n (?sz, s', stk')" 
kleing@44296
   433
    by (simp add: algebra_simps)
kleing@44296
   434
      
kleing@44296
   435
  then obtain n1 s1 stk1 n2 s2 stk2 n3 where 
kleing@52396
   436
    "acomp a1 \<turnstile> (0,s,stk) \<rightarrow>^n1 (size (acomp a1), s1, stk1)"
kleing@52396
   437
    "acomp a2 \<turnstile> (0,s1,stk1) \<rightarrow>^n2 (size (acomp a2), s2, stk2)" 
kleing@44296
   438
       "[ADD] \<turnstile> (0,s2,stk2) \<rightarrow>^n3 (1, s', stk')"
kleing@44296
   439
    by (auto dest!: exec_n_split_full)
kleing@44296
   440
nipkow@45886
   441
  thus ?case by (fastforce dest: Plus.IH simp: exec_n_simps)
kleing@44296
   442
qed (auto simp: exec_n_simps)
kleing@44296
   443
kleing@44296
   444
lemma bcomp_split:
kleing@52396
   445
  fixes i j :: int
kleing@44875
   446
  assumes "bcomp b c i @ P' \<turnstile> (0, s, stk) \<rightarrow>^n (j, s', stk')" 
kleing@52396
   447
          "j \<notin> {0..<size (bcomp b c i)}" "0 \<le> i"
kleing@52396
   448
  shows "\<exists>s'' stk'' (i'::int) k m. 
kleing@44296
   449
           bcomp b c i \<turnstile> (0, s, stk) \<rightarrow>^k (i', s'', stk'') \<and>
kleing@52396
   450
           (i' = size (bcomp b c i) \<or> i' = i + size (bcomp b c i)) \<and>
kleing@44296
   451
           bcomp b c i @ P' \<turnstile> (i', s'', stk'') \<rightarrow>^m (j, s', stk') \<and>
kleing@44296
   452
           n = k + m"
nipkow@45761
   453
  using assms by (cases "bcomp b c i = []") (fastforce dest!: exec_n_drop_right)+
kleing@44296
   454
kleing@44296
   455
lemma bcomp_exec_n [dest]:
kleing@52396
   456
  fixes i j :: int
kleing@44875
   457
  assumes "bcomp b c j \<turnstile> (0, s, stk) \<rightarrow>^n (i, s', stk')"
kleing@52396
   458
          "size (bcomp b c j) \<le> i" "0 \<le> j"
kleing@52396
   459
  shows "i = size(bcomp b c j) + (if c = bval b s then j else 0) \<and>
kleing@44875
   460
         s' = s \<and> stk' = stk"
nipkow@45886
   461
using assms proof (induction b arbitrary: c j i n s' stk')
nipkow@46068
   462
  case Bc thus ?case 
kleing@44296
   463
    by (simp split: split_if_asm add: exec_n_simps)
kleing@44296
   464
next
kleing@44296
   465
  case (Not b) 
kleing@44296
   466
  from Not.prems show ?case
nipkow@45886
   467
    by (fastforce dest!: Not.IH) 
kleing@44296
   468
next
kleing@44296
   469
  case (And b1 b2)
kleing@44296
   470
  
kleing@44296
   471
  let ?b2 = "bcomp b2 c j" 
kleing@52396
   472
  let ?m  = "if c then size ?b2 else size ?b2 + j"
kleing@44296
   473
  let ?b1 = "bcomp b1 False ?m" 
kleing@44296
   474
kleing@52396
   475
  have j: "size (bcomp (And b1 b2) c j) \<le> i" "0 \<le> j" by fact+
kleing@44296
   476
  
kleing@44296
   477
  from And.prems
kleing@52396
   478
  obtain s'' stk'' and i'::int and k m where 
kleing@44296
   479
    b1: "?b1 \<turnstile> (0, s, stk) \<rightarrow>^k (i', s'', stk'')"
kleing@52396
   480
        "i' = size ?b1 \<or> i' = ?m + size ?b1" and
kleing@52396
   481
    b2: "?b2 \<turnstile> (i' - size ?b1, s'', stk'') \<rightarrow>^m (i - size ?b1, s', stk')"
kleing@44296
   482
    by (auto dest!: bcomp_split dest: exec_n_drop_left)
kleing@44296
   483
  from b1 j
kleing@52396
   484
  have "i' = size ?b1 + (if \<not>bval b1 s then ?m else 0) \<and> s'' = s \<and> stk'' = stk"
nipkow@45886
   485
    by (auto dest!: And.IH)
kleing@44296
   486
  with b2 j
kleing@44296
   487
  show ?case 
nipkow@45886
   488
    by (fastforce dest!: And.IH simp: exec_n_end split: split_if_asm)
kleing@44296
   489
next
kleing@44296
   490
  case Less
kleing@44296
   491
  thus ?case by (auto dest!: exec_n_split_full simp: exec_n_simps) (* takes time *) 
kleing@44296
   492
qed
kleing@44296
   493
kleing@44296
   494
lemma ccomp_empty [elim!]:
kleing@44296
   495
  "ccomp c = [] \<Longrightarrow> (c,s) \<Rightarrow> s"
kleing@44296
   496
  by (induct c) auto
kleing@44296
   497
kleing@44934
   498
declare assign_simp [simp]
kleing@44296
   499
kleing@44296
   500
lemma ccomp_exec_n:
kleing@52396
   501
  "ccomp c \<turnstile> (0,s,stk) \<rightarrow>^n (size(ccomp c),t,stk')
kleing@44296
   502
  \<Longrightarrow> (c,s) \<Rightarrow> t \<and> stk'=stk"
nipkow@45886
   503
proof (induction c arbitrary: s t stk stk' n)
kleing@44296
   504
  case SKIP
kleing@44296
   505
  thus ?case by auto
kleing@44296
   506
next
kleing@44296
   507
  case (Assign x a)
kleing@44296
   508
  thus ?case
nipkow@45761
   509
    by simp (fastforce dest!: exec_n_split_full simp: exec_n_simps)
kleing@44296
   510
next
nipkow@48689
   511
  case (Seq c1 c2)
nipkow@45761
   512
  thus ?case by (fastforce dest!: exec_n_split_full)
kleing@44296
   513
next
kleing@44296
   514
  case (If b c1 c2)
nipkow@45886
   515
  note If.IH [dest!]
kleing@44296
   516
kleing@44296
   517
  let ?if = "IF b THEN c1 ELSE c2"
kleing@44296
   518
  let ?cs = "ccomp ?if"
kleing@52396
   519
  let ?bcomp = "bcomp b False (size (ccomp c1) + 1)"
kleing@44296
   520
  
kleing@52396
   521
  from `?cs \<turnstile> (0,s,stk) \<rightarrow>^n (size ?cs,t,stk')`
kleing@52396
   522
  obtain i' :: int and k m s'' stk'' where
kleing@52396
   523
    cs: "?cs \<turnstile> (i',s'',stk'') \<rightarrow>^m (size ?cs,t,stk')" and
kleing@44296
   524
        "?bcomp \<turnstile> (0,s,stk) \<rightarrow>^k (i', s'', stk'')" 
kleing@52396
   525
        "i' = size ?bcomp \<or> i' = size ?bcomp + size (ccomp c1) + 1"
kleing@44296
   526
    by (auto dest!: bcomp_split)
kleing@44296
   527
kleing@44296
   528
  hence i':
kleing@44296
   529
    "s''=s" "stk'' = stk" 
kleing@52396
   530
    "i' = (if bval b s then size ?bcomp else size ?bcomp+size(ccomp c1)+1)"
kleing@44296
   531
    by auto
kleing@44296
   532
  
kleing@44296
   533
  with cs have cs':
kleing@52396
   534
    "ccomp c1@JMP (size (ccomp c2))#ccomp c2 \<turnstile> 
kleing@52396
   535
       (if bval b s then 0 else size (ccomp c1)+1, s, stk) \<rightarrow>^m
kleing@52396
   536
       (1 + size (ccomp c1) + size (ccomp c2), t, stk')"
nipkow@45761
   537
    by (fastforce dest: exec_n_drop_left simp: exits_Cons isuccs_def algebra_simps)
kleing@44296
   538
     
kleing@44296
   539
  show ?case
kleing@44296
   540
  proof (cases "bval b s")
kleing@44296
   541
    case True with cs'
kleing@44296
   542
    show ?thesis
kleing@44296
   543
      by simp
nipkow@45761
   544
         (fastforce dest: exec_n_drop_right 
kleing@44296
   545
                   split: split_if_asm simp: exec_n_simps)
kleing@44296
   546
  next
kleing@44296
   547
    case False with cs'
kleing@44296
   548
    show ?thesis
kleing@44296
   549
      by (auto dest!: exec_n_drop_Cons exec_n_drop_left 
kleing@44296
   550
               simp: exits_Cons isuccs_def)
kleing@44296
   551
  qed
kleing@44296
   552
next
kleing@44296
   553
  case (While b c)
kleing@44296
   554
kleing@44296
   555
  from While.prems
kleing@44296
   556
  show ?case
nipkow@45886
   557
  proof (induction n arbitrary: s rule: nat_less_induct)
kleing@44296
   558
    case (1 n)
kleing@44296
   559
    
kleing@44296
   560
    { assume "\<not> bval b s"
kleing@44296
   561
      with "1.prems"
kleing@44296
   562
      have ?case
kleing@44296
   563
        by simp
nipkow@45761
   564
           (fastforce dest!: bcomp_exec_n bcomp_split 
kleing@44296
   565
                     simp: exec_n_simps)
kleing@44296
   566
    } moreover {
kleing@44296
   567
      assume b: "bval b s"
kleing@44296
   568
      let ?c0 = "WHILE b DO c"
kleing@44296
   569
      let ?cs = "ccomp ?c0"
kleing@52396
   570
      let ?bs = "bcomp b False (size (ccomp c) + 1)"
kleing@52396
   571
      let ?jmp = "[JMP (-((size ?bs + size (ccomp c) + 1)))]"
kleing@44296
   572
      
kleing@44296
   573
      from "1.prems" b
kleing@44296
   574
      obtain k where
kleing@52396
   575
        cs: "?cs \<turnstile> (size ?bs, s, stk) \<rightarrow>^k (size ?cs, t, stk')" and
kleing@44296
   576
        k:  "k \<le> n"
nipkow@45761
   577
        by (fastforce dest!: bcomp_split)
kleing@44296
   578
      
kleing@44296
   579
      have ?case
kleing@44296
   580
      proof cases
kleing@44296
   581
        assume "ccomp c = []"
kleing@44296
   582
        with cs k
kleing@44296
   583
        obtain m where
kleing@52396
   584
          "?cs \<turnstile> (0,s,stk) \<rightarrow>^m (size (ccomp ?c0), t, stk')"
kleing@44296
   585
          "m < n"
kleing@44296
   586
          by (auto simp: exec_n_step [where k=k])
nipkow@45886
   587
        with "1.IH"
kleing@44296
   588
        show ?case by blast
kleing@44296
   589
      next
kleing@44296
   590
        assume "ccomp c \<noteq> []"
kleing@44296
   591
        with cs
kleing@44296
   592
        obtain m m' s'' stk'' where
kleing@52396
   593
          c: "ccomp c \<turnstile> (0, s, stk) \<rightarrow>^m' (size (ccomp c), s'', stk'')" and 
kleing@52396
   594
          rest: "?cs \<turnstile> (size ?bs + size (ccomp c), s'', stk'') \<rightarrow>^m 
kleing@52396
   595
                       (size ?cs, t, stk')" and
kleing@44296
   596
          m: "k = m + m'"
kleing@44296
   597
          by (auto dest: exec_n_split [where i=0, simplified])
kleing@44296
   598
        from c
kleing@44296
   599
        have "(c,s) \<Rightarrow> s''" and stk: "stk'' = stk"
nipkow@45886
   600
          by (auto dest!: While.IH)
kleing@44296
   601
        moreover
kleing@44296
   602
        from rest m k stk
kleing@44296
   603
        obtain k' where
kleing@52396
   604
          "?cs \<turnstile> (0, s'', stk) \<rightarrow>^k' (size ?cs, t, stk')"
kleing@44296
   605
          "k' < n"
kleing@44296
   606
          by (auto simp: exec_n_step [where k=m])
nipkow@45886
   607
        with "1.IH"
kleing@44296
   608
        have "(?c0, s'') \<Rightarrow> t \<and> stk' = stk" by blast
kleing@44296
   609
        ultimately
kleing@44296
   610
        show ?case using b by blast
kleing@44296
   611
      qed
kleing@44296
   612
    }
kleing@44296
   613
    ultimately show ?case by cases
kleing@44296
   614
  qed
kleing@44296
   615
qed
kleing@44296
   616
kleing@44296
   617
theorem ccomp_exec:
kleing@52396
   618
  "ccomp c \<turnstile> (0,s,stk) \<rightarrow>* (size(ccomp c),t,stk') \<Longrightarrow> (c,s) \<Rightarrow> t"
kleing@44296
   619
  by (auto dest: exec_exec_n ccomp_exec_n)
kleing@44296
   620
kleing@44296
   621
corollary ccomp_sound:
kleing@52396
   622
  "ccomp c \<turnstile> (0,s,stk) \<rightarrow>* (size(ccomp c),t,stk)  \<longleftrightarrow>  (c,s) \<Rightarrow> t"
kleing@44296
   623
  by (blast intro!: ccomp_exec ccomp_bigstep)
kleing@44296
   624
kleing@44296
   625
end