%% Lambda calculus and various results on typing and evaluation
%%
%% Even though we use higher-order abstract syntax here, we do not need
%% to define a meta-level context predicate because all contexts are
%% empty in the inductions.
Specification "eval".
%%
%% Determinancy
%%
Theorem eval_det :
forall E V1 V2, {eval E V1} -> {eval E V2} -> V1 = V2.
IH : induction on 1. intros Heval1 Heval2. case Heval1.
- case Heval2. search.
- HevalM1 HevalN1 => HevalM2 HevalN2 : case Heval2.
apply IH to HevalM1 HevalM2. apply IH to HevalN1 HevalN2.
search.
Theorem step_det :
forall E1 E2 E3, {step E1 E2} -> {step E1 E3} -> E2 = E3.
IH : induction on 1. intros Hstep1 Hstep2. case Hstep1.
- case Hstep2.
+ search.
+ HstepAbs => case HstepAbs.
- HstepTail => case Hstep2.
+ case HstepTail.
+ HstepTail2 => apply IH to HstepTail HstepTail2. search.
Theorem nstep_det :
forall E R1 R2, {nstep E (abs R1)} -> {nstep E (abs R2)} -> R1 = R2.
IH : induction on 1. intros Hnstep1 Hnstep2. case Hnstep1.
- case Hnstep2.
+ search.
+ HstepAbs => case HstepAbs.
- HstepTail HnstepTail => case Hnstep2.
+ case HstepTail.
+ HstepTail2 HnstepTail2 => apply step_det to HstepTail HstepTail2.
apply IH to HnstepTail HnstepTail2. search.
%%
%% Equivalence of semantics
%%
Theorem nstep_lemma :
forall M N R V, {nstep M (abs R)} -> {nstep (R N) V} -> {nstep (app M N) V}.
IH : induction on 1. intros Hnstep1 Hnstep2. case Hnstep1.
- search.
- HstepTail HnstepTail => apply IH to HnstepTail Hnstep2. search.
Theorem eval_nstep :
forall E V, {eval E V} -> {nstep E V}.
IH : induction on 1. intros Heval. case Heval.
- search.
- HevalM HevalN => HnstepM : apply IH to HevalM.
HnstepN : apply IH to HevalN.
apply nstep_lemma to HnstepM HnstepN. search.
Theorem step_eval_lemma :
forall A B C, {step A B} -> {eval B C} -> {eval A C}.
IH : induction on 1. intros Hstep Heval. case Hstep.
- search.
- HstepTail => HevalM HevalN : case Heval.
apply IH to HstepTail HevalM. search.
Theorem nstep_eval :
forall E R, {nstep E (abs R)} -> {eval E (abs R)}.
IH : induction on 1. intros Hnstep. case Hnstep.
- search.
- Hstep HnstepTail => HevalB : apply IH to HnstepTail.
apply step_eval_lemma to Hstep HevalB. search.
%%
%% Subject reduction
%%
Theorem sr_eval :
forall E V T, {eval E V} -> {of E T} -> {of V T}.
IH : induction on 1. intros Heval Hof. case Heval.
- search.
- !N HevalM HevalN => HofM HofN : case Hof.
HofAbs : apply IH to HevalM HofM.
HseqBody : case HofAbs.
HseqBodyInst : inst HseqBody with n1 = N.
HofRN : cut HseqBodyInst with HofN.
apply IH to HevalN HofRN. search.
Theorem sr_step :
forall E1 E2 T, {step E1 E2} -> {of E1 T} -> {of E2 T}.
IH : induction on 1. intros Hstep Hof. case Hstep.
- !M => HofM HofN : case Hof.
HseqBody : case HofM.
HseqBodyInst : inst HseqBody with n1 = M.
HofCut : cut HseqBodyInst with HofN. search.
- HstepTail => HofM HofN : case Hof.
HofM' : apply IH to HstepTail HofM. search.
Theorem sr_nstep :
forall E1 E2 T, {nstep E1 E2} -> {of E1 T} -> {of E2 T}.
IH : induction on 1. intros Hnstep Hof. case Hnstep.
- search.
- Hstep HnstepTail => HofB : apply sr_step to Hstep Hof.
apply IH to HnstepTail HofB. search.
%%
%% Misc
%%
% A cut property for typing judgments
Theorem type_subst : forall L E E' T T', nabla x,
{L, of x T' |- of (E x) T} -> {L |- of E' T'} ->
{L |- of (E E') T}.
intros !_ !_ !E' Hseq1 Hseq2.
HseqInst : inst Hseq1 with n1 = E'.
cut HseqInst with Hseq2. search.
% Self application cannot be simply-typed
Theorem of_self_app_absurd : forall T,
{of (abs (x\ app x x)) T} -> false.
intros Hseq. HseqBody : case Hseq. case HseqBody.
- HofM HofN =>
HseqBack1 Hmem1 : case HofM.
case Hmem1.
+ case HseqBack1.
HseqBack2 Hmem2 : case HofN.
case Hmem2.
* case HseqBack2.
* HmemNil2 => case HmemNil2.
+ HmemNil1 => case HmemNil1.
- HseqBack Hmem =>
case Hmem.
+ case HseqBack.
+ HmemTail => case HmemTail.
% The term (lambda x . x x) (lambda x . x x) does not evaluate to a value
Theorem no_eval : forall V,
{eval (app (abs x\ app x x) (abs x\ app x x)) V} -> false.
IH : induction on 1. intros Heval. case Heval.
HevalM HevalN => case HevalM. apply IH to HevalN.
% We can also define divergence coinductivly to prove that
% (lambda x . x x) (lambda x . x x) diverges
CoDefine diverge : tm -> prop by
diverge (app M N) := diverge M ;
diverge (app M N) := exists R, {eval M (abs R)} /\ diverge (R N).
Theorem omega_diverge :
diverge (app (abs x\ app x x) (abs x\ app x x)).
coinduction. search.
% More generally we can prove that evaluation and divergence are
% mutually exclusive
Theorem eval_diverge_absurd : forall M V,
{eval M V} -> diverge M -> false.
IH : induction on 1. intros Heval Hdiv. case Heval.
- case Hdiv.
- HevalM HevalN => case Hdiv.
+ HdivM => apply IH to HevalM HdivM.
+ HevalM2 HdivTail => apply eval_det to HevalM HevalM2.
apply IH to HevalN HdivTail.