ILP Part 40 — Ternary logic

This is the fortieth part of the ILP series. For your convenience you can find other parts in the table of contents in Part 1 – Boolean algebra

We already know how to implement binary logic in ILP, today we are going to utilize it to implement ternary logic.

Implementation

Zero and one (or false and true) are rather ordinary, third value is called null or unknown and represent situation when we don’t know exact value. It usually has value \frac{1}{2}. Truth tables for ternary logic go as follows:
Disjunction:

    \[ \begin{tabular}{cc|c} a & b & x = a \vee b \\ \hline 0 & 0 & 0 \\ 0 & $\frac{1}{2}$ & $\frac{1}{2}$ \\ 0 & 1 & 1 \\ $\frac{1}{2}$ & 0 & $\frac{1}{2}$\\ $\frac{1}{2}$ & $\frac{1}{2}$ & $\frac{1}{2}$\\ $\frac{1}{2}$ & 1 & 1\\ 1 & 0 & 1 \\ 1 & $\frac{1}{2}$ & 1\\ 1 & 1 & 1 \end{tabular} \]

Conjunction:

    \[ \begin{tabular}{cc|c} a & b & x = a \wedge b \\ \hline 0 & 0 & 0 \\ 0 & $\frac{1}{2}$ & 0 \\ 0 & 1 & 0 \\ $\frac{1}{2}$ & 0 & 0\\ $\frac{1}{2}$ & $\frac{1}{2}$ & $\frac{1}{2}$\\ $\frac{1}{2}$ & 1 & $\frac{1}{2}$\\ 1 & 0 & 0 \\ 1 & $\frac{1}{2}$ & $\frac{1}{2}$\\ 1 & 1 & 1 \end{tabular} \]

We are going to represent any ternary logic value as a pair of boolean flags. You can intuitively treat this as a number in binary system with values multiplied by two, so false is 0, unknown is 1, and true is 2. We know that disjunction can be calculated as a \max function and conjunction as a \min function and in theory we can implement operations in this way. However, simply checking all cases is faster. This is disjunction:

    \[ (a_1, a_2) \vee (b_1, b_2) = 2 \cdot (a_1 \vee b_1) + \neg(a_1 \vee b_1) \wedge (a_2 \vee b_2) \]

Conjunction goes this way:

    \[ (a_1, a_2) \wedge(b_1, b_2) = 2 \cdot (a_1 \wedge b_1) + \neg(a_1 \wedge b_1) \wedge (a_1 \vee a_2) \wedge (b_1 \vee b_2) \]

Of course we need to destructure result to have two boolean flags at the end.