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