Skip to content

Instantly share code, notes, and snippets.

@mythmon
Forked from anonymous/playground.rs
Last active August 29, 2015 14:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mythmon/4e1af0b84fbe25687a3f to your computer and use it in GitHub Desktop.
Save mythmon/4e1af0b84fbe25687a3f 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