user
|  |
| --- |
| This page uses content from **Wikipedia**. The original article was at Ternary logic. The list of authors can be seen in the page history. As with Rosetta Code, the text of Wikipedia is available under the GNU FDL. (See links for details on variance) |

In logic, a **three-valued logic** (also **trivalent**, **ternary**, or **trinary logic**, sometimes abbreviated **3VL**) is any of several many-valued logic systems in which there are three truth values indicating *true*, *false* and some indeterminate third value.

This is contrasted with the more commonly known bivalent logics (such as classical sentential or boolean logic) which provide only for *true* and *false*.

Conceptual form and basic ideas were initially created by Łukasiewicz, Lewis and Sulski.

These were then re-formulated by Grigore Moisil in an axiomatic algebraic form, and also extended to *n*-valued logics in 1945.

**Example *Ternary Logic Operators* in *Truth Tables*:**

|  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |
| --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- |
| *not* a  | ¬ | | | --- | --- | | True | False | | Maybe | Maybe | | False | True | | a *and* b  |  |  |  |  | | --- | --- | --- | --- | | ∧ | True | Maybe | False | | True | True | Maybe | False | | Maybe | Maybe | Maybe | False | | False | False | False | False | | a *or* b  |  |  |  |  | | --- | --- | --- | --- | | ∨ | True | Maybe | False | | True | True | True | True | | Maybe | True | Maybe | Maybe | | False | True | Maybe | False | |
| *if* a *then* b  |  |  |  |  | | --- | --- | --- | --- | | ⊃ | True | Maybe | False | | True | True | Maybe | False | | Maybe | True | Maybe | Maybe | | False | True | True | True | | a *is equivalent to* b  |  |  |  |  | | --- | --- | --- | --- | | ≡ | True | Maybe | False | | True | True | Maybe | False | | Maybe | Maybe | Maybe | Maybe | | False | False | Maybe | True | |

Task

* Define a new type that emulates *ternary logic* by storing data **trits**.
* Given all the binary logic operators of the original programming language, reimplement these operators for the new *Ternary logic* type **trit**.
* Generate a sampling of results using **trit** variables.
* Kudos for actually thinking up a test case algorithm where *ternary logic* is intrinsically useful, optimises the test case algorithm and is preferable to binary logic.

Note:   **Setun**   (Сетунь) was a   balanced ternary   computer developed in 1958 at   Moscow State University.   The device was built under the lead of   Sergei Sobolev   and   Nikolay Brusentsov.   It was the only modern   ternary computer,   using three-valued ternary logic
assistant
(formerly Perl 6)

**Works with**: rakudo version 2018.03

The precedence of each operator is specified as equivalent to an existing operator. We've taken the liberty of using a double arrow for implication, to avoid confusing it with ⊃, (U+2283 SUPERSET OF).

```
# Implementation:
enum Trit <Foo Moo Too>;

sub prefix:<¬> (Trit $a) { Trit(1-($a-1)) }

sub infix:<∧> (Trit $a, Trit $b) is equiv(&infix:<*>) { $a min $b }
sub infix:<∨> (Trit $a, Trit $b) is equiv(&infix:<+>) { $a max $b }

sub infix:<⇒> (Trit $a, Trit $b) is equiv(&infix:<..>) { ¬$a max $b }
sub infix:<≡> (Trit $a, Trit $b) is equiv(&infix:<eq>) { Trit(1 + ($a-1) * ($b-1)) }

# Testing:
say '¬';
say "Too {¬Too}";
say "Moo {¬Moo}";
say "Foo {¬Foo}";

sub tbl (&op,$name) {
    say '';
    say "$name   Too Moo Foo";
    say "   ╔═══════════";
    say "Too║{op Too,Too} {op Too,Moo} {op Too,Foo}";
    say "Moo║{op Moo,Too} {op Moo,Moo} {op Moo,Foo}";
    say "Foo║{op Foo,Too} {op Foo,Moo} {op Foo,Foo}";
}

tbl(&infix:<∧>, '∧');
tbl(&infix:<∨>, '∨');
tbl(&infix:<⇒>, '⇒');
tbl(&infix:<≡>, '≡');

say '';
say 'Precedence tests should all print "Too":';
say ~(
    Foo ∧ Too ∨ Too ≡ Too,
    Foo ∧ (Too ∨ Too) ≡ Foo,
    Too ∨ Too ∧ Foo ≡ Too,
    (Too ∨ Too) ∧ Foo ≡ Foo,

    ¬Too ∧ Too ∨ Too ≡ Too,
    ¬Too ∧ (Too ∨ Too) ≡ ¬Too,
    Too ∨ Too ∧ ¬Too ≡ Too,
    (Too ∨ Too) ∧ ¬Too ≡ ¬Too,
 
    Foo ∧ Too ∨ Foo ⇒ Foo ≡ Too,
    Foo ∧ Too ∨ Too ⇒ Foo ≡ Foo,
);

```

Output:

```
¬
Too Foo
Moo Moo
Foo Too

∧   Too Moo Foo
   ╔═══════════
Too║Too Moo Foo
Moo║Moo Moo Foo
Foo║Foo Foo Foo

∨   Too Moo Foo
   ╔═══════════
Too║Too Too Too
Moo║Too Moo Moo
Foo║Too Moo Foo

⇒   Too Moo Foo
   ╔═══════════
Too║Too Moo Foo
Moo║Too Moo Moo
Foo║Too Too Too

≡   Too Moo Foo
   ╔═══════════
Too║Too Moo Foo
Moo║Moo Moo Moo
Foo║Foo Moo Too

Precedence tests should all print "Too":
Too Too Too Too Too Too Too Too Too Too
```