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

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

open import Data.List.Membership.Propositional

import GenSim.Simulation.Universe
import GenSim.Simulation.Countdown

module Relation where
  open SimulationAlphabet 𝒜 public
  open Syn RA public
  open FixedScope Γ public
  open GenSim.Simulation.Universe 𝒜 Γ public

open Relation

infix 4 _⊢_∼?_ _⊢[_]_∼?_

_⊢_∼?_ : List 𝒰 → {t u : Tm} → ⊢ t → ⊢ u → Set
_⊢_∼?_ Θ {t} {u} x y = Θ ⊢ x ∼ y ⊎ t ≁ u

_⊢[_]_∼?_ : List 𝒰 → Vec Pol n → Γ ⊢ ts ∶ ⋆* n → Γ ⊢ us ∶ ⋆* n → Set
_⊢[_]_∼?_ {ts = ts} {us = us} Θ V xs ys =
  Θ ⊢[ V ] xs ∼* ys ⊎ ts ≁[ V ] us

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

  private variable
    η : List Hyp

  ∈-eq? : ∀ {a} {A : Set a} {x : A} {xs : List A} → DecidableEquality (x ∈ xs)
  ∈-eq? (here px)   (there y)   = no λ()
  ∈-eq? (there x)   (here py)   = no λ()
  ∈-eq? (here refl) (here refl) = yes refl
  ∈-eq? (there x)   (there y)   = map′ (cong there) L.Any.there-injective (∈-eq? x y)

  run : (C : η ⊕ n) {x : ⊢ t} {y : ⊢ u}
    → Subterm t
    → Subterm u
    → ⟨ η ⟩* ⊢ x ∼? y

  run* : (C : η ⊕ n) (V : Vec Pol k) (xs : Γ ⊢ ts ∶ ⋆* k) (ys : Γ ⊢ us ∶ ⋆* k)
    → Subterm ts
    → Subterm us
    → ⟨ η ⟩* ⊢[ V ] xs ∼? ys

  runUnfold : (C : η ⊕ n) {x : ⊢ t} {y : ⊢ u}
    → Subterm t
    → Subterm u
    → ∀ {d₁} → .(μPrefix t ≡ d₁)
    → ∀ {d₂} → .(μPrefix u ≡ d₂)
    → ⟨ η ⟩* ⊢ x ∼? y

  runHnf : (C : η ⊕ n) {x : ⊢ t} {y : ⊢ u}
    → Subterm t
    → Subterm u
    → .(μPrefix t ≡ 0)
    → .(μPrefix u ≡ 0)
    → ⟨ η ⟩* ⊢ x ∼? y

  run C t⊑ u⊑ = runUnfold C t⊑ u⊑ refl refl

  runUnfold C t⊑ u⊑ {0} eq₁ {0} eq₂ = runHnf C t⊑ u⊑ eq₁ eq₂
  runUnfold C {x} {⊢μ y c} t⊑ u⊑ {0} eq₁ {suc d₂} eq₂
    with runUnfold C t⊑ (subterm-μ u⊑) eq₁ (μPrefix-unfold y c ■ suc⁻¹ eq₂)
  ... | inj₁ x∼unfold·y = inj₁ (∼-μ₂ y c x∼unfold·y)
  ... | inj₂ mis = inj₂ (≁-μ₂ mis)
  runUnfold C {⊢μ x c} {y} t⊑ u⊑ {suc d₁} eq₁ eq₂
    with runUnfold C (subterm-μ t⊑) u⊑ (μPrefix-unfold x c ■ suc⁻¹ eq₁) eq₂
  ... | inj₁ unfold·x∼y = inj₁ (∼-μ₁ x c unfold·x∼y)
  ... | inj₂ mis = inj₂ (≁-μ₁ mis)

  runHnf C x@{⊢# {f = f₁} xs} y@{⊢# {f = f₂} ys} t⊑ u⊑ hnfₜ hnfᵤ
    with 𝓑? (these f₁ f₂)
  ... | yes B = inj₁ (∼-□ (#-# B))
  ... | no ¬B
    with eq? f₁ f₂
  ... | no f≢ = inj₂ (here (#-∦ ¬B f≢))
  ... | yes refl
    using H ← mkHyp (x ; y) t⊑ u⊑
    with lookupOrInsert C H
  ... | inj₁ H∈ = inj₁ (∼-# (inj₁ (⟨∈⟩⇒∈ H H∈)))
  ... | inj₂ (_ , refl , C′)
    with run* C′ (𝓥 f₁) _ _ (subterm-# t⊑) (subterm-# u⊑)
  ... | inj₁ xs∼ys = inj₁ (∼-# (inj₂ xs∼ys))
  ... | inj₂ mis = inj₂ (≁-# ¬B mis)
  runHnf {η = η} C {⊢` α} {⊢` β} t⊑ u⊑ hnfₜ hnfᵤ
    with ∈-eq? α β
  ... | yes refl = inj₁ (∼-□ `-`)
  ... | no  α≢β  = inj₂ (here (`-≢ α≢β))
  runHnf C {⊢# {f} _} {⊢` _} t⊑ u⊑ hnfₜ hnfᵤ
    with 𝓑? (this f)
  ... | yes B = inj₁ (∼-□ (#-` B))
  ... | no ¬B = inj₂ (here (#-` ¬B))
  runHnf C {⊢` _} {⊢# {f} _} t⊑ u⊑ hnfₜ hnfᵤ
    with 𝓑? (that f)
  ... | yes B = inj₁ (∼-□ (`-# B))
  ... | no ¬B = inj₂ (here (`-# ¬B))

  run* C [] [] [] ts⊑ us⊑ = inj₁ []
  run* C (v ∷ V) (x ∷ xs) (y ∷ ys) ts⊑ us⊑
    with run* C V xs ys (subterm-∷ ts⊑ .proj₂) (subterm-∷ us⊑ .proj₂)
  ... | inj₂ mis = inj₂ (there mis)
  ... | inj₁ xs∼ys
    with v
  ... | Pol.⊕ = Sum.map (_∷ xs∼ys) here⁺ $
                run C {x} {y} (subterm-∷ ts⊑ .proj₁) (subterm-∷ us⊑ .proj₁)
  ... | Pol.⊝ = Sum.map (_∷ xs∼ys) here⁻ $
                run C {y} {x} (subterm-∷ us⊑ .proj₁) (subterm-∷ ts⊑ .proj₁)

∼-dec : (x : ⊢ t) (y : ⊢ u) → [] ⊢ x ∼ y ⊎ t ≁ u
∼-dec x y = run Countdown.empty subterm₁ subterm₂
  where
  open Core (x ; y)
  open Countdown