#[repr(i8)]pub enum Trit {
Neg = -1,
Zero = 0,
Pos = 1,
}Expand description
A balanced ternary digit: -1, 0, or +1 (D-01).
Discriminants are the numeric values, so trit as i8 gives the value.
Under the convention in D-06 the same values serve as three-valued logic
constants:
| Trit | Number | Logic |
|---|---|---|
Trit::Neg | -1 | false |
Trit::Zero | 0 | unknown |
Trit::Pos | +1 | true |
unknown means indeterminate: a value exists but is not known. It is not
an error channel. See D-06.
Variants§
Implementations§
Source§impl Trit
impl Trit
Sourcepub const NAT_CODE: u8 = 0b10
pub const NAT_CODE: u8 = 0b10
The NaT (not-a-trit) code. Host-side poison; never visible to a guest.
Sourcepub const fn from_value(v: i8) -> Option<Trit>
pub const fn from_value(v: i8) -> Option<Trit>
Builds a trit from its numeric value. None outside -1..=1.
Sourcepub const fn from_bct_code(code: u8) -> Option<Trit>
pub const fn from_bct_code(code: u8) -> Option<Trit>
Decodes a 2-bit BCT code. None for the 10 NaT pattern.
Sourcepub const fn not(self) -> Trit
pub const fn not(self) -> Trit
Logical NOT - arithmetic negation. Swaps true/false, fixes unknown.
Sourcepub const fn and(self, other: Trit) -> Trit
pub const fn and(self, other: Trit) -> Trit
Logical AND - the minimum of the two.
Kleene semantics absorb unknown where the result is determined
regardless: unknown AND false == false, since the conjunction is
false for either value the unknown could take.
Sourcepub const fn cycle(self) -> Trit
pub const fn cycle(self) -> Trit
Cyclic successor, -1 -> 0 -> +1 -> -1.
{MIN, MAX, NEG} is not functionally complete. All three respect the ordering -1 < 0 < +1, so no composition of them produces a map that does not. This one does, and adding it completes the set (D-06).
Sourcepub const fn webb(self, other: Trit) -> Trit
pub const fn webb(self, other: Trit) -> Trit
The Webb function, V(x, y) = max(x, y) + 1 (mod 3).
The ternary analogue of NAND: functionally complete on its own, so every one of the 19,683 two-input ternary operations is a composition of this single one (Post, 1941).
§Completeness
webb_generates_every_unary_function in this module verifies this by
construction: closing V under composition reaches all 27 unary ternary
functions, including the three constants. Two of them:
cycle(x) = V(x, x)
NOT(x) = V( V(V(x,x), V(x,V(x,x))),
V( V(x,V(x,V(x,x))), V(V(x,x), V(x,V(x,x))) ) )Cost differs sharply: cycle is one gate, NOT is seven, and
Trit::not is one operation. Webb is a foundation rather than an
implementation strategy. See D-13 in the roadmap.
Sourcepub const fn half_add(self, other: Trit) -> (Trit, Trit)
pub const fn half_add(self, other: Trit) -> (Trit, Trit)
Half adder: the sum trit and carry trit of self + other.
The sum of two trits lies in -2..=2 and needs two trits to hold, hence the carry. There is no separate borrow: subtraction is addition of the negation.
use ternaria_arith::Trit;
// 1 + 1 = 2, which in balanced ternary is 1T: carry 1, sum -1.
let (sum, carry) = Trit::Pos.half_add(Trit::Pos);
assert_eq!((sum, carry), (Trit::Neg, Trit::Pos));Sourcepub const fn full_add(self, other: Trit, carry_in: Trit) -> (Trit, Trit)
pub const fn full_add(self, other: Trit, carry_in: Trit) -> (Trit, Trit)
Full adder: sum and carry of self + other + carry_in.
The input sum spans -3..=3 and resolves into one sum trit and one carry trit.
Sourcepub const fn to_char(self) -> char
pub const fn to_char(self) -> char
The display character: T for -1, 0, 1.
T is the conventional balanced-ternary notation for the -1 digit,
keeping every digit one character wide so a numeral reads like any other
positional numeral.
§Counting from -9 to 9
Worth reading down the middle column: the negative half is the positive half with every digit flipped, because negation is digit flipping. There is no sign to carry around, and no gap or asymmetry at either end.
| n | balanced ternary | n | balanced ternary | |
|---|---|---|---|---|
| -9 | T00 | 9 | 100 | |
| -8 | T01 | 8 | 10T | |
| -7 | T1T | 7 | 1T1 | |
| -6 | T10 | 6 | 1T0 | |
| -5 | T11 | 5 | 1TT | |
| -4 | TT | 4 | 11 | |
| -3 | T0 | 3 | 10 | |
| -2 | T1 | 2 | 1T | |
| -1 | T | 1 | 1 | |
| 0 | 0 |
Note 2 is 1T - one three, minus one - rather than needing a digit
worth two. Every value has exactly one such representation.