Trait Int

Source
pub trait Int: Default {
    // Required method
    fn reify() -> isize;
}
Expand description

The Int kind is for signed, type-level integers. They are represented through balanced ternary in order to avoid a potentially non-unique representation of zero. Balanced ternary is a signed digit representation. A signed digit representation is two things:

  1. Really cool.
  2. A number system in which digits can be multiples of negative one. If you want to go even further down the Cool Spectrum, Donald Knuth suggested a complex-base number system which has digits that are multiples of powers of i. I’m honestly not sure how useful that is but it sure is really cool!

In balanced ternary, we have three digits: zero, plus, and minus. So, here are the integers from negative five to positive five, in balanced ternary:

DecimalBalanced ternary (0+-)Balanced ternary (01T)Expression
-5-++T113 * (3 * (3 * 0 - 1) + 1) + 1
-4--TT3 * (3 * 0 - 1) - 1
-3-0T03 * (3 * 0 - 1)
-2-+T13 * (3 * 0 - 1) + 1
-1-T3 * 0 - 1
0000
1+13 * 0 + 1
2+-1T3 * (3 * 0 + 1) - 1
3+0103 * (3 * 0 + 1)
4++113 * (3 * 0 + 1) + 1
5+--1TT3 * (3 * (3 * 0 + 1) - 1) - 1

Like the ternary representation in this library, we use a type-level linked list to represent our balanced ternary numbers. Our digits are:

  • Term represents the end of our linked list - our “nil” - and also “zero”.
  • Zero<X>, Plus<X>, and Minus<X> represent 3 * X, 3 * X + 1, and 3 * X - 1, respectively.

Like the ternary representation, this linked list is stored with the least-significant digit at the head of the list. Precautions are taken to avoid issues with non-unique representations with redundant zeroes such as Zero<Zero<Term>> and if one ever crops up in your code, please lodge an issue! It is definitely caused by either a user error or a bug.

Int can be reified to isize. Like Nats, Ints cannot be reified to a constant expression due to limitations with Rust’s associated const fns and constants. If and when these features are added to Rust in the future they will be added here too.

Ints are always zero-sized, so you can store them directly in your structs instead of using PhantomData. They implement Default.

Required Methods§

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§

Source§

impl Int for Term

Source§

impl Int for Undefined

Source§

impl<X: Int> Int for Minus<X>

Source§

impl<X: Int> Int for Plus<X>

Source§

impl<X: Int> Int for Zero<X>