-------------------------------------------------------------------------------
-- 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.Universe as Sim

module GenSim.Simulation.Countdown (π’œ : SimulationAlphabet) {Ξ“ : Syn.Context RA⟨ π’œ ⟩} (𝒰⁰ : Sim.𝒰 π’œ Ξ“) where

open import Data.List.Membership.Propositional using (_∈_)
import Relation.Binary.Construct.On as On

open Nat using (_*_)

open SimulationAlphabet π’œ
open Syn RA
open Sim π’œ Ξ“ hiding (_β‰ˆ_)

import GenSim.Syntax.Subterm
module Subterm = GenSim.Syntax.Subterm RA

open Subterm using (_βŠ‘_; βŠ‘-trans; βŠ‘-reflexive) public
open Subterm using (βŠ‘-to-β‰Ό; β‰Ό-index; β‰Ό-index-injective)
open _βŠ‘_ public


module _ where
  open FixedScope Ξ“

  Subterm : Ξ“ ⊒ s β†’ Set
  Subterm t = t βŠ‘ proj₁ (𝒰.terms 𝒰⁰) ⊎ t βŠ‘ projβ‚‚ (𝒰.terms 𝒰⁰)

  -- The Subterm predicate is anti-monotone.
  βŠ‘-anti-mono : t βŠ‘ u β†’ Subterm u β†’ Subterm t
  βŠ‘-anti-mono tβŠ‘u = Sum.map (βŠ‘-trans tβŠ‘u) (βŠ‘-trans tβŠ‘u)

  subterm₁ : Subterm (𝒰⁰ .𝒰.t)
  subterm₁ = inj₁ refl

  subtermβ‚‚ : Subterm (𝒰⁰ .𝒰.u)
  subtermβ‚‚ = injβ‚‚ refl

  subterm-# : Subterm (f # ts) β†’ Subterm ts
  subterm-# = βŠ‘-anti-mono (βŠ‘-# refl)

  subterm-∷ : Subterm (t ∷ ts) β†’ Subterm t Γ— Subterm ts
  subterm-∷ x = βŠ‘-anti-mono (here refl) x , βŠ‘-anti-mono (there refl) x

  subterm-ΞΌ : Subterm (ΞΌ t) β†’ Subterm (unfold t)
  subterm-ΞΌ = βŠ‘-anti-mono (βŠ‘-ΞΌ refl)


record Hyp : Set where
  constructor mkHyp
  field
    u  : 𝒰
    βŠ‘β‚ : Subterm (proj₁ (𝒰.terms u))
    βŠ‘β‚‚ : Subterm (projβ‚‚ (𝒰.terms u))

open Hyp public using () renaming (u to ⟨_⟩)

⟨_⟩* : List Hyp β†’ List 𝒰
⟨_⟩* = L.map ⟨_⟩

Hyp-decSetoid : DecSetoid _ _
Hyp-decSetoid = On.decSetoid 𝒰-decSetoid ⟨_⟩

Hyp-setoid : Setoid _ _
Hyp-setoid = DecSetoid.setoid Hyp-decSetoid

open import Data.List.Countdown Hyp-decSetoid as Countdown using (_βŠ•_; lookupOrInsert) public
open import Data.List.Membership.Setoid Hyp-setoid renaming (_∈_ to _⟨∈⟩_; _βˆ‰_ to _βŸ¨βˆ‰βŸ©_)
open import Data.List.Membership.Setoid.Properties

open DecSetoid Hyp-decSetoid using (_β‰ˆ_)
open FixedScope Ξ“

βŸ¨βˆˆβŸ©β‡’βˆˆ : (H : Hyp) {Ξ· : List Hyp} β†’ H ⟨∈⟩ Ξ· β†’ ⟨ H ⟩ ∈ ⟨ Ξ· ⟩*
βŸ¨βˆˆβŸ©β‡’βˆˆ H H∈ = ∈-map⁺ Hyp-setoid 𝒰-setoid (Ξ» eq β†’ eq) {H} H∈

βˆˆβ‡’βŸ¨βˆˆβŸ© : (H : Hyp) {Ξ· : List Hyp} β†’ ⟨ H ⟩ ∈ ⟨ Ξ· ⟩* β†’ H ⟨∈⟩ Ξ·
βˆˆβ‡’βŸ¨βˆˆβŸ© H H∈ with ∈-map⁻ Hyp-setoid 𝒰-setoid H∈
... | Hβ€² , Hβ€²βˆˆ , eq rewrite eq = Hβ€²βˆˆ

subterms : β„•
subterms = Subterm.count (proj₁ (𝒰.terms 𝒰⁰)) + Subterm.count (projβ‚‚ (𝒰.terms 𝒰⁰))

indexOfSubterm : Subterm t β†’ 𝔽 subterms
indexOfSubterm (inj₁ βŠ‘β‚) = β‰Ό-index (βŠ‘-to-β‰Ό βŠ‘β‚) ↑ˑ Subterm.count (projβ‚‚ (𝒰.terms 𝒰⁰))
indexOfSubterm (injβ‚‚ βŠ‘β‚‚) = Subterm.count (proj₁ (𝒰.terms 𝒰⁰)) ↑ʳ β‰Ό-index (βŠ‘-to-β‰Ό βŠ‘β‚‚)

indexOf : Hyp β†’ 𝔽 (subterms * subterms)
indexOf H = Fin.combine (indexOfSubterm (Hyp.βŠ‘β‚ H)) (indexOfSubterm (Hyp.βŠ‘β‚‚ H))

indexOfSubterm-injective : (x : Subterm t) (y : Subterm u) β†’
  indexOfSubterm x ≑ indexOfSubterm y β†’ t ≑ u
indexOfSubterm-injective (inj₁ x) (inj₁ y) eq =
  β‰Ό-index-injective (βŠ‘-to-β‰Ό x) (βŠ‘-to-β‰Ό y) $ Fin.↑ˑ-injective _ _ _ eq
indexOfSubterm-injective (injβ‚‚ x) (injβ‚‚ y) eq =
  β‰Ό-index-injective (βŠ‘-to-β‰Ό x) (βŠ‘-to-β‰Ό y) $ Fin.↑ʳ-injective _ _ _ eq
indexOfSubterm-injective (inj₁ x) (injβ‚‚ y) eq = βŠ₯-elim (↑ˑ≒↑ʳ eq)
indexOfSubterm-injective (injβ‚‚ x) (inj₁ y) eq = βŠ₯-elim (↑ˑ≒↑ʳ (sym eq))

indexOf-injective : (H₁ Hβ‚‚ : Hyp) β†’ indexOf H₁ ≑ indexOf Hβ‚‚ β†’ H₁ β‰ˆ Hβ‚‚
indexOf-injective H₁ Hβ‚‚ eq =
  let eq₁ , eqβ‚‚ = Fin.combine-injective (indexOfSubterm (Hyp.βŠ‘β‚ H₁)) (indexOfSubterm (Hyp.βŠ‘β‚‚ H₁))
                                        (indexOfSubterm (Hyp.βŠ‘β‚ Hβ‚‚)) (indexOfSubterm (Hyp.βŠ‘β‚‚ Hβ‚‚))
                                        eq
  in
  β‰ˆ-to-≑ $ congβ‚‚ _,_
    (indexOfSubterm-injective (Hyp.βŠ‘β‚ H₁) (Hyp.βŠ‘β‚ Hβ‚‚) eq₁)
    (indexOfSubterm-injective (Hyp.βŠ‘β‚‚ H₁) (Hyp.βŠ‘β‚‚ Hβ‚‚) eqβ‚‚)

empty : [] βŠ• subterms * subterms
empty = record
  { kind = injβ‚‚ ∘ indexOf
  ; injective = Ξ» {H₁} {Hβ‚‚} eq₁ eqβ‚‚ β†’
      indexOf-injective H₁ Hβ‚‚
        (injβ‚‚-injective eq₁ β–  sym (injβ‚‚-injective eqβ‚‚))
  }