File: /sagenb/sage_install/sage-4.8-sage.math.washington.edu-x86_64-Linux/local/lib/python2.6/site-packages/sage/logic/booleval.py
Type: <type ‘module’>
Definition: booleval( [noargspec] )
Docstring:
Evaluate Boolean Formulas
Perform boolean evaluation of boolean formulas.
AUTHORS:
- Chris Gorecki
EXAMPLES:
sage: import sage.logic.booleval as booleval sage: t = ['|', ['&', 'a', 'b'], ['&', 'a', 'c']] sage: d = {'a' : True, 'b' : False, 'c' : True} sage: booleval.eval_formula(t, d) True sage: d['a'] = False sage: booleval.eval_formula(t, d) False
Classes and functions¶
File: /sagenb/sage_install/sage-4.8-sage.math.washington.edu-x86_64-Linux/local/lib/python2.6/site-packages/sage/logic/boolformula.py
Type: <type 'module'>
Definition: boolformula( [noargspec] )
Docstring:
Module associated with booleval, logictable, and logicparser, to do
boolean evaluation of boolean formulas.
Formulas consist of the operators &, |, ~, ^, ->, <->, corresponding
to and, or, not, xor, if...then, if and only if. Operators can
be applied to variables that consist of a leading letter and trailing
underscores and alphanumerics. Parentheses may be used to
explicitly show order of operation.
AUTHORS:
-- Chris Gorecki
EXAMPLES:
sage: import sage.logic.propcalc as propcalc
sage: f = propcalc.formula("a&((b|c)^a->c)<->b")
sage: g = propcalc.formula("boolean<->algebra")
sage: (f&~g).ifthen(f)
((a&((b|c)^a->c)<->b)&(~(boolean<->algebra)))->(a&((b|c)^a->c)<->b)
EXAMPLES:
sage: import sage.logic.propcalc as propcalc
sage: f = propcalc.formula("a&((b|c)^a->c)<->b")
sage: g = propcalc.formula("boolean<->algebra")
sage: (f&~g).ifthen(f)
((a&((b|c)^a->c)<->b)&(~(boolean<->algebra)))->(a&((b|c)^a->c)<->b)
We can create a truth table from a formula.
sage: f.truthtable()
a b c value
False False False True
False False True True
False True False False
False True True False
True False False True
True False True False
True True False True
True True True True
sage: f.truthtable(end=3)
a b c value
False False False True
False False True True
False True False False
sage: f.truthtable(start=4)
a b c value
True False False True
True False True False
True True False True
True True True True
sage: propcalc.formula("a").truthtable()
a value
False False
True True
Now we can evaluate the formula for a given set of inputs.
sage: f.evaluate({'a':True, 'b':False, 'c':True})
False
sage: f.evaluate({'a':False, 'b':False, 'c':True})
True
And we can convert a boolean formula to conjunctive normal form.
sage: f.convert_cnf_table()
sage: f
(a|~b|c)&(a|~b|~c)&(~a|b|~c)
sage: f.convert_cnf_recur()
sage: f
(a|~b|c)&(a|~b|~c)&(~a|b|~c)
Or determine if an expression is satisfiable, a contradiction, or a tautology.
sage: f = propcalc.formula("a|b")
sage: f.is_satisfiable()
True
sage: f = f & ~f
sage: f.is_satisfiable()
False
sage: f.is_contradiction()
True
sage: f = f | ~f
sage: f.is_tautology()
True
The equality operator compares semantic equivalence.
sage: f = propcalc.formula("(a|b)&c")
sage: g = propcalc.formula("c&(b|a)")
sage: f == g
True
sage: g = propcalc.formula("a|b&c")
sage: f == g
False
It is an error to create a formula with bad syntax.
sage: propcalc.formula("")
Traceback (most recent call last):
...
SyntaxError: malformed statement
sage: propcalc.formula("a&b~(c|(d)")
Traceback (most recent call last):
...
SyntaxError: malformed statement
sage: propcalc.formula("a&&b")
Traceback (most recent call last):
...
SyntaxError: malformed statement
sage: propcalc.formula("a&b a")
Traceback (most recent call last):
...
SyntaxError: malformed statement
It is also an error to not abide by the naming conventions.
sage: propcalc.formula("~a&9b")
Traceback (most recent call last):
...
NameError: invalid variable name 9b: identifiers must begin with a letter and contain only alphanumerics and underscoresFile: /sagenb/sage_install/sage-4.8-sage.math.washington.edu-x86_64-Linux/local/lib/python2.6/site-packages/sage/logic/logic.py
Type: <type 'module'>
Definition: logic( [noargspec] )
Docstring:
Manipulation of symbolic logic expressions.
An expression is created from a string that consists of the
operators !, &, |, ->, <->, which correspond to the logical
functions not, and, or, if then, if and only if, respectively.
Variable names must start with a letter and contain only
alpha-numerics and the underscore character.
AUTHORS:
-- Chris Gorecki (2007): initial version
-- William Stein (2007-08-31): integration into Sage 2.8.4File: /sagenb/sage_install/sage-4.8-sage.math.washington.edu-x86_64-Linux/local/lib/python2.6/site-packages/sage/logic/logicparser.py
Type: <type 'module'>
Definition: logicparser( [noargspec] )
Docstring:
Module that creates and modifies parse trees of well formed
boolean formulas.
AUTHORS:
-- Chris Gorecki
EXAMPLES:
sage: import sage.logic.logicparser as logicparser
sage: s = 'a|b&c'
sage: t = logicparser.parse(s)
sage: t
(['|', 'a', ['&', 'b', 'c']], ['a', 'b', 'c'])File: /sagenb/sage_install/sage-4.8-sage.math.washington.edu-x86_64-Linux/local/lib/python2.6/site-packages/sage/logic/logictable.py
Type: <type 'module'>
Definition: logictable( [noargspec] )
Docstring:
Module designed for the creation and printing of truth tables that are
associated with a logical statement.
A logic table is essentially a 2-D array that is created by the statement class
and stored in the private global variable table, along with a list containing
the variable names to be used, in order.
The order in which the table is listed essentially amounts to counting in binary.
For instance, with the variables A, B, and C the truth table looks like:
A B C value
False False False ?
False False True ?
False True False ?
False True True ?
True False False ?
True False True ?
True True False ?
True True True ?
This is equivalent to counting in binary, where a table would appear thus;
2^2 2^1 2^0 value
0 0 0 0
0 0 1 1
0 1 0 2
0 1 1 3
1 0 0 4
1 0 1 5
1 1 0 6
1 1 1 7
Given that a table can be created corresponding to any range of acceptable
values for a given statement, it is easy to find the value of a statement
for arbitrary values of its variables.
EXAMPLES:
sage: import sage.logic.propcalc as propcalc
sage: s = propcalc.formula("a&b|~(c|a)")
sage: s.truthtable()
a b c value
False False False True
False False True False
False True False True
False True True False
True False False False
True False True False
True True False True
True True True True
sage: latex(s.truthtable(5,11))
\\\begin{tabular}{llll}c & b & a & value \\\hline True & False & True & False \\True & True & False & True \\True & True & True & True\end{tabular}
It is not an error to use nonsensical numeric inputs.
sage: s = propcalc.formula("a&b|~(c|a)")
sage: s.truthtable(5, 9)
a b c value
True False True False
True True False True
True True True True
sage: s.truthtable(9, 5)
a b c value
If one argument is provided, truthtable defaults to the end.
sage: s.truthtable(-1)
a b c value
False False False True
False False True False
False True False True
False True True False
True False False False
True False True False
True True False True
True True True True
If the second argument is negative, truthtable defaults to the end.
sage: s.truthtable(4, -2)
a b c value
True False False False
True False True False
True True False True
True True True True
NOTES:
For statements that contain a variable list that when printed is longer than
the \latex page, the columns of the table will run off the screen.File: /sagenb/sage_install/sage-4.8-sage.math.washington.edu-x86_64-Linux/local/lib/python2.6/site-packages/sage/logic/propcalc.py
Type: <type ‘module’>
Definition: propcalc( [noargspec] )
Docstring:
Propositional Calculus
Formulas consist of the following operators:
- & – and
- | – or
- ~ – not
- ^ – xor
- -> – if-then
- <-> – if and only if
Operators can be applied to variables that consist of a leading letter and trailing underscores and alphanumerics. Parentheses may be used to explicitly show order of operation.
AUTHORS:
- Chris Gorecki – propcalc, boolformula, logictable, logicparser, booleval
- Michael Greenberg – boolopt
EXAMPLES:
sage: import sage.logic.propcalc as propcalc sage: f = propcalc.formula("a&((b|c)^a->c)<->b") sage: g = propcalc.formula("boolean<->algebra") sage: (f&~g).ifthen(f) ((a&((b|c)^a->c)<->b)&(~(boolean<->algebra)))->(a&((b|c)^a->c)<->b)
We can create a truth table from a formula:
sage: f.truthtable() a b c value False False False True False False True True False True False False False True True False True False False True True False True False True True False True True True True True sage: f.truthtable(end=3) a b c value False False False True False False True True False True False False sage: f.truthtable(start=4) a b c value True False False True True False True False True True False True True True True True sage: propcalc.formula("a").truthtable() a value False False True True
Now we can evaluate the formula for a given set of input:
sage: f.evaluate({'a':True, 'b':False, 'c':True}) False sage: f.evaluate({'a':False, 'b':False, 'c':True}) True
And we can convert a boolean formula to conjunctive normal form:
sage: f.convert_cnf_table() sage: f (a|~b|c)&(a|~b|~c)&(~a|b|~c) sage: f.convert_cnf_recur() sage: f (a|~b|c)&(a|~b|~c)&(~a|b|~c)
Or determine if an expression is satisfiable, a contradiction, or a tautology:
sage: f = propcalc.formula("a|b") sage: f.is_satisfiable() True sage: f = f & ~f sage: f.is_satisfiable() False sage: f.is_contradiction() True sage: f = f | ~f sage: f.is_tautology() True
The equality operator compares semantic equivalence:
sage: f = propcalc.formula("(a|b)&c") sage: g = propcalc.formula("c&(b|a)") sage: f == g True sage: g = propcalc.formula("a|b&c") sage: f == g False
TESTS:
It is an error to create a formula with bad syntax:
sage: propcalc.formula("") Traceback (most recent call last): ... SyntaxError: malformed statement sage: propcalc.formula("a&b~(c|(d)") Traceback (most recent call last): ... SyntaxError: malformed statement sage: propcalc.formula("a&&b") Traceback (most recent call last): ... SyntaxError: malformed statement sage: propcalc.formula("a&b a") Traceback (most recent call last): ... SyntaxError: malformed statement It is also an error to not abide by the naming conventions. sage: propcalc.formula("~a&9b") Traceback (most recent call last): ... NameError: invalid variable name 9b: identifiers must begin with a letter and contain only alphanumerics and underscores