turing-py 0.1.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.
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Eric Santos
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,161 @@
1
+ Metadata-Version: 2.4
2
+ Name: turing-py
3
+ Version: 0.1.0
4
+ Summary: Education Turing Machine simulator in Python
5
+ Author-email: Eric Santos <ericshantos13@gmail.com>
6
+ Project-URL: Homepage, https://github.com/ericshantos/turing-py
7
+ Requires-Python: >=3.10
8
+ Description-Content-Type: text/markdown
9
+ License-File: LICENSE
10
+ Dynamic: license-file
11
+
12
+ # py-turing
13
+
14
+ A simple and extensible **Turing Machine simulator written in Python**, designed for learning, experimentation, and educational purposes.
15
+
16
+ This project allows users to define **states, transitions, and tapes** to simulate the behavior of a **deterministic Turing Machine**, one of the fundamental models in the theory of computation.
17
+
18
+ ---
19
+
20
+ ## 📚 About
21
+
22
+ A **Turing Machine** is an abstract computational model introduced by **Alan Turing** in 1936. It is widely used in computer science to study the limits of computation and algorithm design.
23
+
24
+ **py-turing** provides a Python implementation that enables:
25
+
26
+ * Simulation of Turing Machines
27
+ * Creation of custom transition rules
28
+ * Step-by-step computation
29
+ * Experimentation with theoretical algorithms
30
+
31
+ This project is intended for:
32
+
33
+ * Computer Science students
34
+ * Theory of Computation courses
35
+ * Developers interested in computational models
36
+ * Educational demonstrations
37
+
38
+ ---
39
+
40
+ ## ✨ Features
41
+
42
+ * Deterministic Turing Machine simulation
43
+ * Customizable tape input
44
+ * Configurable states and transitions
45
+ * Step-by-step execution
46
+ * Simple and modular Python architecture
47
+ * Easy to extend and modify
48
+ * CLI-ready structure
49
+
50
+ ---
51
+
52
+ ## 📂 Project Structure
53
+
54
+ ```
55
+ py-turing/
56
+
57
+ ├── pyproject.toml
58
+ ├── README.md
59
+ ├── LICENSE
60
+ ├── .gitignore
61
+
62
+ ├── src/
63
+ └── pyturing/
64
+ ├── __init__.py
65
+
66
+ ├── machine/
67
+ │ ├── __init__.py
68
+ │ ├── turing_machine.py
69
+ │ ├── tape.py
70
+ │ └── states.py
71
+
72
+ ├── alphabet/
73
+ │ ├── __init__.py
74
+ │ ├── symbol.py
75
+ │ ├── alphabet.py
76
+ │ └── tape_alphabet.py
77
+
78
+ └─ transition/
79
+ ├── __init__.py
80
+ ├── transition.py
81
+ ├── transition_function.py
82
+ └── direction.py
83
+ ```
84
+
85
+ ---
86
+
87
+ ## 🧪 Example Turing Machines
88
+
89
+ Examples you can implement using **py-turing**:
90
+
91
+ * Binary increment machine
92
+ * Palindrome checker
93
+ * Unary addition
94
+ * Even/Odd binary checker
95
+ * String copying machine
96
+
97
+ ---
98
+
99
+ ## 🧩 How It Works
100
+
101
+ A Turing Machine consists of:
102
+
103
+ * **Tape** – infinite memory divided into cells
104
+ * **Head** – reads and writes symbols on the tape
105
+ * **States** – machine configuration states
106
+ * **Transition Function** – rules that determine the next action
107
+
108
+ Each step follows this rule:
109
+
110
+ ```
111
+ (Current State, Read Symbol) → (Next State, Write Symbol, Move Direction)
112
+ ```
113
+
114
+ Where direction can be:
115
+
116
+ * `L` → Move left
117
+ * `R` → Move right
118
+
119
+ ---
120
+
121
+ ## 🤝 Contributing
122
+
123
+ Contributions are welcome!
124
+
125
+ To contribute:
126
+
127
+ 1. Fork the repository
128
+ 2. Create a new branch
129
+
130
+ ```bash
131
+ git checkout -b feature/my-feature
132
+ ```
133
+
134
+ 3. Make your changes
135
+ 4. Commit your work
136
+
137
+ ```bash
138
+ git commit -m "Add new feature"
139
+ ```
140
+
141
+ 5. Push to your fork
142
+
143
+ ```bash
144
+ git push origin feature/my-feature
145
+ ```
146
+
147
+ 6. Open a Pull Request
148
+
149
+ ---
150
+
151
+ ## 📜 License
152
+
153
+ This project is licensed under the **MIT License**.
154
+
155
+ ---
156
+
157
+ ## 👨‍💻 Author
158
+
159
+ Developed by **Eric Santos**.
160
+
161
+ ---
@@ -0,0 +1,150 @@
1
+ # py-turing
2
+
3
+ A simple and extensible **Turing Machine simulator written in Python**, designed for learning, experimentation, and educational purposes.
4
+
5
+ This project allows users to define **states, transitions, and tapes** to simulate the behavior of a **deterministic Turing Machine**, one of the fundamental models in the theory of computation.
6
+
7
+ ---
8
+
9
+ ## 📚 About
10
+
11
+ A **Turing Machine** is an abstract computational model introduced by **Alan Turing** in 1936. It is widely used in computer science to study the limits of computation and algorithm design.
12
+
13
+ **py-turing** provides a Python implementation that enables:
14
+
15
+ * Simulation of Turing Machines
16
+ * Creation of custom transition rules
17
+ * Step-by-step computation
18
+ * Experimentation with theoretical algorithms
19
+
20
+ This project is intended for:
21
+
22
+ * Computer Science students
23
+ * Theory of Computation courses
24
+ * Developers interested in computational models
25
+ * Educational demonstrations
26
+
27
+ ---
28
+
29
+ ## ✨ Features
30
+
31
+ * Deterministic Turing Machine simulation
32
+ * Customizable tape input
33
+ * Configurable states and transitions
34
+ * Step-by-step execution
35
+ * Simple and modular Python architecture
36
+ * Easy to extend and modify
37
+ * CLI-ready structure
38
+
39
+ ---
40
+
41
+ ## 📂 Project Structure
42
+
43
+ ```
44
+ py-turing/
45
+
46
+ ├── pyproject.toml
47
+ ├── README.md
48
+ ├── LICENSE
49
+ ├── .gitignore
50
+
51
+ ├── src/
52
+ └── pyturing/
53
+ ├── __init__.py
54
+
55
+ ├── machine/
56
+ │ ├── __init__.py
57
+ │ ├── turing_machine.py
58
+ │ ├── tape.py
59
+ │ └── states.py
60
+
61
+ ├── alphabet/
62
+ │ ├── __init__.py
63
+ │ ├── symbol.py
64
+ │ ├── alphabet.py
65
+ │ └── tape_alphabet.py
66
+
67
+ └─ transition/
68
+ ├── __init__.py
69
+ ├── transition.py
70
+ ├── transition_function.py
71
+ └── direction.py
72
+ ```
73
+
74
+ ---
75
+
76
+ ## 🧪 Example Turing Machines
77
+
78
+ Examples you can implement using **py-turing**:
79
+
80
+ * Binary increment machine
81
+ * Palindrome checker
82
+ * Unary addition
83
+ * Even/Odd binary checker
84
+ * String copying machine
85
+
86
+ ---
87
+
88
+ ## 🧩 How It Works
89
+
90
+ A Turing Machine consists of:
91
+
92
+ * **Tape** – infinite memory divided into cells
93
+ * **Head** – reads and writes symbols on the tape
94
+ * **States** – machine configuration states
95
+ * **Transition Function** – rules that determine the next action
96
+
97
+ Each step follows this rule:
98
+
99
+ ```
100
+ (Current State, Read Symbol) → (Next State, Write Symbol, Move Direction)
101
+ ```
102
+
103
+ Where direction can be:
104
+
105
+ * `L` → Move left
106
+ * `R` → Move right
107
+
108
+ ---
109
+
110
+ ## 🤝 Contributing
111
+
112
+ Contributions are welcome!
113
+
114
+ To contribute:
115
+
116
+ 1. Fork the repository
117
+ 2. Create a new branch
118
+
119
+ ```bash
120
+ git checkout -b feature/my-feature
121
+ ```
122
+
123
+ 3. Make your changes
124
+ 4. Commit your work
125
+
126
+ ```bash
127
+ git commit -m "Add new feature"
128
+ ```
129
+
130
+ 5. Push to your fork
131
+
132
+ ```bash
133
+ git push origin feature/my-feature
134
+ ```
135
+
136
+ 6. Open a Pull Request
137
+
138
+ ---
139
+
140
+ ## 📜 License
141
+
142
+ This project is licensed under the **MIT License**.
143
+
144
+ ---
145
+
146
+ ## 👨‍💻 Author
147
+
148
+ Developed by **Eric Santos**.
149
+
150
+ ---
@@ -0,0 +1,19 @@
1
+ [project]
2
+ name="turing-py"
3
+ version="0.1.0"
4
+ description = "Education Turing Machine simulator in Python"
5
+ readme = "README.md"
6
+ requires-python = ">=3.10"
7
+
8
+ authors = [
9
+ { name = "Eric Santos", email = "ericshantos13@gmail.com" }
10
+ ]
11
+
12
+ dependencies = []
13
+
14
+ [build-system]
15
+ requires = ["setuptools", "wheel"]
16
+ build-backend = "setuptools.build_meta"
17
+
18
+ [project.urls]
19
+ "Homepage" = "https://github.com/ericshantos/turing-py"
@@ -0,0 +1,4 @@
1
+ [egg_info]
2
+ tag_build =
3
+ tag_date = 0
4
+
@@ -0,0 +1,161 @@
1
+ Metadata-Version: 2.4
2
+ Name: turing-py
3
+ Version: 0.1.0
4
+ Summary: Education Turing Machine simulator in Python
5
+ Author-email: Eric Santos <ericshantos13@gmail.com>
6
+ Project-URL: Homepage, https://github.com/ericshantos/turing-py
7
+ Requires-Python: >=3.10
8
+ Description-Content-Type: text/markdown
9
+ License-File: LICENSE
10
+ Dynamic: license-file
11
+
12
+ # py-turing
13
+
14
+ A simple and extensible **Turing Machine simulator written in Python**, designed for learning, experimentation, and educational purposes.
15
+
16
+ This project allows users to define **states, transitions, and tapes** to simulate the behavior of a **deterministic Turing Machine**, one of the fundamental models in the theory of computation.
17
+
18
+ ---
19
+
20
+ ## 📚 About
21
+
22
+ A **Turing Machine** is an abstract computational model introduced by **Alan Turing** in 1936. It is widely used in computer science to study the limits of computation and algorithm design.
23
+
24
+ **py-turing** provides a Python implementation that enables:
25
+
26
+ * Simulation of Turing Machines
27
+ * Creation of custom transition rules
28
+ * Step-by-step computation
29
+ * Experimentation with theoretical algorithms
30
+
31
+ This project is intended for:
32
+
33
+ * Computer Science students
34
+ * Theory of Computation courses
35
+ * Developers interested in computational models
36
+ * Educational demonstrations
37
+
38
+ ---
39
+
40
+ ## ✨ Features
41
+
42
+ * Deterministic Turing Machine simulation
43
+ * Customizable tape input
44
+ * Configurable states and transitions
45
+ * Step-by-step execution
46
+ * Simple and modular Python architecture
47
+ * Easy to extend and modify
48
+ * CLI-ready structure
49
+
50
+ ---
51
+
52
+ ## 📂 Project Structure
53
+
54
+ ```
55
+ py-turing/
56
+
57
+ ├── pyproject.toml
58
+ ├── README.md
59
+ ├── LICENSE
60
+ ├── .gitignore
61
+
62
+ ├── src/
63
+ └── pyturing/
64
+ ├── __init__.py
65
+
66
+ ├── machine/
67
+ │ ├── __init__.py
68
+ │ ├── turing_machine.py
69
+ │ ├── tape.py
70
+ │ └── states.py
71
+
72
+ ├── alphabet/
73
+ │ ├── __init__.py
74
+ │ ├── symbol.py
75
+ │ ├── alphabet.py
76
+ │ └── tape_alphabet.py
77
+
78
+ └─ transition/
79
+ ├── __init__.py
80
+ ├── transition.py
81
+ ├── transition_function.py
82
+ └── direction.py
83
+ ```
84
+
85
+ ---
86
+
87
+ ## 🧪 Example Turing Machines
88
+
89
+ Examples you can implement using **py-turing**:
90
+
91
+ * Binary increment machine
92
+ * Palindrome checker
93
+ * Unary addition
94
+ * Even/Odd binary checker
95
+ * String copying machine
96
+
97
+ ---
98
+
99
+ ## 🧩 How It Works
100
+
101
+ A Turing Machine consists of:
102
+
103
+ * **Tape** – infinite memory divided into cells
104
+ * **Head** – reads and writes symbols on the tape
105
+ * **States** – machine configuration states
106
+ * **Transition Function** – rules that determine the next action
107
+
108
+ Each step follows this rule:
109
+
110
+ ```
111
+ (Current State, Read Symbol) → (Next State, Write Symbol, Move Direction)
112
+ ```
113
+
114
+ Where direction can be:
115
+
116
+ * `L` → Move left
117
+ * `R` → Move right
118
+
119
+ ---
120
+
121
+ ## 🤝 Contributing
122
+
123
+ Contributions are welcome!
124
+
125
+ To contribute:
126
+
127
+ 1. Fork the repository
128
+ 2. Create a new branch
129
+
130
+ ```bash
131
+ git checkout -b feature/my-feature
132
+ ```
133
+
134
+ 3. Make your changes
135
+ 4. Commit your work
136
+
137
+ ```bash
138
+ git commit -m "Add new feature"
139
+ ```
140
+
141
+ 5. Push to your fork
142
+
143
+ ```bash
144
+ git push origin feature/my-feature
145
+ ```
146
+
147
+ 6. Open a Pull Request
148
+
149
+ ---
150
+
151
+ ## 📜 License
152
+
153
+ This project is licensed under the **MIT License**.
154
+
155
+ ---
156
+
157
+ ## 👨‍💻 Author
158
+
159
+ Developed by **Eric Santos**.
160
+
161
+ ---
@@ -0,0 +1,20 @@
1
+ LICENSE
2
+ README.md
3
+ pyproject.toml
4
+ src/turing_py.egg-info/PKG-INFO
5
+ src/turing_py.egg-info/SOURCES.txt
6
+ src/turing_py.egg-info/dependency_links.txt
7
+ src/turing_py.egg-info/top_level.txt
8
+ src/turingpy/__init__.py
9
+ src/turingpy/alphabet/__init__.py
10
+ src/turingpy/alphabet/input_alphabet.py
11
+ src/turingpy/alphabet/symbol.py
12
+ src/turingpy/alphabet/tape_alphabet.py
13
+ src/turingpy/machine/__init__.py
14
+ src/turingpy/machine/machine.py
15
+ src/turingpy/machine/states.py
16
+ src/turingpy/machine/tape.py
17
+ src/turingpy/transition/__init__.py
18
+ src/turingpy/transition/direction.py
19
+ src/turingpy/transition/transition.py
20
+ src/turingpy/transition/transition_function.py
@@ -0,0 +1 @@
1
+ turingpy
@@ -0,0 +1,21 @@
1
+ from .transition import TransitionFunction
2
+ from .transition import Transition
3
+
4
+ from .alphabet import Alphabet
5
+ from .alphabet import TapeAlphabet
6
+ from .alphabet import Symbol
7
+
8
+ from .machine import TuringMachine
9
+ from .machine import States
10
+ from .machine import Tape
11
+
12
+ __all__ = [
13
+ "TransitionFunction",
14
+ "Transition",
15
+ "Alphabet",
16
+ "TapeAlphabet",
17
+ "Symbol",
18
+ "TuringMachine",
19
+ "States",
20
+ "Tape",
21
+ ]
@@ -0,0 +1,5 @@
1
+ from .input_alphabet import Alphabet
2
+ from .tape_alphabet import TapeAlphabet
3
+ from .symbol import Symbol
4
+
5
+ __all__ = ["Alphabet", "TapeAlphabet", "Symbol"]
@@ -0,0 +1,35 @@
1
+ # -*- coding: utf-8 -*-
2
+ """
3
+ Input alphabet implementation for the Turing machine.
4
+
5
+ @author: Eric Santos <ericshantos13@gmail.com>
6
+ """
7
+
8
+
9
+ from typing import Iterator
10
+ from .symbol import Symbol
11
+
12
+
13
+ class Alphabet:
14
+ caracter: str = "Σ"
15
+
16
+ def __init__(self, *symbols: Symbol) -> None:
17
+
18
+ if not symbols:
19
+ raise ValueError("Alphabet cannot be empty")
20
+
21
+ self._symbols: set[Symbol] = set(symbols)
22
+
23
+ def __contains__(self, symbol: Symbol) -> bool:
24
+ return symbol in self._symbols
25
+
26
+ def __iter__(self) -> Iterator[Symbol]:
27
+ return iter(self._symbols)
28
+
29
+ def __str__(self) -> str:
30
+ return f"{self.caracter} = {{ {', '.join(str(s) for s in self._symbols)} }}"
31
+
32
+ def __or__(self, other: "Alphabet") -> "Alphabet":
33
+ if not isinstance(other, Alphabet):
34
+ return NotImplemented
35
+ return Alphabet(*(self._symbols | other._symbols))
@@ -0,0 +1,35 @@
1
+ # -*- coding: utf-8 -*-
2
+ """
3
+ Symbol implementation for the Turing machine.
4
+
5
+ @author: Eric Santos <ericshantos13@gmail.com>
6
+ """
7
+
8
+
9
+ class Symbol:
10
+
11
+ _instances: dict[str, "Symbol"] = {}
12
+
13
+ def __new__(cls, value: str):
14
+
15
+ if value not in cls._instances:
16
+
17
+ instance = super().__new__(cls)
18
+ instance.value = value
19
+
20
+ cls._instances[value] = instance
21
+
22
+ return cls._instances[value]
23
+
24
+ def __str__(self) -> str:
25
+ return self.value
26
+
27
+ def __repr__(self) -> str:
28
+ return f"Symbol({self.value})"
29
+
30
+ def __hash__(self) -> int:
31
+ return hash(self.value)
32
+
33
+ def __eq__(self, other) -> bool:
34
+ return isinstance(other, Symbol) and self.value == other.value
35
+
@@ -0,0 +1,24 @@
1
+ # -*- coding: utf-8 -*-
2
+ """
3
+ Tape alphabet implementation for the Turing machine.
4
+
5
+ @Author: Eric Santos <ericshantos13@gmail.com>
6
+ """
7
+
8
+
9
+ from .input_alphabet import Alphabet
10
+ from .symbol import Symbol
11
+
12
+
13
+ class TapeAlphabet(Alphabet):
14
+ caracter: str = "Γ"
15
+
16
+ def __init__(self, blank: Symbol, *symbols: Symbol) -> None:
17
+
18
+ super().__init__(*symbols)
19
+
20
+ if blank not in self._symbols:
21
+ raise ValueError("Blank must belong to the alphabet")
22
+
23
+ self.blank = blank
24
+
@@ -0,0 +1,6 @@
1
+
2
+ from .machine import TuringMachine
3
+ from .states import States
4
+ from .tape import Tape
5
+
6
+ __all__ = ["TuringMachine", "States", "Tape"]
@@ -0,0 +1,96 @@
1
+ """
2
+ @author: Eric Santos <ericshantos13@gmail.com>
3
+
4
+ Turing Machine implementation.
5
+ """
6
+
7
+
8
+ from ..alphabet import Alphabet, TapeAlphabet, Symbol
9
+ from .tape import Tape
10
+ from ..transition import TransitionFunction
11
+
12
+
13
+ class TuringMachine:
14
+ def __init__(
15
+ self,
16
+ states: str,
17
+ alphabet: Alphabet,
18
+ tape_alphabet: TapeAlphabet,
19
+ delta: TransitionFunction,
20
+ blank: Symbol
21
+ ):
22
+ self.states = states
23
+ self.alphabet = alphabet
24
+ self.tape_alphabet = tape_alphabet
25
+ self.delta = delta
26
+ self.blank = blank
27
+
28
+ self.head: int = 0
29
+
30
+ def run(
31
+ self,
32
+ input: str,
33
+ debug: bool = False,
34
+ max_steps: int = 10000
35
+ ) -> None:
36
+
37
+ steps = 0
38
+
39
+ self.states.current_state = self.states.initial_state
40
+ self._load_tape(input)
41
+
42
+ while self.states.current_state not in self.states.final_states:
43
+
44
+ if steps > max_steps:
45
+ raise RuntimeError("Max steps exceeded")
46
+
47
+ steps += 1
48
+
49
+ if debug:
50
+ print(self)
51
+
52
+ if not self._step():
53
+ break
54
+
55
+ if debug:
56
+ print(self)
57
+
58
+ def _load_tape(self, input: str) -> None:
59
+ alphabet = self.alphabet | self.tape_alphabet
60
+ self.tape = Tape(input, alphabet, self.blank)
61
+ self.head = 0
62
+
63
+ def _step(self) -> bool:
64
+ symbol = self.tape[self.head]
65
+
66
+ t = self.delta(self.states.current_state, symbol)
67
+
68
+ if t is None:
69
+ return False
70
+
71
+ self.tape[self.head] = t.new_symbol
72
+ self.states.current_state = t.new_state
73
+
74
+ self.head = t.direction.move(self.head)
75
+
76
+ return True
77
+
78
+ def __str__(self) -> str:
79
+
80
+ tape_str = "".join(str(s) for s in self.tape)
81
+
82
+ head_indicator = " " * self.head + "^"
83
+
84
+ return f"""
85
+ State : {self.states.current_state}
86
+ Head : {self.head}
87
+ Tape : {tape_str}
88
+ {head_indicator}
89
+ """
90
+
91
+ @property
92
+ def result(self) -> str:
93
+
94
+ tape = "".join(str(s) for s in self.tape)
95
+
96
+ return tape.rstrip(str(self.blank))
@@ -0,0 +1,36 @@
1
+ # -*- coding: utf-8 -*-
2
+ """
3
+ @Author: Eric Santos <ericshantos13@gmail.com>
4
+
5
+ States implementation for the Turing machine.
6
+ """
7
+
8
+
9
+ from typing import Iterator
10
+
11
+
12
+ class States:
13
+ def __init__(self, *states: str, initial_state: str, final_states: set[str]):
14
+ self._states = set(states)
15
+
16
+ if initial_state not in self._states:
17
+ raise ValueError("Initial state must belong to the state set")
18
+
19
+ if not final_states.issubset(self._states):
20
+ raise ValueError("Final states must belong to the state set")
21
+
22
+ self.initial_state = initial_state
23
+ self.current_state = initial_state
24
+ self.final_states = final_states
25
+
26
+ def __contains__(self, state: str) -> bool:
27
+ return state in self._states
28
+
29
+ def __iter__(self) -> Iterator[str]:
30
+ return iter(self._states)
31
+
32
+ def __len__(self)-> int:
33
+ return len(self._states)
34
+
35
+ def __str__(self) -> str:
36
+ return f"Q = {self._states}"
@@ -0,0 +1,54 @@
1
+ # -*- coding: utf-8 -*-
2
+ """
3
+ @Author: Eric Santos <ericshantos13@gmail.com>
4
+
5
+ Tape implementation for the Turing machine.
6
+ """
7
+
8
+
9
+ from typing import Iterator
10
+ from ..alphabet.symbol import Symbol
11
+ from ..alphabet.input_alphabet import Alphabet
12
+
13
+
14
+ class Tape:
15
+
16
+ def __init__(self, input: str, alphabet: Alphabet, blank: Symbol) -> None:
17
+
18
+ self.blank = blank
19
+ self.alphabet = alphabet
20
+
21
+ self._tape: list[Symbol] = [Symbol(c) for c in input]
22
+
23
+ def __getitem__(self, idx: int) -> Symbol:
24
+
25
+ if idx < 0:
26
+ raise IndexError("Head moved left of tape start")
27
+
28
+ if idx >= len(self._tape):
29
+ self._tape.extend([self.blank] * (idx - len(self._tape) + 1))
30
+
31
+ return self._tape[idx]
32
+
33
+ def __setitem__(self, idx: int, value: str) -> None:
34
+
35
+ if value not in self.alphabet:
36
+ raise ValueError(f"Symbol {value} not in alphabet")
37
+
38
+ if idx >= len(self._tape):
39
+ self._tape.extend([self.blank] * (idx - len(self._tape) + 1))
40
+
41
+ self._tape[idx] = value
42
+
43
+ def __str__(self) -> str:
44
+ return "".join(str(s) for s in self._tape)
45
+
46
+ def __len__(self) -> int:
47
+ return len(self._tape)
48
+
49
+ def __iter__(self) -> Iterator[Symbol]:
50
+ yield from self._tape
51
+
52
+ def __repr__(self) -> str:
53
+ return f"Tape({self._tape})"
54
+
@@ -0,0 +1,4 @@
1
+ from .transition_function import TransitionFunction
2
+ from .transition import Transition
3
+
4
+ __all__ = ["TransitionFunction", "Transition"]
@@ -0,0 +1,21 @@
1
+ # -*- coding: utf-8 -*-
2
+ """
3
+ @Author: Eric Santos <ericshantos13@gmail.com>
4
+
5
+ Module for the direction of the transition.
6
+ """
7
+
8
+
9
+ from enum import Enum
10
+ from collections.abc import Iterable
11
+
12
+ class Direction(Enum):
13
+ RIGHT: int = 1
14
+ LEFT: int = -1
15
+
16
+ def move(self, head: int) -> int:
17
+ return head + self.value
18
+
19
+ def __str__(self) -> str:
20
+ return self.name
21
+
@@ -0,0 +1,22 @@
1
+ """
2
+ @Author: Eric Santos <ericshantos13@gmail.com>
3
+
4
+ Transition implementation for the Turing machine.
5
+ """
6
+
7
+
8
+ from dataclasses import dataclass
9
+ from ..alphabet.symbol import Symbol
10
+ from .direction import Direction
11
+
12
+
13
+ @dataclass(frozen=True)
14
+ class Transition:
15
+ state: str
16
+ symbol: Symbol
17
+ new_state: str
18
+ new_symbol: Symbol
19
+ direction: Direction
20
+
21
+ def __str__(self) -> str:
22
+ return f"δ({self.state}, {self.symbol}) → ({self.new_state}, {self.new_symbol}, {self.direction})"
@@ -0,0 +1,37 @@
1
+ """
2
+ @author: Eric Santos <ericshantos13@gmail.com>
3
+
4
+ This module defines the TransitionFunction class, which represents the
5
+ transition function of a finite automaton. It allows adding transitions
6
+ and retrieving transitions based on the current state and input symbol.
7
+ """
8
+
9
+
10
+ from collections.abc import Iterable
11
+ from .transition import Transition
12
+ from ..alphabet import Symbol
13
+
14
+
15
+ class TransitionFunction:
16
+ def __init__(self) -> None:
17
+ self._transitions: dict[tuple[str, Symbol], Transition] = {}
18
+
19
+ def add(self, *transitions: Transition) -> None:
20
+
21
+ for t in transitions:
22
+
23
+ if isinstance(t, Iterable) and not isinstance(t, Transition):
24
+ for tr in t:
25
+ self._transitions[(tr.state, tr.symbol)] = tr
26
+ else:
27
+ self._transitions[(t.state, t.symbol)] = t
28
+
29
+ def __call__(self, state: str, symbol: Symbol) -> Transition | None:
30
+ return self._transitions.get((state, symbol))
31
+
32
+ def __str__(self) -> str:
33
+ return "\n".join(str(t) for t in self._transitions.values())
34
+
35
+ @property
36
+ def conjugates(self) -> dict[tuple[str, str], Transition]:
37
+ return self._transitions