import Spa.Language.Base
import Spa.Language.Semantics
import Spa.Language.Graphs
import Spa.Language.Traces
import Spa.Language.Properties
import Mathlib.Data.Finset.Sort
import Mathlib.Data.String.Basic

namespace Spa

structure Program where
  rootStmt : Stmt

namespace Program

variable (p : Program)

def graph : Graph := Graph.wrap (buildCfg p.rootStmt)

abbrev State : Type := p.graph.Index

def initialState : p.State := (buildCfg p.rootStmt).wrapInput

def finalState : p.State := (buildCfg p.rootStmt).wrapOutput

theorem trace {ρ : Env} (h : EvalStmt [] p.rootStmt ρ) :
    Trace p.graph p.initialState p.finalState [] ρ := by
  obtain ⟨i₁, h₁, i₂, h₂, tr⟩ := EndToEndTrace.wrap (buildCfg_sufficient h)
  rw [Graph.wrap_inputs, List.mem_singleton] at h₁
  rw [Graph.wrap_outputs, List.mem_singleton] at h₂
  subst h₁; subst h₂
  exact tr

def vars : List String := p.rootStmt.vars.sort (· ≤ ·)

theorem vars_nodup : p.vars.Nodup := Finset.sort_nodup _ _

def states : List p.State := p.graph.indices

theorem states_complete (s : p.State) : s ∈ p.states := p.graph.mem_indices s

theorem states_nodup : p.states.Nodup := p.graph.nodup_indices

def code (st : p.State) : List BasicStmt := p.graph.nodes st

def incoming (s : p.State) : List p.State := p.graph.predecessors s

theorem incoming_initialState_eq_nil : p.incoming p.initialState = [] :=
  Graph.wrap_predecessors_eq_nil (buildCfg p.rootStmt) p.initialState
    (by rw [Graph.wrap_inputs]; exact List.mem_singleton_self _)

theorem mem_incoming_of_edge {s₁ s₂ : p.State}
    (h : (s₁, s₂) ∈ p.graph.edges) : s₁ ∈ p.incoming s₂ :=
  p.graph.mem_predecessors_of_edge h

end Program

end Spa
