-------------------------------------------------------------------------------
-- Copyright 2026 Janek Spaderna
-- 
-- Licensed under the Apache License, Version 2.0 (the "License");
-- you may not use this file except in compliance with the License.
-- You may obtain a copy of the License at
-- 
-- http://www.apache.org/licenses/LICENSE-2.0
-- 
-- Unless required by applicable law or agreed to in writing, software
-- distributed under the License is distributed on an "AS IS" BASIS,
-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-- See the License for the specific language governing permissions and
-- limitations under the License.
-------------------------------------------------------------------------------

open import GenSim.Prelude
open import GenSim.Syntax.Alphabet

module GenSim.Equivalence (𝒜 : RankedAlphabet) where

open import Data.List.Membership.Propositional
open import Relation.Binary.Indexed.Heterogeneous hiding (_=[_]⇒_)

open import GenSim.Syntax 𝒜

open RankedAlphabet 𝒜
open Tm-Variables

private variable
  o h h₁ h₂ : ℕ
  mn mn′ mn₁ mn₂ mn₃ : ℕ × ℕ

infix 9 `_
infix 7 _#_
infixr 5 _∷_
infix 4 𝓣_≤_∶_

data 𝓣_≤_∶_ (Γ : Context) : ℕ → Sort st → Set where
  𝓣⊥   :                                     𝓣 Γ ≤ zero   ∶ s
  _#_  : (f : Σ) →  𝓣 Γ ≤ h ∶ tm* (ar f)  →  𝓣 Γ ≤ suc h  ∶ tm
  `_   : s ∈ Γ                            →  𝓣 Γ ≤ suc h  ∶ s
  []   :                                     𝓣 Γ ≤ suc h  ∶ tm* 0
  _∷_  : 𝓣 Γ ≤ h ∶ tm → 𝓣 Γ ≤ h ∶ tm* n   →  𝓣 Γ ≤ suc h  ∶ tm* (suc n)

𝓣∷-injective : {t u : 𝓣 Γ ≤ h ∶ tm} {ts us : 𝓣 Γ ≤ h ∶ tm* n} → t ∷ ts ≡ u ∷ us → t ≡ u × ts ≡ us
𝓣∷-injective refl = refl , refl

𝓣#-injective : {ts : 𝓣 Γ ≤ h ∶ tm* (ar f₁)} {us : 𝓣 Γ ≤ h ∶ tm* (ar f₂)} →
  f₁ # ts ≡ f₂ # us → f₁ ≡ f₂ × ts ≅ us
𝓣#-injective refl = refl , refl

𝓣`-injective : ∀ {α₁ α₂ : s ∈ Γ} →
  let t₁ : 𝓣 Γ ≤ suc h ∶ s; t₁ = ` α₁ in
  let t₂ : 𝓣 Γ ≤ suc h ∶ s; t₂ = ` α₂ in
  t₁ ≡ t₂ → α₁ ≡ α₂
𝓣`-injective refl = refl

mutual
  ⟦_⟧_  : {t : Γ ⊢ s} {T : Γ ∶⊢ s} → Γ ⊢ t ∶ T → ∀ h → 𝓣 Γ ≤ h ∶ s
  ⟦ x ⟧ h = grow x refl h

  grow : {t : Γ ⊢ s} {T : Γ ∶⊢ s} → Γ ⊢ t ∶ T → ∀ {p} → .(μPrefix t ≡ p) → ∀ h → 𝓣 Γ ≤ h ∶ s
  grow x                     eq 0        = 𝓣⊥
  grow []           {0}      eq (suc h)  = []
  grow (x ∷ xs)     {0}      eq (suc h)  = ⟦ x ⟧ h ∷ ⟦ xs ⟧ h
  grow (⊢# {f} xs)  {0}      eq (suc h)  = f # ⟦ xs ⟧ h
  grow (⊢` α)       {0}      eq (suc h)  = ` α
  grow (⊢μ x c)     {suc _}  eq (suc h)  = grow (⊢unfold x c) (μPrefix-unfold x c ■ suc⁻¹ eq) (suc h)


cast-grow-μPrefix : {t : Γ ⊢ s} {u : Γ ∶⊢ s} (⊢t : Γ ⊢ t ∶ u) →
  ∀ {p₁} .(eq₁ : μPrefix t ≡ p₁) →
  ∀ {p₂} .(eq₂ : μPrefix t ≡ p₂) →
  ∀ {h} → grow ⊢t eq₁ h ≡ grow ⊢t eq₂ h
cast-grow-μPrefix e eq₁ eq₂ {zero} = refl
cast-grow-μPrefix e     {0}     eq₁ {0}     eq₂ {suc _} = refl
cast-grow-μPrefix e     {0}     eq₁ {suc _} eq₂ {suc _} = ⊥-elim-irr (Nat.0≢1+n (sym eq₁ ■ eq₂))
cast-grow-μPrefix e     {suc _} eq₁ {0}     eq₂ {suc _} = ⊥-elim-irr (Nat.0≢1+n (sym eq₂ ■ eq₁))
cast-grow-μPrefix (⊢μ e c) {suc _} eq₁ {suc _} eq₂ {suc _} =
  cast-grow-μPrefix (⊢unfold e c) (μPrefix-unfold e c ■ suc⁻¹ eq₁)
                                  (μPrefix-unfold e c ■ suc⁻¹ eq₂)

cong-grow : {t₁ t₂ : Γ ⊢ s} {u₁ u₂ : Γ ∶⊢ s} (⊢t₁ : Γ ⊢ t₁ ∶ u₁) (⊢t₂ : Γ ⊢ t₂ ∶ u₂) →
  ∀ {p₁} .(eq₁ : μPrefix t₁ ≡ p₁) →
  ∀ {p₂} .(eq₂ : μPrefix t₂ ≡ p₂) →
  t₁ ≡ t₂ →
  grow ⊢t₁ eq₁ h ≡ grow ⊢t₂ eq₂ h
cong-grow t₁ t₂ eq₁ eq₂ refl
  with refl ← ⊢-det t₁ t₂
  rewrite ⊢-irr t₁ t₂
  = cast-grow-μPrefix t₂ eq₁ eq₂

ext-grow≡ : {t₁ t₂ : Γ ⊢ s} {u₁ u₂ : Γ ∶⊢ s} {⊢t₁ : Γ ⊢ t₁ ∶ u₁} {⊢t₂ : Γ ⊢ t₂ ∶ u₂} →
  ∀ {p₁} .{eq₁ : μPrefix t₁ ≡ p₁} →
  ∀ {p₂} .{eq₂ : μPrefix t₂ ≡ p₂} →
  (∀ h → grow ⊢t₁ eq₁ (suc h) ≡ grow ⊢t₂ eq₂ (suc h)) →
  ∀ {h} → grow ⊢t₁ eq₁ h ≡ grow ⊢t₂ eq₂ h
ext-grow≡ eq {0}     = refl
ext-grow≡ eq {suc h} = eq h

cut : 𝓣 Γ ≤ suc h ∶ s → 𝓣 Γ ≤ h ∶ s
cut {h = zero}  t = 𝓣⊥
cut {h = suc h} (` α)    = ` α
cut {h = suc h} (c # xs) = c # cut xs
cut {h = suc h} []       = []
cut {h = suc h} (x ∷ xs) = cut x ∷ cut xs

⟦_⟧·suc-cut : {t : Γ ⊢ s} {u : Γ ∶⊢ s} (e : Γ ⊢ t ∶ u) →
  cut (⟦ e ⟧ suc h) ≡ ⟦ e ⟧ h
cut-grow-suc : {t : Γ ⊢ s} {u : Γ ∶⊢ s} (e : Γ ⊢ t ∶ u) →
  ∀ {p} .(eq : μPrefix t ≡ p) →
  ∀ h → cut (grow e eq (suc h)) ≡ grow e eq h

⟦ e ⟧·suc-cut = cut-grow-suc e refl _

cut-grow-suc t {p} eq zero = refl
cut-grow-suc (⊢# ts)  {0} eq (suc h) = cong (_ #_) ⟦ ts ⟧·suc-cut
cut-grow-suc (⊢` ⊢α)  {0} eq (suc h) = refl
cut-grow-suc []       {0} eq (suc h) = refl
cut-grow-suc (t ∷ ts) {0} eq (suc h) = cong₂ _∷_ ⟦ t ⟧·suc-cut ⟦ ts ⟧·suc-cut
cut-grow-suc (⊢μ t c) {suc p} eq (suc h) = cut-grow-suc (⊢unfold t c) (μPrefix-unfold t c ■ suc⁻¹ eq) (suc h)

cut-∷ : (x : Γ ⊢ t ∶ ⋆) (xs : Γ ⊢ ts ∶ ⋆* n) → cut (⟦ x ⟧ h ∷ ⟦ xs ⟧ h) ≡ ⟦ x ∷ xs ⟧ h
cut-∷ {h = zero} x xs = refl
cut-∷ {h = suc h} x xs = cong₂ _∷_ ⟦ x ⟧·suc-cut ⟦ xs ⟧·suc-cut

module 𝓣-Sub where
  𝓣≤_Sub : ℕ → Context → Context → Set
  𝓣≤ h Sub Γ₁ Γ₂ = ∀ s (α : s ∈ Γ₁) → 𝓣 Γ₂ ≤ h ∶ s

  infix 4 _∼_

  _∼_ : Rel (𝓣≤ h Sub Γ₁ Γ₂) _
  σ₁ ∼ σ₂ = ∀ s α → σ₁ s α ≡ σ₂ s α

  𝓣-idₛ : 𝓣≤ h Sub Γ Γ
  𝓣-idₛ {h = zero}  s α = 𝓣⊥
  𝓣-idₛ {h = suc h} s α = ` α

  _𝓣-∷ₛ_ : 𝓣 Γ₂ ≤ h ∶ s → 𝓣≤ h Sub Γ₁ Γ₂ → 𝓣≤ h Sub (s ∷ Γ₁) Γ₂
  (t 𝓣-∷ₛ σ) s (here refl) = t
  (t 𝓣-∷ₛ σ) s (there α)   = σ s α

  𝓣-0↦ : 𝓣 Γ ≤ h ∶ s → 𝓣≤ h Sub (s ∷ Γ) Γ
  𝓣-0↦ t = t 𝓣-∷ₛ 𝓣-idₛ

  ⟦_⟧ₛ_ : ⦃ K : Kit _∋/⊢_ ⦄ ⦃ TK : TKit K ⦄
    {ϕ : Γ₁ –[ K ]→ Γ₂} → ϕ ∶ ctx ⇒ₖ ctx → ∀ h → 𝓣≤ h Sub Γ₁ Γ₂
  ⟦_⟧ₛ_ ⦃ TK = TK ⦄ ϕ h tm α = ⟦ ⊢`/id ⦃ TK ⦄ (ϕ α ⋆ (∶⊢-irr tm _ _)) ⟧ h

  cutₛ : 𝓣≤ suc h Sub Γ₁ Γ₂ → 𝓣≤ h Sub Γ₁ Γ₂
  cutₛ σ s α = cut (σ s α)

  ⟦_⟧ₛ·suc-cut : ⦃ K : Kit _∋/⊢_ ⦄ ⦃ TK : TKit K ⦄
    {ϕ : Γ₁ –[ K ]→ Γ₂} →
    (⊢ϕ : ϕ ∶ ctx ⇒ₖ ctx) →
    cutₛ (⟦_⟧ₛ_ ⦃ K ⦄ ⦃ TK ⦄ ⊢ϕ (suc h)) ∼ ⟦_⟧ₛ_ ⦃ K ⦄ ⦃ TK ⦄ ⊢ϕ h
  ⟦ ϕ ⟧ₛ·suc-cut tm α = ⟦ _ ⟧·suc-cut

  cut∼ : {σ₁ σ₂ : 𝓣≤ suc h Sub Γ₁ Γ₂} → σ₁ ∼ σ₂ → cutₛ σ₁ ∼ cutₛ σ₂
  cut∼ eq s α = cong cut (eq s α)

  cut-id∼ : cutₛ 𝓣-idₛ ∼ 𝓣-idₛ {h = h} {Γ = Γ}
  cut-id∼ {zero}  s α = refl
  cut-id∼ {suc h} s α = refl

  infixr 1 _■∼_

  _■∼_ : {σ₁ σ₂ σ₃ : 𝓣≤ h Sub Γ₁ Γ₂} → σ₁ ∼ σ₂ → σ₂ ∼ σ₃ → σ₁ ∼ σ₃
  eq₁ ■∼ eq₂ = λ s α → eq₁ s α ■ eq₂ s α

  infixl 5 _𝓣⋯_

  _𝓣⋯_ : 𝓣 Γ₁ ≤ h ∶ s → 𝓣≤ h Sub Γ₁ Γ₂ → 𝓣 Γ₂ ≤ h ∶ s
  𝓣⊥       𝓣⋯ σ = 𝓣⊥
  ` α      𝓣⋯ σ = σ _ α
  (c # xs) 𝓣⋯ σ = c # (xs 𝓣⋯ cutₛ σ)
  []       𝓣⋯ σ = []
  (x ∷ xs) 𝓣⋯ σ = (x 𝓣⋯ cutₛ σ) ∷ (xs 𝓣⋯ cutₛ σ)

  infixr 9 _𝓣·_

  _𝓣·_ : 𝓣≤ h Sub Γ₁ Γ₂ → 𝓣≤ h Sub Γ₂ Γ₃ → 𝓣≤ h Sub Γ₁ Γ₃
  (σ₁ 𝓣· σ₂) s z = σ₁ s z 𝓣⋯ σ₂

  𝓣⋯-cong : (t : 𝓣 Γ₁ ≤ h ∶ s) {σ₁ σ₂ : 𝓣≤ h Sub Γ₁ Γ₂} → σ₁ ∼ σ₂ → t 𝓣⋯ σ₁ ≡ t 𝓣⋯ σ₂
  𝓣⋯-cong 𝓣⊥       eq = refl
  𝓣⋯-cong (` α)    eq = eq _ α
  𝓣⋯-cong (c # xs) eq = cong (c #_) (𝓣⋯-cong xs (cut∼ eq))
  𝓣⋯-cong []       eq = refl
  𝓣⋯-cong (x ∷ xs) eq = cong₂ _∷_ (𝓣⋯-cong x (cut∼ eq)) (𝓣⋯-cong xs (cut∼ eq))

  𝓣⋯-id : {σ : 𝓣≤ h Sub Γ Γ} → σ ∼ 𝓣-idₛ → ∀ {st} {s : Sort st} (t : 𝓣 Γ ≤ h ∶ s) → t 𝓣⋯ σ ≡ t
  𝓣⋯-id σ≗ 𝓣⊥       = refl
  𝓣⋯-id σ≗ (` α)    = σ≗ _ α
  𝓣⋯-id σ≗ (c # ts) = cong (c #_) (𝓣⋯-id (cut∼ σ≗ ■∼ cut-id∼) ts)
  𝓣⋯-id σ≗ []       = refl
  𝓣⋯-id σ≗ (x ∷ xs) = cong₂ _∷_ (𝓣⋯-id (cut∼ σ≗ ■∼ cut-id∼) x) (𝓣⋯-id (cut∼ σ≗ ■∼ cut-id∼) xs)

  ⟦_⟧-comm-⋯ :
    ⦃ K : Kit _∋/⊢_ ⦄ ⦃ W : WkKit K ⦄ ⦃ TK : TKit K ⦄ ⦃ C₁ : CKit K Kᵣ K ⦄ ⦃ C₂ : CKit K Kₛ Kₛ ⦄ →
    {ϕ : Γ₁ –[ K ]→ Γ₂} {e : Γ₁ ⊢ s} {t : Γ₁ ∶⊢ s} →
    (x : Γ₁ ⊢ e ∶ t) (⊢ϕ : ϕ ∶ ctx ⇒ₖ ctx) →
    ∀ {h} → ⟦ _⊢⋯_ ⦃ TK = TK ⦄ x ⊢ϕ ⟧ h ≡ ⟦ x ⟧ h 𝓣⋯ ⟦_⟧ₛ_ ⦃ TK = TK ⦄ ⊢ϕ h

  grow-comm-⋯ :
    ⦃ K : Kit _∋/⊢_ ⦄ ⦃ W : WkKit K ⦄ ⦃ TK : TKit K ⦄ ⦃ C₁ : CKit K Kᵣ K ⦄ ⦃ C₂ : CKit K Kₛ Kₛ ⦄ →
    {ϕ : Γ₁ –[ K ]→ Γ₂} {e : Γ₁ ⊢ s} {t : Γ₁ ∶⊢ s} →
    (x : Γ₁ ⊢ e ∶ t) (⊢ϕ : ϕ ∶ ctx ⇒ₖ ctx) →
    ∀ {p}  .(eq  : μPrefix e ≡ p) →
    ∀ {p′} .(eq′ : μPrefix (e ⋯ ϕ) ≡ p′) →
    ∀ h → grow (x ⊢⋯ ⊢ϕ) eq′ h ≡ grow x eq h 𝓣⋯ ⟦_⟧ₛ_ ⦃ TK = TK ⦄ ⊢ϕ h

  ⟦ x ⟧-comm-⋯ ϕ = grow-comm-⋯ x ϕ refl refl _

  grow-comm-⋯ t ϕ eq eq′ 0 = refl
  grow-comm-⋯ {s = tm} ⦃ TK = TK ⦄ (⊢` α) ϕ {0} eq eq′ (suc h) =
    cong-grow (⊢`/id ⦃ TK ⦄ (ϕ α _ (∶⊢-irr tm _ _)))
              (⊢`/id ⦃ TK ⦄ (ϕ α _ (∶⊢-irr tm _ _)))
              eq′ refl refl
  grow-comm-⋯ []       ϕ {0} eq {0} eq′ (suc h) = refl
  grow-comm-⋯ (x ∷ xs) ϕ {0} eq {0} eq′ (suc h) =
    cong₂ _∷_ (⟦ x  ⟧-comm-⋯ ϕ ■ sym (𝓣⋯-cong (⟦ x  ⟧ h) ⟦ ϕ ⟧ₛ·suc-cut))
              (⟦ xs ⟧-comm-⋯ ϕ ■ sym (𝓣⋯-cong (⟦ xs ⟧ h) ⟦ ϕ ⟧ₛ·suc-cut))
  grow-comm-⋯ (⊢# xs) ϕ {0} eq {0} eq′ (suc h) = cong (_ #_) $
    ⟦ xs ⟧-comm-⋯ ϕ ■ sym (𝓣⋯-cong (⟦ xs ⟧ h) ⟦ ϕ ⟧ₛ·suc-cut)
  grow-comm-⋯ ⦃ TK = TK ⦄ {e = μ e} (⊢μ x c) ϕ {suc p} eq {suc p′} eq′ (suc h) =
    let μPrefix-unfold/μ⋯ϕ =
          sym (cong μPrefix (↑-dist-unfold e _))
            ■ μPrefix-unfold (x ⊢⋯ _⊢↑_ ⦃ TK ⦄ ϕ _) (𝓒-⋯↑ c)
    in
    let IH = grow-comm-⋯ (⊢unfold x c) ϕ
               {p}  (μPrefix-unfold x c ■ suc⁻¹ eq)
               {p′} (μPrefix-unfold/μ⋯ϕ ■ suc⁻¹ eq′)
    in
    let grow-eq = cong-grow (⊢unfold (x ⊢⋯ _⊢↑_ ⦃ TK ⦄ ϕ _) (𝓒-⋯↑ c))
                            (⊢unfold x c ⊢⋯ ϕ)
                            (μPrefix-unfold (x ⊢⋯ _⊢↑_ ⦃ TK ⦄ ϕ _) (𝓒-⋯↑ c) ■ suc⁻¹ eq′)
                            (μPrefix-unfold/μ⋯ϕ ■ suc⁻¹ eq′)
                            (↑-dist-unfold e _)
    in
    grow-eq ■ IH _

  cut-⋯ : (t : 𝓣 Γ ≤ suc h ∶ s) (σ : 𝓣≤ suc h Sub Γ Γ′) →
    cut (t 𝓣⋯ σ) ≡ cut t 𝓣⋯ cutₛ σ
  cut-⋯ {h = zero} t σ = refl
  cut-⋯ {h = suc h} (` α)    σ = refl
  cut-⋯ {h = suc h} []       σ = refl
  cut-⋯ {h = suc h} (t ∷ ts) σ = cong₂ _∷_ (cut-⋯ t (cutₛ σ)) (cut-⋯ ts (cutₛ σ))
  cut-⋯ {h = suc h} (f # t)  σ = cong (f #_) (cut-⋯ t (cutₛ σ))

  cut-dist-· : (σ₁ : 𝓣≤ suc h Sub Γ₁ Γ₂) (σ₂ : 𝓣≤ suc h Sub Γ₂ Γ₃) →
    cutₛ (σ₁ 𝓣· σ₂) ∼ cutₛ σ₁ 𝓣· cutₛ σ₂
  cut-dist-· σ₁ σ₂ s α = cut-⋯ (σ₁ s α) σ₂

  𝓣-fusion : (t : 𝓣 Γ₁ ≤ h ∶ s) (σ₁ : 𝓣≤ h Sub Γ₁ Γ₂) (σ₂ : 𝓣≤ h Sub Γ₂ Γ₃) →
    t 𝓣⋯ σ₁ 𝓣⋯ σ₂ ≡ t 𝓣⋯ σ₁ 𝓣· σ₂
  𝓣-fusion 𝓣⊥ σ₁ σ₂ = refl
  𝓣-fusion [] σ₁ σ₂ = refl
  𝓣-fusion (t ∷ ts) σ₁ σ₂ = cong₂ _∷_
    (𝓣-fusion t (cutₛ σ₁) (cutₛ σ₂) ■ sym (𝓣⋯-cong t (cut-dist-· σ₁ σ₂)))
    (𝓣-fusion ts (cutₛ σ₁) (cutₛ σ₂) ■ sym (𝓣⋯-cong ts (cut-dist-· σ₁ σ₂)))
  𝓣-fusion (f # ts) σ₁ σ₂ = cong (f #_)
    (𝓣-fusion ts (cutₛ σ₁) (cutₛ σ₂) ■ sym (𝓣⋯-cong ts (cut-dist-· σ₁ σ₂)))
  𝓣-fusion (` α) σ₁ σ₂ = refl

  ⟦_⟧-fusion-⋯ :
    {t : Γ₁ ⊢ s} {T : Γ₁ ∶⊢ s} (x : Γ₁ ⊢ t ∶ T) →
    ⦃ K₁ : Kit _∋/⊢₁_ ⦄ ⦃ K₂ : Kit _∋/⊢₂_ ⦄
    ⦃ W₁ : WkKit K₁ ⦄ ⦃ W₁ : WkKit K₂ ⦄ ⦃ TK₁ : TKit K₁ ⦄ ⦃ TK₂ : TKit K₂ ⦄
    ⦃ _ : CKit K₁ K₁ K₁ ⦄ ⦃ _ : CKit K₁ Kᵣ K₁ ⦄ ⦃ _ : CKit K₁ Kₛ Kₛ ⦄
    ⦃ _ : CKit K₂ K₂ K₂ ⦄ ⦃ _ : CKit K₂ Kᵣ K₂ ⦄ ⦃ _ : CKit K₂ Kₛ Kₛ ⦄
    ⦃ K : Kit _∋/⊢_ ⦄ ⦃ _ : CKit K₁ K₂ K ⦄
    {ϕ₁ : Γ₁ –[ K₁ ]→ Γ₂} {ϕ₂ : Γ₂ –[ K₂ ]→ Γ₃} →
    (⊢ϕ₁ : ϕ₁ ∶ ctx ⇒[ TK₁ ] ctx) (⊢ϕ₂ : ϕ₂ ∶ ctx ⇒[ TK₂ ] ctx) →
    ∀ {h} → ⟦ x ⊢⋯ ⊢ϕ₁ ⊢⋯ ⊢ϕ₂ ⟧ h
              ≡
            ⟦ x ⟧ h 𝓣⋯ (⟦_⟧ₛ_ ⦃ K₁ ⦄ ⊢ϕ₁ h 𝓣· ⟦_⟧ₛ_ ⦃ K₂ ⦄ ⊢ϕ₂ h)

  ⟦ x ⟧-fusion-⋯ ⊢ϕ₁ ⊢ϕ₂ =
    ⟦ x ⊢⋯ ⊢ϕ₁ ⟧-comm-⋯ ⊢ϕ₂
      ■ cong (_𝓣⋯ _) (⟦ x ⟧-comm-⋯ ⊢ϕ₁)
      ■ 𝓣-fusion (⟦ x ⟧ _) _ _

infix 4 _≃_ _≄_

_≃_ : {t₁ t₂ : Γ ⊢ s} {T₁ T₂ : Γ ∶⊢ s} → Γ ⊢ t₁ ∶ T₁ → Γ ⊢ t₂ ∶ T₂ → Set
x ≃ y = ∀ h → ⟦ x ⟧ h ≡ ⟦ y ⟧ h

_≄_ : {t₁ t₂ : Γ ⊢ s} {T₁ T₂ : Γ ∶⊢ s} → Γ ⊢ t₁ ∶ T₁ → Γ ⊢ t₂ ∶ T₂ → Set
x ≄ y = ¬ x ≃ y

≃-refl : {t : Γ ⊢ s} {T : Γ ∶⊢ s} {x : Γ ⊢ t ∶ T} → x ≃ x
≃-refl h = refl

≃-reflexive : {t₁ t₂ : Γ ⊢ s} {T₁ T₂ : Γ ∶⊢ s} {x : Γ ⊢ t₁ ∶ T₁} {y : Γ ⊢ t₂ ∶ T₂} → t₁ ≡ t₂ → x ≃ y
≃-reflexive {x = x} {y} refl
  with refl ← ⊢-det x y
  with refl ← ⊢-irr x y
  = ≃-refl

≃-sym : {t₁ t₂ : Γ ⊢ s} {T₁ T₂ : Γ ∶⊢ s} {x : Γ ⊢ t₁ ∶ T₁} {y : Γ ⊢ t₂ ∶ T₂} → x ≃ y → y ≃ x
≃-sym eq h = sym (eq h)

infixr 1 _≃■_

_≃■_ : {t₁ t₂ t₃ : Γ ⊢ s} {T₁ T₂ T₃ : Γ ∶⊢ s} {x : Γ ⊢ t₁ ∶ T₁} {y : Γ ⊢ t₂ ∶ T₂} {z : Γ ⊢ t₃ ∶ T₃} → x ≃ y → y ≃ z → x ≃ z
eq₁₂ ≃■ eq₂₃ = λ h → eq₁₂ h ■ eq₂₃ h

≃-isEquivalence : (Γ : Context) (s : Sort st) (T : Γ ∶⊢ s) → IsIndexedEquivalence {I = Γ ⊢ s} (Γ ⊢_∶ T) _≃_
≃-isEquivalence _ _ _ = record { refl = ≃-refl ; sym = ≃-sym ; trans = _≃■_ }

≃-setoid : (Γ : Context) (T : Γ ∶⊢ s) → IndexedSetoid (Γ ⊢ s) _ _
≃-setoid Γ T = record { isEquivalence = ≃-isEquivalence Γ _ T }

≃-# : {es₁ es₂ : Γ ⊢ tm* (ar f)} {xs : Γ ⊢ es₁ ∶ ⋆* _} {ys : Γ ⊢ es₂ ∶ ⋆* _} →
      xs ≃ ys →
      ⊢# {f = f} xs  ≃ ⊢# {f = f} ys
≃-# eq zero = refl
≃-# eq (suc h) = cong (_ #_) (eq h)

≃-∷ : {e₁ e₂ : Γ ⊢ tm} {es₁ es₂ : Γ ⊢ tm* n} →
      {x : Γ ⊢ e₁ ∶ ⋆} {y : Γ ⊢ e₂ ∶ ⋆} →
      {xs : Γ ⊢ es₁ ∶ ⋆* n} {ys : Γ ⊢ es₂ ∶ ⋆* n} →
      x ≃ y →
      xs ≃ ys →
      x ∷ xs ≃ y ∷ ys
≃-∷ eq₁ eq₂ zero = refl
≃-∷ eq₁ eq₂ (suc h) = cong₂ _∷_ (eq₁ h) (eq₂ h)

≃-μ  : {e : tm ∷ Γ ⊢ tm} (x : tm ∷ Γ ⊢ e ∶ ⋆) (c : 𝓒₀ e) → ⊢μ x c ≃ ⊢unfold x c
≃-μ x c zero = refl
≃-μ x c (suc h) = cast-grow-μPrefix (⊢unfold x c) (μPrefix-unfold x c) refl

≃⁻¹-∷ : {e₁ e₂ : Γ ⊢ tm} {es₁ es₂ : Γ ⊢ tm* n} →
        {x : Γ ⊢ e₁ ∶ ⋆} {y : Γ ⊢ e₂ ∶ ⋆} →
        {xs : Γ ⊢ es₁ ∶ ⋆* n} {ys : Γ ⊢ es₂ ∶ ⋆* n} →
        x ∷ xs ≃ y ∷ ys →
        x ≃ y × xs ≃ ys
≃⁻¹-∷ eq = proj₁ ∘ 𝓣∷-injective ∘ eq ∘ suc , proj₂ ∘ 𝓣∷-injective ∘ eq ∘ suc

≃⁻¹-# : {es₁ es₂ : Γ ⊢ tm* (ar f)} →
        {xs : Γ ⊢ es₁ ∶ ⋆* _} {ys : Γ ⊢ es₂ ∶ ⋆* _} →
        ⊢# xs ≃ ⊢# ys →
        xs ≃ ys
≃⁻¹-# eq = ≅-to-≡ ∘ proj₂ ∘ 𝓣#-injective ∘ eq ∘ suc