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