Logic Tables

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.

AUTHORS:

  • William Stein (2006): initial version

  • Chris Gorecki (2006): initial version

  • Paul Scurek (2013-08-03): updated docstring formatting

EXAMPLES:

Create a truth table of a boolean formula:

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
>>> from sage.all import *
>>> import sage.logic.propcalc as propcalc
>>> s = propcalc.formula("a&b|~(c|a)")
>>> 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

Get the LaTeX code for a truth table:

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}
>>> from sage.all import *
>>> latex(s.truthtable(Integer(5),Integer(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
>>> from sage.all import *
>>> s = propcalc.formula("a&b|~(c|a)")
>>> s.truthtable(Integer(5), Integer(9))
a      b      c      value
True   False  True   False
True   True   False  True
True   True   True   True

>>> s.truthtable(Integer(9), Integer(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
>>> from sage.all import *
>>> s.truthtable(-Integer(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
>>> from sage.all import *
>>> s.truthtable(Integer(4), -Integer(2))
a      b      c      value
True   False  False  False
True   False  True   False
True   True   False  True
True   True   True   True

Note

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.

class sage.logic.logictable.Truthtable(t, vo)[source]

Bases: object

A truth table.

INPUT:

  • t – a 2-D array containing the table values

  • vo – list of the variables in the expression in order, with each variable occurring only once

get_table_list()[source]

Return a list representation of the calling table object.

OUTPUT: list representation of the table

EXAMPLES:

This example illustrates how to show the table as a list:

sage: import sage.logic.propcalc as propcalc
sage: s = propcalc.formula("man->monkey&human")
sage: s.truthtable().get_table_list()
 [['man', 'monkey', 'human'], [False, False, False, True], [False, False, True, True], [False, True, False, True], [False, True, True, True], [True, False, False, False], [True, False, True, False], [True, True, False, False], [True, True, True, True]]
>>> from sage.all import *
>>> import sage.logic.propcalc as propcalc
>>> s = propcalc.formula("man->monkey&human")
>>> s.truthtable().get_table_list()
 [['man', 'monkey', 'human'], [False, False, False, True], [False, False, True, True], [False, True, False, True], [False, True, True, True], [True, False, False, False], [True, False, True, False], [True, True, False, False], [True, True, True, True]]