-
-
Save mythmon/4e1af0b84fbe25687a3f to your computer and use it in GitHub Desktop.
Shared via Rust Playground
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
fn main() { | |
type One = Succ<Zero>; | |
type Two = Succ<One>; | |
type Three = Succ<Two>; | |
type Five = <Two as Add<Three>>::Result; | |
type Four = <One as Sub<Five>>::Result; | |
println!("Zero: {}", Zero::fmt()); | |
println!("One: {}", One::fmt()); | |
println!("Two: {}", Two::fmt()); | |
println!("Three: {}", Three::fmt()); | |
println!("Four: {}", Four::fmt()); | |
println!("Five: {}", Five::fmt()); | |
} | |
trait Peano { | |
fn fmt() -> String; | |
} | |
struct Zero; | |
impl Peano for Zero { | |
fn fmt() -> String { | |
"Zero".to_string() | |
} | |
} | |
struct Succ<T> { | |
t: T, | |
} | |
impl<T: Peano> Peano for Succ<T> { | |
fn fmt() -> String { | |
format!("Succ<{}>", T::fmt()) | |
} | |
} | |
trait Add<T> { | |
type Result: Peano; | |
} | |
impl<T: Peano> Add<T> for Zero { | |
type Result = T; | |
} | |
impl<T, U> Add<T> for Succ<U> | |
where U: Add<Succ<T>>, | |
{ | |
type Result = <U as Add<Succ<T>>>::Result; | |
} | |
trait Sub<T> { | |
type Result: Peano; | |
} | |
impl<T: Peano> Sub<T> for Zero { | |
type Result = T; | |
} | |
impl<T, U> Sub<Succ<T>> for Succ<U> | |
where T: Peano, | |
U: Peano, | |
U: Sub<T>, | |
{ | |
type Result = <U as Sub<T>>::Result; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment