%% Tait's logical relations argument for weak normalization of STLC
%%
%% This is based on the presentation in Types and Programming
%% Languages by Pierce.
Specification "stlc-weak-norm".
Close tm, ty.
%% Conventions:
%% Types: A, B, C
%% Terms: M, N, P, R, V
%% Values (Terms): V
%% Contexts: L
Define halts : tm -> prop by
halts M := exists V, {steps M V} /\ {value V}.
%% Reduce is the logical relation used to prove normalization.
%%
%% This definition uses itself recursively in a negative context (the
%% left side of an implication). In order to guarantee consistency of
%% the system we must ensure this definition is stratified. Note the
%% second argument to reduce is strictly smaller in the (negative)
%% recursive occurrence, thus the definition is indeed stratified.
Define reduce : tm -> ty -> prop by
reduce M top := {of M top} /\ halts M ;
reduce M (arrow A B) := {of M (arrow A B)} /\
halts M /\
(forall N, reduce N A -> reduce (app M N) B).
Theorem reduce_halts : forall A M, reduce M A -> halts M.
intros !A !M Hreduce.
case Hreduce.
- search.
- search.
Theorem reduce_of : forall A M, reduce M A -> {of M A}.
intros !A !M Hreduce.
case Hreduce.
- search.
- search.
%% Properties of evaluation
Theorem absurd_step_value : forall V M,
{step V M} -> {value V} -> false.
intros !V !M Hstep Hvalue.
case Hvalue.
case Hstep.
Theorem step_det : forall M N P,
{step M N} -> {step M P} -> N = P.
IHstep : induction on 1.
intros !M !N !P HstepMN HstepMP.
case HstepMN.
- HstepM1 => case HstepMP.
+ HstepM1b => apply IHstep to *HstepM1 *HstepM1b.
search.
+ HvalueM1 HstepN1 => apply absurd_step_value to *HstepM1 *HvalueM1.
+ HvalueN1 => case HstepM1.
- HvalueM1 HstepN1 => case HstepMP.
+ HstepM1b => apply absurd_step_value to *HstepM1b *HvalueM1.
+ HvalueM1b HstepN1b => apply IHstep to *HstepN1 *HstepN1b.
search.
+ HvalueN1 => apply absurd_step_value to *HstepN1 *HvalueN1.
- HvalueM1 => case HstepMP.
+ HstepAbs => case HstepAbs.
+ HvalueAbs HstepM1b => apply absurd_step_value to *HstepM1b *HvalueM1.
+ search.
Theorem step_follows_steps : forall M N V,
{step M N} -> {steps M V} -> {value V} -> {steps N V}.
intros !M !N !V Hstep Hsteps Hvalue.
case Hsteps.
- apply absurd_step_value to *Hstep *Hvalue.
- HstepP HstepsP => apply step_det to *Hstep *HstepP.
search.
Theorem steps_right : forall M N R,
{steps M N} -> {step N R} -> {steps M R}.
IHsteps : induction on 1.
intros !M !N !R Hsteps Hstep.
case Hsteps.
- search.
- HstepM HstepsP => apply IHsteps to *HstepsP *Hstep.
search.
Theorem steps_app_value : forall M N R,
{value M} -> {steps N R} -> {steps (app M N) (app M R)}.
IHsteps : induction on 2.
intros !M !N !R Hvalue Hsteps.
case Hsteps.
- search.
- HstepN HstepsP => apply IHsteps to Hvalue *HstepsP.
search.
Theorem halt_step_forward : forall M N,
halts M -> {step M N} -> halts N.
intros !M !N Hhalts Hstep.
HstepsV HvalueV : case Hhalts.
apply step_follows_steps to *Hstep *HstepsV HvalueV.
search.
Theorem halt_step_backward : forall M N,
halts N -> {step M N} -> halts M.
intros !M !N Hhalts Hstep.
HstepsV HvalueV : case Hhalts.
search.
Theorem of_step_forward : forall M N A,
{of M A} -> {step M N} -> {of N A}.
IHof : induction on 2.
intros !M !N !A Hof Hstep.
case Hstep.
- HstepM1 => !A2 HofM HofN : case Hof.
apply IHof to *HofM *HstepM1.
search.
- HvalueM1 HstepN1 => !A2 HofM HofN : case Hof.
apply IHof to *HofN *HstepN1.
search.
- !M1 !_ !A1 HvalueM1 => !A2 HofAbs HofM1 : case Hof.
HofAbsBody : case HofAbs.
HofAbsInst : inst *HofAbsBody with n1 = M1.
cut *HofAbsInst with *HofM1.
search.
Theorem reduce_step_forward : forall M N A,
reduce M A -> {step M N} -> reduce N A.
IHreduce : induction on 1.
intros !M !N !A Hreduce Hstep.
case Hreduce.
- Hof Hhalts => apply of_step_forward to *Hof Hstep.
apply halt_step_forward to *Hhalts *Hstep.
search.
- Hof Hhalts Hgen => unfold.
+ apply of_step_forward to *Hof *Hstep.
search.
+ apply halt_step_forward to *Hhalts *Hstep.
search.
+ intros !Narg HreduceNarg.
HreduceApp : apply *Hgen to *HreduceNarg.
apply IHreduce to *HreduceApp _.
search.
Theorem reduce_steps_forward : forall M N A,
reduce M A -> {steps M N} -> reduce N A.
IHreduce : induction on 2.
intros !M !N !A Hreduce Hsteps.
case Hsteps.
- search.
- HstepM HstepsP => HreduceP : apply reduce_step_forward to *Hreduce *HstepM.
apply IHreduce to *HreduceP *HstepsP.
search.
Theorem reduce_step_backward : forall M N A,
reduce N A -> {step M N} -> {of M A} -> reduce M A.
IHreduce : induction on 1.
intros !M !N !A Hreduce Hstep Hof.
case Hreduce.
- HofN HhaltsN => apply halt_step_backward to *HhaltsN *Hstep.
search.
- HofN HhaltsN Hgen => unfold.
+ search.
+ apply halt_step_backward to *HhaltsN *Hstep.
search.
+ intros !Narg HreduceNarg.
HreduceAppN : apply *Hgen to HreduceNarg.
apply reduce_of to *HreduceNarg.
apply IHreduce to *HreduceAppN _ _.
search.
Theorem reduce_steps_backward : forall M N A,
reduce N A -> {steps M N} -> {of M A} -> reduce M A.
IHreduce : induction on 2.
intros !M !N !A Hreduce Hsteps Hof.
case Hsteps.
- search.
- HstepM HstepsP => HofP : apply of_step_forward to Hof HstepM.
HreduceP : apply IHreduce to *Hreduce *HstepsP *HofP.
apply reduce_step_backward to *HreduceP *HstepM *Hof.
search.
%% Properties about nominal constants, e.g. where they can and cannot occur.
%% Also properties about the contexts of judgments.
Define ctx : olist -> prop by
ctx nil ;
nabla x, ctx (of x A :: L) := ctx L.
% A term is closed if it can be typed in the empty context
Define closed : tm -> prop by
closed M := exists A, {of M A}.
Define name : tm -> prop by
nabla x, name x.
Theorem ctx_member :
forall L E,
ctx L -> member E L ->
exists X A, E = of X A /\ name X.
IHctx : induction on 1.
intros !L !E Hctx Hmem.
case Hctx.
- case Hmem.
- HctxTail => case Hmem.
+ search.
+ HmemTail => apply IHctx to *HctxTail *HmemTail.
search.
Theorem member_prune : forall (L:olist) E, nabla (x:tm),
member (E x) L -> exists F, E = y\F.
IHmem : induction on 1.
intros Hmem.
case Hmem.
- search.
- HmemTail => apply IHmem to *HmemTail.
search.
% A term cannot contain a nominal variable which does not appear in
% its typing context L.
Theorem prune_of : forall L R A, nabla (x:tm),
ctx L -> {L |- of (R x) A} -> exists M, R = y\M.
IHprune : induction on 2.
intros !L !R !A Hctx Hof.
case Hof.
- HofM HofN => apply IHprune to Hctx *HofM.
apply IHprune to Hctx *HofN.
search.
- HofBody => apply IHprune to _ *HofBody.
search.
- HofTail Hmem => apply ctx_member to *Hctx Hmem.
case HofTail.
apply member_prune to *Hmem.
search.
% Closed terms cannot contain any nominal constants
Theorem prune_closed : forall R, nabla (x:tm),
closed (R x) -> exists M, R = y\M.
intros !R Hclosed.
Hof : case Hclosed.
apply prune_of to _ *Hof.
search.
Theorem reduce_closed : forall M A, reduce M A -> closed M.
intros !M !A Hreduce.
apply reduce_of to *Hreduce.
search.
Theorem prune_reduce : forall R A, nabla (x:tm),
reduce (R x) A -> exists M, R = y\M.
intros !R !A Hreduce.
Hclosed : apply reduce_closed to *Hreduce.
apply prune_closed to *Hclosed.
search.
%% Now we need to state the generalize theorem. This will require reasoning
%% about all possible closed instantiations of an open term.
% Suppose that {L |- of M A} is true. Then M is an open term with nominal
% constants that are listed in L. The judgment "subst L M N" holds for all
% instantiations N of the nominal constants in M with values that satisfy
% the reduce relation for their respective types. The key to defining this
% judgment is the use of nabla in the head of a definition which extracts
% a nominal constant from L and M. The definition of subst then substitutes
% for this nominal constant and continues processing the list L.
Define subst : olist -> tm -> tm -> prop by
subst nil M M ;
nabla x, subst (of x A :: L) (R x) M :=
exists V, reduce V A /\ {value V} /\ subst L (R V) M.
% Subst on a closed term has no effect
Theorem closed_subst : forall L M N,
closed M -> subst L M N -> M = N.
IHsubst : induction on 2.
intros !L !M !N Hclosed Hsubst.
case Hsubst.
- search.
- HreduceV HvalueV HsubstV => apply prune_closed to Hclosed.
apply IHsubst to *Hclosed *HsubstV.
search.
Theorem subst_var : forall L M N A,
ctx L -> member (of M A) L -> subst L M N -> reduce N A.
IHsubst : induction on 1.
intros !L !M !N !A Hctx Hmem Hsubst.
case Hctx.
- case Hmem.
- HctxTail => case Hmem.
+ HreduceV HvalueV HsubstV : case Hsubst.
HclosedV : apply reduce_closed to HreduceV.
apply closed_subst to *HclosedV *HsubstV.
search.
+ HmemTail => HreduceW HvalueW HsubstW : case Hsubst.
apply member_prune to HmemTail.
apply IHsubst to *HctxTail *HmemTail *HsubstW.
search.
Theorem subst_app : forall L M N R,
ctx L -> subst L (app M N) R ->
exists MR NR, R = app MR NR /\ subst L M MR /\ subst L N NR.
IHsubst : induction on 1.
intros !L !M !N !R Hctx Hsubst.
case Hctx.
- case Hsubst.
search.
- HctxTail => HreduceV HvalueV HsubstV : case Hsubst.
apply IHsubst to *HctxTail *HsubstV.
search.
Theorem subst_abs : forall L M R A,
ctx L -> subst L (abs A M) R ->
exists MR, R = abs A MR /\
(forall V, reduce V A -> {value V} ->
nabla x, subst (of x A :: L) (M x) (MR V)).
IHsubst : induction on 1.
intros !L !M !R !A Hctx Hsubst.
case Hctx.
- case Hsubst.
exists M.
split.
+ search.
+ intros !V HreduceV HvalueV.
search.
- HctxTail => HreduceV HvalueV HsubstV : case Hsubst.
!MR HsubstAbs : apply IHsubst to *HctxTail *HsubstV.
exists MR.
split.
+ search.
+ intros !W HreduceW HvalueW.
apply prune_reduce to HreduceW.
HsubstW : apply *HsubstAbs to *HreduceW *HvalueW.
case HsubstW.
search.
Theorem subst_preserves_ty : forall L M N A,
ctx L -> subst L M N -> {L |- of M A} -> {of N A}.
IHty : induction on 1.
intros !L !M !N !A Hctx Hsubst Hof.
case Hctx.
- case Hsubst.
search.
- HctxTail => !V HreduceV HvalueV HsubstV : case Hsubst.
HofV : apply reduce_of to *HreduceV.
HofInst : inst *Hof with n1 = V.
HofCut : cut *HofInst with *HofV.
apply IHty to *HctxTail *HsubstV *HofCut.
search.
Theorem weak_norm_ext : forall L M R A,
ctx L -> {L |- of M A} -> subst L M R -> reduce R A.
IHext : induction on 2.
intros !L !M !R !A Hctx Hof Hsubst.
case Hof (keep).
- HofM HofN => HsubstM HsubstN : apply subst_app to Hctx *Hsubst.
HreduceM : apply IHext to Hctx *HofM *HsubstM.
HreduceN : apply IHext to *Hctx *HofN *HsubstN.
HofMarr HhaltsM HgenMN : case HreduceM.
apply *HgenMN to *HreduceN.
search.
- !_ !_ !A1 HofAbs => !MR HgenAbs : apply subst_abs to Hctx Hsubst.
apply subst_preserves_ty to Hctx *Hsubst *Hof.
unfold.
+ search.
+ search.
+ intros !Narg HreduceNarg.
HhaltsNarg : apply reduce_halts to HreduceNarg.
!Varg HstepsV HvalueV : case HhaltsNarg.
HreduceV : apply reduce_steps_forward to HreduceNarg HstepsV.
HsubstBody : apply *HgenAbs to *HreduceV HvalueV.
HreduceBody : apply IHext to _ *HofAbs *HsubstBody.
HstepsApp : apply steps_app_value to _ HstepsV
with M = abs A1 MR.
HstepsBeta : apply steps_right to HstepsApp _
with R = MR Varg.
apply reduce_of to *HreduceNarg.
apply reduce_steps_backward to *HreduceBody *HstepsBeta _.
search.
- HofTail HmemTail => apply ctx_member to Hctx HmemTail.
case HofTail.
apply subst_var to *Hctx *HmemTail *Hsubst.
search.
Theorem weak_norm : forall M A, {of M A} -> halts M.
intros !M !A Hof.
Hreduce : apply weak_norm_ext to _ *Hof _.
apply reduce_halts to *Hreduce.
search.