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