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