{-# OPTIONS --without-K --safe #-}

open import Definition.Typed.EqualityRelation
open import Tools.Level
open import Tools.Relation

module Definition.LogicalRelation {a ℓ′} (Mod′ : Setoid a ℓ′)
                                  {{eqrel : EqRelSet Mod′}} where
open EqRelSet {{...}}

open Setoid Mod′ using (_≈_) renaming (Carrier to Mod)

open import Definition.Untyped Mod as U hiding (_∷_)
open import Definition.Untyped.BindingType Mod′
open import Definition.Typed.Properties Mod′
open import Definition.Typed Mod′
open import Definition.Typed.Weakening Mod′

open import Tools.Nat
open import Tools.Product
import Tools.PropositionalEquality as PE

private
  variable
    p q : Mod
     : Nat
    Γ : Con Term 

-- The different cases of the logical relation are spread out through out
-- this file. This is due to them having different dependencies.

-- We will refer to expressions that satisfies the logical relation as reducible.

-- Reducibility of Neutrals:

-- Neutral type
record _⊩ne_ { : Nat} (Γ : Con Term ) (A : Term ) : Set (a  ℓ′) where
  constructor ne
  field
    K   : Term 
    D   : Γ  A :⇒*: K
    neK : Neutral K
    K≡K : Γ  K ~ K  U

-- Neutral type equality
record _⊩ne_≡_/_ (Γ : Con Term ) (A B : Term ) ([A] : Γ ⊩ne A) : Set (a  ℓ′) where
  constructor ne₌
  open _⊩ne_ [A]
  field
    M   : Term 
    D′  : Γ  B :⇒*: M
    neM : Neutral M
    K≡M : Γ  K ~ M  U

-- Neutral term in WHNF
record _⊩neNf_∷_ (Γ : Con Term ) (k A : Term ) : Set (a  ℓ′) where
  inductive
  constructor neNfₜ
  field
    neK  : Neutral k
    ⊢k   : Γ  k  A
    k≡k  : Γ  k ~ k  A

-- Neutral term
record _⊩ne_∷_/_ (Γ : Con Term ) (t A : Term ) ([A] : Γ ⊩ne A) : Set (a  ℓ′) where
  inductive
  constructor neₜ
  open _⊩ne_ [A]
  field
    k   : Term 
    d   : Γ  t :⇒*: k  K
    nf  : Γ ⊩neNf k  K

-- Neutral term equality in WHNF
record _⊩neNf_≡_∷_ (Γ : Con Term ) (k m A : Term ) : Set (a  ℓ′) where
  inductive
  constructor neNfₜ₌
  field
    neK  : Neutral k
    neM  : Neutral m
    k≡m  : Γ  k ~ m  A

-- Neutral term equality
record _⊩ne_≡_∷_/_ (Γ : Con Term ) (t u A : Term ) ([A] : Γ ⊩ne A) : Set (a  ℓ′) where
  constructor neₜ₌
  open _⊩ne_ [A]
  field
    k m : Term 
    d   : Γ  t :⇒*: k  K
    d′  : Γ  u :⇒*: m  K
    nf  : Γ ⊩neNf k  m  K

-- Reducibility of natural numbers:

-- Natural number type
_⊩ℕ_ : (Γ : Con Term ) (A : Term )  Set (a  ℓ′)
Γ ⊩ℕ A = Γ  A :⇒*: 

-- Natural number type equality
_⊩ℕ_≡_ : (Γ : Con Term ) (A B : Term )  Set (a  ℓ′)
Γ ⊩ℕ A  B = Γ  B ⇒* 

mutual
  -- Natural number term
  record _⊩ℕ_∷ℕ (Γ : Con Term ) (t : Term ) : Set (a  ℓ′) where
    inductive
    constructor ℕₜ
    field
      n : Term 
      d : Γ  t :⇒*: n  
      n≡n : Γ  n  n  
      prop : Natural-prop Γ n

  -- WHNF property of natural number terms
  data Natural-prop (Γ : Con Term ) : (n : Term )  Set (a  ℓ′) where
    sucᵣ  :  {n}  Γ ⊩ℕ n ∷ℕ  Natural-prop Γ (suc n)
    zeroᵣ : Natural-prop Γ zero
    ne    :  {n}  Γ ⊩neNf n    Natural-prop Γ n

mutual
  -- Natural number term equality
  record _⊩ℕ_≡_∷ℕ (Γ : Con Term ) (t u : Term ) : Set (a  ℓ′) where
    inductive
    constructor ℕₜ₌
    field
      k k′ : Term 
      d : Γ  t :⇒*: k   
      d′ : Γ  u :⇒*: k′  
      k≡k′ : Γ  k  k′  
      prop : [Natural]-prop Γ k k′

  -- WHNF property of Natural number term equality
  data [Natural]-prop (Γ : Con Term ) : (n n′ : Term )  Set (a  ℓ′) where
    sucᵣ  :  {n n′}  Γ ⊩ℕ n  n′ ∷ℕ  [Natural]-prop Γ (suc n) (suc n′)
    zeroᵣ : [Natural]-prop Γ zero zero
    ne    :  {n n′}  Γ ⊩neNf n  n′    [Natural]-prop Γ n n′

-- Natural extraction from term WHNF property
natural :  {n}  Natural-prop Γ n  Natural n
natural (sucᵣ x) = sucₙ
natural zeroᵣ = zeroₙ
natural (ne (neNfₜ neK ⊢k k≡k)) = ne neK

-- Natural extraction from term equality WHNF property
split :  {a b}  [Natural]-prop Γ a b  Natural a × Natural b
split (sucᵣ x) = sucₙ , sucₙ
split zeroᵣ = zeroₙ , zeroₙ
split (ne (neNfₜ₌ neK neM k≡m)) = ne neK , ne neM

-- Reducibility of Empty

-- Empty type
_⊩Empty_ : (Γ : Con Term ) (A : Term )  Set (a  ℓ′)
Γ ⊩Empty A = Γ  A :⇒*: Empty

-- Empty type equality
_⊩Empty_≡_ : (Γ : Con Term ) (A B : Term )  Set (a  ℓ′)
Γ ⊩Empty A  B = Γ  B ⇒* Empty

-- WHNF property of absurd terms
data Empty-prop (Γ : Con Term ) : (n : Term )  Set (a  ℓ′) where
  ne    :  {n}  Γ ⊩neNf n  Empty  Empty-prop Γ n

-- Empty term
record _⊩Empty_∷Empty (Γ : Con Term ) (t : Term ) : Set (a  ℓ′) where
  inductive
  constructor Emptyₜ
  field
    n : Term 
    d : Γ  t :⇒*: n  Empty
    n≡n : Γ  n  n  Empty
    prop : Empty-prop Γ n

data [Empty]-prop (Γ : Con Term ) : (n n′ : Term )  Set (a  ℓ′) where
  ne    :  {n n′}  Γ ⊩neNf n  n′  Empty  [Empty]-prop Γ n n′

-- Empty term equality
record _⊩Empty_≡_∷Empty (Γ : Con Term ) (t u : Term ) : Set (a  ℓ′) where
  inductive
  constructor Emptyₜ₌
  field
    k k′ : Term 
    d : Γ  t :⇒*: k   Empty
    d′ : Γ  u :⇒*: k′  Empty
    k≡k′ : Γ  k  k′  Empty
    prop : [Empty]-prop Γ k k′

empty :  {n}  Empty-prop Γ n  Neutral n
empty (ne (neNfₜ neK _ _)) = neK

esplit :  {a b}  [Empty]-prop Γ a b  Neutral a × Neutral b
esplit (ne (neNfₜ₌ neK neM k≡m)) = neK , neM

-- Reducibility of Unit

-- Unit type
_⊩Unit_ : (Γ : Con Term ) (A : Term )  Set (a  ℓ′)
Γ ⊩Unit A = Γ  A :⇒*: Unit

-- Unit type equality
_⊩Unit_≡_ : (Γ : Con Term ) (A B : Term )  Set (a  ℓ′)
Γ ⊩Unit A  B = Γ  B ⇒* Unit

record _⊩Unit_∷Unit (Γ : Con Term ) (t : Term ) : Set (a  ℓ′) where
  inductive
  constructor Unitₜ
  field
    n : Term 
    d : Γ  t :⇒*: n  Unit
    prop : Whnf n

-- Unit term equality
record _⊩Unit_≡_∷Unit (Γ : Con Term ) (t u : Term ) : Set (a  ℓ′) where
  constructor Unitₜ₌
  field
    ⊢t : Γ  t  Unit
    ⊢u : Γ  u  Unit

-- Type levels

data TypeLevel : Set where
   : TypeLevel
  ¹ : TypeLevel

data _<_ : (i j : TypeLevel)  Set where
  0<1 :  < ¹

-- Logical relation
-- Exported interface
record LogRelKit : Set (lsuc (a  ℓ′)) where
  constructor Kit
  field
    _⊩U : (Γ : Con Term )  Set (a  ℓ′)
    _⊩B⟨_⟩_ : (Γ : Con Term ) (W : BindingType)  Term   Set (a  ℓ′)

    _⊩_ : (Γ : Con Term )  Term   Set (a  ℓ′)
    _⊩_≡_/_ : (Γ : Con Term ) (A B : Term )  Γ  A  Set (a  ℓ′)
    _⊩_∷_/_ : (Γ : Con Term ) (t A : Term )  Γ  A  Set (a  ℓ′)
    _⊩_≡_∷_/_ : (Γ : Con Term ) (t u A : Term )  Γ  A  Set (a  ℓ′)

module LogRel (l : TypeLevel) (rec :  {l′}  l′ < l  LogRelKit) where

  -- Reducibility of Universe:

  -- Universe type
  record _⊩¹U (Γ : Con Term ) : Set (a  ℓ′) where
    constructor Uᵣ
    field
      l′ : TypeLevel
      l< : l′ < l
      ⊢Γ :  Γ

  -- Universe type equality
  _⊩¹U≡_ : (Γ : Con Term ) (B : Term )  Set (a  ℓ′)
  Γ ⊩¹U≡ B = Lift ℓ′ (B PE.≡ U) -- Note lack of reduction

  -- Universe term
  record _⊩¹U_∷U/_ {l′} (Γ : Con Term ) (t : Term ) (l< : l′ < l) : Set (a  ℓ′) where
    constructor Uₜ
    open LogRelKit (rec l<)
    field
      A     : Term 
      d     : Γ  t :⇒*: A  U
      typeA : Type A
      A≡A   : Γ  A  A  U
      [t]   : Γ  t

  -- Universe term equality
  record _⊩¹U_≡_∷U/_ {l′} (Γ : Con Term ) (t u : Term ) (l< : l′ < l) : Set (a  ℓ′) where
    constructor Uₜ₌
    open LogRelKit (rec l<)
    field
      A B   : Term 
      d     : Γ  t :⇒*: A  U
      d′    : Γ  u :⇒*: B  U
      typeA : Type A
      typeB : Type B
      A≡B   : Γ  A  B  U
      [t]   : Γ  t
      [u]   : Γ  u
      [t≡u] : Γ  t  u / [t]

  mutual

    -- Reducibility of Binding types (Π, Σ):

    -- B-type
    record _⊩¹B⟨_⟩_ (Γ : Con Term ) (W : BindingType) (A : Term ) : Set (a  ℓ′) where
      inductive
      constructor Bᵣ
      eta-equality
      field
        F : Term 
        G : Term (1+ )
        D : Γ  A :⇒*:  W  F  G
        ⊢F : Γ  F
        ⊢G : Γ  F  G
        A≡A : Γ   W  F  G   W  F  G
        [F] :  {m} {ρ : Wk m } {Δ : Con Term m}  ρ  Δ  Γ   Δ  Δ ⊩¹ U.wk ρ F
        [G] :  {m} {ρ : Wk m } {Δ : Con Term m} {a : Term m}
             ([ρ] : ρ  Δ  Γ) (⊢Δ :  Δ)
             Δ ⊩¹ a  U.wk ρ F / [F] [ρ] ⊢Δ
             Δ ⊩¹ U.wk (lift ρ) G [ a ]
        G-ext :  {m} {ρ : Wk m } {Δ : Con Term m} {a b}
               ([ρ] : ρ  Δ  Γ) (⊢Δ :  Δ)
               ([a] : Δ ⊩¹ a  U.wk ρ F / [F] [ρ] ⊢Δ)
               ([b] : Δ ⊩¹ b  U.wk ρ F / [F] [ρ] ⊢Δ)
               Δ ⊩¹ a  b  U.wk ρ F / [F] [ρ] ⊢Δ
               Δ ⊩¹ U.wk (lift ρ) G [ a ]  U.wk (lift ρ) G [ b ] / [G] [ρ] ⊢Δ [a]

    -- B-type equality
    record _⊩¹B⟨_⟩_≡_/_ (Γ : Con Term ) (W : BindingType) (A B : Term ) ([A] : Γ ⊩¹B⟨ W  A) : Set (a  ℓ′) where
      inductive
      constructor B₌
      eta-equality
      open _⊩¹B⟨_⟩_ [A]
      field
        F′     : Term 
        G′     : Term (1+ )
        W′     : BindingType
        D′     : Γ  B ⇒*  W′  F′  G′
        W≋W′   : W  W′
        A≡B    : Γ   W  F  G   W′  F′  G′
        [F≡F′] : {m : Nat} {ρ : Wk m } {Δ : Con Term m}
                ([ρ] : ρ  Δ  Γ) (⊢Δ :  Δ)
                Δ ⊩¹ U.wk ρ F  U.wk ρ F′ / [F] [ρ] ⊢Δ
        [G≡G′] :  {m} {ρ : Wk m } {Δ : Con Term m} {a}
                ([ρ] : ρ  Δ  Γ) (⊢Δ :  Δ)
                ([a] : Δ ⊩¹ a  U.wk ρ F / [F] [ρ] ⊢Δ)
                Δ ⊩¹ U.wk (lift ρ) G [ a ]  U.wk (lift ρ) G′ [ a ] / [G] [ρ] ⊢Δ [a]

    -- Term reducibility of Π-type
    _⊩¹Π_∷_/_ : { : Nat} {p q : Mod} (Γ : Con Term ) (t A : Term ) ([A] : Γ ⊩¹B⟨  p q  A)  Set (a  ℓ′)
    _⊩¹Π_∷_/_ {} {p} {q} Γ t A (Bᵣ F G D ⊢F ⊢G A≡A [F] [G] G-ext) =
       λ f  Γ  t :⇒*: f  Π p , q  F  G
            × Function f
            × Γ  f  f  Π p , q  F  G
            × (∀ {m} {ρ : Wk m } {Δ : Con Term m} {a b} {p₁ p₂ : Mod}
              ([ρ] : ρ  Δ  Γ) (⊢Δ :  Δ)
              ([a] : Δ ⊩¹ a  U.wk ρ F / [F] [ρ] ⊢Δ)
              ([b] : Δ ⊩¹ b  U.wk ρ F / [F] [ρ] ⊢Δ)
              ([a≡b] : Δ ⊩¹ a  b  U.wk ρ F / [F] [ρ] ⊢Δ)
               p  p₁
               p  p₂
               Δ ⊩¹ U.wk ρ f  p₁  a  U.wk ρ f  p₂  b  U.wk (lift ρ) G [ a ] / [G] [ρ] ⊢Δ [a])
            × (∀ {m} {ρ : Wk m } {Δ : Con Term m} {a} {p′ : Mod}  ([ρ] : ρ  Δ  Γ) (⊢Δ :  Δ)
               ([a] : Δ ⊩¹ a  U.wk ρ F / [F] [ρ] ⊢Δ)
               p  p′
               Δ ⊩¹ U.wk ρ f  p′  a  U.wk (lift ρ) G [ a ] / [G] [ρ] ⊢Δ [a])
              {- NOTE(WN): Last 2 fields could be refactored to a single forall.
                           But touching this definition is painful, so only do it
                           if you have to change it anyway. -}
    -- Issue: Agda complains about record use not being strictly positive.
    --        Therefore we have to use ×

    -- Term equality of Π-type
    _⊩¹Π_≡_∷_/_ : { : Nat} {p q : Mod} (Γ : Con Term ) (t u A : Term ) ([A] : Γ ⊩¹B⟨  p q  A)  Set (a  ℓ′)
    _⊩¹Π_≡_∷_/_  {} {p} {q} Γ t u A [A]@(Bᵣ F G D ⊢F ⊢G A≡A [F] [G] G-ext) =
      ∃₂ λ f g  Γ  t :⇒*: f  Π p , q  F  G
               × Γ  u :⇒*: g  Π p , q  F  G
               × Function f
               × Function g
               × Γ  f  g  Π p , q  F  G
               × Γ ⊩¹Π t  A / [A]
               × Γ ⊩¹Π u  A / [A]
               × (∀ {m} {ρ : Wk m } {Δ : Con Term m} {a} {p₁ p₂ : Mod} ([ρ] : ρ  Δ  Γ) (⊢Δ :  Δ)
                 ([a] : Δ ⊩¹ a  U.wk ρ F / [F] [ρ] ⊢Δ)  p  p₁  p  p₂
                  Δ ⊩¹ U.wk ρ f  p₁  a  U.wk ρ g  p₂  a  U.wk (lift ρ) G [ a ] / [G] [ρ] ⊢Δ [a])
    -- Issue: Same as above.


    -- Term reducibility of Σ-type
    _⊩¹Σ_∷_/_ : {q : Mod} {m : SigmaMode} (Γ : Con Term ) (t A : Term ) ([A] : Γ ⊩¹B⟨  q m  A)  Set (a  ℓ′)
    _⊩¹Σ_∷_/_ {q = q} {m} Γ t A [A]@(Bᵣ F G D ⊢F ⊢G A≡A [F] [G] G-ext) =
       λ p  Γ  t :⇒*: p  Σ⟨ m  q  F  G
            × Γ  p  p  Σ⟨ m  q  F  G
            × Σ (Product p) λ pProd
             Σ-prop m p Γ [A] pProd

    Σ-prop :  {A q} (m : SigmaMode) (p : Term )  (Γ : Con Term )
            ([A] : Γ ⊩¹B⟨  q m  A)  (Product p)  Set (a  ℓ′)
    Σ-prop Σₚ p Γ (Bᵣ F G D ⊢F ⊢G A≡A [F] [G] G-ext) _ =
      Σ (Γ ⊩¹ fst p  U.wk id F / [F] id (wf ⊢F)) λ [fst]
       Γ ⊩¹ snd p  U.wk (lift id) G [ fst p ] / [G] id (wf ⊢F) [fst]
    Σ-prop Σᵣ p Γ (Bᵣ F G D ⊢F ⊢G A≡A [F] [G] G-ext) (prodₙ {t = p₁} {u = p₂}) =
           Σ (Γ ⊩¹ p₁  U.wk id F / [F] id (wf ⊢F)) λ [p₁]
            Γ ⊩¹ p₂  U.wk (lift id) G [ p₁ ] / [G] id (wf ⊢F) [p₁]
    Σ-prop {q = q} Σᵣ p Γ (Bᵣ F G D ⊢F ⊢G A≡A [F] [G] G-ext) (ne x) =
           Γ  p ~ p  Σᵣ q  F  G

    -- Term equality of Σ-type
    _⊩¹Σ_≡_∷_/_ : {q : Mod} {m : SigmaMode} (Γ : Con Term ) (t u A : Term ) ([A] : Γ ⊩¹B⟨  q m  A)  Set (a  ℓ′)
    _⊩¹Σ_≡_∷_/_ {q = q} {m} Γ t u A [A]@(Bᵣ F G D ⊢F ⊢G A≡A [F] [G] G-ext) =
      ∃₂ λ p r  Γ  t :⇒*: p  Σ⟨ m  q  F  G
               × Γ  u :⇒*: r  Σ⟨ m  q  F  G
               × Γ  p  r  Σ⟨ m  q  F  G
               × Γ ⊩¹Σ t  A / [A]
               × Γ ⊩¹Σ u  A / [A]
               × Σ (Product p) λ pProd
                Σ (Product r) λ rProd
                [Σ]-prop m p r Γ [A] pProd rProd

    [Σ]-prop :  {A q} (m : SigmaMode) (p r : Term )  (Γ : Con Term )
              ([A] : Γ ⊩¹B⟨  q m  A)  (Product p)  (Product r)  Set (a  ℓ′)
    [Σ]-prop Σₚ p r Γ (Bᵣ F G D ⊢F ⊢G A≡A [F] [G] G-ext) _ _ =
      Σ (Γ ⊩¹ fst p  U.wk id F / [F] id (wf ⊢F)) λ [fstp]
       Γ ⊩¹ fst r  U.wk id F / [F] id (wf ⊢F)
      × Γ ⊩¹ fst p  fst r  U.wk id F / [F] id (wf ⊢F)
      × Γ ⊩¹ snd p  snd r  U.wk (lift id) G [ fst p ] / [G] id (wf ⊢F) [fstp]
    [Σ]-prop Σᵣ p r Γ (Bᵣ F G D ⊢F ⊢G A≡A [F] [G] G-ext) (prodₙ {t = p₁} {u = p₂}) (prodₙ {t = r₁} {u = r₂}) =
             Σ (Γ ⊩¹ p₁  U.wk id F / [F] id (wf ⊢F)) λ [p₁] 
             Σ (Γ ⊩¹ r₁  U.wk id F / [F] id (wf ⊢F)) λ [r₁]
              (Γ ⊩¹ p₂  U.wk (lift id) G [ p₁ ] / [G] id (wf ⊢F) [p₁])
             × (Γ ⊩¹ r₂  U.wk (lift id) G [ r₁ ] / [G] id (wf ⊢F) [r₁])
             × (Γ ⊩¹ p₁  r₁  U.wk id F / [F] id (wf ⊢F))
             × (Γ ⊩¹ p₂  r₂  U.wk (lift id) G [ p₁ ] / [G] id (wf ⊢F) [p₁])
    [Σ]-prop Σᵣ p r Γ (Bᵣ F G D ⊢F ⊢G A≡A [F] [G] G-ext) (prodₙ {t = p₁} {u = p₂}) (ne y) =
      Lift (a  ℓ′) PE.⊥
    [Σ]-prop Σᵣ p r Γ (Bᵣ F G D ⊢F ⊢G A≡A [F] [G] G-ext) (ne x) (prodₙ {t = r₁} {u = r₂}) =
      Lift (a  ℓ′) PE.⊥
    [Σ]-prop {q = q} Σᵣ p r Γ (Bᵣ F G D ⊢F ⊢G A≡A [F] [G] G-ext) (ne x) (ne y) =
             Γ  p ~ r  Σᵣ q  F  G

    -- Logical relation definition
    data _⊩¹_ (Γ : Con Term ) : Term   Set (a  ℓ′) where
      Uᵣ  : Γ ⊩¹U  Γ ⊩¹ U
      ℕᵣ  :  {A}  Γ ⊩ℕ A  Γ ⊩¹ A
      Emptyᵣ :  {A}  Γ ⊩Empty A  Γ ⊩¹ A
      Unitᵣ :  {A}  Γ ⊩Unit A  Γ ⊩¹ A
      ne  :  {A}  Γ ⊩ne A  Γ ⊩¹ A
      Bᵣ  :  {A} W  Γ ⊩¹B⟨ W  A  Γ ⊩¹ A
      emb :  {A l′} (l< : l′ < l) (let open LogRelKit (rec l<))
            ([A] : Γ  A)  Γ ⊩¹ A

    _⊩¹_≡_/_ : (Γ : Con Term ) (A B : Term )  Γ ⊩¹ A  Set (a  ℓ′)
    Γ ⊩¹ A  B / Uᵣ UA = Γ ⊩¹U≡ B
    Γ ⊩¹ A  B / ℕᵣ D = Γ ⊩ℕ A  B
    Γ ⊩¹ A  B / Emptyᵣ D = Γ ⊩Empty A  B
    Γ ⊩¹ A  B / Unitᵣ D = Γ ⊩Unit A  B
    Γ ⊩¹ A  B / ne neA = Γ ⊩ne A  B / neA
    Γ ⊩¹ A  B / Bᵣ W BA = Γ ⊩¹B⟨ W  A  B / BA
    Γ ⊩¹ A  B / emb l< [A] = Γ  A  B / [A]
      where open LogRelKit (rec l<)

    _⊩¹_∷_/_ : (Γ : Con Term ) (t A : Term )  Γ ⊩¹ A  Set (a  ℓ′)
    Γ ⊩¹ t  .U / Uᵣ (Uᵣ l′ l< ⊢Γ) = Γ ⊩¹U t ∷U/ l<
    Γ ⊩¹ t  A / ℕᵣ D = Γ ⊩ℕ t ∷ℕ
    Γ ⊩¹ t  A / Emptyᵣ D = Γ ⊩Empty t ∷Empty
    Γ ⊩¹ t  A / Unitᵣ D = Γ ⊩Unit t ∷Unit
    Γ ⊩¹ t  A / ne neA = Γ ⊩ne t  A / neA
    Γ ⊩¹ t  A / Bᵣ BΠ! ΠA  = Γ ⊩¹Π t  A / ΠA
    Γ ⊩¹ t  A / Bᵣ BΣ! ΣA  = Γ ⊩¹Σ t  A / ΣA
    Γ ⊩¹ t  A / emb l< [A] = Γ  t  A / [A]
      where open LogRelKit (rec l<)

    _⊩¹_≡_∷_/_ : (Γ : Con Term ) (t u A : Term )  Γ ⊩¹ A  Set (a  ℓ′)
    Γ ⊩¹ t  u  .U / Uᵣ (Uᵣ l′ l< ⊢Γ) = Γ ⊩¹U t  u ∷U/ l<
    Γ ⊩¹ t  u  A / ℕᵣ D = Γ ⊩ℕ t  u ∷ℕ
    Γ ⊩¹ t  u  A / Emptyᵣ D = Γ ⊩Empty t  u ∷Empty
    Γ ⊩¹ t  u  A / Unitᵣ D = Γ ⊩Unit t  u ∷Unit
    Γ ⊩¹ t  u  A / ne neA = Γ ⊩ne t  u  A / neA
    Γ ⊩¹ t  u  A / Bᵣ BΠ! ΠA = Γ ⊩¹Π t  u  A / ΠA
    Γ ⊩¹ t  u  A / Bᵣ BΣ! ΣA  = Γ ⊩¹Σ t  u  A / ΣA
    Γ ⊩¹ t  u  A / emb l< [A] = Γ  t  u  A / [A]
      where open LogRelKit (rec l<)

    kit : LogRelKit
    kit = Kit _⊩¹U _⊩¹B⟨_⟩_
              _⊩¹_ _⊩¹_≡_/_ _⊩¹_∷_/_ _⊩¹_≡_∷_/_

open LogRel public using (Uᵣ; ℕᵣ; Emptyᵣ; Unitᵣ; ne; Bᵣ; B₌; emb; Uₜ; Uₜ₌)

-- Patterns for the non-records of Π
pattern Πₜ f d funcF f≡f [f] [f]₁ = f , d , funcF , f≡f , [f] , [f]₁
pattern Πₜ₌ f g d d′ funcF funcG f≡g [f] [g] [f≡g] = f , g , d , d′ , funcF , funcG , f≡g , [f] , [g] , [f≡g]
pattern Σₜ p d p≡p pProd prop =  p , d , p≡p , pProd , prop
pattern Σₜ₌ p r d d′ pProd rProd p≅r [t] [u] prop = p , r , d , d′ , p≅r , [t] , [u] , pProd , rProd , prop

pattern Uᵣ′ a b c = Uᵣ (Uᵣ a b c)
pattern ne′ a b c d = ne (ne a b c d)
pattern Bᵣ′ W a b c d e f g h i = Bᵣ W (Bᵣ a b c d e f g h i)
pattern Πᵣ′ a b c d e f g h i = Bᵣ′ BΠ! a b c d e f g h i
pattern Σᵣ′ a b c d e f g h i = Bᵣ′ BΣ! a b c d e f g h i

logRelRec :  l {l′}  l′ < l  LogRelKit
logRelRec  = λ ()
logRelRec ¹ 0<1 = LogRel.kit   ())

kit :  (i : TypeLevel)  LogRelKit
kit l = LogRel.kit l (logRelRec l)
-- a bit of repetition in "kit ¹" definition, would work better with Fin 2 for
-- TypeLevel because you could recurse.

_⊩′⟨_⟩U : (Γ : Con Term ) (l : TypeLevel)  Set (a  ℓ′)
Γ ⊩′⟨ l ⟩U = Γ ⊩U where open LogRelKit (kit l)

_⊩′⟨_⟩B⟨_⟩_ : (Γ : Con Term ) (l : TypeLevel) (W : BindingType)  Term   Set (a  ℓ′)
Γ ⊩′⟨ l ⟩B⟨ W  A = Γ ⊩B⟨ W  A where open LogRelKit (kit l)

_⊩⟨_⟩_ : (Γ : Con Term ) (l : TypeLevel)  Term   Set (a  ℓ′)
Γ ⊩⟨ l  A = Γ  A where open LogRelKit (kit l)

_⊩⟨_⟩_≡_/_ : (Γ : Con Term ) (l : TypeLevel) (A B : Term )  Γ ⊩⟨ l  A  Set (a  ℓ′)
Γ ⊩⟨ l  A  B / [A] = Γ  A  B / [A] where open LogRelKit (kit l)

_⊩⟨_⟩_∷_/_ : (Γ : Con Term ) (l : TypeLevel) (t A : Term )  Γ ⊩⟨ l  A  Set (a  ℓ′)
Γ ⊩⟨ l  t  A / [A] = Γ  t  A / [A] where open LogRelKit (kit l)

_⊩⟨_⟩_≡_∷_/_ : (Γ : Con Term ) (l : TypeLevel) (t u A : Term )  Γ ⊩⟨ l  A  Set (a  ℓ′)
Γ ⊩⟨ l  t  u  A / [A] = Γ  t  u  A / [A] where open LogRelKit (kit l)