-------------------------------------------------------------------------------
-- 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.Subterm (𝒜 : RankedAlphabet) where

open import Data.List.Membership.Propositional

open import GenSim.Prelude
open import GenSim.Syntax 𝒜

open RankedAlphabet 𝒜
open Tm-Variables

infix 4 _⊑_

data _⊑_ (t : Γ ⊢ s) : Γ ⊢ s′ → Set where
  refl  : t ⊑ t
  ⊑-μ   : t ⊑ unfold u → t ⊑ μ u
  ⊑-#   : t ⊑ us       → t ⊑ f # us
  here  : t ⊑ u        → t ⊑ u ∷ us
  there : t ⊑ us       → t ⊑ u ∷ us

⊑-reflexive : t ≡ u → t ⊑ u
⊑-reflexive refl = refl

⊑-⋯ : ⦃ K : Kit _∋/⊢_ ⦄ ⦃ W : WkKit K ⦄ ⦃ C₁ : CKit K Kᵣ K ⦄ ⦃ C₂ : CKit K Kₛ Kₛ ⦄ →
  t ⊑ u → (ϕ : Γ –[ K ]→ Γ′) → t ⋯ ϕ ⊑ u ⋯ ϕ
⊑-⋯ refl      ϕ = refl
⊑-⋯ (⊑-#   x) ϕ = ⊑-#   (⊑-⋯ x ϕ)
⊑-⋯ (here  x) ϕ = here  (⊑-⋯ x ϕ)
⊑-⋯ (there x) ϕ = there (⊑-⋯ x ϕ)
⊑-⋯ {t = t} {u = μ u} (⊑-μ x) ϕ =
  ⊑-μ (subst (t ⋯ ϕ ⊑_) (sym (↑-dist-unfold u ϕ)) (⊑-⋯ x ϕ))

⊑-trans : t₁ ⊑ t₂ → t₂ ⊑ t₃ → t₁ ⊑ t₃
⊑-trans x refl      = x
⊑-trans x (⊑-μ   y) = ⊑-μ   (⊑-trans x y)
⊑-trans x (⊑-#   y) = ⊑-#   (⊑-trans x y)
⊑-trans x (here  y) = here  (⊑-trans x y)
⊑-trans x (there y) = there (⊑-trans x y)

infix 4 _≼_

data _≼_ {Γ} : Γ ⊢ s → Γ ⊢ s′ → Set where
  refl  : t ≼ t
  ≼-μ   : t ≼ u  → t ⋯ ⦅ μ u ⦆ ≼ μ u
  ≼-#   : t ≼ us → t ≼ f # us
  here  : t ≼ u  → t ≼ u ∷ us
  there : t ≼ us → t ≼ u ∷ us

≼-reflexive : t ≡ u → t ≼ u
≼-reflexive refl = refl

≼-⋯ : ⦃ K : Kit _∋/⊢_ ⦄ ⦃ W : WkKit K ⦄ ⦃ C₁ : CKit K Kᵣ K ⦄ ⦃ C : CKit K Kₛ Kₛ ⦄ →
  t ≼ u → (ϕ : Γ –[ K ]→ Γ′) → t ⋯ ϕ ≼ u ⋯ ϕ
≼-⋯ refl      ϕ = refl
≼-⋯ (≼-#   x) ϕ = ≼-#   (≼-⋯ x ϕ)
≼-⋯ (here  x) ϕ = here  (≼-⋯ x ϕ)
≼-⋯ (there x) ϕ = there (≼-⋯ x ϕ)
≼-⋯ (≼-μ {t = t} {u = u} x) ϕ =
  subst (_≼ _) (sym (dist-↑-⦅⦆-⋯ t (μ u) ϕ)) (≼-μ (≼-⋯ x (ϕ ↑ _)))

≼-wk⁻¹ : ∀ Γ {s⁰} {t : Γ ++ s⁰ ∷ Γ′ ⊢ s} {u : Γ ++ Γ′ ⊢ s′} →
  t ≼ u ⋯ weakenᵣ _ ↑* Γ →
  ∃ λ t′ → t ≡ t′ ⋯ weakenᵣ _ ↑* Γ × t′ ≼ u
≼-wk⁻¹ Γ {u = f # u} (≼-# x) with ≼-wk⁻¹ Γ x
... | t′ , eq , t′≼ = t′ , eq , ≼-# t′≼
≼-wk⁻¹ Γ {u = u ∷ us} (here x) with ≼-wk⁻¹ Γ x
... | t′ , eq , t′≼ = t′ , eq , here t′≼
≼-wk⁻¹ Γ {u = u ∷ us} (there x) with ≼-wk⁻¹ Γ x
... | t′ , eq , t′≼ = t′ , eq , there t′≼
≼-wk⁻¹ Γ {u = μ u} (≼-μ x) with ≼-wk⁻¹ (tm ∷ Γ) x
... | t′ , refl , t′≼ = t′ ⋯ ⦅ μ u ⦆ , sym (dist-↑-⦅⦆-⋯ t′ (μ u) (weakenᵣ _ ↑* Γ)) , ≼-μ t′≼
≼-wk⁻¹ Γ refl = _ , refl , refl

≼-wk/⋯-≼ : {t : tm ∷ Γ ⊢ s} {t₀ : Γ ⊢ tm} → t ≼ u ⋯ weakenᵣ _ → t ⋯ ⦅ t₀ ⦆ ≼ u
≼-wk/⋯-≼ {t₀ = t₀} x
  with t′ , refl , x′ ← ≼-wk⁻¹ [] x
  = subst (_≼ _) (sym (wk-cancels-⦅⦆-⋯ t′ t₀)) x′

≼-⋯⁻¹-` : ∀ Γ {t : Γ ++ Γ′ ⊢ s} {u : Γ′ ⊢ tm} (α : tm ∈ (Γ ++ tm ∷ Γ′)) →
  t ≼ α & ⦅ u ⦆ ↑* Γ →
  t ≼ u ⋯ weaken* ⦃ Kᵣ ⦄ Γ ⊎ ∃ λ t′ → t ≡ t′ ⋯ ⦅ u ⦆ ↑* Γ × t′ ≼ ` α
≼-⋯⁻¹-` [] (here  refl) x    = inj₁ (subst (_ ≼_) (sym (⋯-id _)) x)
≼-⋯⁻¹-` [] (there α)    refl = inj₂ (_ , refl , refl)
≼-⋯⁻¹-` (_ ∷ Γ) (here refl) refl = inj₂ (_ , refl , refl)
≼-⋯⁻¹-` (_ ∷ Γ) {u = u} (there α) x
  with t′ , t-eq , x′ ← ≼-wk⁻¹ [] {u = α & ⦅ _ ⦆ ↑* Γ} x
  with ≼-⋯⁻¹-` Γ α x′
... | inj₁ t′≼ = inj₁ (subst₂ _≼_ (sym t-eq) (fusion u (weaken* Γ) (weakenᵣ _)) (≼-⋯ t′≼ (weakenᵣ _)))
... | inj₂ (_ , refl , refl) = inj₂ (_ , t-eq , refl)

≼-⋯⁻¹ : ∀ Γ {t : Γ ++ Γ′ ⊢ s} {u : Γ′ ⊢ tm} (v : Γ ++ tm ∷ Γ′ ⊢ s′) →
  t ≼ v ⋯ ⦅ u ⦆ ↑* Γ →
  t ≼ u ⋯ weaken* ⦃ Kᵣ ⦄ Γ ⊎ ∃ λ t′ → t ≡ t′ ⋯ ⦅ u ⦆ ↑* Γ × t′ ≼ v
≼-⋯⁻¹ Γ (` α) x = ≼-⋯⁻¹-` Γ α x
≼-⋯⁻¹ Γ (v ∷ vs) (here x) with ≼-⋯⁻¹ Γ v x
... | inj₁ t≼u = inj₁ t≼u
... | inj₂ (t′ , eq , t′≼) = inj₂ (t′ , eq , here t′≼)
≼-⋯⁻¹ Γ (v ∷ vs) (there x) with ≼-⋯⁻¹ Γ vs x
... | inj₁ t≼u = inj₁ t≼u
... | inj₂ (t′ , eq , t′≼) = inj₂ (t′ , eq , there t′≼)
≼-⋯⁻¹ Γ (f # vs) (≼-# x) with ≼-⋯⁻¹ Γ vs x
... | inj₁ t≼u = inj₁ t≼u
... | inj₂ (t′ , eq , t′≼) = inj₂ (t′ , eq , ≼-# t′≼)
≼-⋯⁻¹ Γ {u = u} (μ v) (≼-μ x) with ≼-⋯⁻¹ (tm ∷ Γ) v x
... | inj₁ t≼u = inj₁ $ ≼-wk/⋯-≼ $ subst (_ ≼_) (sym (fusion u (weaken* Γ) (weakenᵣ _))) t≼u
... | inj₂ (t′ , refl , t′≼) = inj₂ (t′ ⋯ ⦅ μ v ⦆ , sym (dist-↑-⦅⦆-⋯ t′ (μ v) (⦅ _ ⦆ ↑* Γ)) , ≼-μ t′≼)
≼-⋯⁻¹ Γ v refl = inj₂ (_ , refl , refl)

⊑-to-≼ : t ⊑ u → t ≼ u
⊑-to-≼ refl      = refl
⊑-to-≼ (⊑-#   x) = ≼-#   (⊑-to-≼ x)
⊑-to-≼ (here  x) = here  (⊑-to-≼ x)
⊑-to-≼ (there x) = there (⊑-to-≼ x)
⊑-to-≼ {t = t} (⊑-μ {u} x)
  with ≼-⋯⁻¹ [] u (⊑-to-≼ x)
... | inj₁ x′ = subst (t ≼_) (⋯-id (μ u)) x′
... | inj₂ (t′ , refl , x′) = ≼-μ x′

count  : Γ ⊢ s → ℕ
count′ : Γ ⊢ s → ℕ

count t = suc (count′ t)

count′ ⋆        = 0
count′ (⋆* n)   = 0
count′ (` α)    = 0
count′ (μ t)    = count t
count′ (f # ts) = count ts
count′ []       = 0
count′ (t ∷ ts) = count t + count ts

≼-index : t ≼ u → 𝔽 (count u)
≼-index refl      = zero
≼-index (≼-μ   x) = suc (≼-index x)
≼-index (≼-#   x) = suc (≼-index x)
≼-index (here  x) = suc (≼-index x ↑ˡ _)
≼-index (there {u = u} x) = suc (count u ↑ʳ ≼-index x)

≼-index-injective : (x : t₁ ≼ u) (y : t₂ ≼ u) → ≼-index x ≡ ≼-index y → t₁ ≡ t₂
≼-index-injective refl refl eq = refl
≼-index-injective (≼-μ x) (≼-μ y) eq = cong (_⋯ ⦅ _ ⦆) (≼-index-injective x y (Fin.suc-injective eq))
≼-index-injective (≼-# x) (≼-# y) eq = ≼-index-injective x y (Fin.suc-injective eq)
≼-index-injective (here x) (here y) eq =
  ≼-index-injective x y (↑ˡ-injective _ (≼-index x) (≼-index y) (Fin.suc-injective eq))
≼-index-injective (there {u = u} x) (there y) eq =
  ≼-index-injective x y (↑ʳ-injective (suc (count u)) (≼-index x) (≼-index y) eq)
≼-index-injective (here x) (there y) eq = ⊥-elim (Fin.↑ˡ≢↑ʳ eq)
≼-index-injective (there x) (here y) eq = ⊥-elim (Fin.↑ˡ≢↑ʳ (sym eq))