Skip to content

Commit

Permalink
sagemathgh-37154: minor details in logic folder
Browse files Browse the repository at this point in the history
    
This is just fixing some suggestions of ruff and pycodestyle in the
logic folder.

### 📝 Checklist

- [x] The title is concise, informative, and self-explanatory.
- [x] The description explains in detail what this PR is about.
    
URL: sagemath#37154
Reported by: Frédéric Chapoton
Reviewer(s): Travis Scrimshaw
  • Loading branch information
Release Manager committed Jan 24, 2024
2 parents f9083b5 + 7183f88 commit 0940183
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 81 deletions.
11 changes: 6 additions & 5 deletions src/sage/logic/booleval.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,15 @@
sage: booleval.eval_formula(t, d)
False
"""
#*****************************************************************************
# ****************************************************************************
# Copyright (C) 2006 Chris Gorecki <[email protected]>
# Copyright (C) 2013 Paul Scurek <[email protected]>
#
# Distributed under the terms of the GNU General Public License (GPL)
# as published by the Free Software Foundation; either version 2 of
# the License, or (at your option) any later version.
# http://www.gnu.org/licenses/
#*****************************************************************************
# https://www.gnu.org/licenses/
# ****************************************************************************

from . import logicparser

Expand Down Expand Up @@ -72,8 +72,8 @@ def eval_formula(tree, vdict):
"""
global __vars
__vars = vdict
b = logicparser.apply_func(tree, eval_f)
return b
return logicparser.apply_func(tree, eval_f)


def eval_f(tree):
r"""
Expand Down Expand Up @@ -104,6 +104,7 @@ def eval_f(tree):
"""
return eval_op(tree[0], tree[1], tree[2])


def eval_op(op, lv, rv):
r"""
Evaluate ``lv`` and ``rv`` according to the operator ``op``.
Expand Down
16 changes: 5 additions & 11 deletions src/sage/logic/boolformula.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@
('->', '\\rightarrow ')]


class BooleanFormula():
class BooleanFormula:
"""
Boolean formulas.
Expand Down Expand Up @@ -673,10 +673,7 @@ def is_satisfiable(self):
False
"""
table = self.truthtable().get_table_list()
for row in table[1:]:
if row[-1] is True:
return True
return False
return any(row[-1] is True for row in table[1:])

def is_tautology(self):
r"""
Expand Down Expand Up @@ -1309,11 +1306,11 @@ def reduce_op(self, tree):
if tree[0] == '<->':
# parse tree for (~tree[1]|tree[2])&(~tree[2]|tree[1])
new_tree = ['&', ['|', ['~', tree[1], None], tree[2]],
['|', ['~', tree[2], None], tree[1]]]
['|', ['~', tree[2], None], tree[1]]]
elif tree[0] == '^':
# parse tree for (tree[1]|tree[2])&~(tree[1]&tree[2])
new_tree = ['&', ['|', tree[1], tree[2]],
['~', ['&', tree[1], tree[2]], None]]
['~', ['&', tree[1], tree[2]], None]]
elif tree[0] == '->':
# parse tree for ~tree[1]|tree[2]
new_tree = ['|', ['~', tree[1], None], tree[2]]
Expand Down Expand Up @@ -1354,10 +1351,7 @@ def dist_not(self, tree):
if tree[0] == '~' and isinstance(tree[1], list):
op = tree[1][0]
if op != '~':
if op == '&':
op = '|'
else:
op = '&'
op = '|' if op == '&' else '&'
new_tree = [op, ['~', tree[1][1], None], ['~', tree[1][2], None]]
return logicparser.apply_func(new_tree, self.dist_not)
else:
Expand Down
82 changes: 28 additions & 54 deletions src/sage/logic/logic.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,16 @@
- Paul Scurek (2013-08-03): updated docstring formatting
"""
#*****************************************************************************
# ****************************************************************************
# Copyright (C) 2007 Chris Gorecki <[email protected]>
# Copyright (C) 2007 William Stein <[email protected]>
# Copyright (C) 2013 Paul Scurek <[email protected]>
#
# Distributed under the terms of the GNU General Public License (GPL)
# as published by the Free Software Foundation; either version 2 of
# the License, or (at your option) any later version.
# http://www.gnu.org/licenses/
#*****************************************************************************
# https://www.gnu.org/licenses/
# ****************************************************************************

import string

Expand All @@ -36,6 +36,7 @@
vars = {}
vars_order = []


class SymbolicLogic:
"""
EXAMPLES:
Expand Down Expand Up @@ -184,7 +185,7 @@ def truthtable(self, statement, start=0, end=-1):
end = 2 ** len(vars)
table = [statement]
keys = vars_order
for i in range(start,end):
for i in range(start, end):
j = 0
row = []
for key in reversed(keys):
Expand Down Expand Up @@ -270,10 +271,7 @@ def print_table(self, table):
line = s = ""
i = 0
for e in row:
if e == 'True':
j = 2
else:
j = 1
j = 2 if e == 'True' else 1
s = e + ' ' * j
if i < len(vars_len):
while len(s) <= vars_len[i]:
Expand Down Expand Up @@ -370,6 +368,7 @@ def prove(self, statement):
"""
raise NotImplementedError


def get_bit(x, c):
r"""
Determine if bit ``c`` of the number ``x`` is 1.
Expand Down Expand Up @@ -402,10 +401,7 @@ def get_bit(x, c):
"""
bits = []
while x > 0:
if x % 2 == 0:
b = 'False'
else:
b = 'True'
b = 'False' if x % 2 == 0 else 'True'
x = x // 2
bits.append(b)
if c > len(bits) - 1:
Expand Down Expand Up @@ -456,6 +452,7 @@ def eval(toks):
raise RuntimeError
return stack[0]


def eval_ltor_toks(lrtoks):
r"""
Evaluates the expression contained in ``lrtoks``.
Expand Down Expand Up @@ -494,6 +491,7 @@ def eval_ltor_toks(lrtoks):
raise RuntimeError
return lrtoks[0]


def reduce_bins(lrtoks):
r"""
Evaluate ``lrtoks`` to a single boolean value.
Expand Down Expand Up @@ -531,6 +529,7 @@ def reduce_bins(lrtoks):
reduce_bins(lrtoks)
i += 1


def reduce_monos(lrtoks):
r"""
Replace monotonic operator/variable pairs with a boolean value.
Expand Down Expand Up @@ -566,6 +565,7 @@ def reduce_monos(lrtoks):
del lrtoks[i + 1]
i += 1


def eval_mon_op(args):
r"""
Return a boolean value based on the truth table of the operator
Expand All @@ -592,21 +592,17 @@ def eval_mon_op(args):
sage: log = SymbolicLogic()
sage: s = log.statement("!(a&b)|!a"); s
[['OPAREN', 'NOT', 'OPAREN', 'a', 'AND', 'b', 'CPAREN', 'OR', 'NOT', 'a', 'CPAREN'],
[['OPAREN', 'NOT', 'OPAREN', 'a', 'AND', 'b', 'CPAREN', 'OR',
'NOT', 'a', 'CPAREN'],
{'a': 'False', 'b': 'False'},
['a', 'b']]
sage: sage.logic.logic.eval_mon_op(['NOT', 'a'])
'True'
"""
if args[1] != 'True' and args[1] != 'False':
val = vars[args[1]]
else:
val = args[1]
val = vars[args[1]] if args[1] != 'True' and args[1] != 'False' else args[1]

return 'False' if val == 'True' else 'True'

if val == 'True':
return 'False'
else:
return 'True'

def eval_bin_op(args):
r"""
Expand Down Expand Up @@ -660,6 +656,7 @@ def eval_bin_op(args):
elif args[1] == 'IFF':
return eval_iff_op(lval, rval)


def eval_and_op(lval, rval):
r"""
Apply the 'and' operator to ``lval`` and ``rval``.
Expand Down Expand Up @@ -691,14 +688,8 @@ def eval_and_op(lval, rval):
sage: sage.logic.logic.eval_and_op('True', 'True')
'True'
"""
if lval == 'False' and rval == 'False':
return 'False'
elif lval == 'False' and rval == 'True':
return 'False'
elif lval == 'True' and rval == 'False':
return 'False'
elif lval == 'True' and rval == 'True':
return 'True'
return 'True' if (lval == 'True' == rval) else 'False'


def eval_or_op(lval, rval):
r"""
Expand Down Expand Up @@ -731,14 +722,8 @@ def eval_or_op(lval, rval):
sage: sage.logic.logic.eval_or_op('True', 'True')
'True'
"""
if lval == 'False' and rval == 'False':
return 'False'
elif lval == 'False' and rval == 'True':
return 'True'
elif lval == 'True' and rval == 'False':
return 'True'
elif lval == 'True' and rval == 'True':
return 'True'
return 'True' if (lval == 'True' or rval == 'True') else 'False'


def eval_ifthen_op(lval, rval):
r"""
Expand Down Expand Up @@ -772,14 +757,8 @@ def eval_ifthen_op(lval, rval):
sage: sage.logic.logic.eval_ifthen_op('True', 'True')
'True'
"""
if lval == 'False' and rval == 'False':
return 'True'
elif lval == 'False' and rval == 'True':
return 'True'
elif lval == 'True' and rval == 'False':
return 'False'
elif lval == 'True' and rval == 'True':
return 'True'
return 'False' if (lval == 'True' and rval == 'False') else 'True'


def eval_iff_op(lval, rval):
r"""
Expand Down Expand Up @@ -813,14 +792,8 @@ def eval_iff_op(lval, rval):
sage: sage.logic.logic.eval_iff_op('True', 'True')
'True'
"""
if lval == 'False' and rval == 'False':
return 'True'
elif lval == 'False' and rval == 'True':
return 'False'
elif lval == 'True' and rval == 'False':
return 'False'
elif lval == 'True' and rval == 'True':
return 'True'
return 'True' if (lval == rval) else 'False'


def tokenize(s, toks):
r"""
Expand Down Expand Up @@ -887,7 +860,8 @@ def tokenize(s, toks):
if tok[0] not in string.ascii_letters:
valid = 0
for c in tok:
if c not in string.ascii_letters and c not in string.digits and c != '_':
if not (c in string.ascii_letters
or c in string.digits or c == '_'):
valid = 0

if valid == 1:
Expand Down
11 changes: 9 additions & 2 deletions src/sage/logic/logicparser.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,15 +75,15 @@
sage: logicparser.tree_parse(r, polish = True)
['|', ['~', ['~', 'a']], 'b']
"""
#*****************************************************************************
# ****************************************************************************
# Copyright (C) 2007 Chris Gorecki <[email protected]>
# Copyright (C) 2013 Paul Scurek <[email protected]>
#
# Distributed under the terms of the GNU General Public License (GPL)
# as published by the Free Software Foundation; either version 2 of
# the License, or (at your option) any later version.
# https://www.gnu.org/licenses/
#*****************************************************************************
# ****************************************************************************

import string

Expand Down Expand Up @@ -164,6 +164,7 @@ def polish_parse(s):
return vars_order
return tree


def get_trees(*statements):
r"""
Return the full syntax parse trees of the statements.
Expand Down Expand Up @@ -276,6 +277,7 @@ def recover_formula(prefix_tree):
return formula
return formula[1:-1]


def recover_formula_internal(prefix_tree):
r"""
Recover the formula from a parse tree in prefix form.
Expand Down Expand Up @@ -385,6 +387,7 @@ def prefix_to_infix(prefix_tree):
raise TypeError("the input must be a parse tree as a list")
return apply_func(prefix_tree, to_infix_internal)


def to_infix_internal(prefix_tree):
r"""
Convert a simple parse tree from prefix form to infix form.
Expand Down Expand Up @@ -434,6 +437,7 @@ def to_infix_internal(prefix_tree):
return [prefix_tree[1], prefix_tree[0], prefix_tree[2]]
return prefix_tree


def tokenize(s):
r"""
Return the tokens and the distinct variables appearing in a boolean
Expand Down Expand Up @@ -516,6 +520,7 @@ def tokenize(s):
toks.append(')')
return toks, vars_order


def tree_parse(toks, polish=False):
r"""
Return a parse tree from the tokens in ``toks``.
Expand Down Expand Up @@ -572,6 +577,7 @@ def tree_parse(toks, polish=False):
stack.append(branch)
return stack[0]


def parse_ltor(toks, n=0, polish=False):
r"""
Return a parse tree from ``toks``, where each token in ``toks`` is atomic.
Expand Down Expand Up @@ -657,6 +663,7 @@ def parse_ltor(toks, n=0, polish=False):
raise SyntaxError
return toks[0]


def apply_func(tree, func):
r"""
Apply ``func`` to each node of ``tree``, and return a new parse tree.
Expand Down
Loading

0 comments on commit 0940183

Please sign in to comment.