| author | traytel | 
| Tue, 22 Jan 2013 14:33:45 +0100 | |
| changeset 51673 | 4dfa00e264d8 | 
| parent 51259 | 1491459df114 | 
| child 52046 | bc01725d7918 | 
| permissions | -rw-r--r-- | 
| 
51259
 
1491459df114
eliminated isize in favour of size + type coercion
 
kleing 
parents: 
50133 
diff
changeset
 | 
1  | 
(* Author: Tobias Nipkow and Gerwin Klein *)  | 
| 43141 | 2  | 
|
| 50061 | 3  | 
header "Compiler for IMP"  | 
| 10343 | 4  | 
|
| 
43438
 
a666b8d11252
IMP compiler with int, added reverse soundness direction
 
kleing 
parents: 
43158 
diff
changeset
 | 
5  | 
theory Compiler imports Big_Step  | 
| 43141 | 6  | 
begin  | 
| 12429 | 7  | 
|
| 
43438
 
a666b8d11252
IMP compiler with int, added reverse soundness direction
 
kleing 
parents: 
43158 
diff
changeset
 | 
8  | 
subsection "List setup"  | 
| 
 
a666b8d11252
IMP compiler with int, added reverse soundness direction
 
kleing 
parents: 
43158 
diff
changeset
 | 
9  | 
|
| 
51259
 
1491459df114
eliminated isize in favour of size + type coercion
 
kleing 
parents: 
50133 
diff
changeset
 | 
10  | 
text {* 
 | 
| 
 
1491459df114
eliminated isize in favour of size + type coercion
 
kleing 
parents: 
50133 
diff
changeset
 | 
11  | 
In the following, we use the length of lists as integers  | 
| 
 
1491459df114
eliminated isize in favour of size + type coercion
 
kleing 
parents: 
50133 
diff
changeset
 | 
12  | 
  instead of natural numbers. Instead of converting @{typ nat}
 | 
| 
 
1491459df114
eliminated isize in favour of size + type coercion
 
kleing 
parents: 
50133 
diff
changeset
 | 
13  | 
  to @{typ int} explicitly, we tell Isabelle to coerce @{typ nat}
 | 
| 
 
1491459df114
eliminated isize in favour of size + type coercion
 
kleing 
parents: 
50133 
diff
changeset
 | 
14  | 
automatically when necessary.  | 
| 
43438
 
a666b8d11252
IMP compiler with int, added reverse soundness direction
 
kleing 
parents: 
43158 
diff
changeset
 | 
15  | 
*}  | 
| 
51259
 
1491459df114
eliminated isize in favour of size + type coercion
 
kleing 
parents: 
50133 
diff
changeset
 | 
16  | 
declare [[coercion_enabled]]  | 
| 
 
1491459df114
eliminated isize in favour of size + type coercion
 
kleing 
parents: 
50133 
diff
changeset
 | 
17  | 
declare [[coercion "int :: nat \<Rightarrow> int"]]  | 
| 
43438
 
a666b8d11252
IMP compiler with int, added reverse soundness direction
 
kleing 
parents: 
43158 
diff
changeset
 | 
18  | 
|
| 
51259
 
1491459df114
eliminated isize in favour of size + type coercion
 
kleing 
parents: 
50133 
diff
changeset
 | 
19  | 
text {* 
 | 
| 
 
1491459df114
eliminated isize in favour of size + type coercion
 
kleing 
parents: 
50133 
diff
changeset
 | 
20  | 
Similarly, we will want to access the ith element of a list,  | 
| 
 
1491459df114
eliminated isize in favour of size + type coercion
 
kleing 
parents: 
50133 
diff
changeset
 | 
21  | 
  where @{term i} is an @{typ int}.
 | 
| 
 
1491459df114
eliminated isize in favour of size + type coercion
 
kleing 
parents: 
50133 
diff
changeset
 | 
22  | 
*}  | 
| 45637 | 23  | 
fun inth :: "'a list \<Rightarrow> int \<Rightarrow> 'a" (infixl "!!" 100) where  | 
24  | 
"(x # xs) !! n = (if n = 0 then x else xs !! (n - 1))"  | 
|
| 
43438
 
a666b8d11252
IMP compiler with int, added reverse soundness direction
 
kleing 
parents: 
43158 
diff
changeset
 | 
25  | 
|
| 
 
a666b8d11252
IMP compiler with int, added reverse soundness direction
 
kleing 
parents: 
43158 
diff
changeset
 | 
26  | 
text {*
 | 
| 
51259
 
1491459df114
eliminated isize in favour of size + type coercion
 
kleing 
parents: 
50133 
diff
changeset
 | 
27  | 
The only additional lemma we need about this function  | 
| 
 
1491459df114
eliminated isize in favour of size + type coercion
 
kleing 
parents: 
50133 
diff
changeset
 | 
28  | 
is indexing over append:  | 
| 
43438
 
a666b8d11252
IMP compiler with int, added reverse soundness direction
 
kleing 
parents: 
43158 
diff
changeset
 | 
29  | 
*}  | 
| 
 
a666b8d11252
IMP compiler with int, added reverse soundness direction
 
kleing 
parents: 
43158 
diff
changeset
 | 
30  | 
lemma inth_append [simp]:  | 
| 
 
a666b8d11252
IMP compiler with int, added reverse soundness direction
 
kleing 
parents: 
43158 
diff
changeset
 | 
31  | 
"0 \<le> n \<Longrightarrow>  | 
| 
51259
 
1491459df114
eliminated isize in favour of size + type coercion
 
kleing 
parents: 
50133 
diff
changeset
 | 
32  | 
(xs @ ys) !! n = (if n < size xs then xs !! n else ys !! (n - size xs))"  | 
| 
50060
 
43753eca324a
replaced relation by function - simplifies development
 
nipkow 
parents: 
47818 
diff
changeset
 | 
33  | 
by (induction xs arbitrary: n) (auto simp: algebra_simps)  | 
| 
43438
 
a666b8d11252
IMP compiler with int, added reverse soundness direction
 
kleing 
parents: 
43158 
diff
changeset
 | 
34  | 
|
| 43141 | 35  | 
subsection "Instructions and Stack Machine"  | 
| 10342 | 36  | 
|
| 43141 | 37  | 
datatype instr =  | 
| 45323 | 38  | 
LOADI int |  | 
39  | 
LOAD vname |  | 
|
| 
43438
 
a666b8d11252
IMP compiler with int, added reverse soundness direction
 
kleing 
parents: 
43158 
diff
changeset
 | 
40  | 
ADD |  | 
| 45323 | 41  | 
STORE vname |  | 
| 
43438
 
a666b8d11252
IMP compiler with int, added reverse soundness direction
 
kleing 
parents: 
43158 
diff
changeset
 | 
42  | 
JMP int |  | 
| 
45322
 
654cc47f6115
JMPF(LESS|GE) -> JMP(LESS|GE) because jumps are int now.
 
kleing 
parents: 
45200 
diff
changeset
 | 
43  | 
JMPLESS int |  | 
| 
 
654cc47f6115
JMPF(LESS|GE) -> JMP(LESS|GE) because jumps are int now.
 
kleing 
parents: 
45200 
diff
changeset
 | 
44  | 
JMPGE int  | 
| 10342 | 45  | 
|
| 
43438
 
a666b8d11252
IMP compiler with int, added reverse soundness direction
 
kleing 
parents: 
43158 
diff
changeset
 | 
46  | 
type_synonym stack = "val list"  | 
| 45637 | 47  | 
type_synonym config = "int \<times> state \<times> stack"  | 
| 43141 | 48  | 
|
49  | 
abbreviation "hd2 xs == hd(tl xs)"  | 
|
50  | 
abbreviation "tl2 xs == tl(tl xs)"  | 
|
| 11275 | 51  | 
|
| 
50060
 
43753eca324a
replaced relation by function - simplifies development
 
nipkow 
parents: 
47818 
diff
changeset
 | 
52  | 
fun iexec :: "instr \<Rightarrow> config \<Rightarrow> config" where  | 
| 
 
43753eca324a
replaced relation by function - simplifies development
 
nipkow 
parents: 
47818 
diff
changeset
 | 
53  | 
"iexec instr (i,s,stk) = (case instr of  | 
| 
 
43753eca324a
replaced relation by function - simplifies development
 
nipkow 
parents: 
47818 
diff
changeset
 | 
54  | 
LOADI n \<Rightarrow> (i+1,s, n#stk) |  | 
| 
 
43753eca324a
replaced relation by function - simplifies development
 
nipkow 
parents: 
47818 
diff
changeset
 | 
55  | 
LOAD x \<Rightarrow> (i+1,s, s x # stk) |  | 
| 
 
43753eca324a
replaced relation by function - simplifies development
 
nipkow 
parents: 
47818 
diff
changeset
 | 
56  | 
ADD \<Rightarrow> (i+1,s, (hd2 stk + hd stk) # tl2 stk) |  | 
| 
 
43753eca324a
replaced relation by function - simplifies development
 
nipkow 
parents: 
47818 
diff
changeset
 | 
57  | 
STORE x \<Rightarrow> (i+1,s(x := hd stk),tl stk) |  | 
| 
 
43753eca324a
replaced relation by function - simplifies development
 
nipkow 
parents: 
47818 
diff
changeset
 | 
58  | 
JMP n \<Rightarrow> (i+1+n,s,stk) |  | 
| 
 
43753eca324a
replaced relation by function - simplifies development
 
nipkow 
parents: 
47818 
diff
changeset
 | 
59  | 
JMPLESS n \<Rightarrow> (if hd2 stk < hd stk then i+1+n else i+1,s,tl2 stk) |  | 
| 
 
43753eca324a
replaced relation by function - simplifies development
 
nipkow 
parents: 
47818 
diff
changeset
 | 
60  | 
JMPGE n \<Rightarrow> (if hd2 stk >= hd stk then i+1+n else i+1,s,tl2 stk))"  | 
| 
43438
 
a666b8d11252
IMP compiler with int, added reverse soundness direction
 
kleing 
parents: 
43158 
diff
changeset
 | 
61  | 
|
| 
44000
 
ab4d8499815c
resolved code_pred FIXME in IMP; clearer notation for exec_n
 
kleing 
parents: 
43438 
diff
changeset
 | 
62  | 
definition  | 
| 
50060
 
43753eca324a
replaced relation by function - simplifies development
 
nipkow 
parents: 
47818 
diff
changeset
 | 
63  | 
exec1 :: "instr list \<Rightarrow> config \<Rightarrow> config \<Rightarrow> bool"  | 
| 
 
43753eca324a
replaced relation by function - simplifies development
 
nipkow 
parents: 
47818 
diff
changeset
 | 
64  | 
     ("(_/ \<turnstile> (_ \<rightarrow>/ _))" [59,0,59] 60) 
 | 
| 
44000
 
ab4d8499815c
resolved code_pred FIXME in IMP; clearer notation for exec_n
 
kleing 
parents: 
43438 
diff
changeset
 | 
65  | 
where  | 
| 
43438
 
a666b8d11252
IMP compiler with int, added reverse soundness direction
 
kleing 
parents: 
43158 
diff
changeset
 | 
66  | 
"P \<turnstile> c \<rightarrow> c' =  | 
| 
51259
 
1491459df114
eliminated isize in favour of size + type coercion
 
kleing 
parents: 
50133 
diff
changeset
 | 
67  | 
(\<exists>i s stk. c = (i,s,stk) \<and> c' = iexec(P!!i) (i,s,stk) \<and> 0 \<le> i \<and> i < size P)"  | 
| 
44000
 
ab4d8499815c
resolved code_pred FIXME in IMP; clearer notation for exec_n
 
kleing 
parents: 
43438 
diff
changeset
 | 
68  | 
|
| 
50060
 
43753eca324a
replaced relation by function - simplifies development
 
nipkow 
parents: 
47818 
diff
changeset
 | 
69  | 
declare exec1_def [simp]  | 
| 
44000
 
ab4d8499815c
resolved code_pred FIXME in IMP; clearer notation for exec_n
 
kleing 
parents: 
43438 
diff
changeset
 | 
70  | 
|
| 
 
ab4d8499815c
resolved code_pred FIXME in IMP; clearer notation for exec_n
 
kleing 
parents: 
43438 
diff
changeset
 | 
71  | 
lemma exec1I [intro, code_pred_intro]:  | 
| 
51259
 
1491459df114
eliminated isize in favour of size + type coercion
 
kleing 
parents: 
50133 
diff
changeset
 | 
72  | 
"c' = iexec (P!!i) (i,s,stk) \<Longrightarrow> 0 \<le> i \<Longrightarrow> i < size P  | 
| 
50060
 
43753eca324a
replaced relation by function - simplifies development
 
nipkow 
parents: 
47818 
diff
changeset
 | 
73  | 
\<Longrightarrow> P \<turnstile> (i,s,stk) \<rightarrow> c'"  | 
| 
 
43753eca324a
replaced relation by function - simplifies development
 
nipkow 
parents: 
47818 
diff
changeset
 | 
74  | 
by simp  | 
| 43141 | 75  | 
|
| 
50060
 
43753eca324a
replaced relation by function - simplifies development
 
nipkow 
parents: 
47818 
diff
changeset
 | 
76  | 
inductive exec :: "instr list \<Rightarrow> config \<Rightarrow> config \<Rightarrow> bool"  | 
| 
 
43753eca324a
replaced relation by function - simplifies development
 
nipkow 
parents: 
47818 
diff
changeset
 | 
77  | 
   ("(_/ \<turnstile> (_ \<rightarrow>*/ _))" 50)
 | 
| 43141 | 78  | 
where  | 
79  | 
refl: "P \<turnstile> c \<rightarrow>* c" |  | 
|
80  | 
step: "P \<turnstile> c \<rightarrow> c' \<Longrightarrow> P \<turnstile> c' \<rightarrow>* c'' \<Longrightarrow> P \<turnstile> c \<rightarrow>* c''"  | 
|
81  | 
||
| 
43438
 
a666b8d11252
IMP compiler with int, added reverse soundness direction
 
kleing 
parents: 
43158 
diff
changeset
 | 
82  | 
declare refl[intro] step[intro]  | 
| 43141 | 83  | 
|
84  | 
lemmas exec_induct = exec.induct[split_format(complete)]  | 
|
85  | 
||
| 50133 | 86  | 
code_pred exec by fastforce  | 
| 43141 | 87  | 
|
88  | 
values  | 
|
89  | 
  "{(i,map t [''x'',''y''],stk) | i t stk.
 | 
|
90  | 
[LOAD ''y'', STORE ''x''] \<turnstile>  | 
|
| 44036 | 91  | 
(0, <''x'' := 3, ''y'' := 4>, []) \<rightarrow>* (i,t,stk)}"  | 
| 43141 | 92  | 
|
93  | 
||
94  | 
subsection{* Verification infrastructure *}
 | 
|
95  | 
||
96  | 
lemma exec_trans: "P \<turnstile> c \<rightarrow>* c' \<Longrightarrow> P \<turnstile> c' \<rightarrow>* c'' \<Longrightarrow> P \<turnstile> c \<rightarrow>* c''"  | 
|
| 
50060
 
43753eca324a
replaced relation by function - simplifies development
 
nipkow 
parents: 
47818 
diff
changeset
 | 
97  | 
by (induction rule: exec.induct) fastforce+  | 
| 43141 | 98  | 
|
99  | 
text{* Below we need to argue about the execution of code that is embedded in
 | 
|
100  | 
larger programs. For this purpose we show that execution is preserved by  | 
|
101  | 
appending code to the left or right of a program. *}  | 
|
102  | 
||
| 
50060
 
43753eca324a
replaced relation by function - simplifies development
 
nipkow 
parents: 
47818 
diff
changeset
 | 
103  | 
lemma iexec_shift [simp]:  | 
| 
 
43753eca324a
replaced relation by function - simplifies development
 
nipkow 
parents: 
47818 
diff
changeset
 | 
104  | 
"((n+i',s',stk') = iexec x (n+i,s,stk)) = ((i',s',stk') = iexec x (i,s,stk))"  | 
| 
 
43753eca324a
replaced relation by function - simplifies development
 
nipkow 
parents: 
47818 
diff
changeset
 | 
105  | 
by(auto split:instr.split)  | 
| 
 
43753eca324a
replaced relation by function - simplifies development
 
nipkow 
parents: 
47818 
diff
changeset
 | 
106  | 
|
| 
43438
 
a666b8d11252
IMP compiler with int, added reverse soundness direction
 
kleing 
parents: 
43158 
diff
changeset
 | 
107  | 
lemma exec1_appendR: "P \<turnstile> c \<rightarrow> c' \<Longrightarrow> P@P' \<turnstile> c \<rightarrow> c'"  | 
| 
50060
 
43753eca324a
replaced relation by function - simplifies development
 
nipkow 
parents: 
47818 
diff
changeset
 | 
108  | 
by auto  | 
| 11275 | 109  | 
|
| 43141 | 110  | 
lemma exec_appendR: "P \<turnstile> c \<rightarrow>* c' \<Longrightarrow> P@P' \<turnstile> c \<rightarrow>* c'"  | 
| 
50060
 
43753eca324a
replaced relation by function - simplifies development
 
nipkow 
parents: 
47818 
diff
changeset
 | 
111  | 
by (induction rule: exec.induct) (fastforce intro: exec1_appendR)+  | 
| 
13095
 
8ed413a57bdc
New machine architecture and other direction of compiler proof.
 
nipkow 
parents: 
12566 
diff
changeset
 | 
112  | 
|
| 43141 | 113  | 
lemma exec1_appendL:  | 
| 
51259
 
1491459df114
eliminated isize in favour of size + type coercion
 
kleing 
parents: 
50133 
diff
changeset
 | 
114  | 
fixes i i' :: int  | 
| 
 
1491459df114
eliminated isize in favour of size + type coercion
 
kleing 
parents: 
50133 
diff
changeset
 | 
115  | 
shows  | 
| 
43438
 
a666b8d11252
IMP compiler with int, added reverse soundness direction
 
kleing 
parents: 
43158 
diff
changeset
 | 
116  | 
"P \<turnstile> (i,s,stk) \<rightarrow> (i',s',stk') \<Longrightarrow>  | 
| 
51259
 
1491459df114
eliminated isize in favour of size + type coercion
 
kleing 
parents: 
50133 
diff
changeset
 | 
117  | 
P' @ P \<turnstile> (size(P')+i,s,stk) \<rightarrow> (size(P')+i',s',stk')"  | 
| 
 
1491459df114
eliminated isize in favour of size + type coercion
 
kleing 
parents: 
50133 
diff
changeset
 | 
118  | 
by (auto split: instr.split)  | 
| 
13095
 
8ed413a57bdc
New machine architecture and other direction of compiler proof.
 
nipkow 
parents: 
12566 
diff
changeset
 | 
119  | 
|
| 43141 | 120  | 
lemma exec_appendL:  | 
| 
51259
 
1491459df114
eliminated isize in favour of size + type coercion
 
kleing 
parents: 
50133 
diff
changeset
 | 
121  | 
fixes i i' :: int  | 
| 
 
1491459df114
eliminated isize in favour of size + type coercion
 
kleing 
parents: 
50133 
diff
changeset
 | 
122  | 
shows  | 
| 43141 | 123  | 
"P \<turnstile> (i,s,stk) \<rightarrow>* (i',s',stk') \<Longrightarrow>  | 
| 
51259
 
1491459df114
eliminated isize in favour of size + type coercion
 
kleing 
parents: 
50133 
diff
changeset
 | 
124  | 
P' @ P \<turnstile> (size(P')+i,s,stk) \<rightarrow>* (size(P')+i',s',stk')"  | 
| 
 
1491459df114
eliminated isize in favour of size + type coercion
 
kleing 
parents: 
50133 
diff
changeset
 | 
125  | 
by (induction rule: exec_induct) (blast intro!: exec1_appendL)+  | 
| 
13095
 
8ed413a57bdc
New machine architecture and other direction of compiler proof.
 
nipkow 
parents: 
12566 
diff
changeset
 | 
126  | 
|
| 43141 | 127  | 
text{* Now we specialise the above lemmas to enable automatic proofs of
 | 
128  | 
@{prop "P \<turnstile> c \<rightarrow>* c'"} where @{text P} is a mixture of concrete instructions and
 | 
|
129  | 
pieces of code that we already know how they execute (by induction), combined  | 
|
130  | 
by @{text "@"} and @{text "#"}. Backward jumps are not supported.
 | 
|
131  | 
The details should be skipped on a first reading.  | 
|
| 
13095
 
8ed413a57bdc
New machine architecture and other direction of compiler proof.
 
nipkow 
parents: 
12566 
diff
changeset
 | 
132  | 
|
| 
43438
 
a666b8d11252
IMP compiler with int, added reverse soundness direction
 
kleing 
parents: 
43158 
diff
changeset
 | 
133  | 
If we have just executed the first instruction of the program, drop it: *}  | 
| 43141 | 134  | 
|
| 
43438
 
a666b8d11252
IMP compiler with int, added reverse soundness direction
 
kleing 
parents: 
43158 
diff
changeset
 | 
135  | 
lemma exec_Cons_1 [intro]:  | 
| 
 
a666b8d11252
IMP compiler with int, added reverse soundness direction
 
kleing 
parents: 
43158 
diff
changeset
 | 
136  | 
"P \<turnstile> (0,s,stk) \<rightarrow>* (j,t,stk') \<Longrightarrow>  | 
| 
 
a666b8d11252
IMP compiler with int, added reverse soundness direction
 
kleing 
parents: 
43158 
diff
changeset
 | 
137  | 
instr#P \<turnstile> (1,s,stk) \<rightarrow>* (1+j,t,stk')"  | 
| 
50060
 
43753eca324a
replaced relation by function - simplifies development
 
nipkow 
parents: 
47818 
diff
changeset
 | 
138  | 
by (drule exec_appendL[where P'="[instr]"]) simp  | 
| 
13095
 
8ed413a57bdc
New machine architecture and other direction of compiler proof.
 
nipkow 
parents: 
12566 
diff
changeset
 | 
139  | 
|
| 43141 | 140  | 
lemma exec_appendL_if[intro]:  | 
| 
51259
 
1491459df114
eliminated isize in favour of size + type coercion
 
kleing 
parents: 
50133 
diff
changeset
 | 
141  | 
fixes i i' :: int  | 
| 
 
1491459df114
eliminated isize in favour of size + type coercion
 
kleing 
parents: 
50133 
diff
changeset
 | 
142  | 
shows  | 
| 
 
1491459df114
eliminated isize in favour of size + type coercion
 
kleing 
parents: 
50133 
diff
changeset
 | 
143  | 
"size P' <= i  | 
| 
 
1491459df114
eliminated isize in favour of size + type coercion
 
kleing 
parents: 
50133 
diff
changeset
 | 
144  | 
\<Longrightarrow> P \<turnstile> (i - size P',s,stk) \<rightarrow>* (i',s',stk')  | 
| 
 
1491459df114
eliminated isize in favour of size + type coercion
 
kleing 
parents: 
50133 
diff
changeset
 | 
145  | 
\<Longrightarrow> P' @ P \<turnstile> (i,s,stk) \<rightarrow>* (size P' + i',s',stk')"  | 
| 
50060
 
43753eca324a
replaced relation by function - simplifies development
 
nipkow 
parents: 
47818 
diff
changeset
 | 
146  | 
by (drule exec_appendL[where P'=P']) simp  | 
| 10342 | 147  | 
|
| 43141 | 148  | 
text{* Split the execution of a compound program up into the excution of its
 | 
149  | 
parts: *}  | 
|
| 
13095
 
8ed413a57bdc
New machine architecture and other direction of compiler proof.
 
nipkow 
parents: 
12566 
diff
changeset
 | 
150  | 
|
| 43141 | 151  | 
lemma exec_append_trans[intro]:  | 
| 
51259
 
1491459df114
eliminated isize in favour of size + type coercion
 
kleing 
parents: 
50133 
diff
changeset
 | 
152  | 
fixes i' i'' j'' :: int  | 
| 
 
1491459df114
eliminated isize in favour of size + type coercion
 
kleing 
parents: 
50133 
diff
changeset
 | 
153  | 
shows  | 
| 43141 | 154  | 
"P \<turnstile> (0,s,stk) \<rightarrow>* (i',s',stk') \<Longrightarrow>  | 
| 
51259
 
1491459df114
eliminated isize in favour of size + type coercion
 
kleing 
parents: 
50133 
diff
changeset
 | 
155  | 
size P \<le> i' \<Longrightarrow>  | 
| 
 
1491459df114
eliminated isize in favour of size + type coercion
 
kleing 
parents: 
50133 
diff
changeset
 | 
156  | 
P' \<turnstile> (i' - size P,s',stk') \<rightarrow>* (i'',s'',stk'') \<Longrightarrow>  | 
| 
 
1491459df114
eliminated isize in favour of size + type coercion
 
kleing 
parents: 
50133 
diff
changeset
 | 
157  | 
j'' = size P + i''  | 
| 43141 | 158  | 
\<Longrightarrow>  | 
159  | 
P @ P' \<turnstile> (0,s,stk) \<rightarrow>* (j'',s'',stk'')"  | 
|
| 
50060
 
43753eca324a
replaced relation by function - simplifies development
 
nipkow 
parents: 
47818 
diff
changeset
 | 
160  | 
by(metis exec_trans[OF exec_appendR exec_appendL_if])  | 
| 
13095
 
8ed413a57bdc
New machine architecture and other direction of compiler proof.
 
nipkow 
parents: 
12566 
diff
changeset
 | 
161  | 
|
| 43141 | 162  | 
|
| 
50060
 
43753eca324a
replaced relation by function - simplifies development
 
nipkow 
parents: 
47818 
diff
changeset
 | 
163  | 
declare Let_def[simp]  | 
| 
13095
 
8ed413a57bdc
New machine architecture and other direction of compiler proof.
 
nipkow 
parents: 
12566 
diff
changeset
 | 
164  | 
|
| 
 
8ed413a57bdc
New machine architecture and other direction of compiler proof.
 
nipkow 
parents: 
12566 
diff
changeset
 | 
165  | 
|
| 43141 | 166  | 
subsection "Compilation"  | 
| 
13095
 
8ed413a57bdc
New machine architecture and other direction of compiler proof.
 
nipkow 
parents: 
12566 
diff
changeset
 | 
167  | 
|
| 43141 | 168  | 
fun acomp :: "aexp \<Rightarrow> instr list" where  | 
169  | 
"acomp (N n) = [LOADI n]" |  | 
|
170  | 
"acomp (V x) = [LOAD x]" |  | 
|
171  | 
"acomp (Plus a1 a2) = acomp a1 @ acomp a2 @ [ADD]"  | 
|
172  | 
||
173  | 
lemma acomp_correct[intro]:  | 
|
| 
51259
 
1491459df114
eliminated isize in favour of size + type coercion
 
kleing 
parents: 
50133 
diff
changeset
 | 
174  | 
"acomp a \<turnstile> (0,s,stk) \<rightarrow>* (size(acomp a),s,aval a s#stk)"  | 
| 
50060
 
43753eca324a
replaced relation by function - simplifies development
 
nipkow 
parents: 
47818 
diff
changeset
 | 
175  | 
by (induction a arbitrary: stk) fastforce+  | 
| 10342 | 176  | 
|
| 
43438
 
a666b8d11252
IMP compiler with int, added reverse soundness direction
 
kleing 
parents: 
43158 
diff
changeset
 | 
177  | 
fun bcomp :: "bexp \<Rightarrow> bool \<Rightarrow> int \<Rightarrow> instr list" where  | 
| 45200 | 178  | 
"bcomp (Bc v) c n = (if v=c then [JMP n] else [])" |  | 
| 43141 | 179  | 
"bcomp (Not b) c n = bcomp b (\<not>c) n" |  | 
180  | 
"bcomp (And b1 b2) c n =  | 
|
181  | 
(let cb2 = bcomp b2 c n;  | 
|
| 
51259
 
1491459df114
eliminated isize in favour of size + type coercion
 
kleing 
parents: 
50133 
diff
changeset
 | 
182  | 
m = (if c then size cb2 else (size cb2::int)+n);  | 
| 43141 | 183  | 
cb1 = bcomp b1 False m  | 
184  | 
in cb1 @ cb2)" |  | 
|
185  | 
"bcomp (Less a1 a2) c n =  | 
|
| 
45322
 
654cc47f6115
JMPF(LESS|GE) -> JMP(LESS|GE) because jumps are int now.
 
kleing 
parents: 
45200 
diff
changeset
 | 
186  | 
acomp a1 @ acomp a2 @ (if c then [JMPLESS n] else [JMPGE n])"  | 
| 43141 | 187  | 
|
188  | 
value  | 
|
189  | 
"bcomp (And (Less (V ''x'') (V ''y'')) (Not(Less (V ''u'') (V ''v''))))  | 
|
190  | 
False 3"  | 
|
191  | 
||
192  | 
lemma bcomp_correct[intro]:  | 
|
| 
51259
 
1491459df114
eliminated isize in favour of size + type coercion
 
kleing 
parents: 
50133 
diff
changeset
 | 
193  | 
fixes n :: int  | 
| 
 
1491459df114
eliminated isize in favour of size + type coercion
 
kleing 
parents: 
50133 
diff
changeset
 | 
194  | 
shows  | 
| 
43438
 
a666b8d11252
IMP compiler with int, added reverse soundness direction
 
kleing 
parents: 
43158 
diff
changeset
 | 
195  | 
"0 \<le> n \<Longrightarrow>  | 
| 
 
a666b8d11252
IMP compiler with int, added reverse soundness direction
 
kleing 
parents: 
43158 
diff
changeset
 | 
196  | 
bcomp b c n \<turnstile>  | 
| 
51259
 
1491459df114
eliminated isize in favour of size + type coercion
 
kleing 
parents: 
50133 
diff
changeset
 | 
197  | 
(0,s,stk) \<rightarrow>* (size(bcomp b c n) + (if c = bval b s then n else 0),s,stk)"  | 
| 
45129
 
1fce03e3e8ad
tuned proofs -- eliminated vacuous "induct arbitrary: ..." situations;
 
wenzelm 
parents: 
45114 
diff
changeset
 | 
198  | 
proof(induction b arbitrary: c n)  | 
| 43141 | 199  | 
case Not  | 
| 
44890
 
22f665a2e91c
new fastforce replacing fastsimp - less confusing name
 
nipkow 
parents: 
44036 
diff
changeset
 | 
200  | 
from Not(1)[where c="~c"] Not(2) show ?case by fastforce  | 
| 
13095
 
8ed413a57bdc
New machine architecture and other direction of compiler proof.
 
nipkow 
parents: 
12566 
diff
changeset
 | 
201  | 
next  | 
| 43141 | 202  | 
case (And b1 b2)  | 
| 
51259
 
1491459df114
eliminated isize in favour of size + type coercion
 
kleing 
parents: 
50133 
diff
changeset
 | 
203  | 
from And(1)[of "if c then size(bcomp b2 c n) else size(bcomp b2 c n) + n"  | 
| 
43438
 
a666b8d11252
IMP compiler with int, added reverse soundness direction
 
kleing 
parents: 
43158 
diff
changeset
 | 
204  | 
"False"]  | 
| 
 
a666b8d11252
IMP compiler with int, added reverse soundness direction
 
kleing 
parents: 
43158 
diff
changeset
 | 
205  | 
And(2)[of n "c"] And(3)  | 
| 
44890
 
22f665a2e91c
new fastforce replacing fastsimp - less confusing name
 
nipkow 
parents: 
44036 
diff
changeset
 | 
206  | 
show ?case by fastforce  | 
| 
 
22f665a2e91c
new fastforce replacing fastsimp - less confusing name
 
nipkow 
parents: 
44036 
diff
changeset
 | 
207  | 
qed fastforce+  | 
| 43141 | 208  | 
|
209  | 
fun ccomp :: "com \<Rightarrow> instr list" where  | 
|
210  | 
"ccomp SKIP = []" |  | 
|
211  | 
"ccomp (x ::= a) = acomp a @ [STORE x]" |  | 
|
212  | 
"ccomp (c\<^isub>1;c\<^isub>2) = ccomp c\<^isub>1 @ ccomp c\<^isub>2" |  | 
|
213  | 
"ccomp (IF b THEN c\<^isub>1 ELSE c\<^isub>2) =  | 
|
| 
51259
 
1491459df114
eliminated isize in favour of size + type coercion
 
kleing 
parents: 
50133 
diff
changeset
 | 
214  | 
(let cc\<^isub>1 = ccomp c\<^isub>1; cc\<^isub>2 = ccomp c\<^isub>2; cb = bcomp b False (size cc\<^isub>1 + 1)  | 
| 
 
1491459df114
eliminated isize in favour of size + type coercion
 
kleing 
parents: 
50133 
diff
changeset
 | 
215  | 
in cb @ cc\<^isub>1 @ JMP (size cc\<^isub>2) # cc\<^isub>2)" |  | 
| 43141 | 216  | 
"ccomp (WHILE b DO c) =  | 
| 
51259
 
1491459df114
eliminated isize in favour of size + type coercion
 
kleing 
parents: 
50133 
diff
changeset
 | 
217  | 
(let cc = ccomp c; cb = bcomp b False (size cc + 1)  | 
| 
 
1491459df114
eliminated isize in favour of size + type coercion
 
kleing 
parents: 
50133 
diff
changeset
 | 
218  | 
in cb @ cc @ [JMP (-(size cb + size cc + 1))])"  | 
| 44004 | 219  | 
|
| 43141 | 220  | 
|
221  | 
value "ccomp  | 
|
222  | 
(IF Less (V ''u'') (N 1) THEN ''u'' ::= Plus (V ''u'') (N 1)  | 
|
223  | 
ELSE ''v'' ::= V ''u'')"  | 
|
224  | 
||
225  | 
value "ccomp (WHILE Less (V ''u'') (N 1) DO (''u'' ::= Plus (V ''u'') (N 1)))"
 | 
|
226  | 
||
227  | 
||
| 45114 | 228  | 
subsection "Preservation of semantics"  | 
| 43141 | 229  | 
|
| 
43438
 
a666b8d11252
IMP compiler with int, added reverse soundness direction
 
kleing 
parents: 
43158 
diff
changeset
 | 
230  | 
lemma ccomp_bigstep:  | 
| 
51259
 
1491459df114
eliminated isize in favour of size + type coercion
 
kleing 
parents: 
50133 
diff
changeset
 | 
231  | 
"(c,s) \<Rightarrow> t \<Longrightarrow> ccomp c \<turnstile> (0,s,stk) \<rightarrow>* (size(ccomp c),t,stk)"  | 
| 45015 | 232  | 
proof(induction arbitrary: stk rule: big_step_induct)  | 
| 43141 | 233  | 
case (Assign x a s)  | 
| 
44890
 
22f665a2e91c
new fastforce replacing fastsimp - less confusing name
 
nipkow 
parents: 
44036 
diff
changeset
 | 
234  | 
show ?case by (fastforce simp:fun_upd_def cong: if_cong)  | 
| 
13095
 
8ed413a57bdc
New machine architecture and other direction of compiler proof.
 
nipkow 
parents: 
12566 
diff
changeset
 | 
235  | 
next  | 
| 47818 | 236  | 
case (Seq c1 s1 s2 c2 s3)  | 
| 43141 | 237  | 
let ?cc1 = "ccomp c1" let ?cc2 = "ccomp c2"  | 
| 
51259
 
1491459df114
eliminated isize in favour of size + type coercion
 
kleing 
parents: 
50133 
diff
changeset
 | 
238  | 
have "?cc1 @ ?cc2 \<turnstile> (0,s1,stk) \<rightarrow>* (size ?cc1,s2,stk)"  | 
| 47818 | 239  | 
using Seq.IH(1) by fastforce  | 
| 43141 | 240  | 
moreover  | 
| 
51259
 
1491459df114
eliminated isize in favour of size + type coercion
 
kleing 
parents: 
50133 
diff
changeset
 | 
241  | 
have "?cc1 @ ?cc2 \<turnstile> (size ?cc1,s2,stk) \<rightarrow>* (size(?cc1 @ ?cc2),s3,stk)"  | 
| 47818 | 242  | 
using Seq.IH(2) by fastforce  | 
| 43141 | 243  | 
ultimately show ?case by simp (blast intro: exec_trans)  | 
244  | 
next  | 
|
245  | 
case (WhileTrue b s1 c s2 s3)  | 
|
246  | 
let ?cc = "ccomp c"  | 
|
| 
51259
 
1491459df114
eliminated isize in favour of size + type coercion
 
kleing 
parents: 
50133 
diff
changeset
 | 
247  | 
let ?cb = "bcomp b False (size ?cc + 1)"  | 
| 43141 | 248  | 
let ?cw = "ccomp(WHILE b DO c)"  | 
| 
51259
 
1491459df114
eliminated isize in favour of size + type coercion
 
kleing 
parents: 
50133 
diff
changeset
 | 
249  | 
have "?cw \<turnstile> (0,s1,stk) \<rightarrow>* (size ?cb,s1,stk)"  | 
| 50133 | 250  | 
using `bval b s1` by fastforce  | 
251  | 
moreover  | 
|
| 
51259
 
1491459df114
eliminated isize in favour of size + type coercion
 
kleing 
parents: 
50133 
diff
changeset
 | 
252  | 
have "?cw \<turnstile> (size ?cb,s1,stk) \<rightarrow>* (size ?cb + size ?cc,s2,stk)"  | 
| 50133 | 253  | 
using WhileTrue.IH(1) by fastforce  | 
| 43141 | 254  | 
moreover  | 
| 
51259
 
1491459df114
eliminated isize in favour of size + type coercion
 
kleing 
parents: 
50133 
diff
changeset
 | 
255  | 
have "?cw \<turnstile> (size ?cb + size ?cc,s2,stk) \<rightarrow>* (0,s2,stk)"  | 
| 
44890
 
22f665a2e91c
new fastforce replacing fastsimp - less confusing name
 
nipkow 
parents: 
44036 
diff
changeset
 | 
256  | 
by fastforce  | 
| 43141 | 257  | 
moreover  | 
| 
51259
 
1491459df114
eliminated isize in favour of size + type coercion
 
kleing 
parents: 
50133 
diff
changeset
 | 
258  | 
have "?cw \<turnstile> (0,s2,stk) \<rightarrow>* (size ?cw,s3,stk)" by(rule WhileTrue.IH(2))  | 
| 43141 | 259  | 
ultimately show ?case by(blast intro: exec_trans)  | 
| 
44890
 
22f665a2e91c
new fastforce replacing fastsimp - less confusing name
 
nipkow 
parents: 
44036 
diff
changeset
 | 
260  | 
qed fastforce+  | 
| 
13095
 
8ed413a57bdc
New machine architecture and other direction of compiler proof.
 
nipkow 
parents: 
12566 
diff
changeset
 | 
261  | 
|
| 
20217
 
25b068a99d2b
linear arithmetic splits certain operators (e.g. min, max, abs)
 
webertj 
parents: 
16417 
diff
changeset
 | 
262  | 
end  |