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

module GenSim.Equivalence.Decide (RA : RankedAlphabet) where

open import Relation.Unary.Properties

private
  𝒜 : SimulationAlphabet
  𝒜 = record { RA = RA ; 𝓑? = ∅? ; 𝓥 = λ _ → V.replicate _ ⊕ }

open import GenSim.Equivalence RA public
open import GenSim.Simulation 𝒜

module _ {Γ : Context} where
  open FixedScope Γ

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

  ∼-to-≃  : Replay Θ → Θ ⊢ x ∼ y → x ≃ y

  ∼-to-≃* :
    Replay Θ →
    Θ ⊢[ V.replicate _ ⊕ ] xs ∼* ys →
    xs ≃ ys

  ∼-to-≃ Θ x∼y zero = refl
  ∼-to-≃ Θ (∼-# ∈⊎∼) (suc h)
    with ∼-# xs∼ys ← Θ-lookup Θ ∈⊎∼
    = cong (_ #_) (∼-to-≃* (Θ-cons xs∼ys Θ) xs∼ys h)
  ∼-to-≃ Θ (∼-μ₁ x c x∼y) (suc h) = ≃-μ x c (suc h) ■ ∼-to-≃ Θ x∼y (suc h)
  ∼-to-≃ Θ (∼-μ₂ y c x∼y) (suc h) = ∼-to-≃ Θ x∼y (suc h) ■ sym (≃-μ y c (suc h))
  ∼-to-≃ {x = ⊢` _} {y = ⊢` _} Θ (∼-□ `-`) (suc h) = refl

  ∼-to-≃* Θ [] = ≃-refl
  ∼-to-≃* Θ (x∼y ∷ xs∼ys) zero = refl
  ∼-to-≃* Θ (x∼y ∷ xs∼ys) (suc h) = cong₂ _∷_ (∼-to-≃ Θ x∼y h) (∼-to-≃* Θ xs∼ys h)

  infix 4 _≃?_

  _≃?_ : (x : ⊢ t) (y : ⊢ u) → Dec (x ≃ y)
  x ≃? y with ∼-dec x y
  ... | inj₁ x∼y = yes (∼-to-≃ [] x∼y)
  ... | inj₂ x≁y = no λ x≃y → ∼-contradiction [] (∼-reflexive x≃y) x≁y