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