NLinker

Untitled

Jun 19th, 2020
187
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Rust 4.84 KB | None | 0 0
  1. // Rust compiler CRASHES
  2.  
  3. // From the discussion in TG
  4. // https://scastie.scala-lang.org/Odomontois/RmcXO65LQrqR87YcDIwl5w/1
  5. // https://play.rust-lang.org/?version=nightly&mode=release&edition=2018&gist=1956b17e0da475a61dc30396921a3a9a
  6.  
  7. /*
  8. trait Monoid[A]:
  9.     def empty: A
  10.     def (a: A) |+| (b: A): A
  11.  
  12. object Monoid:
  13.     def empty[A](using m: Monoid[A]): A  = m.empty
  14.  
  15.     given Monoid[Int]:
  16.         def empty = 0
  17.         def (x: Int) |+| (y: Int) = x + y
  18.  
  19. trait Measured[A, R]:
  20.     def (a: A) measure: R
  21.  
  22. object Measured:
  23.     def of [A, R](a: A)(using Measured[A, R]): R = a.measure    
  24.  
  25.     given [A] as Measured[A, A]:
  26.         def (a: A) measure: A = a
  27.  
  28.     given [A, R](using Measured[A, R], Monoid[R]) as Measured[(A, A), R]:
  29.         def (a: (A, A)) measure: R = a._1.measure |+| a._2.measure
  30.  
  31.     given [A, R](using Measured[A, R], Monoid[R]) as Measured[Option[A], R]:
  32.         def (a: Option[A]) measure: R = a.fold(Monoid.empty)(_.measure)
  33.  
  34. enum CherryTree[A]:    
  35.     case Empty()
  36.     case One(a: A)
  37.     case Branch(left: Option[A], mid: CherryTree[(A, A)], right: Option[A])
  38.  
  39.     def :+(a: A): CherryTree[A] = this match
  40.         case Empty()               => One(a)
  41.         case One(b)                => Branch(None, One((b, a)), None)
  42.         case Branch(l, m, None)    => Branch(l, m, Some(a))
  43.         case Branch(l, m, Some(b)) => Branch(l, m :+ (b, a), None)
  44.  
  45.     def +:(a: A): CherryTree[A] = this match
  46.         case Empty()               => One(a)
  47.         case One(b)                => Branch(None, One((a, b)), None)
  48.         case Branch(None, m, r)    => Branch(Some(a), m, r)
  49.         case Branch(Some(b), m, r) => Branch(None, (b, a) +: m, r)
  50.  
  51. object CherryTree:
  52.     def apply[A](a: A*): CherryTree[A] = a.foldLeft(CherryTree.Empty())(_ :+ _)
  53.  
  54.     given measured[A, R](using Measured[A, R], Monoid[R]) as Measured[CherryTree[A], R]:
  55.         def (tree: CherryTree[A]) measure = tree match
  56.             case CherryTree.Empty()          => Monoid.empty
  57.             case CherryTree.One(a)           => a.measure
  58.             case CherryTree.Branch(l, m, r)  =>
  59.                 Measured.of[R = R](l) |+| Measured.of[R = R](m) |+| Measured.of[R = R](r)
  60.  
  61. @main def cherryTreeMain() =
  62.     val tree = CherryTree(1, 2, 3, 4, 5, 6, 7)
  63.     println(tree)
  64.     println(tree.measure)
  65. */
  66.  
  67.  
  68. trait Monoid {
  69.     fn empty() -> Self;
  70.     fn plus(self, b: Self) -> Self;
  71. }
  72.  
  73. impl Monoid for i32 {
  74.     fn empty() -> Self { 0 }
  75.     fn plus(self, b: i32) -> Self { self + b }
  76. }
  77.  
  78. trait Measured<R> {
  79.     fn measure(self) -> R;
  80. }
  81.  
  82. impl<A> Measured<A> for A {
  83.     fn measure(self) -> A { self }
  84. }
  85.  
  86. impl<A: Measured<R>, R: Monoid> Measured<R> for (A, A) {
  87.     fn measure(self) -> R {
  88.         self.0.measure().plus(self.1.measure())
  89.     }
  90. }
  91.  
  92. impl<A: Measured<R>, R: Monoid> Measured<R> for Option<A> {
  93.     fn measure(self) -> R {
  94.         self.map(|x| x.measure()).unwrap_or(Monoid::empty())
  95.     }
  96. }
  97.  
  98. #[derive(Debug)]
  99. enum CherryTree<A> {
  100.     Empty(),
  101.     One(A),
  102.     Branch { left: Option<A>, mid: Box<CherryTree<(A, A)>>, right: Option<A> }
  103. }
  104.  
  105. impl<A> CherryTree<A> {
  106.     fn right(self, a: A) -> Self {
  107.         match self {
  108.             CherryTree::Empty() =>
  109.                 CherryTree::One(a),
  110.             CherryTree::One(b) =>
  111.                 CherryTree::Branch {left: None, mid: Box::new(CherryTree::One((b, a))), right: None},
  112.             CherryTree::Branch { left, mid, right: None } =>
  113.                 CherryTree::Branch {left, mid, right: Some(a)},
  114.             CherryTree::Branch { left, mid, right: Some(b) } =>
  115.                 CherryTree::Branch { left, mid: Box::new(mid.right((b, a))), right: None }
  116.         }
  117.     }
  118.  
  119.     fn left(self, a: A) -> Self {
  120.         match self {
  121.             CherryTree::Empty() =>
  122.                 CherryTree::One(a),
  123.             CherryTree::One(b) =>
  124.                 CherryTree::Branch {left: None, mid: Box::new(CherryTree::One((a, b))), right: None},
  125.             CherryTree::Branch { left: None, mid, right } =>
  126.                 CherryTree::Branch {left: Some(a), mid, right},
  127.             CherryTree::Branch { left: Some(b), mid, right } =>
  128.                 CherryTree::Branch { left: None, mid: Box::new(mid.left((b, a))), right }
  129.         }
  130.     }
  131. }
  132.  
  133. impl<'a, A: Measured<R>, R: Monoid> Measured<R> for &'a CherryTree<A> {
  134.     fn measure(self) -> R {
  135.         match self {
  136.             CherryTree::Empty() => Monoid::empty(),
  137.             CherryTree::One(a) => a.measure(),
  138.             CherryTree::Branch { left, mid, right } =>
  139.                 left.measure().plus(mid.as_ref().measure()).plus(right.measure()),
  140.         }
  141.     }
  142. }
  143.  
  144.  
  145. fn main() {
  146.     let tree = CherryTree::Empty()
  147.         .right(1)
  148.         .right(2)
  149.         .right(3)
  150.         .right(4)
  151.         .right(5)
  152.         .right(6)
  153.         .right(7);
  154.  
  155.     println!("{:?}", tree);
  156.     println!("{:?}", tree.measure());
  157. }
Add Comment
Please, Sign In to add comment