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