Skip to content

Commit

Permalink
Fixes to make linter happy.
Browse files Browse the repository at this point in the history
  • Loading branch information
robjsliwa committed Jun 21, 2024
1 parent 202623c commit 1ef4f41
Show file tree
Hide file tree
Showing 11 changed files with 207 additions and 217 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/python-package.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ jobs:

strategy:
matrix:
python-version: [3.10, 3.11, 3.12]
python-version: ['3.10', '3.11', '3.12']

steps:
- name: Check out repository
Expand Down
10 changes: 2 additions & 8 deletions prolog/builtins.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,7 @@ def match(self, other):
return {}

def substitute(self, bindings):
result = Write(*map(
(lambda arg: arg.substitute(bindings)),
self.args
))
result = Write(*map((lambda arg: arg.substitute(bindings)), self.args))
return result

def display(self, stream_writer):
Expand Down Expand Up @@ -142,10 +139,7 @@ def query(self, runtime, bindings={}):
param_bound = list(self.arg.query(runtime))
if param_bound:
param_bound = param_bound[0]
unified = merge_bindings(
self.match(param_bound),
bindings
)
unified = merge_bindings(self.match(param_bound), bindings)
self.substitute(unified).execute(runtime)
else:
self.substitute(bindings).execute(runtime)
Expand Down
79 changes: 46 additions & 33 deletions prolog/interpreter.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
import io
from .types import TermFunction, Variable, Term, merge_bindings, \
Arithmetic, Logic, FALSE, TRUE, CUT
from .types import (
TermFunction,
Variable,
Term,
merge_bindings,
Arithmetic,
Logic,
FALSE,
TRUE,
CUT,
)
from .builtins import Write, Nl, Tab, Fail, Cut, Retract, AssertA, AssertZ


Expand All @@ -21,16 +30,20 @@ def __init__(self, args):
super().__init__(None, *args)

def _is_builtin(self, arg):
if isinstance(arg, Write) or \
isinstance(arg, Nl) or \
isinstance(arg, Tab):
if (
isinstance(arg, Write)
or isinstance(arg, Nl)
or isinstance(arg, Tab)
):
return True
return False

def _is_db_builtin(self, arg):
if isinstance(arg, Retract) or \
isinstance(arg, AssertA) or \
isinstance(arg, AssertZ):
if (
isinstance(arg, Retract)
or isinstance(arg, AssertA)
or isinstance(arg, AssertZ)
):
return True
return False

Expand All @@ -52,10 +65,7 @@ def solutions(index, bindings):
arg = self.args[index]
if self._is_cut(arg):
for item in runtime.execute(arg.substitute(bindings)):
unified = merge_bindings(
arg.match(item),
bindings
)
unified = merge_bindings(arg.match(item), bindings)
if unified is not None:
yield from solutions(index + 1, unified)
yield CUT()
Expand All @@ -69,30 +79,21 @@ def solutions(index, bindings):
yield from solutions(index + 1, bindings)
elif isinstance(arg, Arithmetic):
val = arg.substitute(bindings).evaluate()
unified = merge_bindings(
{arg.var: val},
bindings
)
unified = merge_bindings({arg.var: val}, bindings)
yield from solutions(index + 1, unified)
elif isinstance(arg, Logic):
yield arg.substitute(bindings).evaluate()
else:
for item in runtime.execute(arg.substitute(bindings)):
unified = merge_bindings(
arg.match(item),
bindings
)
unified = merge_bindings(arg.match(item), bindings)
if unified is not None:
yield from solutions(index + 1, unified)

yield from solutions(0, {})

def substitute(self, bindings):
return Conjunction(
map(
(lambda arg: arg.substitute(bindings)),
self.args
)
map((lambda arg: arg.substitute(bindings)), self.args)
)


Expand Down Expand Up @@ -146,20 +147,32 @@ def insert_rule_right(self, entry):
if last_index == -1:
self.rules.append(entry)
else:
self.rules.insert(last_index+1, entry)
self.rules.insert(last_index + 1, entry)

def remove_rule(self, rule):
if isinstance(rule, Term):
rule = Rule(rule, TRUE())
for i, item in enumerate(self.rules):
if rule.head.pred == item.head.pred and \
len(rule.head.args) == len(item.head.args) and \
all([
x.pred == y.pred if isinstance(x, Term) and isinstance(y, Term) # noqa
else x.name == y.name if isinstance(x, Variable) and isinstance(y, Variable) # noqa
else False
for x, y in zip(rule.head.args, item.head.args)
]):
if (
rule.head.pred == item.head.pred
and len(rule.head.args) == len(item.head.args)
and all(
[
(
x.pred == y.pred
if isinstance(x, Term)
and isinstance(y, Term) # noqa
else (
x.name == y.name
if isinstance(x, Variable)
and isinstance(y, Variable) # noqa
else False
)
)
for x, y in zip(rule.head.args, item.head.args)
]
)
):
self.rules.pop(i)
break

Expand Down
5 changes: 4 additions & 1 deletion prolog/logic_interpreter.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,15 @@ class LogicInterpreter(Visitor):
This class walks the expression tree and evaluates returning
the computed value.
"""

def _evaluate_expr(self, expr):
return expr.accept(self)

def _compute_binary_operand(self, left, operand, right):
if type(left) != type(right):
raise InterpreterError(f'left {left} and right {right} operand must have the same type') # noqa
raise InterpreterError(
f'left {left} and right {right} operand must have the same type'
) # noqa
if operand == '==':
return left.equal(right)
elif operand == '=/':
Expand Down
5 changes: 4 additions & 1 deletion prolog/math_interpreter.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,15 @@ class MathInterpreter(Visitor):
This class walks the expression tree and evaluates returning
the computed value.
"""

def _evaluate_expr(self, expr):
return expr.accept(self)

def _compute_binary_operand(self, left, operand, right):
if type(left) != type(right):
raise InterpreterError(f'left {left} and right {right} operand must have the same type') # noqa
raise InterpreterError(
f'left {left} and right {right} operand must have the same type'
) # noqa
if operand == '*':
return left.multiply(right)
elif operand == '/':
Expand Down
Loading

0 comments on commit 1ef4f41

Please sign in to comment.