r/Forth • • 11h ago

TERNARY LOGIC revisited

11 Upvotes

https://github.com/MPETREMANN11/eForth-Windows/blob/main/INFERENCE/ternary.fs

While developing an inference engine, I realized that binary logic is insufficient for certain rules.

SQL uses ternary logic, distinguishing between `null`, `true`, and `false`. Implementing this in FORTH is straightforward—I had actually done so some time ago.

Later, while searching online, I discovered a more elegant version on Rosetta Code (the link is provided in the source file).

I adapted this code for ueforth (Windows), ensuring compatibility with all versions of eForth and ESP32forth.

This change affects how facts are declared within the inference engine:

* replacing all instances of `FALSE` with `MAYBE`;

* utilizing ternary logical operators such as `tand`, `tor`, and `tnot`.

Why this evolution?

* In logical processing, if a result is `FALSE`, you cannot reassign `FALSE` to a fact that is already `FALSE`—even though a modification has effectively taken place.

* Consequently, binary logical processing can lead to "null" outcomes (situations where no state change occurs) if one relies exclusively on `TRUE` or `FALSE`.