1 (* Title: HOL/Lambda/WeakNorm.thy
3 Author: Stefan Berghofer
4 Copyright 2003 TU Muenchen
7 header {* Weak normalization for simply-typed lambda calculus *}
10 imports Type Pretty_Int
14 Formalization by Stefan Berghofer. Partly based on a paper proof by
15 Felix Joachimski and Ralph Matthes \cite{Matthes-Joachimski-AML}.
19 subsection {* Terms in normal form *}
22 listall :: "('a \<Rightarrow> bool) \<Rightarrow> 'a list \<Rightarrow> bool" where
23 "listall P xs \<equiv> (\<forall>i. i < length xs \<longrightarrow> P (xs ! i))"
25 declare listall_def [extraction_expand]
27 theorem listall_nil: "listall P []"
28 by (simp add: listall_def)
30 theorem listall_nil_eq [simp]: "listall P [] = True"
31 by (iprover intro: listall_nil)
33 theorem listall_cons: "P x \<Longrightarrow> listall P xs \<Longrightarrow> listall P (x # xs)"
34 apply (simp add: listall_def)
35 apply (rule allI impI)+
40 theorem listall_cons_eq [simp]: "listall P (x # xs) = (P x \<and> listall P xs)"
44 apply (erule listall_cons)
46 apply (unfold listall_def)
48 apply (erule_tac x=0 in allE)
52 apply (erule_tac x="Suc i" in allE)
56 lemma listall_conj1: "listall (\<lambda>x. P x \<and> Q x) xs \<Longrightarrow> listall P xs"
57 by (induct xs) simp_all
59 lemma listall_conj2: "listall (\<lambda>x. P x \<and> Q x) xs \<Longrightarrow> listall Q xs"
60 by (induct xs) simp_all
62 lemma listall_app: "listall P (xs @ ys) = (listall P xs \<and> listall P ys)"
64 apply (rule iffI, simp, simp)
65 apply (rule iffI, simp, simp)
68 lemma listall_snoc [simp]: "listall P (xs @ [x]) = (listall P xs \<and> P x)"
70 apply (simp add: listall_app)+
73 lemma listall_cong [cong, extraction_expand]:
74 "xs = ys \<Longrightarrow> listall P xs = listall P ys"
75 -- {* Currently needed for strange technical reasons *}
76 by (unfold listall_def) simp
78 inductive NF :: "dB \<Rightarrow> bool"
80 App: "listall NF ts \<Longrightarrow> NF (Var x \<degree>\<degree> ts)"
81 | Abs: "NF t \<Longrightarrow> NF (Abs t)"
84 lemma nat_eq_dec: "\<And>n::nat. m = n \<or> m \<noteq> n"
87 apply (case_tac [3] n)
88 apply (simp only: nat.simps, iprover?)+
91 lemma nat_le_dec: "\<And>n::nat. m < n \<or> \<not> (m < n)"
94 apply (case_tac [3] n)
95 apply (simp del: simp_thms, iprover?)+
98 lemma App_NF_D: assumes NF: "NF (Var n \<degree>\<degree> ts)"
99 shows "listall NF ts" using NF
103 subsection {* Properties of @{text NF} *}
105 lemma Var_NF: "NF (Var n)"
106 apply (subgoal_tac "NF (Var n \<degree>\<degree> [])")
112 lemma subst_terms_NF: "listall NF ts \<Longrightarrow>
113 listall (\<lambda>t. \<forall>i j. NF (t[Var i/j])) ts \<Longrightarrow>
114 listall NF (map (\<lambda>t. t[Var i/j]) ts)"
115 by (induct ts) simp_all
117 lemma subst_Var_NF: "NF t \<Longrightarrow> NF (t[Var i/j])"
118 apply (induct arbitrary: i j set: NF)
120 apply (frule listall_conj1)
121 apply (drule listall_conj2)
122 apply (drule_tac i=i and j=j in subst_terms_NF)
124 apply (rule_tac m=x and n=j in nat_eq_dec [THEN disjE, standard])
127 apply (rule_tac m=j and n=x in nat_le_dec [THEN disjE, standard])
129 apply (iprover intro: NF.App)
131 apply (iprover intro: NF.App)
133 apply (iprover intro: NF.Abs)
136 lemma app_Var_NF: "NF t \<Longrightarrow> \<exists>t'. t \<degree> Var i \<rightarrow>\<^sub>\<beta>\<^sup>* t' \<and> NF t'"
137 apply (induct set: NF)
138 apply (simplesubst app_last) --{*Using @{text subst} makes extraction fail*}
141 apply (rule rtranclp.rtrancl_refl)
143 apply (drule listall_conj1)
144 apply (simp add: listall_app)
148 apply (rule rtranclp.rtrancl_into_rtrancl)
149 apply (rule rtranclp.rtrancl_refl)
151 apply (erule subst_Var_NF)
154 lemma lift_terms_NF: "listall NF ts \<Longrightarrow>
155 listall (\<lambda>t. \<forall>i. NF (lift t i)) ts \<Longrightarrow>
156 listall NF (map (\<lambda>t. lift t i) ts)"
157 by (induct ts) simp_all
159 lemma lift_NF: "NF t \<Longrightarrow> NF (lift t i)"
160 apply (induct arbitrary: i set: NF)
161 apply (frule listall_conj1)
162 apply (drule listall_conj2)
163 apply (drule_tac i=i in lift_terms_NF)
165 apply (rule_tac m=x and n=i in nat_le_dec [THEN disjE, standard])
178 subsection {* Main theorems *}
181 assumes f_compat: "\<And>t t'. t \<rightarrow>\<^sub>\<beta>\<^sup>* t' \<Longrightarrow> f t \<rightarrow>\<^sub>\<beta>\<^sup>* f t'"
182 and f_NF: "\<And>t. NF t \<Longrightarrow> NF (f t)"
183 and uNF: "NF u" and uT: "e \<turnstile> u : T"
184 shows "\<And>Us. e\<langle>i:T\<rangle> \<tturnstile> as : Us \<Longrightarrow>
185 listall (\<lambda>t. \<forall>e T' u i. e\<langle>i:T\<rangle> \<turnstile> t : T' \<longrightarrow>
186 NF u \<longrightarrow> e \<turnstile> u : T \<longrightarrow> (\<exists>t'. t[u/i] \<rightarrow>\<^sub>\<beta>\<^sup>* t' \<and> NF t')) as \<Longrightarrow>
187 \<exists>as'. \<forall>j. Var j \<degree>\<degree> map (\<lambda>t. f (t[u/i])) as \<rightarrow>\<^sub>\<beta>\<^sup>*
188 Var j \<degree>\<degree> map f as' \<and> NF (Var j \<degree>\<degree> map f as')"
189 (is "\<And>Us. _ \<Longrightarrow> listall ?R as \<Longrightarrow> \<exists>as'. ?ex Us as as'")
190 proof (induct as rule: rev_induct)
192 with Var_NF have "?ex Us [] []" by simp
196 have "e\<langle>i:T\<rangle> \<tturnstile> bs @ [b] : Us" by fact
197 then obtain Vs W where Us: "Us = Vs @ [W]"
198 and bs: "e\<langle>i:T\<rangle> \<tturnstile> bs : Vs" and bT: "e\<langle>i:T\<rangle> \<turnstile> b : W"
199 by (rule types_snocE)
200 from snoc have "listall ?R bs" by simp
201 with bs have "\<exists>bs'. ?ex Vs bs bs'" by (rule snoc)
202 then obtain bs' where
203 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'"
204 and bsNF: "\<And>j. NF (Var j \<degree>\<degree> map f bs')" by iprover
205 from snoc have "?R b" by simp
206 with bT and uNF and uT have "\<exists>b'. b[u/i] \<rightarrow>\<^sub>\<beta>\<^sup>* b' \<and> NF b'"
208 then obtain b' where bred: "b[u/i] \<rightarrow>\<^sub>\<beta>\<^sup>* b'" and bNF: "NF b'"
210 from bsNF [of 0] have "listall NF (map f bs')"
212 moreover have "NF (f b')" using bNF by (rule f_NF)
213 ultimately have "listall NF (map f (bs' @ [b']))"
215 hence "\<And>j. NF (Var j \<degree>\<degree> map f (bs' @ [b']))" by (rule NF.App)
216 moreover from bred have "f (b[u/i]) \<rightarrow>\<^sub>\<beta>\<^sup>* f b'"
219 "\<And>j. (Var j \<degree>\<degree> map (\<lambda>t. f (t[u/i])) bs) \<degree> f (b[u/i]) \<rightarrow>\<^sub>\<beta>\<^sup>*
220 (Var j \<degree>\<degree> map f bs') \<degree> f b'" by (rule rtrancl_beta_App)
221 ultimately have "?ex Us (bs @ [b]) (bs' @ [b'])" by simp
226 "\<And>t e T u i. NF t \<Longrightarrow> e\<langle>i:U\<rangle> \<turnstile> t : T \<Longrightarrow> NF u \<Longrightarrow> e \<turnstile> u : U \<Longrightarrow> \<exists>t'. t[u/i] \<rightarrow>\<^sub>\<beta>\<^sup>* t' \<and> NF t'"
227 (is "PROP ?P U" is "\<And>t e T u i. _ \<Longrightarrow> PROP ?Q t e T u i U")
230 let ?R = "\<lambda>t. \<forall>e T' u i.
231 e\<langle>i:T\<rangle> \<turnstile> t : T' \<longrightarrow> NF u \<longrightarrow> e \<turnstile> u : T \<longrightarrow> (\<exists>t'. t[u/i] \<rightarrow>\<^sub>\<beta>\<^sup>* t' \<and> NF t')"
232 assume MI1: "\<And>T1 T2. T = T1 \<Rightarrow> T2 \<Longrightarrow> PROP ?P T1"
233 assume MI2: "\<And>T1 T2. T = T1 \<Rightarrow> T2 \<Longrightarrow> PROP ?P T2"
235 thus "\<And>e T' u i. PROP ?Q t e T' u i T"
237 fix e T' u i assume uNF: "NF u" and uT: "e \<turnstile> u : T"
239 case (App ts x e_ T'_ u_ i_)
240 assume "e\<langle>i:T\<rangle> \<turnstile> Var x \<degree>\<degree> ts : T'"
242 where varT: "e\<langle>i:T\<rangle> \<turnstile> Var x : Us \<Rrightarrow> T'"
243 and argsT: "e\<langle>i:T\<rangle> \<tturnstile> ts : Us"
244 by (rule var_app_typesE)
245 from nat_eq_dec show "\<exists>t'. (Var x \<degree>\<degree> ts)[u/i] \<rightarrow>\<^sub>\<beta>\<^sup>* t' \<and> NF t'"
251 with eq have "(Var x \<degree>\<degree> [])[u/i] \<rightarrow>\<^sub>\<beta>\<^sup>* u" by simp
252 with Nil and uNF show ?thesis by simp iprover
255 with argsT obtain T'' Ts where Us: "Us = T'' # Ts"
256 by (cases Us) (rule FalseE, simp+, erule that)
257 from varT and Us have varT: "e\<langle>i:T\<rangle> \<turnstile> Var x : T'' \<Rightarrow> Ts \<Rrightarrow> T'"
259 from varT eq have T: "T = T'' \<Rightarrow> Ts \<Rrightarrow> T'" by cases auto
260 with uT have uT': "e \<turnstile> u : T'' \<Rightarrow> Ts \<Rrightarrow> T'" by simp
261 from argsT Us Cons have argsT': "e\<langle>i:T\<rangle> \<tturnstile> as : Ts" by simp
262 from argsT Us Cons have argT: "e\<langle>i:T\<rangle> \<turnstile> a : T''" by simp
263 from argT uT refl have aT: "e \<turnstile> a[u/i] : T''" by (rule subst_lemma)
264 from App and Cons have "listall ?R as" by simp (iprover dest: listall_conj2)
265 with lift_preserves_beta' lift_NF uNF uT argsT'
266 have "\<exists>as'. \<forall>j. Var j \<degree>\<degree> map (\<lambda>t. lift (t[u/i]) 0) as \<rightarrow>\<^sub>\<beta>\<^sup>*
267 Var j \<degree>\<degree> map (\<lambda>t. lift t 0) as' \<and>
268 NF (Var j \<degree>\<degree> map (\<lambda>t. lift t 0) as')" by (rule norm_list)
269 then obtain as' where
270 asred: "Var 0 \<degree>\<degree> map (\<lambda>t. lift (t[u/i]) 0) as \<rightarrow>\<^sub>\<beta>\<^sup>*
271 Var 0 \<degree>\<degree> map (\<lambda>t. lift t 0) as'"
272 and asNF: "NF (Var 0 \<degree>\<degree> map (\<lambda>t. lift t 0) as')" by iprover
273 from App and Cons have "?R a" by simp
274 with argT and uNF and uT have "\<exists>a'. a[u/i] \<rightarrow>\<^sub>\<beta>\<^sup>* a' \<and> NF a'"
276 then obtain a' where ared: "a[u/i] \<rightarrow>\<^sub>\<beta>\<^sup>* a'" and aNF: "NF a'" by iprover
277 from uNF have "NF (lift u 0)" by (rule lift_NF)
278 hence "\<exists>u'. lift u 0 \<degree> Var 0 \<rightarrow>\<^sub>\<beta>\<^sup>* u' \<and> NF u'" by (rule app_Var_NF)
279 then obtain u' where ured: "lift u 0 \<degree> Var 0 \<rightarrow>\<^sub>\<beta>\<^sup>* u'" and u'NF: "NF u'"
281 from T and u'NF have "\<exists>ua. u'[a'/0] \<rightarrow>\<^sub>\<beta>\<^sup>* ua \<and> NF ua"
283 have "e\<langle>0:T''\<rangle> \<turnstile> lift u 0 \<degree> Var 0 : Ts \<Rrightarrow> T'"
284 proof (rule typing.App)
285 from uT' show "e\<langle>0:T''\<rangle> \<turnstile> lift u 0 : T'' \<Rightarrow> Ts \<Rrightarrow> T'" by (rule lift_type)
286 show "e\<langle>0:T''\<rangle> \<turnstile> Var 0 : T''" by (rule typing.Var) simp
288 with ured show "e\<langle>0:T''\<rangle> \<turnstile> u' : Ts \<Rrightarrow> T'" by (rule subject_reduction')
289 from ared aT show "e \<turnstile> a' : T''" by (rule subject_reduction')
292 then obtain ua where uared: "u'[a'/0] \<rightarrow>\<^sub>\<beta>\<^sup>* ua" and uaNF: "NF ua"
294 from ared have "(lift u 0 \<degree> Var 0)[a[u/i]/0] \<rightarrow>\<^sub>\<beta>\<^sup>* (lift u 0 \<degree> Var 0)[a'/0]"
295 by (rule subst_preserves_beta2')
296 also from ured have "(lift u 0 \<degree> Var 0)[a'/0] \<rightarrow>\<^sub>\<beta>\<^sup>* u'[a'/0]"
297 by (rule subst_preserves_beta')
299 finally have "(lift u 0 \<degree> Var 0)[a[u/i]/0] \<rightarrow>\<^sub>\<beta>\<^sup>* ua" .
300 hence uared': "u \<degree> a[u/i] \<rightarrow>\<^sub>\<beta>\<^sup>* ua" by simp
301 from T asNF _ uaNF have "\<exists>r. (Var 0 \<degree>\<degree> map (\<lambda>t. lift t 0) as')[ua/0] \<rightarrow>\<^sub>\<beta>\<^sup>* r \<and> NF r"
303 have "e\<langle>0:Ts \<Rrightarrow> T'\<rangle> \<turnstile> Var 0 \<degree>\<degree> map (\<lambda>t. lift (t[u/i]) 0) as : T'"
304 proof (rule list_app_typeI)
305 show "e\<langle>0:Ts \<Rrightarrow> T'\<rangle> \<turnstile> Var 0 : Ts \<Rrightarrow> T'" by (rule typing.Var) simp
306 from uT argsT' have "e \<tturnstile> map (\<lambda>t. t[u/i]) as : Ts"
307 by (rule substs_lemma)
308 hence "e\<langle>0:Ts \<Rrightarrow> T'\<rangle> \<tturnstile> map (\<lambda>t. lift t 0) (map (\<lambda>t. t[u/i]) as) : Ts"
310 thus "e\<langle>0:Ts \<Rrightarrow> T'\<rangle> \<tturnstile> map (\<lambda>t. lift (t[u/i]) 0) as : Ts"
311 by (simp_all add: map_compose [symmetric] o_def)
313 with asred show "e\<langle>0:Ts \<Rrightarrow> T'\<rangle> \<turnstile> Var 0 \<degree>\<degree> map (\<lambda>t. lift t 0) as' : T'"
314 by (rule subject_reduction')
315 from argT uT refl have "e \<turnstile> a[u/i] : T''" by (rule subst_lemma)
316 with uT' have "e \<turnstile> u \<degree> a[u/i] : Ts \<Rrightarrow> T'" by (rule typing.App)
317 with uared' show "e \<turnstile> ua : Ts \<Rrightarrow> T'" by (rule subject_reduction')
319 then obtain r where rred: "(Var 0 \<degree>\<degree> map (\<lambda>t. lift t 0) as')[ua/0] \<rightarrow>\<^sub>\<beta>\<^sup>* r"
320 and rnf: "NF r" by iprover
322 "(Var 0 \<degree>\<degree> map (\<lambda>t. lift (t[u/i]) 0) as)[u \<degree> a[u/i]/0] \<rightarrow>\<^sub>\<beta>\<^sup>*
323 (Var 0 \<degree>\<degree> map (\<lambda>t. lift t 0) as')[u \<degree> a[u/i]/0]"
324 by (rule subst_preserves_beta')
325 also from uared' have "(Var 0 \<degree>\<degree> map (\<lambda>t. lift t 0) as')[u \<degree> a[u/i]/0] \<rightarrow>\<^sub>\<beta>\<^sup>*
326 (Var 0 \<degree>\<degree> map (\<lambda>t. lift t 0) as')[ua/0]" by (rule subst_preserves_beta2')
328 finally have "(Var 0 \<degree>\<degree> map (\<lambda>t. lift (t[u/i]) 0) as)[u \<degree> a[u/i]/0] \<rightarrow>\<^sub>\<beta>\<^sup>* r" .
329 with rnf Cons eq show ?thesis
330 by (simp add: map_compose [symmetric] o_def) iprover
333 assume neq: "x \<noteq> i"
334 from App have "listall ?R ts" by (iprover dest: listall_conj2)
335 with TrueI TrueI uNF uT argsT
336 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>
337 NF (Var j \<degree>\<degree> ts')" (is "\<exists>ts'. ?ex ts'")
338 by (rule norm_list [of "\<lambda>t. t", simplified])
339 then obtain ts' where NF: "?ex ts'" ..
340 from nat_le_dec show ?thesis
343 with NF show ?thesis by simp iprover
345 assume "\<not> (i < x)"
346 with NF neq show ?thesis by (simp add: subst_Var) iprover
350 case (Abs r e_ T'_ u_ i_)
351 assume absT: "e\<langle>i:T\<rangle> \<turnstile> Abs r : T'"
352 then obtain R S where "e\<langle>0:R\<rangle>\<langle>Suc i:T\<rangle> \<turnstile> r : S" by (rule abs_typeE) simp
353 moreover have "NF (lift u 0)" using `NF u` by (rule lift_NF)
354 moreover have "e\<langle>0:R\<rangle> \<turnstile> lift u 0 : T" using uT by (rule lift_type)
355 ultimately have "\<exists>t'. r[lift u 0/Suc i] \<rightarrow>\<^sub>\<beta>\<^sup>* t' \<and> NF t'" by (rule Abs)
356 thus "\<exists>t'. Abs r[u/i] \<rightarrow>\<^sub>\<beta>\<^sup>* t' \<and> NF t'"
357 by simp (iprover intro: rtrancl_beta_Abs NF.Abs)
363 -- {* A computationally relevant copy of @{term "e \<turnstile> t : T"} *}
364 inductive rtyping :: "(nat \<Rightarrow> type) \<Rightarrow> dB \<Rightarrow> type \<Rightarrow> bool" ("_ \<turnstile>\<^sub>R _ : _" [50, 50, 50] 50)
366 Var: "e x = T \<Longrightarrow> e \<turnstile>\<^sub>R Var x : T"
367 | Abs: "e\<langle>0:T\<rangle> \<turnstile>\<^sub>R t : U \<Longrightarrow> e \<turnstile>\<^sub>R Abs t : (T \<Rightarrow> U)"
368 | App: "e \<turnstile>\<^sub>R s : T \<Rightarrow> U \<Longrightarrow> e \<turnstile>\<^sub>R t : T \<Longrightarrow> e \<turnstile>\<^sub>R (s \<degree> t) : U"
370 lemma rtyping_imp_typing: "e \<turnstile>\<^sub>R t : T \<Longrightarrow> e \<turnstile> t : T"
371 apply (induct set: rtyping)
372 apply (erule typing.Var)
373 apply (erule typing.Abs)
374 apply (erule typing.App)
380 assumes "e \<turnstile>\<^sub>R t : T"
381 shows "\<exists>t'. t \<rightarrow>\<^sub>\<beta>\<^sup>* t' \<and> NF t'" using assms
384 show ?case by (iprover intro: Var_NF)
387 thus ?case by (iprover intro: rtrancl_beta_Abs NF.Abs)
390 from App obtain s' t' where
391 sred: "s \<rightarrow>\<^sub>\<beta>\<^sup>* s'" and "NF s'"
392 and tred: "t \<rightarrow>\<^sub>\<beta>\<^sup>* t'" and tNF: "NF t'" by iprover
393 have "\<exists>u. (Var 0 \<degree> lift t' 0)[s'/0] \<rightarrow>\<^sub>\<beta>\<^sup>* u \<and> NF u"
394 proof (rule subst_type_NF)
395 have "NF (lift t' 0)" using tNF by (rule lift_NF)
396 hence "listall NF [lift t' 0]" by (rule listall_cons) (rule listall_nil)
397 hence "NF (Var 0 \<degree>\<degree> [lift t' 0])" by (rule NF.App)
398 thus "NF (Var 0 \<degree> lift t' 0)" by simp
399 show "e\<langle>0:T \<Rightarrow> U\<rangle> \<turnstile> Var 0 \<degree> lift t' 0 : U"
400 proof (rule typing.App)
401 show "e\<langle>0:T \<Rightarrow> U\<rangle> \<turnstile> Var 0 : T \<Rightarrow> U"
402 by (rule typing.Var) simp
403 from tred have "e \<turnstile> t' : T"
404 by (rule subject_reduction') (rule rtyping_imp_typing, rule App.hyps)
405 thus "e\<langle>0:T \<Rightarrow> U\<rangle> \<turnstile> lift t' 0 : T"
408 from sred show "e \<turnstile> s' : T \<Rightarrow> U"
409 by (rule subject_reduction') (rule rtyping_imp_typing, rule App.hyps)
412 then obtain u where ured: "s' \<degree> t' \<rightarrow>\<^sub>\<beta>\<^sup>* u" and unf: "NF u" by simp iprover
413 from sred tred have "s \<degree> t \<rightarrow>\<^sub>\<beta>\<^sup>* s' \<degree> t'" by (rule rtrancl_beta_App)
414 hence "s \<degree> t \<rightarrow>\<^sub>\<beta>\<^sup>* u" using ured by (rule rtranclp_trans)
415 with unf show ?case by iprover
419 subsection {* Extracting the program *}
421 declare NF.induct [ind_realizer]
422 declare rtranclp.induct [ind_realizer irrelevant]
423 declare rtyping.induct [ind_realizer]
424 lemmas [extraction_expand] = conj_assoc listall_cons_eq
428 lemma rtranclR_rtrancl_eq: "rtranclpR r a b = r\<^sup>*\<^sup>* a b"
430 apply (erule rtranclpR.induct)
431 apply (rule rtranclp.rtrancl_refl)
432 apply (erule rtranclp.rtrancl_into_rtrancl)
434 apply (erule rtranclp.induct)
435 apply (rule rtranclpR.rtrancl_refl)
436 apply (erule rtranclpR.rtrancl_into_rtrancl)
440 lemma NFR_imp_NF: "NFR nf t \<Longrightarrow> NF t"
441 apply (erule NFR.induct)
442 apply (rule NF.intros)
443 apply (simp add: listall_def)
444 apply (erule NF.intros)
449 \renewcommand{\isastyle}{\scriptsize\it}%
450 @{thm [display,eta_contract=false,margin=100] subst_type_NF_def}
451 \renewcommand{\isastyle}{\small\it}%
452 \caption{Program extracted from @{text subst_type_NF}}
453 \label{fig:extr-subst-type-nf}
457 \renewcommand{\isastyle}{\scriptsize\it}%
458 @{thm [display,margin=100] subst_Var_NF_def}
459 @{thm [display,margin=100] app_Var_NF_def}
460 @{thm [display,margin=100] lift_NF_def}
461 @{thm [display,eta_contract=false,margin=100] type_NF_def}
462 \renewcommand{\isastyle}{\small\it}%
463 \caption{Program extracted from lemmas and main theorem}
464 \label{fig:extr-type-nf}
469 The program corresponding to the proof of the central lemma, which
470 performs substitution and normalization, is shown in Figure
471 \ref{fig:extr-subst-type-nf}. The correctness
472 theorem corresponding to the program @{text "subst_type_NF"} is
473 @{thm [display,margin=100] subst_type_NF_correctness
474 [simplified rtranclR_rtrancl_eq Collect_mem_eq, no_vars]}
475 where @{text NFR} is the realizability predicate corresponding to
476 the datatype @{text NFT}, which is inductively defined by the rules
478 @{thm [display,margin=90] NFR.App [of ts nfs x] NFR.Abs [of nf t]}
480 The programs corresponding to the main theorem @{text "type_NF"}, as
481 well as to some lemmas, are shown in Figure \ref{fig:extr-type-nf}.
482 The correctness statement for the main function @{text "type_NF"} is
483 @{thm [display,margin=100] type_NF_correctness
484 [simplified rtranclR_rtrancl_eq Collect_mem_eq, no_vars]}
485 where the realizability predicate @{text "rtypingR"} corresponding to the
486 computationally relevant version of the typing judgement is inductively
488 @{thm [display,margin=100] rtypingR.Var [no_vars]
489 rtypingR.Abs [of ty, no_vars] rtypingR.App [of ty e s T U ty' t]}
492 subsection {* Generating executable code *}
495 "arbitrary :: 'a" ("(error \"arbitrary\")")
496 "arbitrary :: 'a \<Rightarrow> 'b" ("(fn '_ => error \"arbitrary\")")
503 The following functions convert between Isabelle's built-in {\tt term}
504 datatype and the generated {\tt dB} datatype. This allows to
505 generate example terms using Isabelle's parser and inspect
506 normalized terms using Isabelle's pretty printer.
510 fun nat_of_int 0 = Norm.zero
511 | nat_of_int n = Norm.Suc (nat_of_int (n-1));
513 fun int_of_nat Norm.zero = 0
514 | int_of_nat (Norm.Suc n) = 1 + int_of_nat n;
516 fun dBtype_of_typ (Type ("fun", [T, U])) =
517 Norm.Fun (dBtype_of_typ T, dBtype_of_typ U)
518 | dBtype_of_typ (TFree (s, _)) = (case explode s of
519 ["'", a] => Norm.Atom (nat_of_int (ord a - 97))
520 | _ => error "dBtype_of_typ: variable name")
521 | dBtype_of_typ _ = error "dBtype_of_typ: bad type";
523 fun dB_of_term (Bound i) = Norm.dB_Var (nat_of_int i)
524 | dB_of_term (t $ u) = Norm.App (dB_of_term t, dB_of_term u)
525 | dB_of_term (Abs (_, _, t)) = Norm.Abs (dB_of_term t)
526 | dB_of_term _ = error "dB_of_term: bad term";
528 fun term_of_dB Ts (Type ("fun", [T, U])) (Norm.Abs dBt) =
529 Abs ("x", T, term_of_dB (T :: Ts) U dBt)
530 | term_of_dB Ts _ dBt = term_of_dB' Ts dBt
531 and term_of_dB' Ts (Norm.dB_Var n) = Bound (int_of_nat n)
532 | term_of_dB' Ts (Norm.App (dBt, dBu)) =
533 let val t = term_of_dB' Ts dBt
534 in case fastype_of1 (Ts, t) of
535 Type ("fun", [T, U]) => t $ term_of_dB Ts T dBu
536 | _ => error "term_of_dB: function type expected"
538 | term_of_dB' _ _ = error "term_of_dB: term not in normal form";
540 fun typing_of_term Ts e (Bound i) =
541 Norm.Var (e, nat_of_int i, dBtype_of_typ (List.nth (Ts, i)))
542 | typing_of_term Ts e (t $ u) = (case fastype_of1 (Ts, t) of
543 Type ("fun", [T, U]) => Norm.rtypingT_App (e, dB_of_term t,
544 dBtype_of_typ T, dBtype_of_typ U, dB_of_term u,
545 typing_of_term Ts e t, typing_of_term Ts e u)
546 | _ => error "typing_of_term: function type expected")
547 | typing_of_term Ts e (Abs (s, T, t)) =
548 let val dBT = dBtype_of_typ T
549 in Norm.rtypingT_Abs (e, dBT, dB_of_term t,
550 dBtype_of_typ (fastype_of1 (T :: Ts, t)),
551 typing_of_term (T :: Ts) (Norm.shift e Norm.zero dBT) t)
553 | typing_of_term _ _ _ = error "typing_of_term: bad term";
555 fun dummyf _ = error "dummy";
559 We now try out the extracted program @{text "type_NF"} on some example terms.
563 val ct1 = @{cterm "%f. ((%f x. f (f (f x))) ((%f x. f (f (f (f x)))) f))"};
564 val (dB1, _) = Norm.type_NF (typing_of_term [] dummyf (term_of ct1));
565 val ct1' = cterm_of @{theory} (term_of_dB [] (#T (rep_cterm ct1)) dB1);
567 val ct2 = @{cterm "%f x. (%x. f x x) ((%x. f x x) ((%x. f x x) ((%x. f x x) ((%x. f x x) ((%x. f x x) x)))))"};
568 val (dB2, _) = Norm.type_NF (typing_of_term [] dummyf (term_of ct2));
569 val ct2' = cterm_of @{theory} (term_of_dB [] (#T (rep_cterm ct2)) dB2);
573 The same story again for code next generation.
577 CodeTarget.add_undefined "SML" "arbitrary" "(raise Fail \"arbitrary\")"
581 int :: "nat \<Rightarrow> int" where
582 "int \<equiv> of_nat"
584 code_gen type_NF nat int in SML module_name Norm
587 val nat_of_int = Norm.nat o IntInf.fromInt;
588 val int_of_nat = IntInf.toInt o Norm.int;
590 fun dBtype_of_typ (Type ("fun", [T, U])) =
591 Norm.Fun (dBtype_of_typ T, dBtype_of_typ U)
592 | dBtype_of_typ (TFree (s, _)) = (case explode s of
593 ["'", a] => Norm.Atom (nat_of_int (ord a - 97))
594 | _ => error "dBtype_of_typ: variable name")
595 | dBtype_of_typ _ = error "dBtype_of_typ: bad type";
597 fun dB_of_term (Bound i) = Norm.Var (nat_of_int i)
598 | dB_of_term (t $ u) = Norm.App (dB_of_term t, dB_of_term u)
599 | dB_of_term (Abs (_, _, t)) = Norm.Abs (dB_of_term t)
600 | dB_of_term _ = error "dB_of_term: bad term";
602 fun term_of_dB Ts (Type ("fun", [T, U])) (Norm.Abs dBt) =
603 Abs ("x", T, term_of_dB (T :: Ts) U dBt)
604 | term_of_dB Ts _ dBt = term_of_dB' Ts dBt
605 and term_of_dB' Ts (Norm.Var n) = Bound (int_of_nat n)
606 | term_of_dB' Ts (Norm.App (dBt, dBu)) =
607 let val t = term_of_dB' Ts dBt
608 in case fastype_of1 (Ts, t) of
609 Type ("fun", [T, U]) => t $ term_of_dB Ts T dBu
610 | _ => error "term_of_dB: function type expected"
612 | term_of_dB' _ _ = error "term_of_dB: term not in normal form";
614 fun typing_of_term Ts e (Bound i) =
615 Norm.Vara (e, nat_of_int i, dBtype_of_typ (nth Ts i))
616 | typing_of_term Ts e (t $ u) = (case fastype_of1 (Ts, t) of
617 Type ("fun", [T, U]) => Norm.Appaa (e, dB_of_term t,
618 dBtype_of_typ T, dBtype_of_typ U, dB_of_term u,
619 typing_of_term Ts e t, typing_of_term Ts e u)
620 | _ => error "typing_of_term: function type expected")
621 | typing_of_term Ts e (Abs (s, T, t)) =
622 let val dBT = dBtype_of_typ T
623 in Norm.Absaa (e, dBT, dB_of_term t,
624 dBtype_of_typ (fastype_of1 (T :: Ts, t)),
625 typing_of_term (T :: Ts) (Norm.shift e Norm.Zero_nat dBT) t)
627 | typing_of_term _ _ _ = error "typing_of_term: bad term";
629 fun dummyf _ = error "dummy";
633 val ct1 = @{cterm "%f. ((%f x. f (f (f x))) ((%f x. f (f (f (f x)))) f))"};
634 val (dB1, _) = Norm.type_NF (typing_of_term [] dummyf (term_of ct1));
635 val ct1' = cterm_of @{theory} (term_of_dB [] (#T (rep_cterm ct1)) dB1);
637 val ct2 = @{cterm "%f x. (%x. f x x) ((%x. f x x) ((%x. f x x) ((%x. f x x) ((%x. f x x) ((%x. f x x) x)))))"};
638 val (dB2, _) = Norm.type_NF (typing_of_term [] dummyf (term_of ct2));
639 val ct2' = cterm_of @{theory} (term_of_dB [] (#T (rep_cterm ct2)) dB2);