{-# OPTIONS --cubical --safe #-}

module FormalTopology where

open import Basis
open import Poset

InteractionStr : (A : Type )  Type (ℓ-suc )
InteractionStr { = } A =
  Σ[ B  (A  Type ) ] Σ[ C  ({x : A}  B x  Type ) ]({x : A}  {y : B x}  C y  A)

InteractionSys : ( : Level)  Type (ℓ-suc )
InteractionSys  = Σ (Type ) InteractionStr

state       : InteractionSys   Type 
action      : (D : InteractionSys )  state D  Type 
reaction    : (D : InteractionSys )  {x : state D}  action D x  Type 
δ           : (D : InteractionSys ) {x : state D} {y : action D x}
             reaction D y  state D

state    (A , _ , _ , _) = A
action   (_ , B , _ , _) = B
reaction (_ , _ , C , _) = C
δ        (_ , _ , _ , d) = d

hasMono : (P : Poset ℓ₀ ℓ₁)  InteractionStr  P ∣ₚ  Type (ℓ-max ℓ₀ ℓ₁)
hasMono P i =
  (a : state IS) (b : action IS a) (c : reaction IS b)  [ δ IS c ⊑[ P ] a ]
  where
    IS : InteractionSys _
    IS =  P ∣ₚ , i

module _ (P : Poset ℓ₀ ℓ₁) (ℐ-str : InteractionStr  P ∣ₚ) where
   : InteractionSys ℓ₀
   = ( P ∣ₚ , ℐ-str)

  hasSimulation : Type (ℓ-max ℓ₀ ℓ₁)
  hasSimulation =
    (a′ a :  P ∣ₚ)  [ a′ ⊑[ P ] a ] 
      (b : action  a)  Σ[ b′  action  a′ ]
        ((c′ : reaction  b′)  Σ[ c  reaction  b ] [ δ  c′ ⊑[ P ] δ  c ])

FormalTopology : (ℓ₀ ℓ₁ : Level)  Type (ℓ-max (ℓ-suc ℓ₀) (ℓ-suc ℓ₁))
FormalTopology ℓ₀ ℓ₁ =
  Σ[ P  Poset ℓ₀ ℓ₁ ] Σ[   InteractionStr  P ∣ₚ ] hasMono P  × hasSimulation P 

pos : FormalTopology ℓ₀ ℓ₁  Poset ℓ₀ ℓ₁
pos (P , _) = P

IS : ( : FormalTopology ℓ₀ ℓ₁)  InteractionStr  pos  ∣ₚ
IS (_ , is , _) = is

stage : FormalTopology ℓ₀ ℓ₁  Type ℓ₀
stage (P , ℐ-str , _) = state ( P ∣ₚ , ℐ-str)

exp : ( : FormalTopology ℓ₀ ℓ₁)  stage   Type ℓ₀
exp (P , ℐ-str , _) = action ( P ∣ₚ , ℐ-str)

outcome : ( : FormalTopology ℓ₀ ℓ₁)  {a : stage }  exp  a  Type ℓ₀
outcome (P , ℐ-str , _) = reaction ( P ∣ₚ , ℐ-str)

next : ( : FormalTopology ℓ₀ ℓ₁) {a : stage } {b : exp  a}  outcome  b  stage 
next (P , ℐ-str , _) = δ ( P ∣ₚ , ℐ-str)

mono : ( : FormalTopology ℓ₀ ℓ₁)  hasMono (pos ) (IS )
mono (_ , _ , φ , _) = φ

sim : ( : FormalTopology ℓ₀ ℓ₁)  hasSimulation (pos ) (IS )
sim (_ , _ , _ , φ) = φ