RunST.rec_beta

From RunST Require Import lang typing contextual_refinement fundamental
     soundness rec_unfolding lam_beta.lam_beta.

Lemma rec_beta Γ e1' e2' τ τ' :
  TArrow τ τ' :: τ :: Γ ⊢ₜ e1' : τ' ->
  Γ ⊢ₜ e2' : τ ->
  Γ App (Rec e1') e2' ctx e1'.[Rec e1', e2'/] : τ'.
Proof.
  intros Ht1 Ht2.
  eapply ctx_refines_trans.
  { eapply (ctx_refines_ctx_closed _ _ _ _ [CTX_AppL _]); eauto.
    repeat econstructor; eauto.
    eapply rec_unfolding; auto. }
  eapply ctx_refines_trans.
  { eapply lam_beta; eauto.
    eapply typed_subst; eauto.
    econstructor.
    eapply (context_gen_weakening [_] [_;_]); eauto. }
  asimpl.
  assert (Γ ⊢ₜ e1'.[Rec e1',e2'/] : τ') as Htp.
  { replace e1'.[Rec e1',e2'/] with e1'.[Rec e1'.[upn 2 (ren (+1))]/].[e2'/]
      by by asimpl.
    eapply typed_subst; eauto.
    eapply typed_subst; eauto.
    econstructor.
    eapply (context_gen_weakening [_] [_;_]); eauto. }
  eapply binary_soundness; auto.
  intros; eapply binary_fundamental; auto.
Qed.