------------------------------------------------------------------------
-- Unary variants of some relations
------------------------------------------------------------------------

open import Configuration-types hiding (K)
open import Definition.Typed.EqualityRelation
open import Graded.Modality

module Definition.LogicalRelation.Unary
  {a} {M : Set a}
  {𝕄 : Modality M}
  (TC : Type-configuration 𝕄)
  ⦃ eqrel : EqRelSet TC ⦄
  where

open EqRelSet eqrel
open Type-configuration TC

open import Definition.LogicalRelation TC
open import Definition.LogicalRelation.Properties.Kit TC
open import Definition.LogicalRelation.Properties.Primitive TC
open import Definition.LogicalRelation.Properties.Reflexivity TC
open import Definition.LogicalRelation.Properties.Whnf TC
open import Definition.LogicalRelation.Weakening.Restricted TC

open import Definition.Typed TC
open import Definition.Typed.Properties TC
open import Definition.Typed.Weakening.Definition TC

open import Definition.Untyped M hiding (K)
open import Definition.Untyped.Neutral M TC⁻
open import Definition.Untyped.Neutral.Atomic M TC⁻
open import Definition.Untyped.Whnf M TC⁻

open import Tools.Empty
open import Tools.Function
open import Tools.Level as L
open import Tools.Nat
open import Tools.Product
import Tools.PropositionalEquality as PE
open import Tools.Relation
open import Tools.Sum

private variable
  m n            : Nat
  Γ              : Cons _ _
  A B t t₁ t₂ t′ : Term _
  l l′           : Universe-level
  l′<l           : _ <ᵘ _
  s              : Strength
  t-prod t-prod′ : Productᵃ _ _ _
  t-id           : Identityᵃ _ _ _
  p q            : M

------------------------------------------------------------------------
-- Neutral terms

-- Unary reducibility for neutral terms.

infix 4 _⊩neNf_∷_

record _⊩neNf_∷_ (Γ : Cons m n) (k A : Term n) : Set a where
  no-eta-equality
  pattern
  constructor neNfₜ
  field
    neK : Neutralᵃₗ (Γ .defs) k
    k≡k : Γ ⊢~ k ∷ A

opaque

  -- The relation _⊩neNf_∷_ is pointwise logically equivalent to the
  -- diagonal of _⊩neNf_≡_∷_.

  ⊩neNf∷⇔⊩neNf≡∷ : Γ ⊩neNf t ∷ B ⇔ Γ ⊩neNf t ≡ t ∷ B
  ⊩neNf∷⇔⊩neNf≡∷ =
      (λ where
         (neNfₜ a b) → neNfₜ₌ a a b)
    , (λ where
         (neNfₜ₌ a _ b) → neNfₜ a b)

-- Unary reducibility for terms that reduce to neutral terms.

infix 4 _⊩ne_∷_/_

record _⊩ne_∷_/_ (Γ : Cons m n) (t A : Term n) (⊩A : Γ ⊩ne A) :
         Set a where
  no-eta-equality
  pattern
  constructor neₜ
  open _⊩ne_ ⊩A
  field
    k  : Term n
    d  : Γ ⊢ t ⇒* k ∷ K
    nf : Γ ⊩neNf k ∷ K

opaque

  -- The relation _⊩ne_∷_/_ is pointwise logically equivalent to the
  -- diagonal of _⊩ne_≡_∷_.

  ⊩ne∷⇔⊩ne≡∷ : ∀ {⊩B} → Γ ⊩ne t ∷ B / ⊩B ⇔ Γ ⊩ne t ≡ t ∷ B / ⊩B
  ⊩ne∷⇔⊩ne≡∷ =
      (λ where
         (neₜ _ t⇒u ⊩u) → neₜ₌ _ _ t⇒u t⇒u (⊩neNf∷⇔⊩neNf≡∷ .proj₁ ⊩u))
    , (λ where
         (neₜ₌ _ _ t⇒u t⇒v u≡v@(neNfₜ₌ u-ne v-ne _)) →
           neₜ _ t⇒u
             (⊩neNf∷⇔⊩neNf≡∷ .proj₂ $
              PE.subst (flip (_⊩neNf_≡_∷_ _ _) _)
                (whrDet*Term (t⇒v , ne! v-ne) (t⇒u , ne! u-ne)) u≡v))

------------------------------------------------------------------------
-- U

-- Unary reducibility for universe terms.

infix 4 _⊩U_∷U/_

record _⊩U_∷U/_ {T} (Γ : Cons m n) (t : Term n) ([T] : Γ ⊩′⟨ l ⟩U T) :
         Set a where
  no-eta-equality
  pattern
  constructor Uₜ
  open _⊩₁U_ [T]
  open LogRelKit (kit′ k<)
  field
    C      : Term n
    ⇒*C    : Γ ⊢ t ⇒* C ∷ U k
    C-type : Typeₗ (Γ .defs) C
    ≅C     : Γ ⊢≅ C ∷ U k
    ⊩t     : Γ ⊩ t

opaque

  -- The relation _⊩U_∷U/_ is pointwise logically equivalent to
  -- the diagonal of a certain relation.

  ⊩U∷U⇔⊩U≡∷U : ∀ {T} {[T] : Γ ⊩′⟨ l ⟩U T} → Γ ⊩U t ∷U/ [T] ⇔ LogRel._⊩₁U_≡_∷U/_ _ kit′ Γ t t [T]
  ⊩U∷U⇔⊩U≡∷U {[T]} =
      (λ where
         (Uₜ _ t⇒A A-type ≅A ⊩t) →
           Uₜ₌ _ _ t⇒A t⇒A A-type A-type ≅A ⊩t ⊩t
             (⊩<≡⇔⊩≡ k< .proj₂ $ reflEq $ ⊩<⇔⊩ k< .proj₁ ⊩t))
    , (λ where
         (Uₜ₌ _ _ t⇒A t⇒B A-type B-type A≅B ⊩t _ _) →
           Uₜ _ t⇒A A-type
             (PE.subst (flip (_⊢_≅_∷_ _ _) _)
                (whrDet*Term (t⇒B , typeWhnf B-type)
                   (t⇒A , typeWhnf A-type))
                A≅B)
             ⊩t)
    where open _⊩₁U_ [T]

------------------------------------------------------------------------
-- Empty

-- A property for terms of the empty type in WHNF.

data Empty-prop (Γ : Cons m n) (t : Term n) : Set a where
  ne : Γ ⊩neNf t ∷ Empty → Empty-prop Γ t

opaque

  -- The relation Empty-prop is pointwise logically equivalent to the
  -- diagonal of [Empty]-prop.

  Empty-prop⇔[Empty]-prop : Empty-prop Γ t ⇔ [Empty]-prop Γ t t
  Empty-prop⇔[Empty]-prop =
      (λ { (ne ⊩t) → ne (⊩neNf∷⇔⊩neNf≡∷ .proj₁ ⊩t) })
    , (λ { (ne ⊩t) → ne (⊩neNf∷⇔⊩neNf≡∷ .proj₂ ⊩t) })

opaque

  -- If t satisfies Empty-prop Γ, then t is an atomic neutral term (a
  -- specific kind of WHNF).

  empty : Empty-prop Γ t → Neutralᵃₗ (Γ .defs) t
  empty (ne (neNfₜ t-ne _)) = t-ne

-- Unary reducibility for terms of the empty type.

infix 4 _⊩Empty_∷Empty

record _⊩Empty_∷Empty (Γ : Cons m n) (t : Term n) : Set a where
  no-eta-equality
  pattern
  constructor Emptyₜ
  field
    u    : Term n
    ⇒*u  : Γ ⊢ t ⇒* u ∷ Empty
    ≅u   : Γ ⊢≅ u ∷ Empty
    prop : Empty-prop Γ u

opaque

  -- The relation _⊩Empty_∷Empty is pointwise logically equivalent to
  -- the diagonal of _⊩Empty_≡_∷Empty.

  ⊩Empty∷Empty⇔⊩Empty≡∷Empty : Γ ⊩Empty t ∷Empty ⇔ Γ ⊩Empty t ≡ t ∷Empty
  ⊩Empty∷Empty⇔⊩Empty≡∷Empty =
      (λ where
         (Emptyₜ _ ⇒u ≅u u-prop) →
           Emptyₜ₌ _ _ ⇒u ⇒u ≅u
             (Empty-prop⇔[Empty]-prop .proj₁ u-prop))
    , (λ where
         (Emptyₜ₌ u v t⇒u t⇒v u≅v u-v-prop) →
           let u-ne , v-ne = esplit u-v-prop
               v≡u         = whrDet*Term (t⇒v , ne-whnf v-ne)
                               (t⇒u , ne-whnf u-ne)
           in
           Emptyₜ _ t⇒u (PE.subst (flip (_⊢_≅_∷_ _ _) _) v≡u u≅v)
             (Empty-prop⇔[Empty]-prop .proj₂ $
              PE.subst ([Empty]-prop _ _) v≡u u-v-prop))

------------------------------------------------------------------------
-- Unit

-- A property for terms of unit type in WHNF.

data Unit-prop′ (Γ : Cons m n) (s : Strength) :
       Term n → Set a where
  starᵣ : Unit-prop′ Γ s (star s)
  ne    : Γ ⊩neNf t ∷ Unit s → Unit-prop′ Γ s t

opaque

  -- The relation Unit-prop′ Γ s is pointwise logically equivalent
  -- to the diagonal of [Unit]-prop′ Γ s.

  Unit-prop′⇔[Unit]-prop′ : ∀ {s} → Unit-prop′ Γ s t ⇔ [Unit]-prop′ Γ s t t
  Unit-prop′⇔[Unit]-prop′ {s} =
      (λ where
         starᵣ → starᵣ
         (ne ⊩t) → ne (⊩neNf∷⇔⊩neNf≡∷ .proj₁ ⊩t))
    , flip lemma PE.refl
    where
    lemma : [Unit]-prop′ Γ s t t′ → t PE.≡ t′ → Unit-prop′ Γ s t
    lemma starᵣ _ = starᵣ
    lemma (ne ⊩t) PE.refl = ne (⊩neNf∷⇔⊩neNf≡∷ .proj₂ ⊩t)

-- A property for terms of unit type in WHNF.

data Unit-prop (Γ : Cons m n) :
       Strength → Term n → Set a where
  Unitₜʷ : Unit-prop′ Γ 𝕨 t → ¬ Unitʷ-η → Unit-prop Γ 𝕨 t
  Unitₜˢ : Unit-with-η s → Unit-prop Γ s t

opaque

  -- The relation Unit-prop is pointwise logically equivalent to the
  -- diagonal of [Unit]-prop.

  Unit-prop⇔[Unit]-prop : Unit-prop Γ s t ⇔ [Unit]-prop Γ s t t
  Unit-prop⇔[Unit]-prop =
      (λ where
         (Unitₜʷ prop no-η) →
           Unitₜ₌ʷ (Unit-prop′⇔[Unit]-prop′ .proj₁ prop) no-η
         (Unitₜˢ η) → Unitₜ₌ˢ η)
    , (λ where
         (Unitₜ₌ʷ prop no-η) →
           Unitₜʷ (Unit-prop′⇔[Unit]-prop′ .proj₂ prop) no-η
         (Unitₜ₌ˢ η) → Unitₜˢ η)

opaque

  -- A "smart constructor" for Unit-prop.

  Unit-prop′→Unit-prop :
    Unit-prop′ Γ s t →
    Unit-prop Γ s t
  Unit-prop′→Unit-prop {s} prop =
    case Unit-with-η? s of λ where
      (inj₁ η)                → Unitₜˢ η
      (inj₂ (PE.refl , no-η)) → Unitₜʷ prop no-η

-- Unary reducibility for terms of unit type.

infix 4 _⊩Unit⟨_⟩_∷Unit

record _⊩Unit⟨_⟩_∷Unit
         (Γ : Cons m n) (s : Strength) (t : Term n) :
         Set a where
  no-eta-equality
  pattern
  constructor Unitₜ
  field
    u    : Term n
    ↘u   : Γ ⊢ t ↘ u ∷ Unit s
    prop : Unit-prop Γ s u

opaque

  -- The relation _⊩Unit⟨_,_⟩_∷Unit is pointwise logically equivalent
  -- to the diagonal of _⊩Unit⟨_,_⟩_≡_∷Unit.

  ⊩Unit∷Unit⇔⊩Unit≡∷Unit :
    Γ ⊩Unit⟨ s ⟩ t ∷Unit ⇔ Γ ⊩Unit⟨ s ⟩ t ≡ t ∷Unit
  ⊩Unit∷Unit⇔⊩Unit≡∷Unit =
      (λ (Unitₜ _ ↘u prop) →
         Unitₜ₌ _ _ ↘u ↘u (Unit-prop⇔[Unit]-prop .proj₁ prop))
    , (λ (Unitₜ₌ _ _ ↘u ↘v prop) →
         Unitₜ _ ↘u
           (Unit-prop⇔[Unit]-prop .proj₂ $
            PE.subst ([Unit]-prop _ _ _) (whrDet*Term ↘v ↘u) prop))

------------------------------------------------------------------------
-- Π

-- Unary term reducibility for Π-types.

infix 4 _⊩⟨_⟩Π_∷_/_

record _⊩⟨_⟩Π_∷_/_ (Γ : Cons m n) (l : Universe-level) (t A : Term n)
         (⊩A : Γ ⊩′⟨ l ⟩B⟨ BΠ p q ⟩ A) :
         Set a where
  no-eta-equality
  pattern
  constructor Πₜ
  open _⊩ₗB⟨_⟩_ ⊩A
  field
    u     : Term n
    ⇒*u   : Γ ⊢ t ⇒* u ∷ Π p , q ▷ F ▹ G
    u-fun : Functionᵃₗ (Γ .defs) u
    ≅u    : Γ ⊢≅ u ∷ Π p , q ▷ F ▹ G
    ⊩u    : ∀ {m′ n′} {Δ : Cons m′ n′} {ρ : Wk n′ n} {v w}
            (⊢ρ : Δ ⊢ʷᵏʳ ρ ∷ Γ)
            (⊩v : Δ ⊩⟨ l ⟩ v ∷ wk ρ F / [F] ⊢ρ) →
            Δ ⊩⟨ l ⟩ w ∷ wk ρ F / [F] ⊢ρ →
            Δ ⊩⟨ l ⟩ v ≡ w ∷ wk ρ F / [F] ⊢ρ →
            Δ ⊩⟨ l ⟩ wk ρ u ∘⟨ p ⟩ v ≡ wk ρ u ∘⟨ p ⟩ w ∷
              wk (lift ρ) G [ v ]₀ / [G] ⊢ρ ⊩v

opaque

  -- The relation _⊩⟨_⟩Π_∷_/_ is pointwise logically equivalent to the
  -- diagonal of a certain relation.

  ⊩Π∷⇔⊩Π≡∷ :
    (⊩A : Γ ⊩′⟨ l ⟩B⟨ BΠ p q ⟩ A) →
    Γ ⊩⟨ l ⟩Π t ∷ A / ⊩A ⇔
    LogRel._⊩ₗΠ_≡_∷_/_ l kit′ Γ t t A ⊩A
  ⊩Π∷⇔⊩Π≡∷ record{} =
      (λ (Πₜ _ t⇒u u-fun ≅u ⊩u) →
         _ , _ , t⇒u , t⇒u , u-fun , u-fun , ≅u , ⊩u)
    , (λ (_ , _ , t⇒u , t⇒v , u-fun , v-fun , u≅v , u≡v) →
         case whrDet*Term (t⇒u , Functionᵃ→Whnf u-fun)
                (t⇒v , Functionᵃ→Whnf v-fun) of λ {
           PE.refl →
         Πₜ _ t⇒u u-fun u≅v u≡v })

------------------------------------------------------------------------
-- Σ

-- A property for terms of Σ-type in WHNF.

data Σ-prop (Γ : Cons m n) :
  (t : Term n) (s : Strength) → Productᵃₗ (Γ .defs) t →
  Γ ⊩′⟨ l ⟩B⟨ BΣ s p q ⟩ A → Set a where
  𝕤 :
    {⊩A : Γ ⊩′⟨ l ⟩B⟨ BΣ 𝕤 p q ⟩ A} →
    let open _⊩ₗB⟨_⟩_ ⊩A
        id-Γ = ⊢ʷᵏʳid (wf (≅-eq A≡A))
    in
    (t-prod : Productᵃₗ (Γ .defs) t) →
    (⊩fst : Γ ⊩⟨ l ⟩ fst p t ∷ wk id F / [F] id-Γ) →
    Γ ⊩⟨ l ⟩ snd p t ∷ wk (lift id) G [ fst p t ]₀ / [G] id-Γ ⊩fst →
    Σ-prop Γ t 𝕤 t-prod ⊩A
  𝕨-prodₙ :
    {⊩A : Γ ⊩′⟨ l ⟩B⟨ BΣ 𝕨 p q ⟩ A} →
    let open _⊩ₗB⟨_⟩_ ⊩A
        id-Γ = ⊢ʷᵏʳid (wf (≅-eq A≡A))
    in
    (⊩t₁ : Γ ⊩⟨ l ⟩ t₁ ∷ wk id F / [F] id-Γ) →
    Γ ⊩⟨ l ⟩ t₂ ∷ wk (lift id) G [ t₁ ]₀ / [G] id-Γ ⊩t₁ →
    Σ-prop Γ (prodʷ p t₁ t₂) 𝕨 prodₙ ⊩A
  𝕨-ne :
    {⊩A : Γ ⊩′⟨ l ⟩B⟨ BΣ 𝕨 p q ⟩ A} →
    let open _⊩ₗB⟨_⟩_ ⊩A in
    (t-ne : Neutralᵃₗ (Γ .defs) t) →
    Γ ⊢~ t ∷ Σʷ p , q ▷ F ▹ G →
    Σ-prop Γ t 𝕨 (ne t-ne) ⊩A

opaque

  private

    -- Some lemmas used below.

    [Σ]-prop→Σ-prop :
      ∀ s t-prod t-prod′ (⊩A : Γ ⊩′⟨ l ⟩B⟨ BΣ s p q ⟩ A) →
      LogRel.[Σ]-prop l kit′ s t t Γ ⊩A t-prod t-prod′ →
      Σ-prop Γ t s t-prod ⊩A
    [Σ]-prop→Σ-prop 𝕤 t-prod _ record{} (⊩fst , _ , _ , ⊩snd) =
      𝕤 t-prod ⊩fst ⊩snd
    [Σ]-prop→Σ-prop
      𝕨 prodₙ prodₙ record{}
      (PE.refl , PE.refl , PE.refl , _ , ⊩t₁ , _ , _ , ⊩t₂) =
      𝕨-prodₙ ⊩t₁ ⊩t₂
    [Σ]-prop→Σ-prop 𝕨 (ne t-ne) (ne _) record{} ~t =
      𝕨-ne t-ne ~t
    [Σ]-prop→Σ-prop 𝕨 prodₙ  (ne _) record{} ()
    [Σ]-prop→Σ-prop 𝕨 (ne _) prodₙ  record{} ()

    Σ-prop→[Σ]-prop :
      (⊩A : Γ ⊩′⟨ l ⟩B⟨ BΣ s p q ⟩ A) →
      ∀ t-prod′ →
      Σ-prop Γ t s t-prod ⊩A →
      LogRel.[Σ]-prop l kit′ s t t Γ ⊩A t-prod t-prod′
    Σ-prop→[Σ]-prop record{} _ (𝕤 _ ⊩fst ⊩snd) =
      ⊩fst , ⊩fst , ⊩fst , ⊩snd
    Σ-prop→[Σ]-prop record{} prodₙ (𝕨-prodₙ ⊩t₁ ⊩t₂) =
      PE.refl , PE.refl , PE.refl , PE.refl ,
      ⊩t₁ , ⊩t₁ , ⊩t₁ , ⊩t₂
    Σ-prop→[Σ]-prop record{} (ne _) (𝕨-ne _ ~t) =
      ~t
    Σ-prop→[Σ]-prop record{} (ne (ne () _)) (𝕨-prodₙ _ _)
    Σ-prop→[Σ]-prop record{} prodₙ (𝕨-ne (ne () _) _)

  -- A variant of Σ-prop⇔[Σ]-prop (which is defined below).

  Σ-prop⇔[Σ]-prop′ :
    {⊩A : Γ ⊩′⟨ l ⟩B⟨ BΣ s p q ⟩ A} →
    Σ-prop Γ t s t-prod ⊩A ⇔
    LogRel.[Σ]-prop l kit′ s t t Γ ⊩A t-prod t-prod′
  Σ-prop⇔[Σ]-prop′ = Σ-prop→[Σ]-prop _ _ , [Σ]-prop→Σ-prop _ _ _ _

  -- The relation Σ-prop is pointwise logically equivalent to a
  -- certain relation.

  Σ-prop⇔[Σ]-prop :
    {⊩A : Γ ⊩′⟨ l ⟩B⟨ BΣ s p q ⟩ A} →
    Σ-prop Γ t s t-prod ⊩A ⇔
    LogRel.[Σ]-prop l kit′ s t t Γ ⊩A t-prod t-prod
  Σ-prop⇔[Σ]-prop = Σ-prop⇔[Σ]-prop′

-- Unary term reducibility for Σ-types.

infix 4 _⊩⟨_⟩Σ_∷_/_

record _⊩⟨_⟩Σ_∷_/_ (Γ : Cons m n) (l : Universe-level) (t A : Term n)
         (⊩A : Γ ⊩′⟨ l ⟩B⟨ BΣ s p q ⟩ A) :
         Set a where
  no-eta-equality
  pattern
  constructor Σₜ
  open _⊩ₗB⟨_⟩_ ⊩A
  field
    u      : Term n
    ⇒*u    : Γ ⊢ t ⇒* u ∷ Σ⟨ s ⟩ p , q ▷ F ▹ G
    u-prod : Productᵃₗ (Γ .defs) u
    ≅u     : Γ ⊢≅ u ∷ Σ⟨ s ⟩ p , q ▷ F ▹ G
    prop   : Σ-prop Γ u s u-prod ⊩A

opaque

  -- The relation _⊩⟨_⟩Σ_∷_/_ is pointwise logically equivalent to the
  -- diagonal of a certain relation.

  ⊩Σ∷⇔⊩Σ≡∷ :
    (⊩A : Γ ⊩′⟨ l ⟩B⟨ BΣ s p q ⟩ A) →
    Γ ⊩⟨ l ⟩Σ t ∷ A / ⊩A ⇔
    LogRel._⊩ₗΣ_≡_∷_/_ l kit′ Γ t t A ⊩A
  ⊩Σ∷⇔⊩Σ≡∷ record{} =
      (λ (Σₜ _ t⇒u u-prod ≅u prop) →
         _ , _ , t⇒u , t⇒u , ≅u , u-prod , u-prod ,
         Σ-prop⇔[Σ]-prop .proj₁ prop)
    , (λ (_ , _ , t⇒u , t⇒v , u≅v , u-prod , v-prod , prop) →
         case whrDet*Term (t⇒u , Productᵃ→Whnf u-prod)
                (t⇒v , Productᵃ→Whnf v-prod) of λ {
           PE.refl →
         Σₜ _ t⇒u u-prod u≅v (Σ-prop⇔[Σ]-prop′ .proj₂ prop) })

------------------------------------------------------------------------
-- ℕ

mutual

  -- Unary reducibility for natural number terms.

  infix 4 _⊩ℕ_∷ℕ

  record _⊩ℕ_∷ℕ (Γ : Cons m n) (t : Term n) : Set a where
    inductive
    no-eta-equality
    pattern
    constructor ℕₜ
    field
      u    : Term n
      ⇒*u  : Γ ⊢ t ⇒* u ∷ ℕ
      ≅u   : Γ ⊢≅ u ∷ ℕ
      prop : Natural-prop Γ u

  -- A property for natural number terms in WHNF.

  data Natural-prop (Γ : Cons m n) : Term n → Set a where
    sucᵣ  : Γ ⊩ℕ t ∷ℕ → Natural-prop Γ (suc t)
    zeroᵣ : Natural-prop Γ zero
    ne    : Γ ⊩neNf t ∷ ℕ → Natural-prop Γ t

private

  -- Some lemmas used below.

  opaque mutual

    ⊩ℕ∷ℕ→⊩ℕ≡∷ℕ : Γ ⊩ℕ t ∷ℕ → Γ ⊩ℕ t ≡ t ∷ℕ
    ⊩ℕ∷ℕ→⊩ℕ≡∷ℕ (ℕₜ _ t⇒u ≅u u-prop) =
      ℕₜ₌ _ _ t⇒u t⇒u ≅u (Natural-prop→[Natural]-prop u-prop)

    Natural-prop→[Natural]-prop :
      Natural-prop Γ t → [Natural]-prop Γ t t
    Natural-prop→[Natural]-prop = λ where
      (sucᵣ ⊩t) → sucᵣ (⊩ℕ∷ℕ→⊩ℕ≡∷ℕ ⊩t)
      zeroᵣ     → zeroᵣ
      (ne ⊩t)   → ne (⊩neNf∷⇔⊩neNf≡∷ .proj₁ ⊩t)

  opaque mutual

    ⊩ℕ≡∷ℕ→⊩ℕ∷ℕ : Γ ⊩ℕ t ≡ t ∷ℕ → Γ ⊩ℕ t ∷ℕ
    ⊩ℕ≡∷ℕ→⊩ℕ∷ℕ (ℕₜ₌ _ _ t⇒u t⇒v u≅v u-v-prop) =
      let u-nat , v-nat = split u-v-prop
          v≡u           = whrDet*Term (t⇒v , naturalWhnf v-nat)
                            (t⇒u , naturalWhnf u-nat)
      in
      ℕₜ _ t⇒u (PE.subst (flip (_⊢_≅_∷_ _ _) _) v≡u u≅v)
        ([Natural]-prop→Natural-prop v≡u u-v-prop)

    [Natural]-prop→Natural-prop :
      t′ PE.≡ t → [Natural]-prop Γ t t′ → Natural-prop Γ t
    [Natural]-prop→Natural-prop PE.refl = λ where
      (sucᵣ ⊩t) → sucᵣ (⊩ℕ≡∷ℕ→⊩ℕ∷ℕ ⊩t)
      zeroᵣ     → zeroᵣ
      (ne ⊩t)   → ne (⊩neNf∷⇔⊩neNf≡∷ .proj₂ ⊩t)

opaque

  -- The relation _⊩ℕ_∷ℕ is pointwise logically equivalent to the
  -- diagonal of _⊩ℕ_≡_∷ℕ.

  ⊩ℕ∷ℕ⇔⊩ℕ≡∷ℕ : Γ ⊩ℕ t ∷ℕ ⇔ Γ ⊩ℕ t ≡ t ∷ℕ
  ⊩ℕ∷ℕ⇔⊩ℕ≡∷ℕ = ⊩ℕ∷ℕ→⊩ℕ≡∷ℕ , ⊩ℕ≡∷ℕ→⊩ℕ∷ℕ

opaque

  -- The relation Natural-prop is pointwise logically equivalent to
  -- the diagonal of [Natural]-prop.

  Natural-prop⇔[Natural]-prop : Natural-prop Γ t ⇔ [Natural]-prop Γ t t
  Natural-prop⇔[Natural]-prop =
    Natural-prop→[Natural]-prop , [Natural]-prop→Natural-prop PE.refl

opaque

  -- If t satisfies Natural-prop Γ, then t is a "Natural" (a specific
  -- kind of WHNF).

  natural : Natural-prop Γ t → Natural Var-included (Γ .defs) t
  natural (sucᵣ _)            = sucₙ
  natural zeroᵣ               = zeroₙ
  natural (ne (neNfₜ t-ne _)) = ne (ne⁻ t-ne)

------------------------------------------------------------------------
-- Id

opaque

  -- A variant of ⊩Id∷-view⇔ (which is defined below).

  ⊩Id∷-view⇔′ :
    {⊩A : Γ ⊩′⟨ l ⟩Id A}
    {t-id t-id′ : Identityᵃₗ (Γ .defs) t} →
    let open _⊩ₗId_ ⊩A in
    ⊩Id∷-view ⊩A t t-id ⇔
    Identityᵃ-rec t-id
      (Identityᵃ-rec t-id′
         (Γ ⊩⟨ l ⟩ lhs ≡ rhs ∷ Ty / ⊩Ty)
         (L.Lift _ ⊥))
      (Identityᵃ-rec t-id′
         (L.Lift _ ⊥)
         (Γ ⊢~ t ∷ Id Ty lhs rhs))
  ⊩Id∷-view⇔′ {Γ} {l} {A} {t} {⊩A} = lemma₁ _ , lemma₂ _ _
    where
    open _⊩ₗId_ ⊩A

    lemma₁ :
      (t-id′ : Identityᵃₗ (Γ .defs) t) →
      ⊩Id∷-view ⊩A t t-id →
      Identityᵃ-rec t-id
        (Identityᵃ-rec t-id′
           (Γ ⊩⟨ l ⟩ lhs ≡ rhs ∷ Ty / ⊩Ty)
           (L.Lift _ ⊥))
        (Identityᵃ-rec t-id′
           (L.Lift _ ⊥)
           (Γ ⊢~ t ∷ Id Ty lhs rhs))
    lemma₁ rflₙ           (rflᵣ lhs≡rhs)   = lhs≡rhs
    lemma₁ (ne _)         (ne _ ~t)        = ~t
    lemma₁ rflₙ           (ne (ne () _) _)
    lemma₁ (ne (ne () _)) (rflᵣ _)

    lemma₂ :
      (t-id t-id′ : Identityᵃₗ (Γ .defs) t) →
      Identityᵃ-rec t-id
        (Identityᵃ-rec t-id′
           (Γ ⊩⟨ l ⟩ lhs ≡ rhs ∷ Ty / ⊩Ty)
           (L.Lift _ ⊥))
        (Identityᵃ-rec t-id′
           (L.Lift _ ⊥)
           (Γ ⊢~ t ∷ Id Ty lhs rhs)) →
      ⊩Id∷-view ⊩A t t-id
    lemma₂ rflₙ           rflₙ    lhs≡rhs = rflᵣ lhs≡rhs
    lemma₂ (ne t-ne)      (ne _)  ~t      = ne t-ne ~t
    lemma₂ rflₙ           (ne (ne () _))
    lemma₂ (ne (ne () _)) rflₙ

  -- The relation ⊩Id∷-view is pointwise logically equivalent to a
  -- certain relation.

  ⊩Id∷-view⇔ :
    {⊩A : Γ ⊩′⟨ l ⟩Id A} →
    let open _⊩ₗId_ ⊩A in
    ⊩Id∷-view ⊩A t t-id ⇔
    Identityᵃ-rec t-id
      (Identityᵃ-rec t-id
         (Γ ⊩⟨ l ⟩ lhs ≡ rhs ∷ Ty / ⊩Ty)
         (L.Lift _ ⊥))
      (Identityᵃ-rec t-id
         (L.Lift _ ⊥)
         (Γ ⊢~ t ∷ Id Ty lhs rhs))
  ⊩Id∷-view⇔ = ⊩Id∷-view⇔′

-- Unary term reducibility for identity types.

infix 4 _⊩⟨_⟩Id_∷_/_

record _⊩⟨_⟩Id_∷_/_ (Γ : Cons m n) (l : Universe-level) (t A : Term n)
         (⊩A : Γ ⊩′⟨ l ⟩Id A) :
         Set a where
  no-eta-equality
  pattern
  constructor Idₜ
  open _⊩ₗId_ ⊩A
  field
    u    : Term n
    ⇒*u  : Γ ⊢ t ⇒* u ∷ Id Ty lhs rhs
    u-id : Identityᵃₗ (Γ .defs) u
    prop : ⊩Id∷-view ⊩A u u-id

opaque

  -- The relation _⊩⟨_⟩Id_∷_/_ is pointwise logically equivalent to
  -- the diagonal of a certain relation.

  ⊩Id∷⇔⊩Id≡∷ :
    (⊩A : Γ ⊩′⟨ l ⟩Id A) →
    Γ ⊩⟨ l ⟩Id t ∷ A / ⊩A ⇔
    LogRel._⊩ₗId_≡_∷_/_ l kit′ Γ t t A ⊩A
  ⊩Id∷⇔⊩Id≡∷ _ =
      (λ ⊩t →
         let open _⊩⟨_⟩Id_∷_/_ ⊩t in
         _ , _ , ⇒*u , ⇒*u , u-id , u-id , ⊩Id∷-view⇔ .proj₁ prop)
    , (λ (u , v , t⇒u , t⇒v , u-id , v-id , rest) →
         record
           { ⇒*u  = t⇒u
           ; u-id = u-id
           ; prop =
               case whrDet*Term (t⇒u , Identityᵃ→Whnf u-id)
                      (t⇒v , Identityᵃ→Whnf v-id) of λ {
                 PE.refl →
               ⊩Id∷-view⇔′ .proj₂ rest }
           })