TM
Turing Machine (TM) class in pykleene
states: set[str]
inputAlphabet: set[str]
tapeAlphabet: set[str]
startState: str
transitions: dict[tuple[str, str], tuple[str, str, str]]
leftEndMarker: str
blankSymbol: str
acceptState: str
rejectState: str
CONSTRUCTOR
Initialize a new Turing Machine with specified parameters.
Parameters
states
: Set of states in the TMinputAlphabet
: Set of input symbolstapeAlphabet
: Set of tape symbolsstartState
: Initial state of the TMtransitions
: Mapping of state-symbol pairs to next state, write symbol, and directionleftEndMarker
: Marker for the left end of the tapeblankSymbol
: Symbol used to represent empty tape cellsacceptState
: State indicating successful computationrejectState
: State indicating failed computation
Return Value
Creates a new TM object with specified configuration
loadFromJSONDict
Load TM configuration from a JSON dictionary.
# Example
{
"TM_ADD": {
"states": ["q0", "q1", "q2", "t", "r"],
"inputAlphabet": ["0", "1"],
"tapeAlphabet": ["0", "1", "#", "♭", "⊢"],
"startState": "q0",
"transitions": [
["q0", "⊢", "q0", "⊢", "R"],
["q0", "1", "q0", "1", "R"],
["q0", "#", "q0", "1", "R"],
["q0", "♭", "q1", "♭", "L"],
["q1", "1", "q2", "♭", "L"],
["q2", "1", "q2", "1", "L"],
["q2", "⊢", "t", "⊢", "S"]
],
"leftEndMarker": "⊢",
"blankSymbol": "♭",
"acceptState": "t",
"rejectState": "r"
}
}
Parameters
jsonDict
: Dictionary containing TM configuration
Return Value
Populates the TM object with configuration from the dictionary
isValid
Validate the TM configuration.
Parameters
None
Return Value
Boolean indicating whether the TM configuration is valid
accepts
Determine if a given string is accepted by the TM.
Parameters
inputString
: Input string to be processedverbose
: Boolean to enable detailed computation tracing
Return Value
Tuple of (acceptance boolean, final tape contents)
image
Generate a graphical representation of the TM.
Parameters
dir
: Directory to save the imagesave
: Boolean to determine if image should be savedmonochrome
: Boolean to generate a black and white visualization
Return Value
Graphviz Digraph object representing the Turing Machine