Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemath
GitHub Repository: sagemath/sagelib
Path: blob/master/sage/logic/propcalc.py
4068 views
1
r"""
2
Propositional Calculus
3
4
Formulas consist of the following operators:
5
6
* ``&`` -- and
7
* ``|`` -- or
8
* ``~`` -- not
9
* ``^`` -- xor
10
* ``->`` -- if-then
11
* ``<->`` -- if and only if
12
13
Operators can be applied to variables that consist of a leading letter and
14
trailing underscores and alphanumerics. Parentheses may be used to explicitly
15
show order of operation.
16
17
AUTHORS:
18
19
- Chris Gorecki -- propcalc, boolformula, logictable, logicparser, booleval
20
21
- Michael Greenberg -- boolopt
22
23
EXAMPLES::
24
25
sage: import sage.logic.propcalc as propcalc
26
sage: f = propcalc.formula("a&((b|c)^a->c)<->b")
27
sage: g = propcalc.formula("boolean<->algebra")
28
sage: (f&~g).ifthen(f)
29
((a&((b|c)^a->c)<->b)&(~(boolean<->algebra)))->(a&((b|c)^a->c)<->b)
30
31
We can create a truth table from a formula::
32
33
sage: f.truthtable()
34
a b c value
35
False False False True
36
False False True True
37
False True False False
38
False True True False
39
True False False True
40
True False True False
41
True True False True
42
True True True True
43
sage: f.truthtable(end=3)
44
a b c value
45
False False False True
46
False False True True
47
False True False False
48
sage: f.truthtable(start=4)
49
a b c value
50
True False False True
51
True False True False
52
True True False True
53
True True True True
54
sage: propcalc.formula("a").truthtable()
55
a value
56
False False
57
True True
58
59
Now we can evaluate the formula for a given set of input::
60
61
sage: f.evaluate({'a':True, 'b':False, 'c':True})
62
False
63
sage: f.evaluate({'a':False, 'b':False, 'c':True})
64
True
65
66
And we can convert a boolean formula to conjunctive normal form::
67
68
sage: f.convert_cnf_table()
69
sage: f
70
(a|~b|c)&(a|~b|~c)&(~a|b|~c)
71
sage: f.convert_cnf_recur()
72
sage: f
73
(a|~b|c)&(a|~b|~c)&(~a|b|~c)
74
75
Or determine if an expression is satisfiable, a contradiction, or a tautology::
76
77
sage: f = propcalc.formula("a|b")
78
sage: f.is_satisfiable()
79
True
80
sage: f = f & ~f
81
sage: f.is_satisfiable()
82
False
83
sage: f.is_contradiction()
84
True
85
sage: f = f | ~f
86
sage: f.is_tautology()
87
True
88
89
The equality operator compares semantic equivalence::
90
91
sage: f = propcalc.formula("(a|b)&c")
92
sage: g = propcalc.formula("c&(b|a)")
93
sage: f == g
94
True
95
sage: g = propcalc.formula("a|b&c")
96
sage: f == g
97
False
98
99
TESTS:
100
101
It is an error to create a formula with bad syntax::
102
103
sage: propcalc.formula("")
104
Traceback (most recent call last):
105
...
106
SyntaxError: malformed statement
107
sage: propcalc.formula("a&b~(c|(d)")
108
Traceback (most recent call last):
109
...
110
SyntaxError: malformed statement
111
sage: propcalc.formula("a&&b")
112
Traceback (most recent call last):
113
...
114
SyntaxError: malformed statement
115
sage: propcalc.formula("a&b a")
116
Traceback (most recent call last):
117
...
118
SyntaxError: malformed statement
119
120
It is also an error to not abide by the naming conventions.
121
sage: propcalc.formula("~a&9b")
122
Traceback (most recent call last):
123
...
124
NameError: invalid variable name 9b: identifiers must begin with a letter and contain only alphanumerics and underscores
125
126
127
Classes and functions
128
=====================
129
"""
130
131
#**************************************************************************
132
# Copyright (C) 2006 William Stein <[email protected]>
133
# Copyright (C) 2006 Chris Gorecki <[email protected]>
134
#
135
# Distributed under the terms of the GNU General Public License (GPL)
136
# http://www.gnu.org/licenses/
137
#**************************************************************************
138
139
### TODO:
140
### converts (cnf) returns w/o change
141
142
import boolformula
143
import logicparser
144
145
def formula(s):
146
r"""
147
Returns an instance of
148
:class:`BooleanFormula <sage.logic.boolformula.BooleanFormula>`
149
if possible, and throws a syntax error if not.
150
151
INPUT:
152
153
- ``s`` -- a string that contains a logical expression.
154
155
OUTPUT:
156
157
- An instance of
158
:class:`BooleanFormula <sage.logic.boolformula.BooleanFormula>`
159
representing the logical expression ``s``.
160
161
EXAMPLES::
162
163
sage: import sage.logic.propcalc as propcalc
164
sage: f = propcalc.formula("a&~b|c")
165
sage: g = propcalc.formula("a^c<->b")
166
sage: f&g|f
167
((a&~b|c)&(a^c<->b))|(a&~b|c)
168
169
TESTS:
170
171
There are a number of possible errors::
172
173
sage: propcalc.formula("((a&b)")
174
Traceback (most recent call last):
175
...
176
SyntaxError: malformed statement
177
sage: propcalc.formula("_a&b")
178
Traceback (most recent call last):
179
...
180
NameError: invalid variable name _a: identifiers must begin with a letter and contain only alphanumerics and underscores
181
"""
182
try:
183
parse_tree, vars_order = logicparser.parse(s)
184
f = boolformula.BooleanFormula(s, parse_tree, vars_order)
185
f.truthtable(0, 1)
186
except (KeyError, RuntimeError, IndexError, SyntaxError):
187
msg = "malformed statement"
188
raise SyntaxError(msg)
189
return f
190
191