Compile-time construction of Peano axioms-based natural numbers and addition, multiplication, and exponentiation defined through them within Rust. (Mirrored from Codeberg)

rename operations

+21 -23
+18 -18
src/lib.rs
··· 29 29 geq::Geq, 30 30 31 31 add::Add, 32 - mul::Times, 32 + mul::Mul, 33 33 exp::Exp, 34 34 35 35 parity::Even, ··· 85 85 use super::prelude::*; 86 86 87 87 pub trait Add<RHS: Nat> { 88 - type Result: Nat; 88 + type Sum: Nat; 89 89 } 90 90 91 91 // Base case 92 92 // N + 0 = N 93 93 impl<T: Nat> Add<_0> for T { 94 - type Result = T; 94 + type Sum = T; 95 95 } 96 96 97 97 // Induction ··· 100 100 where 101 101 A: Add<B> 102 102 { 103 - type Result = Succ<<A as Add<B>>::Result>; 103 + type Sum = Succ<<A as Add<B>>::Sum>; 104 104 } 105 105 } 106 106 107 107 pub mod mul { 108 108 use super::prelude::*; 109 109 110 - pub trait Times<RHS: Nat> { 111 - type Result: Nat; 110 + pub trait Mul<RHS: Nat> { 111 + type Prod: Nat; 112 112 } 113 113 114 114 // Base case 115 115 // N * 0 = 0 116 - impl<T: Nat> Times<_0> for T { 117 - type Result = _0; 116 + impl<T: Nat> Mul<_0> for T { 117 + type Prod = _0; 118 118 } 119 119 120 120 // Induction 121 121 // A * S(B) = A * B + A 122 - impl<A: Nat, B: Nat> Times<Succ<B>> for A 122 + impl<A: Nat, B: Nat> Mul<Succ<B>> for A 123 123 where 124 - A: Times<B>, 125 - <A as Times<B>>::Result: Add<A> 124 + A: Mul<B>, 125 + <A as Mul<B>>::Prod: Add<A> 126 126 { 127 - type Result = <<A as Times<B>>::Result as Add<A>>::Result; 127 + type Prod = <<A as Mul<B>>::Prod as Add<A>>::Sum; 128 128 } 129 129 } 130 130 131 131 pub mod exp { 132 132 use super::prelude::*; 133 133 134 - pub trait Exp<Pow: Nat> { 135 - type Result: Nat; 134 + pub trait Exp<P: Nat> { 135 + type Pow: Nat; 136 136 } 137 137 138 138 // Base case 139 139 // N ^ 0 = 1 140 140 impl<T: Nat> Exp<_0> for T { 141 - type Result = _1; 141 + type Pow = _1; 142 142 } 143 143 144 144 // Induction ··· 146 146 impl<A: Nat, B: Nat> Exp<Succ<B>> for A 147 147 where 148 148 A: Exp<B>, 149 - <A as Exp<B>>::Result: Times<A> 149 + <A as Exp<B>>::Pow: Mul<A> 150 150 { 151 - type Result = 152 - <<A as Exp<B>>::Result as Times<A>>::Result; 151 + type Pow = 152 + <<A as Exp<B>>::Pow as Mul<A>>::Prod; 153 153 } 154 154 } 155 155
+3 -5
src/main.rs
··· 1 - #![recursion_limit = "132"] 2 - 3 1 use rspeano::prelude::*; 4 2 5 3 fn five_plus_five_is_ten() { 6 - type Claimed10 = <_5 as Add<_5>>::Result; 4 + type Claimed10 = <_5 as Add<_5>>::Sum; 7 5 8 6 type Verified10 = 9 7 Succ< // 1 ··· 23 21 } 24 22 25 23 fn four_times_two_is_eight() { 26 - type Claimed8 = <_4 as Times<_2>>::Result; 24 + type Claimed8 = <_4 as Mul<_2>>::Prod; 27 25 28 26 type Verified8 = 29 27 Succ< // 1 ··· 41 39 } 42 40 43 41 fn two_squared_is_four() { 44 - type Claimed4 = <_2 as Exp<_2>>::Result; 42 + type Claimed4 = <_2 as Exp<_2>>::Pow; 45 43 46 44 type Verified4 = 47 45 Succ<