174 done |
174 done |
175 |
175 |
176 |
176 |
177 subsection {* Main theorems *} |
177 subsection {* Main theorems *} |
178 |
178 |
|
179 lemma norm_list: |
|
180 assumes f_compat: "\<And>t t'. t \<rightarrow>\<^sub>\<beta>\<^sup>* t' \<Longrightarrow> f t \<rightarrow>\<^sub>\<beta>\<^sup>* f t'" |
|
181 and f_NF: "\<And>t. t \<in> NF \<Longrightarrow> f t \<in> NF" |
|
182 and uNF: "u \<in> NF" and uT: "e \<turnstile> u : T" |
|
183 shows "\<And>Us. e\<langle>i:T\<rangle> \<tturnstile> as : Us \<Longrightarrow> |
|
184 listall (\<lambda>t. \<forall>e T' u i. e\<langle>i:T\<rangle> \<turnstile> t : T' \<longrightarrow> |
|
185 u \<in> NF \<longrightarrow> e \<turnstile> u : T \<longrightarrow> (\<exists>t'. t[u/i] \<rightarrow>\<^sub>\<beta>\<^sup>* t' \<and> t' \<in> NF)) as \<Longrightarrow> |
|
186 \<exists>as'. \<forall>j. Var j \<degree>\<degree> map (\<lambda>t. f (t[u/i])) as \<rightarrow>\<^sub>\<beta>\<^sup>* |
|
187 Var j \<degree>\<degree> map f as' \<and> Var j \<degree>\<degree> map f as' \<in> NF" |
|
188 (is "\<And>Us. _ \<Longrightarrow> listall ?R as \<Longrightarrow> \<exists>as'. ?ex Us as as'") |
|
189 proof (induct as rule: rev_induct) |
|
190 case (Nil Us) |
|
191 with Var_NF have "?ex Us [] []" by simp |
|
192 thus ?case .. |
|
193 next |
|
194 case (snoc b bs Us) |
|
195 have "e\<langle>i:T\<rangle> \<tturnstile> bs @ [b] : Us" . |
|
196 then obtain Vs W where Us: "Us = Vs @ [W]" |
|
197 and bs: "e\<langle>i:T\<rangle> \<tturnstile> bs : Vs" and bT: "e\<langle>i:T\<rangle> \<turnstile> b : W" |
|
198 by (rule types_snocE) |
|
199 from snoc have "listall ?R bs" by simp |
|
200 with bs have "\<exists>bs'. ?ex Vs bs bs'" by (rule snoc) |
|
201 then obtain bs' where |
|
202 bsred: "\<And>j. Var j \<degree>\<degree> map (\<lambda>t. f (t[u/i])) bs \<rightarrow>\<^sub>\<beta>\<^sup>* Var j \<degree>\<degree> map f bs'" |
|
203 and bsNF: "\<And>j. Var j \<degree>\<degree> map f bs' \<in> NF" by iprover |
|
204 from snoc have "?R b" by simp |
|
205 with bT and uNF and uT have "\<exists>b'. b[u/i] \<rightarrow>\<^sub>\<beta>\<^sup>* b' \<and> b' \<in> NF" |
|
206 by iprover |
|
207 then obtain b' where bred: "b[u/i] \<rightarrow>\<^sub>\<beta>\<^sup>* b'" and bNF: "b' \<in> NF" |
|
208 by iprover |
|
209 from bsNF [of 0] have "listall (\<lambda>t. t \<in> NF) (map f bs')" |
|
210 by (rule App_NF_D) |
|
211 moreover have "f b' \<in> NF" by (rule f_NF) |
|
212 ultimately have "listall (\<lambda>t. t \<in> NF) (map f (bs' @ [b']))" |
|
213 by simp |
|
214 hence "\<And>j. Var j \<degree>\<degree> map f (bs' @ [b']) \<in> NF" by (rule NF.App) |
|
215 moreover from bred have "f (b[u/i]) \<rightarrow>\<^sub>\<beta>\<^sup>* f b'" |
|
216 by (rule f_compat) |
|
217 with bsred have |
|
218 "\<And>j. (Var j \<degree>\<degree> map (\<lambda>t. f (t[u/i])) bs) \<degree> f (b[u/i]) \<rightarrow>\<^sub>\<beta>\<^sup>* |
|
219 (Var j \<degree>\<degree> map f bs') \<degree> f b'" by (rule rtrancl_beta_App) |
|
220 ultimately have "?ex Us (bs @ [b]) (bs' @ [b'])" by simp |
|
221 thus ?case .. |
|
222 qed |
|
223 |
179 lemma subst_type_NF: |
224 lemma subst_type_NF: |
180 "\<And>t e T u i. t \<in> NF \<Longrightarrow> e\<langle>i:U\<rangle> \<turnstile> t : T \<Longrightarrow> u \<in> NF \<Longrightarrow> e \<turnstile> u : U \<Longrightarrow> \<exists>t'. t[u/i] \<rightarrow>\<^sub>\<beta>\<^sup>* t' \<and> t' \<in> NF" |
225 "\<And>t e T u i. t \<in> NF \<Longrightarrow> e\<langle>i:U\<rangle> \<turnstile> t : T \<Longrightarrow> u \<in> NF \<Longrightarrow> e \<turnstile> u : U \<Longrightarrow> \<exists>t'. t[u/i] \<rightarrow>\<^sub>\<beta>\<^sup>* t' \<and> t' \<in> NF" |
181 (is "PROP ?P U" is "\<And>t e T u i. _ \<Longrightarrow> PROP ?Q t e T u i U") |
226 (is "PROP ?P U" is "\<And>t e T u i. _ \<Longrightarrow> PROP ?Q t e T u i U") |
182 proof (induct U) |
227 proof (induct U) |
183 fix T t |
228 fix T t |
189 thus "\<And>e T' u i. PROP ?Q t e T' u i T" |
234 thus "\<And>e T' u i. PROP ?Q t e T' u i T" |
190 proof induct |
235 proof induct |
191 fix e T' u i assume uNF: "u \<in> NF" and uT: "e \<turnstile> u : T" |
236 fix e T' u i assume uNF: "u \<in> NF" and uT: "e \<turnstile> u : T" |
192 { |
237 { |
193 case (App ts x e_ T'_ u_ i_) |
238 case (App ts x e_ T'_ u_ i_) |
194 assume appT: "e\<langle>i:T\<rangle> \<turnstile> Var x \<degree>\<degree> ts : T'" |
239 assume "e\<langle>i:T\<rangle> \<turnstile> Var x \<degree>\<degree> ts : T'" |
|
240 then obtain Us |
|
241 where varT: "e\<langle>i:T\<rangle> \<turnstile> Var x : Us \<Rrightarrow> T'" |
|
242 and argsT: "e\<langle>i:T\<rangle> \<tturnstile> ts : Us" |
|
243 by (rule var_app_typesE) |
195 from nat_eq_dec show "\<exists>t'. (Var x \<degree>\<degree> ts)[u/i] \<rightarrow>\<^sub>\<beta>\<^sup>* t' \<and> t' \<in> NF" |
244 from nat_eq_dec show "\<exists>t'. (Var x \<degree>\<degree> ts)[u/i] \<rightarrow>\<^sub>\<beta>\<^sup>* t' \<and> t' \<in> NF" |
196 proof |
245 proof |
197 assume eq: "x = i" |
246 assume eq: "x = i" |
198 show ?thesis |
247 show ?thesis |
199 proof (cases ts) |
248 proof (cases ts) |
200 case Nil |
249 case Nil |
201 with eq have "(Var x \<degree>\<degree> [])[u/i] \<rightarrow>\<^sub>\<beta>\<^sup>* u" by simp |
250 with eq have "(Var x \<degree>\<degree> [])[u/i] \<rightarrow>\<^sub>\<beta>\<^sup>* u" by simp |
202 with Nil and uNF show ?thesis by simp iprover |
251 with Nil and uNF show ?thesis by simp iprover |
203 next |
252 next |
204 case (Cons a as) |
253 case (Cons a as) |
205 with appT have "e\<langle>i:T\<rangle> \<turnstile> Var x \<degree>\<degree> (a # as) : T'" by simp |
254 with argsT obtain T'' Ts where Us: "Us = T'' # Ts" |
206 then obtain Us |
|
207 where varT': "e\<langle>i:T\<rangle> \<turnstile> Var x : Us \<Rrightarrow> T'" |
|
208 and argsT': "e\<langle>i:T\<rangle> \<tturnstile> a # as : Us" |
|
209 by (rule var_app_typesE) |
|
210 from argsT' obtain T'' Ts where Us: "Us = T'' # Ts" |
|
211 by (cases Us) (rule FalseE, simp+) |
255 by (cases Us) (rule FalseE, simp+) |
212 from varT' and Us have varT: "e\<langle>i:T\<rangle> \<turnstile> Var x : T'' \<Rightarrow> Ts \<Rrightarrow> T'" |
256 from varT and Us have varT: "e\<langle>i:T\<rangle> \<turnstile> Var x : T'' \<Rightarrow> Ts \<Rrightarrow> T'" |
213 by simp |
257 by simp |
214 from varT eq have T: "T = T'' \<Rightarrow> Ts \<Rrightarrow> T'" by cases auto |
258 from varT eq have T: "T = T'' \<Rightarrow> Ts \<Rrightarrow> T'" by cases auto |
215 with uT have uT': "e \<turnstile> u : T'' \<Rightarrow> Ts \<Rrightarrow> T'" by simp |
259 with uT have uT': "e \<turnstile> u : T'' \<Rightarrow> Ts \<Rrightarrow> T'" by simp |
216 from argsT' and Us have argsT: "e\<langle>i:T\<rangle> \<tturnstile> as : Ts" by simp |
260 from argsT Us Cons have argsT': "e\<langle>i:T\<rangle> \<tturnstile> as : Ts" by simp |
217 from argsT' and Us have argT: "e\<langle>i:T\<rangle> \<turnstile> a : T''" by simp |
261 from argsT Us Cons have argT: "e\<langle>i:T\<rangle> \<turnstile> a : T''" by simp |
218 from argT uT refl have aT: "e \<turnstile> a[u/i] : T''" by (rule subst_lemma) |
262 from argT uT refl have aT: "e \<turnstile> a[u/i] : T''" by (rule subst_lemma) |
219 have as: "\<And>Us. e\<langle>i:T\<rangle> \<tturnstile> as : Us \<Longrightarrow> listall ?R as \<Longrightarrow> |
|
220 \<exists>as'. Var 0 \<degree>\<degree> map (\<lambda>t. lift (t[u/i]) 0) as \<rightarrow>\<^sub>\<beta>\<^sup>* |
|
221 Var 0 \<degree>\<degree> map (\<lambda>t. lift t 0) as' \<and> |
|
222 Var 0 \<degree>\<degree> map (\<lambda>t. lift t 0) as' \<in> NF" |
|
223 (is "\<And>Us. _ \<Longrightarrow> _ \<Longrightarrow> \<exists>as'. ?ex Us as as'") |
|
224 proof (induct as rule: rev_induct) |
|
225 case (Nil Us) |
|
226 with Var_NF have "?ex Us [] []" by simp |
|
227 thus ?case .. |
|
228 next |
|
229 case (snoc b bs Us) |
|
230 have "e\<langle>i:T\<rangle> \<tturnstile> bs @ [b] : Us" . |
|
231 then obtain Vs W where Us: "Us = Vs @ [W]" |
|
232 and bs: "e\<langle>i:T\<rangle> \<tturnstile> bs : Vs" and bT: "e\<langle>i:T\<rangle> \<turnstile> b : W" by (rule types_snocE) |
|
233 from snoc have "listall ?R bs" by simp |
|
234 with bs have "\<exists>bs'. ?ex Vs bs bs'" by (rule snoc) |
|
235 then obtain bs' where |
|
236 bsred: "Var 0 \<degree>\<degree> map (\<lambda>t. lift (t[u/i]) 0) bs \<rightarrow>\<^sub>\<beta>\<^sup>* |
|
237 Var 0 \<degree>\<degree> map (\<lambda>t. lift t 0) bs'" |
|
238 and bsNF: "Var 0 \<degree>\<degree> map (\<lambda>t. lift t 0) bs' \<in> NF" by iprover |
|
239 from snoc have "?R b" by simp |
|
240 with bT and uNF and uT have "\<exists>b'. b[u/i] \<rightarrow>\<^sub>\<beta>\<^sup>* b' \<and> b' \<in> NF" by iprover |
|
241 then obtain b' where bred: "b[u/i] \<rightarrow>\<^sub>\<beta>\<^sup>* b'" and bNF: "b' \<in> NF" by iprover |
|
242 from bsNF have "listall (\<lambda>t. t \<in> NF) (map (\<lambda>t. lift t 0) bs')" |
|
243 by (rule App_NF_D) |
|
244 moreover have "lift b' 0 \<in> NF" by (rule lift_NF) |
|
245 ultimately have "listall (\<lambda>t. t \<in> NF) (map (\<lambda>t. lift t 0) (bs' @ [b']))" |
|
246 by simp |
|
247 hence "Var 0 \<degree>\<degree> map (\<lambda>t. lift t 0) (bs' @ [b']) \<in> NF" by (rule NF.App) |
|
248 moreover from bred have "lift (b[u/i]) 0 \<rightarrow>\<^sub>\<beta>\<^sup>* lift b' 0" |
|
249 by (rule lift_preserves_beta') |
|
250 with bsred have |
|
251 "(Var 0 \<degree>\<degree> map (\<lambda>t. lift (t[u/i]) 0) bs) \<degree> lift (b[u/i]) 0 \<rightarrow>\<^sub>\<beta>\<^sup>* |
|
252 (Var 0 \<degree>\<degree> map (\<lambda>t. lift t 0) bs') \<degree> lift b' 0" by (rule rtrancl_beta_App) |
|
253 ultimately have "?ex Us (bs @ [b]) (bs' @ [b'])" by simp |
|
254 thus ?case .. |
|
255 qed |
|
256 from App and Cons have "listall ?R as" by simp (iprover dest: listall_conj2) |
263 from App and Cons have "listall ?R as" by simp (iprover dest: listall_conj2) |
257 with argsT have "\<exists>as'. ?ex Ts as as'" by (rule as) |
264 with lift_preserves_beta' lift_NF uNF uT argsT' |
|
265 have "\<exists>as'. \<forall>j. Var j \<degree>\<degree> map (\<lambda>t. lift (t[u/i]) 0) as \<rightarrow>\<^sub>\<beta>\<^sup>* |
|
266 Var j \<degree>\<degree> map (\<lambda>t. lift t 0) as' \<and> |
|
267 Var j \<degree>\<degree> map (\<lambda>t. lift t 0) as' \<in> NF" by (rule norm_list) |
258 then obtain as' where |
268 then obtain as' where |
259 asred: "Var 0 \<degree>\<degree> map (\<lambda>t. lift (t[u/i]) 0) as \<rightarrow>\<^sub>\<beta>\<^sup>* |
269 asred: "Var 0 \<degree>\<degree> map (\<lambda>t. lift (t[u/i]) 0) as \<rightarrow>\<^sub>\<beta>\<^sup>* |
260 Var 0 \<degree>\<degree> map (\<lambda>t. lift t 0) as'" |
270 Var 0 \<degree>\<degree> map (\<lambda>t. lift t 0) as'" |
261 and asNF: "Var 0 \<degree>\<degree> map (\<lambda>t. lift t 0) as' \<in> NF" by iprover |
271 and asNF: "Var 0 \<degree>\<degree> map (\<lambda>t. lift t 0) as' \<in> NF" by iprover |
262 from App and Cons have "?R a" by simp |
272 from App and Cons have "?R a" by simp |
289 from T have "\<exists>r. (Var 0 \<degree>\<degree> map (\<lambda>t. lift t 0) as')[ua/0] \<rightarrow>\<^sub>\<beta>\<^sup>* r \<and> r \<in> NF" |
299 from T have "\<exists>r. (Var 0 \<degree>\<degree> map (\<lambda>t. lift t 0) as')[ua/0] \<rightarrow>\<^sub>\<beta>\<^sup>* r \<and> r \<in> NF" |
290 proof (rule MI2) |
300 proof (rule MI2) |
291 have "e\<langle>0:Ts \<Rrightarrow> T'\<rangle> \<turnstile> Var 0 \<degree>\<degree> map (\<lambda>t. lift (t[u/i]) 0) as : T'" |
301 have "e\<langle>0:Ts \<Rrightarrow> T'\<rangle> \<turnstile> Var 0 \<degree>\<degree> map (\<lambda>t. lift (t[u/i]) 0) as : T'" |
292 proof (rule list_app_typeI) |
302 proof (rule list_app_typeI) |
293 show "e\<langle>0:Ts \<Rrightarrow> T'\<rangle> \<turnstile> Var 0 : Ts \<Rrightarrow> T'" by (rule typing.Var) simp |
303 show "e\<langle>0:Ts \<Rrightarrow> T'\<rangle> \<turnstile> Var 0 : Ts \<Rrightarrow> T'" by (rule typing.Var) simp |
294 from uT argsT have "e \<tturnstile> map (\<lambda>t. t[u/i]) as : Ts" |
304 from uT argsT' have "e \<tturnstile> map (\<lambda>t. t[u/i]) as : Ts" |
295 by (rule substs_lemma) |
305 by (rule substs_lemma) |
296 hence "e\<langle>0:Ts \<Rrightarrow> T'\<rangle> \<tturnstile> map (\<lambda>t. lift t 0) (map (\<lambda>t. t[u/i]) as) : Ts" |
306 hence "e\<langle>0:Ts \<Rrightarrow> T'\<rangle> \<tturnstile> map (\<lambda>t. lift t 0) (map (\<lambda>t. t[u/i]) as) : Ts" |
297 by (rule lift_types) |
307 by (rule lift_types) |
298 thus "e\<langle>0:Ts \<Rrightarrow> T'\<rangle> \<tturnstile> map (\<lambda>t. lift (t[u/i]) 0) as : Ts" |
308 thus "e\<langle>0:Ts \<Rrightarrow> T'\<rangle> \<tturnstile> map (\<lambda>t. lift (t[u/i]) 0) as : Ts" |
299 by (simp_all add: map_compose [symmetric] o_def) |
309 by (simp_all add: map_compose [symmetric] o_def) |
317 with rnf Cons eq show ?thesis |
327 with rnf Cons eq show ?thesis |
318 by (simp add: map_compose [symmetric] o_def) iprover |
328 by (simp add: map_compose [symmetric] o_def) iprover |
319 qed |
329 qed |
320 next |
330 next |
321 assume neq: "x \<noteq> i" |
331 assume neq: "x \<noteq> i" |
322 show ?thesis |
332 from App have "listall ?R ts" by (iprover dest: listall_conj2) |
323 proof - |
333 with TrueI TrueI uNF uT argsT |
324 from appT obtain Us |
334 have "\<exists>ts'. \<forall>j. Var j \<degree>\<degree> map (\<lambda>t. t[u/i]) ts \<rightarrow>\<^sub>\<beta>\<^sup>* Var j \<degree>\<degree> ts' \<and> |
325 where varT: "e\<langle>i:T\<rangle> \<turnstile> Var x : Us \<Rrightarrow> T'" |
335 Var j \<degree>\<degree> ts' \<in> NF" (is "\<exists>ts'. ?ex ts'") |
326 and argsT: "e\<langle>i:T\<rangle> \<tturnstile> ts : Us" |
336 by (rule norm_list [of "\<lambda>t. t", simplified]) |
327 by (rule var_app_typesE) |
337 then obtain ts' where NF: "?ex ts'" .. |
328 have ts: "\<And>Us. e\<langle>i:T\<rangle> \<tturnstile> ts : Us \<Longrightarrow> listall ?R ts \<Longrightarrow> |
338 from nat_le_dec show ?thesis |
329 \<exists>ts'. \<forall>x'. Var x' \<degree>\<degree> map (\<lambda>t. t[u/i]) ts \<rightarrow>\<^sub>\<beta>\<^sup>* Var x' \<degree>\<degree> ts' \<and> |
339 proof |
330 Var x' \<degree>\<degree> ts' \<in> NF" |
340 assume "i < x" |
331 (is "\<And>Us. _ \<Longrightarrow> _ \<Longrightarrow> \<exists>ts'. ?ex Us ts ts'") |
341 with NF show ?thesis by simp iprover |
332 proof (induct ts rule: rev_induct) |
342 next |
333 case (Nil Us) |
343 assume "\<not> (i < x)" |
334 with Var_NF have "?ex Us [] []" by simp |
344 with NF neq show ?thesis by (simp add: subst_Var) iprover |
335 thus ?case .. |
|
336 next |
|
337 case (snoc b bs Us) |
|
338 have "e\<langle>i:T\<rangle> \<tturnstile> bs @ [b] : Us" . |
|
339 then obtain Vs W where Us: "Us = Vs @ [W]" |
|
340 and bs: "e\<langle>i:T\<rangle> \<tturnstile> bs : Vs" and bT: "e\<langle>i:T\<rangle> \<turnstile> b : W" by (rule types_snocE) |
|
341 from snoc have "listall ?R bs" by simp |
|
342 with bs have "\<exists>bs'. ?ex Vs bs bs'" by (rule snoc) |
|
343 then obtain bs' where |
|
344 bsred: "\<And>x'. Var x' \<degree>\<degree> map (\<lambda>t. t[u/i]) bs \<rightarrow>\<^sub>\<beta>\<^sup>* Var x' \<degree>\<degree> bs'" |
|
345 and bsNF: "\<And>x'. Var x' \<degree>\<degree> bs' \<in> NF" by iprover |
|
346 from snoc have "?R b" by simp |
|
347 with bT and uNF and uT have "\<exists>b'. b[u/i] \<rightarrow>\<^sub>\<beta>\<^sup>* b' \<and> b' \<in> NF" by iprover |
|
348 then obtain b' where bred: "b[u/i] \<rightarrow>\<^sub>\<beta>\<^sup>* b'" and bNF: "b' \<in> NF" by iprover |
|
349 from bsred bred have "\<And>x'. (Var x' \<degree>\<degree> map (\<lambda>t. t[u/i]) bs) \<degree> b[u/i] \<rightarrow>\<^sub>\<beta>\<^sup>* |
|
350 (Var x' \<degree>\<degree> bs') \<degree> b'" by (rule rtrancl_beta_App) |
|
351 moreover from bsNF [of 0] have "listall (\<lambda>t. t \<in> NF) bs'" |
|
352 by (rule App_NF_D) |
|
353 with bNF have "listall (\<lambda>t. t \<in> NF) (bs' @ [b'])" by simp |
|
354 hence "\<And>x'. Var x' \<degree>\<degree> (bs' @ [b']) \<in> NF" by (rule NF.App) |
|
355 ultimately have "?ex Us (bs @ [b]) (bs' @ [b'])" by simp |
|
356 thus ?case .. |
|
357 qed |
|
358 from App have "listall ?R ts" by (iprover dest: listall_conj2) |
|
359 with argsT have "\<exists>ts'. ?ex Ts ts ts'" by (rule ts) |
|
360 then obtain ts' where NF: "?ex Ts ts ts'" .. |
|
361 from nat_le_dec show ?thesis |
|
362 proof |
|
363 assume "i < x" |
|
364 with NF show ?thesis by simp iprover |
|
365 next |
|
366 assume "\<not> (i < x)" |
|
367 with NF neq show ?thesis by (simp add: subst_Var) iprover |
|
368 qed |
|
369 qed |
345 qed |
370 qed |
346 qed |
371 next |
347 next |
372 case (Abs r e_ T'_ u_ i_) |
348 case (Abs r e_ T'_ u_ i_) |
373 assume absT: "e\<langle>i:T\<rangle> \<turnstile> Abs r : T'" |
349 assume absT: "e\<langle>i:T\<rangle> \<turnstile> Abs r : T'" |