-------------------------------------------------------------------------------
-- 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.Syntax.Alphabet

module GenSim.Syntax (𝒜 : RankedAlphabet) where

open import Data.List.Membership.Propositional

open import GenSim.Prelude
open import GenSim.Syntax.Kits as Kits

open Nat using (_≤_; z≤n; s≤s; s≤s⁻¹; ≤-trans)
open Nat.Variables

open L.Any using (here-injective; there-injective)
open RankedAlphabet 𝒜

open Kits
  using (SortTy)
  public

data Sort : SortTy → Set where
  tm   : Sort Var;    tm*  : (n : ℕ) → Sort NoVar
  kd   : Sort NoVar;  kd*  : (n : ℕ) → Sort NoVar

Context = List (Sort Var)

context : ℕ → Context
context n = L.replicate n tm

private module V1 where
  variable
    st   : SortTy
    s s′ : Sort st
    Γ Γ′ Γ₁ Γ₂ Γ₃ : Context
    α α′ α″ α₁ α₂ α₃ : s ∈ Γ

open V1

infix 9 `_ μ_ ⋆*_
infix 7 _#_
infixr 5 _∷_
infix 4 _⊢_

data _⊢_ (Γ : Context) : Sort st → Set where
  ⋆    :                               Γ ⊢ kd
  ⋆*_  : (n : ℕ)                    →  Γ ⊢ kd*  n

  _#_  : (f : Σ) → Γ ⊢ tm* (ar f)   →  Γ ⊢ tm
  `_   : tm ∈ Γ                     →  Γ ⊢ tm
  μ_   : tm ∷ Γ ⊢ tm                →  Γ ⊢ tm

  []   :                              Γ ⊢ tm*  zero
  _∷_  : Γ ⊢ tm → Γ ⊢ tm* n        →  Γ ⊢ tm*  (suc n)

private module V2 where
  variable
    e e′ e″ e₁ e₂ e₃ : Γ ⊢ tm
    t t′ t₁ t₂ t₃ : Γ ⊢ s
    u u′ u₁ u₂ u₃ : Γ ⊢ s
    ts ts₁ ts₂ ts₃ us us₁ us₂ : Γ ⊢ tm* n

open V2

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

#-injective : f₁ # ts ≡ f₂ # us → f₁ ≡ f₂ × ts ≅ us
#-injective refl = refl , refl

μ-injective : μ t₁ ≡ μ t₂ → t₁ ≡ t₂
μ-injective refl = refl

Tm-Syntax : Syntax
Tm-Syntax = record
  { Sort = Sort
  ; _⊢_  = λ Γ s → Γ ⊢ s
  ; `_   = λ{ {_} {tm} → `_ }
  ; `-injective = λ{ {_} {tm} refl → refl }
  }

open Syntax Tm-Syntax hiding (Sort; _⊢_; `_; _∼_) public
open Syntax Tm-Syntax using (_∼_)

infix 4 _≟_

_≟_ : DecidableEquality (Γ ⊢ s)
⋆       ≟ ⋆       = yes refl
⋆* _    ≟ ⋆* _    = yes refl
[]      ≟ []      = yes refl
x ∷ xs  ≟ y ∷ ys  = map′ (uncurry (cong₂ _∷_)) ∷-injective (x ≟ y ×-dec xs ≟ ys)
aˣ # xs ≟ aʸ # ys with eq? aˣ aʸ
... | yes refl = map′ (cong (_ #_)) (≅-to-≡ ∘ proj₂ ∘ #-injective) (xs ≟ ys)
... | no  f≠   = no (f≠ ∘ proj₁ ∘ #-injective)
` here refl ≟ ` here refl = yes refl
` here _    ≟ ` there _   = no λ()
` there _   ≟ ` here _    = no λ()
` there α₁  ≟ ` there α₂  =
  map′ (cong (`_ ∘ there) ∘ `-injective)
       (cong `_ ∘ there-injective ∘ `-injective)
       (` α₁ ≟ ` α₂)
μ x     ≟ μ y     = map′ (cong μ_) (λ{ refl → refl }) (x ≟ y)
cˣ # xs ≟ ` α₂    = no λ()
cˣ # xs ≟ μ y     = no λ()
` α₁    ≟ cʸ # ys = no λ()
` α₁    ≟ μ y     = no λ()
μ x     ≟ cʸ # ys = no λ()
μ x     ≟ ` α₂    = no λ()

infixl 5 _⋯_

_⋯_  : ⦃ K : Kit _∋/⊢_ ⦄ → Γ₁ ⊢ s → Γ₁ –[ K ]→ Γ₂ → Γ₂ ⊢ s
⋆        ⋯ ϕ = ⋆
⋆* n     ⋯ ϕ = ⋆* n
` α      ⋯ ϕ = `/id (α & ϕ)
μ t      ⋯ ϕ = μ (t ⋯ ϕ ↑ tm)
f # ts   ⋯ ϕ = f # (ts ⋯ ϕ)
[]       ⋯ ϕ = []
(t ∷ ts) ⋯ ϕ = (t ⋯ ϕ) ∷ (ts ⋯ ϕ)

⋯-id  : ⦃ K : Kit _∋/⊢_ ⦄ (t : Γ ⊢ s) → t ⋯ id ≡ t
⋯-id ⋆        = refl
⋯-id (⋆* n)   = refl
⋯-id (` α)    = `/`-is-` α
⋯-id (μ t)    = cong μ_ (cong (t ⋯_) (∼-ext id↑∼id) ■ ⋯-id t)
⋯-id (f # ts) = cong (f #_) (⋯-id ts)
⋯-id []       = refl
⋯-id (x ∷ xs) = cong₂ _∷_ (⋯-id x) (⋯-id xs)

Tm-Traversal : Traversal
Tm-Traversal = record
  { _⋯_     = _⋯_
  ; ⋯-var   = λ{ {_} {_} {tm} α ϕ → refl }
  ; ⋯-id    = ⋯-id
  }

open Traversal Tm-Traversal hiding (_⋯_; ⋯-id) public

fusion : ⦃ K₁ : Kit _∋/⊢₁_ ⦄ ⦃ K₂ : Kit _∋/⊢₂_ ⦄ ⦃ K : Kit _∋/⊢_ ⦄ ⦃ W₁ : WkKit K₁ ⦄
         ⦃ C : CKit K₁ K₂ K ⦄ (t : Γ₁ ⊢ s) (ϕ₁ : Γ₁ –[ K₁ ]→ Γ₂) (ϕ₂ : Γ₂ –[ K₂ ]→ Γ₃) →
         (t ⋯ ϕ₁) ⋯ ϕ₂ ≡ t ⋯ (ϕ₁ ·ₖ ϕ₂)

fusion ⋆        ϕ₁ ϕ₂ = refl
fusion (⋆* n)   ϕ₁ ϕ₂ = refl
fusion (` α)    ϕ₁ ϕ₂ = sym (&/⋯-⋯ (α & ϕ₁) ϕ₂)
fusion (μ t)    ϕ₁ ϕ₂ = cong μ_ (fusion t (ϕ₁ ↑ _) (ϕ₂ ↑ _) ■ sym (⋯-cong t (dist-↑-· _ ϕ₁ ϕ₂)))
fusion (f # ts) ϕ₁ ϕ₂ = cong (f #_) $ fusion ts ϕ₁ ϕ₂
fusion []       ϕ₁ ϕ₂ = refl
fusion (t ∷ ts) ϕ₁ ϕ₂ = cong₂ _∷_ (fusion t ϕ₁ ϕ₂) (fusion ts ϕ₁ ϕ₂)

Tm-CTraversal : CTraversal
Tm-CTraversal = record { fusion = fusion }

open CTraversal Tm-CTraversal hiding (fusion) public

Tm-Types : Types
Tm-Types = record { ↑ᵗ = λ where
  tm      → _ , kd
  kd      → _ , kd
  (tm* n) → _ , kd* n
  (kd* n) → _ , kd* n }

open Types Tm-Types hiding (lookup) public

private module V3 where
  variable
    T T₁ T₂ T' T₁' T₂' : Γ ∶⊢ s

open V3

infix 4 𝓒_·_ 𝓒₀_

data 𝓒_·_ (α : tm ∈ Γ) : Γ ⊢ tm → Set where
  𝓒-#  :                    𝓒 α · f # ts
  𝓒-`  :  .(α ≢ α′)      →  𝓒 α · ` α′
  𝓒-μ  :  𝓒 there α · t  →  𝓒 α · μ t

𝓒₀_ : tm ∷ Γ ⊢ tm → Set
𝓒₀ t = 𝓒 here refl · t

𝓒-μ⁻¹ : 𝓒 α · μ t → 𝓒 there α · t
𝓒-μ⁻¹ (𝓒-μ x) = x

¬𝓒α·`α : ¬ 𝓒 α · ` α
¬𝓒α·`α (𝓒-` α≢α) = ⊥-elim-irr (α≢α refl)

𝓒-`-wk⁻¹ : 𝓒 there {x = s} α₁ · ` there α₂ → 𝓒 α₁ · ` α₂
𝓒-`-wk⁻¹ (𝓒-` α≢) = 𝓒-` (α≢ ∘ cong there)

𝓒-wk* : ∀ m → let Γ′ = context m in
  {α : tm ∈ Γ} {t : Γ′ ++ Γ ⊢ tm} →
  𝓒 α & weaken* Γ′ · t →
  𝓒 α & weakenᵣ s′ & weaken* Γ′ · t ⋯ (weakenᵣ s′ ↑* Γ′)
𝓒-wk* m 𝓒-# = 𝓒-#
𝓒-wk* m (𝓒-μ x) = 𝓒-μ (𝓒-wk* (suc m) x)
𝓒-wk* zero (𝓒-` α≢) = 𝓒-` (α≢ ∘ there-injective)
𝓒-wk* (suc m) {α} (𝓒-` {here refl} α≢) = 𝓒-` λ()
𝓒-wk* {s′ = s′} (suc m) {α} (𝓒-` {there α′} α≢)
  with 𝓒-wk* {s′ = s′} m {α} (𝓒-` (α≢ ∘ cong there))
... | 𝓒-` α≢₂ = 𝓒-` (α≢₂ ∘ there-injective)

𝓒-wk : {α : tm ∈ Γ} {t : Γ ⊢ tm} → 𝓒 α · t → 𝓒 wk s′ α · wk s′ t
𝓒-wk = 𝓒-wk* 0

𝓒₀-↑wk : ∀ m → let Γ′ = context m in
  (t : Γ′ ++ Γ ⊢ tm) →
  𝓒 here refl & weaken* Γ′ · t ⋯ weakenᵣ _ ↑* Γ′
𝓒₀-↑wk m       (f # ts)      = 𝓒-#
𝓒₀-↑wk m       (μ t)         = 𝓒-μ (𝓒₀-↑wk (suc m) t)
𝓒₀-↑wk zero    (` α)         = 𝓒-` λ ()
𝓒₀-↑wk (suc m) (` here refl) = 𝓒-` λ ()
𝓒₀-↑wk (suc m) (` there α)   = 𝓒-wk (𝓒₀-↑wk m (` α))

𝓒₀-wk : (t : Γ ⊢ tm) → 𝓒₀ wk _ t
𝓒₀-wk = 𝓒₀-↑wk 0

infix 4 _⊢_∶_

data _⊢_∶_ (Γ : Context) : Γ ⊢ s → Γ ∶⊢ s → Set where
  ⊢#   : Γ ⊢ ts ∶ ⋆* (ar f)         →  Γ ⊢ f # ts  ∶ ⋆
  ⊢`   : (α : tm ∈ Γ)               →  Γ ⊢ ` α     ∶ ⋆
  ⊢μ   : tm ∷ Γ ⊢ t ∶ ⋆ → 𝓒₀ t      →  Γ ⊢ μ t     ∶ ⋆

  []   :                               Γ ⊢ []      ∶ ⋆* zero
  _∷_  : Γ ⊢ t ∶ ⋆ → Γ ⊢ ts ∶ ⋆* n  →  Γ ⊢ t ∷ ts  ∶ ⋆* suc n

∶⊢-irr : (s : Sort st) (T U : Γ ∶⊢ s) → T ≡ U
∶⊢-irr tm ⋆ ⋆ = refl
∶⊢-irr kd ⋆ ⋆ = refl
∶⊢-irr (tm* n) (⋆* n) (⋆* n) = refl
∶⊢-irr (kd* n) (⋆* n) (⋆* n) = refl

ctx : Ctx Γ
ctx {[]}     = []
ctx {tm ∷ Γ} = ⋆ ∷ ctx

ctx-irr : (Γ₁ Γ₂ : Ctx Γ) → Γ₁ ≡ Γ₂
ctx-irr [] [] = refl
ctx-irr (_∷_ {s = s} T₁ Γ₁) (T₂ ∷ Γ₂) = cong₂ _∷_ (∶⊢-irr s T₁ T₂) (ctx-irr Γ₁ Γ₂)

Tm-Typing : Typing
Tm-Typing = record
  { _⊢_∶_ = λ {S = Γ} _ t T → Γ ⊢ t ∶ T
  ; ⊢`    = λ { {Γ} {tm} {_} {α} eq → subst (Γ ⊢ ` α ∶_) (∶⊢-irr tm _ _) (⊢` α) }
  }

open Typing Tm-Typing hiding (_⊢_∶_; ⊢`) public

𝓒-irr : (x y : 𝓒 α · t) → x ≡ y
𝓒-irr 𝓒-#       𝓒-#       = refl
𝓒-irr (𝓒-` α≢₁) (𝓒-` α≢₂) = refl
𝓒-irr (𝓒-μ x)   (𝓒-μ y)   = cong 𝓒-μ (𝓒-irr x y)

⊢-irr  : {t : Γ ⊢ s} {T : Γ ∶⊢ s} (x y : Γ ⊢ t ∶ T) → x ≡ y
⊢-irr (⊢# xs)    (⊢# ys)    = cong ⊢# (⊢-irr xs ys)
⊢-irr (⊢` _)     (⊢` _)     = refl
⊢-irr (⊢μ x cˣ)  (⊢μ y cʸ)  = cong₂ ⊢μ (⊢-irr x y) (𝓒-irr cˣ cʸ)
⊢-irr []         []         = refl
⊢-irr (x ∷ xs)   (y ∷ ys)   = cong₂ _∷_ (⊢-irr x y) (⊢-irr xs ys)

⊢-det : {t : Γ ⊢ s} {u₁ u₂ : Γ ∶⊢ s} → Γ ⊢ t ∶ u₁ → Γ ⊢ t ∶ u₂ → u₁ ≡ u₂
⊢-det []        []        = refl
⊢-det (x ∷ xs)  (y ∷ ys)  = refl
⊢-det (⊢# x)    (⊢# y)    = refl
⊢-det (⊢μ x cˣ) (⊢μ y cʸ) = refl
⊢-det {s = s} (⊢` ⊢α) (⊢` ⊢β) = ∶⊢-irr s _ _

𝓒-⋯ : ∀ ⦃ K : Kit _∋/⊢_ ⦄ ⦃ W : WkKit K ⦄ ⦃ TK : TKit K ⦄
        ⦃ C₁ : CKit K Kᵣ K ⦄ ⦃ C₂ : CKit K Kₛ Kₛ ⦄
        {e : Γ₁ ⊢ tm} {ϕ : Γ₁ –[ K ]→ Γ₂} →
      𝓒 α₁ · e →
      `/id (α₁ & ϕ) ≡ ` α₂ →
      (∀ α′ → .(α₁ ≢ α′) → 𝓒 α₂ · `/id (α′ & ϕ)) →
      𝓒 α₂ · e ⋯ ϕ
𝓒-⋯ 𝓒-#      ϕα≡` ∀𝓒 = 𝓒-#
𝓒-⋯ (𝓒-` α≢) ϕα≡` ∀𝓒 = ∀𝓒 _ α≢
𝓒-⋯ ⦃ K ⦄ (𝓒-μ x)  ϕα≡` ∀𝓒 = 𝓒-μ $ 𝓒-⋯ x (sym (wk-`/id _ _) ■ cong (wk _) ϕα≡`) λ where
  (here refl) α≢ → subst (𝓒 _ ·_) (sym (`/`-is-` ⦃ K ⦄ (here refl))) (𝓒-` λ())
  (there α)   α≢ → subst (𝓒 _ ·_) (wk-`/id _ _) (𝓒-wk (∀𝓒 α (α≢ ∘ cong there)))

𝓒-⋯↑ : ∀ ⦃ K : Kit _∋/⊢_ ⦄ ⦃ W : WkKit K ⦄ ⦃ TK : TKit K ⦄
         ⦃ C₁ : CKit K Kᵣ K ⦄ ⦃ C₂ : CKit K Kₛ Kₛ ⦄
         {e : tm ∷ Γ₁ ⊢ tm} {ϕ : Γ₁ –[ K ]→ Γ₂} →
       𝓒₀ e → 𝓒₀ (e ⋯ ϕ ↑ tm)
𝓒-⋯↑ ⦃ K ⦄ {ϕ = ϕ} x = 𝓒-⋯ x (`/`-is-` ⦃ K ⦄ _) λ where
  (here refl) α≢ → ⊥-elim-irr (α≢ refl)
  (there α)   α≢ → subst (𝓒 _ ·_) (wk-`/id _ _) (𝓒₀-wk (`/id (α & ϕ)))

infixl 5 _⊢⋯_

_⊢⋯_ : ∀ ⦃ K : Kit _∋/⊢_ ⦄ ⦃ W : WkKit K ⦄ ⦃ TK : TKit K ⦄
         ⦃ C₁ : CKit K Kᵣ K ⦄ ⦃ C₂ : CKit K Kₛ Kₛ ⦄
         {e : Γ₁ ⊢ s} {t : Γ₁ ∶⊢ s} {ϕ : Γ₁ –[ K ]→ Γ₂} →
       Γ₁ ⊢ e ∶ t → ϕ ∶ ctx ⇒[ TK ] ctx → Γ₂ ⊢ e ⋯ ϕ ∶ t ⋯ ϕ

⊢# xs     ⊢⋯ ⊢ϕ = ⊢# (xs ⊢⋯ ⊢ϕ)
⊢μ x c    ⊢⋯ ⊢ϕ = ⊢μ (x ⊢⋯ ⊢ϕ ⊢↑ _) (𝓒-⋯↑ c)
[]        ⊢⋯ ⊢ϕ = []
(x ∷ xs)  ⊢⋯ ⊢ϕ = (x ⊢⋯ ⊢ϕ) ∷ (xs ⊢⋯ ⊢ϕ)
(⊢` ⊢α)   ⊢⋯ ⊢ϕ = ⊢`/id (⊢ϕ _ ⋆ (∶⊢-irr tm _ _))

Tm-TTraversal : TTraversal
Tm-TTraversal = record { _⊢⋯_ = λ {Γ₁ = Γ₁} {Γ₂} ⊢t ⊢ϕ → ⊢t ⊢⋯ (subst₂ (_ ∶_⇒ₖ_) (ctx-irr Γ₁ ctx) (ctx-irr Γ₂ ctx) ⊢ϕ) }

open TTraversal Tm-TTraversal hiding (_⊢⋯_) public

unfoldₛ : tm ∷ Γ ⊢ tm → tm ∷ Γ →ₛ Γ
unfoldₛ t = ⦅ μ t ⦆

⊢unfoldₛ : tm ∷ Γ ⊢ t ∶ ⋆ → 𝓒₀ t → unfoldₛ t ∶ ctx ⇒ₛ ctx
⊢unfoldₛ x c = ⊢⦅ ⊢μ x c ⦆ₛ

unfold : tm ∷ Γ ⊢ tm → Γ ⊢ tm
unfold   t    = t  ⋯   ⦅ μ t ⦆ₛ

⊢unfold :  tm ∷ Γ ⊢ t ∶ ⋆ → 𝓒₀ t → Γ ⊢ unfold t ∶ ⋆
⊢unfold  x c  = x  ⊢⋯  ⊢⦅ ⊢μ x c ⦆ₛ

↑-dist-unfold′ :
  ⦃ K : Kit _∋/⊢_ ⦄ ⦃ W : WkKit K ⦄ ⦃ C₁ : CKit K Kᵣ K ⦄ ⦃ C₂ : CKit K Kₛ Kₛ ⦄
  (e e′ : tm ∷ Γ₁ ⊢ tm) (ϕ : Γ₁ –[ K ]→ Γ₂) →
  e ⋯ ϕ ↑ tm ⋯ ⦅ μ e′ ⋯ ϕ ⦆ ≡ e ⋯ ⦅ μ e′ ⦆ ⋯ ϕ
↑-dist-unfold′ ⦃ K ⦄ e e′ ϕ = sym (dist-↑-⦅⦆-⋯ e (μ e′) ϕ)

↑-dist-unfold :
  ⦃ K : Kit _∋/⊢_ ⦄ ⦃ W : WkKit K ⦄ ⦃ C₁ : CKit K Kᵣ K ⦄ ⦃ C₂ : CKit K Kₛ Kₛ ⦄
  (e : tm ∷ Γ₁ ⊢ tm) (ϕ : Γ₁ –[ K ]→ Γ₂) → unfold (e ⋯ ϕ ↑ tm) ≡ unfold e ⋯ ϕ
↑-dist-unfold e = ↑-dist-unfold′ e e

μPrefix : Γ ⊢ s → ℕ
μPrefix (μ t)  = 1 + μPrefix t
μPrefix _      = 0

-- Renamings preserve the μPrefix unconditionally.
μPrefix-⋯ᵣ : (t : Γ₁ ⊢ s) (ρ : Γ₁ →ᵣ Γ₂) → μPrefix (t ⋯ ρ) ≡ μPrefix t
μPrefix-⋯ᵣ ⋆        ρ = refl
μPrefix-⋯ᵣ (⋆* n)   ρ = refl
μPrefix-⋯ᵣ []       ρ = refl
μPrefix-⋯ᵣ (t ∷ ts) ρ = refl
μPrefix-⋯ᵣ (𝓒 # ts) ρ = refl
μPrefix-⋯ᵣ (` α)    ρ = refl
μPrefix-⋯ᵣ (μ t)    ρ = cong suc (μPrefix-⋯ᵣ t (ρ ↑ _))

-- The μPrefix never decreases through substitution.
μPrefix-≤-⋯ : ⦃ K : Kit _∋/⊢_ ⦄ (t : Γ₁ ⊢ s) (ϕ : Γ₁ –[ K ]→ Γ₂) → μPrefix t ≤ μPrefix (t ⋯ ϕ)
μPrefix-≤-⋯ ⋆        ϕ = z≤n
μPrefix-≤-⋯ (⋆* n)   ϕ = z≤n
μPrefix-≤-⋯ []       ϕ = z≤n
μPrefix-≤-⋯ (t ∷ ts) ϕ = z≤n
μPrefix-≤-⋯ (𝓒 # ts) ϕ = z≤n
μPrefix-≤-⋯ (` α)    ϕ = z≤n
μPrefix-≤-⋯ (μ t)    ϕ = s≤s (μPrefix-≤-⋯ t (ϕ ↑ _))

-- Any other kit preserves the μPrefix if it preserves variables.
μPrefix-⋯ : ⦃ K : Kit _∋/⊢_ ⦄ ⦃ W : WkKit K ⦄ →
  (⊢t : Γ₁ ⊢ t ∶ ⋆) (ϕ : Γ₁ –[ K ]→ Γ₂) →
  ((α : tm ∈ Γ₁) → 𝓒 α · t ⊎ ∃ λ α′ → `/id ⦃ K ⦄ (α & ϕ) ≡ ` α′) →
  μPrefix (t ⋯ ϕ) ≡ μPrefix t
μPrefix-⋯ (⊢# ⊢t*)   ϕ ∀𝓒⊎` = refl
μPrefix-⋯ (⊢μ ⊢t c) ϕ ∀𝓒⊎` = cong suc $ μPrefix-⋯ ⊢t (ϕ ↑ tm) λ where
  (here refl) → inj₁ c
  (there α)   → Sum.map 𝓒-μ⁻¹ (Π.map (wk _) λ eq → sym (wk-`/id _ (ϕ _ α)) ■ cong (wk _) eq) (∀𝓒⊎` α)
μPrefix-⋯ (⊢` α) ϕ ∀𝓒⊎` with `/id (ϕ _ α) in ϕα≡
... | f # ts = refl
... | ` α    = refl
... | μ t with ∀𝓒⊎` α
... | inj₁ 𝓒α·α        = case ¬𝓒α·`α 𝓒α·α    of λ()
... | inj₂ (α′ , ϕα≡′) = case sym ϕα≡ ■ ϕα≡′ of λ()

-- As a consequence, unfolding preserves the μPrefix.
μPrefix-unfold : tm ∷ Γ ⊢ t ∶ ⋆ → 𝓒₀ t → μPrefix (unfold t) ≡ μPrefix t

μPrefix-unfold ⊢t c = μPrefix-⋯ ⊢t ⦅ μ _ ⦆ₛ λ where
  (here refl) → inj₁ c
  (there α)   → inj₂ (α , refl)

μPrefix≡1+n⇒∃μ : (e₁ e₂ : Γ ⊢ tm) → .(μPrefix e₁ + μPrefix e₂ ≡ suc n) → ∃ λ e → e₁ ≡ μ e ⊎ e₂ ≡ μ e
μPrefix≡1+n⇒∃μ (μ e₁) e₂     eq = e₁ , inj₁ refl
μPrefix≡1+n⇒∃μ _      (μ e₂) eq = e₂ , inj₂ refl

module Tm-Variables where
  open Nat.Variables public
  open V1 public
  open V2 public
  open V3 public

module FixedScope (Γ : Context) where
  open Tm-Variables public hiding (Γ)

  infix 4 ⊢_ ⊢*_

  Tm   : Set
  Tm*  : ℕ → Set

  Tm      = Γ ⊢ tm
  Tm* n   = Γ ⊢ tm* n

  ⊢_   : Tm → Set
  ⊢*_  : Tm* n → Set

  ⊢   t   = Γ ⊢ t   ∶ ⋆
  ⊢*  ts  = Γ ⊢ ts  ∶ ⋆* _