Last active
September 17, 2025 16:30
-
-
Save liquidhelium/1ff5aff1c0ea9d48ec07b47f029ef383 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 定义零:作为自然数的起点 | |
| struct Zero; | |
| // 定义后继:Succ<N> 表示自然数 N 的后继(即 N + 1) | |
| struct Succ<N>(N); | |
| // 为自然数定义特性 | |
| trait Natural { | |
| // 获取数值(运行时) | |
| fn value() -> usize; | |
| // 类型级别的值表示(编译时) | |
| type Value: NaturalValue; | |
| } | |
| // 类型级别的值表示 | |
| trait NaturalValue { | |
| const VALUE: usize; | |
| } | |
| // 零的实现 | |
| impl Natural for Zero { | |
| fn value() -> usize { 0 } | |
| type Value = ZeroValue; | |
| } | |
| struct ZeroValue; | |
| impl NaturalValue for ZeroValue { | |
| const VALUE: usize = 0; | |
| } | |
| // 后继的实现 | |
| impl<N: Natural> Natural for Succ<N> { | |
| fn value() -> usize { N::value() + 1 } | |
| type Value = SuccValue<N::Value>; | |
| } | |
| struct SuccValue<V: NaturalValue>(V); | |
| impl<V: NaturalValue> NaturalValue for SuccValue<V> { | |
| const VALUE: usize = V::VALUE + 1; | |
| } | |
| // 类型别名,方便使用 | |
| type One = Succ<Zero>; | |
| type Two = Succ<One>; | |
| type Three = Succ<Two>; | |
| type Four = Succ<Three>; | |
| // 加法:N + M = R | |
| trait Add<M: Natural> { | |
| type Result: Natural; | |
| } | |
| // 零加任何数等于该数 | |
| impl<M: Natural> Add<M> for Zero { | |
| type Result = M; | |
| } | |
| // Succ<N> + M = Succ<N + M> | |
| impl<N: Natural, M: Natural> Add<M> for Succ<N> | |
| where | |
| N: Add<M>, | |
| { | |
| type Result = Succ<<N as Add<M>>::Result>; | |
| } | |
| // 乘法:N * M = R | |
| trait Mul<M: Natural> { | |
| type Result: Natural; | |
| } | |
| // 零乘任何数等于零 | |
| impl<M: Natural> Mul<M> for Zero { | |
| type Result = Zero; | |
| } | |
| // Succ<N> * M = (N * M) + M | |
| impl<N: Natural, M: Natural> Mul<M> for Succ<N> | |
| where | |
| N: Mul<M>, | |
| <N as Mul<M>>::Result: Add<M>, | |
| { | |
| type Result = <<N as Mul<M>>::Result as Add<M>>::Result; | |
| } | |
| // 相等性证明:证明 N == M | |
| trait Eq<M: Natural> {} | |
| // 零等于零 | |
| impl Eq<Zero> for Zero {} | |
| // 如果 N == M,则 Succ<N> == Succ<M> | |
| impl<N: Natural, M: Natural> Eq<Succ<M>> for Succ<N> | |
| where | |
| N: Eq<M>, | |
| {} | |
| // 函数:验证两个自然数相加的结果 | |
| fn verify_addition<N: Natural, M: Natural, R: Natural>() | |
| where | |
| N: Add<M, Result = R>, // 类型级别验证:N + M = R | |
| { | |
| println!("{} + {} = {}", N::value(), M::value(), R::value()); | |
| } | |
| // 函数:验证两个自然数相乘的结果 | |
| fn verify_multiplication<N: Natural, M: Natural, R: Natural>() | |
| where | |
| N: Mul<M, Result = R>, // 类型级别验证:N * M = R | |
| { | |
| println!("{} * {} = {}", N::value(), M::value(), R::value()); | |
| } | |
| // 函数:验证两个自然数相等 | |
| fn verify_equality<N: Natural, M: Natural>() | |
| where | |
| N: Eq<M>, // 类型级别验证:N == M | |
| { | |
| println!("{} == {}", N::value(), M::value()); | |
| } | |
| fn main() { | |
| // 这些调用会在编译时验证类型级别的计算 | |
| // 如果计算不正确,会导致编译错误 | |
| // 验证加法 | |
| verify_addition::<One, Two, Three>(); // 1 + 2 = 3 | |
| verify_addition::<Two, Two, Four>(); // 2 + 2 = 4 | |
| // 验证乘法 | |
| verify_multiplication::<Two, Two, Four>(); // 2 * 2 = 4 | |
| verify_multiplication::<Two, Three, Six>(); // 2 * 3 = 6 | |
| // 验证相等性 | |
| verify_equality::<Two, Two>(); // 2 == 2 | |
| // 下面的代码会导致编译错误,因为 2 + 2 ≠ 5 | |
| // verify_addition::<Two, Two, Five>(); // 编译错误! | |
| // 下面的代码会导致编译错误,因为 3 ≠ 2 | |
| // verify_equality::<Three, Two>(); // 编译错误! | |
| } | |
| // 定义更多的自然数 | |
| type Five = Succ<Four>; | |
| type Six = Succ<Five>; | |
| type Seven = Succ<Six>; | |
| type Eight = Succ<Seven>; | |
| type Nine = Succ<Eight>; | |
| type Ten = Succ<Nine>; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment