The simplest way of representing propositional logic truth functions in software is by storing their truth tables. Although this solution is rather inefficient, it is the best way forward for my purpose of developing a first baseline prototype.
The following truth table fully defines a truth function f of variables a, b, c and d. The table has 16 rows (excluding the header). Each row shows a set of values for the variables and the corresponding value for the function. For instance the 12th row indicates that f (1, 0, 1, 1) = 1.
| a | b | c | d | f (a, b, c, d) |
| 0 | 0 | 0 | 0 | 0 |
| 0 | 0 | 0 | 1 | 1 |
| 0 | 0 | 1 | 0 | 1 |
| 0 | 0 | 1 | 1 | 0 |
| 0 | 1 | 0 | 0 | 1 |
| 0 | 1 | 0 | 1 | 0 |
| 0 | 1 | 1 | 0 | 0 |
| 0 | 1 | 1 | 1 | 1 |
| 1 | 0 | 0 | 0 | 1 |
| 1 | 0 | 0 | 1 | 0 |
| 1 | 0 | 1 | 0 | 0 |
| 1 | 0 | 1 | 1 | 1 |
| 1 | 1 | 0 | 0 | 0 |
| 1 | 1 | 0 | 1 | 1 |
| 1 | 1 | 1 | 0 | 1 |
| 1 | 1 | 1 | 1 | 0 |
The values of f (a, b, c, d) from top to bottom in the rightmost column can be represented through a sequence of 16 booleans: "0110100110010110".
A given set of values for the function parameters from the leftmost (a) to the rightmost (d) can be represented using a sequence of 4 booleans. For instance the set of values in the 12th row of the table can be represented as: "1011".
The sequence of 4 booleans can then be converted to an index to extract from the sequence of 16 booleans the corresponding value of the truth function.
For instance the sequence of booleans "1011" corresponding to the 12th row can be seen as the binary representation of decimal number 11 (as 8 * 1 + 4 * 0 + 2 * 1 + 1 * 1 = 11).
More in general, the sequence of booleans corresponding to row n converts to the number n - 1.
This number can be used as an index from 0 to 15 upon the sequence of 16 booleans "0110100110010110", where the first boolean (left) is at index 0 and the last boolean (right) is at index 15.
Please note the conventions that I will be adopting for the representation of truth functions f (x1, x2, …, xn):
- In the sequence of booleans representing a set of truth values for the parameters x1, x2, …, xn, the first parameter x1 corresponds to the leftmost boolean and to the most significant bit of the corresponding binary representation.
- In the sequence of booleans representing the results of the truth function for all sets of parameter values, the result for set (0, 0, …, 0) corresponds to the leftmost boolean and to the lowest index 0.