FormalConjecturesForMathlib/Computability/TuringMachine/Notation.lean
Explanation legend Lean4 Domain Explanation Language (L4DEL)
L4DEL explanations are a separate layer. They do not change the Lean source. Intuition is explanatory prose; numbered items are a reading path; sources link the wording back to evidence.
- L4DEL
- Lean4 Domain Explanation Language
Credit: The upstream authors and contributors retain credit for the project and mathematics. lean4-explainer only renders their pinned source and adds the separately disclosed explanation layer.
Project sources: Pinned project documentation, Formal Conjectures paper, arXiv v1
19 declarations, 0 contain sorry, 0 declared as axioms
Turing Machine Parser
This module provides a parser for defining a Turing machine from a simple string description.
The main entry point is the turing_machine% elaborator, which takes a string representing the
machine's transitions and constructs a term of type Machine (Fin m) StateType where StateType
is an inductive type generated on the fly.
Encoding format
The machine's transitions are encoded as a single string, with each state's transitions
separated by an underscore (_).
For each state, a 3m-character substring defines the behavior:
- The first 3 characters
"ABC"describe the action when the head reads0:A: The symbol to write (0, 1, ..., m-1).B: The direction to move the head (LorR).C: The new state (AthroughZ).
- The next 3 characters
"DEF"describe the action when the head reads1using the same format. - The next 3 characters
"GHI"describe the action when the head reads2using the same format. - So on...
The character Z is reserved for the halting state. The string "---" can be used to represent
a transition to the halting state without writing or moving.
Example of a tape: 1RA0LB_0LA---
There are more examples at the end of this file.
Imports (3)
Imported by 1 file
- def
Alphabetline 58 source - def
Char.toDirSyntaxChar.toDirSyntax coutputs the syntax corresponding to the charactercifcisRorLand throws an error in other cases line 64 sourceSource documentation
Char.toDirSyntax coutputs the syntax corresponding to the charactercifcisRorLand throws an error in other cases. This is used when parsing the "direction" component of turing machine string representations.private def Char.toDirSyntax : Char → TermElabM Term | 'R' => `(Dir.right) | 'L' => `(Dir.left) | char => throwError "Invalid direction {char}." /-- `Char.toBinarySyntax c` outputs the syntax corresponding to the character `c` if `c` is in `0, ..., 9` and throws an error in other cases. This is used when parsing the "symbol" component of turing machine string representations. -/Mentions: c
- def
Char.toNumeralSyntaxChar.toBinarySyntax coutputs the syntax corresponding to the charactercifcis in0, ..., 9and throws an error in other cases line 72 sourceSource documentation
Char.toBinarySyntax coutputs the syntax corresponding to the charactercifcis in0, ..., 9and throws an error in other cases. This is used when parsing the "symbol" component of turing machine string representations.private def Char.toNumeralSyntax (c : Char) : TermElabM Term := do …
Mentions: c
- def
Char.toStateSyntaxChar.toStateSyntax c stateNameoutputs the syntax of the constructor corresponding tocifcis a capital letter (i.e. something betweenAandZ.) line 83 sourceSource documentation
Char.toStateSyntax c stateNameoutputs the syntax of the constructor corresponding tocifcis a capital letter (i.e. something betweenAandZ.). For example,Awould output the syntaxstateName.AwherestateNameis the name of the type used to index sets. This is used when parsing the "state" component of turing machine string representations.private def Char.toStateSyntax (c : Char) (stateName : Name) (numStates : Nat) : TermElabM Term := do …
Mentions: c
- def
Nat.toStateSyntaxNat.toStateSyntax n stateNameoutputs the syntax of then-th constructor of the type used to index states line 95 sourceSource documentation
Nat.toStateSyntax n stateNameoutputs the syntax of then-th constructor of the type used to index states.private def Nat.toStateSyntax (n : Nat) (stateName : Name) : TermElabM Term := do …
- def
String.toStmtSyntaxString.toStmtSyntax s stateNameparses a component of a Turing machine string representation and outputs the syntax of the corresponding(state, statement) : State × Stmtpair line 102 sourceSource documentation
String.toStmtSyntax s stateNameparses a component of a Turing machine string representation and outputs the syntax of the corresponding(state, statement) : State × Stmtpair.private def String.toStmtSyntax (s : String) (stateName : Name) (numStates : Nat) : TermElabM Term := do …
- def
String.nextnline 116 source - def
mkMachineMatchAltExprDocumented declaration: mkMachineMatchAltExpr line 125 sourceSource documentation
Take as input a list of strings and return an array of
matchAltExprsyntaxes mapping(state, symbol)pairs to actions of a Turing Machine, given by a term of typeOption (State n).def mkMachineMatchAltExpr (L : List String) (stateName : Name) (numSymbols numStates: Nat) : TermElabM (Array (TSyntax ``matchAltExpr)) := do … - def
Nat.toStateNameThe name of the state type withnconstructorsA, B, ...line 147 sourceSource documentation
The name of the state type with
nconstructorsA, B, ....private def Nat.toStateName (n : Nat) : Name := .mkSimple s!"State{n}" … - def
mkStateTypeDocumented declaration: mkStateType line 160 sourceSource documentation
mkStateType nadds to the environment an inductive type withnstates namesState{n}with constructors the symbolsA, B, ..., together with the instanceInhabited (State{n}).Both declarations have to be
public.liftCommandElabMruns them in a fresh scope that does not carry the surrounding module's visibility, so without it they are declared private and end up named_private.<module>.0.State{n}. The lookup below then fails withUnknown constant State{n}, and the type would in any case be private while appearing in the type of the term this elaborator returns.L4DEL explanation
mkStateType nadds to the environment an inductive type withnstates namesState{n}with constructors the symbolsA, B, ..., together with the instanceInhabited (State{n}). Both declarations have to bepublic.liftCommandElabMruns them in a fresh scope that does not carry the surrounding module's visibility, so without it they are declared private and end up named_private..0.State{n}. The lookup below then fails withUnknown constant State{n}, and the type would in any case be private while appearing in the type of the term this elaborator returns.Sources and attribution: Pinned Lean source (1646ca16afd6)
def mkStateType (n : ℕ) (stateName : Name) : TermElabM Unit := do …
- def
parseTuringparseTuring tmStringDescriptionoutputs theExprcorresponding to the Turing Machine described by the stringtmStringDescriptionline 189 sourceSource documentation
parseTuring tmStringDescriptionoutputs theExprcorresponding to the Turing Machine described by the stringtmStringDescription.To do so, it
- Runs
mkStateTypeto add the state type to the environment - Defines the corresponding Turing Machine as a function using the
matchsyntax.
Warning: if a state type (i.e. something with the name
State1, State2, ...) already exists in the environment then this will be reused without checking that this is the right inductive type - such checks are left to the user.def parseTuring (descr : String) : TermElabM Expr := do …
- Runs
- example
(example at line 231)line 231 source - example
(example at line 234)line 234 source - example
(example at line 242)line 242 source - example
(example at line 250)line 250 source - example
(example at line 288)line 288 sourceSource comment
A machine with no transitions halts after zero steps.example : IsHalting (turing_machine% "------") := …
- example
(example at line 292)line 292 sourceSource comment
A machine that moves to a state without transitions halts after one step.example : IsHalting (turing_machine% "0RB0RB_------") := …
- example
(example at line 295)line 295 source - example
(example at line 302)line 302 sourceSource comment
A machine whose transitions never enter the halting state does not halt.example : ¬ IsHalting (turing_machine% "0RB0LB_0RA0LB") := by …