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:
EXAMPLES:
This example illustrates how to create a boolean formula and print its table:
sage: log = SymbolicLogic()
sage: s = log.statement("a&b|!(c|a)")
sage: t = log.truthtable(s)
sage: log.print_table(t)
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 |
Return a new statement which contains the two statements or’d together.
INPUT:
OUTPUT:
A new staement which or’d the given statements together.
EXAMPLES:
sage: log = SymbolicLogic()
sage: s1 = log.statement("(a&b)")
sage: s2 = log.statement("b")
sage: log.combine(s1,s2)
[['OPAREN',
'OPAREN',
'OPAREN',
'a',
'AND',
'b',
'CPAREN',
'CPAREN',
'OR',
'OPAREN',
'b',
'CPAREN',
'CPAREN'],
{'a': 'False', 'b': 'False'},
['a', 'b', 'b']]
Return a truthtable corresponding to the given statement.
INPUT:
OUTPUT:
A formatted version of the truth table.
EXAMPLES:
This example illustrates the creation of a statement and its truth table:
sage: log = SymbolicLogic()
sage: s = log.statement("a&b|!(c|a)")
sage: t = log.truthtable(s) #creates the whole truth table
sage: log.print_table(t)
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 |
We can also print a shortened table:
sage: t = log.truthtable(s, 1, 5)
sage: log.print_table(t)
a | b | c | value | value |
----------------------------------------
False | False | False | True | True |
False | False | True | False | False |
False | False | True | True | False |
False | True | False | False | True |
A function to test to see if the statement is a tautology or contradiction by calling a C++ library.
Todo
Implement this method.
EXAMPLES:
sage: log = SymbolicLogic()
sage: s = log.statement("a&b|!(c|a)")
sage: log.prove(s)
Traceback (most recent call last):
...
NotImplementedError
Call a C++ implementation of the ESPRESSO algorithm to simplify the given truth table.
Todo
Implement this method.
EXAMPLES:
sage: log = SymbolicLogic()
sage: s = log.statement("a&b|!(c|a)")
sage: t = log.truthtable(s)
sage: log.simplify(t)
Traceback (most recent call last):
...
NotImplementedError
Return a token list to be used by other functions in the class
INPUT:
OUTPUT:
A list of length three containing the following in this order:
EXAMPLES:
This example illustrates the creation of a statement:
sage: log = SymbolicLogic()
sage: s = log.statement("a&b|!(c|a)")
sage: s2 = log.statement("!((!(a&b)))")
It is an error to use invalid variable names:
sage: s = log.statement("3fe & @q")
Invalid variable name: 3fe
Invalid variable name: @q
It is also an error to use invalid syntax:
sage: s = log.statement("a&&b")
Malformed Statement
sage: s = log.statement("a&((b)")
Malformed Statement
Return a truth table.
INPUT:
OUTPUT:
The truth table as a 2d array with the creating formula tacked to the front.
EXAMPLES:
This example illustrates the creation of a statement:
sage: log = SymbolicLogic()
sage: s = log.statement("a&b|!(c|a)")
sage: t = log.truthtable(s) #creates the whole truth table
We can now create truthtable of rows 1 to 5:
sage: s2 = log.truthtable(s, 1, 5); s2
[[['OPAREN', 'a', 'AND', 'b', 'OR', 'NOT', 'OPAREN', 'c', 'OR', 'a', 'CPAREN', 'CPAREN'],
{'a': 'False', 'c': 'True', 'b': 'False'}, ['a', 'b', 'c']],
['False', 'False', 'True', 'False'], ['False', 'True', 'False', 'True'],
['False', 'True', 'True', 'True'], ['True', 'False', 'False', 'False']]
Note
When sent with no start or end parameters this is an
exponential time function requiring time, where
is the number of variables in the logic expression
Evaluate the expression contained in toks.
INPUT:
OUTPUT:
A boolean value to be determined as follows:
Note
This function is for internal use by the SymbolicLogic class. The evaluations rely on setting the values of the variables in the global dictionary vars.
TESTS:
sage: log = SymbolicLogic()
sage: s = log.statement("a&b|!(c|a)")
sage: sage.logic.logic.eval(s[0])
'True'
Apply the ‘and’ operator to lval and rval.
INPUT:
OUTPUT:
The result of applying ‘and’ to lval and rval as a string.
Note
This function is for internal use by the SymbolicLogic class.
TESTS:
sage: sage.logic.logic.eval_and_op('False', 'False')
'False'
sage: sage.logic.logic.eval_and_op('False', 'True')
'False'
sage: sage.logic.logic.eval_and_op('True', 'False')
'False'
sage: sage.logic.logic.eval_and_op('True', 'True')
'True'
Return a boolean value based on the truth table of the operator in args.
INPUT:
OUTPUT:
A boolean value; this is the evaluation of the operator based on the truth values of the variables.
Note
This function is for internal use by the SymbolicLogic class.
TESTS:
sage: log = SymbolicLogic()
sage: s = log.statement("!(a&b)"); s
[['OPAREN', 'NOT', 'OPAREN', 'a', 'AND', 'b', 'CPAREN', 'CPAREN'],
{'a': 'False', 'b': 'False'},
['a', 'b']]
sage: sage.logic.logic.eval_bin_op(['a', 'AND', 'b'])
'False'
Apply the ‘if and only if’ operator to lval and rval.
INPUT:
OUTPUT:
A string representing the result of applying ‘if and only if’ to lval and rval.
Note
This function is for internal use by the SymbolicLogic class.
TESTS:
sage: sage.logic.logic.eval_iff_op('False', 'False')
'True'
sage: sage.logic.logic.eval_iff_op('False', 'True')
'False'
sage: sage.logic.logic.eval_iff_op('True', 'False')
'False'
sage: sage.logic.logic.eval_iff_op('True', 'True')
'True'
Apply the ‘if then’ operator to lval and rval.
INPUT:
OUTPUT:
A string representing the result of applying ‘if then’ to lval and rval.
Note
This function is for internal use by the SymbolicLogic class.
TESTS:
sage: sage.logic.logic.eval_ifthen_op('False', 'False')
'True'
sage: sage.logic.logic.eval_ifthen_op('False', 'True')
'True'
sage: sage.logic.logic.eval_ifthen_op('True', 'False')
'False'
sage: sage.logic.logic.eval_ifthen_op('True', 'True')
'True'
Evaluates the expression contained in lrtoks.
INPUT:
OUTPUT:
A boolean value to be determined as follows:
Note
This function is for internal use by the SymbolicLogic class. The evaluations rely on setting the values of the variables in the global dictionary vars.
TESTS:
sage: log = SymbolicLogic()
sage: s = log.statement("a&b|!c")
sage: ltor = s[0][1:-1]; ltor
['a', 'AND', 'b', 'OR', 'NOT', 'c']
sage: sage.logic.logic.eval_ltor_toks(ltor)
'True'
Return a boolean value based on the truth table of the operator in args.
INPUT:
OUTPUT:
A boolean value to be determined as follows:
Note
This function is for internal use by the SymbolicLogic class.
TESTS:
sage: log = SymbolicLogic()
sage: s = log.statement("!(a&b)|!a"); s
[['OPAREN', 'NOT', 'OPAREN', 'a', 'AND', 'b', 'CPAREN', 'OR', 'NOT', 'a', 'CPAREN'],
{'a': 'False', 'b': 'False'},
['a', 'b']]
sage: sage.logic.logic.eval_mon_op(['NOT', 'a'])
'True'
Apply the ‘or’ operator to lval and rval.
INPUT:
OUTPUT:
A string representing the result of applying ‘or’ to lval and rval.
Note
This function is for internal use by the SymbolicLogic class.
TESTS:
sage: sage.logic.logic.eval_or_op('False', 'False')
'False'
sage: sage.logic.logic.eval_or_op('False', 'True')
'True'
sage: sage.logic.logic.eval_or_op('True', 'False')
'True'
sage: sage.logic.logic.eval_or_op('True', 'True')
'True'
Determine if bit c of the number x is 1.
INPUT:
OUTPUT:
A boolean value to be determined as follows:
Note
This function is for internal use by the SymbolicLogic class.
EXAMPLES:
sage: from sage.logic.logic import get_bit
sage: get_bit(int(2), int(1))
'True'
sage: get_bit(int(8), int(0))
'False'
Evaluate lrtoks to a single boolean value.
INPUT:
OUTPUT:
None; the pointer to lrtoks is now a list containing True or False.
Note
This function is for internal use by the SymbolicLogic class.
TESTS:
sage: log = SymbolicLogic()
sage: s = log.statement("a&b|c")
sage: lrtoks = s[0][1:-1]; lrtoks
['a', 'AND', 'b', 'OR', 'c']
sage: sage.logic.logic.reduce_bins(lrtoks); lrtoks
['False']
Replace monotonic operator/variable pairs with a boolean value.
INPUT:
OUTPUT:
None; the pointer to lrtoks is now a list containing monotonic operators.
Note
This function is for internal use by the SymbolicLogic class.
TESTS:
sage: log = SymbolicLogic()
sage: s = log.statement("!a&!b")
sage: lrtoks = s[0][1:-1]; lrtoks
['NOT', 'a', 'AND', 'NOT', 'b']
sage: sage.logic.logic.reduce_monos(lrtoks); lrtoks
['True', 'AND', 'True']
Tokenize s and place the tokens of s in toks.
INPUT:
OUTPUT:
None; the tokens of s are placed in toks.
Note
This function is for internal use by the SymbolicLogic class.
EXAMPLES:
sage: from sage.logic.logic import tokenize
sage: toks = []
sage: tokenize("(a&b)|c", toks)
sage: toks
['OPAREN', 'a', 'AND', 'b', 'CPAREN', 'OR', 'c', 'CPAREN']