math / data /lean /OAI /Computability /Interspersed /Machines.lean
introvoyz045's picture
Migrated from GitHub
4a56cd6 verified
Raw History Blame Contribute Delete
2.34 kB
import Mathlib
namespace OAI
/-! Finite transition tables and two-sided machine executions. -/
noncomputable section
open scoped ContDiff
namespace PrefixFlows
inductive Move where
| stay | right | left
deriving DecidableEq, Repr
def Move.displacement : Move β†’ β„€
| .stay => 0
| .right => 1
| .left => -1
def Move.code : Move β†’ β„•
| .stay => 0
| .right => 1
| .left => 2
structure Instruction where
nextState : β„•
writeSymbol : β„•
motion : Move
deriving DecidableEq, Repr
structure Machine where
alphabetSize : β„•
blank : β„•
initialState : β„•
haltStates : List β„•
table : List (List (Option Instruction))
deriving DecidableEq, Repr
namespace Machine
def WellFormed (M : Machine) : Prop :=
0 < M.alphabetSize ∧ M.blank < M.alphabetSize ∧
M.initialState < M.table.length ∧
(βˆ€ q ∈ M.haltStates, q < M.table.length) ∧
(βˆ€ row ∈ M.table, row.length = M.alphabetSize ∧
βˆ€ instruction ∈ row, βˆ€ a, instruction = some a β†’
a.nextState < M.table.length ∧ a.writeSymbol < M.alphabetSize)
def ValidInput (M : Machine) (w : List β„•) : Prop :=
βˆ€ a ∈ w, a < M.alphabetSize
def code (M : Machine) : β„• :=
Encodable.encode (M.alphabetSize, M.blank, M.initialState, M.haltStates,
M.table.map (List.map (Option.map fun a =>
(a.nextState, a.writeSymbol, a.motion.code))))
def instruction (M : Machine) (q a : β„•) : Option Instruction :=
(M.table[q]?).bind fun row => (row[a]?).join
structure Configuration where
state : β„•
head : β„€
tape : β„€ β†’ β„•
def initial (M : Machine) (w : List β„•) : Configuration where
state := M.initialState
head := 0
tape := fun j => if 0 ≀ j then (w[j.toNat]?).getD M.blank else M.blank
def step (M : Machine) (c : Configuration) : Option Configuration :=
if c.state ∈ M.haltStates then none else
(M.instruction c.state (c.tape c.head)).map fun a =>
{ state := a.nextState
head := c.head + a.motion.displacement
tape := Function.update c.tape c.head a.writeSymbol }
def run (M : Machine) (w : List β„•) : β„• β†’ Option Configuration
| 0 => some (M.initial w)
| n + 1 => (M.run w n).bind M.step
def Halts (M : Machine) (w : List β„•) : Prop :=
βˆƒ n c, M.run w n = some c ∧ M.step c = none
end Machine
end PrefixFlows
end
end OAI