src/HOL/Lambda/Lambda.thy
author wenzelm
Wed May 12 14:17:26 2010 +0200 (2010-05-12)
changeset 36862 952b2b102a0a
parent 25974 0cb35fa9c6fa
child 39126 ee117c5b3b75
permissions -rw-r--r--
removed obsolete CVS Ids;
nipkow@1269
     1
(*  Title:      HOL/Lambda/Lambda.thy
nipkow@1120
     2
    Author:     Tobias Nipkow
nipkow@1120
     3
    Copyright   1995 TU Muenchen
nipkow@1120
     4
*)
nipkow@1120
     5
wenzelm@9811
     6
header {* Basic definitions of Lambda-calculus *}
wenzelm@9811
     7
haftmann@16417
     8
theory Lambda imports Main begin
wenzelm@9811
     9
nipkow@1120
    10
wenzelm@9811
    11
subsection {* Lambda-terms in de Bruijn notation and substitution *}
wenzelm@9811
    12
wenzelm@9811
    13
datatype dB =
wenzelm@9811
    14
    Var nat
wenzelm@12011
    15
  | App dB dB (infixl "\<degree>" 200)
wenzelm@9811
    16
  | Abs dB
nipkow@1120
    17
wenzelm@25974
    18
primrec
wenzelm@9811
    19
  lift :: "[dB, nat] => dB"
wenzelm@25974
    20
where
wenzelm@25974
    21
    "lift (Var i) k = (if i < k then Var i else Var (i + 1))"
wenzelm@25974
    22
  | "lift (s \<degree> t) k = lift s k \<degree> lift t k"
wenzelm@25974
    23
  | "lift (Abs s) k = Abs (lift s (k + 1))"
nipkow@1153
    24
berghofe@5184
    25
primrec
wenzelm@25974
    26
  subst :: "[dB, dB, nat] => dB"  ("_[_'/_]" [300, 0, 0] 300)
wenzelm@25974
    27
where (* FIXME base names *)
wenzelm@25974
    28
    subst_Var: "(Var i)[s/k] =
wenzelm@25974
    29
      (if k < i then Var (i - 1) else if i = k then s else Var i)"
wenzelm@25974
    30
  | subst_App: "(t \<degree> u)[s/k] = t[s/k] \<degree> u[s/k]"
wenzelm@25974
    31
  | subst_Abs: "(Abs t)[s/k] = Abs (t[lift s 0 / k+1])"
wenzelm@9811
    32
wenzelm@9811
    33
declare subst_Var [simp del]
wenzelm@9811
    34
wenzelm@9811
    35
text {* Optimized versions of @{term subst} and @{term lift}. *}
wenzelm@9811
    36
wenzelm@25974
    37
primrec
wenzelm@9811
    38
  liftn :: "[nat, dB, nat] => dB"
wenzelm@25974
    39
where
wenzelm@25974
    40
    "liftn n (Var i) k = (if i < k then Var i else Var (i + n))"
wenzelm@25974
    41
  | "liftn n (s \<degree> t) k = liftn n s k \<degree> liftn n t k"
wenzelm@25974
    42
  | "liftn n (Abs s) k = Abs (liftn n s (k + 1))"
wenzelm@9811
    43
wenzelm@9811
    44
primrec
wenzelm@25974
    45
  substn :: "[dB, dB, nat] => dB"
wenzelm@25974
    46
where
wenzelm@25974
    47
    "substn (Var i) s k =
wenzelm@25974
    48
      (if k < i then Var (i - 1) else if i = k then liftn k s 0 else Var i)"
wenzelm@25974
    49
  | "substn (t \<degree> u) s k = substn t s k \<degree> substn u s k"
wenzelm@25974
    50
  | "substn (Abs t) s k = Abs (substn t s (k + 1))"
nipkow@1120
    51
wenzelm@9811
    52
wenzelm@9811
    53
subsection {* Beta-reduction *}
nipkow@1153
    54
berghofe@23750
    55
inductive beta :: "[dB, dB] => bool"  (infixl "\<rightarrow>\<^sub>\<beta>" 50)
berghofe@22271
    56
  where
berghofe@22271
    57
    beta [simp, intro!]: "Abs s \<degree> t \<rightarrow>\<^sub>\<beta> s[t/0]"
berghofe@22271
    58
  | appL [simp, intro!]: "s \<rightarrow>\<^sub>\<beta> t ==> s \<degree> u \<rightarrow>\<^sub>\<beta> t \<degree> u"
berghofe@22271
    59
  | appR [simp, intro!]: "s \<rightarrow>\<^sub>\<beta> t ==> u \<degree> s \<rightarrow>\<^sub>\<beta> u \<degree> t"
berghofe@22271
    60
  | abs [simp, intro!]: "s \<rightarrow>\<^sub>\<beta> t ==> Abs s \<rightarrow>\<^sub>\<beta> Abs t"
wenzelm@21404
    61
wenzelm@21404
    62
abbreviation
wenzelm@21404
    63
  beta_reds :: "[dB, dB] => bool"  (infixl "->>" 50) where
berghofe@22271
    64
  "s ->> t == beta^** s t"
wenzelm@19086
    65
wenzelm@21210
    66
notation (latex)
wenzelm@19656
    67
  beta_reds  (infixl "\<rightarrow>\<^sub>\<beta>\<^sup>*" 50)
nipkow@1120
    68
berghofe@23750
    69
inductive_cases beta_cases [elim!]:
berghofe@14065
    70
  "Var i \<rightarrow>\<^sub>\<beta> t"
berghofe@14065
    71
  "Abs r \<rightarrow>\<^sub>\<beta> s"
berghofe@14065
    72
  "s \<degree> t \<rightarrow>\<^sub>\<beta> u"
wenzelm@9811
    73
wenzelm@9811
    74
declare if_not_P [simp] not_less_eq [simp]
wenzelm@9811
    75
  -- {* don't add @{text "r_into_rtrancl[intro!]"} *}
wenzelm@9811
    76
wenzelm@9811
    77
wenzelm@9811
    78
subsection {* Congruence rules *}
wenzelm@9811
    79
wenzelm@9811
    80
lemma rtrancl_beta_Abs [intro!]:
berghofe@14065
    81
    "s \<rightarrow>\<^sub>\<beta>\<^sup>* s' ==> Abs s \<rightarrow>\<^sub>\<beta>\<^sup>* Abs s'"
berghofe@23750
    82
  by (induct set: rtranclp) (blast intro: rtranclp.rtrancl_into_rtrancl)+
wenzelm@9811
    83
wenzelm@9811
    84
lemma rtrancl_beta_AppL:
berghofe@14065
    85
    "s \<rightarrow>\<^sub>\<beta>\<^sup>* s' ==> s \<degree> t \<rightarrow>\<^sub>\<beta>\<^sup>* s' \<degree> t"
berghofe@23750
    86
  by (induct set: rtranclp) (blast intro: rtranclp.rtrancl_into_rtrancl)+
wenzelm@9811
    87
wenzelm@9811
    88
lemma rtrancl_beta_AppR:
berghofe@14065
    89
    "t \<rightarrow>\<^sub>\<beta>\<^sup>* t' ==> s \<degree> t \<rightarrow>\<^sub>\<beta>\<^sup>* s \<degree> t'"
berghofe@23750
    90
  by (induct set: rtranclp) (blast intro: rtranclp.rtrancl_into_rtrancl)+
wenzelm@9811
    91
wenzelm@9811
    92
lemma rtrancl_beta_App [intro]:
berghofe@14065
    93
    "[| s \<rightarrow>\<^sub>\<beta>\<^sup>* s'; t \<rightarrow>\<^sub>\<beta>\<^sup>* t' |] ==> s \<degree> t \<rightarrow>\<^sub>\<beta>\<^sup>* s' \<degree> t'"
berghofe@23750
    94
  by (blast intro!: rtrancl_beta_AppL rtrancl_beta_AppR intro: rtranclp_trans)
wenzelm@9811
    95
wenzelm@9811
    96
wenzelm@9811
    97
subsection {* Substitution-lemmas *}
wenzelm@9811
    98
wenzelm@9811
    99
lemma subst_eq [simp]: "(Var k)[u/k] = u"
wenzelm@18241
   100
  by (simp add: subst_Var)
wenzelm@9811
   101
wenzelm@9811
   102
lemma subst_gt [simp]: "i < j ==> (Var j)[u/i] = Var (j - 1)"
wenzelm@18241
   103
  by (simp add: subst_Var)
wenzelm@9811
   104
wenzelm@9811
   105
lemma subst_lt [simp]: "j < i ==> (Var j)[u/i] = Var j"
wenzelm@18241
   106
  by (simp add: subst_Var)
wenzelm@9811
   107
wenzelm@18241
   108
lemma lift_lift:
wenzelm@18241
   109
    "i < k + 1 \<Longrightarrow> lift (lift t i) (Suc k) = lift (lift t k) i"
wenzelm@20503
   110
  by (induct t arbitrary: i k) auto
wenzelm@9811
   111
wenzelm@9811
   112
lemma lift_subst [simp]:
wenzelm@18241
   113
    "j < i + 1 \<Longrightarrow> lift (t[s/j]) i = (lift t (i + 1)) [lift s i / j]"
wenzelm@20503
   114
  by (induct t arbitrary: i j s)
wenzelm@18241
   115
    (simp_all add: diff_Suc subst_Var lift_lift split: nat.split)
wenzelm@9811
   116
wenzelm@9811
   117
lemma lift_subst_lt:
wenzelm@18241
   118
    "i < j + 1 \<Longrightarrow> lift (t[s/j]) i = (lift t i) [lift s i / j + 1]"
wenzelm@20503
   119
  by (induct t arbitrary: i j s) (simp_all add: subst_Var lift_lift)
wenzelm@9811
   120
wenzelm@9811
   121
lemma subst_lift [simp]:
wenzelm@18241
   122
    "(lift t k)[s/k] = t"
wenzelm@20503
   123
  by (induct t arbitrary: k s) simp_all
wenzelm@9811
   124
wenzelm@18241
   125
lemma subst_subst:
wenzelm@18241
   126
    "i < j + 1 \<Longrightarrow> t[lift v i / Suc j][u[v/j]/i] = t[u/i][v/j]"
wenzelm@20503
   127
  by (induct t arbitrary: i j u v)
wenzelm@18241
   128
    (simp_all add: diff_Suc subst_Var lift_lift [symmetric] lift_subst_lt
wenzelm@9811
   129
      split: nat.split)
wenzelm@9811
   130
wenzelm@9811
   131
wenzelm@9811
   132
subsection {* Equivalence proof for optimized substitution *}
wenzelm@9811
   133
wenzelm@18241
   134
lemma liftn_0 [simp]: "liftn 0 t k = t"
wenzelm@20503
   135
  by (induct t arbitrary: k) (simp_all add: subst_Var)
wenzelm@9811
   136
wenzelm@18241
   137
lemma liftn_lift [simp]: "liftn (Suc n) t k = lift (liftn n t k) k"
wenzelm@20503
   138
  by (induct t arbitrary: k) (simp_all add: subst_Var)
wenzelm@9811
   139
wenzelm@18241
   140
lemma substn_subst_n [simp]: "substn t s n = t[liftn n s 0 / n]"
wenzelm@20503
   141
  by (induct t arbitrary: n) (simp_all add: subst_Var)
wenzelm@9811
   142
wenzelm@9811
   143
theorem substn_subst_0: "substn t s 0 = t[s/0]"
wenzelm@18241
   144
  by simp
wenzelm@9811
   145
wenzelm@9811
   146
wenzelm@9811
   147
subsection {* Preservation theorems *}
wenzelm@9811
   148
wenzelm@9811
   149
text {* Not used in Church-Rosser proof, but in Strong
wenzelm@9811
   150
  Normalization. \medskip *}
wenzelm@9811
   151
berghofe@13915
   152
theorem subst_preserves_beta [simp]:
wenzelm@18257
   153
    "r \<rightarrow>\<^sub>\<beta> s ==> r[t/i] \<rightarrow>\<^sub>\<beta> s[t/i]"
wenzelm@20503
   154
  by (induct arbitrary: t i set: beta) (simp_all add: subst_subst [symmetric])
wenzelm@9811
   155
berghofe@14065
   156
theorem subst_preserves_beta': "r \<rightarrow>\<^sub>\<beta>\<^sup>* s ==> r[t/i] \<rightarrow>\<^sub>\<beta>\<^sup>* s[t/i]"
berghofe@23750
   157
  apply (induct set: rtranclp)
berghofe@23750
   158
   apply (rule rtranclp.rtrancl_refl)
berghofe@23750
   159
  apply (erule rtranclp.rtrancl_into_rtrancl)
berghofe@14065
   160
  apply (erule subst_preserves_beta)
berghofe@14065
   161
  done
berghofe@14065
   162
berghofe@13915
   163
theorem lift_preserves_beta [simp]:
wenzelm@18257
   164
    "r \<rightarrow>\<^sub>\<beta> s ==> lift r i \<rightarrow>\<^sub>\<beta> lift s i"
wenzelm@20503
   165
  by (induct arbitrary: i set: beta) auto
wenzelm@9811
   166
berghofe@14065
   167
theorem lift_preserves_beta': "r \<rightarrow>\<^sub>\<beta>\<^sup>* s ==> lift r i \<rightarrow>\<^sub>\<beta>\<^sup>* lift s i"
berghofe@23750
   168
  apply (induct set: rtranclp)
berghofe@23750
   169
   apply (rule rtranclp.rtrancl_refl)
berghofe@23750
   170
  apply (erule rtranclp.rtrancl_into_rtrancl)
berghofe@14065
   171
  apply (erule lift_preserves_beta)
berghofe@14065
   172
  done
berghofe@14065
   173
wenzelm@18241
   174
theorem subst_preserves_beta2 [simp]: "r \<rightarrow>\<^sub>\<beta> s ==> t[r/i] \<rightarrow>\<^sub>\<beta>\<^sup>* t[s/i]"
wenzelm@20503
   175
  apply (induct t arbitrary: r s i)
berghofe@23750
   176
    apply (simp add: subst_Var r_into_rtranclp)
wenzelm@9811
   177
   apply (simp add: rtrancl_beta_App)
wenzelm@9811
   178
  apply (simp add: rtrancl_beta_Abs)
wenzelm@9811
   179
  done
wenzelm@9811
   180
berghofe@14065
   181
theorem subst_preserves_beta2': "r \<rightarrow>\<^sub>\<beta>\<^sup>* s ==> t[r/i] \<rightarrow>\<^sub>\<beta>\<^sup>* t[s/i]"
berghofe@23750
   182
  apply (induct set: rtranclp)
berghofe@23750
   183
   apply (rule rtranclp.rtrancl_refl)
berghofe@23750
   184
  apply (erule rtranclp_trans)
berghofe@14065
   185
  apply (erule subst_preserves_beta2)
berghofe@14065
   186
  done
berghofe@14065
   187
wenzelm@11638
   188
end