💾 Archived View for thrig.me › blog › 2023 › 09 › 30 › truth-table.gmi captured on 2024-07-09 at 01:11:17. Gemini links have been rewritten to link to archived content

View Raw

More Information

⬅️ Previous capture (2023-11-14)

-=-=-=-=-=-=-

Truth Table

What's a truth table? This object spawns from logic and comprises 16 so-called functions, though some are less useful than others. Here's the tables for "and", "or", and "exclusive or".

    first | second | result
    -----------------------
    true  | true   | true
    true  | false  | false
    false | true   | false
    false | false  | false
    first | second | result
    -----------------------
    true  | true   | true
    true  | false  | true
    false | true   | true
    false | false  | false
    first | second | result
    -----------------------
    true  | true   | false
    true  | false  | true
    false | true   | true
    false | false  | false

But what use is this?

One use is to help confirm that the logic of a statement is aright; all four cases may need to be thought about. Usually in computing one relies on code coverage or formal proofs or other such automations. Code coverage might tell you that a particular branch is never reached: could it ever be reached? If not, simplify the logic. Computers also tend to short-circuit logic checks for reasons of efficency: there's no point in checking the other statement when the first expression to an "and" is false.

    (defun the-other-statement ()
      (error "aaaaaaaaaaaaaaaaaargh")
      t)
    (and nil (the-other-statement))

The 16 functions can be named from reading down the result column, so "and" would be TFFF and "or" TTTF. On the somewhat useless front one then has TTTT and FFFF.

    (defun logic-tfff (first second) (and first second))
    (defun logic-tttf (first second) (or first second))
    (defun logic-tttt (first second) t)
    (defun logic-ffff (first second) nil)

    (defun logic-table (fn)
      (dolist (first '(t nil))
        (dolist (second '(t nil))
          (format t "~&~:[F~;T~] ~:[F~;T~] | ~:[F~;T~]"
                  first second (funcall fn first second)))))
    SBCL> (logic-table 'logic-tttf)

    T T | T
    T F | T
    F T | T
    F F | F
    NIL
    SBCL> (logic-table 'logic-tfff)

    T T | T
    T F | F
    F T | F
    F F | F
    NIL

A truth table was perhaps more exotic back in the 1920s when digital computers were not much of a thing. Most languages also only give you a few primitives from which (if you need to) things like the conditional can be expressed.

    (defun logic-tftt (first second) (not (and first (not second))))

Deeper into this Rabbit Hole

https://lojban.github.io/cll/14/1/

https://tellerprimer.ucdavis.edu/

tags #logic