Skip to content

Commit 7183f88

Browse files
committed
minor details in logic folder
1 parent 3dd953c commit 7183f88

File tree

6 files changed

+57
-81
lines changed

6 files changed

+57
-81
lines changed

src/sage/logic/booleval.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,15 @@
2323
sage: booleval.eval_formula(t, d)
2424
False
2525
"""
26-
#*****************************************************************************
26+
# ****************************************************************************
2727
# Copyright (C) 2006 Chris Gorecki <[email protected]>
2828
# Copyright (C) 2013 Paul Scurek <[email protected]>
2929
#
3030
# Distributed under the terms of the GNU General Public License (GPL)
3131
# as published by the Free Software Foundation; either version 2 of
3232
# the License, or (at your option) any later version.
33-
# http://www.gnu.org/licenses/
34-
#*****************************************************************************
33+
# https://www.gnu.org/licenses/
34+
# ****************************************************************************
3535

3636
from . import logicparser
3737

@@ -72,8 +72,8 @@ def eval_formula(tree, vdict):
7272
"""
7373
global __vars
7474
__vars = vdict
75-
b = logicparser.apply_func(tree, eval_f)
76-
return b
75+
return logicparser.apply_func(tree, eval_f)
76+
7777

7878
def eval_f(tree):
7979
r"""
@@ -104,6 +104,7 @@ def eval_f(tree):
104104
"""
105105
return eval_op(tree[0], tree[1], tree[2])
106106

107+
107108
def eval_op(op, lv, rv):
108109
r"""
109110
Evaluate ``lv`` and ``rv`` according to the operator ``op``.

src/sage/logic/boolformula.py

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@
148148
('->', '\\rightarrow ')]
149149

150150

151-
class BooleanFormula():
151+
class BooleanFormula:
152152
"""
153153
Boolean formulas.
154154
@@ -673,10 +673,7 @@ def is_satisfiable(self):
673673
False
674674
"""
675675
table = self.truthtable().get_table_list()
676-
for row in table[1:]:
677-
if row[-1] is True:
678-
return True
679-
return False
676+
return any(row[-1] is True for row in table[1:])
680677

681678
def is_tautology(self):
682679
r"""
@@ -1309,11 +1306,11 @@ def reduce_op(self, tree):
13091306
if tree[0] == '<->':
13101307
# parse tree for (~tree[1]|tree[2])&(~tree[2]|tree[1])
13111308
new_tree = ['&', ['|', ['~', tree[1], None], tree[2]],
1312-
['|', ['~', tree[2], None], tree[1]]]
1309+
['|', ['~', tree[2], None], tree[1]]]
13131310
elif tree[0] == '^':
13141311
# parse tree for (tree[1]|tree[2])&~(tree[1]&tree[2])
13151312
new_tree = ['&', ['|', tree[1], tree[2]],
1316-
['~', ['&', tree[1], tree[2]], None]]
1313+
['~', ['&', tree[1], tree[2]], None]]
13171314
elif tree[0] == '->':
13181315
# parse tree for ~tree[1]|tree[2]
13191316
new_tree = ['|', ['~', tree[1], None], tree[2]]
@@ -1354,10 +1351,7 @@ def dist_not(self, tree):
13541351
if tree[0] == '~' and isinstance(tree[1], list):
13551352
op = tree[1][0]
13561353
if op != '~':
1357-
if op == '&':
1358-
op = '|'
1359-
else:
1360-
op = '&'
1354+
op = '|' if op == '&' else '&'
13611355
new_tree = [op, ['~', tree[1][1], None], ['~', tree[1][2], None]]
13621356
return logicparser.apply_func(new_tree, self.dist_not)
13631357
else:

src/sage/logic/logic.py

Lines changed: 28 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,16 @@
1515
1616
- Paul Scurek (2013-08-03): updated docstring formatting
1717
"""
18-
#*****************************************************************************
18+
# ****************************************************************************
1919
# Copyright (C) 2007 Chris Gorecki <[email protected]>
2020
# Copyright (C) 2007 William Stein <[email protected]>
2121
# Copyright (C) 2013 Paul Scurek <[email protected]>
2222
#
2323
# Distributed under the terms of the GNU General Public License (GPL)
2424
# as published by the Free Software Foundation; either version 2 of
2525
# the License, or (at your option) any later version.
26-
# http://www.gnu.org/licenses/
27-
#*****************************************************************************
26+
# https://www.gnu.org/licenses/
27+
# ****************************************************************************
2828

2929
import string
3030

@@ -36,6 +36,7 @@
3636
vars = {}
3737
vars_order = []
3838

39+
3940
class SymbolicLogic:
4041
"""
4142
EXAMPLES:
@@ -184,7 +185,7 @@ def truthtable(self, statement, start=0, end=-1):
184185
end = 2 ** len(vars)
185186
table = [statement]
186187
keys = vars_order
187-
for i in range(start,end):
188+
for i in range(start, end):
188189
j = 0
189190
row = []
190191
for key in reversed(keys):
@@ -270,10 +271,7 @@ def print_table(self, table):
270271
line = s = ""
271272
i = 0
272273
for e in row:
273-
if e == 'True':
274-
j = 2
275-
else:
276-
j = 1
274+
j = 2 if e == 'True' else 1
277275
s = e + ' ' * j
278276
if i < len(vars_len):
279277
while len(s) <= vars_len[i]:
@@ -370,6 +368,7 @@ def prove(self, statement):
370368
"""
371369
raise NotImplementedError
372370

371+
373372
def get_bit(x, c):
374373
r"""
375374
Determine if bit ``c`` of the number ``x`` is 1.
@@ -402,10 +401,7 @@ def get_bit(x, c):
402401
"""
403402
bits = []
404403
while x > 0:
405-
if x % 2 == 0:
406-
b = 'False'
407-
else:
408-
b = 'True'
404+
b = 'False' if x % 2 == 0 else 'True'
409405
x = x // 2
410406
bits.append(b)
411407
if c > len(bits) - 1:
@@ -456,6 +452,7 @@ def eval(toks):
456452
raise RuntimeError
457453
return stack[0]
458454

455+
459456
def eval_ltor_toks(lrtoks):
460457
r"""
461458
Evaluates the expression contained in ``lrtoks``.
@@ -494,6 +491,7 @@ def eval_ltor_toks(lrtoks):
494491
raise RuntimeError
495492
return lrtoks[0]
496493

494+
497495
def reduce_bins(lrtoks):
498496
r"""
499497
Evaluate ``lrtoks`` to a single boolean value.
@@ -531,6 +529,7 @@ def reduce_bins(lrtoks):
531529
reduce_bins(lrtoks)
532530
i += 1
533531

532+
534533
def reduce_monos(lrtoks):
535534
r"""
536535
Replace monotonic operator/variable pairs with a boolean value.
@@ -566,6 +565,7 @@ def reduce_monos(lrtoks):
566565
del lrtoks[i + 1]
567566
i += 1
568567

568+
569569
def eval_mon_op(args):
570570
r"""
571571
Return a boolean value based on the truth table of the operator
@@ -592,21 +592,17 @@ def eval_mon_op(args):
592592
593593
sage: log = SymbolicLogic()
594594
sage: s = log.statement("!(a&b)|!a"); s
595-
[['OPAREN', 'NOT', 'OPAREN', 'a', 'AND', 'b', 'CPAREN', 'OR', 'NOT', 'a', 'CPAREN'],
595+
[['OPAREN', 'NOT', 'OPAREN', 'a', 'AND', 'b', 'CPAREN', 'OR',
596+
'NOT', 'a', 'CPAREN'],
596597
{'a': 'False', 'b': 'False'},
597598
['a', 'b']]
598599
sage: sage.logic.logic.eval_mon_op(['NOT', 'a'])
599600
'True'
600601
"""
601-
if args[1] != 'True' and args[1] != 'False':
602-
val = vars[args[1]]
603-
else:
604-
val = args[1]
602+
val = vars[args[1]] if args[1] != 'True' and args[1] != 'False' else args[1]
603+
604+
return 'False' if val == 'True' else 'True'
605605

606-
if val == 'True':
607-
return 'False'
608-
else:
609-
return 'True'
610606

611607
def eval_bin_op(args):
612608
r"""
@@ -660,6 +656,7 @@ def eval_bin_op(args):
660656
elif args[1] == 'IFF':
661657
return eval_iff_op(lval, rval)
662658

659+
663660
def eval_and_op(lval, rval):
664661
r"""
665662
Apply the 'and' operator to ``lval`` and ``rval``.
@@ -691,14 +688,8 @@ def eval_and_op(lval, rval):
691688
sage: sage.logic.logic.eval_and_op('True', 'True')
692689
'True'
693690
"""
694-
if lval == 'False' and rval == 'False':
695-
return 'False'
696-
elif lval == 'False' and rval == 'True':
697-
return 'False'
698-
elif lval == 'True' and rval == 'False':
699-
return 'False'
700-
elif lval == 'True' and rval == 'True':
701-
return 'True'
691+
return 'True' if (lval == 'True' == rval) else 'False'
692+
702693

703694
def eval_or_op(lval, rval):
704695
r"""
@@ -731,14 +722,8 @@ def eval_or_op(lval, rval):
731722
sage: sage.logic.logic.eval_or_op('True', 'True')
732723
'True'
733724
"""
734-
if lval == 'False' and rval == 'False':
735-
return 'False'
736-
elif lval == 'False' and rval == 'True':
737-
return 'True'
738-
elif lval == 'True' and rval == 'False':
739-
return 'True'
740-
elif lval == 'True' and rval == 'True':
741-
return 'True'
725+
return 'True' if (lval == 'True' or rval == 'True') else 'False'
726+
742727

743728
def eval_ifthen_op(lval, rval):
744729
r"""
@@ -772,14 +757,8 @@ def eval_ifthen_op(lval, rval):
772757
sage: sage.logic.logic.eval_ifthen_op('True', 'True')
773758
'True'
774759
"""
775-
if lval == 'False' and rval == 'False':
776-
return 'True'
777-
elif lval == 'False' and rval == 'True':
778-
return 'True'
779-
elif lval == 'True' and rval == 'False':
780-
return 'False'
781-
elif lval == 'True' and rval == 'True':
782-
return 'True'
760+
return 'False' if (lval == 'True' and rval == 'False') else 'True'
761+
783762

784763
def eval_iff_op(lval, rval):
785764
r"""
@@ -813,14 +792,8 @@ def eval_iff_op(lval, rval):
813792
sage: sage.logic.logic.eval_iff_op('True', 'True')
814793
'True'
815794
"""
816-
if lval == 'False' and rval == 'False':
817-
return 'True'
818-
elif lval == 'False' and rval == 'True':
819-
return 'False'
820-
elif lval == 'True' and rval == 'False':
821-
return 'False'
822-
elif lval == 'True' and rval == 'True':
823-
return 'True'
795+
return 'True' if (lval == rval) else 'False'
796+
824797

825798
def tokenize(s, toks):
826799
r"""
@@ -887,7 +860,8 @@ def tokenize(s, toks):
887860
if tok[0] not in string.ascii_letters:
888861
valid = 0
889862
for c in tok:
890-
if c not in string.ascii_letters and c not in string.digits and c != '_':
863+
if not (c in string.ascii_letters
864+
or c in string.digits or c == '_'):
891865
valid = 0
892866

893867
if valid == 1:

src/sage/logic/logicparser.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,15 +75,15 @@
7575
sage: logicparser.tree_parse(r, polish = True)
7676
['|', ['~', ['~', 'a']], 'b']
7777
"""
78-
#*****************************************************************************
78+
# ****************************************************************************
7979
# Copyright (C) 2007 Chris Gorecki <[email protected]>
8080
# Copyright (C) 2013 Paul Scurek <[email protected]>
8181
#
8282
# Distributed under the terms of the GNU General Public License (GPL)
8383
# as published by the Free Software Foundation; either version 2 of
8484
# the License, or (at your option) any later version.
8585
# https://www.gnu.org/licenses/
86-
#*****************************************************************************
86+
# ****************************************************************************
8787

8888
import string
8989

@@ -164,6 +164,7 @@ def polish_parse(s):
164164
return vars_order
165165
return tree
166166

167+
167168
def get_trees(*statements):
168169
r"""
169170
Return the full syntax parse trees of the statements.
@@ -276,6 +277,7 @@ def recover_formula(prefix_tree):
276277
return formula
277278
return formula[1:-1]
278279

280+
279281
def recover_formula_internal(prefix_tree):
280282
r"""
281283
Recover the formula from a parse tree in prefix form.
@@ -385,6 +387,7 @@ def prefix_to_infix(prefix_tree):
385387
raise TypeError("the input must be a parse tree as a list")
386388
return apply_func(prefix_tree, to_infix_internal)
387389

390+
388391
def to_infix_internal(prefix_tree):
389392
r"""
390393
Convert a simple parse tree from prefix form to infix form.
@@ -434,6 +437,7 @@ def to_infix_internal(prefix_tree):
434437
return [prefix_tree[1], prefix_tree[0], prefix_tree[2]]
435438
return prefix_tree
436439

440+
437441
def tokenize(s):
438442
r"""
439443
Return the tokens and the distinct variables appearing in a boolean
@@ -516,6 +520,7 @@ def tokenize(s):
516520
toks.append(')')
517521
return toks, vars_order
518522

523+
519524
def tree_parse(toks, polish=False):
520525
r"""
521526
Return a parse tree from the tokens in ``toks``.
@@ -572,6 +577,7 @@ def tree_parse(toks, polish=False):
572577
stack.append(branch)
573578
return stack[0]
574579

580+
575581
def parse_ltor(toks, n=0, polish=False):
576582
r"""
577583
Return a parse tree from ``toks``, where each token in ``toks`` is atomic.
@@ -657,6 +663,7 @@ def parse_ltor(toks, n=0, polish=False):
657663
raise SyntaxError
658664
return toks[0]
659665

666+
660667
def apply_func(tree, func):
661668
r"""
662669
Apply ``func`` to each node of ``tree``, and return a new parse tree.

0 commit comments

Comments
 (0)