Skip to main content

Trit

Enum Trit 

Source
#[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:

TritNumberLogic
Trit::Neg-1false
Trit::Zero0unknown
Trit::Pos+1true

unknown means indeterminate: a value exists but is not known. It is not an error channel. See D-06.

Variants§

§

Neg = -1

-1. Logical false.

§

Zero = 0

  1. Logical unknown.
§

Pos = 1

+1. Logical true.

Implementations§

Source§

impl Trit

Source

pub const ALL: [Trit; 3]

Every trit, ascending.

Source

pub const NAT_CODE: u8 = 0b10

The NaT (not-a-trit) code. Host-side poison; never visible to a guest.

Source

pub const fn value(self) -> i8

The numeric value, -1, 0, or +1.

Source

pub const fn from_value(v: i8) -> Option<Trit>

Builds a trit from its numeric value. None outside -1..=1.

Source

pub const fn bct_code(self) -> u8

The 2-bit BCT code for this trit.

Source

pub const fn from_bct_code(code: u8) -> Option<Trit>

Decodes a 2-bit BCT code. None for the 10 NaT pattern.

Source

pub const fn not(self) -> Trit

Logical NOT - arithmetic negation. Swaps true/false, fixes unknown.

Source

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.

Source

pub const fn or(self, other: Trit) -> Trit

Logical OR - the maximum of the two.

Source

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).

Source

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.

Source

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));
Source

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.

Source

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.

nbalanced ternarynbalanced ternary
-9T009100
-8T01810T
-7T1T71T1
-6T1061T0
-5T1151TT
-4TT411
-3T0310
-2T121T
-1T11
00

Note 2 is 1T - one three, minus one - rather than needing a digit worth two. Every value has exactly one such representation.

Source

pub const fn from_char(c: char) -> Option<Trit>

Parses a display character. Accepts T/t/- for -1 and + for +1.

Trait Implementations§

Source§

impl Clone for Trit

Source§

fn clone(&self) -> Trit

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Copy for Trit

Source§

impl Debug for Trit

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for Trit

Source§

fn default() -> Trit

Returns the “default value” for a type. Read more
Source§

impl Display for Trit

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Eq for Trit

Source§

impl From<Trit> for i8

Source§

fn from(t: Trit) -> i8

Converts to this type from the input type.
Source§

impl From<Trit> for i64

Source§

fn from(t: Trit) -> i64

Converts to this type from the input type.
Source§

impl Hash for Trit

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl Neg for Trit

Source§

fn neg(self) -> Trit

Negation is the logical NOT - one operation serving both roles (D-01).

Source§

type Output = Trit

The resulting type after applying the - operator.
Source§

impl Ord for Trit

Source§

fn cmp(&self, other: &Trit) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 (const: unstable) · Source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 (const: unstable) · Source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 (const: unstable) · Source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
Source§

impl PartialEq for Trit

Source§

fn eq(&self, other: &Trit) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl PartialOrd for Trit

Source§

fn partial_cmp(&self, other: &Trit) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 (const: unstable) · Source§

fn lt(&self, other: &Rhs) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 (const: unstable) · Source§

fn le(&self, other: &Rhs) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 (const: unstable) · Source§

fn gt(&self, other: &Rhs) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 (const: unstable) · Source§

fn ge(&self, other: &Rhs) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl StructuralPartialEq for Trit

Auto Trait Implementations§

§

impl Freeze for Trit

§

impl RefUnwindSafe for Trit

§

impl Send for Trit

§

impl Sync for Trit

§

impl Unpin for Trit

§

impl UnsafeUnpin for Trit

§

impl UnwindSafe for Trit

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.