turing-sim 1.0.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- turing_sim-1.0.0/LICENSE +21 -0
- turing_sim-1.0.0/PKG-INFO +189 -0
- turing_sim-1.0.0/README.md +146 -0
- turing_sim-1.0.0/pyproject.toml +95 -0
- turing_sim-1.0.0/setup.cfg +4 -0
- turing_sim-1.0.0/src/turing/__init__.py +33 -0
- turing_sim-1.0.0/src/turing/__main__.py +6 -0
- turing_sim-1.0.0/src/turing/browser.py +53 -0
- turing_sim-1.0.0/src/turing/cli.py +70 -0
- turing_sim-1.0.0/src/turing/definition.py +188 -0
- turing_sim-1.0.0/src/turing/exceptions.py +22 -0
- turing_sim-1.0.0/src/turing/py.typed +0 -0
- turing_sim-1.0.0/src/turing/render.py +92 -0
- turing_sim-1.0.0/src/turing/state.py +19 -0
- turing_sim-1.0.0/src/turing/transition_function.py +8 -0
- turing_sim-1.0.0/src/turing/turing_machine.py +272 -0
- turing_sim-1.0.0/src/turing_sim.egg-info/PKG-INFO +189 -0
- turing_sim-1.0.0/src/turing_sim.egg-info/SOURCES.txt +29 -0
- turing_sim-1.0.0/src/turing_sim.egg-info/dependency_links.txt +1 -0
- turing_sim-1.0.0/src/turing_sim.egg-info/entry_points.txt +2 -0
- turing_sim-1.0.0/src/turing_sim.egg-info/requires.txt +11 -0
- turing_sim-1.0.0/src/turing_sim.egg-info/top_level.txt +1 -0
- turing_sim-1.0.0/tests/test_browser.py +66 -0
- turing_sim-1.0.0/tests/test_cli.py +90 -0
- turing_sim-1.0.0/tests/test_examples.py +81 -0
- turing_sim-1.0.0/tests/test_legacy_format.py +69 -0
- turing_sim-1.0.0/tests/test_nondeterminism.py +146 -0
- turing_sim-1.0.0/tests/test_render.py +59 -0
- turing_sim-1.0.0/tests/test_site.py +14 -0
- turing_sim-1.0.0/tests/test_turing_machine.py +142 -0
- turing_sim-1.0.0/tests/test_yaml_format.py +113 -0
turing_sim-1.0.0/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Franciszek Wozniak
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,189 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: turing-sim
|
|
3
|
+
Version: 1.0.0
|
|
4
|
+
Summary: Turing machine simulator with step-by-step tracing, YAML definitions and nondeterminism
|
|
5
|
+
Author: Franciszek Wozniak
|
|
6
|
+
License-Expression: MIT
|
|
7
|
+
Project-URL: Homepage, https://gitlab.com/lorax0/turingmachine
|
|
8
|
+
Project-URL: Source, https://gitlab.com/lorax0/turingmachine
|
|
9
|
+
Project-URL: Issues, https://gitlab.com/lorax0/turingmachine/-/issues
|
|
10
|
+
Project-URL: Documentation, https://lorax0.gitlab.io/turingmachine/api/
|
|
11
|
+
Project-URL: Changelog, https://gitlab.com/lorax0/turingmachine/-/blob/main/CHANGELOG.md
|
|
12
|
+
Keywords: turing-machine,automata,simulator,computability
|
|
13
|
+
Classifier: Development Status :: 5 - Production/Stable
|
|
14
|
+
Classifier: Environment :: Console
|
|
15
|
+
Classifier: Intended Audience :: Education
|
|
16
|
+
Classifier: Intended Audience :: Science/Research
|
|
17
|
+
Classifier: Programming Language :: Python :: 3
|
|
18
|
+
Classifier: Programming Language :: Python :: 3 :: Only
|
|
19
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
20
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
21
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
22
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
23
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
24
|
+
Classifier: Programming Language :: Python :: 3.15
|
|
25
|
+
Classifier: Operating System :: OS Independent
|
|
26
|
+
Classifier: Topic :: Education
|
|
27
|
+
Classifier: Topic :: Scientific/Engineering :: Mathematics
|
|
28
|
+
Classifier: Typing :: Typed
|
|
29
|
+
Requires-Python: >=3.10
|
|
30
|
+
Description-Content-Type: text/markdown
|
|
31
|
+
License-File: LICENSE
|
|
32
|
+
Requires-Dist: pyyaml>=6
|
|
33
|
+
Requires-Dist: rich>=13
|
|
34
|
+
Provides-Extra: dev
|
|
35
|
+
Requires-Dist: build; extra == "dev"
|
|
36
|
+
Requires-Dist: mypy; extra == "dev"
|
|
37
|
+
Requires-Dist: types-PyYAML; extra == "dev"
|
|
38
|
+
Requires-Dist: pytest; extra == "dev"
|
|
39
|
+
Requires-Dist: pytest-cov; extra == "dev"
|
|
40
|
+
Requires-Dist: ruff; extra == "dev"
|
|
41
|
+
Requires-Dist: pdoc; extra == "dev"
|
|
42
|
+
Dynamic: license-file
|
|
43
|
+
|
|
44
|
+
# Turing Machine Simulator
|
|
45
|
+
|
|
46
|
+
[](https://gitlab.com/lorax0/turingmachine/-/pipelines)
|
|
47
|
+
[](https://gitlab.com/lorax0/turingmachine/-/pipelines)
|
|
48
|
+

|
|
49
|
+
[](https://gitlab.com/lorax0/turingmachine/-/blob/main/LICENSE)
|
|
50
|
+
|
|
51
|
+
Single-tape Turing machine simulator. Machines are written in YAML and can be run from the
|
|
52
|
+
command line, step by step, or in the browser. Nondeterministic machines are supported too.
|
|
53
|
+
|
|
54
|
+
It started as a university assignment and was later rewritten as a standalone project.
|
|
55
|
+
|
|
56
|
+
**Web demo:** https://lorax0.gitlab.io/turingmachine/
|
|
57
|
+
|
|
58
|
+

|
|
59
|
+
|
|
60
|
+
## Installation
|
|
61
|
+
|
|
62
|
+
```bash
|
|
63
|
+
pip install git+https://gitlab.com/lorax0/turingmachine.git
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
Python 3.10 or newer.
|
|
67
|
+
|
|
68
|
+
## Usage
|
|
69
|
+
|
|
70
|
+
```console
|
|
71
|
+
$ turing -c examples/binary_addition.yaml 101+11
|
|
72
|
+
1000
|
|
73
|
+
|
|
74
|
+
$ turing -c examples/binary_palindrome.yaml 10101
|
|
75
|
+
1
|
|
76
|
+
|
|
77
|
+
$ turing -c examples/anbn.yaml --trace aabb
|
|
78
|
+
$ turing -c examples/busy_beaver_3.yaml --animate ""
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
| Option | Description |
|
|
82
|
+
|-------------------|------------------------------------------------|
|
|
83
|
+
| `tape` | input word (`""` for an empty tape) |
|
|
84
|
+
| `-c, --config` | machine definition file |
|
|
85
|
+
| `-t, --trace` | print every step as a table |
|
|
86
|
+
| `-a, --animate` | animate the run |
|
|
87
|
+
| `-d, --delay` | delay between animation frames (default 0.2 s) |
|
|
88
|
+
| `-s, --max-steps` | step limit (default 10000) |
|
|
89
|
+
|
|
90
|
+
On error the message goes to stderr and the exit code is 1.
|
|
91
|
+
|
|
92
|
+

|
|
93
|
+
|
|
94
|
+
## Machine format
|
|
95
|
+
|
|
96
|
+
```yaml
|
|
97
|
+
name: Binary increment
|
|
98
|
+
description: Adds 1 to a binary number.
|
|
99
|
+
|
|
100
|
+
input_alphabet: [0, 1]
|
|
101
|
+
initial_state: q0
|
|
102
|
+
final_states: [qf]
|
|
103
|
+
|
|
104
|
+
transitions:
|
|
105
|
+
q0: # go to the end of the number
|
|
106
|
+
0: [q0, 0, R]
|
|
107
|
+
1: [q0, 1, R]
|
|
108
|
+
X: [q1, X, L]
|
|
109
|
+
q1: # add 1 and carry to the left
|
|
110
|
+
0: [qf, 1, S]
|
|
111
|
+
1: [q1, 0, L]
|
|
112
|
+
X: [qf, 1, S]
|
|
113
|
+
```
|
|
114
|
+
|
|
115
|
+
Each rule is `symbol: [next_state, write, direction]`, where direction is `L`, `R` or `S`
|
|
116
|
+
(stay). `X` is the blank symbol and the tape is unbounded in both directions.
|
|
117
|
+
|
|
118
|
+
- The machine halts when it enters a final state. A missing rule is an error.
|
|
119
|
+
- `states` and `tape_alphabet` can be omitted; they are taken from the transitions.
|
|
120
|
+
- Unknown keys are rejected, so a typo like `final_state` is reported.
|
|
121
|
+
- A symbol with several rules makes the machine nondeterministic, e.g.
|
|
122
|
+
`a: [[guess, a, R], [upper, A, R]]`. The input is accepted if any branch reaches a final state.
|
|
123
|
+
The simulator does a breadth-first search and shows the shortest accepting run.
|
|
124
|
+
|
|
125
|
+
The old text format from the original assignment (six lines, rules like `((q0,0)->(q0,0,1))`)
|
|
126
|
+
still works for files not ending in `.yaml`.
|
|
127
|
+
|
|
128
|
+
## Examples
|
|
129
|
+
|
|
130
|
+
| File | Input | Output | |
|
|
131
|
+
|--------------------------|----------|--------------------|-------------------------------------|
|
|
132
|
+
| `binary_increment.yaml` | `1011` | `1100` | add 1 |
|
|
133
|
+
| `binary_negation.yaml` | `1011` | `0100` | flip bits |
|
|
134
|
+
| `binary_addition.yaml` | `101+11` | `1000` | add two numbers |
|
|
135
|
+
| `unary_doubling.yaml` | `aaa` | `aaaaaa` | aⁿ -> a²ⁿ |
|
|
136
|
+
| `binary_palindrome.yaml` | `10101` | `1` / `0` | palindrome check |
|
|
137
|
+
| `anbn.yaml` | `aabb` | `1` / `0` | aⁿbⁿ check |
|
|
138
|
+
| `repeated_word.yaml` | `abab` | `1` / rejected | ww check, nondeterministic |
|
|
139
|
+
| `busy_beaver_3.yaml` | empty | `111111` | 3-state busy beaver |
|
|
140
|
+
| `infinite_loop.yaml` | `1` | step limit error | never halts |
|
|
141
|
+
|
|
142
|
+
Each example is tested against a plain Python implementation on all short inputs
|
|
143
|
+
(`tests/test_examples.py`).
|
|
144
|
+
|
|
145
|
+
## Web demo
|
|
146
|
+
|
|
147
|
+
The demo page loads the package with [Pyodide](https://pyodide.org) and runs it in the browser,
|
|
148
|
+
so there is no backend and the simulator is not duplicated in JavaScript. You can edit the YAML,
|
|
149
|
+
run it and step through the result.
|
|
150
|
+
|
|
151
|
+
[](https://lorax0.gitlab.io/turingmachine/)
|
|
152
|
+
|
|
153
|
+
## Python API
|
|
154
|
+
|
|
155
|
+
```python
|
|
156
|
+
from turing import TuringMachine
|
|
157
|
+
|
|
158
|
+
machine = TuringMachine.from_file("examples/binary_addition.yaml")
|
|
159
|
+
print(machine.tape_to_str(machine.start("101+11"))) # 1000
|
|
160
|
+
|
|
161
|
+
for snapshot in machine.steps("1+1"):
|
|
162
|
+
print(snapshot.step, snapshot.state, "".join(snapshot.tape), snapshot.head)
|
|
163
|
+
|
|
164
|
+
ww = TuringMachine.from_file("examples/repeated_word.yaml")
|
|
165
|
+
path = ww.search("abab") # shortest accepting run
|
|
166
|
+
```
|
|
167
|
+
|
|
168
|
+
Errors inherit from `turing.TuringMachineError`.
|
|
169
|
+
|
|
170
|
+
## Development
|
|
171
|
+
|
|
172
|
+
```bash
|
|
173
|
+
pip install -e ".[dev]"
|
|
174
|
+
pytest --cov
|
|
175
|
+
mypy
|
|
176
|
+
ruff check . && ruff format .
|
|
177
|
+
```
|
|
178
|
+
|
|
179
|
+
`python scripts/build_site.py` builds the web demo into `public/`
|
|
180
|
+
(`python -m http.server -d public` to try it locally). `python scripts/screenshot.py` and
|
|
181
|
+
`python scripts/demo_gif.py` regenerate the images in `assets/` (the GIF needs ImageMagick).
|
|
182
|
+
|
|
183
|
+
GitLab CI runs the linters and tests (Python 3.10 to 3.15), builds the package, deploys the
|
|
184
|
+
demo and the [API docs](https://lorax0.gitlab.io/turingmachine/api/) to Pages, and publishes to
|
|
185
|
+
PyPI (manual job on version tags). See [CONTRIBUTING.md](CONTRIBUTING.md).
|
|
186
|
+
|
|
187
|
+
## License
|
|
188
|
+
|
|
189
|
+
[MIT](https://gitlab.com/lorax0/turingmachine/-/blob/main/LICENSE)
|
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
# Turing Machine Simulator
|
|
2
|
+
|
|
3
|
+
[](https://gitlab.com/lorax0/turingmachine/-/pipelines)
|
|
4
|
+
[](https://gitlab.com/lorax0/turingmachine/-/pipelines)
|
|
5
|
+

|
|
6
|
+
[](https://gitlab.com/lorax0/turingmachine/-/blob/main/LICENSE)
|
|
7
|
+
|
|
8
|
+
Single-tape Turing machine simulator. Machines are written in YAML and can be run from the
|
|
9
|
+
command line, step by step, or in the browser. Nondeterministic machines are supported too.
|
|
10
|
+
|
|
11
|
+
It started as a university assignment and was later rewritten as a standalone project.
|
|
12
|
+
|
|
13
|
+
**Web demo:** https://lorax0.gitlab.io/turingmachine/
|
|
14
|
+
|
|
15
|
+

|
|
16
|
+
|
|
17
|
+
## Installation
|
|
18
|
+
|
|
19
|
+
```bash
|
|
20
|
+
pip install git+https://gitlab.com/lorax0/turingmachine.git
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
Python 3.10 or newer.
|
|
24
|
+
|
|
25
|
+
## Usage
|
|
26
|
+
|
|
27
|
+
```console
|
|
28
|
+
$ turing -c examples/binary_addition.yaml 101+11
|
|
29
|
+
1000
|
|
30
|
+
|
|
31
|
+
$ turing -c examples/binary_palindrome.yaml 10101
|
|
32
|
+
1
|
|
33
|
+
|
|
34
|
+
$ turing -c examples/anbn.yaml --trace aabb
|
|
35
|
+
$ turing -c examples/busy_beaver_3.yaml --animate ""
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
| Option | Description |
|
|
39
|
+
|-------------------|------------------------------------------------|
|
|
40
|
+
| `tape` | input word (`""` for an empty tape) |
|
|
41
|
+
| `-c, --config` | machine definition file |
|
|
42
|
+
| `-t, --trace` | print every step as a table |
|
|
43
|
+
| `-a, --animate` | animate the run |
|
|
44
|
+
| `-d, --delay` | delay between animation frames (default 0.2 s) |
|
|
45
|
+
| `-s, --max-steps` | step limit (default 10000) |
|
|
46
|
+
|
|
47
|
+
On error the message goes to stderr and the exit code is 1.
|
|
48
|
+
|
|
49
|
+

|
|
50
|
+
|
|
51
|
+
## Machine format
|
|
52
|
+
|
|
53
|
+
```yaml
|
|
54
|
+
name: Binary increment
|
|
55
|
+
description: Adds 1 to a binary number.
|
|
56
|
+
|
|
57
|
+
input_alphabet: [0, 1]
|
|
58
|
+
initial_state: q0
|
|
59
|
+
final_states: [qf]
|
|
60
|
+
|
|
61
|
+
transitions:
|
|
62
|
+
q0: # go to the end of the number
|
|
63
|
+
0: [q0, 0, R]
|
|
64
|
+
1: [q0, 1, R]
|
|
65
|
+
X: [q1, X, L]
|
|
66
|
+
q1: # add 1 and carry to the left
|
|
67
|
+
0: [qf, 1, S]
|
|
68
|
+
1: [q1, 0, L]
|
|
69
|
+
X: [qf, 1, S]
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
Each rule is `symbol: [next_state, write, direction]`, where direction is `L`, `R` or `S`
|
|
73
|
+
(stay). `X` is the blank symbol and the tape is unbounded in both directions.
|
|
74
|
+
|
|
75
|
+
- The machine halts when it enters a final state. A missing rule is an error.
|
|
76
|
+
- `states` and `tape_alphabet` can be omitted; they are taken from the transitions.
|
|
77
|
+
- Unknown keys are rejected, so a typo like `final_state` is reported.
|
|
78
|
+
- A symbol with several rules makes the machine nondeterministic, e.g.
|
|
79
|
+
`a: [[guess, a, R], [upper, A, R]]`. The input is accepted if any branch reaches a final state.
|
|
80
|
+
The simulator does a breadth-first search and shows the shortest accepting run.
|
|
81
|
+
|
|
82
|
+
The old text format from the original assignment (six lines, rules like `((q0,0)->(q0,0,1))`)
|
|
83
|
+
still works for files not ending in `.yaml`.
|
|
84
|
+
|
|
85
|
+
## Examples
|
|
86
|
+
|
|
87
|
+
| File | Input | Output | |
|
|
88
|
+
|--------------------------|----------|--------------------|-------------------------------------|
|
|
89
|
+
| `binary_increment.yaml` | `1011` | `1100` | add 1 |
|
|
90
|
+
| `binary_negation.yaml` | `1011` | `0100` | flip bits |
|
|
91
|
+
| `binary_addition.yaml` | `101+11` | `1000` | add two numbers |
|
|
92
|
+
| `unary_doubling.yaml` | `aaa` | `aaaaaa` | aⁿ -> a²ⁿ |
|
|
93
|
+
| `binary_palindrome.yaml` | `10101` | `1` / `0` | palindrome check |
|
|
94
|
+
| `anbn.yaml` | `aabb` | `1` / `0` | aⁿbⁿ check |
|
|
95
|
+
| `repeated_word.yaml` | `abab` | `1` / rejected | ww check, nondeterministic |
|
|
96
|
+
| `busy_beaver_3.yaml` | empty | `111111` | 3-state busy beaver |
|
|
97
|
+
| `infinite_loop.yaml` | `1` | step limit error | never halts |
|
|
98
|
+
|
|
99
|
+
Each example is tested against a plain Python implementation on all short inputs
|
|
100
|
+
(`tests/test_examples.py`).
|
|
101
|
+
|
|
102
|
+
## Web demo
|
|
103
|
+
|
|
104
|
+
The demo page loads the package with [Pyodide](https://pyodide.org) and runs it in the browser,
|
|
105
|
+
so there is no backend and the simulator is not duplicated in JavaScript. You can edit the YAML,
|
|
106
|
+
run it and step through the result.
|
|
107
|
+
|
|
108
|
+
[](https://lorax0.gitlab.io/turingmachine/)
|
|
109
|
+
|
|
110
|
+
## Python API
|
|
111
|
+
|
|
112
|
+
```python
|
|
113
|
+
from turing import TuringMachine
|
|
114
|
+
|
|
115
|
+
machine = TuringMachine.from_file("examples/binary_addition.yaml")
|
|
116
|
+
print(machine.tape_to_str(machine.start("101+11"))) # 1000
|
|
117
|
+
|
|
118
|
+
for snapshot in machine.steps("1+1"):
|
|
119
|
+
print(snapshot.step, snapshot.state, "".join(snapshot.tape), snapshot.head)
|
|
120
|
+
|
|
121
|
+
ww = TuringMachine.from_file("examples/repeated_word.yaml")
|
|
122
|
+
path = ww.search("abab") # shortest accepting run
|
|
123
|
+
```
|
|
124
|
+
|
|
125
|
+
Errors inherit from `turing.TuringMachineError`.
|
|
126
|
+
|
|
127
|
+
## Development
|
|
128
|
+
|
|
129
|
+
```bash
|
|
130
|
+
pip install -e ".[dev]"
|
|
131
|
+
pytest --cov
|
|
132
|
+
mypy
|
|
133
|
+
ruff check . && ruff format .
|
|
134
|
+
```
|
|
135
|
+
|
|
136
|
+
`python scripts/build_site.py` builds the web demo into `public/`
|
|
137
|
+
(`python -m http.server -d public` to try it locally). `python scripts/screenshot.py` and
|
|
138
|
+
`python scripts/demo_gif.py` regenerate the images in `assets/` (the GIF needs ImageMagick).
|
|
139
|
+
|
|
140
|
+
GitLab CI runs the linters and tests (Python 3.10 to 3.15), builds the package, deploys the
|
|
141
|
+
demo and the [API docs](https://lorax0.gitlab.io/turingmachine/api/) to Pages, and publishes to
|
|
142
|
+
PyPI (manual job on version tags). See [CONTRIBUTING.md](CONTRIBUTING.md).
|
|
143
|
+
|
|
144
|
+
## License
|
|
145
|
+
|
|
146
|
+
[MIT](https://gitlab.com/lorax0/turingmachine/-/blob/main/LICENSE)
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["setuptools>=77"]
|
|
3
|
+
build-backend = "setuptools.build_meta"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "turing-sim"
|
|
7
|
+
dynamic = ["version"]
|
|
8
|
+
description = "Turing machine simulator with step-by-step tracing, YAML definitions and nondeterminism"
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
requires-python = ">=3.10"
|
|
11
|
+
license = "MIT"
|
|
12
|
+
license-files = ["LICENSE"]
|
|
13
|
+
dependencies = [
|
|
14
|
+
"pyyaml>=6",
|
|
15
|
+
"rich>=13",
|
|
16
|
+
]
|
|
17
|
+
keywords = ["turing-machine", "automata", "simulator", "computability"]
|
|
18
|
+
classifiers = [
|
|
19
|
+
"Development Status :: 5 - Production/Stable",
|
|
20
|
+
"Environment :: Console",
|
|
21
|
+
"Intended Audience :: Education",
|
|
22
|
+
"Intended Audience :: Science/Research",
|
|
23
|
+
"Programming Language :: Python :: 3",
|
|
24
|
+
"Programming Language :: Python :: 3 :: Only",
|
|
25
|
+
"Programming Language :: Python :: 3.10",
|
|
26
|
+
"Programming Language :: Python :: 3.11",
|
|
27
|
+
"Programming Language :: Python :: 3.12",
|
|
28
|
+
"Programming Language :: Python :: 3.13",
|
|
29
|
+
"Programming Language :: Python :: 3.14",
|
|
30
|
+
"Programming Language :: Python :: 3.15",
|
|
31
|
+
"Operating System :: OS Independent",
|
|
32
|
+
"Topic :: Education",
|
|
33
|
+
"Topic :: Scientific/Engineering :: Mathematics",
|
|
34
|
+
"Typing :: Typed",
|
|
35
|
+
]
|
|
36
|
+
|
|
37
|
+
authors = [
|
|
38
|
+
{name = "Franciszek Wozniak"}
|
|
39
|
+
]
|
|
40
|
+
|
|
41
|
+
[project.urls]
|
|
42
|
+
Homepage = "https://gitlab.com/lorax0/turingmachine"
|
|
43
|
+
Source = "https://gitlab.com/lorax0/turingmachine"
|
|
44
|
+
Issues = "https://gitlab.com/lorax0/turingmachine/-/issues"
|
|
45
|
+
Documentation = "https://lorax0.gitlab.io/turingmachine/api/"
|
|
46
|
+
Changelog = "https://gitlab.com/lorax0/turingmachine/-/blob/main/CHANGELOG.md"
|
|
47
|
+
|
|
48
|
+
[project.optional-dependencies]
|
|
49
|
+
dev = [
|
|
50
|
+
"build",
|
|
51
|
+
"mypy",
|
|
52
|
+
"types-PyYAML",
|
|
53
|
+
"pytest",
|
|
54
|
+
"pytest-cov",
|
|
55
|
+
"ruff",
|
|
56
|
+
"pdoc",
|
|
57
|
+
]
|
|
58
|
+
|
|
59
|
+
[tool.setuptools]
|
|
60
|
+
package-dir = {"" = "src"}
|
|
61
|
+
|
|
62
|
+
[tool.setuptools.dynamic]
|
|
63
|
+
version = {attr = "turing.__version__"}
|
|
64
|
+
|
|
65
|
+
[tool.setuptools.packages.find]
|
|
66
|
+
where = ["src"]
|
|
67
|
+
|
|
68
|
+
[project.scripts]
|
|
69
|
+
turing = "turing.cli:main"
|
|
70
|
+
|
|
71
|
+
[tool.ruff]
|
|
72
|
+
line-length = 100
|
|
73
|
+
target-version = "py310"
|
|
74
|
+
src = ["src", "tests", "scripts"]
|
|
75
|
+
|
|
76
|
+
[tool.ruff.lint]
|
|
77
|
+
select = ["E", "W", "F", "I", "B", "UP", "SIM"]
|
|
78
|
+
|
|
79
|
+
[tool.ruff.lint.per-file-ignores]
|
|
80
|
+
"src/turing/__init__.py" = ["F401"]
|
|
81
|
+
|
|
82
|
+
[tool.mypy]
|
|
83
|
+
strict = true
|
|
84
|
+
files = ["src", "tests", "scripts"]
|
|
85
|
+
|
|
86
|
+
[tool.pytest.ini_options]
|
|
87
|
+
testpaths = ["tests"]
|
|
88
|
+
addopts = "-ra --strict-markers"
|
|
89
|
+
|
|
90
|
+
[tool.coverage.run]
|
|
91
|
+
source = ["turing"]
|
|
92
|
+
omit = ["*/__main__.py"]
|
|
93
|
+
|
|
94
|
+
[tool.coverage.report]
|
|
95
|
+
show_missing = true
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
"""Single-tape Turing machine simulator."""
|
|
2
|
+
|
|
3
|
+
from .definition import BLANK_SYMBOL, Definition, Transition, read_definition
|
|
4
|
+
from .exceptions import (
|
|
5
|
+
ConfigurationError,
|
|
6
|
+
InputAlphabetError,
|
|
7
|
+
StepLimitExceededError,
|
|
8
|
+
TapeAlphabetError,
|
|
9
|
+
TuringMachineError,
|
|
10
|
+
UndefinedTransitionError,
|
|
11
|
+
)
|
|
12
|
+
from .state import State
|
|
13
|
+
from .transition_function import TransitionFunction
|
|
14
|
+
from .turing_machine import Snapshot, TuringMachine
|
|
15
|
+
|
|
16
|
+
__version__ = "1.0.0"
|
|
17
|
+
|
|
18
|
+
__all__ = [
|
|
19
|
+
"BLANK_SYMBOL",
|
|
20
|
+
"ConfigurationError",
|
|
21
|
+
"Definition",
|
|
22
|
+
"InputAlphabetError",
|
|
23
|
+
"Snapshot",
|
|
24
|
+
"State",
|
|
25
|
+
"StepLimitExceededError",
|
|
26
|
+
"TapeAlphabetError",
|
|
27
|
+
"Transition",
|
|
28
|
+
"TransitionFunction",
|
|
29
|
+
"TuringMachine",
|
|
30
|
+
"TuringMachineError",
|
|
31
|
+
"UndefinedTransitionError",
|
|
32
|
+
"read_definition",
|
|
33
|
+
]
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
"""Entry point for the web demo, which runs the package on Pyodide."""
|
|
2
|
+
|
|
3
|
+
import json
|
|
4
|
+
from typing import Any
|
|
5
|
+
|
|
6
|
+
from .definition import parse_yaml
|
|
7
|
+
from .exceptions import TuringMachineError
|
|
8
|
+
from .turing_machine import DEFAULT_MAX_STEPS, Snapshot, TuringMachine
|
|
9
|
+
|
|
10
|
+
MAX_BROWSER_STEPS = 5_000
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
def _snapshot_to_dict(snapshot: Snapshot) -> dict[str, Any]:
|
|
14
|
+
return {
|
|
15
|
+
"step": snapshot.step,
|
|
16
|
+
"state": snapshot.state,
|
|
17
|
+
"tape": list(snapshot.tape),
|
|
18
|
+
"head": snapshot.head,
|
|
19
|
+
"read": snapshot.read,
|
|
20
|
+
"written": snapshot.written,
|
|
21
|
+
"direction": snapshot.direction,
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
def simulate(definition: str, word: str, max_steps: int = DEFAULT_MAX_STEPS) -> str:
|
|
26
|
+
"""Run a YAML machine on a word and return the run as JSON.
|
|
27
|
+
|
|
28
|
+
Steps computed before an error (e.g. the step limit) are still included.
|
|
29
|
+
"""
|
|
30
|
+
response: dict[str, Any] = {
|
|
31
|
+
"name": "",
|
|
32
|
+
"description": "",
|
|
33
|
+
"deterministic": True,
|
|
34
|
+
"snapshots": [],
|
|
35
|
+
"result": None,
|
|
36
|
+
"accepted": False,
|
|
37
|
+
"error": None,
|
|
38
|
+
}
|
|
39
|
+
try:
|
|
40
|
+
machine = TuringMachine.from_definition(parse_yaml(definition))
|
|
41
|
+
response.update(
|
|
42
|
+
name=machine.name,
|
|
43
|
+
description=machine.description,
|
|
44
|
+
deterministic=machine.is_deterministic,
|
|
45
|
+
)
|
|
46
|
+
snapshots = response["snapshots"]
|
|
47
|
+
for snapshot in machine.steps(word, max_steps=min(max_steps, MAX_BROWSER_STEPS)):
|
|
48
|
+
snapshots.append(_snapshot_to_dict(snapshot))
|
|
49
|
+
response["result"] = machine.tape_to_str(snapshots[-1]["tape"])
|
|
50
|
+
response["accepted"] = True
|
|
51
|
+
except TuringMachineError as error:
|
|
52
|
+
response["error"] = str(error)
|
|
53
|
+
return json.dumps(response)
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import argparse
|
|
2
|
+
|
|
3
|
+
from rich.console import Console
|
|
4
|
+
|
|
5
|
+
from . import __version__
|
|
6
|
+
from .exceptions import TuringMachineError
|
|
7
|
+
from .render import animate, print_trace
|
|
8
|
+
from .turing_machine import DEFAULT_MAX_STEPS, TuringMachine
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
def build_parser() -> argparse.ArgumentParser:
|
|
12
|
+
parser = argparse.ArgumentParser(
|
|
13
|
+
prog="turing", description="Run a Turing machine on an input word."
|
|
14
|
+
)
|
|
15
|
+
parser.add_argument("--version", action="version", version=f"%(prog)s {__version__}")
|
|
16
|
+
parser.add_argument("tape", type=str, help="input word written on the tape")
|
|
17
|
+
parser.add_argument(
|
|
18
|
+
"-c",
|
|
19
|
+
"--config",
|
|
20
|
+
type=str,
|
|
21
|
+
required=True,
|
|
22
|
+
help="machine definition file (.yaml, or legacy .txt format)",
|
|
23
|
+
)
|
|
24
|
+
parser.add_argument(
|
|
25
|
+
"-s",
|
|
26
|
+
"--max-steps",
|
|
27
|
+
type=int,
|
|
28
|
+
default=DEFAULT_MAX_STEPS,
|
|
29
|
+
help=f"maximum number of steps before giving up (default: {DEFAULT_MAX_STEPS})",
|
|
30
|
+
)
|
|
31
|
+
view = parser.add_mutually_exclusive_group()
|
|
32
|
+
view.add_argument(
|
|
33
|
+
"-t", "--trace", action="store_true", help="print every step of the run as a table"
|
|
34
|
+
)
|
|
35
|
+
view.add_argument(
|
|
36
|
+
"-a", "--animate", action="store_true", help="animate the run in the terminal"
|
|
37
|
+
)
|
|
38
|
+
parser.add_argument(
|
|
39
|
+
"-d",
|
|
40
|
+
"--delay",
|
|
41
|
+
type=float,
|
|
42
|
+
default=0.2,
|
|
43
|
+
help="pause between animation frames in seconds (default: 0.2)",
|
|
44
|
+
)
|
|
45
|
+
return parser
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
def main(argv: list[str] | None = None) -> int:
|
|
49
|
+
args = build_parser().parse_args(argv)
|
|
50
|
+
console = Console(highlight=False)
|
|
51
|
+
errors = Console(stderr=True, highlight=False)
|
|
52
|
+
|
|
53
|
+
try:
|
|
54
|
+
machine = TuringMachine.from_file(args.config)
|
|
55
|
+
if args.trace or args.animate:
|
|
56
|
+
snapshots = machine.steps(args.tape, max_steps=args.max_steps)
|
|
57
|
+
if args.trace:
|
|
58
|
+
last = print_trace(console, snapshots)
|
|
59
|
+
else:
|
|
60
|
+
last = animate(console, snapshots, args.delay)
|
|
61
|
+
result = machine.tape_to_str(list(last.tape))
|
|
62
|
+
console.print(f"\n[bold]Result:[/] {result} [dim]({last.step} steps)[/]")
|
|
63
|
+
if not machine.is_deterministic:
|
|
64
|
+
console.print("[dim]Nondeterministic machine: shortest accepting computation.[/]")
|
|
65
|
+
else:
|
|
66
|
+
print(machine.tape_to_str(machine.start(args.tape, max_steps=args.max_steps)))
|
|
67
|
+
except TuringMachineError as error:
|
|
68
|
+
errors.print(f"[bold red]Error:[/] {error}")
|
|
69
|
+
return 1
|
|
70
|
+
return 0
|