Skip to content

Instantly share code, notes, and snippets.

Created August 5, 2015 21:06
Show Gist options
  • Save anonymous/14ce6b2fb103ec780cd9 to your computer and use it in GitHub Desktop.
Save anonymous/14ce6b2fb103ec780cd9 to your computer and use it in GitHub Desktop.
Shared via Rust Playground
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