23503
|
1 |
(* ID: $Id$
|
|
2 |
Author: Klaus Aehlig, Tobias Nipkow
|
|
3 |
Work in progress
|
|
4 |
*)
|
|
5 |
|
23854
|
6 |
theory NBE imports Main Executable_Set begin
|
23503
|
7 |
|
|
8 |
ML"set quick_and_dirty"
|
|
9 |
|
|
10 |
declare Let_def[simp]
|
|
11 |
|
|
12 |
consts_code undefined ("(raise Match)")
|
|
13 |
|
|
14 |
(*typedecl const_name*)
|
|
15 |
types lam_var_name = nat
|
|
16 |
ml_var_name = nat
|
|
17 |
const_name = nat
|
|
18 |
|
|
19 |
datatype tm = Ct const_name | Vt lam_var_name | Lam tm | At tm tm
|
|
20 |
| term_of ml (* function 'to_term' *)
|
|
21 |
and ml = (* rep of universal datatype *)
|
|
22 |
C const_name "ml list" | V lam_var_name "ml list"
|
|
23 |
| Fun ml "ml list" nat
|
|
24 |
| "apply" ml ml (* function 'apply' *)
|
|
25 |
(* ML *)
|
|
26 |
| V_ML ml_var_name | A_ML ml "ml list" | Lam_ML ml
|
|
27 |
| CC const_name (* ref to compiled code *)
|
|
28 |
|
|
29 |
lemma [simp]: "x \<in> set vs \<Longrightarrow> size x < Suc (ml_list_size1 vs)"
|
|
30 |
by (induct vs) auto
|
|
31 |
lemma [simp]: "x \<in> set vs \<Longrightarrow> size x < Suc (ml_list_size2 vs)"
|
|
32 |
by (induct vs) auto
|
|
33 |
lemma [simp]:"x \<in> set vs \<Longrightarrow> size x < Suc (size v + ml_list_size3 vs)"
|
|
34 |
by (induct vs) auto
|
|
35 |
lemma [simp]: "x \<in> set vs \<Longrightarrow> size x < Suc (size v + ml_list_size4 vs)"
|
|
36 |
by (induct vs) auto
|
|
37 |
|
|
38 |
locale Vars =
|
|
39 |
fixes r s t:: tm
|
|
40 |
and rs ss ts :: "tm list"
|
|
41 |
and u v w :: ml
|
|
42 |
and us vs ws :: "ml list"
|
|
43 |
and nm :: const_name
|
|
44 |
and x :: lam_var_name
|
|
45 |
and X :: ml_var_name
|
|
46 |
|
23778
|
47 |
inductive_set Pure_tms :: "tm set"
|
|
48 |
where
|
|
49 |
"Ct s : Pure_tms"
|
|
50 |
| "Vt x : Pure_tms"
|
|
51 |
| "t : Pure_tms ==> Lam t : Pure_tms"
|
|
52 |
| "s : Pure_tms ==> t : Pure_tms ==> At s t : Pure_tms"
|
23503
|
53 |
|
|
54 |
consts
|
|
55 |
R :: "(const_name * tm list * tm)set" (* reduction rules *)
|
|
56 |
compR :: "(const_name * ml list * ml)set" (* compiled reduction rules *)
|
|
57 |
|
|
58 |
fun
|
|
59 |
lift_tm :: "nat \<Rightarrow> tm \<Rightarrow> tm" ("lift") and
|
|
60 |
lift_ml :: "nat \<Rightarrow> ml \<Rightarrow> ml" ("lift")
|
|
61 |
where
|
|
62 |
"lift i (Ct nm) = Ct nm" |
|
|
63 |
"lift i (Vt x) = Vt(if x < i then x else x+1)" |
|
|
64 |
"lift i (Lam t) = Lam (lift (i+1) t)" |
|
|
65 |
"lift i (At s t) = At (lift i s) (lift i t)" |
|
|
66 |
"lift i (term_of v) = term_of (lift i v)" |
|
|
67 |
|
|
68 |
"lift i (C nm vs) = C nm (map (lift i) vs)" |
|
|
69 |
"lift i (V x vs) = V (if x < i then x else x+1) (map (lift i) vs)" |
|
|
70 |
"lift i (Fun v vs n) = Fun (lift i v) (map (lift i) vs) n" |
|
|
71 |
"lift i (apply u v) = apply (lift i u) (lift i v)" |
|
|
72 |
"lift i (V_ML X) = V_ML X" |
|
|
73 |
"lift i (A_ML v vs) = A_ML (lift i v) (map (lift i) vs)" |
|
|
74 |
"lift i (Lam_ML v) = Lam_ML (lift i v)" |
|
|
75 |
"lift i (CC nm) = CC nm"
|
|
76 |
(*
|
|
77 |
termination
|
|
78 |
apply (relation "measure (sum_case (%(i,t). size t) (%(i,v). size v))")
|
|
79 |
apply auto
|
|
80 |
*)
|
|
81 |
|
|
82 |
fun
|
|
83 |
lift_tm_ML :: "nat \<Rightarrow> tm \<Rightarrow> tm" ("lift\<^bsub>ML\<^esub>") and
|
|
84 |
lift_ml_ML :: "nat \<Rightarrow> ml \<Rightarrow> ml" ("lift\<^bsub>ML\<^esub>")
|
|
85 |
where
|
|
86 |
"lift\<^bsub>ML\<^esub> i (Ct nm) = Ct nm" |
|
|
87 |
"lift\<^bsub>ML\<^esub> i (Vt x) = Vt x" |
|
|
88 |
"lift\<^bsub>ML\<^esub> i (Lam t) = Lam (lift\<^bsub>ML\<^esub> i t)" |
|
|
89 |
"lift\<^bsub>ML\<^esub> i (At s t) = At (lift\<^bsub>ML\<^esub> i s) (lift\<^bsub>ML\<^esub> i t)" |
|
|
90 |
"lift\<^bsub>ML\<^esub> i (term_of v) = term_of (lift\<^bsub>ML\<^esub> i v)" |
|
|
91 |
|
|
92 |
"lift\<^bsub>ML\<^esub> i (C nm vs) = C nm (map (lift\<^bsub>ML\<^esub> i) vs)" |
|
|
93 |
"lift\<^bsub>ML\<^esub> i (V x vs) = V x (map (lift\<^bsub>ML\<^esub> i) vs)" |
|
|
94 |
"lift\<^bsub>ML\<^esub> i (Fun v vs n) = Fun (lift\<^bsub>ML\<^esub> i v) (map (lift\<^bsub>ML\<^esub> i) vs) n" |
|
|
95 |
"lift\<^bsub>ML\<^esub> i (apply u v) = apply (lift\<^bsub>ML\<^esub> i u) (lift\<^bsub>ML\<^esub> i v)" |
|
|
96 |
"lift\<^bsub>ML\<^esub> i (V_ML X) = V_ML (if X < i then X else X+1)" |
|
|
97 |
"lift\<^bsub>ML\<^esub> i (A_ML v vs) = A_ML (lift\<^bsub>ML\<^esub> i v) (map (lift\<^bsub>ML\<^esub> i) vs)" |
|
|
98 |
"lift\<^bsub>ML\<^esub> i (Lam_ML v) = Lam_ML (lift\<^bsub>ML\<^esub> (i+1) v)" |
|
|
99 |
"lift\<^bsub>ML\<^esub> i (CC nm) = CC nm"
|
|
100 |
(*
|
|
101 |
termination
|
|
102 |
by (relation "measure (sum_case (%(i,t). size t) (%(i,v). size v))") auto
|
|
103 |
*)
|
|
104 |
constdefs
|
|
105 |
cons :: "tm \<Rightarrow> (nat \<Rightarrow> tm) \<Rightarrow> (nat \<Rightarrow> tm)" (infix "##" 65)
|
|
106 |
"t##f \<equiv> \<lambda>i. case i of 0 \<Rightarrow> t | Suc j \<Rightarrow> lift 0 (f j)"
|
|
107 |
cons_ML :: "ml \<Rightarrow> (nat \<Rightarrow> ml) \<Rightarrow> (nat \<Rightarrow> ml)" (infix "##" 65)
|
|
108 |
"v##f \<equiv> \<lambda>i. case i of 0 \<Rightarrow> v::ml | Suc j \<Rightarrow> lift\<^bsub>ML\<^esub> 0 (f j)"
|
|
109 |
|
|
110 |
(* Only for pure terms! *)
|
|
111 |
consts subst :: "(nat \<Rightarrow> tm) \<Rightarrow> tm \<Rightarrow> tm"
|
|
112 |
primrec
|
|
113 |
"subst f (Ct nm) = Ct nm"
|
|
114 |
"subst f (Vt x) = f x"
|
|
115 |
"subst f (Lam t) = Lam (subst (Vt 0 ## f) t)"
|
|
116 |
"subst f (At s t) = At (subst f s) (subst f t)"
|
|
117 |
|
|
118 |
lemma size_lift[simp]: shows
|
|
119 |
"size(lift i t) = size(t::tm)" and "size(lift i (v::ml)) = size v"
|
|
120 |
and "ml_list_size1 (map (lift i) vs) = ml_list_size1 vs"
|
|
121 |
and "ml_list_size2 (map (lift i) vs) = ml_list_size2 vs"
|
|
122 |
and "ml_list_size3 (map (lift i) vs) = ml_list_size3 vs"
|
|
123 |
and "ml_list_size4 (map (lift i) vs) = ml_list_size4 vs"
|
|
124 |
by (induct arbitrary: i and i and i and i and i and i rule: tm_ml.inducts)
|
|
125 |
simp_all
|
|
126 |
|
|
127 |
lemma size_lift_ML[simp]: shows
|
|
128 |
"size(lift\<^bsub>ML\<^esub> i t) = size(t::tm)" and "size(lift\<^bsub>ML\<^esub> i (v::ml)) = size v"
|
|
129 |
and "ml_list_size1 (map (lift\<^bsub>ML\<^esub> i) vs) = ml_list_size1 vs"
|
|
130 |
and "ml_list_size2 (map (lift\<^bsub>ML\<^esub> i) vs) = ml_list_size2 vs"
|
|
131 |
and "ml_list_size3 (map (lift\<^bsub>ML\<^esub> i) vs) = ml_list_size3 vs"
|
|
132 |
and "ml_list_size4 (map (lift\<^bsub>ML\<^esub> i) vs) = ml_list_size4 vs"
|
|
133 |
by (induct arbitrary: i and i and i and i and i and i rule: tm_ml.inducts)
|
|
134 |
simp_all
|
|
135 |
|
|
136 |
fun
|
|
137 |
subst_ml_ML :: "(nat \<Rightarrow> ml) \<Rightarrow> ml \<Rightarrow> ml" ("subst\<^bsub>ML\<^esub>") and
|
|
138 |
subst_tm_ML :: "(nat \<Rightarrow> ml) \<Rightarrow> tm \<Rightarrow> tm" ("subst\<^bsub>ML\<^esub>")
|
|
139 |
where
|
|
140 |
"subst\<^bsub>ML\<^esub> f (Ct nm) = Ct nm" |
|
|
141 |
"subst\<^bsub>ML\<^esub> f (Vt x) = Vt x" |
|
|
142 |
"subst\<^bsub>ML\<^esub> f (Lam t) = Lam (subst\<^bsub>ML\<^esub> (lift 0 o f) t)" |
|
|
143 |
"subst\<^bsub>ML\<^esub> f (At s t) = At (subst\<^bsub>ML\<^esub> f s) (subst\<^bsub>ML\<^esub> f t)" |
|
|
144 |
"subst\<^bsub>ML\<^esub> f (term_of v) = term_of (subst\<^bsub>ML\<^esub> f v)" |
|
|
145 |
|
|
146 |
"subst\<^bsub>ML\<^esub> f (C nm vs) = C nm (map (subst\<^bsub>ML\<^esub> f) vs)" |
|
|
147 |
"subst\<^bsub>ML\<^esub> f (V x vs) = V x (map (subst\<^bsub>ML\<^esub> f) vs)" |
|
|
148 |
"subst\<^bsub>ML\<^esub> f (Fun v vs n) = Fun (subst\<^bsub>ML\<^esub> f v) (map (subst\<^bsub>ML\<^esub> f) vs) n" |
|
|
149 |
"subst\<^bsub>ML\<^esub> f (apply u v) = apply (subst\<^bsub>ML\<^esub> f u) (subst\<^bsub>ML\<^esub> f v)" |
|
|
150 |
"subst\<^bsub>ML\<^esub> f (V_ML X) = f X" |
|
|
151 |
"subst\<^bsub>ML\<^esub> f (A_ML v vs) = A_ML (subst\<^bsub>ML\<^esub> f v) (map (subst\<^bsub>ML\<^esub> f) vs)" |
|
|
152 |
"subst\<^bsub>ML\<^esub> f (Lam_ML v) = Lam_ML (subst\<^bsub>ML\<^esub> (V_ML 0 ## f) v)" |
|
|
153 |
"subst\<^bsub>ML\<^esub> f (CC nm) = CC nm"
|
|
154 |
|
|
155 |
(* FIXME currrently needed for code generator *)
|
|
156 |
lemmas [code] = lift_tm_ML.simps lift_ml_ML.simps
|
|
157 |
lemmas [code] = lift_tm.simps lift_ml.simps
|
|
158 |
lemmas [code] = subst_tm_ML.simps subst_ml_ML.simps
|
|
159 |
|
|
160 |
abbreviation
|
|
161 |
subst_decr :: "nat \<Rightarrow> tm \<Rightarrow> nat \<Rightarrow> tm" where
|
|
162 |
"subst_decr k t == %n. if n<k then Vt n else if n=k then t else Vt(n - 1)"
|
|
163 |
abbreviation
|
|
164 |
subst_decr_ML :: "nat \<Rightarrow> ml \<Rightarrow> nat \<Rightarrow> ml" where
|
|
165 |
"subst_decr_ML k v == %n. if n<k then V_ML n else if n=k then v else V_ML(n - 1)"
|
|
166 |
abbreviation
|
|
167 |
subst1 :: "tm \<Rightarrow> tm \<Rightarrow> nat \<Rightarrow> tm" ("(_/[_'/_])" [300, 0, 0] 300) where
|
|
168 |
"s[t/k] == subst (subst_decr k t) s"
|
|
169 |
abbreviation
|
|
170 |
subst1_ML :: "ml \<Rightarrow> ml \<Rightarrow> nat \<Rightarrow> ml" ("(_/[_'/_])" [300, 0, 0] 300) where
|
|
171 |
"u[v/k] == subst\<^bsub>ML\<^esub> (subst_decr_ML k v) u"
|
|
172 |
|
|
173 |
|
|
174 |
lemma size_subst_ML[simp]: shows
|
|
175 |
"(!x. size(f x) = 0) \<longrightarrow> size(subst\<^bsub>ML\<^esub> f t) = size(t::tm)" and
|
|
176 |
"(!x. size(f x) = 0) \<longrightarrow> size(subst\<^bsub>ML\<^esub> f (v::ml)) = size v"
|
|
177 |
and "(!x. size(f x) = 0) \<longrightarrow> ml_list_size1 (map (subst\<^bsub>ML\<^esub> f) vs) = ml_list_size1 vs"
|
|
178 |
and "(!x. size(f x) = 0) \<longrightarrow> ml_list_size2 (map (subst\<^bsub>ML\<^esub> f) vs) = ml_list_size2 vs"
|
|
179 |
and "(!x. size(f x) = 0) \<longrightarrow> ml_list_size3 (map (subst\<^bsub>ML\<^esub> f) vs) = ml_list_size3 vs"
|
|
180 |
and "(!x. size(f x) = 0) \<longrightarrow> ml_list_size4 (map (subst\<^bsub>ML\<^esub> f) vs) = ml_list_size4 vs"
|
|
181 |
apply (induct arbitrary: f and f and f and f and f and f rule: tm_ml.inducts)
|
|
182 |
apply (simp_all add:cons_ML_def split:nat.split)
|
|
183 |
done
|
|
184 |
|
|
185 |
lemma lift_lift: includes Vars shows
|
|
186 |
"i < k+1 \<Longrightarrow> lift (Suc k) (lift i t) = lift i (lift k t)"
|
|
187 |
and "i < k+1 \<Longrightarrow> lift (Suc k) (lift i v) = lift i (lift k v)"
|
|
188 |
apply(induct t and v arbitrary: i and i rule:lift_tm_lift_ml.induct)
|
|
189 |
apply(simp_all add:map_compose[symmetric])
|
|
190 |
done
|
|
191 |
|
|
192 |
corollary lift_o_lift: shows
|
|
193 |
"i < k+1 \<Longrightarrow> lift_tm (Suc k) o (lift_tm i) = lift_tm i o lift_tm k" and
|
|
194 |
"i < k+1 \<Longrightarrow> lift_ml (Suc k) o (lift_ml i) = lift_ml i o lift_ml k"
|
|
195 |
by(rule ext, simp add:lift_lift)+
|
|
196 |
|
|
197 |
lemma lift_lift_ML: includes Vars shows
|
|
198 |
"i < k+1 \<Longrightarrow> lift\<^bsub>ML\<^esub> (Suc k) (lift\<^bsub>ML\<^esub> i t) = lift\<^bsub>ML\<^esub> i (lift\<^bsub>ML\<^esub> k t)"
|
|
199 |
and "i < k+1 \<Longrightarrow> lift\<^bsub>ML\<^esub> (Suc k) (lift\<^bsub>ML\<^esub> i v) = lift\<^bsub>ML\<^esub> i (lift\<^bsub>ML\<^esub> k v)"
|
|
200 |
apply(induct t and v arbitrary: i and i rule:lift_tm_ML_lift_ml_ML.induct)
|
|
201 |
apply(simp_all add:map_compose[symmetric])
|
|
202 |
done
|
|
203 |
|
|
204 |
|
|
205 |
lemma lift_lift_ML_comm: includes Vars shows
|
|
206 |
"lift j (lift\<^bsub>ML\<^esub> i t) = lift\<^bsub>ML\<^esub> i (lift j t)" and
|
|
207 |
"lift j (lift\<^bsub>ML\<^esub> i v) = lift\<^bsub>ML\<^esub> i (lift j v)"
|
|
208 |
apply(induct t and v arbitrary: i j and i j rule:lift_tm_ML_lift_ml_ML.induct)
|
|
209 |
apply(simp_all add:map_compose[symmetric])
|
|
210 |
done
|
|
211 |
|
|
212 |
lemma [simp]:
|
|
213 |
"V_ML 0 ## subst_decr_ML k v = subst_decr_ML (Suc k) (lift\<^bsub>ML\<^esub> 0 v)"
|
|
214 |
by(rule ext)(simp add:cons_ML_def split:nat.split)
|
|
215 |
|
|
216 |
lemma [simp]: "lift 0 o subst_decr_ML k v = subst_decr_ML k (lift 0 v)"
|
|
217 |
by(rule ext)(simp add:cons_ML_def split:nat.split)
|
|
218 |
|
|
219 |
lemma subst_lift_id[simp]: includes Vars shows
|
|
220 |
"subst\<^bsub>ML\<^esub> (subst_decr_ML k v) (lift\<^bsub>ML\<^esub> k t) = t" and "(lift\<^bsub>ML\<^esub> k u)[v/k] = u"
|
|
221 |
apply(induct k t and k u arbitrary: v and v rule: lift_tm_ML_lift_ml_ML.induct)
|
|
222 |
apply (simp_all add:map_idI map_compose[symmetric])
|
|
223 |
apply (simp cong:if_cong)
|
|
224 |
done
|
|
225 |
|
23778
|
226 |
inductive_set
|
|
227 |
tRed :: "(tm * tm)set" (* beta + R reduction on pure terms *)
|
|
228 |
and tred :: "[tm, tm] => bool" (infixl "\<rightarrow>" 50)
|
|
229 |
where
|
23503
|
230 |
"s \<rightarrow> t == (s, t) \<in> tRed"
|
23778
|
231 |
| "At (Lam t) s \<rightarrow> t[s/0]"
|
|
232 |
| "(nm,ts,t) : R ==> foldl At (Ct nm) (map (subst rs) ts) \<rightarrow> subst rs t"
|
|
233 |
| "t \<rightarrow> t' ==> Lam t \<rightarrow> Lam t'"
|
|
234 |
| "s \<rightarrow> s' ==> At s t \<rightarrow> At s' t"
|
|
235 |
| "t \<rightarrow> t' ==> At s t \<rightarrow> At s t'"
|
|
236 |
|
23503
|
237 |
abbreviation
|
|
238 |
treds :: "[tm, tm] => bool" (infixl "\<rightarrow>*" 50) where
|
|
239 |
"s \<rightarrow>* t == (s, t) \<in> tRed^*"
|
23778
|
240 |
|
|
241 |
inductive_set
|
|
242 |
tRed_list :: "(tm list * tm list) set"
|
|
243 |
and treds_list :: "[tm list, tm list] \<Rightarrow> bool" (infixl "\<rightarrow>*" 50)
|
|
244 |
where
|
23503
|
245 |
"ss \<rightarrow>* ts == (ss, ts) \<in> tRed_list"
|
23778
|
246 |
| "[] \<rightarrow>* []"
|
|
247 |
| "ts \<rightarrow>* ts' ==> t \<rightarrow>* t' ==> t#ts \<rightarrow>* t'#ts'"
|
23503
|
248 |
|
|
249 |
declare tRed_list.intros[simp]
|
|
250 |
|
|
251 |
lemma tRed_list_refl[simp]: includes Vars shows "ts \<rightarrow>* ts"
|
|
252 |
by(induct ts) auto
|
|
253 |
|
|
254 |
|
|
255 |
fun ML_closed :: "nat \<Rightarrow> ml \<Rightarrow> bool"
|
|
256 |
and ML_closed_t :: "nat \<Rightarrow> tm \<Rightarrow> bool" where
|
|
257 |
"ML_closed i (C nm vs) = (ALL v:set vs. ML_closed i v)" |
|
|
258 |
"ML_closed i (V nm vs) = (ALL v:set vs. ML_closed i v)" |
|
|
259 |
"ML_closed i (Fun f vs n) = (ML_closed i f & (ALL v:set vs. ML_closed i v))" |
|
|
260 |
"ML_closed i (A_ML v vs) = (ML_closed i v & (ALL v:set vs. ML_closed i v))" |
|
|
261 |
"ML_closed i (apply v w) = (ML_closed i v & ML_closed i w)" |
|
|
262 |
"ML_closed i (CC nm) = True" |
|
|
263 |
"ML_closed i (V_ML X) = (X<i)" |
|
|
264 |
"ML_closed i (Lam_ML v) = ML_closed (i+1) v" |
|
|
265 |
"ML_closed_t i (term_of v) = ML_closed i v" |
|
|
266 |
"ML_closed_t i (At r s) = (ML_closed_t i r & ML_closed_t i s)" |
|
|
267 |
"ML_closed_t i (Lam t) = (ML_closed_t i t)" |
|
|
268 |
"ML_closed_t i v = True"
|
|
269 |
thm ML_closed.simps ML_closed_t.simps
|
|
270 |
|
23778
|
271 |
inductive_set
|
|
272 |
Red :: "(ml * ml)set"
|
|
273 |
and Redt :: "(tm * tm)set"
|
|
274 |
and Redl :: "(ml list * ml list)set"
|
|
275 |
and red :: "[ml, ml] => bool" (infixl "\<Rightarrow>" 50)
|
|
276 |
and redl :: "[ml list, ml list] => bool" (infixl "\<Rightarrow>" 50)
|
|
277 |
and redt :: "[tm, tm] => bool" (infixl "\<Rightarrow>" 50)
|
|
278 |
and reds :: "[ml, ml] => bool" (infixl "\<Rightarrow>*" 50)
|
|
279 |
and redts :: "[tm, tm] => bool" (infixl "\<Rightarrow>*" 50)
|
|
280 |
where
|
|
281 |
"s \<Rightarrow> t == (s, t) \<in> Red"
|
|
282 |
| "s \<Rightarrow> t == (s, t) \<in> Redl"
|
|
283 |
| "s \<Rightarrow> t == (s, t) \<in> Redt"
|
|
284 |
| "s \<Rightarrow>* t == (s, t) \<in> Red^*"
|
|
285 |
| "s \<Rightarrow>* t == (s, t) \<in> Redt^*"
|
23503
|
286 |
(* ML *)
|
23778
|
287 |
| "A_ML (Lam_ML u) [v] \<Rightarrow> u[v/0]"
|
23503
|
288 |
(* compiled rules *)
|
23778
|
289 |
| "(nm,vs,v) : compR ==> ALL i. ML_closed 0 (f i) \<Longrightarrow> A_ML (CC nm) (map (subst\<^bsub>ML\<^esub> f) vs) \<Rightarrow> subst\<^bsub>ML\<^esub> f v"
|
23503
|
290 |
(* apply *)
|
23778
|
291 |
| apply_Fun1: "apply (Fun f vs (Suc 0)) v \<Rightarrow> A_ML f (vs @ [v])"
|
|
292 |
| apply_Fun2: "n > 0 ==>
|
23503
|
293 |
apply (Fun f vs (Suc n)) v \<Rightarrow> Fun f (vs @ [v]) n"
|
23778
|
294 |
| apply_C: "apply (C nm vs) v \<Rightarrow> C nm (vs @ [v])"
|
|
295 |
| apply_V: "apply (V x vs) v \<Rightarrow> V x (vs @ [v])"
|
23503
|
296 |
(* term_of *)
|
23778
|
297 |
| term_of_C: "term_of (C nm vs) \<Rightarrow> foldl At (Ct nm) (map term_of vs)"
|
|
298 |
| term_of_V: "term_of (V x vs) \<Rightarrow> foldl At (Vt x) (map term_of vs)"
|
|
299 |
| term_of_Fun: "term_of(Fun vf vs n) \<Rightarrow>
|
23503
|
300 |
Lam (term_of ((apply (lift 0 (Fun vf vs n)) (V_ML 0))[V 0 []/0]))"
|
|
301 |
(* Context *)
|
23778
|
302 |
| ctxt_Lam: "t \<Rightarrow> t' ==> Lam t \<Rightarrow> Lam t'"
|
|
303 |
| ctxt_At1: "s \<Rightarrow> s' ==> At s t \<Rightarrow> At s' t"
|
|
304 |
| ctxt_At2: "t \<Rightarrow> t' ==> At s t \<Rightarrow> At s t'"
|
|
305 |
| ctxt_term_of: "v \<Rightarrow> v' ==> term_of v \<Rightarrow> term_of v'"
|
|
306 |
| ctxt_C: "vs \<Rightarrow> vs' ==> C nm vs \<Rightarrow> C nm vs'"
|
|
307 |
| ctxt_V: "vs \<Rightarrow> vs' ==> V x vs \<Rightarrow> V x vs'"
|
|
308 |
| ctxt_Fun1: "f \<Rightarrow> f' ==> Fun f vs n \<Rightarrow> Fun f' vs n"
|
|
309 |
| ctxt_Fun3: "vs \<Rightarrow> vs' ==> Fun f vs n \<Rightarrow> Fun f vs' n"
|
|
310 |
| ctxt_apply1: "s \<Rightarrow> s' ==> apply s t \<Rightarrow> apply s' t"
|
|
311 |
| ctxt_apply2: "t \<Rightarrow> t' ==> apply s t \<Rightarrow> apply s t'"
|
|
312 |
| ctxt_A_ML1: "f \<Rightarrow> f' ==> A_ML f vs \<Rightarrow> A_ML f' vs"
|
|
313 |
| ctxt_A_ML2: "vs \<Rightarrow> vs' ==> A_ML f vs \<Rightarrow> A_ML f vs'"
|
|
314 |
| ctxt_list1: "v \<Rightarrow> v' ==> v#vs \<Rightarrow> v'#vs"
|
|
315 |
| ctxt_list2: "vs \<Rightarrow> vs' ==> v#vs \<Rightarrow> v#vs'"
|
23503
|
316 |
|
|
317 |
|
|
318 |
consts
|
|
319 |
ar :: "const_name \<Rightarrow> nat"
|
|
320 |
|
|
321 |
axioms
|
|
322 |
ar_pos: "ar nm > 0"
|
|
323 |
|
|
324 |
types env = "ml list"
|
|
325 |
|
|
326 |
consts eval :: "tm \<Rightarrow> env \<Rightarrow> ml"
|
|
327 |
primrec
|
|
328 |
"eval (Vt x) e = e!x"
|
|
329 |
"eval (Ct nm) e = Fun (CC nm) [] (ar nm)"
|
|
330 |
"eval (At s t) e = apply (eval s e) (eval t e)"
|
|
331 |
"eval (Lam t) e = Fun (Lam_ML (eval t ((V_ML 0) # map (lift\<^bsub>ML\<^esub> 0) e))) [] 1"
|
|
332 |
|
|
333 |
fun size' :: "ml \<Rightarrow> nat" where
|
|
334 |
"size' (C nm vs) = (\<Sum>v\<leftarrow>vs. size' v)+1" |
|
|
335 |
"size' (V nm vs) = (\<Sum>v\<leftarrow>vs. size' v)+1" |
|
|
336 |
"size' (Fun f vs n) = (size' f + (\<Sum>v\<leftarrow>vs. size' v))+1" |
|
|
337 |
"size' (A_ML v vs) = (size' v + (\<Sum>v\<leftarrow>vs. size' v))+1" |
|
|
338 |
"size' (apply v w) = (size' v + size' w)+1" |
|
|
339 |
"size' (CC nm) = 1" |
|
|
340 |
"size' (V_ML X) = 1" |
|
|
341 |
"size' (Lam_ML v) = size' v + 1"
|
|
342 |
|
|
343 |
lemma listsum_size'[simp]:
|
|
344 |
"v \<in> set vs \<Longrightarrow> size' v < Suc(listsum (map size' vs))"
|
|
345 |
sorry
|
|
346 |
|
|
347 |
corollary cor_listsum_size'[simp]:
|
|
348 |
"v \<in> set vs \<Longrightarrow> size' v < Suc(m + listsum (map size' vs))"
|
|
349 |
using listsum_size'[of v vs] by arith
|
|
350 |
|
|
351 |
lemma
|
|
352 |
size_subst_ML[simp]: includes Vars assumes A: "!i. size(f i) = 0"
|
|
353 |
shows "size(subst\<^bsub>ML\<^esub> f t) = size(t)"
|
|
354 |
and "size(subst\<^bsub>ML\<^esub> f v) = size(v)"
|
|
355 |
and "ml_list_size1 (map (subst\<^bsub>ML\<^esub> f) vs) = ml_list_size1 vs"
|
|
356 |
and "ml_list_size2 (map (subst\<^bsub>ML\<^esub> f) vs) = ml_list_size2 vs"
|
|
357 |
and "ml_list_size3 (map (subst\<^bsub>ML\<^esub> f) vs) = ml_list_size3 vs"
|
|
358 |
and "ml_list_size4 (map (subst\<^bsub>ML\<^esub> f) vs) = ml_list_size4 vs"
|
|
359 |
by (induct rule: tm_ml.inducts) (simp_all add: A cons_ML_def split:nat.split)
|
|
360 |
|
|
361 |
lemma [simp]:
|
|
362 |
"\<forall>i j. size'(f i) = size'(V_ML j) \<Longrightarrow> size' (subst\<^bsub>ML\<^esub> f v) = size' v"
|
|
363 |
sorry
|
|
364 |
|
|
365 |
lemma [simp]: "size' (lift i v) = size' v"
|
|
366 |
sorry
|
|
367 |
|
|
368 |
(* the kernel function as in Section 4.1 of "Operational aspects\<dots>" *)
|
|
369 |
|
|
370 |
function kernel :: "ml \<Rightarrow> tm" ("_!" 300) where
|
|
371 |
"(C nm vs)! = foldl At (Ct nm) (map kernel vs)" |
|
|
372 |
"(Lam_ML v)! = Lam (((lift 0 v)[V 0 []/0])!)" |
|
|
373 |
"(Fun f vs n)! = foldl At (f!) (map kernel vs)" |
|
|
374 |
"(A_ML v vs)! = foldl At (v!) (map kernel vs)" |
|
|
375 |
"(apply v w)! = At (v!) (w!)" |
|
|
376 |
"(CC nm)! = Ct nm" |
|
|
377 |
"(V x vs)! = foldl At (Vt x) (map kernel vs)" |
|
|
378 |
"(V_ML X)! = undefined"
|
|
379 |
by pat_completeness auto
|
|
380 |
termination by(relation "measure size'") auto
|
|
381 |
|
|
382 |
consts kernelt :: "tm \<Rightarrow> tm" ("_!" 300)
|
|
383 |
primrec
|
|
384 |
"(Ct nm)! = Ct nm"
|
|
385 |
"(term_of v)! = v!"
|
|
386 |
"(Vt x)! = Vt x"
|
|
387 |
"(At s t)! = At (s!) (t!)"
|
|
388 |
"(Lam t)! = Lam (t!)"
|
|
389 |
|
|
390 |
abbreviation
|
|
391 |
kernels :: "ml list \<Rightarrow> tm list" ("_!" 300) where
|
|
392 |
"vs ! == map kernel vs"
|
|
393 |
|
|
394 |
(* soundness of the code generator *)
|
|
395 |
axioms
|
|
396 |
compiler_correct:
|
|
397 |
"(nm, vs, v) : compR ==> ALL i. ML_closed 0 (f i) \<Longrightarrow> (nm, (map (subst\<^bsub>ML\<^esub> f) vs)!, (subst\<^bsub>ML\<^esub> f v)!) : R"
|
|
398 |
|
|
399 |
|
|
400 |
consts
|
|
401 |
free_vars :: "tm \<Rightarrow> lam_var_name set"
|
|
402 |
primrec
|
|
403 |
"free_vars (Ct nm) = {}"
|
|
404 |
"free_vars (Vt x) = {x}"
|
|
405 |
"free_vars (Lam t) = {i. EX j : free_vars t. j = i+1}"
|
|
406 |
"free_vars (At s t) = free_vars s \<union> free_vars t"
|
|
407 |
|
|
408 |
lemma [simp]: "t : Pure_tms \<Longrightarrow> lift\<^bsub>ML\<^esub> k t = t"
|
|
409 |
by (erule Pure_tms.induct) simp_all
|
|
410 |
|
|
411 |
lemma kernel_pure: includes Vars assumes "t : Pure_tms" shows "t! = t"
|
|
412 |
using assms by (induct) simp_all
|
|
413 |
|
|
414 |
lemma lift_eval:
|
|
415 |
"t : Pure_tms \<Longrightarrow> ALL e k. (ALL i : free_vars t. i < size e) --> lift k (eval t e) = eval t (map (lift k) e)"
|
|
416 |
apply(induct set:Pure_tms)
|
|
417 |
apply simp_all
|
|
418 |
apply clarsimp
|
|
419 |
apply(erule_tac x = "V_ML 0 # map (lift\<^bsub>ML\<^esub> 0) e" in allE)
|
|
420 |
apply simp
|
|
421 |
apply(erule impE)
|
|
422 |
apply clarsimp
|
|
423 |
apply(case_tac i)apply simp
|
|
424 |
apply simp
|
|
425 |
apply (simp add: map_compose[symmetric])
|
|
426 |
apply (simp add: o_def lift_lift_ML_comm)
|
|
427 |
done
|
|
428 |
|
|
429 |
lemma lift_ML_eval[rule_format]:
|
|
430 |
"t : Pure_tms \<Longrightarrow> ALL e k. (ALL i : free_vars t. i < size e) --> lift\<^bsub>ML\<^esub> k (eval t e) = eval t (map (lift\<^bsub>ML\<^esub> k) e)"
|
|
431 |
apply(induct set:Pure_tms)
|
|
432 |
apply simp_all
|
|
433 |
apply clarsimp
|
|
434 |
apply(erule_tac x = "V_ML 0 # map (lift\<^bsub>ML\<^esub> 0) e" in allE)
|
|
435 |
apply simp
|
|
436 |
apply(erule impE)
|
|
437 |
apply clarsimp
|
|
438 |
apply(case_tac i)apply simp
|
|
439 |
apply simp
|
|
440 |
apply (simp add: map_compose[symmetric])
|
|
441 |
apply (simp add:o_def lift_lift_ML)
|
|
442 |
done
|
|
443 |
|
|
444 |
lemma [simp]: includes Vars shows "(v ## f) 0 = v"
|
|
445 |
by(simp add:cons_ML_def)
|
|
446 |
|
|
447 |
lemma [simp]: includes Vars shows "(v ## f) (Suc n) = lift\<^bsub>ML\<^esub> 0 (f n)"
|
|
448 |
by(simp add:cons_ML_def)
|
|
449 |
|
|
450 |
lemma lift_o_shift: "lift k o (V_ML 0 ## f) = (V_ML 0 ## (lift k \<circ> f))"
|
|
451 |
apply(rule ext)
|
|
452 |
apply (simp add:cons_ML_def lift_lift_ML_comm split:nat.split)
|
|
453 |
done
|
|
454 |
|
|
455 |
lemma lift_subst_ML: shows
|
|
456 |
"lift_tm k (subst\<^bsub>ML\<^esub> f t) = subst\<^bsub>ML\<^esub> (lift_ml k o f) (lift_tm k t)" and
|
|
457 |
"lift_ml k (subst\<^bsub>ML\<^esub> f v) = subst\<^bsub>ML\<^esub> (lift_ml k o f) (lift_ml k v)"
|
|
458 |
apply (induct t and v arbitrary: f k and f k rule: lift_tm_lift_ml.induct)
|
|
459 |
apply (simp_all add:map_compose[symmetric] o_assoc lift_o_lift lift_o_shift)
|
|
460 |
done
|
|
461 |
|
|
462 |
corollary lift_subst_ML1: "\<forall>v k. lift_ml 0 (u[v/k]) = (lift_ml 0 u)[lift 0 v/k]"
|
|
463 |
apply(rule measure_induct[where f = "size" and a = u])
|
|
464 |
apply(case_tac x)
|
|
465 |
apply(simp_all add:lift_lift map_compose[symmetric] lift_subst_ML)
|
|
466 |
apply(subst lift_lift_ML_comm)apply simp
|
|
467 |
done
|
|
468 |
|
|
469 |
lemma lift_ML_lift_ML: includes Vars shows
|
|
470 |
"i < k+1 \<Longrightarrow> lift\<^bsub>ML\<^esub> (Suc k) (lift\<^bsub>ML\<^esub> i t) = lift\<^bsub>ML\<^esub> i (lift\<^bsub>ML\<^esub> k t)"
|
|
471 |
and "i < k+1 \<Longrightarrow> lift\<^bsub>ML\<^esub> (Suc k) (lift\<^bsub>ML\<^esub> i v) = lift\<^bsub>ML\<^esub> i (lift\<^bsub>ML\<^esub> k v)"
|
|
472 |
apply (induct k t and k v arbitrary: i k and i k
|
|
473 |
rule: lift_tm_ML_lift_ml_ML.induct)
|
|
474 |
apply(simp_all add:map_compose[symmetric])
|
|
475 |
done
|
|
476 |
|
|
477 |
corollary lift_ML_o_lift_ML: shows
|
|
478 |
"i < k+1 \<Longrightarrow> lift_tm_ML (Suc k) o (lift_tm_ML i) = lift_tm_ML i o lift_tm_ML k" and
|
|
479 |
"i < k+1 \<Longrightarrow> lift_ml_ML (Suc k) o (lift_ml_ML i) = lift_ml_ML i o lift_ml_ML k"
|
|
480 |
by(rule ext, simp add:lift_ML_lift_ML)+
|
|
481 |
|
|
482 |
abbreviation insrt where
|
|
483 |
"insrt k f == (%i. if i<k then lift_ml_ML k (f i) else if i=k then V_ML k else lift_ml_ML k (f(i - 1)))"
|
|
484 |
|
|
485 |
lemma subst_insrt_lift: includes Vars shows
|
|
486 |
"subst\<^bsub>ML\<^esub> (insrt k f) (lift\<^bsub>ML\<^esub> k t) = lift\<^bsub>ML\<^esub> k (subst\<^bsub>ML\<^esub> f t)" and
|
|
487 |
"subst\<^bsub>ML\<^esub> (insrt k f) (lift\<^bsub>ML\<^esub> k v) = lift\<^bsub>ML\<^esub> k (subst\<^bsub>ML\<^esub> f v)"
|
|
488 |
apply (induct k t and k v arbitrary: f k and f k rule: lift_tm_ML_lift_ml_ML.induct)
|
|
489 |
apply (simp_all add:map_compose[symmetric] o_assoc lift_o_lift lift_o_shift)
|
|
490 |
apply(subgoal_tac "lift 0 \<circ> insrt k f = insrt k (lift 0 \<circ> f)")
|
|
491 |
apply simp
|
|
492 |
apply(rule ext)
|
|
493 |
apply (simp add:lift_lift_ML_comm)
|
|
494 |
apply(subgoal_tac "V_ML 0 ## insrt k f = insrt (Suc k) (V_ML 0 ## f)")
|
|
495 |
apply simp
|
|
496 |
apply(rule ext)
|
|
497 |
apply (simp add:lift_ML_lift_ML cons_ML_def split:nat.split)
|
|
498 |
done
|
|
499 |
|
|
500 |
corollary subst_cons_lift: includes Vars shows
|
|
501 |
"subst\<^bsub>ML\<^esub> (V_ML 0 ## f) o (lift_ml_ML 0) = lift_ml_ML 0 o (subst_ml_ML f)"
|
|
502 |
apply(rule ext)
|
|
503 |
apply(simp add: cons_ML_def subst_insrt_lift[symmetric])
|
|
504 |
apply(subgoal_tac "nat_case (V_ML 0) (\<lambda>j. lift\<^bsub>ML\<^esub> 0 (f j)) = (\<lambda>i. if i = 0 then V_ML 0 else lift\<^bsub>ML\<^esub> 0 (f (i - 1)))")
|
|
505 |
apply simp
|
|
506 |
apply(rule ext, simp split:nat.split)
|
|
507 |
done
|
|
508 |
|
|
509 |
lemma subst_eval[rule_format]: "t : Pure_tms \<Longrightarrow>
|
|
510 |
ALL f e. (ALL i : free_vars t. i < size e) \<longrightarrow> subst\<^bsub>ML\<^esub> f (eval t e) = eval t (map (subst\<^bsub>ML\<^esub> f) e)"
|
|
511 |
apply(induct set:Pure_tms)
|
|
512 |
apply simp_all
|
|
513 |
apply clarsimp
|
|
514 |
apply(erule_tac x="V_ML 0 ## f" in allE)
|
|
515 |
apply(erule_tac x= "(V_ML 0 # map (lift\<^bsub>ML\<^esub> 0) e)" in allE)
|
|
516 |
apply(erule impE)
|
|
517 |
apply clarsimp
|
|
518 |
apply(case_tac i)apply simp
|
|
519 |
apply simp
|
|
520 |
apply (simp add:subst_cons_lift map_compose[symmetric])
|
|
521 |
done
|
|
522 |
|
|
523 |
|
|
524 |
theorem kernel_eval[rule_format]: includes Vars shows
|
|
525 |
"t : Pure_tms ==>
|
|
526 |
ALL e. (ALL i : free_vars t. i < size e) \<longrightarrow> (ALL i < size e. e!i = V i []) --> (eval t e)! = t!"
|
|
527 |
apply(induct set:Pure_tms)
|
|
528 |
apply simp_all
|
|
529 |
apply clarsimp
|
|
530 |
apply(subst lift_eval) apply simp
|
|
531 |
apply clarsimp
|
|
532 |
apply(case_tac i)apply simp
|
|
533 |
apply simp
|
|
534 |
apply(subst subst_eval) apply simp
|
|
535 |
apply clarsimp
|
|
536 |
apply(case_tac i)apply simp
|
|
537 |
apply simp
|
|
538 |
apply(erule_tac x="map (subst\<^bsub>ML\<^esub> (\<lambda>n. if n = 0 then V 0 [] else V_ML (n - 1)))
|
|
539 |
(map (lift 0) (V_ML 0 # map (lift\<^bsub>ML\<^esub> 0) e))" in allE)
|
|
540 |
apply(erule impE)
|
|
541 |
apply(clarsimp)
|
|
542 |
apply(case_tac i)apply simp
|
|
543 |
apply simp
|
|
544 |
apply(erule impE)
|
|
545 |
apply(clarsimp)
|
|
546 |
apply(case_tac i)apply simp
|
|
547 |
apply simp
|
|
548 |
apply simp
|
|
549 |
done
|
|
550 |
|
|
551 |
(*
|
|
552 |
lemma subst_ML_compose:
|
|
553 |
"subst_ml_ML f2 (subst_ml_ML f1 v) = subst_ml_ML (%i. subst_ml_ML f2 (f1 i)) v"
|
|
554 |
sorry
|
|
555 |
*)
|
|
556 |
|
|
557 |
lemma map_eq_iff_nth:
|
|
558 |
"(map f xs = map g xs) = (!i<size xs. f(xs!i) = g(xs!i))"
|
|
559 |
sorry
|
|
560 |
|
|
561 |
lemma [simp]: includes Vars shows "ML_closed k v \<Longrightarrow> lift\<^bsub>ML\<^esub> k v = v"
|
|
562 |
sorry
|
|
563 |
lemma [simp]: includes Vars shows "ML_closed 0 v \<Longrightarrow> subst\<^bsub>ML\<^esub> f v = v"
|
|
564 |
sorry
|
|
565 |
lemma [simp]: includes Vars shows "ML_closed k v \<Longrightarrow> ML_closed k (lift m v)"
|
|
566 |
sorry
|
|
567 |
|
|
568 |
lemma red_Lam[simp]: includes Vars shows "t \<rightarrow>* t' ==> Lam t \<rightarrow>* Lam t'"
|
|
569 |
apply(induct rule:rtrancl_induct)
|
|
570 |
apply(simp_all)
|
|
571 |
apply(blast intro: rtrancl_into_rtrancl tRed.intros)
|
|
572 |
done
|
|
573 |
|
|
574 |
lemma red_At1[simp]: includes Vars shows "t \<rightarrow>* t' ==> At t s \<rightarrow>* At t' s"
|
|
575 |
apply(induct rule:rtrancl_induct)
|
|
576 |
apply(simp_all)
|
|
577 |
apply(blast intro: rtrancl_into_rtrancl tRed.intros)
|
|
578 |
done
|
|
579 |
|
|
580 |
lemma red_At2[simp]: includes Vars shows "t \<rightarrow>* t' ==> At s t \<rightarrow>* At s t'"
|
|
581 |
apply(induct rule:rtrancl_induct)
|
|
582 |
apply(simp_all)
|
|
583 |
apply(blast intro:rtrancl_into_rtrancl tRed.intros)
|
|
584 |
done
|
|
585 |
|
|
586 |
lemma tRed_list_foldl_At:
|
|
587 |
"ts \<rightarrow>* ts' \<Longrightarrow> s \<rightarrow>* s' \<Longrightarrow> foldl At s ts \<rightarrow>* foldl At s' ts'"
|
|
588 |
apply(induct arbitrary:s s' rule:tRed_list.induct)
|
|
589 |
apply simp
|
|
590 |
apply simp
|
|
591 |
apply(blast dest: red_At1 red_At2 intro:rtrancl_trans)
|
|
592 |
done
|
|
593 |
|
|
594 |
lemma [trans]: "s = t \<Longrightarrow> t \<rightarrow> t' \<Longrightarrow> s \<rightarrow> t'"
|
|
595 |
by simp
|
|
596 |
|
|
597 |
|
|
598 |
lemma subst_foldl[simp]:
|
|
599 |
"subst f (foldl At s ts) = foldl At (subst f s) (map (subst f) ts)"
|
|
600 |
by (induct ts arbitrary: s) auto
|
|
601 |
|
|
602 |
|
|
603 |
lemma foldl_At_size: "size ts = size ts' \<Longrightarrow>
|
|
604 |
foldl At s ts = foldl At s' ts' \<longleftrightarrow> s = s' & ts = ts'"
|
|
605 |
by (induct arbitrary: s s' rule:list_induct2) simp_all
|
|
606 |
|
|
607 |
consts depth_At :: "tm \<Rightarrow> nat"
|
|
608 |
primrec
|
|
609 |
"depth_At(Ct cn) = 0"
|
|
610 |
"depth_At(Vt x) = 0"
|
|
611 |
"depth_At(Lam t) = 0"
|
|
612 |
"depth_At(At s t) = depth_At s + 1"
|
|
613 |
"depth_At(term_of v) = 0"
|
|
614 |
|
|
615 |
lemma depth_At_foldl:
|
|
616 |
"depth_At(foldl At s ts) = depth_At s + size ts"
|
|
617 |
by (induct ts arbitrary: s) simp_all
|
|
618 |
|
|
619 |
lemma foldl_At_eq_length:
|
|
620 |
"foldl At s ts = foldl At s ts' \<Longrightarrow> length ts = length ts'"
|
|
621 |
apply(subgoal_tac "depth_At(foldl At s ts) = depth_At(foldl At s ts')")
|
|
622 |
apply(erule thin_rl)
|
|
623 |
apply (simp add:depth_At_foldl)
|
|
624 |
apply simp
|
|
625 |
done
|
|
626 |
|
|
627 |
lemma foldl_At_eq[simp]: "foldl At s ts = foldl At s ts' \<longleftrightarrow> ts = ts'"
|
|
628 |
apply(rule)
|
|
629 |
prefer 2 apply simp
|
|
630 |
apply(blast dest:foldl_At_size foldl_At_eq_length)
|
|
631 |
done
|
|
632 |
|
|
633 |
lemma [simp]: "foldl At s ts ! = foldl At (s!) (map kernelt ts)"
|
|
634 |
by (induct ts arbitrary: s) simp_all
|
|
635 |
|
|
636 |
lemma [simp]: "(kernelt \<circ> term_of) = kernel"
|
|
637 |
by(rule ext) simp
|
|
638 |
|
|
639 |
lemma shift_subst_decr:
|
|
640 |
"Vt 0 ## subst_decr k t = subst_decr (Suc k) (lift 0 t)"
|
|
641 |
apply(rule ext)
|
|
642 |
apply (simp add:cons_def split:nat.split)
|
|
643 |
done
|
|
644 |
|
|
645 |
lemma [simp]: "lift k (foldl At s ts) = foldl At (lift k s) (map (lift k) ts)"
|
|
646 |
by(induct ts arbitrary:s) simp_all
|
|
647 |
|
|
648 |
subsection "Horrible detour"
|
|
649 |
|
|
650 |
definition "liftn n == lift_ml 0 ^ n"
|
|
651 |
|
|
652 |
lemma [simp]: "liftn n (C i vs) = C i (map (liftn n) vs)"
|
|
653 |
apply(unfold liftn_def)
|
|
654 |
apply(induct n)
|
|
655 |
apply (simp_all add: map_compose[symmetric])
|
|
656 |
done
|
|
657 |
|
|
658 |
lemma [simp]: "liftn n (CC nm) = CC nm"
|
|
659 |
apply(unfold liftn_def)
|
|
660 |
apply(induct n)
|
|
661 |
apply (simp_all add: map_compose[symmetric])
|
|
662 |
done
|
|
663 |
|
|
664 |
lemma [simp]: "liftn n (apply v w) = apply (liftn n v) (liftn n w)"
|
|
665 |
apply(unfold liftn_def)
|
|
666 |
apply(induct n)
|
|
667 |
apply (simp_all add: map_compose[symmetric])
|
|
668 |
done
|
|
669 |
|
|
670 |
lemma [simp]: "liftn n (A_ML v vs) = A_ML (liftn n v) (map (liftn n) vs)"
|
|
671 |
apply(unfold liftn_def)
|
|
672 |
apply(induct n)
|
|
673 |
apply (simp_all add: map_compose[symmetric])
|
|
674 |
done
|
|
675 |
|
|
676 |
lemma [simp]:
|
|
677 |
"liftn n (Fun v vs i) = Fun (liftn n v) (map (liftn n) vs) i"
|
|
678 |
apply(unfold liftn_def)
|
|
679 |
apply(induct n)
|
|
680 |
apply (simp_all add: map_compose[symmetric] id_def)
|
|
681 |
done
|
|
682 |
|
|
683 |
lemma [simp]: "liftn n (Lam_ML v) = Lam_ML (liftn n v)"
|
|
684 |
apply(unfold liftn_def)
|
|
685 |
apply(induct n)
|
|
686 |
apply (simp_all add: map_compose[symmetric] id_def)
|
|
687 |
done
|
|
688 |
|
|
689 |
lemma liftn_liftn_add: "liftn m (liftn n v) = liftn (m+n) v"
|
|
690 |
by(simp add:liftn_def funpow_add)
|
|
691 |
|
|
692 |
lemma [simp]: "liftn n (V_ML k) = V_ML k"
|
|
693 |
apply(unfold liftn_def)
|
|
694 |
apply(induct n)
|
|
695 |
apply (simp_all)
|
|
696 |
done
|
|
697 |
|
|
698 |
lemma liftn_lift_ML_comm: "liftn n (lift\<^bsub>ML\<^esub> 0 v) = lift\<^bsub>ML\<^esub> 0 (liftn n v)"
|
|
699 |
apply(unfold liftn_def)
|
|
700 |
apply(induct n)
|
|
701 |
apply (simp_all add:lift_lift_ML_comm)
|
|
702 |
done
|
|
703 |
|
|
704 |
lemma liftn_cons: "liftn n ((V_ML 0 ## f) x) = (V_ML 0 ## (liftn n o f)) x"
|
|
705 |
apply(simp add:cons_ML_def liftn_lift_ML_comm split:nat.split)
|
|
706 |
done
|
|
707 |
|
|
708 |
text{* End of horrible detour *}
|
|
709 |
|
|
710 |
lemma kernel_subst1:
|
|
711 |
"ML_closed 1 u \<Longrightarrow> ML_closed 0 v \<Longrightarrow> kernel( u[v/0]) = (kernel((lift 0 u)[V 0 []/0]))[kernel v/0]"
|
|
712 |
sorry
|
|
713 |
|
|
714 |
lemma includes Vars shows foldl_Pure[simp]:
|
|
715 |
"t : Pure_tms \<Longrightarrow> \<forall>t\<in>set ts. t : Pure_tms \<Longrightarrow>
|
|
716 |
(!!s t. s : Pure_tms \<Longrightarrow> t : Pure_tms \<Longrightarrow> f s t : Pure_tms) \<Longrightarrow>
|
|
717 |
foldl f t ts \<in> Pure_tms"
|
|
718 |
by(induct ts arbitrary: t) simp_all
|
|
719 |
|
|
720 |
declare Pure_tms.intros[simp]
|
|
721 |
|
|
722 |
lemma includes Vars shows "ML_closed 0 v \<Longrightarrow> kernel v : Pure_tms"
|
|
723 |
apply(induct rule:kernel.induct)
|
|
724 |
apply simp_all
|
|
725 |
apply(rule Pure_tms.intros);
|
|
726 |
(* "ML_closed (Suc k) v \<Longrightarrow> ML_closed k (lift 0 v)" *)
|
|
727 |
sorry
|
|
728 |
|
|
729 |
lemma subst_Vt: includes Vars shows "subst Vt = id"
|
|
730 |
sorry
|
|
731 |
(*
|
|
732 |
apply(rule ext)
|
|
733 |
apply(induct_tac x)
|
|
734 |
apply simp_all
|
|
735 |
|
|
736 |
done
|
|
737 |
*)
|
|
738 |
(* klappt noch nicht ganz *)
|
|
739 |
theorem Red_sound: includes Vars
|
|
740 |
shows "v \<Rightarrow> v' \<Longrightarrow> ML_closed 0 v \<Longrightarrow> v! \<rightarrow>* v'! & ML_closed 0 v'"
|
|
741 |
and "t \<Rightarrow> t' \<Longrightarrow> ML_closed_t 0 t \<Longrightarrow> kernelt t \<rightarrow>* kernelt t' & ML_closed_t 0 t'"
|
|
742 |
and "(vs :: ml list) \<Rightarrow> vs' \<Longrightarrow> !v : set vs . ML_closed 0 v \<Longrightarrow> map kernel vs \<rightarrow>* map kernel vs' & (! v':set vs'. ML_closed 0 v')"
|
|
743 |
proof(induct rule:Red_Redt_Redl.inducts)
|
|
744 |
fix u v
|
|
745 |
let ?v = "A_ML (Lam_ML u) [v]"
|
|
746 |
assume cl: "ML_closed 0 (A_ML (Lam_ML u) [v])"
|
|
747 |
let ?u' = "(lift_ml 0 u)[V 0 []/0]"
|
|
748 |
have "?v! = At (Lam ((?u')!)) (v !)" by simp
|
|
749 |
also have "\<dots> \<rightarrow> (?u' !)[v!/0]" (is "_ \<rightarrow> ?R") by(rule tRed.intros)
|
|
750 |
also have "?R = u[v/0]!" using cl
|
|
751 |
apply(cut_tac u = "u" and v = "v" in kernel_subst1)
|
|
752 |
apply(simp_all)
|
|
753 |
done
|
|
754 |
finally have "kernel(A_ML (Lam_ML u) [v]) \<rightarrow>* kernel(u[v/0])" (is ?A) by(rule r_into_rtrancl)
|
|
755 |
moreover have "ML_closed 0 (u[v/0])" (is "?C") using cl apply simp sorry
|
|
756 |
ultimately show "?A & ?C" ..
|
|
757 |
next
|
|
758 |
case term_of_C thus ?case apply (auto simp:map_compose[symmetric])sorry
|
|
759 |
next
|
|
760 |
fix f :: "nat \<Rightarrow> ml" and nm vs v
|
|
761 |
assume f: "\<forall>i. ML_closed 0 (f i)" and compR: "(nm, vs, v) \<in> compR"
|
|
762 |
note tRed.intros(2)[OF compiler_correct[OF compR f], of Vt,simplified map_compose[symmetric]]
|
|
763 |
hence red: "foldl At (Ct nm) (map (kernel o subst\<^bsub>ML\<^esub> f) vs) \<rightarrow>
|
|
764 |
(subst\<^bsub>ML\<^esub> f v)!" (is "_ \<rightarrow> ?R") apply(simp add:map_compose) sorry
|
|
765 |
have "A_ML (CC nm) (map (subst\<^bsub>ML\<^esub> f) vs)! =
|
|
766 |
foldl At (Ct nm) (map (kernel o subst\<^bsub>ML\<^esub> f) vs)" by (simp add:map_compose)
|
|
767 |
also(* have "map (kernel o subst\<^bsub>ML\<^esub> f) vs = map (subst (kernel o f)) (vs!)"
|
|
768 |
using closed_subst_kernel(2)[OF compiled_V_free1[OF compR]]
|
|
769 |
by (simp add:map_compose[symmetric])
|
|
770 |
also*) note red
|
|
771 |
(*also have "?R = subst\<^bsub>ML\<^esub> f v!"
|
|
772 |
using closed_subst_kernel(2)[OF compiled_V_free2[OF compR]] by simp*)
|
|
773 |
finally have "A_ML (CC nm) (map (subst\<^bsub>ML\<^esub> f) vs)! \<rightarrow>* subst\<^bsub>ML\<^esub> f v!" (is "?A")
|
|
774 |
by(rule r_into_rtrancl) (*
|
|
775 |
also have "?l = (subst\<^bsub>ML\<^esub> fa (A_ML (CC nm) (map (subst\<^bsub>ML\<^esub> f) vs)))!" (is "_ = ?l'") sorry
|
|
776 |
also have "?r = subst\<^bsub>ML\<^esub> fa (subst\<^bsub>ML\<^esub> f v)!" (is "_ = ?r'") sorry
|
|
777 |
finally have "?l' \<rightarrow>* ?r'" (is ?A) . *)
|
|
778 |
moreover have "ML_closed 0 (subst\<^bsub>ML\<^esub> f v)" (is "?C") using prems sorry
|
|
779 |
ultimately show "?A & ?C" ..
|
|
780 |
next
|
|
781 |
case term_of_V thus ?case apply (auto simp:map_compose[symmetric]) sorry
|
|
782 |
next
|
23778
|
783 |
case (term_of_Fun vf vs n)
|
23503
|
784 |
hence "term_of (Fun vf vs n)! \<rightarrow>*
|
|
785 |
Lam (term_of (apply (lift 0 (Fun vf vs n)) (V_ML 0)[V 0 []/0]))!" sorry
|
|
786 |
moreover
|
|
787 |
have "ML_closed_t 0
|
|
788 |
(Lam (term_of (apply (lift 0 (Fun vf vs n)) (V_ML 0)[V 0 []/0])))" sorry
|
|
789 |
ultimately show ?case ..
|
|
790 |
next
|
|
791 |
case apply_Fun1 thus ?case by simp
|
|
792 |
next
|
|
793 |
case apply_Fun2 thus ?case by simp
|
|
794 |
next
|
|
795 |
case apply_C thus ?case by simp
|
|
796 |
next
|
|
797 |
case apply_V thus ?case by simp
|
|
798 |
next
|
|
799 |
case ctxt_Lam thus ?case by(auto)
|
|
800 |
next
|
|
801 |
case ctxt_At1 thus ?case by(auto)
|
|
802 |
next
|
|
803 |
case ctxt_At2 thus ?case by (auto)
|
|
804 |
next
|
|
805 |
case ctxt_term_of thus ?case by (auto)
|
|
806 |
next
|
|
807 |
case ctxt_C thus ?case by (fastsimp simp:tRed_list_foldl_At)
|
|
808 |
next
|
|
809 |
case ctxt_V thus ?case by (fastsimp simp:tRed_list_foldl_At)
|
|
810 |
next
|
|
811 |
case ctxt_Fun1 thus ?case by (fastsimp simp:tRed_list_foldl_At)
|
|
812 |
next
|
|
813 |
case ctxt_Fun3 thus ?case by (fastsimp simp:tRed_list_foldl_At)
|
|
814 |
next
|
|
815 |
case ctxt_apply1 thus ?case by auto
|
|
816 |
next
|
|
817 |
case ctxt_apply2 thus ?case by auto
|
|
818 |
next
|
|
819 |
case ctxt_A_ML1 thus ?case by (fastsimp simp:tRed_list_foldl_At)
|
|
820 |
next
|
|
821 |
case ctxt_A_ML2 thus ?case by (fastsimp simp:tRed_list_foldl_At)
|
|
822 |
next
|
|
823 |
case ctxt_list1 thus ?case by simp
|
|
824 |
next
|
|
825 |
case ctxt_list2 thus ?case by simp
|
|
826 |
qed
|
|
827 |
|
|
828 |
|
|
829 |
inductive_cases tRedE: "Ct n \<rightarrow> u"
|
|
830 |
thm tRedE
|
|
831 |
|
|
832 |
lemma [simp]: "Ct n = foldl At t ts \<longleftrightarrow> t = Ct n & ts = []"
|
|
833 |
by (induct ts arbitrary:t) auto
|
|
834 |
|
|
835 |
corollary kernel_inv: includes Vars shows
|
|
836 |
"(t :: tm) \<Rightarrow>* t' ==> ML_closed_t 0 t ==> t! \<rightarrow>* t'!"
|
|
837 |
sorry
|
|
838 |
|
|
839 |
theorem includes Vars
|
|
840 |
assumes t: "t : Pure_tms" and t': "t' : Pure_tms" and
|
|
841 |
closed: "free_vars t = {}" and reds: "term_of (eval t []) \<Rightarrow>* t'"
|
|
842 |
shows "t \<rightarrow>* t' "
|
|
843 |
proof -
|
|
844 |
have ML_cl: "ML_closed_t 0 (term_of (eval t []))" sorry
|
|
845 |
have "(eval t [])! = t!"
|
|
846 |
using kernel_eval[OF t, where e="[]"] closed by simp
|
|
847 |
hence "(term_of (eval t []))! = t!" by simp
|
|
848 |
moreover have "term_of (eval t [])! \<rightarrow>* t'!"
|
|
849 |
using kernel_inv[OF reds ML_cl] by auto
|
|
850 |
ultimately have "t! \<rightarrow>* t'!" by simp
|
|
851 |
thus ?thesis using kernel_pure t t' by auto
|
|
852 |
qed
|
|
853 |
|
|
854 |
end
|