Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemath
GitHub Repository: sagemath/sagesmc
Path: blob/master/src/sage/logic/booleval.py
8817 views
1
r"""
2
Module used to evaluate boolean formulas
3
4
AUTHORS:
5
6
- Chris Gorecki (2006): initial version
7
8
- Paul Scurek (2013-08-05): updated docstring formatting
9
10
EXAMPLES:
11
12
We can assign values to the variables and evaluate a formula::
13
14
sage: import sage.logic.booleval as booleval
15
sage: t = ['|', ['&', 'a', 'b'], ['&', 'a', 'c']]
16
sage: d = {'a' : True, 'b' : False, 'c' : True}
17
sage: booleval.eval_formula(t, d)
18
True
19
20
We can change our assignment of values by modifying the dictionary::
21
22
sage: d['a'] = False
23
sage: booleval.eval_formula(t, d)
24
False
25
"""
26
#*****************************************************************************
27
# Copyright (C) 2006 Chris Gorecki <[email protected]>
28
# Copyright (C) 2013 Paul Scurek <[email protected]>
29
#
30
# Distributed under the terms of the GNU General Public License (GPL)
31
# as published by the Free Software Foundation; either version 2 of
32
# the License, or (at your option) any later version.
33
# http://www.gnu.org/licenses/
34
#*****************************************************************************
35
36
import logicparser
37
38
# dictionary containing variable keys and boolean values
39
__vars = {}
40
41
42
def eval_formula(tree, vdict):
43
r"""
44
Evaluate the tree and return a boolean value.
45
46
INPUT:
47
48
- ``tree`` -- a list of three elements corresponding to a branch of a
49
parse tree.
50
51
- ``vdict`` -- a dictionary containing variable keys and boolean values.
52
53
OUTPUT:
54
55
The result of the evaluation as a boolean value
56
57
EXAMPLES:
58
59
This example illustrates evaluating a boolean formula.
60
61
::
62
63
sage: import sage.logic.booleval as booleval
64
sage: t = ['|', ['&', 'a', 'b'], ['&', 'a', 'c']]
65
sage: d = {'a' : True, 'b' : False, 'c' : True}
66
sage: booleval.eval_formula(t, d)
67
True
68
69
::
70
71
sage: d['a'] = False
72
sage: booleval.eval_formula(t, d)
73
False
74
"""
75
global __vars
76
__vars = vdict
77
b = logicparser.apply_func(tree, eval_f)
78
return b
79
80
def eval_f(tree):
81
r"""
82
Evaluate the tree
83
84
INPUT:
85
86
- ``tree`` -- a list of three elements corresponding to a branch of a
87
parse tree.
88
89
OUTPUT:
90
91
The result of the evaluation as a boolean value
92
93
EXAMPLES:
94
95
This example illustrates how to evaluate a parse tree.
96
97
::
98
99
sage: import sage.logic.booleval as booleval
100
sage: booleval.eval_f(['&', True, False])
101
False
102
103
::
104
105
sage: booleval.eval_f(['^', True, True])
106
False
107
108
::
109
110
sage: booleval.eval_f(['|', False, True])
111
True
112
"""
113
return eval_op(tree[0], tree[1], tree[2])
114
115
def eval_op(op, lv, rv):
116
r"""
117
Evaluate ``lv`` and ``rv`` according to the operator ``op``.
118
119
INPUT:
120
121
- ``op`` -- a string or character representing a boolean operator.
122
123
- ``lv`` -- a boolean or variable.
124
125
- ``rv`` -- a boolean or variable.
126
127
OUTPUT:
128
129
The evaluation of ``lv op rv`` as a boolean value
130
131
EXAMPLES:
132
133
We can evaluate an operator given the values on either side.
134
135
::
136
137
sage: import sage.logic.booleval as booleval
138
sage: booleval.eval_op('&', True, False)
139
False
140
141
::
142
143
sage: booleval.eval_op('^', True, True)
144
False
145
146
::
147
148
sage: booleval.eval_op('|', False, True)
149
True
150
"""
151
lval = rval = None
152
if lv == False:
153
lval = False
154
elif lv == True:
155
lval = True
156
elif lv is not None:
157
lval = __vars[lv]
158
159
if rv == False:
160
rval = False
161
elif rv == True:
162
rval = True
163
elif rv is not None:
164
rval = __vars[rv]
165
166
if op == '~':
167
return not lval
168
elif op == '&':
169
return lval and rval
170
elif op == '|':
171
return lval or rval
172
elif op == '^':
173
return lval ^ rval
174
elif op == '->':
175
return (not lval) or rval
176
elif op == '<->':
177
return (not lval or rval) and (not rval or lval)
178
else: # one variable
179
return __vars[op]
180
181