# Library L

Require Export Base UnifConfl.

# Syntax of the weak call-by-value lambda calculus

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.

# Notation using binders

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 xmatch pos x F with None#100 | Some t# t end
| bapp s tapp (convert' F s) (convert' F t)
| blam x slam (convert' (x:: F) s)
| bter tt
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).

# Important terms

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.

# Substitution

Fixpoint subst (s : term) (k : nat) (u : term) :=
match s with
| var nif decision (n = k) then u else (var n)
| app s tapp (subst s k u) (subst t k u)
| lam slam (subst s (S k) u)
end.

# Important definitions

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.

# Size of terms

Fixpoint size (t : term) :=
match t with
| var n ⇒ 0
| app s t ⇒ 1+ size s + size t
| lam s ⇒ 1 + size s
end.

## Alternative definition of closedness

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).

# Reduction

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.

# Properties of the reduction relation

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.

## Properties of the reflexive, transitive closure of reduction

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.

# Equivalence

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.

## Properties of the equivalence relation

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 of convergence

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.

# Eta expansion

Lemma Eta (s : term ) t : closed s lambda t (lam (s #0)) t == s t.

# Useful lemmas

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.