-
-
Notifications
You must be signed in to change notification settings - Fork 481
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
57 additions
and
81 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
|
||
|
@@ -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""" | ||
|
@@ -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``. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
|
||
|
@@ -36,6 +36,7 @@ | |
vars = {} | ||
vars_order = [] | ||
|
||
|
||
class SymbolicLogic: | ||
""" | ||
EXAMPLES: | ||
|
@@ -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): | ||
|
@@ -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]: | ||
|
@@ -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. | ||
|
@@ -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: | ||
|
@@ -456,6 +452,7 @@ def eval(toks): | |
raise RuntimeError | ||
return stack[0] | ||
|
||
|
||
def eval_ltor_toks(lrtoks): | ||
r""" | ||
Evaluates the expression contained in ``lrtoks``. | ||
|
@@ -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. | ||
|
@@ -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. | ||
|
@@ -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 | ||
|
@@ -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""" | ||
|
@@ -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``. | ||
|
@@ -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""" | ||
|
@@ -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""" | ||
|
@@ -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""" | ||
|
@@ -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""" | ||
|
@@ -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: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
|
||
|
@@ -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. | ||
|
@@ -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. | ||
|
@@ -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. | ||
|
@@ -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 | ||
|
@@ -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``. | ||
|
@@ -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. | ||
|
@@ -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. | ||
|
Oops, something went wrong.