Library L
Inductive term : Type :=
| var (n : nat) : term
| app (s : term) (t : term) : term
| lam (s : term).
Implicit Types s t u : term.
Coercion app : term >-> Funclass.
Notation "# v" := (var v) (at level 1, format "# v"): L_scope.
Open Scope L_scope.
Notation "(λ s )" := (lam s) (right associativity, at level 0).
Instance term_eq_dec : eq_dec term.
Definition term_eq_dec_proc s t := if decision (s = t) then true else false.
Hint Resolve term_eq_dec.
Require Import String.
Inductive bterm : Type :=
| bvar (x : string) : bterm
| bapp (s t : bterm) : bterm
| blam (x : string) (s : bterm) : bterm
| bter (s : term) : bterm.
Open Scope string_scope.
Instance eq_dec_string : eq_dec string.
Fixpoint convert' (F : list string) (s : bterm) : term :=
match s with
| bvar x ⇒ match pos x F with None ⇒ #100 | Some t ⇒ # t end
| bapp s t ⇒ app (convert' F s) (convert' F t)
| blam x s ⇒ lam (convert' (x:: F) s)
| bter t ⇒ t
end.
Coercion bvar : string >-> bterm.
Coercion bapp : bterm >-> Funclass.
Definition convert:=convert' [].
Coercion convert : bterm >-> term.
Notation ".\ x , .. , y ; t" := ((blam x .. (blam y t) .. )) (at level 100, right associativity).
Notation "'λ' x , .. , y ; t" := ((blam x .. (blam y t) .. )) (at level 100, right associativity).
Notation "'!!' s" := (bter s) (at level 0).
Definition r : term := Eval simpl in .\"r","f";"f" (.\"x";"r" "r" "f" "x").
Definition R : term := r r.
Definition rho s : term := Eval simpl in .\"x";!!r !!r !!s "x".
Definition I : term := Eval simpl in .\"x"; "x".
Definition K : term := Eval simpl in .\"x","y"; "x".
Definition omega : term := Eval simpl in .\"x"; "x" "x".
Definition Omega : term := omega omega.
Fixpoint subst (s : term) (k : nat) (u : term) :=
match s with
| var n ⇒ if decision (n = k) then u else (var n)
| app s t ⇒ app (subst s k u) (subst t k u)
| lam s ⇒ lam (subst s (S k) u)
end.
Definition closed s := ∀ n u, subst s n u = s.
Definition lambda s := ∃ t, s = lam t.
Definition proc s := closed s ∧ lambda s.
Lemma lambda_lam s : lambda (lam s).
Hint Resolve lambda_lam.
Instance lambda_dec s : dec (lambda s).
Lemma var_not_closed n: ¬closed (var n).
Lemma closed_l (s t:term): closed (s t) → closed s.
Lemma closed_r (s t:term): closed (s t) → closed t.
Fixpoint size (t : term) :=
match t with
| var n ⇒ 0
| app s t ⇒ 1+ size s + size t
| lam s ⇒ 1 + size s
end.
Inductive dclosed : nat → term → Prop :=
| dclvar k n : k > n → dclosed k (var n)
| dclApp k s t : dclosed k s → dclosed k t → dclosed k (s t)
| dcllam k s : dclosed (S k) s → dclosed k (lam s).
Lemma dclosedApp_iff n (s t : term): dclosed n (s t) ↔ dclosed n s ∧ dclosed n t.
Lemma dclosedVar_iff n x : dclosed n (# x) ↔ x < n.
Lemma dclosedLam_iff n s : dclosed n (lam s) ↔ dclosed (S n) s.
Lemma dclosed_closed_k s k u : dclosed k s → subst s k u = s.
Lemma dclosed_ge k s : dclosed k s → ∀ m, m ≥ k → dclosed m s.
Lemma dclosed_gt k s : dclosed k s → ∀ m, m > k → dclosed m s.
Lemma dclosed_closed s k u : dclosed 0 s → subst s k u = s.
Lemma closed_k_dclosed k s : (∀ n u, n ≥ k → subst s n u = s) → dclosed k s.
Lemma closed_dcl s : closed s ↔ dclosed 0 s.
Lemma closed_dclosed s k : closed s → dclosed k s.
Lemma closed_app (s t : term) : closed (s t) → closed s ∧ closed t.
Lemma app_closed (s t : term) : closed s → closed t → closed (s t).
Instance dclosed_dec k s : dec (dclosed k s).
Instance closed_dec s : dec (closed s).
Lemma proc_dec s : dec (proc s).
Reserved Notation "s '>>' t" (at level 50).
Inductive step : term → term → Prop :=
| stepApp s t : app (lam s) (lam t) >> subst s 0 (lam t)
| stepAppR s t t' : t >> t' → app s t >> app s t'
| stepAppL s s' t : s >> s' → app s t >> app s' t
where "s '>>' t" := (step s t).
Hint Constructors step.
Lemma closed_subst s t k : dclosed (S k) s → dclosed k t → dclosed k (subst s k t).
Lemma closed_step s t : s >> t → closed s → closed t.
Lemma comb_proc_red s : closed s → proc s ∨ ∃ t, s >> t.
Lemma irred_iff s : closed s → (irred step s ↔ lambda s).
Ltac inv_step :=
match goal with
| H : step (app _ _) _ |- _ ⇒ inv H
| H : step (lam _) _ |- _ ⇒ inv H
| H : step (var _) _ |- _ ⇒ inv H
end.
Theorem uniform_confluence : uniformly_confluent step.
Notation "s '>^' k" := (pow step k s) (at level 9, format "s '>^' k").
Lemma step_pow_app_l s s' t n: s >^n s' → (s t) >^n (s' t).
Lemma step_pow_app_r s t t' n: t >^n t' → (s t) >^n (s t').
Lemma step_pow_app s s' t t' k l: s >^k s' → t >^l t' → (s t) >^(k+l) (s' t').
Lemma confluence : confluent step.
Lemma step_value s v :
lambda v → (lam s) v >> subst s 0 v.
Notation "s '>*' t" := (star step s t) (at level 50).
Lemma step_star s s':
s >> s' → s >* s'.
Lemma star_trans_l s s' t :
s >* s' → s t >* s' t.
Lemma star_trans_r (s s' t:term):
s >* s' → t s >* t s'.
Instance star_step_app_proper :
Proper ((star step) ==> (star step) ==> (star step)) app.
Lemma closed_star s t: s >* t → closed s → closed t.
Instance star_closed_proper :
Proper ((star step) ==> Basics.impl) closed.
Reserved Notation "s '==' t" (at level 50).
Inductive equiv : term → term → Prop :=
| eqStep s t : step s t → s == t
| eqRef s : s == s
| eqSym s t : t == s → s == t
| eqTrans s t u: s == t → t == u → s == u
where "s '==' t" := (equiv s t).
Hint Immediate eqRef.
Instance equiv_Equivalence : Equivalence equiv.
Lemma equiv_ecl s t : s == t ↔ ecl step s t.
Lemma church_rosser s t : s == t → ∃ u, s >* u ∧ t >* u.
Lemma star_equiv s t :
s >* t → s == t.
Hint Resolve star_equiv.
Instance star_equiv_subrelation : subrelation (star step) equiv.
Instance step_equiv_subrelation : subrelation step equiv.
Lemma equiv_lambda s t : lambda t → s == t → s >* t.
Lemma eqStarT s t u : s >* t → t == u → s == u.
Lemma eqApp s s' u u' : s == s' → u == u' → s u == s' u'.
Instance equiv_app_proper :
Proper (equiv ==> equiv ==> equiv) app.
Definition eval s t := s >* t ∧ lambda t.
Hint Unfold eval.
Definition converges s := ∃ t, s == t ∧ lambda t.
Lemma converges_equiv s t : s == t → (converges s ↔ converges t).
Instance converges_proper :
Proper (equiv ==> iff) converges.
Lemma unique_normal_forms (s t : term) : lambda s → lambda t → s == t → s = t.
Lemma pow_trans_lam' t v s k n :
lambda v → pow step n t v → pow step (S k) t s → ∃ m, m < n ∧ pow step m s v.
Lemma pow_trans_lam t v s k n :
lambda v → pow step n t v → pow step (S k) t s → ∃ m, m < n ∧ pow step m s v.
Lemma powSk t t' s : t >> t' → t' >* s → ∃ k, pow step (S k) t s.
Close Scope L_scope.