src/HOL/Lambda/WeakNorm.thy
author haftmann
Fri, 23 Mar 2007 12:05:43 +0100
changeset 22512 04242efdcece
parent 22499 68c8a8390e16
child 22513 651777b8dc30
permissions -rw-r--r--
fixed typing bug in generated code
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
14063
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
     1
(*  Title:      HOL/Lambda/WeakNorm.thy
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
     2
    ID:         $Id$
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
     3
    Author:     Stefan Berghofer
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
     4
    Copyright   2003 TU Muenchen
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
     5
*)
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
     6
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
     7
header {* Weak normalization for simply-typed lambda calculus *}
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
     8
22512
04242efdcece fixed typing bug in generated code
haftmann
parents: 22499
diff changeset
     9
theory WeakNorm
04242efdcece fixed typing bug in generated code
haftmann
parents: 22499
diff changeset
    10
imports Type
04242efdcece fixed typing bug in generated code
haftmann
parents: 22499
diff changeset
    11
begin
14063
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
    12
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
    13
text {*
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
    14
Formalization by Stefan Berghofer. Partly based on a paper proof by
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
    15
Felix Joachimski and Ralph Matthes \cite{Matthes-Joachimski-AML}.
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
    16
*}
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
    17
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
    18
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
    19
subsection {* Terms in normal form *}
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
    20
19086
1b3780be6cc2 new-style definitions/abbreviations;
wenzelm
parents: 18513
diff changeset
    21
definition
21404
eb85850d3eb7 more robust syntax for definition/abbreviation/notation;
wenzelm
parents: 21334
diff changeset
    22
  listall :: "('a \<Rightarrow> bool) \<Rightarrow> 'a list \<Rightarrow> bool" where
22499
68c8a8390e16 fixed code generator setup
haftmann
parents: 22271
diff changeset
    23
  [extraction_expand]: "listall P xs \<equiv> (\<forall>i. i < length xs \<longrightarrow> P (xs ! i))"
14063
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
    24
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
    25
theorem listall_nil: "listall P []"
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
    26
  by (simp add: listall_def)
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
    27
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
    28
theorem listall_nil_eq [simp]: "listall P [] = True"
17589
58eeffd73be1 renamed rules to iprover
nipkow
parents: 17145
diff changeset
    29
  by (iprover intro: listall_nil)
14063
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
    30
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
    31
theorem listall_cons: "P x \<Longrightarrow> listall P xs \<Longrightarrow> listall P (x # xs)"
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
    32
  apply (simp add: listall_def)
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
    33
  apply (rule allI impI)+
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
    34
  apply (case_tac i)
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
    35
  apply simp+
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
    36
  done
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
    37
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
    38
theorem listall_cons_eq [simp]: "listall P (x # xs) = (P x \<and> listall P xs)"
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
    39
  apply (rule iffI)
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
    40
  prefer 2
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
    41
  apply (erule conjE)
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
    42
  apply (erule listall_cons)
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
    43
  apply assumption
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
    44
  apply (unfold listall_def)
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
    45
  apply (rule conjI)
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
    46
  apply (erule_tac x=0 in allE)
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
    47
  apply simp
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
    48
  apply simp
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
    49
  apply (rule allI)
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
    50
  apply (erule_tac x="Suc i" in allE)
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
    51
  apply simp
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
    52
  done
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
    53
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
    54
lemma listall_conj1: "listall (\<lambda>x. P x \<and> Q x) xs \<Longrightarrow> listall P xs"
18241
afdba6b3e383 tuned induction proofs;
wenzelm
parents: 17589
diff changeset
    55
  by (induct xs) simp_all
14063
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
    56
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
    57
lemma listall_conj2: "listall (\<lambda>x. P x \<and> Q x) xs \<Longrightarrow> listall Q xs"
18241
afdba6b3e383 tuned induction proofs;
wenzelm
parents: 17589
diff changeset
    58
  by (induct xs) simp_all
14063
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
    59
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
    60
lemma listall_app: "listall P (xs @ ys) = (listall P xs \<and> listall P ys)"
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
    61
  apply (induct xs)
18241
afdba6b3e383 tuned induction proofs;
wenzelm
parents: 17589
diff changeset
    62
   apply (rule iffI, simp, simp)
14063
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
    63
  apply (rule iffI, simp, simp)
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
    64
  done
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
    65
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
    66
lemma listall_snoc [simp]: "listall P (xs @ [x]) = (listall P xs \<and> P x)"
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
    67
  apply (rule iffI)
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
    68
  apply (simp add: listall_app)+
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
    69
  done
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
    70
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
    71
lemma listall_cong [cong, extraction_expand]:
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
    72
  "xs = ys \<Longrightarrow> listall P xs = listall P ys"
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
    73
  -- {* Currently needed for strange technical reasons *}
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
    74
  by (unfold listall_def) simp
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
    75
22271
51a80e238b29 Adapted to new inductive definition package.
berghofe
parents: 21546
diff changeset
    76
inductive2 NF :: "dB \<Rightarrow> bool"
51a80e238b29 Adapted to new inductive definition package.
berghofe
parents: 21546
diff changeset
    77
where
51a80e238b29 Adapted to new inductive definition package.
berghofe
parents: 21546
diff changeset
    78
  App: "listall NF ts \<Longrightarrow> NF (Var x \<degree>\<degree> ts)"
51a80e238b29 Adapted to new inductive definition package.
berghofe
parents: 21546
diff changeset
    79
| Abs: "NF t \<Longrightarrow> NF (Abs t)"
14063
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
    80
monos listall_def
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
    81
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
    82
lemma nat_eq_dec: "\<And>n::nat. m = n \<or> m \<noteq> n"
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
    83
  apply (induct m)
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
    84
  apply (case_tac n)
15236
f289e8ba2bb3 Proofs needed to be updated because induction now preserves name of
nipkow
parents: 14860
diff changeset
    85
  apply (case_tac [3] n)
17589
58eeffd73be1 renamed rules to iprover
nipkow
parents: 17145
diff changeset
    86
  apply (simp only: nat.simps, iprover?)+
14063
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
    87
  done
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
    88
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
    89
lemma nat_le_dec: "\<And>n::nat. m < n \<or> \<not> (m < n)"
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
    90
  apply (induct m)
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
    91
  apply (case_tac n)
15236
f289e8ba2bb3 Proofs needed to be updated because induction now preserves name of
nipkow
parents: 14860
diff changeset
    92
  apply (case_tac [3] n)
17589
58eeffd73be1 renamed rules to iprover
nipkow
parents: 17145
diff changeset
    93
  apply (simp del: simp_thms, iprover?)+
14063
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
    94
  done
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
    95
22271
51a80e238b29 Adapted to new inductive definition package.
berghofe
parents: 21546
diff changeset
    96
lemma App_NF_D: assumes NF: "NF (Var n \<degree>\<degree> ts)"
51a80e238b29 Adapted to new inductive definition package.
berghofe
parents: 21546
diff changeset
    97
  shows "listall NF ts" using NF
14063
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
    98
  by cases simp_all
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
    99
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
   100
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
   101
subsection {* Properties of @{text NF} *}
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
   102
22271
51a80e238b29 Adapted to new inductive definition package.
berghofe
parents: 21546
diff changeset
   103
lemma Var_NF: "NF (Var n)"
51a80e238b29 Adapted to new inductive definition package.
berghofe
parents: 21546
diff changeset
   104
  apply (subgoal_tac "NF (Var n \<degree>\<degree> [])")
14063
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
   105
   apply simp
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
   106
  apply (rule NF.App)
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
   107
  apply simp
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
   108
  done
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
   109
22271
51a80e238b29 Adapted to new inductive definition package.
berghofe
parents: 21546
diff changeset
   110
lemma subst_terms_NF: "listall NF ts \<Longrightarrow>
51a80e238b29 Adapted to new inductive definition package.
berghofe
parents: 21546
diff changeset
   111
    listall (\<lambda>t. \<forall>i j. NF (t[Var i/j])) ts \<Longrightarrow>
51a80e238b29 Adapted to new inductive definition package.
berghofe
parents: 21546
diff changeset
   112
    listall NF (map (\<lambda>t. t[Var i/j]) ts)"
18513
791b53bf4073 tuned proofs;
wenzelm
parents: 18331
diff changeset
   113
  by (induct ts) simp_all
14063
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
   114
22271
51a80e238b29 Adapted to new inductive definition package.
berghofe
parents: 21546
diff changeset
   115
lemma subst_Var_NF: "NF t \<Longrightarrow> NF (t[Var i/j])"
20503
503ac4c5ef91 induct method: renamed 'fixing' to 'arbitrary';
wenzelm
parents: 20453
diff changeset
   116
  apply (induct arbitrary: i j set: NF)
14063
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
   117
  apply simp
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
   118
  apply (frule listall_conj1)
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
   119
  apply (drule listall_conj2)
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
   120
  apply (drule_tac i=i and j=j in subst_terms_NF)
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
   121
  apply assumption
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
   122
  apply (rule_tac m=x and n=j in nat_eq_dec [THEN disjE, standard])
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
   123
  apply simp
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
   124
  apply (erule NF.App)
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
   125
  apply (rule_tac m=j and n=x in nat_le_dec [THEN disjE, standard])
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
   126
  apply simp
17589
58eeffd73be1 renamed rules to iprover
nipkow
parents: 17145
diff changeset
   127
  apply (iprover intro: NF.App)
14063
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
   128
  apply simp
17589
58eeffd73be1 renamed rules to iprover
nipkow
parents: 17145
diff changeset
   129
  apply (iprover intro: NF.App)
14063
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
   130
  apply simp
17589
58eeffd73be1 renamed rules to iprover
nipkow
parents: 17145
diff changeset
   131
  apply (iprover intro: NF.Abs)
14063
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
   132
  done
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
   133
22271
51a80e238b29 Adapted to new inductive definition package.
berghofe
parents: 21546
diff changeset
   134
lemma app_Var_NF: "NF t \<Longrightarrow> \<exists>t'. t \<degree> Var i \<rightarrow>\<^sub>\<beta>\<^sup>* t' \<and> NF t'"
14063
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
   135
  apply (induct set: NF)
15948
d97c12a4f31b oops...cannot use subst here
paulson
parents: 15944
diff changeset
   136
  apply (simplesubst app_last)  --{*Using @{text subst} makes extraction fail*}
14063
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
   137
  apply (rule exI)
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
   138
  apply (rule conjI)
22271
51a80e238b29 Adapted to new inductive definition package.
berghofe
parents: 21546
diff changeset
   139
  apply (rule rtrancl.rtrancl_refl)
14063
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
   140
  apply (rule NF.App)
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
   141
  apply (drule listall_conj1)
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
   142
  apply (simp add: listall_app)
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
   143
  apply (rule Var_NF)
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
   144
  apply (rule exI)
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
   145
  apply (rule conjI)
22271
51a80e238b29 Adapted to new inductive definition package.
berghofe
parents: 21546
diff changeset
   146
  apply (rule rtrancl.rtrancl_into_rtrancl)
51a80e238b29 Adapted to new inductive definition package.
berghofe
parents: 21546
diff changeset
   147
  apply (rule rtrancl.rtrancl_refl)
14063
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
   148
  apply (rule beta)
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
   149
  apply (erule subst_Var_NF)
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
   150
  done
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
   151
22271
51a80e238b29 Adapted to new inductive definition package.
berghofe
parents: 21546
diff changeset
   152
lemma lift_terms_NF: "listall NF ts \<Longrightarrow>
51a80e238b29 Adapted to new inductive definition package.
berghofe
parents: 21546
diff changeset
   153
    listall (\<lambda>t. \<forall>i. NF (lift t i)) ts \<Longrightarrow>
51a80e238b29 Adapted to new inductive definition package.
berghofe
parents: 21546
diff changeset
   154
    listall NF (map (\<lambda>t. lift t i) ts)"
18257
2124b24454dd tuned induct proofs;
wenzelm
parents: 18241
diff changeset
   155
  by (induct ts) simp_all
14063
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
   156
22271
51a80e238b29 Adapted to new inductive definition package.
berghofe
parents: 21546
diff changeset
   157
lemma lift_NF: "NF t \<Longrightarrow> NF (lift t i)"
20503
503ac4c5ef91 induct method: renamed 'fixing' to 'arbitrary';
wenzelm
parents: 20453
diff changeset
   158
  apply (induct arbitrary: i set: NF)
14063
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
   159
  apply (frule listall_conj1)
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
   160
  apply (drule listall_conj2)
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
   161
  apply (drule_tac i=i in lift_terms_NF)
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
   162
  apply assumption
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
   163
  apply (rule_tac m=x and n=i in nat_le_dec [THEN disjE, standard])
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
   164
  apply simp
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
   165
  apply (rule NF.App)
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
   166
  apply assumption
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
   167
  apply simp
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
   168
  apply (rule NF.App)
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
   169
  apply assumption
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
   170
  apply simp
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
   171
  apply (rule NF.Abs)
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
   172
  apply simp
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
   173
  done
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
   174
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
   175
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
   176
subsection {* Main theorems *}
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
   177
18331
eb3a7d3d874b Factored out proof for normalization of applications (norm_list).
berghofe
parents: 18257
diff changeset
   178
lemma norm_list:
eb3a7d3d874b Factored out proof for normalization of applications (norm_list).
berghofe
parents: 18257
diff changeset
   179
  assumes f_compat: "\<And>t t'. t \<rightarrow>\<^sub>\<beta>\<^sup>* t' \<Longrightarrow> f t \<rightarrow>\<^sub>\<beta>\<^sup>* f t'"
22271
51a80e238b29 Adapted to new inductive definition package.
berghofe
parents: 21546
diff changeset
   180
  and f_NF: "\<And>t. NF t \<Longrightarrow> NF (f t)"
51a80e238b29 Adapted to new inductive definition package.
berghofe
parents: 21546
diff changeset
   181
  and uNF: "NF u" and uT: "e \<turnstile> u : T"
18331
eb3a7d3d874b Factored out proof for normalization of applications (norm_list).
berghofe
parents: 18257
diff changeset
   182
  shows "\<And>Us. e\<langle>i:T\<rangle> \<tturnstile> as : Us \<Longrightarrow>
eb3a7d3d874b Factored out proof for normalization of applications (norm_list).
berghofe
parents: 18257
diff changeset
   183
    listall (\<lambda>t. \<forall>e T' u i. e\<langle>i:T\<rangle> \<turnstile> t : T' \<longrightarrow>
22271
51a80e238b29 Adapted to new inductive definition package.
berghofe
parents: 21546
diff changeset
   184
      NF u \<longrightarrow> e \<turnstile> u : T \<longrightarrow> (\<exists>t'. t[u/i] \<rightarrow>\<^sub>\<beta>\<^sup>* t' \<and> NF t')) as \<Longrightarrow>
18331
eb3a7d3d874b Factored out proof for normalization of applications (norm_list).
berghofe
parents: 18257
diff changeset
   185
    \<exists>as'. \<forall>j. Var j \<degree>\<degree> map (\<lambda>t. f (t[u/i])) as \<rightarrow>\<^sub>\<beta>\<^sup>*
22271
51a80e238b29 Adapted to new inductive definition package.
berghofe
parents: 21546
diff changeset
   186
      Var j \<degree>\<degree> map f as' \<and> NF (Var j \<degree>\<degree> map f as')"
18331
eb3a7d3d874b Factored out proof for normalization of applications (norm_list).
berghofe
parents: 18257
diff changeset
   187
  (is "\<And>Us. _ \<Longrightarrow> listall ?R as \<Longrightarrow> \<exists>as'. ?ex Us as as'")
eb3a7d3d874b Factored out proof for normalization of applications (norm_list).
berghofe
parents: 18257
diff changeset
   188
proof (induct as rule: rev_induct)
eb3a7d3d874b Factored out proof for normalization of applications (norm_list).
berghofe
parents: 18257
diff changeset
   189
  case (Nil Us)
eb3a7d3d874b Factored out proof for normalization of applications (norm_list).
berghofe
parents: 18257
diff changeset
   190
  with Var_NF have "?ex Us [] []" by simp
eb3a7d3d874b Factored out proof for normalization of applications (norm_list).
berghofe
parents: 18257
diff changeset
   191
  thus ?case ..
eb3a7d3d874b Factored out proof for normalization of applications (norm_list).
berghofe
parents: 18257
diff changeset
   192
next
eb3a7d3d874b Factored out proof for normalization of applications (norm_list).
berghofe
parents: 18257
diff changeset
   193
  case (snoc b bs Us)
eb3a7d3d874b Factored out proof for normalization of applications (norm_list).
berghofe
parents: 18257
diff changeset
   194
  have "e\<langle>i:T\<rangle> \<tturnstile> bs  @ [b] : Us" .
eb3a7d3d874b Factored out proof for normalization of applications (norm_list).
berghofe
parents: 18257
diff changeset
   195
  then obtain Vs W where Us: "Us = Vs @ [W]"
eb3a7d3d874b Factored out proof for normalization of applications (norm_list).
berghofe
parents: 18257
diff changeset
   196
    and bs: "e\<langle>i:T\<rangle> \<tturnstile> bs : Vs" and bT: "e\<langle>i:T\<rangle> \<turnstile> b : W"
eb3a7d3d874b Factored out proof for normalization of applications (norm_list).
berghofe
parents: 18257
diff changeset
   197
    by (rule types_snocE)
eb3a7d3d874b Factored out proof for normalization of applications (norm_list).
berghofe
parents: 18257
diff changeset
   198
  from snoc have "listall ?R bs" by simp
eb3a7d3d874b Factored out proof for normalization of applications (norm_list).
berghofe
parents: 18257
diff changeset
   199
  with bs have "\<exists>bs'. ?ex Vs bs bs'" by (rule snoc)
eb3a7d3d874b Factored out proof for normalization of applications (norm_list).
berghofe
parents: 18257
diff changeset
   200
  then obtain bs' where
eb3a7d3d874b Factored out proof for normalization of applications (norm_list).
berghofe
parents: 18257
diff changeset
   201
    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'"
22271
51a80e238b29 Adapted to new inductive definition package.
berghofe
parents: 21546
diff changeset
   202
    and bsNF: "\<And>j. NF (Var j \<degree>\<degree> map f bs')" by iprover
18331
eb3a7d3d874b Factored out proof for normalization of applications (norm_list).
berghofe
parents: 18257
diff changeset
   203
  from snoc have "?R b" by simp
22271
51a80e238b29 Adapted to new inductive definition package.
berghofe
parents: 21546
diff changeset
   204
  with bT and uNF and uT have "\<exists>b'. b[u/i] \<rightarrow>\<^sub>\<beta>\<^sup>* b' \<and> NF b'"
18331
eb3a7d3d874b Factored out proof for normalization of applications (norm_list).
berghofe
parents: 18257
diff changeset
   205
    by iprover
22271
51a80e238b29 Adapted to new inductive definition package.
berghofe
parents: 21546
diff changeset
   206
  then obtain b' where bred: "b[u/i] \<rightarrow>\<^sub>\<beta>\<^sup>* b'" and bNF: "NF b'"
18331
eb3a7d3d874b Factored out proof for normalization of applications (norm_list).
berghofe
parents: 18257
diff changeset
   207
    by iprover
22271
51a80e238b29 Adapted to new inductive definition package.
berghofe
parents: 21546
diff changeset
   208
  from bsNF [of 0] have "listall NF (map f bs')"
18331
eb3a7d3d874b Factored out proof for normalization of applications (norm_list).
berghofe
parents: 18257
diff changeset
   209
    by (rule App_NF_D)
22271
51a80e238b29 Adapted to new inductive definition package.
berghofe
parents: 21546
diff changeset
   210
  moreover have "NF (f b')" by (rule f_NF)
51a80e238b29 Adapted to new inductive definition package.
berghofe
parents: 21546
diff changeset
   211
  ultimately have "listall NF (map f (bs' @ [b']))"
18331
eb3a7d3d874b Factored out proof for normalization of applications (norm_list).
berghofe
parents: 18257
diff changeset
   212
    by simp
22271
51a80e238b29 Adapted to new inductive definition package.
berghofe
parents: 21546
diff changeset
   213
  hence "\<And>j. NF (Var j \<degree>\<degree> map f (bs' @ [b']))" by (rule NF.App)
18331
eb3a7d3d874b Factored out proof for normalization of applications (norm_list).
berghofe
parents: 18257
diff changeset
   214
  moreover from bred have "f (b[u/i]) \<rightarrow>\<^sub>\<beta>\<^sup>* f b'"
eb3a7d3d874b Factored out proof for normalization of applications (norm_list).
berghofe
parents: 18257
diff changeset
   215
    by (rule f_compat)
eb3a7d3d874b Factored out proof for normalization of applications (norm_list).
berghofe
parents: 18257
diff changeset
   216
  with bsred have
eb3a7d3d874b Factored out proof for normalization of applications (norm_list).
berghofe
parents: 18257
diff changeset
   217
    "\<And>j. (Var j \<degree>\<degree> map (\<lambda>t. f (t[u/i])) bs) \<degree> f (b[u/i]) \<rightarrow>\<^sub>\<beta>\<^sup>*
eb3a7d3d874b Factored out proof for normalization of applications (norm_list).
berghofe
parents: 18257
diff changeset
   218
     (Var j \<degree>\<degree> map f bs') \<degree> f b'" by (rule rtrancl_beta_App)
eb3a7d3d874b Factored out proof for normalization of applications (norm_list).
berghofe
parents: 18257
diff changeset
   219
  ultimately have "?ex Us (bs @ [b]) (bs' @ [b'])" by simp
eb3a7d3d874b Factored out proof for normalization of applications (norm_list).
berghofe
parents: 18257
diff changeset
   220
  thus ?case ..
eb3a7d3d874b Factored out proof for normalization of applications (norm_list).
berghofe
parents: 18257
diff changeset
   221
qed
eb3a7d3d874b Factored out proof for normalization of applications (norm_list).
berghofe
parents: 18257
diff changeset
   222
14063
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
   223
lemma subst_type_NF:
22271
51a80e238b29 Adapted to new inductive definition package.
berghofe
parents: 21546
diff changeset
   224
  "\<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'"
14063
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
   225
  (is "PROP ?P U" is "\<And>t e T u i. _ \<Longrightarrow> PROP ?Q t e T u i U")
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
   226
proof (induct U)
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
   227
  fix T t
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
   228
  let ?R = "\<lambda>t. \<forall>e T' u i.
22271
51a80e238b29 Adapted to new inductive definition package.
berghofe
parents: 21546
diff changeset
   229
    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')"
14063
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
   230
  assume MI1: "\<And>T1 T2. T = T1 \<Rightarrow> T2 \<Longrightarrow> PROP ?P T1"
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
   231
  assume MI2: "\<And>T1 T2. T = T1 \<Rightarrow> T2 \<Longrightarrow> PROP ?P T2"
22271
51a80e238b29 Adapted to new inductive definition package.
berghofe
parents: 21546
diff changeset
   232
  assume "NF t"
14063
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
   233
  thus "\<And>e T' u i. PROP ?Q t e T' u i T"
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
   234
  proof induct
22271
51a80e238b29 Adapted to new inductive definition package.
berghofe
parents: 21546
diff changeset
   235
    fix e T' u i assume uNF: "NF u" and uT: "e \<turnstile> u : T"
14063
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
   236
    {
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
   237
      case (App ts x e_ T'_ u_ i_)
18331
eb3a7d3d874b Factored out proof for normalization of applications (norm_list).
berghofe
parents: 18257
diff changeset
   238
      assume "e\<langle>i:T\<rangle> \<turnstile> Var x \<degree>\<degree> ts : T'"
eb3a7d3d874b Factored out proof for normalization of applications (norm_list).
berghofe
parents: 18257
diff changeset
   239
      then obtain Us
eb3a7d3d874b Factored out proof for normalization of applications (norm_list).
berghofe
parents: 18257
diff changeset
   240
	where varT: "e\<langle>i:T\<rangle> \<turnstile> Var x : Us \<Rrightarrow> T'"
eb3a7d3d874b Factored out proof for normalization of applications (norm_list).
berghofe
parents: 18257
diff changeset
   241
	and argsT: "e\<langle>i:T\<rangle> \<tturnstile> ts : Us"
eb3a7d3d874b Factored out proof for normalization of applications (norm_list).
berghofe
parents: 18257
diff changeset
   242
	by (rule var_app_typesE)
22271
51a80e238b29 Adapted to new inductive definition package.
berghofe
parents: 21546
diff changeset
   243
      from nat_eq_dec show "\<exists>t'. (Var x \<degree>\<degree> ts)[u/i] \<rightarrow>\<^sub>\<beta>\<^sup>* t' \<and> NF t'"
14063
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
   244
      proof
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
   245
	assume eq: "x = i"
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
   246
	show ?thesis
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
   247
	proof (cases ts)
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
   248
	  case Nil
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
   249
	  with eq have "(Var x \<degree>\<degree> [])[u/i] \<rightarrow>\<^sub>\<beta>\<^sup>* u" by simp
17589
58eeffd73be1 renamed rules to iprover
nipkow
parents: 17145
diff changeset
   250
	  with Nil and uNF show ?thesis by simp iprover
14063
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
   251
	next
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
   252
	  case (Cons a as)
18331
eb3a7d3d874b Factored out proof for normalization of applications (norm_list).
berghofe
parents: 18257
diff changeset
   253
          with argsT obtain T'' Ts where Us: "Us = T'' # Ts"
14063
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
   254
	    by (cases Us) (rule FalseE, simp+)
18331
eb3a7d3d874b Factored out proof for normalization of applications (norm_list).
berghofe
parents: 18257
diff changeset
   255
	  from varT and Us have varT: "e\<langle>i:T\<rangle> \<turnstile> Var x : T'' \<Rightarrow> Ts \<Rrightarrow> T'"
14063
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
   256
	    by simp
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
   257
          from varT eq have T: "T = T'' \<Rightarrow> Ts \<Rrightarrow> T'" by cases auto
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
   258
          with uT have uT': "e \<turnstile> u : T'' \<Rightarrow> Ts \<Rrightarrow> T'" by simp
18331
eb3a7d3d874b Factored out proof for normalization of applications (norm_list).
berghofe
parents: 18257
diff changeset
   259
	  from argsT Us Cons have argsT': "e\<langle>i:T\<rangle> \<tturnstile> as : Ts" by simp
eb3a7d3d874b Factored out proof for normalization of applications (norm_list).
berghofe
parents: 18257
diff changeset
   260
	  from argsT Us Cons have argT: "e\<langle>i:T\<rangle> \<turnstile> a : T''" by simp
14063
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
   261
	  from argT uT refl have aT: "e \<turnstile> a[u/i] : T''" by (rule subst_lemma)
17589
58eeffd73be1 renamed rules to iprover
nipkow
parents: 17145
diff changeset
   262
	  from App and Cons have "listall ?R as" by simp (iprover dest: listall_conj2)
18331
eb3a7d3d874b Factored out proof for normalization of applications (norm_list).
berghofe
parents: 18257
diff changeset
   263
	  with lift_preserves_beta' lift_NF uNF uT argsT'
eb3a7d3d874b Factored out proof for normalization of applications (norm_list).
berghofe
parents: 18257
diff changeset
   264
	  have "\<exists>as'. \<forall>j. Var j \<degree>\<degree> map (\<lambda>t. lift (t[u/i]) 0) as \<rightarrow>\<^sub>\<beta>\<^sup>*
eb3a7d3d874b Factored out proof for normalization of applications (norm_list).
berghofe
parents: 18257
diff changeset
   265
            Var j \<degree>\<degree> map (\<lambda>t. lift t 0) as' \<and>
22271
51a80e238b29 Adapted to new inductive definition package.
berghofe
parents: 21546
diff changeset
   266
	    NF (Var j \<degree>\<degree> map (\<lambda>t. lift t 0) as')" by (rule norm_list)
14063
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
   267
	  then obtain as' where
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
   268
	    asred: "Var 0 \<degree>\<degree> map (\<lambda>t. lift (t[u/i]) 0) as \<rightarrow>\<^sub>\<beta>\<^sup>*
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
   269
	      Var 0 \<degree>\<degree> map (\<lambda>t. lift t 0) as'"
22271
51a80e238b29 Adapted to new inductive definition package.
berghofe
parents: 21546
diff changeset
   270
	    and asNF: "NF (Var 0 \<degree>\<degree> map (\<lambda>t. lift t 0) as')" by iprover
14063
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
   271
	  from App and Cons have "?R a" by simp
22271
51a80e238b29 Adapted to new inductive definition package.
berghofe
parents: 21546
diff changeset
   272
	  with argT and uNF and uT have "\<exists>a'. a[u/i] \<rightarrow>\<^sub>\<beta>\<^sup>* a' \<and> NF a'"
17589
58eeffd73be1 renamed rules to iprover
nipkow
parents: 17145
diff changeset
   273
	    by iprover
22271
51a80e238b29 Adapted to new inductive definition package.
berghofe
parents: 21546
diff changeset
   274
	  then obtain a' where ared: "a[u/i] \<rightarrow>\<^sub>\<beta>\<^sup>* a'" and aNF: "NF a'" by iprover
51a80e238b29 Adapted to new inductive definition package.
berghofe
parents: 21546
diff changeset
   275
	  from uNF have "NF (lift u 0)" by (rule lift_NF)
51a80e238b29 Adapted to new inductive definition package.
berghofe
parents: 21546
diff changeset
   276
	  hence "\<exists>u'. lift u 0 \<degree> Var 0 \<rightarrow>\<^sub>\<beta>\<^sup>* u' \<and> NF u'" by (rule app_Var_NF)
51a80e238b29 Adapted to new inductive definition package.
berghofe
parents: 21546
diff changeset
   277
	  then obtain u' where ured: "lift u 0 \<degree> Var 0 \<rightarrow>\<^sub>\<beta>\<^sup>* u'" and u'NF: "NF u'"
17589
58eeffd73be1 renamed rules to iprover
nipkow
parents: 17145
diff changeset
   278
	    by iprover
22271
51a80e238b29 Adapted to new inductive definition package.
berghofe
parents: 21546
diff changeset
   279
	  from T and u'NF have "\<exists>ua. u'[a'/0] \<rightarrow>\<^sub>\<beta>\<^sup>* ua \<and> NF ua"
14063
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
   280
	  proof (rule MI1)
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
   281
	    have "e\<langle>0:T''\<rangle> \<turnstile> lift u 0 \<degree> Var 0 : Ts \<Rrightarrow> T'"
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
   282
	    proof (rule typing.App)
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
   283
	      from uT' show "e\<langle>0:T''\<rangle> \<turnstile> lift u 0 : T'' \<Rightarrow> Ts \<Rrightarrow> T'" by (rule lift_type)
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
   284
	      show "e\<langle>0:T''\<rangle> \<turnstile> Var 0 : T''" by (rule typing.Var) simp
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
   285
	    qed
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
   286
	    with ured show "e\<langle>0:T''\<rangle> \<turnstile> u' : Ts \<Rrightarrow> T'" by (rule subject_reduction')
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
   287
	    from ared aT show "e \<turnstile> a' : T''" by (rule subject_reduction')
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
   288
	  qed
22271
51a80e238b29 Adapted to new inductive definition package.
berghofe
parents: 21546
diff changeset
   289
	  then obtain ua where uared: "u'[a'/0] \<rightarrow>\<^sub>\<beta>\<^sup>* ua" and uaNF: "NF ua"
17589
58eeffd73be1 renamed rules to iprover
nipkow
parents: 17145
diff changeset
   290
	    by iprover
14063
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
   291
	  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]"
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
   292
	    by (rule subst_preserves_beta2')
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
   293
	  also from ured have "(lift u 0 \<degree> Var 0)[a'/0] \<rightarrow>\<^sub>\<beta>\<^sup>* u'[a'/0]"
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
   294
	    by (rule subst_preserves_beta')
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
   295
	  also note uared
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
   296
	  finally have "(lift u 0 \<degree> Var 0)[a[u/i]/0] \<rightarrow>\<^sub>\<beta>\<^sup>* ua" .
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
   297
	  hence uared': "u \<degree> a[u/i] \<rightarrow>\<^sub>\<beta>\<^sup>* ua" by simp
22271
51a80e238b29 Adapted to new inductive definition package.
berghofe
parents: 21546
diff changeset
   298
	  from T have "\<exists>r. (Var 0 \<degree>\<degree> map (\<lambda>t. lift t 0) as')[ua/0] \<rightarrow>\<^sub>\<beta>\<^sup>* r \<and> NF r"
14063
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
   299
	  proof (rule MI2)
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
   300
	    have "e\<langle>0:Ts \<Rrightarrow> T'\<rangle> \<turnstile> Var 0 \<degree>\<degree> map (\<lambda>t. lift (t[u/i]) 0) as : T'"
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
   301
	    proof (rule list_app_typeI)
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
   302
	      show "e\<langle>0:Ts \<Rrightarrow> T'\<rangle> \<turnstile> Var 0 : Ts \<Rrightarrow> T'" by (rule typing.Var) simp
18331
eb3a7d3d874b Factored out proof for normalization of applications (norm_list).
berghofe
parents: 18257
diff changeset
   303
	      from uT argsT' have "e \<tturnstile> map (\<lambda>t. t[u/i]) as : Ts"
14063
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
   304
		by (rule substs_lemma)
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
   305
	      hence "e\<langle>0:Ts \<Rrightarrow> T'\<rangle> \<tturnstile> map (\<lambda>t. lift t 0) (map (\<lambda>t. t[u/i]) as) : Ts"
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
   306
		by (rule lift_types)
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
   307
	      thus "e\<langle>0:Ts \<Rrightarrow> T'\<rangle> \<tturnstile> map (\<lambda>t. lift (t[u/i]) 0) as : Ts"
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
   308
		by (simp_all add: map_compose [symmetric] o_def)
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
   309
	    qed
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
   310
	    with asred show "e\<langle>0:Ts \<Rrightarrow> T'\<rangle> \<turnstile> Var 0 \<degree>\<degree> map (\<lambda>t. lift t 0) as' : T'"
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
   311
	      by (rule subject_reduction')
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
   312
	    from argT uT refl have "e \<turnstile> a[u/i] : T''" by (rule subst_lemma)
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
   313
	    with uT' have "e \<turnstile> u \<degree> a[u/i] : Ts \<Rrightarrow> T'" by (rule typing.App)
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
   314
	    with uared' show "e \<turnstile> ua : Ts \<Rrightarrow> T'" by (rule subject_reduction')
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
   315
	  qed
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
   316
	  then obtain r where rred: "(Var 0 \<degree>\<degree> map (\<lambda>t. lift t 0) as')[ua/0] \<rightarrow>\<^sub>\<beta>\<^sup>* r"
22271
51a80e238b29 Adapted to new inductive definition package.
berghofe
parents: 21546
diff changeset
   317
	    and rnf: "NF r" by iprover
14063
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
   318
	  from asred have
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
   319
	    "(Var 0 \<degree>\<degree> map (\<lambda>t. lift (t[u/i]) 0) as)[u \<degree> a[u/i]/0] \<rightarrow>\<^sub>\<beta>\<^sup>*
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
   320
	    (Var 0 \<degree>\<degree> map (\<lambda>t. lift t 0) as')[u \<degree> a[u/i]/0]"
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
   321
	    by (rule subst_preserves_beta')
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
   322
	  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>*
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
   323
	    (Var 0 \<degree>\<degree> map (\<lambda>t. lift t 0) as')[ua/0]" by (rule subst_preserves_beta2')
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
   324
	  also note rred
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
   325
	  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" .
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
   326
	  with rnf Cons eq show ?thesis
17589
58eeffd73be1 renamed rules to iprover
nipkow
parents: 17145
diff changeset
   327
	    by (simp add: map_compose [symmetric] o_def) iprover
14063
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
   328
	qed
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
   329
      next
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
   330
	assume neq: "x \<noteq> i"
18331
eb3a7d3d874b Factored out proof for normalization of applications (norm_list).
berghofe
parents: 18257
diff changeset
   331
	from App have "listall ?R ts" by (iprover dest: listall_conj2)
eb3a7d3d874b Factored out proof for normalization of applications (norm_list).
berghofe
parents: 18257
diff changeset
   332
	with TrueI TrueI uNF uT argsT
eb3a7d3d874b Factored out proof for normalization of applications (norm_list).
berghofe
parents: 18257
diff changeset
   333
	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>
22271
51a80e238b29 Adapted to new inductive definition package.
berghofe
parents: 21546
diff changeset
   334
	  NF (Var j \<degree>\<degree> ts')" (is "\<exists>ts'. ?ex ts'")
18331
eb3a7d3d874b Factored out proof for normalization of applications (norm_list).
berghofe
parents: 18257
diff changeset
   335
	  by (rule norm_list [of "\<lambda>t. t", simplified])
eb3a7d3d874b Factored out proof for normalization of applications (norm_list).
berghofe
parents: 18257
diff changeset
   336
	then obtain ts' where NF: "?ex ts'" ..
eb3a7d3d874b Factored out proof for normalization of applications (norm_list).
berghofe
parents: 18257
diff changeset
   337
	from nat_le_dec show ?thesis
eb3a7d3d874b Factored out proof for normalization of applications (norm_list).
berghofe
parents: 18257
diff changeset
   338
	proof
eb3a7d3d874b Factored out proof for normalization of applications (norm_list).
berghofe
parents: 18257
diff changeset
   339
	  assume "i < x"
eb3a7d3d874b Factored out proof for normalization of applications (norm_list).
berghofe
parents: 18257
diff changeset
   340
	  with NF show ?thesis by simp iprover
eb3a7d3d874b Factored out proof for normalization of applications (norm_list).
berghofe
parents: 18257
diff changeset
   341
	next
eb3a7d3d874b Factored out proof for normalization of applications (norm_list).
berghofe
parents: 18257
diff changeset
   342
	  assume "\<not> (i < x)"
eb3a7d3d874b Factored out proof for normalization of applications (norm_list).
berghofe
parents: 18257
diff changeset
   343
	  with NF neq show ?thesis by (simp add: subst_Var) iprover
14063
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
   344
	qed
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
   345
      qed
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
   346
    next
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
   347
      case (Abs r e_ T'_ u_ i_)
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
   348
      assume absT: "e\<langle>i:T\<rangle> \<turnstile> Abs r : T'"
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
   349
      then obtain R S where "e\<langle>0:R\<rangle>\<langle>Suc i:T\<rangle>  \<turnstile> r : S" by (rule abs_typeE) simp
22271
51a80e238b29 Adapted to new inductive definition package.
berghofe
parents: 21546
diff changeset
   350
      moreover have "NF (lift u 0)" by (rule lift_NF)
14063
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
   351
      moreover have "e\<langle>0:R\<rangle> \<turnstile> lift u 0 : T" by (rule lift_type)
22271
51a80e238b29 Adapted to new inductive definition package.
berghofe
parents: 21546
diff changeset
   352
      ultimately have "\<exists>t'. r[lift u 0/Suc i] \<rightarrow>\<^sub>\<beta>\<^sup>* t' \<and> NF t'" by (rule Abs)
51a80e238b29 Adapted to new inductive definition package.
berghofe
parents: 21546
diff changeset
   353
      thus "\<exists>t'. Abs r[u/i] \<rightarrow>\<^sub>\<beta>\<^sup>* t' \<and> NF t'"
17589
58eeffd73be1 renamed rules to iprover
nipkow
parents: 17145
diff changeset
   354
	by simp (iprover intro: rtrancl_beta_Abs NF.Abs)
14063
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
   355
    }
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
   356
  qed
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
   357
qed
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
   358
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
   359
22271
51a80e238b29 Adapted to new inductive definition package.
berghofe
parents: 21546
diff changeset
   360
-- {* A computationally relevant copy of @{term "e \<turnstile> t : T"} *}
51a80e238b29 Adapted to new inductive definition package.
berghofe
parents: 21546
diff changeset
   361
inductive2 rtyping :: "(nat \<Rightarrow> type) \<Rightarrow> dB \<Rightarrow> type \<Rightarrow> bool"  ("_ \<turnstile>\<^sub>R _ : _" [50, 50, 50] 50)
51a80e238b29 Adapted to new inductive definition package.
berghofe
parents: 21546
diff changeset
   362
  where
14063
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
   363
    Var: "e x = T \<Longrightarrow> e \<turnstile>\<^sub>R Var x : T"
22271
51a80e238b29 Adapted to new inductive definition package.
berghofe
parents: 21546
diff changeset
   364
  | Abs: "e\<langle>0:T\<rangle> \<turnstile>\<^sub>R t : U \<Longrightarrow> e \<turnstile>\<^sub>R Abs t : (T \<Rightarrow> U)"
51a80e238b29 Adapted to new inductive definition package.
berghofe
parents: 21546
diff changeset
   365
  | 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"
14063
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
   366
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
   367
lemma rtyping_imp_typing: "e \<turnstile>\<^sub>R t : T \<Longrightarrow> e \<turnstile> t : T"
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
   368
  apply (induct set: rtyping)
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
   369
  apply (erule typing.Var)
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
   370
  apply (erule typing.Abs)
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
   371
  apply (erule typing.App)
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
   372
  apply assumption
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
   373
  done
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
   374
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
   375
18513
791b53bf4073 tuned proofs;
wenzelm
parents: 18331
diff changeset
   376
theorem type_NF:
791b53bf4073 tuned proofs;
wenzelm
parents: 18331
diff changeset
   377
  assumes "e \<turnstile>\<^sub>R t : T"
22271
51a80e238b29 Adapted to new inductive definition package.
berghofe
parents: 21546
diff changeset
   378
  shows "\<exists>t'. t \<rightarrow>\<^sub>\<beta>\<^sup>* t' \<and> NF t'" using prems
14063
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
   379
proof induct
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
   380
  case Var
17589
58eeffd73be1 renamed rules to iprover
nipkow
parents: 17145
diff changeset
   381
  show ?case by (iprover intro: Var_NF)
14063
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
   382
next
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
   383
  case Abs
17589
58eeffd73be1 renamed rules to iprover
nipkow
parents: 17145
diff changeset
   384
  thus ?case by (iprover intro: rtrancl_beta_Abs NF.Abs)
14063
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
   385
next
22271
51a80e238b29 Adapted to new inductive definition package.
berghofe
parents: 21546
diff changeset
   386
  case (App e s T U t)
14063
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
   387
  from App obtain s' t' where
22271
51a80e238b29 Adapted to new inductive definition package.
berghofe
parents: 21546
diff changeset
   388
    sred: "s \<rightarrow>\<^sub>\<beta>\<^sup>* s'" and sNF: "NF s'"
51a80e238b29 Adapted to new inductive definition package.
berghofe
parents: 21546
diff changeset
   389
    and tred: "t \<rightarrow>\<^sub>\<beta>\<^sup>* t'" and tNF: "NF t'" by iprover
51a80e238b29 Adapted to new inductive definition package.
berghofe
parents: 21546
diff changeset
   390
  have "\<exists>u. (Var 0 \<degree> lift t' 0)[s'/0] \<rightarrow>\<^sub>\<beta>\<^sup>* u \<and> NF u"
14063
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
   391
  proof (rule subst_type_NF)
22271
51a80e238b29 Adapted to new inductive definition package.
berghofe
parents: 21546
diff changeset
   392
    have "NF (lift t' 0)" by (rule lift_NF)
51a80e238b29 Adapted to new inductive definition package.
berghofe
parents: 21546
diff changeset
   393
    hence "listall NF [lift t' 0]" by (rule listall_cons) (rule listall_nil)
51a80e238b29 Adapted to new inductive definition package.
berghofe
parents: 21546
diff changeset
   394
    hence "NF (Var 0 \<degree>\<degree> [lift t' 0])" by (rule NF.App)
51a80e238b29 Adapted to new inductive definition package.
berghofe
parents: 21546
diff changeset
   395
    thus "NF (Var 0 \<degree> lift t' 0)" by simp
14063
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
   396
    show "e\<langle>0:T \<Rightarrow> U\<rangle> \<turnstile> Var 0 \<degree> lift t' 0 : U"
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
   397
    proof (rule typing.App)
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
   398
      show "e\<langle>0:T \<Rightarrow> U\<rangle> \<turnstile> Var 0 : T \<Rightarrow> U"
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
   399
      	by (rule typing.Var) simp
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
   400
      from tred have "e \<turnstile> t' : T"
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
   401
      	by (rule subject_reduction') (rule rtyping_imp_typing)
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
   402
      thus "e\<langle>0:T \<Rightarrow> U\<rangle> \<turnstile> lift t' 0 : T"
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
   403
      	by (rule lift_type)
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
   404
    qed
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
   405
    from sred show "e \<turnstile> s' : T \<Rightarrow> U"
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
   406
      by (rule subject_reduction') (rule rtyping_imp_typing)
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
   407
  qed
22271
51a80e238b29 Adapted to new inductive definition package.
berghofe
parents: 21546
diff changeset
   408
  then obtain u where ured: "s' \<degree> t' \<rightarrow>\<^sub>\<beta>\<^sup>* u" and unf: "NF u" by simp iprover
14063
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
   409
  from sred tred have "s \<degree> t \<rightarrow>\<^sub>\<beta>\<^sup>* s' \<degree> t'" by (rule rtrancl_beta_App)
22271
51a80e238b29 Adapted to new inductive definition package.
berghofe
parents: 21546
diff changeset
   410
  hence "s \<degree> t \<rightarrow>\<^sub>\<beta>\<^sup>* u" using ured by (rule rtrancl_trans')
17589
58eeffd73be1 renamed rules to iprover
nipkow
parents: 17145
diff changeset
   411
  with unf show ?case by iprover
14063
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
   412
qed
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
   413
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
   414
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
   415
subsection {* Extracting the program *}
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
   416
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
   417
declare NF.induct [ind_realizer]
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
   418
declare rtrancl.induct [ind_realizer irrelevant]
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
   419
declare rtyping.induct [ind_realizer]
22271
51a80e238b29 Adapted to new inductive definition package.
berghofe
parents: 21546
diff changeset
   420
lemmas [extraction_expand] = conj_assoc listall_cons_eq
14063
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
   421
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
   422
extract type_NF
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
   423
22271
51a80e238b29 Adapted to new inductive definition package.
berghofe
parents: 21546
diff changeset
   424
lemma rtranclR_rtrancl_eq: "rtranclR r a b = rtrancl r a b"
14063
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
   425
  apply (rule iffI)
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
   426
  apply (erule rtranclR.induct)
22271
51a80e238b29 Adapted to new inductive definition package.
berghofe
parents: 21546
diff changeset
   427
  apply (rule rtrancl.rtrancl_refl)
51a80e238b29 Adapted to new inductive definition package.
berghofe
parents: 21546
diff changeset
   428
  apply (erule rtrancl.rtrancl_into_rtrancl)
51a80e238b29 Adapted to new inductive definition package.
berghofe
parents: 21546
diff changeset
   429
  apply assumption
14063
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
   430
  apply (erule rtrancl.induct)
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
   431
  apply (rule rtranclR.rtrancl_refl)
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
   432
  apply (erule rtranclR.rtrancl_into_rtrancl)
22271
51a80e238b29 Adapted to new inductive definition package.
berghofe
parents: 21546
diff changeset
   433
  apply assumption
14063
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
   434
  done
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
   435
22271
51a80e238b29 Adapted to new inductive definition package.
berghofe
parents: 21546
diff changeset
   436
lemma NFR_imp_NF: "NFR nf t \<Longrightarrow> NF t"
14063
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
   437
  apply (erule NFR.induct)
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
   438
  apply (rule NF.intros)
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
   439
  apply (simp add: listall_def)
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
   440
  apply (erule NF.intros)
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
   441
  done
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
   442
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
   443
text_raw {*
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
   444
\begin{figure}
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
   445
\renewcommand{\isastyle}{\scriptsize\it}%
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
   446
@{thm [display,eta_contract=false,margin=100] subst_type_NF_def}
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
   447
\renewcommand{\isastyle}{\small\it}%
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
   448
\caption{Program extracted from @{text subst_type_NF}}
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
   449
\label{fig:extr-subst-type-nf}
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
   450
\end{figure}
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
   451
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
   452
\begin{figure}
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
   453
\renewcommand{\isastyle}{\scriptsize\it}%
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
   454
@{thm [display,margin=100] subst_Var_NF_def}
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
   455
@{thm [display,margin=100] app_Var_NF_def}
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
   456
@{thm [display,margin=100] lift_NF_def}
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
   457
@{thm [display,eta_contract=false,margin=100] type_NF_def}
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
   458
\renewcommand{\isastyle}{\small\it}%
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
   459
\caption{Program extracted from lemmas and main theorem}
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
   460
\label{fig:extr-type-nf}
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
   461
\end{figure}
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
   462
*}
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
   463
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
   464
text {*
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
   465
The program corresponding to the proof of the central lemma, which
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
   466
performs substitution and normalization, is shown in Figure
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
   467
\ref{fig:extr-subst-type-nf}. The correctness
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
   468
theorem corresponding to the program @{text "subst_type_NF"} is
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
   469
@{thm [display,margin=100] subst_type_NF_correctness
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
   470
  [simplified rtranclR_rtrancl_eq Collect_mem_eq, no_vars]}
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
   471
where @{text NFR} is the realizability predicate corresponding to
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
   472
the datatype @{text NFT}, which is inductively defined by the rules
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
   473
\pagebreak
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
   474
@{thm [display,margin=90] NFR.App [of ts nfs x] NFR.Abs [of nf t]}
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
   475
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
   476
The programs corresponding to the main theorem @{text "type_NF"}, as
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
   477
well as to some lemmas, are shown in Figure \ref{fig:extr-type-nf}.
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
   478
The correctness statement for the main function @{text "type_NF"} is
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
   479
@{thm [display,margin=100] type_NF_correctness
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
   480
  [simplified rtranclR_rtrancl_eq Collect_mem_eq, no_vars]}
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
   481
where the realizability predicate @{text "rtypingR"} corresponding to the
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
   482
computationally relevant version of the typing judgement is inductively
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
   483
defined by the rules
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
   484
@{thm [display,margin=100] rtypingR.Var [no_vars]
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
   485
  rtypingR.Abs [of ty, no_vars] rtypingR.App [of ty e s T U ty' t]}
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
   486
*}
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
   487
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
   488
subsection {* Generating executable code *}
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
   489
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
   490
consts_code
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
   491
  arbitrary :: "'a"       ("(error \"arbitrary\")")
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
   492
  arbitrary :: "'a \<Rightarrow> 'b" ("(fn '_ => error \"arbitrary\")")
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
   493
17145
e623e57b0f44 Adapted to new code generator syntax.
berghofe
parents: 16417
diff changeset
   494
code_module Norm
e623e57b0f44 Adapted to new code generator syntax.
berghofe
parents: 16417
diff changeset
   495
contains
14063
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
   496
  test = "type_NF"
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
   497
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
   498
text {*
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
   499
The following functions convert between Isabelle's built-in {\tt term}
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
   500
datatype and the generated {\tt dB} datatype. This allows to
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
   501
generate example terms using Isabelle's parser and inspect
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
   502
normalized terms using Isabelle's pretty printer.
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
   503
*}
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
   504
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
   505
ML {*
20713
823967ef47f1 renamed 0 and 1 to HOL.zero and HOL.one respectivly; introduced corresponding syntactic classes
haftmann
parents: 20503
diff changeset
   506
fun nat_of_int 0 = Norm.zero
17145
e623e57b0f44 Adapted to new code generator syntax.
berghofe
parents: 16417
diff changeset
   507
  | nat_of_int n = Norm.Suc (nat_of_int (n-1));
14063
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
   508
20713
823967ef47f1 renamed 0 and 1 to HOL.zero and HOL.one respectivly; introduced corresponding syntactic classes
haftmann
parents: 20503
diff changeset
   509
fun int_of_nat Norm.zero = 0
17145
e623e57b0f44 Adapted to new code generator syntax.
berghofe
parents: 16417
diff changeset
   510
  | int_of_nat (Norm.Suc n) = 1 + int_of_nat n;
14063
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
   511
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
   512
fun dBtype_of_typ (Type ("fun", [T, U])) =
17145
e623e57b0f44 Adapted to new code generator syntax.
berghofe
parents: 16417
diff changeset
   513
      Norm.Fun (dBtype_of_typ T, dBtype_of_typ U)
14063
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
   514
  | dBtype_of_typ (TFree (s, _)) = (case explode s of
17145
e623e57b0f44 Adapted to new code generator syntax.
berghofe
parents: 16417
diff changeset
   515
        ["'", a] => Norm.Atom (nat_of_int (ord a - 97))
14063
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
   516
      | _ => error "dBtype_of_typ: variable name")
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
   517
  | dBtype_of_typ _ = error "dBtype_of_typ: bad type";
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
   518
17145
e623e57b0f44 Adapted to new code generator syntax.
berghofe
parents: 16417
diff changeset
   519
fun dB_of_term (Bound i) = Norm.dB_Var (nat_of_int i)
e623e57b0f44 Adapted to new code generator syntax.
berghofe
parents: 16417
diff changeset
   520
  | dB_of_term (t $ u) = Norm.App (dB_of_term t, dB_of_term u)
e623e57b0f44 Adapted to new code generator syntax.
berghofe
parents: 16417
diff changeset
   521
  | dB_of_term (Abs (_, _, t)) = Norm.Abs (dB_of_term t)
14063
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
   522
  | dB_of_term _ = error "dB_of_term: bad term";
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
   523
17145
e623e57b0f44 Adapted to new code generator syntax.
berghofe
parents: 16417
diff changeset
   524
fun term_of_dB Ts (Type ("fun", [T, U])) (Norm.Abs dBt) =
14063
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
   525
      Abs ("x", T, term_of_dB (T :: Ts) U dBt)
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
   526
  | term_of_dB Ts _ dBt = term_of_dB' Ts dBt
17145
e623e57b0f44 Adapted to new code generator syntax.
berghofe
parents: 16417
diff changeset
   527
and term_of_dB' Ts (Norm.dB_Var n) = Bound (int_of_nat n)
e623e57b0f44 Adapted to new code generator syntax.
berghofe
parents: 16417
diff changeset
   528
  | term_of_dB' Ts (Norm.App (dBt, dBu)) =
14063
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
   529
      let val t = term_of_dB' Ts dBt
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
   530
      in case fastype_of1 (Ts, t) of
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
   531
          Type ("fun", [T, U]) => t $ term_of_dB Ts T dBu
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
   532
        | _ => error "term_of_dB: function type expected"
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
   533
      end
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
   534
  | term_of_dB' _ _ = error "term_of_dB: term not in normal form";
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
   535
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
   536
fun typing_of_term Ts e (Bound i) =
17145
e623e57b0f44 Adapted to new code generator syntax.
berghofe
parents: 16417
diff changeset
   537
      Norm.Var (e, nat_of_int i, dBtype_of_typ (List.nth (Ts, i)))
14063
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
   538
  | typing_of_term Ts e (t $ u) = (case fastype_of1 (Ts, t) of
17145
e623e57b0f44 Adapted to new code generator syntax.
berghofe
parents: 16417
diff changeset
   539
        Type ("fun", [T, U]) => Norm.rtypingT_App (e, dB_of_term t,
14063
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
   540
          dBtype_of_typ T, dBtype_of_typ U, dB_of_term u,
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
   541
          typing_of_term Ts e t, typing_of_term Ts e u)
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
   542
      | _ => error "typing_of_term: function type expected")
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
   543
  | typing_of_term Ts e (Abs (s, T, t)) =
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
   544
      let val dBT = dBtype_of_typ T
17145
e623e57b0f44 Adapted to new code generator syntax.
berghofe
parents: 16417
diff changeset
   545
      in Norm.rtypingT_Abs (e, dBT, dB_of_term t,
14063
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
   546
        dBtype_of_typ (fastype_of1 (T :: Ts, t)),
20713
823967ef47f1 renamed 0 and 1 to HOL.zero and HOL.one respectivly; introduced corresponding syntactic classes
haftmann
parents: 20503
diff changeset
   547
        typing_of_term (T :: Ts) (Norm.shift e Norm.zero dBT) t)
14063
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
   548
      end
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
   549
  | typing_of_term _ _ _ = error "typing_of_term: bad term";
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
   550
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
   551
fun dummyf _ = error "dummy";
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
   552
*}
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
   553
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
   554
text {*
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
   555
We now try out the extracted program @{text "type_NF"} on some example terms.
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
   556
*}
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
   557
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
   558
ML {*
22512
04242efdcece fixed typing bug in generated code
haftmann
parents: 22499
diff changeset
   559
val ct1 = @{cterm "%f. ((%f x. f (f (f x))) ((%f x. f (f (f (f x)))) f))"};
17145
e623e57b0f44 Adapted to new code generator syntax.
berghofe
parents: 16417
diff changeset
   560
val (dB1, _) = Norm.type_NF (typing_of_term [] dummyf (term_of ct1));
20942
43e216a9d615 purged some ML legacy
haftmann
parents: 20838
diff changeset
   561
val ct1' = cterm_of thy (term_of_dB [] (#T (rep_cterm ct1)) dB1);
14063
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
   562
22512
04242efdcece fixed typing bug in generated code
haftmann
parents: 22499
diff changeset
   563
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)))))"};
17145
e623e57b0f44 Adapted to new code generator syntax.
berghofe
parents: 16417
diff changeset
   564
val (dB2, _) = Norm.type_NF (typing_of_term [] dummyf (term_of ct2));
20942
43e216a9d615 purged some ML legacy
haftmann
parents: 20838
diff changeset
   565
val ct2' = cterm_of thy (term_of_dB [] (#T (rep_cterm ct2)) dB2);
14063
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
   566
*}
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
   567
21011
19d7f07b0fa3 added codegen2 example
haftmann
parents: 20942
diff changeset
   568
text {*
19d7f07b0fa3 added codegen2 example
haftmann
parents: 20942
diff changeset
   569
The same story again for code next generation.
19d7f07b0fa3 added codegen2 example
haftmann
parents: 20942
diff changeset
   570
*}
19d7f07b0fa3 added codegen2 example
haftmann
parents: 20942
diff changeset
   571
22499
68c8a8390e16 fixed code generator setup
haftmann
parents: 22271
diff changeset
   572
setup {*
68c8a8390e16 fixed code generator setup
haftmann
parents: 22271
diff changeset
   573
  CodegenSerializer.add_undefined "SML" "arbitrary" "(raise Fail \"arbitrary\")"
68c8a8390e16 fixed code generator setup
haftmann
parents: 22271
diff changeset
   574
*}
68c8a8390e16 fixed code generator setup
haftmann
parents: 22271
diff changeset
   575
21011
19d7f07b0fa3 added codegen2 example
haftmann
parents: 20942
diff changeset
   576
code_gen
22499
68c8a8390e16 fixed code generator setup
haftmann
parents: 22271
diff changeset
   577
  type_NF nat int (SML #)
21011
19d7f07b0fa3 added codegen2 example
haftmann
parents: 20942
diff changeset
   578
19d7f07b0fa3 added codegen2 example
haftmann
parents: 20942
diff changeset
   579
ML {*
19d7f07b0fa3 added codegen2 example
haftmann
parents: 20942
diff changeset
   580
structure Norm = ROOT.WeakNorm;
19d7f07b0fa3 added codegen2 example
haftmann
parents: 20942
diff changeset
   581
structure Type = ROOT.Type;
19d7f07b0fa3 added codegen2 example
haftmann
parents: 20942
diff changeset
   582
structure Lambda = ROOT.Lambda;
22499
68c8a8390e16 fixed code generator setup
haftmann
parents: 22271
diff changeset
   583
structure Nat = ROOT.Nat;
21011
19d7f07b0fa3 added codegen2 example
haftmann
parents: 20942
diff changeset
   584
22512
04242efdcece fixed typing bug in generated code
haftmann
parents: 22499
diff changeset
   585
val nat_of_int = ROOT.Integer.nat o IntInf.fromInt;
04242efdcece fixed typing bug in generated code
haftmann
parents: 22499
diff changeset
   586
val int_of_nat = IntInf.toInt o ROOT.Integer.int;
21011
19d7f07b0fa3 added codegen2 example
haftmann
parents: 20942
diff changeset
   587
19d7f07b0fa3 added codegen2 example
haftmann
parents: 20942
diff changeset
   588
fun dBtype_of_typ (Type ("fun", [T, U])) =
19d7f07b0fa3 added codegen2 example
haftmann
parents: 20942
diff changeset
   589
      Type.Fun (dBtype_of_typ T, dBtype_of_typ U)
19d7f07b0fa3 added codegen2 example
haftmann
parents: 20942
diff changeset
   590
  | dBtype_of_typ (TFree (s, _)) = (case explode s of
19d7f07b0fa3 added codegen2 example
haftmann
parents: 20942
diff changeset
   591
        ["'", a] => Type.Atom (nat_of_int (ord a - 97))
19d7f07b0fa3 added codegen2 example
haftmann
parents: 20942
diff changeset
   592
      | _ => error "dBtype_of_typ: variable name")
19d7f07b0fa3 added codegen2 example
haftmann
parents: 20942
diff changeset
   593
  | dBtype_of_typ _ = error "dBtype_of_typ: bad type";
19d7f07b0fa3 added codegen2 example
haftmann
parents: 20942
diff changeset
   594
19d7f07b0fa3 added codegen2 example
haftmann
parents: 20942
diff changeset
   595
fun dB_of_term (Bound i) = Lambda.Var (nat_of_int i)
19d7f07b0fa3 added codegen2 example
haftmann
parents: 20942
diff changeset
   596
  | dB_of_term (t $ u) = Lambda.App (dB_of_term t, dB_of_term u)
19d7f07b0fa3 added codegen2 example
haftmann
parents: 20942
diff changeset
   597
  | dB_of_term (Abs (_, _, t)) = Lambda.Abs (dB_of_term t)
19d7f07b0fa3 added codegen2 example
haftmann
parents: 20942
diff changeset
   598
  | dB_of_term _ = error "dB_of_term: bad term";
19d7f07b0fa3 added codegen2 example
haftmann
parents: 20942
diff changeset
   599
19d7f07b0fa3 added codegen2 example
haftmann
parents: 20942
diff changeset
   600
fun term_of_dB Ts (Type ("fun", [T, U])) (Lambda.Abs dBt) =
19d7f07b0fa3 added codegen2 example
haftmann
parents: 20942
diff changeset
   601
      Abs ("x", T, term_of_dB (T :: Ts) U dBt)
19d7f07b0fa3 added codegen2 example
haftmann
parents: 20942
diff changeset
   602
  | term_of_dB Ts _ dBt = term_of_dB' Ts dBt
19d7f07b0fa3 added codegen2 example
haftmann
parents: 20942
diff changeset
   603
and term_of_dB' Ts (Lambda.Var n) = Bound (int_of_nat n)
19d7f07b0fa3 added codegen2 example
haftmann
parents: 20942
diff changeset
   604
  | term_of_dB' Ts (Lambda.App (dBt, dBu)) =
19d7f07b0fa3 added codegen2 example
haftmann
parents: 20942
diff changeset
   605
      let val t = term_of_dB' Ts dBt
19d7f07b0fa3 added codegen2 example
haftmann
parents: 20942
diff changeset
   606
      in case fastype_of1 (Ts, t) of
19d7f07b0fa3 added codegen2 example
haftmann
parents: 20942
diff changeset
   607
          Type ("fun", [T, U]) => t $ term_of_dB Ts T dBu
19d7f07b0fa3 added codegen2 example
haftmann
parents: 20942
diff changeset
   608
        | _ => error "term_of_dB: function type expected"
19d7f07b0fa3 added codegen2 example
haftmann
parents: 20942
diff changeset
   609
      end
19d7f07b0fa3 added codegen2 example
haftmann
parents: 20942
diff changeset
   610
  | term_of_dB' _ _ = error "term_of_dB: term not in normal form";
19d7f07b0fa3 added codegen2 example
haftmann
parents: 20942
diff changeset
   611
19d7f07b0fa3 added codegen2 example
haftmann
parents: 20942
diff changeset
   612
fun typing_of_term Ts e (Bound i) =
19d7f07b0fa3 added codegen2 example
haftmann
parents: 20942
diff changeset
   613
      Norm.Var (e, nat_of_int i, dBtype_of_typ (nth Ts i))
19d7f07b0fa3 added codegen2 example
haftmann
parents: 20942
diff changeset
   614
  | typing_of_term Ts e (t $ u) = (case fastype_of1 (Ts, t) of
21334
caa210551c01 adjusted name in generated code
haftmann
parents: 21210
diff changeset
   615
        Type ("fun", [T, U]) => Norm.Appa (e, dB_of_term t,
21011
19d7f07b0fa3 added codegen2 example
haftmann
parents: 20942
diff changeset
   616
          dBtype_of_typ T, dBtype_of_typ U, dB_of_term u,
19d7f07b0fa3 added codegen2 example
haftmann
parents: 20942
diff changeset
   617
          typing_of_term Ts e t, typing_of_term Ts e u)
19d7f07b0fa3 added codegen2 example
haftmann
parents: 20942
diff changeset
   618
      | _ => error "typing_of_term: function type expected")
19d7f07b0fa3 added codegen2 example
haftmann
parents: 20942
diff changeset
   619
  | typing_of_term Ts e (Abs (s, T, t)) =
19d7f07b0fa3 added codegen2 example
haftmann
parents: 20942
diff changeset
   620
      let val dBT = dBtype_of_typ T
21334
caa210551c01 adjusted name in generated code
haftmann
parents: 21210
diff changeset
   621
      in Norm.Absa (e, dBT, dB_of_term t,
21011
19d7f07b0fa3 added codegen2 example
haftmann
parents: 20942
diff changeset
   622
        dBtype_of_typ (fastype_of1 (T :: Ts, t)),
22499
68c8a8390e16 fixed code generator setup
haftmann
parents: 22271
diff changeset
   623
        typing_of_term (T :: Ts) (Type.shift e Nat.Zero_nat dBT) t)
21011
19d7f07b0fa3 added codegen2 example
haftmann
parents: 20942
diff changeset
   624
      end
19d7f07b0fa3 added codegen2 example
haftmann
parents: 20942
diff changeset
   625
  | typing_of_term _ _ _ = error "typing_of_term: bad term";
19d7f07b0fa3 added codegen2 example
haftmann
parents: 20942
diff changeset
   626
19d7f07b0fa3 added codegen2 example
haftmann
parents: 20942
diff changeset
   627
fun dummyf _ = error "dummy";
19d7f07b0fa3 added codegen2 example
haftmann
parents: 20942
diff changeset
   628
*}
19d7f07b0fa3 added codegen2 example
haftmann
parents: 20942
diff changeset
   629
19d7f07b0fa3 added codegen2 example
haftmann
parents: 20942
diff changeset
   630
ML {*
22512
04242efdcece fixed typing bug in generated code
haftmann
parents: 22499
diff changeset
   631
val ct1 = @{cterm "%f. ((%f x. f (f (f x))) ((%f x. f (f (f (f x)))) f))"};
21011
19d7f07b0fa3 added codegen2 example
haftmann
parents: 20942
diff changeset
   632
val (dB1, _) = ROOT.WeakNorm.type_NF (typing_of_term [] dummyf (term_of ct1));
19d7f07b0fa3 added codegen2 example
haftmann
parents: 20942
diff changeset
   633
val ct1' = cterm_of thy (term_of_dB [] (#T (rep_cterm ct1)) dB1);
19d7f07b0fa3 added codegen2 example
haftmann
parents: 20942
diff changeset
   634
22512
04242efdcece fixed typing bug in generated code
haftmann
parents: 22499
diff changeset
   635
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)))))"};
04242efdcece fixed typing bug in generated code
haftmann
parents: 22499
diff changeset
   636
val (dB2, _) = ROOT.WeakNorm.type_NF (typing_of_term [] dummyf (term_of ct2));
21011
19d7f07b0fa3 added codegen2 example
haftmann
parents: 20942
diff changeset
   637
val ct2' = cterm_of thy (term_of_dB [] (#T (rep_cterm ct2)) dB2);
19d7f07b0fa3 added codegen2 example
haftmann
parents: 20942
diff changeset
   638
*}
19d7f07b0fa3 added codegen2 example
haftmann
parents: 20942
diff changeset
   639
14063
e61a310cde02 New proof of weak normalization with program extraction.
berghofe
parents:
diff changeset
   640
end