Advertisement
mwchase

Never has the adjective "uninhabitable" been more fitting

Jul 13th, 2017
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Rust 1.20 KB | None | 0 0
  1. fn main() {
  2.     println!("{}", (1, (2, Empty())).get::<Succ<Zero>>());
  3. }
  4.  
  5. struct Empty ();
  6.  
  7. trait Cons {
  8.     type Head;
  9.     type Tail: Cons;
  10.    
  11.     fn get_head(&self) -> &Self::Head;
  12.     fn get_tail(&self) -> &Self::Tail;
  13.    
  14.     fn get<T: Peano<Self>>(&self) -> &T::Type where Self: std::marker::Sized {
  15.         T::get(self)
  16.     }
  17. }
  18.  
  19. impl<A, B:Cons> Cons for (A, B) {
  20.     type Head = A;
  21.     type Tail = B;
  22.    
  23.     fn get_head(&self) -> &Self::Head {
  24.         &self.0
  25.     }
  26.     fn get_tail(&self) -> &Self::Tail {
  27.         &self.1
  28.     }
  29. }
  30.  
  31. impl Cons for Empty {
  32.     type Head = Empty;
  33.     type Tail = Empty;
  34.    
  35.     fn get_head(&self) -> &Self::Head {
  36.         panic!()
  37.     }
  38.     fn get_tail(&self) -> &Self::Tail {
  39.         panic!()
  40.     }
  41. }
  42.  
  43. enum Zero {}
  44.  
  45. trait Peano<T: Cons> {
  46.     type Type;
  47.    
  48.     fn get(cons: &T) -> &Self::Type;
  49. }
  50.  
  51. impl<C: Cons> Peano<C> for Zero {
  52.     type Type = C::Head;
  53.    
  54.     fn get(cons: &C) -> &Self::Type {
  55.         cons.get_head()
  56.     }
  57. }
  58.  
  59. struct Succ<T>(T);
  60.  
  61. impl<C: Cons, T: Peano<C::Tail>> Peano<C> for Succ<T> {
  62.     type Type = T::Type;
  63.    
  64.     fn get(cons: &C) -> &Self::Type {
  65.         T::get(cons.get_tail())
  66.     }
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement