-------------------------------------------------------------------------------
-- 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 hiding (⊕; ⊝)

import GenSim.Syntax as Syn
import GenSim.Simulation.Countdown

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

open import Data.List.Relation.Binary.Subset.Propositional.Properties as Subset using (⊆-refl)

open SimulationAlphabet 𝒜
open Syn RA
open FixedScope Γ

open import GenSim.Equivalence RA
open import GenSim.Simulation.Universe 𝒜 Γ

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

module Respects-≃ (𝒰⁰ : 𝒰) where
  open module Countdown = GenSim.Simulation.Countdown 𝒜 𝒰⁰

  private variable
    η : List Hyp

  go :
    (C : η ⊕ n) →
    {x₁ : ⊢ t₁} {x₂ : ⊢ t₂} {y₁ : ⊢ u₁} {y₂ : ⊢ u₂} →
    Replay Θ →
    ∀ {dˣ} .(d-eqˣ : μPrefix t₂ ≡ dˣ) →
    (∀ h → ⟦ x₁ ⟧ h ≡ grow x₂ d-eqˣ h) →
    ∀ {dʸ} .(d-eqʸ : μPrefix u₂ ≡ dʸ) →
    (∀ h → ⟦ y₁ ⟧ h ≡ grow y₂ d-eqʸ h) →
    Subterm t₂ →
    Subterm u₂ →
    Θ ⊢ x₁ ∼ y₁ →
    ⟨ η ⟩* ⊢ x₂ ∼ y₂

  go* :
    ∀ {ts₁ ts₂ us₁ us₂} →
    (C : η ⊕ n) →
    {xs₁ : Γ ⊢ ts₁ ∶ ⋆* k} {ys₁ : Γ ⊢ us₁ ∶ ⋆* k} →
    Replay Θ →
    {xs₂ : Γ ⊢ ts₂ ∶ ⋆* k} {ys₂ : Γ ⊢ us₂ ∶ ⋆* k} →
    xs₁ ≃ xs₂ →
    ys₁ ≃ ys₂ →
    Subterm ts₂ →
    Subterm us₂ →
    Θ  ⊢[ V ] xs₁ ∼* ys₁ →
    ⟨ η ⟩* ⊢[ V ] xs₂ ∼* ys₂

  go C Θ {0} d-eqˣ eqˣ {0} d-eqʸ eqʸ x⊑ y⊑ x∼y with eqˣ 1 | eqʸ 1
  go C {x₁} {x₂} {y₁} {y₂} Θ d-eqˣ eqˣ d-eqʸ eqʸ x⊑ y⊑ (∼-□ t∥u) | eqˣ∣₁ | eqʸ∣₁
    = ∼-□ (∥-resp-≃ x₁ x₂ y₁ y₂ d-eqˣ eqˣ∣₁ d-eqʸ eqʸ∣₁ t∥u)
  go C {_} x₂@{⊢# _} {_} y₂@{⊢# _} Θ d-eqˣ eqˣ d-eqʸ eqʸ x⊑ y⊑ (∼-# ∈⊎∼) | refl | refl
    using H′ ← mkHyp (x₂ ; y₂) x⊑ y⊑
    with lookupOrInsert C H′
  ... | inj₁ H′∈ = ∼-# (inj₁ (⟨∈⟩⇒∈ H′ H′∈))
  ... | inj₂ (_ , refl , C′)
    with ∼-# xs∼ys ← Θ-lookup Θ ∈⊎∼
    = ∼-# (inj₂ (go* C′ (Θ-cons xs∼ys Θ) (≃⁻¹-# eqˣ) (≃⁻¹-# eqʸ) (subterm-# x⊑) (subterm-# y⊑) xs∼ys))
  go C {_} {x₂} {_} {_} Θ d-eqˣ eqˣ d-eqʸ eqʸ x⊑ y⊑ (∼-μ₁ x c x∼y) | eqˣ∣₁ | eqʸ∣₁
    = go C Θ refl
         (≃-sym (≃-μ x c) ≃■ λ- (ext-grow≡ λ h → eqˣ _ ■ cast-grow-μPrefix x₂ d-eqˣ refl))
         d-eqʸ eqʸ x⊑ y⊑ x∼y
  go C {_} {_} {_} {y₂} Θ d-eqˣ eqˣ d-eqʸ eqʸ x⊑ y⊑ (∼-μ₂ y c x∼y) | eqˣ∣₁ | eqʸ∣₁
    = go C Θ d-eqˣ eqˣ refl
         (≃-sym (≃-μ y c) ≃■ λ- (ext-grow≡ λ h → eqʸ _ ■ cast-grow-μPrefix y₂ d-eqʸ refl))
         x⊑ y⊑ x∼y
  go C {_} {_} {_} {⊢μ y₂ c} Θ {0} d-eqˣ eqˣ {suc _} d-eqʸ eqʸ x⊑ y⊑ x∼y
    = ∼-μ₂ y₂ c $ go C Θ d-eqˣ eqˣ
        (μPrefix-unfold y₂ c ■ suc⁻¹ d-eqʸ)
        (λ h → eqʸ h ■ ext-grow≡ λ h → refl)
        x⊑ (subterm-μ y⊑) x∼y
  go C {_} {⊢μ x₂ c} {_} {_} Θ {suc _} d-eqˣ eqˣ d-eqʸ eqʸ x⊑ y⊑ x∼y
    = ∼-μ₁ x₂ c $ go C Θ
        (μPrefix-unfold x₂ c ■ suc⁻¹ d-eqˣ)
        (λ h → eqˣ h ■ ext-grow≡ λ h → refl)
        d-eqʸ eqʸ (subterm-μ x⊑) y⊑ x∼y

  go* C Θ {[]} {[]} eqˣ eqʸ ts⊑ us⊑ [] = []
  go* C Θ {_ ∷ _} {_ ∷ _} eqˣ eqʸ ts⊑ us⊑ xs∼ys⁰@(_ ∷ xs∼ys)
    using eqx₁ , eqx₂ ← ≃⁻¹-∷ eqˣ
    using eqy₁ , eqy₂ ← ≃⁻¹-∷ eqʸ
    using t⊑ , ts′⊑ ← subterm-∷ ts⊑
    using u⊑ , us′⊑ ← subterm-∷ us⊑
    using xs∼ys′ ← go* C Θ eqx₂ eqy₂ ts′⊑ us′⊑ xs∼ys
    with xs∼ys⁰
  ... | x∼y ∷⁺ _ = go C Θ refl eqx₁ refl eqy₁ t⊑ u⊑ x∼y ∷ xs∼ys′
  ... | y∼x ∷⁻ _ = go C Θ refl eqy₁ refl eqx₁ u⊑ t⊑ y∼x ∷ xs∼ys′

module _ {x : Γ ⊢ t₁ ∶ ⋆} {y : Γ ⊢ u₁ ∶ ⋆} {x′ : Γ ⊢ t₂ ∶ ⋆} {y′ : Γ ⊢ u₂ ∶ ⋆} where

  open import GenSim.Simulation.Decide 𝒜 Γ

  ∼-respects-≃ : Replay Θ → x ≃ x′ → y ≃ y′ →  Θ ⊢ x ∼ y → Θ′ ⊢ x′ ∼ y′
  ∼-respects-≃ Θ eqˣ eqʸ x∼y =
    let open Respects-≃ (x′ ; y′) in
    ∼-wk (λ()) $ go Countdown.empty Θ
                    refl eqˣ refl eqʸ
                    Countdown.subterm₁
                    Countdown.subterm₂
                    x∼y

∼*-respects-≃ :
  {xs₁ : Γ ⊢ ts₁ ∶ ⋆* _} {ys₁ : Γ ⊢ us₁ ∶ ⋆* _} →
  {xs₂ : Γ ⊢ ts₂ ∶ ⋆* _} {ys₂ : Γ ⊢ us₂ ∶ ⋆* _} →
  Replay Θ →
  xs₁ ≃ xs₂ → ys₁ ≃ ys₂ →
  Θ  ⊢[ V ] xs₁ ∼* ys₁ →
  Θ′ ⊢[ V ] xs₂ ∼* ys₂
∼*-respects-≃ {xs₂ = []}    {[]}    Θ eqˣ eqʸ []             = []
∼*-respects-≃ {xs₂ = _ ∷ _} {_ ∷ _} Θ eqˣ eqʸ (x∼y ∷⁺ xs∼ys) =
  ∼-respects-≃ Θ (≃⁻¹-∷ eqˣ .proj₁) (≃⁻¹-∷ eqʸ .proj₁) x∼y
    ∷⁺ ∼*-respects-≃ Θ (≃⁻¹-∷ eqˣ .proj₂) (≃⁻¹-∷ eqʸ .proj₂) xs∼ys
∼*-respects-≃ {xs₂ = _ ∷ _} {_ ∷ _} Θ eqˣ eqʸ (y∼x ∷⁻ xs∼ys) =
  ∼-respects-≃ Θ (≃⁻¹-∷ eqʸ .proj₁) (≃⁻¹-∷ eqˣ .proj₁) y∼x
    ∷⁻ ∼*-respects-≃ Θ (≃⁻¹-∷ eqˣ .proj₂) (≃⁻¹-∷ eqʸ .proj₂) xs∼ys

∼-rebase : Replay Θ → Θ ⊢ x ∼ y → Θ′ ⊢ x ∼ y
∼-rebase Θ = ∼-respects-≃ Θ ≃-refl ≃-refl

∼*-rebase : {xs : ⊢* ts} {ys : ⊢* us} → Replay Θ → Θ ⊢[ V ] xs ∼* ys → Θ′ ⊢[ V ] xs ∼* ys
∼*-rebase Θ = ∼*-respects-≃ Θ ≃-refl ≃-refl


module Reflexive {t⁰} (x⁰ : ⊢ t⁰) where
  open module Countdown = GenSim.Simulation.Countdown 𝒜 (x⁰ ; x⁰)

  private variable
    η : List Hyp

  go  : η ⊕ n → {d : ℕ} {x : ⊢ t} → .(μPrefix t ≡ d) → Subterm t → ⟨ η ⟩* ⊢ x ∼ x
  go* : η ⊕ n → {xs : Γ ⊢ ts ∶ ⋆* k} → Subterm ts → ⟨ η ⟩* ⊢[ V ] xs ∼* xs

  go C {0} {⊢` ⊢α} eq t⊑ = ∼-□ `-`
  go C {0} x@{⊢# {f} xs} eq t⊑
    with 𝓑? (these f f)
  ... | yes B = ∼-□ (#-# B)
  ... | no ¬B
    using H ← mkHyp (x ; x) t⊑ t⊑
    with lookupOrInsert C H
  ... | inj₁ H∈ = ∼-# (inj₁ (⟨∈⟩⇒∈ H H∈))
  ... | inj₂ (_ , refl , C′) = ∼-# (inj₂ (go* C′ (subterm-# t⊑)))
  go C {suc d} {⊢μ x c} eq t⊑ = ∼-μ₁ x c (∼-μ₂ x c (go C {d} (μPrefix-unfold x c ■ suc⁻¹ eq) (subterm-μ t⊑)))

  go* C {[]} ts⊑ = []
  go* {V = Pol.⊕ ∷ V} C {x ∷ xs} ts⊑ = go C refl (subterm-∷ ts⊑ .proj₁) ∷ go* C (subterm-∷ ts⊑ .proj₂)
  go* {V = Pol.⊝ ∷ V} C {x ∷ xs} ts⊑ = go C refl (subterm-∷ ts⊑ .proj₁) ∷ go* C (subterm-∷ ts⊑ .proj₂)

∼-refl : Θ ⊢ x ∼ x
∼-refl {x = x} =
  let open Reflexive x in
  ∼-wk (λ()) (go Countdown.empty refl Countdown.subterm₁)

∼*-refl : {xs : ⊢* ts} → Θ ⊢[ V ] xs ∼* xs
∼*-refl {xs = []} = []
∼*-refl {V = Pol.⊕ ∷ V} {xs = x ∷ xs} = ∼-refl ∷ ∼*-refl
∼*-refl {V = Pol.⊝ ∷ V} {xs = x ∷ xs} = ∼-refl ∷ ∼*-refl

∼-reflexive : x ≃ y →  Θ ⊢ x ∼ y
∼-reflexive eq = ∼-respects-≃ {Θ = []} [] ≃-refl eq ∼-refl

module _ (trans-B : BaseTrans) where
  open BaseTrans trans-B

  module Transitive (𝒰⁰ : 𝒰) where
    open module Countdown = GenSim.Simulation.Countdown 𝒜 𝒰⁰

    private variable
      η : List Hyp

    go :
      (C : η ⊕ n) →
      {x : ⊢ e₁} {y : ⊢ e₂} {z : ⊢ e₃} →
      Replay Θ →
      ∀ {d₁₂} → .(μPrefix e₁ + μPrefix e₂ ≡ d₁₂) →
      ∀ {d₃} → .(μPrefix e₃ ≡ d₃) →
      Subterm e₁ →
      Subterm e₃ →
      Θ ⊢ x ∼ y →
      Θ ⊢ y ∼ z →
      ⟨ η ⟩* ⊢ x ∼ z

    go* :
      (C : η ⊕ n) →
      {xs : Γ ⊢ ts₁ ∶ ⋆* k} {ys : Γ ⊢ ts₂ ∶ ⋆* k} {zs : Γ ⊢ ts₃ ∶ ⋆* k} →
      Replay Θ →
      Subterm ts₁ →
      Subterm ts₃ →
      Θ ⊢[ V ] xs ∼* ys →
      Θ ⊢[ V ] ys ∼* zs →
      ⟨ η ⟩* ⊢[ V ] xs ∼* zs

    go C Θ {0} eq₁₂ {0} eq₃ x⊑ z⊑ (∼-□ ∥₁₂)     (∼-□ ∥₂₃)     = ∼-□ (∥-trans ∥₁₂ ∥₂₃)
    go C Θ {0} eq₁₂ {0} eq₃ x⊑ z⊑ (∼-□ (#-# B)) (∼-# _)     = ∼-□ (#-# B)
    go C Θ {0} eq₁₂ {0} eq₃ x⊑ z⊑ (∼-□ (`-# B)) (∼-# _)     = ∼-□ (`-# B)
    go C Θ {0} eq₁₂ {0} eq₃ x⊑ z⊑ (∼-# ∈⊎∼₁) (∼-□ (#-# B)) = ∼-□ (#-# B)
    go C Θ {0} eq₁₂ {0} eq₃ x⊑ z⊑ (∼-# ∈⊎∼₁) (∼-□ (#-` B)) = ∼-□ (#-` B)
    go C {x} {y} {z} Θ {0} eq₁₂ {0} eq₃ x⊑ z⊑ (∼-# ∈⊎∼₁) (∼-# ∈⊎∼₂)
      using H ← mkHyp (x ; z) x⊑ z⊑
      with lookupOrInsert C H
    ... | inj₁ H∈ = ∼-# (inj₁ (⟨∈⟩⇒∈ H H∈))
    ... | inj₂ (_ , refl , C′)
      with ∼-# ∼₁ ← Θ-lookup Θ ∈⊎∼₁
      with ∼-# ∼₂ ← Θ-lookup Θ ∈⊎∼₂
      = ∼-# (inj₂ (go* C′ (∼⁺-wk (there ∘′ there) (∼-# ∼₂)
                            ∷ ∼⁺-wk (there ∘′ there) (∼-# ∼₁)
                            ∷ L.All.map (∼⁺-wk (there ∘′ there)) Θ)
                          (subterm-# x⊑)
                          (subterm-# z⊑)
                          (∼*-wk there ∼₁)
                          (∼*-wk (Subset.∷⁺ʳ _ there) ∼₂)))
    go C Θ {0} eq₁₂ {0} eq₃ x⊑ z⊑ (∼-μ₂ y c ∼₁) ∼₂
      = ⊥-elim-irr (Nat.0≢1+n (sym eq₁₂ ■ +-suc _ _))
    go C {z = ⊢μ z c} Θ {0} eq₁₂ {suc _} eq₃ x⊑ z⊑ ∼₁ (∼-μ₂ y c ∼₂)
      = ∼-μ₂ y c (go C Θ eq₁₂ (μPrefix-unfold y c ■ suc⁻¹ eq₃) x⊑ (subterm-μ z⊑) ∼₁ ∼₂)
    go C {z = ⊢μ z c} Θ {0} eq₁₂ {suc _} eq₃ x⊑ z⊑ ∼₁ (∼-μ₁ x c₁ ∼₂)
      = ⊥-elim-irr (Nat.0≢1+n (sym eq₁₂ ■ +-suc _ _))
    go {e₂ = e₂} C Θ {suc d} eq₁₂ eq₃ x⊑ z⊑ (∼-μ₁ x c ∼₁) ∼₂
      = ∼-μ₁ x c (go C Θ {d} (cong (_+ μPrefix e₂) (μPrefix-unfold x c) ■ suc⁻¹ eq₁₂) eq₃ (subterm-μ x⊑) z⊑ ∼₁ ∼₂)
    go {e₁ = e₁} C Θ {suc d} eq₁₂ eq₃ x⊑ z⊑ (∼-μ₂ y c₁ ∼₁) (∼-μ₁ y c₂ ∼₂)
      = go C Θ {d} (cong (μPrefix e₁ +_) (μPrefix-unfold y c₁) ■ suc⁻¹ (sym (+-suc (μPrefix e₁) _) ■ eq₁₂)) eq₃ x⊑ z⊑ ∼₁ ∼₂
    go {e₁ = e₁} C Θ {suc d} eq₁₂ eq₃ x⊑ z⊑ (∼-μ₂ y c ∼₁) ∼₂@(∼-μ₂ _ _ _)
      = go C Θ {d} (cong (μPrefix e₁ +_) (μPrefix-unfold y c) ■ suc⁻¹ (sym (+-suc (μPrefix e₁) _) ■ eq₁₂))
           eq₃ x⊑ z⊑ ∼₁ (∼-respects-≃ Θ (≃-μ y c) ≃-refl ∼₂)
    go C Θ {suc _} eq₁₂ eq₃ x⊑ z⊑ (∼-□ ∥₁₂) ∼₂
      = ⊥-elim-irr (Nat.0≢1+n (sym (∥-μPrefix≡0 ∥₁₂) ■ eq₁₂))

    go* C Θ x⊑ z⊑ [] [] = []
    go* C Θ x⊑ z⊑ ∼₁@(_ ∷ xs∼ys) ∼₂@(_ ∷ ys∼zs)
      using x⊑′ , xs⊑ ← subterm-∷ x⊑
      using z⊑′ , zs⊑ ← subterm-∷ z⊑
      using xs∼zs ← go* C Θ xs⊑ zs⊑ xs∼ys ys∼zs
      with ∼₁ | ∼₂
    ... | x∼y ∷⁺ _ | y∼z ∷⁺ _ = go C Θ refl refl x⊑′ z⊑′ x∼y y∼z ∷ xs∼zs
    ... | y∼x ∷⁻ _ | z∼y ∷⁻ _ = go C Θ refl refl z⊑′ x⊑′ z∼y y∼x ∷ xs∼zs

module _ {x : ⊢ t₁} {y : ⊢ t₂} {z : ⊢ t₃} where

  ∼-trans : BaseTrans → Replay Θ₁ → Replay Θ₂ → Θ₁ ⊢ x ∼ y → Θ₂ ⊢ y ∼ z → Θ ⊢ x ∼ z
  ∼-trans {Θ₁ = Θ₁} {Θ₂ = Θ₂} transB ∀Θ₁ ∀Θ₂ x∼y y∼z =
    let open Transitive transB (x ; z) in
    ∼-wk (λ()) $ go Countdown.empty
                    (L.All.++⁺ (L.All.map (∼⁺-wk (Subset.xs⊆xs++ys Θ₁ Θ₂)) ∀Θ₁)
                               (L.All.map (∼⁺-wk (Subset.xs⊆ys++xs Θ₂ Θ₁)) ∀Θ₂))
                    refl refl
                    Countdown.subterm₁
                    Countdown.subterm₂
                    (∼-wk (Subset.xs⊆xs++ys Θ₁ Θ₂) x∼y)
                    (∼-wk (Subset.xs⊆ys++xs Θ₂ Θ₁) y∼z)

∼-sym  : BaseSym → Θ ⊢ x ∼ y → Θ-swap Θ ⊢ y ∼ x
∼*-sym : BaseSym → Θ ⊢[ V ] xs ∼* ys → Θ-swap Θ ⊢[ V ] ys ∼* xs

∼-sym symB (∼-□ t∥u) = ∼-□ (∥-sym symB t∥u)
∼-sym symB (∼-# (inj₁ H∈)) = ∼-# (inj₁ (L.Any.gmap (λ{ refl → refl }) H∈))
∼-sym symB (∼-# (inj₂ xs∼ys)) = ∼-# (inj₂ (∼*-sym symB xs∼ys))
∼-sym symB (∼-μ₁ x c x∼y) = ∼-μ₂ x c (∼-sym symB x∼y)
∼-sym symB (∼-μ₂ y c x∼y) = ∼-μ₁ y c (∼-sym symB x∼y)

∼*-sym symB [] = []
∼*-sym symB (x∼y ∷⁺ xs∼ys) = ∼-sym symB x∼y ∷ ∼*-sym symB xs∼ys
∼*-sym symB (y∼x ∷⁻ xs∼ys) = ∼-sym symB y∼x ∷ ∼*-sym symB xs∼ys

∼-contradiction  : {x : ⊢ t}     {y : ⊢ u}     → Replay Θ → Θ ⊢       x ∼    y   → t   ≁       u   → ⊥
∼*-contradiction : {xs : ⊢* ts}  {ys : ⊢* us}  → Replay Θ → Θ ⊢[ V ]  xs ∼*  ys  → ts  ≁[ V ]  us  → ⊥

∼-contradiction Θ (∼-□ t∥u)      (here t∦u)         = ∥-contradiction t∥u t∦u
∼-contradiction Θ (∼-# ∈⊎∼)      (here (#-∦ ¬B f≢)) = f≢ refl
∼-contradiction Θ (∼-□ (#-# B))  (≁-# ¬B xs≁ys)     = ¬B B
∼-contradiction Θ (∼-# ∈⊎∼)      (≁-# ¬B xs≁ys)     with ∼-# xs∼ys ← Θ-lookup Θ ∈⊎∼
                                                    = ∼*-contradiction (Θ-cons xs∼ys Θ) xs∼ys xs≁ys
∼-contradiction Θ (∼-μ₁ x c x∼y) (≁-μ₁ x≁y)         = ∼-contradiction Θ x∼y x≁y
∼-contradiction Θ (∼-μ₂ y c x∼y) (≁-μ₂ x≁y)         = ∼-contradiction Θ x∼y x≁y
∼-contradiction {x = ⊢μ x c} Θ x∼y@(∼-μ₂ _ _ _) (≁-μ₁ x≁y) = ∼-contradiction Θ (∼-respects-≃ Θ (≃-μ _ _) ≃-refl x∼y) x≁y
∼-contradiction {y = ⊢μ y c} Θ x∼y@(∼-μ₁ _ _ _) (≁-μ₂ x≁y) = ∼-contradiction Θ (∼-respects-≃ Θ ≃-refl (≃-μ _ _) x∼y) x≁y

∼*-contradiction Θ (x∼y ∷ xs∼ys) (here⁺ x≁y)   = ∼-contradiction  Θ x∼y   x≁y
∼*-contradiction Θ (y∼x ∷ xs∼ys) (here⁻ x≁y)   = ∼-contradiction  Θ y∼x   x≁y
∼*-contradiction Θ (_   ∷ xs∼ys) (there xs≁ys) = ∼*-contradiction Θ xs∼ys xs≁ys