import Mathlib namespace OAI namespace OneWayLiveness @[ext] structure BRel (H : Type*) where holds : H → H → Prop namespace BRel variable {H : Type*} instance : One (BRel H) := ⟨⟨Eq⟩⟩ instance : Mul (BRel H) := ⟨fun a b => ⟨fun x z => ∃ y, a.holds x y ∧ b.holds y z⟩⟩ instance : Zero (BRel H) := ⟨⟨fun _ _ => False⟩⟩ @[simp] theorem one_holds (x y : H) : (1 : BRel H).holds x y ↔ x = y := Iff.rfl @[simp] theorem mul_holds (a b : BRel H) (x z : H) : (a * b).holds x z ↔ ∃ y, a.holds x y ∧ b.holds y z := Iff.rfl @[simp] theorem zero_holds (x y : H) : ¬ (0 : BRel H).holds x y := id instance : Monoid (BRel H) where mul_assoc a b c := by ext x z change (∃ y, (∃ w, a.holds x w ∧ b.holds w y) ∧ c.holds y z) ↔ ∃ w, a.holds x w ∧ ∃ y, b.holds w y ∧ c.holds y z aesop one_mul a := by ext x z; change (∃ y, x = y ∧ a.holds y z) ↔ a.holds x z; simp mul_one a := by ext x z; change (∃ y, a.holds x y ∧ y = z) ↔ a.holds x z; simp def restrictedId (F : Set H) : BRel H := ⟨fun x y => x = y ∧ x ∈ F⟩ def live (w : List (BRel H)) : Prop := ∃ x y, w.prod.holds x y instance [Finite H] : Finite (BRel H) := Finite.of_injective BRel.holds (fun _ _ h => BRel.ext h) end BRel abbrev Alphabet (h : ℕ) := BRel (Fin h) def OWL (h : ℕ) : Set (List (Alphabet h)) := {w | BRel.live w} inductive Symbol (Alpha : Type*) where | left : Symbol Alpha | letter : Alpha → Symbol Alpha | right : Symbol Alpha inductive Move where | left | stay | right deriving DecidableEq def Move.Rel : Move → ℕ → ℕ → Prop | .left, i, j => j + 1 = i | .stay, i, j => j = i | .right, i, j => j = i + 1 /-- The finite word including both endmarkers. -/ def scanned {Alpha : Type*} (w : List Alpha) (i : Fin (w.length + 2)) : Symbol Alpha := if h0 : i.val = 0 then .left else if hi : i.val < w.length + 1 then .letter (w[i.val - 1]'(by omega)) else .right abbrev Configuration {Alpha : Type*} (w : List Alpha) (n : ℕ) := Fin (w.length + 2) × Fin n structure DMachine (Alpha : Type*) (s : ℕ) where initial : Fin s accepting : Set (Fin s) transition : Fin s → Symbol Alpha → Option (Fin s × Move) left_boundary : ∀ q q', transition q .left ≠ some (q', .left) right_boundary : ∀ q q', transition q .right ≠ some (q', .right) structure NMachine (Alpha : Type*) (n : ℕ) where initial : Fin n accepting : Set (Fin n) transition : Fin n → Symbol Alpha → Set (Fin n × Move) left_boundary : ∀ q q', (q', .left) ∉ transition q .left right_boundary : ∀ q q', (q', .right) ∉ transition q .right def DMachine.Step {Alpha : Type*} {s : ℕ} (M : DMachine Alpha s) (w : List Alpha) (c c' : Configuration w s) : Prop := ∃ d, M.transition c.2 (scanned w c.1) = some (c'.2, d) ∧ d.Rel c.1.val c'.1.val def NMachine.Step {Alpha : Type*} {n : ℕ} (M : NMachine Alpha n) (w : List Alpha) (c c' : Configuration w n) : Prop := ∃ d, (c'.2, d) ∈ M.transition c.2 (scanned w c.1) ∧ d.Rel c.1.val c'.1.val /-- The positive convention explicitly excludes a length-zero computation. -/ def FiniteRun {C : Type*} (positive : Bool) (step : C → C → Prop) (c c' : C) : Prop := if positive then Relation.TransGen step c c' else Relation.ReflTransGen step c c' def DMachine.Accepts {Alpha : Type*} {s : ℕ} (M : DMachine Alpha s) (positive : Bool) (w : List Alpha) : Prop := ∃ c, FiniteRun positive (M.Step w) (⟨0, by omega⟩, M.initial) c ∧ c.2 ∈ M.accepting def NMachine.Accepts {Alpha : Type*} {n : ℕ} (M : NMachine Alpha n) (positive : Bool) (w : List Alpha) : Prop := ∃ c, FiniteRun positive (M.Step w) (⟨0, by omega⟩, M.initial) c ∧ c.2 ∈ M.accepting def NMachine.NoLeft {Alpha : Type*} {n : ℕ} (M : NMachine Alpha n) : Prop := ∀ q a q', (q', Move.left) ∉ M.transition q a def NMachine.Recognizes {Alpha : Type*} {n : ℕ} (M : NMachine Alpha n) (positive : Bool) (L : Set (List Alpha)) : Prop := ∀ w, M.Accepts positive w ↔ w ∈ L def DMachine.Recognizes {Alpha : Type*} {s : ℕ} (M : DMachine Alpha s) (positive : Bool) (L : Set (List Alpha)) : Prop := ∀ w, M.Accepts positive w ↔ w ∈ L /-- An alphabet-independent polynomial bound for deterministic simulation. -/ def PolynomialSimulation (positive : Bool) : Prop := ∃ C c : ℝ, 0 < C ∧ 0 < c ∧ ∀ (Alpha : Type) (_ : Fintype Alpha) (n : ℕ) (N : NMachine Alpha n), ∃ (s : ℕ) (D : DMachine Alpha s), (s : ℝ) ≤ C * (n : ℝ) ^ c ∧ ∀ w, D.Accepts positive w ↔ N.Accepts positive w end OneWayLiveness end OAI