Truth table

From Fōrmulæ wiki
Jump to: navigation, search

This page is the answer to the task Truth table in the Rosetta Code.

Description (from Rosetta Code)

A Truth table is a display of the inputs to, and the output of a Boolean equation organised as a table where each row gives one combination of input values and the corresponding value of the equation.


  1. Input a Boolean equation from the user as a string then calculate and print a formatted truth table for the given equation.
    (One can assume that the user input is correct).
  2. Print and show output for Boolean equations of two and three input variables, but any program should not be limited to that many variables in the equation.
  3. Either reverse-polish or infix notation expressions are allowed.




Part 1. Input

λ is intended to be the boolean equation, given as a lambda expression.

Under Fōrmulæ, a lambda expression has two subexpressions, the first one is the set of variables to be mapped, and the second one is the mapping expression.

So λ1 is the left part of the lambda expression, this is, the set of variables. Under Fōrmulæ, counting starts in one, rather than zero as many programming languages.

Then 1 is the number of variables.

Part 2. Cartesian product exponentiation

The expression


Is the Cartesian product exponentiation of the list {true, false}. See the following examples.

TruthTableProgram2.png TruthTableProgram3.png TruthTableProgram4.png etc.

Please note that the cartesian product exponentiation always produces a list of lists, shown here with a matrix notation.

This step produces the combination of logic values (true and false) according to the number of variables of the boolean equation.

Part 3. Arraization

The Fōrmulæ arraization expression is similar in notation and functionality to summation, see the following examples:


But in here, we use a variation that iterates over the elements of a list:


Part 4. Calculation and output

The expression


Creates a array from the elements of the values array. Note that this array is a list of lists (a matrix), produced as a cartesian product exponentiation, so the v symbol repeatedly acquires an array (for every row of the matrix).

The expression


Is the lambda application of the λ lambda expression using the set of values provided precisely as the symbol v.

Note that the arraization produces an array, and the expression to create the elements of the array is also an array, the final result is shown as a matrix.

Hacking mode

The provided program was created with clarity purposes in mind. A compressed, but not so clear version could be:


Case 1. One variable

The following example produces the logical negation table


Case 2. Two variables

The following example produces the logical conjunction table


Because there is no restrictions about the mapping expression, it can be an array of expressions involving the arguments.

The following example produces the truth table for logical conjunction, disjunction, material implication, equivalence, and exclusive disjunction.


Case 3. Three variables

In the following example, the truth table is used to show that a boolean formula is a tautology.

Because any function is a named lambda expression, the example also shows the passing of a function as an argument to another function.



See also

Finite-valued logic example.