Compile-time construction of Peano axioms-based natural numbers and addition, multiplication, and exponentiation defined through them within Rust. (Mirrored from Codeberg)
···2929 geq::Geq,
30303131 add::Add,
3232- mul::Times,
3232+ mul::Mul,
3333 exp::Exp,
34343535 parity::Even,
···8585 use super::prelude::*;
86868787 pub trait Add<RHS: Nat> {
8888- type Result: Nat;
8888+ type Sum: Nat;
8989 }
90909191 // Base case
9292 // N + 0 = N
9393 impl<T: Nat> Add<_0> for T {
9494- type Result = T;
9494+ type Sum = T;
9595 }
96969797 // Induction
···100100 where
101101 A: Add<B>
102102 {
103103- type Result = Succ<<A as Add<B>>::Result>;
103103+ type Sum = Succ<<A as Add<B>>::Sum>;
104104 }
105105}
106106107107pub mod mul {
108108 use super::prelude::*;
109109110110- pub trait Times<RHS: Nat> {
111111- type Result: Nat;
110110+ pub trait Mul<RHS: Nat> {
111111+ type Prod: Nat;
112112 }
113113114114 // Base case
115115 // N * 0 = 0
116116- impl<T: Nat> Times<_0> for T {
117117- type Result = _0;
116116+ impl<T: Nat> Mul<_0> for T {
117117+ type Prod = _0;
118118 }
119119120120 // Induction
121121 // A * S(B) = A * B + A
122122- impl<A: Nat, B: Nat> Times<Succ<B>> for A
122122+ impl<A: Nat, B: Nat> Mul<Succ<B>> for A
123123 where
124124- A: Times<B>,
125125- <A as Times<B>>::Result: Add<A>
124124+ A: Mul<B>,
125125+ <A as Mul<B>>::Prod: Add<A>
126126 {
127127- type Result = <<A as Times<B>>::Result as Add<A>>::Result;
127127+ type Prod = <<A as Mul<B>>::Prod as Add<A>>::Sum;
128128 }
129129}
130130131131pub mod exp {
132132 use super::prelude::*;
133133134134- pub trait Exp<Pow: Nat> {
135135- type Result: Nat;
134134+ pub trait Exp<P: Nat> {
135135+ type Pow: Nat;
136136 }
137137138138 // Base case
139139 // N ^ 0 = 1
140140 impl<T: Nat> Exp<_0> for T {
141141- type Result = _1;
141141+ type Pow = _1;
142142 }
143143144144 // Induction
···146146 impl<A: Nat, B: Nat> Exp<Succ<B>> for A
147147 where
148148 A: Exp<B>,
149149- <A as Exp<B>>::Result: Times<A>
149149+ <A as Exp<B>>::Pow: Mul<A>
150150 {
151151- type Result =
152152- <<A as Exp<B>>::Result as Times<A>>::Result;
151151+ type Pow =
152152+ <<A as Exp<B>>::Pow as Mul<A>>::Prod;
153153 }
154154}
155155
+3-5
src/main.rs
···11-#![recursion_limit = "132"]
22-31use rspeano::prelude::*;
4253fn five_plus_five_is_ten() {
66- type Claimed10 = <_5 as Add<_5>>::Result;
44+ type Claimed10 = <_5 as Add<_5>>::Sum;
7586 type Verified10 =
97 Succ< // 1
···2321}
24222523fn four_times_two_is_eight() {
2626- type Claimed8 = <_4 as Times<_2>>::Result;
2424+ type Claimed8 = <_4 as Mul<_2>>::Prod;
27252826 type Verified8 =
2927 Succ< // 1
···4139}
42404341fn two_squared_is_four() {
4444- type Claimed4 = <_2 as Exp<_2>>::Result;
4242+ type Claimed4 = <_2 as Exp<_2>>::Pow;
45434644 type Verified4 =
4745 Succ<