math / data /lean /OAI /Combinatorics /Automata /Model.lean
introvoyz045's picture
Migrated from GitHub
4a56cd6 verified
Raw History Blame Contribute Delete
4.65 kB
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