-------------------------------------------------------------------------------
-- 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

import GenSim.Syntax as Syn

module GenSim.Simulation.Universe (𝒜 : SimulationAlphabet) (Γ : Syn.Context RA⟨ 𝒜 ⟩) where

open import Data.List.Membership.Propositional
open import Data.List.Membership.Propositional.Properties
open import Data.List.Relation.Binary.Subset.Propositional

import Relation.Binary.Construct.On as On
import Data.List.Relation.Binary.Subset.Propositional.Properties as Subset

open SimulationAlphabet 𝒜

open import GenSim.Prelude
open import GenSim.Equivalence RA

module _ where
  open Syn RA
  open FixedScope Γ hiding (t; u)

  -- Universe elements are pairs of terms plus contractiveness (ie. "well-typed") proofs.
  record 𝒰 : Set where
    constructor _;_
    field  {t}  : Tm; x  : ⊢ t
           {u}  : Tm; y  : ⊢ u

    terms : Tm × Tm
    terms = t , u

    swap : 𝒰
    swap = record { x = y; y = x }

  open 𝒰

  -- Equivalence on universe elements defers to the raw terms.
  _≈_ : Rel 𝒰 _
  _≈_ = _≡_ on terms

  ≈-to-≡ : ∀ {H₁ H₂} → H₁ ≈ H₂ → H₁ ≡ H₂
  ≈-to-≡ {H₁} {H₂} refl
    rewrite ⊢-irr (H₁ .x) (H₂ .x) | ⊢-irr (H₁ .y) (H₂ .y)
    = refl

  ≡-to-≈ : ∀ {x y} → x ≡ y → x ≈ y
  ≡-to-≈ refl = refl

  -- Universe element equivalence is decidable.
  _≟𝒰_ : DecidableEquality 𝒰
  x ≟𝒰 y = map′ ≈-to-≡ ≡-to-≈ (Π.≡-dec _≟_ _≟_ (terms x) (terms y))

  𝒰-setoid    = ≡.setoid 𝒰
  𝒰-decSetoid = ≡.decSetoid _≟𝒰_

open Syn RA
open FixedScope Γ

variable
  v : Pol
  V : Vec Pol n
  Θ Θ₁ Θ₂ Θ′ : List 𝒰

private variable
  x : ⊢ t
  y : ⊢ u
  xs : ⊢* ts
  ys : ⊢* us

infix 4 _⊢_∼_ _⊢_∼⁺_ _∥_
infix 4 _⊢[_]_∼_
infix 4 _⊢[_]_∼*_

BaseSym : Set
BaseSym = ∀ {x} → 𝓑 x → 𝓑 (Th.swap x)

data _∥_ : Tm → Tm → Set where
  `-`  :                       ` α      ∥ ` α
  #-`  : 𝓑 (this   f₁)      →  f₁ # ts  ∥ ` α
  `-#  : 𝓑 (that       f₂)  →  ` α      ∥ f₂ # us
  #-#  : 𝓑 (these  f₁  f₂)  →  f₁ # ts  ∥ f₂ # us

∥-sym : BaseSym → Symmetric _∥_
∥-sym sym-B (#-# B) = #-# (sym-B B)
∥-sym sym-B (#-` B) = `-# (sym-B B)
∥-sym sym-B (`-# B) = #-` (sym-B B)
∥-sym sym-B `-`     = `-`

∥-μPrefix≡0 : t ∥ u → μPrefix t + μPrefix u ≡ 0
∥-μPrefix≡0 (#-# _) = refl
∥-μPrefix≡0 (#-` _) = refl
∥-μPrefix≡0 (`-# _) = refl
∥-μPrefix≡0 `-`     = refl

record BaseTrans : Set where
  field  trans-###   : 𝓑 (these  f₁ f₂)  → 𝓑 (these  f₂ f₃)  → 𝓑 (these  f₁ f₃)
         trans-##`   : 𝓑 (these  f₁ f₂)  → 𝓑 (this   f₂)     → 𝓑 (this   f₁)
         trans-`##   : 𝓑 (that   f₂)     → 𝓑 (these  f₂ f₃)  → 𝓑 (that   f₃)
         trans-#`#   : 𝓑 (this   f₁)     → 𝓑 (that   f₃)     → 𝓑 (these  f₁ f₃)
         ¬trans-`#`  : 𝓑 (that   f)      → 𝓑 (this   f)      → ⊥

  ∥-trans : t₁ ∥ t₂ → t₂ ∥ t₃ → t₁ ∥ t₃
  ∥-trans (#-# B) (#-# B′) = #-# (trans-### B B′)
  ∥-trans (#-# B) (#-` B′) = #-` (trans-##` B B′)
  ∥-trans (#-` B) (`-# B′) = #-# (trans-#`# B B′)
  ∥-trans (#-` B) `-`      = #-` B
  ∥-trans (`-# B) (#-# B′) = `-# (trans-`## B B′)
  ∥-trans (`-# B) (#-` B′) = ⊥-elim (¬trans-`#` B B′)
  ∥-trans `-`     B′       = B′

mutual

  data _⊢_∼_ Θ : ⊢ t → ⊢ u → Set where
    ∼-□  :  {x : ⊢ t} {y : ⊢ u} →
      t ∥ u → Θ ⊢ x ∼ y

    ∼-#  :  {xs : ⊢* ts} {ys : ⊢* us}         →
      let H = ⊢# {f = f} xs ; ⊢# {f = f} ys   in
      H ∈ Θ ⊎ H ∷ Θ ⊢[ 𝓥 f ] xs ∼* ys         →
      Θ ⊢ ⊢# {f = f} xs ∼ ⊢# {f = f} ys

    ∼-μ₁  :
      (x  : tm ∷ Γ ⊢ t  ∶ ⋆) (c : 𝓒₀ t)  {y  : ⊢ u}  →
      Θ ⊢ ⊢unfold x c ∼ y → Θ ⊢ ⊢μ x c ∼ y

    ∼-μ₂  :
      (y  : tm ∷ Γ ⊢ u  ∶ ⋆) (c : 𝓒₀ u)  {x  : ⊢ t}  →
      Θ ⊢ x ∼ ⊢unfold y c → Θ ⊢ x ∼ ⊢μ y c

  data _⊢[_]_∼*_ Θ : Vec Pol n → Γ ⊢ ts ∶ ⋆* n → Γ ⊢ us ∶ ⋆* n → Set where

    []   :  Θ ⊢[ V ] [] ∼* []

    _∷_  :  Θ ⊢[ v  ] x   ∼   y   →
            Θ ⊢[ V  ] xs  ∼*  ys  →
            Θ ⊢[ v ∷  V  ] x ∷  xs  ∼*  y ∷ ys

  _⊢[_]_∼_ : List 𝒰 → Pol → ⊢ t → ⊢ u → Set
  Θ ⊢[ ⊕  ] x ∼ y = Θ ⊢ x  ∼ y
  Θ ⊢[ ⊝  ] x ∼ y = Θ ⊢ y  ∼ x

infixr 5 _∷⁺_ _∷⁻_

pattern _∷⁺_  x∼y xs∼ys = _∷_ {v = ⊕}  x∼y xs∼ys
pattern _∷⁻_  y∼x xs∼ys = _∷_ {v = ⊝}  y∼x xs∼ys

data _⊢_∼⁺_ Θ : ⊢ t → ⊢ u → Set where
  ∼-# : {xs : ⊢* ts} {ys : ⊢* us}          →
    let H = ⊢# {f = f} xs ; ⊢# {f = f} ys  in
    H ∷ Θ ⊢[ 𝓥 f ] xs ∼* ys                →
    Θ ⊢ ⊢# {f = f} xs ∼⁺ ⊢# {f = f} ys

∼-wk  : {x : ⊢ t} {y : ⊢ u} → Θ ⊆ Θ′ → Θ ⊢ x ∼ y → Θ′ ⊢ x ∼ y
∼*-wk : {xs : Γ ⊢ ts ∶ ⋆* n} {ys : Γ ⊢ us ∶ ⋆* n} → Θ ⊆ Θ′ → Θ ⊢[ V ] xs ∼* ys → Θ′ ⊢[ V ] xs ∼* ys

∼-wk Θ⊆ (∼-□ t∥u)          = ∼-□ t∥u
∼-wk Θ⊆ (∼-# (inj₁ H∈))    = ∼-# (inj₁ (Θ⊆ H∈))
∼-wk Θ⊆ (∼-# (inj₂ xs∼ys)) = ∼-# (inj₂ (∼*-wk (Subset.∷⁺ʳ _ Θ⊆) xs∼ys))
∼-wk Θ⊆ (∼-μ₁ x c x∼y)     = ∼-μ₁ x c (∼-wk Θ⊆ x∼y)
∼-wk Θ⊆ (∼-μ₂ y c x∼y)     = ∼-μ₂ y c (∼-wk Θ⊆ x∼y)

∼*-wk Θ⊆ []             = []
∼*-wk Θ⊆ (x∼y ∷⁺ xs∼ys) = ∼-wk Θ⊆ x∼y ∷ ∼*-wk Θ⊆ xs∼ys
∼*-wk Θ⊆ (y∼x ∷⁻ xs∼ys) = ∼-wk Θ⊆ y∼x ∷ ∼*-wk Θ⊆ xs∼ys

∼⁺-wk  : {x : ⊢ t} {y : ⊢ u} → Θ ⊆ Θ′ → Θ ⊢ x ∼⁺ y → Θ′ ⊢ x ∼⁺ y
∼⁺-wk Θ⊆ (∼-# xs∼ys) = ∼-# (∼*-wk (Subset.∷⁺ʳ _ Θ⊆) xs∼ys)

Valid : ({t u : Tm} → ⊢ t → ⊢ u → Set) → 𝒰 → Set
Valid _∼_ (t ; u) = t ∼ u

Replay : List 𝒰 → Set
Replay Θ = All (Valid (Θ ⊢_∼⁺_)) Θ

Θ-swap : List 𝒰 → List 𝒰
Θ-swap = L.map 𝒰.swap

Θ-cons :
  let H = ⊢# {f = f} xs ; ⊢# {f = f} ys in
  H ∷ Θ ⊢[ 𝓥 f ] xs ∼* ys →
  Replay Θ →
  Replay (H ∷ Θ)
Θ-cons xs∼ys Θ = ∼-# (∼*-wk there xs∼ys) ∷ L.All.map (∼⁺-wk there) Θ

Θ-lookup :
  let H = ⊢# {f = f} xs ; ⊢# {f = f} ys in
  Replay Θ →
  H ∈ Θ ⊎ H ∷ Θ ⊢[ 𝓥 f ] xs ∼* ys →
  Θ ⊢ ⊢# {f = f} xs ∼⁺ ⊢# {f = f} ys
Θ-lookup Θ = [ L.All.lookup Θ , ∼-# ]′

∥-resp-≃ :
  (x₁ : ⊢ t₁) (x₂ : ⊢ t₂) →
  (y₁ : ⊢ u₁) (y₂ : ⊢ u₂) →
  .(d-eqˣ : μPrefix t₂ ≡ 0) → ⟦ x₁ ⟧ 1 ≡ grow x₂ d-eqˣ 1 →
  .(d-eqʸ : μPrefix u₂ ≡ 0) → ⟦ y₁ ⟧ 1 ≡ grow y₂ d-eqʸ 1 →
  t₁ ∥ u₁ → t₂ ∥ u₂
∥-resp-≃ (⊢# _) (⊢# _) (⊢# _) (⊢# _) d-eqˣ refl d-eqʸ refl (#-# B) = #-# B
∥-resp-≃ (⊢# _) (⊢# _) (⊢` _) (⊢` _) d-eqˣ refl d-eqʸ refl (#-` B) = #-` B
∥-resp-≃ (⊢` _) (⊢` _) (⊢# _) (⊢# _) d-eqˣ refl d-eqʸ refl (`-# B) = `-# B
∥-resp-≃ (⊢` _) (⊢` _) (⊢` _) (⊢` _) d-eqˣ refl d-eqʸ refl `-`     = `-`

infix 4 _∦_

data _∦_ : Tm → Tm → Set where
  #-`  :  ¬ 𝓑 (this  f)  → f   # ts  ∦ ` α
  `-#  :  ¬ 𝓑 (that  f)  → ` α       ∦ f   # us
  #-∦  :  ¬ 𝓑 (these f₁ f₂) → f₁ ≢ f₂
                         → f₁  # ts  ∦ f₂  # us
  `-≢  :  α₁ ≢ α₂        → ` α₁      ∦ ` α₂

∦-sym : BaseSym → Symmetric _∦_
∦-sym sym-B (#-` ¬B)     = `-# (¬B ∘ sym-B)
∦-sym sym-B (`-# ¬B)     = #-` (¬B ∘ sym-B)
∦-sym sym-B (#-∦ ¬B f≢)  = #-∦ (¬B ∘ sym-B) (f≢ ∘ sym)
∦-sym sym-B (`-≢ α≢)     = `-≢ (α≢ ∘ sym)

∦-irrefl : ¬ t ∦ t
∦-irrefl (#-∦ ¬B f≢)  = f≢ refl
∦-irrefl (`-≢ α≢)     = α≢ refl

∥-contradiction : t ∥ u → t ∦ u → ⊥
∥-contradiction (#-# B)  (#-∦ ¬B f≢)  = ¬B B
∥-contradiction (#-` B)  (#-` ¬B)     = ¬B B
∥-contradiction (`-# B)  (`-# ¬B)     = ¬B B
∥-contradiction `-`      (`-≢ α≢)     = α≢ refl


infix 4 _≁_ _≁[_]_

mutual
  data _≁_ : Tm → Tm → Set where
    here  :  t ∦ u            → t    ≁ u
    ≁-μ₁  :  unfold t ≁ u     → μ t  ≁ u
    ≁-μ₂  :  t ≁ unfold u     → t    ≁ μ u
    ≁-#   :  ¬ 𝓑 (these f f)  →
      ts ≁[ 𝓥 f ] us → f # ts ≁ f # us

  data _≁[_]_ : Tm* n → Vec Pol n → Tm* n → Set where
    here⁺  : t   ≁  u       → t ∷ ts ≁[ ⊕  ∷ V ] u ∷ us
    here⁻  : u   ≁  t       → t ∷ ts ≁[ ⊝  ∷ V ] u ∷ us
    there  : ts  ≁[ V ] us  → t ∷ ts ≁[ v  ∷ V ] u ∷ us

≁-respects-≃′ :
  ∀ {pˣ} .(eqˣ : μPrefix t₂ ≡ pˣ) →
  ∀ {pʸ} .(eqʸ : μPrefix u₂ ≡ pʸ) →
  {x₁ : ⊢ t₁} {y₁ : ⊢ u₁} →
  t₁ ≁ u₁ →
  {x₂ : ⊢ t₂} {y₂ : ⊢ u₂} →
  (∀ h → ⟦ x₁ ⟧ h ≡ grow x₂ eqˣ h) →
  (∀ h → ⟦ y₁ ⟧ h ≡ grow y₂ eqʸ h) →
  t₂ ≁ u₂

≁*-respects-≃ :
  ts₁ ≁[ V ] us₁ →
  {xs₁ : ⊢* ts₁} {ys₁ : ⊢* us₁} {xs₂ : ⊢* ts₂} {ys₂ : ⊢* us₂} →
  xs₁ ≃ xs₂ → ys₁ ≃ ys₂ → ts₂ ≁[ V ] us₂

≁-respects-≃′ {pˣ = 0} eqˣ {0} eqʸ (here t∦u) x≃ y≃ with x≃ 1 | y≃ 1
≁-respects-≃′ eqˣ eqʸ {⊢# _} {⊢` _} (here (#-` ¬B))    {⊢# _} {⊢` _} x≃ y≃ | refl | refl = here (#-` ¬B)
≁-respects-≃′ eqˣ eqʸ {⊢` _} {⊢# _} (here (`-# ¬B))    {⊢` _} {⊢# _} x≃ y≃ | refl | refl = here (`-# ¬B)
≁-respects-≃′ eqˣ eqʸ {⊢# _} {⊢# _} (here (#-∦ ¬B f≢)) {⊢# _} {⊢# _} x≃ y≃ | refl | refl = here (#-∦ ¬B f≢)
≁-respects-≃′ eqˣ eqʸ {⊢` _} {⊢` _} (here (`-≢ α≢))    {⊢` _} {⊢` _} x≃ y≃ | refl | refl = here (`-≢ α≢)
≁-respects-≃′ {pˣ = 0} eqˣ {0} eqʸ {x₁ = ⊢μ x c} (≁-μ₁ t≁u) x≃ y≃ =
  ≁-respects-≃′ eqˣ eqʸ t≁u (λ h → sym (≃-μ x c h) ■ x≃ h) y≃
≁-respects-≃′ {pˣ = 0} eqˣ {0} eqʸ {y₁ = ⊢μ y c} (≁-μ₂ t≁u) x≃ y≃ =
  ≁-respects-≃′ eqˣ eqʸ t≁u x≃ (λ h → sym (≃-μ y c h) ■ y≃ h)
≁-respects-≃′ {pˣ = 0} eqˣ {0} eqʸ {⊢# _} {⊢# _} (≁-# ¬B xs≁ys) {x₂} {y₂} x≃ y≃
  with ⊢# _ ← x₂ in eq-x₂ | refl ← x≃ 1
  with ⊢# _ ← y₂ in eq-y₂ | refl ← y≃ 1
  = ≁-# ¬B (≁*-respects-≃ xs≁ys (≃⁻¹-# (subst (_ ≃_) eq-x₂ x≃))
                                (≃⁻¹-# (subst (_ ≃_) eq-y₂ y≃)))
≁-respects-≃′ {pˣ = 0} eqˣ {suc pʸ} eqʸ t≁u {x₂} {⊢μ y₂ c} x≃ y≃ =
  ≁-μ₂ (≁-respects-≃′ eqˣ (μPrefix-unfold y₂ c ■ suc⁻¹ eqʸ) t≁u {x₂} {⊢unfold y₂ c}
                      x≃ (λ h → y≃ h ■ ext-grow≡ (λ h′ → cast-grow-μPrefix (⊢unfold y₂ c) _ _)))
≁-respects-≃′ {pˣ = suc pˣ} eqˣ {pʸ} eqʸ {x₁} {y₁} t≁u {⊢μ x₂ c} {y₂} x≃ y≃ =
  ≁-μ₁ (≁-respects-≃′ (μPrefix-unfold x₂ c ■ suc⁻¹ eqˣ) eqʸ t≁u {⊢unfold x₂ c} {y₂}
                      (λ h → x≃ h ■ ext-grow≡ (λ h′ → cast-grow-μPrefix (⊢unfold x₂ c) _ _)) y≃)

≁*-respects-≃ ts≁us xs≃ ys≃ with xs≃ 1 | ys≃ 1
≁*-respects-≃ (here⁺ t≁u)   {_ ∷ _} {_ ∷ _} {_ ∷ _} {_ ∷ _} xs≃ ys≃ | refl | refl
  = here⁺ (≁-respects-≃′ refl refl t≁u (≃⁻¹-∷ xs≃ .proj₁) (≃⁻¹-∷ ys≃ .proj₁))
≁*-respects-≃ (here⁻ u≁t)   {_ ∷ _} {_ ∷ _} {_ ∷ _} {_ ∷ _} xs≃ ys≃ | refl | refl
  = here⁻ (≁-respects-≃′ refl refl u≁t (≃⁻¹-∷ ys≃ .proj₁) (≃⁻¹-∷ xs≃ .proj₁))
≁*-respects-≃ (there ts≁us) {_ ∷ _} {_ ∷ _} {_ ∷ _} {_ ∷ _} xs≃ ys≃ | refl | refl
  = there (≁*-respects-≃ ts≁us (≃⁻¹-∷ xs≃ .proj₂) (≃⁻¹-∷ ys≃ .proj₂))

module _ {x₁ : ⊢ t₁} {y₁ : ⊢ u₁} {x₂ : ⊢ t₂} {y₂ : ⊢ u₂} where
  ≁-respects-≃ : t₁ ≁ u₁ → x₁ ≃ x₂ → y₁ ≃ y₂ → t₂ ≁ u₂
  ≁-respects-≃ t≁u x≃ y≃ = ≁-respects-≃′ refl refl t≁u x≃ y≃