Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemath
GitHub Repository: sagemath/sagelib
Path: blob/master/sage/logic/booleval.py
4078 views
1
r"""
2
Evaluate Boolean Formulas
3
4
Perform boolean evaluation of boolean formulas.
5
6
AUTHORS:
7
8
- Chris Gorecki
9
10
EXAMPLES::
11
12
sage: import sage.logic.booleval as booleval
13
sage: t = ['|', ['&', 'a', 'b'], ['&', 'a', 'c']]
14
sage: d = {'a' : True, 'b' : False, 'c' : True}
15
sage: booleval.eval_formula(t, d)
16
True
17
sage: d['a'] = False
18
sage: booleval.eval_formula(t, d)
19
False
20
21
22
Classes and functions
23
=====================
24
"""
25
26
import logicparser
27
28
# dictionary containing variable keys and boolean values
29
__vars = {}
30
31
def eval_formula(tree, vdict):
32
r"""
33
Evaluates the tree using the boolean values contained in dictionary
34
and returns a single boolean value.
35
36
INPUT:
37
38
- ``tree`` -- a list of three elements corresponding to a branch of a
39
parse tree.
40
41
- ``vdict`` -- a dictionary containing variable keys and boolean values.
42
43
OUTPUT:
44
45
- Returns the boolean evaluation of a boolean formula.
46
47
EXAMPLES::
48
49
sage: import sage.logic.booleval as booleval
50
sage: t = ['|', ['&', 'a', 'b'], ['&', 'a', 'c']]
51
sage: d = {'a' : True, 'b' : False, 'c' : True}
52
sage: booleval.eval_formula(t, d)
53
True
54
sage: d['a'] = False
55
sage: booleval.eval_formula(t, d)
56
False
57
"""
58
global __vars
59
__vars = vdict
60
b = logicparser.apply_func(tree, eval_f)
61
return b
62
63
def eval_f(tree):
64
r"""
65
This function can be applied to a parse tree to evaluate it.
66
67
INPUT:
68
69
- ``tree`` -- a list of three elements corresponding to a branch of a
70
parse tree.
71
72
OUTPUT:
73
74
- Returns a boolean evaluation of the tree.
75
76
EXAMPLES::
77
78
sage: import sage.logic.booleval as booleval
79
sage: booleval.eval_f(['&', True, False])
80
False
81
sage: booleval.eval_f(['^', True, True])
82
False
83
sage: booleval.eval_f(['|', False, True])
84
True
85
"""
86
return eval_op(tree[0], tree[1], tree[2])
87
88
def eval_op(op, lv, rv):
89
r"""
90
This function evaluates ``lv`` and ``rv`` according to the operator ``op``.
91
92
INPUT:
93
94
- ``op`` -- a string or character representing a boolean operator.
95
96
- ``lv`` -- a boolean or variable.
97
98
- ``rv`` -- a boolean or variable.
99
100
OUTPUT:
101
102
- Returns the evaluation of ``lv op rv``.
103
104
EXAMPLES::
105
106
sage: import sage.logic.booleval as booleval
107
sage: booleval.eval_op('&', True, False)
108
False
109
sage: booleval.eval_op('^', True, True)
110
False
111
sage: booleval.eval_op('|', False, True)
112
True
113
"""
114
lval = rval = None
115
if lv == False:
116
lval = False
117
elif lv == True:
118
lval = True
119
elif lv is not None:
120
lval = __vars[lv]
121
122
if rv == False:
123
rval = False
124
elif rv == True:
125
rval = True
126
elif rv is not None:
127
rval = __vars[rv]
128
129
if op == '~':
130
return not lval
131
elif op == '&':
132
return lval and rval
133
elif op == '|':
134
return lval or rval
135
elif op == '^':
136
return lval ^ rval
137
elif op == '->':
138
return (not lval) or rval
139
elif op == '<->':
140
return (not lval or rval) and (not rval or lval)
141
else: # one variable
142
return __vars[op]
143
144