Skip to content

Commit 61cc3b1

Browse files
committed
basic miner loop
1 parent 8f5c19b commit 61cc3b1

16 files changed

+463
-257
lines changed

.github/workflows/main.yml

-2
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,5 @@ jobs:
1818
pip3 install pdoc3
1919
- name: Test
2020
run: nose2 tests -v
21-
- name: Sample
22-
run: python3 sample.py
2321
- name: Docs
2422
run: pdoc3 --html loda

loda/lang/program.py

+15-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
"""Program model and serialization."""
44

5-
from .operation import Operation
5+
from .operation import Operation, Operand
66

77

88
class Program:
@@ -51,3 +51,17 @@ def __str__(self) -> str:
5151
if op.type == Operation.Type.LPB:
5252
indent += 1
5353
return result
54+
55+
def validate(self):
56+
loop_depth = 0
57+
for op in self.operations:
58+
if op.type != Operation.Type.NOP and op.type != Operation.Type.LPE and op.target.type == Operand.Type.CONSTANT:
59+
raise ValueError("target cannot be a constant")
60+
if op.type == Operation.Type.LPB:
61+
loop_depth += 1
62+
elif op.type == Operation.Type.LPE:
63+
if loop_depth == 0:
64+
raise ValueError("unexpected lpe")
65+
loop_depth -= 1
66+
if loop_depth != 0:
67+
raise ValueError("missing lpe")

loda/mine/__init__.py

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
"""Program mining for integer sequences."""
2+
3+
from .miner import Miner

loda/mine/miner.py

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
"""Miner class for finding integer sequence programs."""
2+
3+
from loda.oeis import PrefixIndex, Sequence
4+
from loda.runtime import Evaluator, Interpreter
5+
6+
7+
class Miner:
8+
9+
def __init__(self, sequences: list, interpreter: Interpreter, generator):
10+
self.__index = PrefixIndex(sequences)
11+
self.__interpreter = interpreter
12+
self.__generator = generator
13+
14+
def __call__(self):
15+
program = self.__generator()
16+
evaluator = Evaluator(program, self.__interpreter)
17+
match = self.__index.global_match()
18+
refine = True
19+
try:
20+
while refine:
21+
term = evaluator()
22+
# print(term)
23+
refine = self.__index.refine_match(match, term)
24+
except Exception as e:
25+
print("evaluation error: {}".format(e))
26+
return
27+
28+
ids = self.__index.get_match_ids(match)
29+
for id in ids:
30+
print("Found match for {}".format(Sequence(id)))
31+
print(program)

0 commit comments

Comments
 (0)