Rocoma.Recipes_joker

Jokers (TOPLAS Theorem 3.11)



Open Scope valid_scope.

notations for the examples
Notation "◻" := Void (at level 0) : ty_scope.
Notation "U --> V" := (Arr U V) (at level 60, right associativity) : ty_scope.
Notation "\[ U ] s" := (Lam s U) (at level 60, right associativity, format "\[ U ] s") : ty_scope.

The joker, TOPLAS Fig. 6

Numbers of continuation parameters

Fixpoint c_len (U : type) :=
  match U with
  | Void => 0
  | TArr V => c_len V
  | Arr _ V => (c_len V).+1
  end.

Numbers of term parameters

Fixpoint t_len (U : type) :=
  match U with
  | Void => 0
  | TArr V => (t_len V).+1
  | Arr _ V => t_len V
  end.

see TOPLAS, Fig. 6
joker_abs z U acc poses one binder per parameter of U and writes the conjunction acc at the bottom each continuation parameter k : W adds its call ∀xs. k xs 0Ws to acc. vcall z W tc is the stack xs 0Ws of arguments given to a continuation k : W, tc counting the term parameters left z is the leaf of the nested jokers, Zero or Top (see Lemma neu_joker).

Fixpoint joker_abs (z : recipe) (U : type) (acc : recipe) : recipe :=
  match U with
  | Void => acc
  | TArr V => TLam (joker_abs z V acc)
  | Arr W V =>
      \[ W ] (joker_abs z V (And acc
               (fall (t_len W) (Var (c_len V) ∘ vcall z W (t_len W)))))
  end
with vcall (z : recipe) (U : type) (tc : nat) : stack :=
  match U with
  | Void => [::]
  | TArr V => T (TVar tc.-1) :: vcall z V tc.-1
  | Arr W V => R (joker_abs z W z) :: vcall z V tc
  end.

Definition joker U := joker_abs Zero U Zero.

joker_all z W f is the call ∀xs. f xs 0Ws of the continuation f.
Definition joker_all z W f :=
  fall (t_len W) (f ∘ vcall z W (t_len W)).

Section Test.

Eval compute in joker (TArr (◻ --> ◻ --> ◻)).
Eval compute in joker (TArr (TArr (TArr ((TArr ◻) --> ◻)))).
Eval compute in joker (TArr (◻ --> ◻) --> ◻).

Eval compute in joker (((◻ --> ◻) --> ◻) --> ◻).
Eval compute in joker ((TArr ((◻ --> ◻) --> ◻) --> ◻) --> ◻).

(* let div (a: int) (b: int) (ok: (q: int) (r: int)) (ko) = … *)
Definition Udiv := TArr (TArr (Arr (TArr (TArr Void)) (Arr Void Void))).

Eval compute in joker Udiv.

(* = λa. λb. λok. λko. (0) ∧ (∀x∀y. ok x y) ∧ (ko) *)

(*
 TLam
  (TLam (\TArr (TArr ◻) \◻
          And
           (And Zero
               (TAll (TAll (TApp (TApp (Var 1) (TVar 1)) (TVar 0)))))
           (Var 0)))
 *)

End Test.

Closed in both sorts

remark: the conjunction is necessary since joker_abs and vcall are mutually recursive
Lemma fv_joker z U :
  closed z ->
  (forall acc n, fv (n + c_len U) acc -> fv n (joker_abs z U acc)) /\
  (forall s tc n, fv n s -> fv n (s ∘ vcall z U tc)).
Proof.
  move=> *.
  elim: U=> [|? [ih1 ih2] ? [ih3 ih4]|? [ih3 ih4]] /=.
  - split=> > //.
    by rewrite addn0.
  - split=> *.
    + apply fv_lam, ih3, fv_and; split.
      * by rewrite addSnnS.
      * apply fv_fall, ih2, fv_var.
        by lia.
    + apply ih4, fv_app; split=>//.
      apply ih1.
      by apply closed_fv.
  - split=> *.
    + by apply fv_tlam, ih3.
    + by apply ih4, fv_tapp.
Qed.

Corollary closed_joker U :
  closed (joker U).
Proof.
  by apply fv_joker.
Qed.

Lemma closed_call W f m :
  closed f ->
  closed (fall m (f ∘ vcall Top W m)).
Proof.
  move=> *.
  by apply fv_fall, fv_joker.
Qed.

Lemma fvh_joker z U :
  closedh z ->
  (forall acc n,
     fvh (n + t_len U) acc ->
     fvh n (joker_abs z U acc)) /\
  (forall s tc n,
     t_len U <= tc -> tc <= n -> fvh n s ->
     fvh n (s ∘ vcall z U tc)).
Proof.
  move=> *.
  elim: U=> [|? [ih1 ih2] ? [ih3 ih4]|? [ih3 ih4]] /=.
  - split=> > //.
    by rewrite addn0.
  - split=> *.
    + apply fvh_lam, ih3, fvh_and; split=>//.
      apply fvh_fall, ih2=>//.
      by lia.
    + apply ih4, fvh_app=>//; split=>//.
      apply ih1.
      by apply closedh_fvh.
  - split=> *.
    + apply fvh_tlam, ih3.
      by rewrite addSnnS.
    + apply ih4=>//; try by lia.
      apply fvh_tapp; split=>//.
      apply fv_tvar.
      by lia.
Qed.

Corollary closedh_joker U :
  closedh (joker U).
Proof.
  by apply fvh_joker.
Qed.

Neutralization: z becomes Top


Lemma neu_joker z U :
  (forall acc, neu (joker_abs z U acc) = joker_abs (neu z) U (neu acc)) /\
  (forall s tc, neu (s ∘ vcall z U tc) = neu s ∘ vcall (neu z) U tc).
Proof.
  elim: U=> [|? [ih1 ih2] ? [ih3 ih4]|? [ih3 ih4]] /=.
  - by split.
  - split=> >.
    + by rewrite ih3 /= neu_fall ih2.
    + by rewrite ih4 /= ih1.
  - split=> >.
    + by rewrite ih3.
    + by rewrite ih4.
Qed.

jokerT U is the neutralized joker, TOPLAS 0^♮_π: it cannot fail, but can call any of its continuations with any arguments.
Definition jokerT U := joker_abs Top U Top.

Corollary neu_joker_top U :
  neu (joker U) = jokerT U.
Proof.
  by apply (neu_joker Zero U).1.
Qed.

Lemma closed_jokerT U :
  closed (jokerT U).
Proof.
  by apply fv_joker.
Qed.

Lemma closedh_jokerT U :
  closedh (jokerT U).
Proof.
  by apply fvh_joker.
Qed.

Lemma unnaffected_jokerT U σ :
  (jokerT U).[σ] = jokerT U.
Proof.
  by apply closed_subst, closed_jokerT.
Qed.

Lemma unnaffectedh_jokerT U θ :
  (jokerT U).|[θ] = jokerT U.
Proof.
  by apply closedh_hsubst, closedh_jokerT.
Qed.

Joker substitutions: acc receives σ (at the bottom), calls are unnaffected.

Fixpoint subst_depth (U : type) (σ : var -> recipe) : var -> recipe :=
  match U with
  | Void => σ
  | TArr V => subst_depth V (σ >>| ren (+1))
  | Arr _ V => subst_depth V (up σ)
  end.

Lemma subst_depth_var U σ k :
  subst_depth U (upn k.+1 σ) (k + c_len U) = Var (k + c_len U).
Proof.
  elim: U σ k => [|? _ ? ih|? ih] σ ? /=.
  - by rewrite addn0 upn_k_sigma_x.
  - rewrite fold_up_upn addnS -addSn.
    by apply ih.
  - asimpl.
    rewrite upn_hcomp.
    by apply ih.
Qed.

Lemma joker_subst z U :
  closed z ->
  (forall acc σ, (joker_abs z U acc).[σ] = joker_abs z U acc.[subst_depth U σ]) /\
  (forall s tc σ, (s ∘ vcall z U tc).[σ] = s.[σ] ∘ vcall z U tc).
Proof.
  move=> hz.
  elim: U=> [|W [_ ih2] V [ih3 ih4]|V [ih3 ih4]] /=.
  - by split.
  - split=> [? σ|?? σ].
    + rewrite ih3 /= fall_subst ih2 /=.
      have:= subst_depth_var V σ 0.
      by rewrite add0n /= => ->.
    + have hc: closed (joker_abs z W z).
      { by apply fv_joker, closed_fv. }
      by rewrite ih4 /= (closed_subst _ hc).
  - split=> >.
    + by rewrite ih3.
    + by rewrite ih4.
Qed.

Lemma joker_hsubst z U :
  closedh z ->
  let tu := t_len U in
  (forall acc θ,
     (joker_abs z U acc).|[θ] = joker_abs z U acc.|[upn tu θ]) /\
  (forall s tc θ,
     tu <= tc ->
     (forall i, i < tc -> θ i = TVar i) ->
     (s ∘ vcall z U tc).|[θ] = s.|[θ] ∘ vcall z U tc).
Proof.
  move=> hz.
  elim: U=> [|W [_ ih2] ? [ih3 ih4]|? [ih3 ih4]] /=.
  - by split.
  - split=> [>|?? θ ??].
    + rewrite ih3 /= fall_hsubst ih2//.
      move=> *.
      by rewrite upn_k_sigma_x.
    + have hc: closedh (joker_abs z W z).
      { by apply (fvh_joker W hz).1, closedh_fvh. }
      by rewrite ih4//= (closedh_hsubst θ hc).
  - split=> >.
    + by rewrite ih3 fold_upn_up.
    + move=> ? h2.
      rewrite ih4 //= => *.
      * rewrite h2=>//.
        by lia.
      * by lia.
      * rewrite h2=>//.
        by lia.
Qed.

Typing

The context at the bottom of joker_abs U, starting with Γ.
Fixpoint piΓ U Γ :=
  match U with
  | Void => Γ
  | TArr V => piΓ V Γ
  | Arr U V => piΓ V (U .: Γ)
  end.

Lemma piΓ_shift U Γ i :
  Γ i = piΓ U Γ (i + c_len U).
Proof.
  elim: U Γ i=> [|U _ ? ih|? ih] Γ i /=.
  - by rewrite addn0.
  - rewrite addnS -addSn.
    by have /= := ih (U .: Γ) i.+1.
  - by apply ih.
Qed.

Lemma has_type_joker_gen z U :
  (forall Γ a, [ Γ, a, a ⊢ z :- Void ]) ->
  (forall acc Γ a,
     [ piΓ U Γ, a + t_len U, a + t_len U ⊢ acc :- Void ] ->
     [ Γ, a, a ⊢ joker_abs z U acc :- U ]) /\
  (forall s tc Γ a,
     [ Γ, a, a ⊢ s :- U ] ->
     [ Γ, a, a ⊢ s ∘ vcall z U tc :- Void ]).
Proof.
  move=> *.
  elim: U=> [|U [ih1 ih2] V [ih3 ih4]|V [ih3 ih4]] /=.
  - split=> []> =>//.
    by rewrite addn0.
  - split=> [????|?????].
    + apply ty_lam, ih3, ty_and=>//.
      apply has_type_fall, ih2, ty_var.
      rewrite -[c_len V]add0n.
      by rewrite -piΓ_shift.
    + apply ih4.
      apply ty_app with U=>//.
      by apply ih1.
  - split=> [? Γ ??|?? Γ ??].
    + apply ty_tlam, ih3.
      by rewrite addSnnS.
    + apply ih4.
      apply ty_tapp=>//.
      by apply fv_plage_eq.
Qed.

Corollary has_type_joker U :
  [ ⊢ joker U :- U ].
Proof.
  by apply has_type_joker_gen=> *; apply ty_zero.
Qed.

Corollary has_type_jokerT U :
  [ ⊢ jokerT U :- U ].
Proof.
  by apply has_type_joker_gen=> *; apply ty_top.
Qed.

Covers and submits

True iff recipe s is in S, but seen below n term binders
Definition mem_ren (S : stack) n s :=
  exists t, List.In (R t) S /\ s = t.|[ren (+n)].

TOPLAS, Appendix D: S covers s when s is in S, or when s is Ψ ∘ l where 0 only occurs in Ψ under boxes, S covers the directly boxed subrecipes of Ψ and S covers the recipes of l. covers_below is the condition on Ψ, covers_app follows the spine of the application, n is (term binders crossed).
Fixpoint covers_below (S : stack) n s : Prop :=
  match s with
  | Zero => False
  | Top | Var _ | Neu _ => True
  | Box s => mem_ren S n s \/ covers_app S n s
  | And s t | App s t => covers_below S n s /\ covers_below S n t
  | TLam s | TAll s => covers_below S n.+1 s
  | TApp s _ | Imp s _ | Lam s _ => covers_below S n s
  end

with covers_app (S : stack) n s : Prop :=
  match s with
  | App s t => covers_app S n s /\ (mem_ren S n t \/ covers_app S n t)
  | TApp s _ => covers_app S n s

  | Zero => False
  | Top | Var _ | Neu _ => True
  | Box s => mem_ren S n s \/ covers_app S n s
  | And s t => covers_below S n s /\ covers_below S n t
  | TLam s | TAll s => covers_below S n.+1 s
  | Imp s _ | Lam s _ => covers_below S n s
  end.

Definition covers S n s := mem_ren S n s \/ covers_app S n s.

Definition submits U s :=
  forall l, aligned l U -> covers l 0 s -> entails (jokerT U ∘ l) s.

Definition SUB W U := logrel (submits W) U.

Lemma mem_ren_0 S s :
  mem_ren S 0 s <-> List.In (R s) S.
Proof.
  split.
  - move=> [t [??]].
    subst.
    by asimpl.
  - move=> *.
    exists s.
    split=>//.
    by asimpl.
Qed.

Lemma covers_below_app S n s :
  covers_below S n s ->
  covers_app S n s.
Proof.
  elim: s n=> //= ? ih ? ih2 ? [??].
  split.
  - by apply ih.
  - right.
    by apply ih2.
Qed.

S covers every recipe of the stack l.
Fixpoint covers_stack S n l :=
  match l with
  | [::] => True
  | T _ :: l => covers_stack S n l
  | R t :: l => covers S n t /\ covers_stack S n l
  end.

Lemma covers_app_appl S n s l :
  covers_app S n (s ∘ l) <-> covers_app S n s /\ covers_stack S n l.
Proof.
  elim: l s=> [|[?|?] ? ih] ? /=.
  - split=>//. by move=>[??].
  - by rewrite ih.
  - rewrite ih /covers.
    repeat split=>*; unpack=>//.
    by destruct H.
    by destruct H.
Qed.

Lemma covers_appl S n s l :
  covers_app S n s ->
  covers_stack S n l ->
  covers S n (s ∘ l).
Proof.
  move=> *.
  right.
  by apply covers_app_appl.
Qed.

Lemma covers_stack_incl S l :
  List.incl l S ->
  covers_stack S 0 l.
Proof.
  elim: l=> [|[?|?] ? ih] h //=.
  - apply ih.
    have [??] := List.incl_cons_inv h.
    done.
  - split.
    + left.
      apply mem_ren_0.
      apply h.
      by left.
    + apply ih.
      have [??] := List.incl_cons_inv h.
      done.
Qed.

Lemma covers_neu S n s :
  covers_app S n (neu s).
Proof.
  apply covers_below_app.
  elim: s n=> //= *; try by split.
  right.
  by apply covers_below_app.
Qed.

Lemma mem_ren_closed S n u :
  closed_stack S ->
  mem_ren S n u ->
  closed u.
Proof.
  move=> ? [? [??]].
  subst.
  apply closed_unaffectedh.
  by apply closed_stack_in with S.
Qed.

Lemma mem_ren_ren S n j k u :
  j <= n ->
  mem_ren S n u ->
  mem_ren S (n + k) u.|[upn j (ren (+k))].
Proof.
  move=> ? [t [??]].
  exists t.
  split=>//.
  subst.
  by rewrite ren_upn_ren.
Qed.

Lemma covers_ren S k s n j :
  j <= n ->
  (covers_below S n s -> covers_below S (n + k) s.|[upn j (ren (+k))]) /\
  (covers_app S n s -> covers_app S (n + k) s.|[upn j (ren (+k))]).
Proof.
  elim: s n j=> //=;
    try (by move=> ? ih ? n j hj; have [??] := ih n j hj);
    try (by move=> ? ih n j hj;
            rewrite fold_up_upn -addSn;
            have [??] := ih n.+1 j.+1 hj).
  - move=> ? ih ? ih2 n j hj.
    have h1 := (ih n j hj).1.
    have h2 := (ih2 n j hj).1.
    by split=> -[/h1 ? /h2 ?].
  - move=> ? ih ? ih2 n j hj.
    have [h1 h1'] := ih n j hj.
    have [h2 h2'] := ih2 n j hj.
    split.
    + by move=> [/h1 ? /h2 ?].
    + move=> [/h1' ? [?|/h2' ?]]; split=>//.
      * left. by apply mem_ren_ren.
      * by right.
  - move=> ? ih n j hj.
    have h := (ih n j hj).2.
    split=> -[?|/h ?]; try by right.
    + left. by apply mem_ren_ren.
    + left. by apply mem_ren_ren.
Qed.

Corollary covers_app_ren S k s n j :
  j <= n ->
  covers_app S n s ->
  covers_app S (n + k) s.|[upn j (ren (+k))].
Proof.
  move=> hj.
  by apply (covers_ren S k s hj).
Qed.

A substitution whose range is variables and covered closed boxes.
Definition sub_cov S n (σ : var -> recipe) :=
  forall x, (exists y, σ x = Var y) \/
            (exists u, [/\ σ x = Box u, closed u & covers S n u]).

Lemma sub_cov_box S n t :
  closed t ->
  covers S n t ->
  sub_cov S n (Box t .: ids).
Proof.
  move=> ?? [|x].
  - right.
    by exists t.
  - left.
    by exists x.
Qed.

Lemma sub_cov_up S n σ :
  sub_cov S n σ ->
  sub_cov S n (up σ).
Proof.
  move=> h [|x].
  - left.
    exists 0.
    by asimpl.
  - case: (h x)=> [[y e]|[u [e hc ?]]].
    + left.
      exists y.+1.
      asimpl.
      rewrite e.
      by asimpl.
    + right.
      exists u.
      asimpl.
      rewrite e /=.
      split=>//.
      by rewrite closed_subst.
Qed.

Lemma sub_cov_hcomp S n σ :
  sub_cov S n σ ->
  sub_cov S n.+1 (σ >>| ren (+1)).
Proof.
  move=> h x.
  have->: (σ >>| ren (+1)) x = (σ x).|[ren (+1)] by [].
  case: (h x)=> [[y e]|[u [e ? hu]]].
  - left.
    exists y.
    by rewrite e.
  - right.
    exists u.|[ren (+1)].
    rewrite e /=.
    split=>//.
    + by apply closed_unaffectedh.
    + case: hu=> [hu|hu].
      * left.
        have := mem_ren_ren (j:=0) 1 (leq0n n) hu.
        by rewrite addn1.
     * right.
        have := covers_app_ren (j:=0) 1 (leq0n n) hu.
        by rewrite addn1.
Qed.

Lemma covers_subst S s n σ :
  closed_stack S ->
  sub_cov S n σ ->
  (covers_below S n s -> covers_below S n s.[σ]) /\
  (covers_app S n s -> covers_app S n s.[σ]).
Proof.
  move=> hS.
  elim: s n σ=> //=;
    try (by move=> ? ih ? n σ hσ; have [??] := ih n σ hσ);
    try (by move=> ? ih n σ hσ; have [??] := ih n.+1 _ (sub_cov_hcomp hσ)).
  - move=> x ? σ hσ.
    by case: (hσ x)=> [[? ->]|[? [-> _ ?]]].
  - move=> ? ih ? ih2 n σ hσ.
    have h1 := (ih n σ hσ).1.
    have h2 := (ih2 n σ hσ).1.
    by split=> -[/h1 ? /h2 ?].
  - move=> ? ih ? ih2 n σ hσ.
    have [h1 h1'] := ih n σ hσ.
    have [h2 h2'] := ih2 n σ hσ.
    split.
    + by move=> [/h1 ? /h2 ?].
    + move=> [/h1' ? [?|/h2' ?]]; split=>//.
      * left.
        rewrite closed_subst=>//.
        by apply mem_ren_closed with S n.
      * by right.
  - move=> ? ih ? n σ hσ.
    by have [??] := ih n (up σ) (sub_cov_up hσ).
  - move=> ? ih n σ hσ.
    have h := (ih n σ hσ).2.
    split=> -[?|/h ?]; try by right.
    + left.
      rewrite closed_subst=>//.
      by apply mem_ren_closed with S n.
    + left.
      rewrite closed_subst=>//.
      by apply mem_ren_closed with S n.
Qed.

Lemma mem_ren_hsubst S m j u (T : term) :
  mem_ren S (m.+1 + j) u ->
  mem_ren S (m + j) u.|[upn j (T .: ids)].
Proof.
  move=> [t [??]].
  exists t.
  split=>//.
  subst.
  by rewrite ren_upn_subst.
Qed.

Lemma covers_hsubst S (T : term) s m j:
  (covers_below S (m.+1 + j) s -> covers_below S (m + j) s.|[upn j (T .: ids)]) /\
  (covers_app S (m.+1 + j) s -> covers_app S (m + j) s.|[upn j (T .: ids)]).
Proof.
  elim: s m j=> //=;
    try (by move=> ? ih ? m j; have [??] := ih m j);
    try (by move=> ? ih m j; rewrite fold_up_upn -!addnS; have [??] := ih m j.+1).
  - move=> ? ih ? ih2 m j.
    have h1 := (ih m j).1.
    have h2 := (ih2 m j).1.
    by split=> -[/h1 ? /h2 ?].
  - move=> ? ih ? ih2 m j.
    have [h1 h1'] := ih m j.
    have [h2 h2'] := ih2 m j.
    split.
    + by move=> [/h1 ? /h2 ?].
    + move=> [/h1' ? [?|/h2' ?]]; split=>//.
      * left. by apply mem_ren_hsubst.
      * by right.
  - move=> ? ih m j.
    have h := (ih m j).2.
    split=> -[?|/h ?]; try by right.
    + left. by apply mem_ren_hsubst.
    + left. by apply mem_ren_hsubst.
Qed.

Corollary covers_below_hsubst0 S (T : term) s m :
  covers_below S m.+1 s ->
  covers_below S m s.|[T/].
Proof.
  move=> h.
  have h2: covers_below S (m.+1 + 0) s by rewrite addn0.
  have h3 := (covers_hsubst S T s m 0).1.
  have := h3 h2.
  by rewrite addn0.
Qed.

Lemma mem_ren_shift S n u :
  mem_ren S n.+1 u ->
  mem_ren (stack_hsubst (ren (+1)) S) n u.
Proof.
  move=> [t [??]].
  exists t.|[ren (+1)].
  split; first by apply in_stack_hsubst.
  subst.
  by rewrite ren_add add1n.
Qed.

Lemma covers_shift S s n :
  (covers_below S n.+1 s -> covers_below (stack_hsubst (ren (+1)) S) n s) /\
  (covers_app S n.+1 s -> covers_app (stack_hsubst (ren (+1)) S) n s).
Proof.
  elim: s n=> //=;
    try (by move=> ? ih ? n; have [??] := ih n);
    try (by move=> ? ih n; have [??] := ih n.+1).
  - move=> ? ih ? ih2 n.
    have h1 := (ih n).1.
    have h2 := (ih2 n).1.
    by split=> -[/h1 ? /h2 ?].
  - move=> ? ih ? ih2 n.
    have [h1 h1'] := ih n.
    have [h2 h2'] := ih2 n.
    split.
    + by move=> [/h1 ? /h2 ?].
    + move=> [/h1' ? [?|/h2' ?]]; split=>//.
      * left. by apply mem_ren_shift.
      * by right.
  - move=> ? ih n.
    have h := (ih n).2.
    split=> -[?|/h ?]; try by right.
    + left. by apply mem_ren_shift.
    + left. by apply mem_ren_shift.
Qed.

Lemma covers_stack_shift S l :
  covers_stack S 0 l ->
  covers_stack (stack_hsubst (ren (+1)) S) 0 (stack_hsubst (ren (+1)) l).
Proof.
  elim: l=> [|[]> ih] //= [ht /ih ?].
  split=>//.
  case: ht=>h.
  - left.
    apply mem_ren_shift.
    by apply (@mem_ren_ren _ 0 0 1).
  - right.
    apply covers_shift.
    by apply (@covers_app_ren S 1 _ 0 0).
Qed.

Evaluation of the joker

joker_inst U l acc reduces beta-steps of eval_lam and eval_tlam.

Fixpoint joker_inst (z : recipe) (U : type) (l : stack) (acc : recipe) : recipe :=
  match U, l with
  | TArr V, T t :: l => joker_inst z V l acc.|[upn (t_len V) (t .: ids)]
  | Arr W V, R r :: l =>
      joker_inst z V l
        (And acc (joker_all z W (Var (c_len V))))
          .[subst_depth V (Box r .: ids)]
  | _, _ => acc
  end.

Lemma subst_depth_hit U r k :
  closed r ->
  subst_depth U (upn k (Box r .: ids)) (k + c_len U) = Box r.|[ren (+ t_len U)].
Proof.
  elim: U r k=> [|? _ V ih|V ih] ?? hc /=.
  - rewrite addn0 upnP_cond //.
    rewrite subnn //=.
    rewrite closed_subst=>//=.
    by asimpl.
  - rewrite fold_up_upn addnS -addSn.
    by apply ih.
  - rewrite upn_hcomp scons_hcomp ih.
    + have->: forall r, r.|[ren (+ t_len V)] = r.|[upn 0 (ren (+ t_len V))] by [].
      by rewrite ren_upn_ren.
    + by apply closed_unaffectedh.
Qed.

Lemma eval_joker_abs z U :
  closed z -> closedh z ->
  forall l acc F,
  aligned l U ->
  eval (joker_abs z U acc ∘ l) [::] F <-> eval (joker_inst z U l acc) [::] F.
Proof.
  move=> hcT hhT.
  elim: U=> [|? _ ? ih|? ih] l ? F hl.
  - case: l hl=> [_|a l [_ h]].
    + by split.
    + by inv h.
  - case: l hl=> [|[?|?] l] [hc h]; try by inv h.
    have hl := aligned_cons_r hc h.
    red.
    apply: (iff_trans _ (ih l _ F hl)).
    rewrite -(joker_subst _ hcT).1.
    red.
    split=> /eval_move_stack_right h2;
       apply eval_move_stack_left.
    + by inv h2.
    + by constructor.
  - case: l hl=> [|[?|?] l] [hc h]; try by inv h.
    have hl := aligned_cons_t hc h.
    red.
    apply: (iff_trans _ (ih l _ F hl)).
    rewrite -(joker_hsubst _ hhT).1.
    red.
    split=> /eval_move_stack_right h2;
       apply eval_move_stack_left.
    + by inv h2.
    + by constructor.
Qed.

u is a part of the super conjunction.
Inductive conj_in (u : recipe) : recipe -> Prop :=
| conj_in_here : conj_in u u
| conj_in_l s t : conj_in u s -> conj_in u (And s t)
| conj_in_r s t : conj_in u t -> conj_in u (And s t).

Lemma conj_in_hsubst u r (θ : var -> term) :
  conj_in u r ->
  conj_in u.|[θ] r.|[θ].
Proof.
  by elim; constructor.
Qed.

Lemma conj_in_subst_closed u r (σ : var -> recipe) :
  closed u ->
  conj_in u r ->
  conj_in u r.[σ].
Proof.
  move=> hc.
  elim; try by constructor.
  rewrite closed_subst=>//.
  by constructor.
Qed.

If u is a sub-conjunction of r, the evaluation of r is stronger than the evaluation of u.
Lemma eval_conj_in u r :
  conj_in u r ->
  forall F,
  eval r [::] F ->
  exists G, eval u [::] G /\ F ||- G.
Proof.
  elim=> [|s t _ ih|s t _ ih] F h.
  - exists F.
    split=>//.
    by apply valid_assumption.
  - inv h.
    have [G [??]] := ih _ H1.
    exists G.
    split=>//.
    apply valid_cut with s2=>//.
    apply valid_and_intro_l.
    left.
    by apply valid_assumption.
  - inv h.
    have [G [??]] := ih _ H4.
    exists G.
    split=>//.
    apply valid_cut with t2=>//.
    apply valid_and_intro_l.
    right.
    by apply valid_assumption.
Qed.

Lemma conj_in_leaf z U l acc u :
  aligned l U ->
  closed u -> closedh u ->
  conj_in u acc ->
  conj_in u (joker_inst z U l acc).
Proof.
  elim: U l acc u=> [|W _ V ih|V ih] [|[t|?] l] acc u [hc h2] hcu hhu h /=;
      try by inv h2.
  - have hl := aligned_cons_r hc h2.
    apply ih=>//.
    by apply (conj_in_subst_closed _ hcu
      (conj_in_l (joker_all z W (Var (c_len V))) h)).
  - have hl := aligned_cons_t hc h2.
    apply ih=>//.
    have h3: conj_in u.|[upn (t_len V) (t .: ids)]
                    acc.|[upn (t_len V) (t .: ids)]
      by apply (conj_in_hsubst _ h).
    by rewrite (closedh_hsubst _ hhu) in h3.
Qed.

Lemma conj_in_joker_inst U l acc s :
  aligned l U ->
  closed s ->
  conj_in (Box s.|[ren (+ t_len U)]) acc ->
  conj_in (Box s) (joker_inst Top U l acc).
Proof.
  elim: U l acc s=> [|W _ V ih|V ih] [|[t|r] l] acc s [hc h2] ? h /=;
      try by inv h2.
  - by asimpl in h.
  - have hl := aligned_cons_r hc h2.
    apply ih=>//.
    have hcB: closed (Box s.|[ren (+ t_len V)]) by apply closed_box, closed_unaffectedh.
    by apply (conj_in_subst_closed (subst_depth V (Box r .: ids)) hcB
      (conj_in_l (joker_all Top W (Var (c_len V))) h)).
  - set tv := t_len V.
    have hl := aligned_cons_t hc h2.
    apply ih=>//.
    have: conj_in (Box (s.|[ren (+tv.+1)]).|[upn tv (t .: ids)])
                   acc.|[upn tv (t .: ids)] by apply (conj_in_hsubst _ h).
    have e := ren_upn_subst s 0 tv t.
    rewrite add1n add0n in e.
    by rewrite e.
Qed.

Lemma conj_in_joker_mem U l acc s :
  aligned l U ->
  List.In (R s) l ->
  [ ⊢ s :- Void ] -> (* not below a forall *)
  conj_in (Box s) (joker_inst Top U l acc).
Proof.
  elim: U l acc s=> [|W _ V ih|V ih] l acc s hl hin ? /=.
  - case: l hl hin=>// a l [? hl] ?. by inv hl.
  - case: l hl hin=> [|[?|r] l] [hc h] hin; try by inv h.
    have hl := aligned_cons_r hc h.
    destruct hin; last by apply ih.
    injections; subst.
    have hcs: closed s by inv hc.
    have htr: [ ⊢ s :- W ] by inv h.
    have ?: W = Void by apply has_type_det with empty_ctx 0 0 s.
    subst.
    apply conj_in_joker_inst=>//.
    have := subst_depth_hit V 0 hcs.
    rewrite add0n /= => e.
    have h3: conj_in (Box s.|[ren (+ t_len V)])
               (And acc.[subst_depth V (Box s .: ids)] (subst_depth V (Box s .: ids) (c_len V))).
    { rewrite e. by apply conj_in_r, conj_in_here. }
    by apply h3.
  - case: l hl hin=> [|[?|?] ?] [hc h] hin; try by inv h.
    have hl := aligned_cons_t hc h.
    apply ih=>//.
    by case: hin.
Qed.

Lemma joker_entails_mem W l s :
  aligned l W ->
  [ ⊢ s :- Void ] ->
  List.In (R s) l ->
  entails (jokerT W ∘ l) s.
Proof.
  move=> hl hts hin F G hf hg.
  rewrite /jokerT in hf.
  have [h _] := eval_joker_abs closed_top closedh_top Top F hl.
  have {hf}h := h hf.
  have hc: conj_in (Box s) (joker_inst Top W l Top) by apply conj_in_joker_mem.
  have [H [hH ?]] := eval_conj_in hc h.
  inv hH.
  have hcs: complete s.
  { split=>//.
    apply closed_stack_in with l=>//.
    by destruct hl. }
  have ->: G = H by apply (uniqueness_eval hcs).
  done.
Qed.

Lemma submits_app W s :
  [ ⊢ s :- Void ] ->
  (forall l, aligned l W -> covers_app l 0 s -> entails (jokerT W ∘ l) s) ->
  submits W s.
Proof.
  move=> ? h ?? [] h1.
  - apply mem_ren_0 in h1.
    by apply joker_entails_mem.
  - by apply h.
Qed.

Lemma joker_bot U l F :
  aligned l U ->
  eval (joker U ∘ l) [::] F ->
  F ||- FBot.
Proof.
  move=> hl hf.
  have hz: closed Zero by [].
  have hzh: closedh Zero by [].
  have [hf2 _] := eval_joker_abs hz hzh Zero F hl.
  have {}hf2 := hf2 hf.
  have hc: conj_in Zero (joker_inst Zero U l Zero).
  { apply conj_in_leaf=>//.
    by constructor. }
  have [G [hG hFG]] := eval_conj_in hc hf2.
  by inv hG.
Qed.

Calls of the items

stack l where recipes are replaced by corresponding joker
Fixpoint jstack (U : type) (l : stack) : stack :=
  match U, l with
  | TArr V, T t :: l2 => T t :: jstack V l2
  | Arr W V, R _ :: l2 => R (jokerT W) :: jstack V l2
  | _, _ => [::]
  end.

Lemma aligned_jstack U l :
  aligned l U ->
  aligned (jstack U l) U.
Proof.
  elim: U l=> [|U _ V ih|U ih] l.
  - case: l=>//.
    by split; constructor.
  - case: l=>// -[] ? l [hc h] /=.
    + repeat constructor=>//.
      by inv h.
    + have hl := aligned_cons_r hc h.
      have [h1 h2] := ih l hl.
      repeat constructor=>//.
      by apply closed_jokerT.
      by apply has_type_jokerT.
  - case: l=>// -[] ? l [hc h] /=.
    + have hl := aligned_cons_t hc h.
      have [h1 h2] := ih l hl.
      by repeat constructor.
    + by inv h.
Qed.

Lemma ent_jokerT W :
  (forall V, order V < order W ->
     forall U r, closed r -> [ ⊢ r :- U ] -> SUB V U r) ->
  forall U s, order U < order W -> closed s -> [ ⊢ s :- U ] ->
  ENT U (jokerT U) (neu s).
Proof.
  move=> ih U s ???.
  apply entails_36.
  - by apply closed_jokerT.
  - by apply has_type_jokerT.
  - by apply closed_sneu.
  - by apply has_type_neu.
  - move=> l hl.
    have hsub: SUB U Void (neu s ∘ l).
    { apply ih=>//.
      - destruct hl.
        apply closed_appl=>//.
        by apply closed_sneu.
      - apply aligned_type_void with U=>//.
        by apply has_type_neu. }
    apply hsub=>//.
    apply covers_appl.
    + by apply covers_neu.
    + by apply covers_stack_incl.
Qed.

Lemma stack_rel2_jstack U l :
  (forall W s,
     arg_of W U -> closed s -> [ ⊢ s :- W ] -> ENT W (jokerT W) (neu s)) ->
  aligned l U -> stack_rel2 entails U (jstack U l) (neu_stack l).
Proof.
  elim: U l=> [|W _ ? ih|? ih] l h.
  - case: l=> [|[?|r] ?] [hc ht]; try by inv ht.
    by constructor.
  - case: l=> [|[?|r] l] [hc ht]; try by inv ht.
    have hl := aligned_cons_r hc ht.
    have hcr: closed r by inv hc.
    have htr: [ ⊢ r :- W ] by inv ht.
    constructor.
    + move=> ξ.
      rewrite /= unnaffectedh_jokerT -neu_hsubst_comm.
      have ?: ENT W (jokerT W) (neu r.|[ren ξ]).
      { apply h.
        - by constructor.
        - by apply closed_unaffectedh.
        - by apply has_type_hsubst_0. }
      split=>//.
      by rewrite /jokerT (neu_joker Top W).1 neu_idempotent.
    + apply ih=>// ? Ψ ?.
      apply h.
      by constructor.
  - case: l=> [|[?|r] l] [hc ht]; try by inv ht.
    have hl := aligned_cons_t hc ht.
    constructor.
    apply ih=>// ? s ?.
    apply h.
    by constructor.
Qed.

Lemma conj_in_call_inst U l acc W r :
  let w := t_len W in
  let u := t_len U in
  aligned l U -> closed r ->
  conj_in
    (joker_all Top W (Box r.|[ren (+(w + u))])) acc ->
  conj_in
    (joker_all Top W (Box r.|[ren (+ w)]))
    (joker_inst Top U l acc).
Proof.
  elim: U l acc W r=> [|? _ V ih|V ih] l ? W r tw tu hl ? h /=; try set (tv := t_len V).
  - case: l hl=> [_|a l [_ h2]].
    + by rewrite addn0 in h.
    + by inv h2.
  - case: l hl=> [|[t|?] l] [hc h2]; try by inv h2.
    have hl := aligned_cons_r hc h2.
    apply ih=>//.
    have hcB: closed
      (joker_all Top W (Box r.|[ren (+(tw + tv))])).
    { apply closed_call, closed_box.
      by apply closed_unaffectedh. }
    by apply (conj_in_subst_closed _ hcB (conj_in_l _ h)).
  - case: l hl=> [|[t|?] ?] [hc h2]; try by inv h2.
    have hl := aligned_cons_t hc h2.
    apply ih=>//.
    have e:
      (joker_all Top W (Box r.|[ren (+(tw + tv.+1))]))
        .|[upn tv (t .: ids)] =
      joker_all Top W (Box r.|[ren (+(tw + tv))]).
    { rewrite fall_hsubst ((joker_hsubst _ closedh_top).2 _ _ _) //=;
        last by move=> ??; rewrite upn_k_sigma_x.
      rewrite upn_add addnS -add1n ren_upn_subst add0n.
      by []. }
    rewrite -e.
    by apply conj_in_hsubst.
Qed.

Lemma conj_in_joker_call W :
  forall l acc s U,
  aligned l W ->
  List.In (R s) l ->
  [ ⊢ s :- U ] ->
  conj_in (joker_all Top U (Box s.|[ren (+ t_len U)]))
          (joker_inst Top W l acc).
Proof.
  elim: W=> [|W _ V ih|V ih] l ? s U hl hin ? /=.
  - case: l hl hin=>// a l [? hl] ?. by inv hl.
  - case: l hl hin=> [|[] ? l] [hc h] hin; try by inv h.
    have hl := aligned_cons_r hc h.
    destruct hin; last by apply ih.
    injections.
    have hcs: closed s by inv hc.
    have htr: [ ⊢ s :- W ] by inv h.
    have e: W = U by apply has_type_det with empty_ctx 0 0 s.
    subst.
    apply conj_in_call_inst=>//.
    have hsb := subst_depth_hit V 0 hcs.
    rewrite add0n in hsb.
    have <-:
      (joker_all Top U (Var (c_len V)))
        .[subst_depth V (Box s .: ids)] =
      joker_all Top U (Box s.|[ren (+(t_len U + t_len V))]).
    { rewrite fall_subst ((joker_subst _ closed_top).2 _ _ _) //= hsb /= ren_add.
      by rewrite addnC. }
    by apply conj_in_r, conj_in_here.
  - case: l hl hin=> [|[?|?] ?] [hc h] [] hin; try by inv h.
    have hl := aligned_cons_t hc h.
    by apply ih.
Qed.

Collect the terms of a U-aligned stack.
Fixpoint terms (U : type) (l : stack) : seq term :=
  match U, l with
  | TArr V, T t :: l2 => t :: terms V l2
  | Arr _ V, R _ :: l2 => terms V l2
  | _, _ => [::]
  end.

Lemma size_terms U l :
  aligned l U ->
  size (terms U l) = t_len U.
Proof.
  elim: U l=> [|? _ ? ih|? ih] l;
    case: l=> [|[] ??] [hc h] //=; try by inv h.
  - rewrite ih //.
    by split; [inv hc|inv h].
  - rewrite ih //.
    by split; [inv hc|inv h].
Qed.

Lemma vcall_hsubst U l tc θ:
  aligned l U -> t_len U <= tc ->
  (forall i, i < t_len U -> θ (tc - i.+1) = nth (TVar 0) (terms U l) i) ->
  stack_hsubst θ (vcall Top U tc) = jstack U l.
Proof.
  elim: U l tc θ=> [|W _ V ih|V ih] l tc θ;
    case: l=> [|[t|?] l] [hc h] htc hθ //=; try by inv h.
  - have ? := aligned_cons_r hc h.
    by rewrite unnaffectedh_jokerT (ih l).
  - have ? := aligned_cons_t hc h.
    have e : θ tc.-1 = t.
    { have /= <-:= hθ 0 erefl.
      by rewrite subn1. }
    have ?: t_len V <= tc.-1.
    { have ?: 0 < tc by apply: leq_trans htc.
      inv htc. by lia. }
    rewrite e (ih l) // => i hi.
    have := hθ i.+1 hi.
    by rewrite -subn1 -subnDA add1n.
Qed.

Lemma joker_call_inst U l s F G :
  aligned l U ->
  closed s -> [ ⊢ s :- U ] ->
  eval (joker_all Top U (Box s.|[ren (+ t_len U)])) [::] F ->
  eval (s ∘ jstack U l) [::] G ->
  F ||- G.
Proof.
  move=> hl ?? hG ?.
  have [H [-> hH]] := eval_fall_inv hG.
  have h1: eval (s.|[ren (+ t_len U)] ∘ vcall Top U (t_len U)) [::] H.
  { apply eval_move_stack_left.
    apply eval_move_stack_right in hH.
    by inv hH. }
  have hsz := size_terms hl.
  have := eval_hsubst h1 (inst (terms U l)).
  have ->: (s.|[ren (+ t_len U)] ∘ vcall Top U (t_len U)).|[inst (terms U l)] = s ∘ jstack U l.
  { rewrite appl_hsubst (vcall_hsubst hl) //.
    - by rewrite -hsz hsubst_comp ren_inst hsubst_id.
    - move=> *.
      rewrite -{1}hsz inst_nth //.
      by rewrite hsz. }
  move=> /= h2.
  have hc: complete (s ∘ jstack U l).
  { apply aligned_complete with U=>//.
    by apply aligned_jstack. }
  have ?: G = H.[inst (terms U l)] by apply (uniqueness_eval hc).
  subst.
  rewrite -hsz.
  by apply valid_fallF_elim.
Qed.

SUB W U is closed under head expansion.
Lemma SUB_expand W U r s :
  [ ⊢ r :- U ] -> [ ⊢ s :- U ] ->
  (forall l, aligned l W -> covers_app l 0 r -> covers_app l 0 s) ->
  (forall l g, eval r l g -> eval s l g) ->
  SUB W U s -> SUB W U r.
Proof.
  move=> ?? hcov hev ?.
  apply logical_relation_lemma_proj2=> [l|] * //.
  have hsl: submits W (s ∘ l) by apply logical_relation_lemma_proj1 with U.
  apply submits_app.
  - by apply aligned_type_void with U.
  - move=> l_ hl_ /covers_app_appl [hcr hcl] f g hf /eval_move_stack_right hg.
    have hca := covers_appl (hcov _ hl_ hcr) hcl.
    by apply (hsl _ hl_ hca), eval_move_stack_left, hev.
Qed.

TOPLAS Lemma D.1.1
Lemma d1_1 W :
  (forall V, order V < order W ->
     forall U r, closed r -> [ ⊢ r :- U ] -> SUB V U r) ->
  forall r U,
  closed r -> [ ⊢ r :- U ] ->
  box_rel (submits W) r -> SUB W U r.
Proof.
  move=> ihW.
  have:
    forall n r U,
    weight r <= n ->
    closed r ->
    [ ⊢ r :- U ] ->
    box_rel (submits W) r -> SUB W U r.
  2: { move=> h r U hc ht h2. by apply (h (weight r) _ U). }
  elim=>[r U ?|n ih].
  (* case {0} *)
  { exfalso. have := weight_pos r. by lia. }
  (* case {n.+1} *)
  { case.
    (* case {Var x} *)
    { move=> ??? hc *. by apply closed_ids in hc. }
    (* case {Zero} *)
    { move=> >?? hty ?.
      inv hty.
      rewrite /SUB/=.
      apply submits_app=>//.
      by apply ty_zero. }
    (* case {Top} *)
    { move=> >?? hty ?.
      inv hty.
      rewrite /SUB/= => > ?? > ? hg.
      inv hg.
      by apply valid_top. }
    (* case {And s t} *)
    { move=> s t U /= ? /closed_and [? hc2] hty hb.
      apply logical_relation_lemma_proj2=>[l hl hcl htl|] //.
      have hs: SUB W U s. { apply ih=>//. by lia. by inv hty. by inv hb. }
      have ht: SUB W U t. { apply ih=>//. by lia. by inv hty. by inv hb. }
      have hsl1: submits W (s ∘ l).
      { apply logical_relation_lemma_proj1 with U=>//. by inv hty. }
      have htl2: submits W (t ∘ l).
      { apply logical_relation_lemma_proj1 with U=>//. by inv hty. }
      apply submits_app.
      - by apply aligned_type_void with U.
      - move=> l_ hl_ /covers_app_appl [[hcs hct] hcl_].
        move=> f g hf /eval_move_stack_right hg.
        have hcas := covers_appl (covers_below_app hcs) hcl_.
        have hcat := covers_appl (covers_below_app hct) hcl_.
        inv hg.
        apply valid_and_intro.
        + apply (hsl1 _ hl_ hcas _ _ hf). by apply eval_move_stack_left.
        + apply (htl2 _ hl_ hcat _ _ hf). by apply eval_move_stack_left. }
    (* case {App s t} *)
    { move=> s t V /= ? /closed_app [? hc2] hty hb.
      inv hty.
      have hs: SUB W (Arr U V) s. { apply ih=>//. by lia. by inv hb. }
      apply hs=>// ξ.
      have hct: closed t.|[ren ξ] by apply closed_unaffectedh.
      have htt: [ ⊢ t.|[ren ξ] :- U ] by apply has_type_hsubst_0.
      have hbt: box_rel (submits W) t.|[ren ξ].
      { inv hb. by apply box_rel_preserved_ren. }
      split.
      - apply ih=>//. rewrite weight_hsubst. by lia.
      - apply ih.
        + have ?: weight t.|[ren ξ] = weight t by apply weight_hsubst.
          have ?: weight (neu t.|[ren ξ]) <= weight t.|[ren ξ] by apply weight_neu.
          by lia.
        + by apply closed_sneu.
        + by apply has_type_neu.
        + by apply box_rel_preserved_neu. }
    (* case {TApp s S} *)
    { move=> s ? V /= ? /closed_tapp ? hty hb.
      inv hty. inv hb.
      have: SUB W (TArr V) s by apply ih.
      by apply. }
    (* case {Lam s U} *)
    { move=> s U Z /= ? hc hty hb.
      inv hty. inv hb.
      rewrite /SUB/= => t ???.
      have ?: closed s.[Box t/].
      { apply fv1.
        - by apply closed_lam in hc.
        - by apply closed_box. }
      have ?: [ ⊢ s.[Box t/] :- V ].
      { apply has_type_beta with U=>//.
        - by apply closed_box.
        - by constructor. }
      have hs: SUB W V s.[Box t/].
      { apply ih=>//.
        - have := weight_subst_box 0 s t.
          by rewrite upn0 => <-.
        - by apply box_rel_preserved_subst0 with U. }
      apply SUB_expand with s.[Box t/]=>//.
      - by apply ty_app with U=>//; constructor.
      - move=> l_ [hcl_ _] [] ??.
        have hsc: sub_cov l_ 0 (Box t .: ids) by apply sub_cov_box.
        apply covers_below_app.
        by apply (covers_subst s hcl_ hsc).1.
      - by move=> > h; inv h; inv H5. }
    (* case {Imp s f} *)
    { move=> s f U /= ? /closed_imp ? hty hb.
      inv hb.
      have ?: U = Void by inv hty. subst.
      have hts: [ ⊢ s :- Void ] by inv hty.
      have: SUB W Void s by apply ih.
      rewrite /SUB/= => hs.
      apply submits_app=>// ? hl_ hcs ?? hf hg.
      inv hg.
      apply valid_imp_intro.
      apply valid_weak.
      by apply (hs _ hl_ (or_intror (covers_below_app hcs)) _ _ hf). }
    (* case {TLam s} *)
    { move=> s V /= ?? hty hb.
      inv hty.
      rewrite /SUB/= => T.
      have hcs: closed s by apply closed_tlam.
      have hcsT: closed s.|[T/] by apply closed_unaffectedh.
      have htsT: [ ⊢ s.|[T/] :- U ] by apply has_type_hsubst1.
      have hs: SUB W U s.|[T/].
      { apply ih=>//.
        - by rewrite weight_hsubst; lia.
        - inv hb. by apply box_rel_preserved_hsubst0 with empty_ctx U 1 1. }
      apply SUB_expand with s.|[T/]=>//.
      - apply ty_tapp.
        + by apply fv_plage_eq.
        + by apply ty_tlam.
      - move=> *.
        apply covers_below_app.
        by apply covers_below_hsubst0.
      - by move=> > h; inv h; inv H4. }
    (* case {TAll s} *)
    { move=> s U /= ?? hty hb. inv hb.
      have ?: [ ⊢ s :- U ] by apply has_type_lower_1; inv hty.
      have ?: closed s by apply closed_tall.
      have hs: SUB W U s by apply ih.
      apply logical_relation_lemma_proj2=> [l hl hcl hsl|//].
      have hsl': submits W (s ∘ stack_hsubst (ren (+1)) l).
      { apply logical_relation_lemma_proj1 with U=>//;
        by [apply stack_rel_ren|apply closed_stack_hsubst|apply stack_has_type_hsubst]. }
      apply submits_app; first by apply aligned_type_void with U.
      move=> l_ ? /covers_app_appl [hcr hcl_] f g ? /eval_move_stack_right hg.
      inv hg.
      apply valid_all_de_bruijn.
      have hl02: aligned (stack_hsubst (ren (+1)) l_) W by apply aligned_hsubst.
      have hcov2: covers (stack_hsubst (ren (+1)) l_) 0 (s ∘ stack_hsubst (ren (+1)) l).
      { apply covers_appl; last by apply covers_stack_shift.
        apply covers_below_app.
        by apply (covers_shift l_ s 0).1. }
      have hf2: eval (jokerT W ∘ stack_hsubst (ren (+1)) l_) (stack_hsubst (ren (+1)) [::]) f.[ren (+1)].
      { have e: closedh (jokerT W) by apply closedh_jokerT.
        rewrite -(closedh_hsubst (ren (+1)) e) -appl_hsubst.
        by apply eval_hsubst. }
      apply (hsl' _ hl02 hcov2 _ _ hf2).
      by apply eval_move_stack_left. }
    (* case {Neu s} *)
    { move=> s U /= ? /closed_neu ? hty hb.
      have hts: [ ⊢ s :- U ] by inv hty.
      have hbs: box_rel (submits W) s by inv hb.
      apply SUB_expand with (neu s)=>//.
      - by apply has_type_neu.
      - by move=> *; apply covers_neu.
      - by move=> > h; inv h.
      - apply ih.
        + have:= weight_neu s.
          by lia.
        + by apply closed_sneu.
        + by apply has_type_neu.
        + by apply box_rel_preserved_neu. }
    (* case {Box s} *)
    { move=> s U ?? hty hb.
      have [V [hcs hts hls]] := box_rel_box_ids hb.
      have ?: V = U by inv hty; apply has_type_det with empty_ctx 0 0 s.
      subst.
      apply logical_relation_lemma_proj2=>[l ???|//].
      have hsl: submits W (s ∘ l).
      { by apply logical_relation_lemma_proj1 with U; first by case: hls. }
      have hnl: submits W (neu s ∘ l).
      { apply logical_relation_lemma_proj1 with U=>//; first by case: hls.
        by apply has_type_neu. }
      have hgl: aligned l U by [].
      apply submits_app; first by apply aligned_type_void with U.
      move=> l_ hl_ /covers_app_appl [hcb hcl] f g hf /eval_move_stack_right hg.
      inv hg.
      have hg2: eval (s ∘ l) [::] g by apply eval_move_stack_left.
      case: hcb.
      - move/mem_ren_0=> hin.
        have hc3: complete (s ∘ l) by apply aligned_complete with U.
        have hcn: complete (neu s ∘ l).
        { apply aligned_complete with U=>//.
          - by apply has_type_neu.
          - by apply closed_sneu. }
        have hcm: complete (s ∘ neu_stack l).
        { apply aligned_complete with U=>//. by apply aligned_neu. }
        have [g1 h1] := eval_def hcn.
        have [g2 h2] := eval_def hcm.
        have hx := (cross_lemma hc3 hg2 h1 h2).2.
        apply valid_cut with (g1 ++ g2)=>//.
        apply valid_and_intro.
        + by apply (hnl _ hl_ (covers_appl (covers_neu _ 0 s) hcl) _ _ hf h1).
        + have [U2 [hU2 htU2]] := in_aligned_arg hl_ hin.
          have ?: U2 = U by apply has_type_det with empty_ctx 0 0 s.
          subst.
          have hf2 := proj1 (eval_joker_abs closed_top closedh_top Top f hl_) hf.
          have [G [hG hfG]] := eval_conj_in (conj_in_joker_call Top hl_ hin hts) hf2.
          have hcj: complete (s ∘ jstack U l).
          { by apply aligned_complete with U=>//; apply aligned_jstack. }
          have [H hH] := eval_def hcj.
          have hGH := joker_call_inst hgl hcs hts hG hH.
          have hent: entails (s ∘ jstack U l) (s ∘ neu_stack l).
          { apply logical_relation_lemma2_proj1 with U=>//.
            - by apply entails_refl.
            - apply stack_rel2_jstack=>// ?? ha.
              apply (ent_jokerT ihW)=>//.
              have := order_arg ha.
              have := order_arg hU2.
              by lia.
            - by apply aligned_jstack.
            - by apply aligned_neu. }
          apply valid_cut with G =>//.
          apply valid_cut with H =>//.
          by apply hent.
      - move=> hcs2.
        by apply (hsl _ hl_ (covers_appl hcs2 hcl) _ _ hf hg2). }
  }
Qed.

D.1 and 3.11

TOPLAS Lemma D.1
Lemma d1 W U r :
  closed r ->
  [ ⊢ r :- U ] ->
  SUB W U r.
Proof.
  (* “By induction over the order of the type signature.” *)
  have outer: forall k W, order W < k ->
    forall U r, closed r -> [ ⊢ r :- U ] -> SUB W U r.
  (* conclude *)
  2: { move=> *. by apply (outer (order W).+1 W). }
  (* prove outer *)
  elim; first by move=> ? /order_nonneg.
  clear_all.
  have inner:
    forall W,
      (forall Z, order Z < order W ->
       forall U1 r1, closed r1 -> [ ⊢ r1 :- U1 ] -> SUB Z U1 r1) ->
      forall n t U, depth t <= n -> closed t -> [ ⊢ t :- U ] -> SUB W U t.
  (* conclude *)
  2: { move=> ? ih Z ? X s ??.
       have ihW: forall W2, order W2 < order Z ->
         forall U2 r2, closed r2 -> [ ⊢ r2 :- U2 ] -> SUB W2 U2 r2.
       - move=> W2 *. apply (ih W2)=>//. by lia.
       - by apply (inner Z ihW (depth s) s X). }
  (* prove inner *)
  { move=> W ?.
    elim=>[|n ih] t U ? hc ht.
    (* case {0} *)
    - apply d1_1=>//.
      apply box_rel_nobox, mem_box_depth.
      have := depth_nonneg t.
      by lia.
      (* case {n.+1} *)
    - apply d1_1=>//.
      apply unfold_box_rel with empty_ctx U 0 0=>// s1 V ?? ξ.
      have ?: closed s1 by apply directly_box_closed with t U empty_ctx 0 0.
      have ?: depth s1 <= n by apply directly_box_depth with t.
      have ?: closed s1.|[ren ξ] by apply closed_unaffectedh.
      have ?: [ ⊢ s1.|[ren ξ] :- V ] by apply has_type_hsubst_0.
      split.
      + apply ih=>//.
        by rewrite depth_hsubst.
      + apply ih.
        * by rewrite -depth_neu depth_hsubst.
        * by apply closed_sneu.
        * by apply has_type_neu. }
Qed.

Lemma stack_rel_submits W U l :
  aligned l U ->
  stack_rel (submits W) U l.
Proof.
  elim: U l=> [|U2 _ ? ih|? ih] [|[]r ?] [hc h]; try by inv h.
  - by constructor.
  - have hl := aligned_cons_r hc h.
    have hcr: closed r by inv hc.
    have htr: [ ⊢ r :- U2 ] by inv h.
    constructor.
      2: by apply ih.
    move=> ξ.
    have ?: closed r.|[ren ξ] by apply closed_unaffectedh.
    have ?: [ ⊢ r.|[ren ξ] :- U2 ] by apply has_type_hsubst_0.
    split; apply d1=>//.
    + by apply closed_sneu.
    + by apply has_type_neu.
  - have hl := aligned_cons_t hc h.
    constructor.
    by apply ih.
Qed.

TOPLAS Theorem 3.11
Theorem joker_entails U r :
  closed r -> [ ⊢ r :- U ] ->
  strongly2 (ENT U) (joker U) r.
Proof.
  move=> hc ht.
  split.
  - apply entails_36=>//.
    + by apply closed_joker.
    + by apply has_type_joker.
    + move=> > hl f g hf ?.
      apply valid_cut with FBot.
      * by apply (joker_bot hl hf).
      * by apply valid_contradiction.
  - have ?: closed (joker U) by apply closed_joker.
    have ?: [ ⊢ joker U :- U ] by apply has_type_joker.
    apply entails_36.
    + by apply closed_sneu.
    + by apply has_type_neu.
    + by apply closed_sneu.
    + by apply has_type_neu.
    + move=> l [??].
      rewrite neu_joker_top.
      have hsub: submits U (neu r ∘ l).
      { apply logical_relation_lemma_proj1 with U=>//.
        - apply d1.
          * by apply closed_sneu.
          * by apply has_type_neu.
        - by apply has_type_neu.
        - by apply stack_rel_submits. }
      apply hsub=>//.
      apply covers_appl.
      * by apply covers_neu.
      * by apply covers_stack_incl.
Qed.