Wolfram Research

Function Repository Resource:

TruthTable

Source Notebook

Generate a truth table for a Boolean expression

Contributed by: Wolfram|Alpha Math Team (Jeremy Stratton-Smith)

ResourceFunction["TruthTable"][expr,vars]

creates a truth table for the logical expression expr with respect to vars.

ResourceFunction["TruthTable"][boole]

creates a truth table for the BooleanFunction boole with generated parameters.

ResourceFunction["TruthTable"][,format]

creates a truth table with output form specified by format.

Details

The input expr is expected to be composed of symbols and logical operators such as And, Or, Xor, Nor, etc. or else a List of such expressions.
The input vars is expected to be a symbol or list of symbols.
The input boole should be any BooleanFunction, and the output will generate a number of unused parameters equal to the value of BooleanVariables[boole], in addition to formatting the BooleanFunction as the simplest explicit form found using BooleanConvert.
The input format can be either "GridForm" (default) or "ListForm". If "ListForm" is entered, ResourceFunction["TruthTable"] will return an unformatted list of lists corresponding to the rows of the truth table.

Examples

Basic Examples (2) 

Produce a truth table for a simple logical expression:

In[1]:=
ResourceFunction["TruthTable"][p && q, {p, q}]
Out[1]=

You can also build up larger expressions:

In[2]:=
ResourceFunction[
 "TruthTable"][((p && q) || (r && p)) && (r || q), {p, q, r}]
Out[2]=

Scope (2) 

TruthTable also accepts raw BooleanFunction inputs and produces an explicit logical expression for the function:

In[3]:=
ResourceFunction["TruthTable"][BooleanFunction[3, 3], "GridForm"]
Out[3]=

Display the same result using the list format:

In[4]:=
ResourceFunction["TruthTable"][BooleanFunction[3, 3], "ListForm"]
Out[4]=

Applications (2) 

Show DeMorgan's law by comparing two logical expressions:

In[5]:=
ResourceFunction["TruthTable"][{! (p && q), ! p || ! q}, {p, q}]
Out[5]=

Show that Implies is not associative by comparing the truth tables of two expressions:

In[6]:=
ResourceFunction[
 "TruthTable"][{(p \[Implies] q) \[Implies] r, p \[Implies] (q \[Implies] r)}, {p, q, r}]
Out[6]=

Neat Examples (1) 

Use a truth table to easily determine if multiple logical expressions are equivalent:

In[7]:=
ResourceFunction[
 "TruthTable"][{p \[Implies] (q \[Implies] r), ! p || ! q || r, ! (q \[Implies] r) \[Implies] ! p, q && ! r \[Implies] ! p, ! (q && ! r) || ! p}, {p, q, r}]
Out[7]=

Publisher

Wolfram|Alpha Math Team

Version History

  • 1.0.0 – 10 May 2021

Related Resources

Author Notes

To view the full source code for TruthTable, evaluate the following:

In[1]:=
SystemOpen[
 FileNameJoin[{DirectoryName[FindFile["ResourceFunctionHelpers`"]], "TruthTable.wl"}]]

License Information