Skip to content

Instantly share code, notes, and snippets.

Created April 19, 2016 20:59
Show Gist options
  • Save anonymous/9c502db0fd3e46a6d753a08dc9e0da02 to your computer and use it in GitHub Desktop.
Save anonymous/9c502db0fd3e46a6d753a08dc9e0da02 to your computer and use it in GitHub Desktop.
Shared via Rust Playground
#![feature(trace_macros)]
trace_macros!(true);
/// Piano numbers
#[derive(Debug, Clone, Copy)]
struct S<T>(T);
trait ToInt {
fn to_int(self) -> i32;
}
impl ToInt for i32 {
fn to_int(self) -> i32 {
self
}
}
impl<T> ToInt for S<T>
where T: ToInt
{
fn to_int(self) -> i32 {
self.0.to_int()+1
}
}
macro_rules! to_peano {
(0) => (0);
(1) => (S(0));
(2) => (S(S(0)));
(3) => (S(((0))));
(4) => (S(to_peano!(3)));
(5) => (S(to_peano!(4)));
(6) => (S(to_peano!(5)));
(7) => (S(to_peano!(6)));
(8) => (S(to_peano!(7)));
}
macro_rules! plus {
($n:expr, 0) => ($n);
($n:expr, S($($m:tt)*)) => (plus!(S($n),$($m)*));
//($n:expr, $($m:tt)*) => (plus!($n,to_peano!($($m)*)))
}
fn main() {
let x : S<S<S<i32>>> = plus!(S(0), S(S(0)));
print!("{:?} = {}\n", x, x.to_int());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment