Skip to content

Commit

Permalink
Add LinRecMachine base class
Browse files Browse the repository at this point in the history
  • Loading branch information
nickdrozd committed Nov 17, 2023
1 parent 6f42d8f commit 2ad6445
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 8 deletions.
2 changes: 1 addition & 1 deletion test/test_tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
if TYPE_CHECKING:
from collections.abc import Iterator

from tm.machine import BasicMachine
BasicMachine = Machine | LooseLinRecMachine

Q = Queue[str]

Expand Down
10 changes: 9 additions & 1 deletion test/test_turing.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ class TuringTest(TestCase):

def assert_marks(self, marks: int):
self.assertEqual(
self.machine.marks,
self.machine.marks, # type: ignore[union-attr]
marks)

def assert_steps(self, steps: int):
Expand Down Expand Up @@ -301,6 +301,8 @@ def _test_simple_terminate(

self.assert_steps(steps)

assert isinstance(self.machine, QuickMachine)

self.assertEqual(
steps,
self.machine.simple_termination)
Expand Down Expand Up @@ -588,6 +590,8 @@ def _test_macro_cycles(self, prog_data: MacroCycles):
prover = False,
)

assert isinstance(self.machine, QuickMachine)

self.assertEqual(
cycles,
None
Expand Down Expand Up @@ -619,6 +623,8 @@ def _test_block_macro_steps(
prover = False,
)

assert isinstance(self.machine, QuickMachine)

assert isinstance(
term := self.machine.simple_termination,
int)
Expand Down Expand Up @@ -801,6 +807,8 @@ def test_macro_multi_backsymbol(self):
prog,
backsym = [back] * back)

assert isinstance(self.machine, QuickMachine)

self.assertIsNotNone(
self.machine.simple_termination)

Expand Down
5 changes: 2 additions & 3 deletions test/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,13 @@

from typing import Self, TYPE_CHECKING

from tm.machine import BasicMachine
from tm.lin_rec import BeepHistory, HeadTape
from tm.lin_rec import BeepHistory, HeadTape, LinRecMachine

if TYPE_CHECKING:
from tm.lin_rec import State, Slot, Tapes


class LinRecSampler(BasicMachine):
class LinRecSampler(LinRecMachine):
history: BeepHistory

def run(
Expand Down
30 changes: 27 additions & 3 deletions tm/lin_rec.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@
from collections import defaultdict
from dataclasses import dataclass, field

from tm.machine import BasicMachine
from tm.parse import tcompile

if TYPE_CHECKING:
from typing import Self

from tm.parse import Color, Shift, State, Slot, GetInstr
from tm.machine import Undfnd

RecRes = tuple[int, int]
LinRec = tuple[int | None, int]
Expand Down Expand Up @@ -281,7 +282,30 @@ def calculate_beeps(
}


class StrictLinRecMachine(BasicMachine):
class LinRecMachine:
program: str
comp: GetInstr

tape: HeadTape
steps: int
cycles: int

blanks: dict[State, int]

halted: int | None = None
spnout: int | None = None
xlimit: int | None = None

undfnd: Undfnd | None = None

infrul: bool | None = None

def __init__(self, prog: str):
self.program = prog
self.comp = tcompile(prog)


class StrictLinRecMachine(LinRecMachine):
history: BeepHistory

linrec: LinRec | None = None
Expand Down Expand Up @@ -357,7 +381,7 @@ def check_rec(self, step: int, slot: Slot) -> RecRes | None:
return result


class LooseLinRecMachine(BasicMachine):
class LooseLinRecMachine(LinRecMachine):
# pylint: disable = while-used, too-many-locals, line-too-long
def run(self, sim_lim: int) -> Self: # no-cover
self.blanks = {}
Expand Down

0 comments on commit 2ad6445

Please sign in to comment.