|
| 1 | + /** Natural transformation. */ |
| 2 | + trait ~>[F[_], G[_]] { |
| 3 | + def apply[A](fa: F[A]): G[A] |
| 4 | + } |
| 5 | + |
| 6 | + /** Higher-kinded pattern functor typeclass. */ |
| 7 | + trait HFunctor[H[f[_], i]] { |
| 8 | + def hmap[A[_], B[_]](nt: A ~> B): ([x] => H[A,x]) ~> ([x] => H[B,x]) |
| 9 | + } |
| 10 | + |
| 11 | + /** Some HK pattern functor. */ |
| 12 | + // enum ExprF[R[_],I] { |
| 13 | + // case Const[R[_]](i: Int) extends ExprF[R,Int] |
| 14 | + // case Neg[R[_]](l: R[Int]) extends ExprF[R,Int] |
| 15 | + // case Eq[R[_]](l: R[Int], r: R[Int]) extends ExprF[R,Boolean] |
| 16 | + // } |
| 17 | + |
| 18 | + sealed trait ExprF[R[_],I] |
| 19 | + final case class Const[R[_]](i: Int) extends ExprF[R,Int] |
| 20 | + final case class Foo[T](t: T) extends ExprF[Option, T] |
| 21 | + final case class Neg[R[_]](l: R[Int]) extends ExprF[R,Int] |
| 22 | + final case class Eq[R[_]](l: R[Int], r: R[Int]) extends ExprF[R,Boolean] |
| 23 | + |
| 24 | + object X { |
| 25 | + val x = (??? : Neg[Option])._1 |
| 26 | + } |
| 27 | + |
| 28 | + /** Companion. */ |
| 29 | + object ExprF { |
| 30 | + implied hfunctor for HFunctor[ExprF] { |
| 31 | + def hmap[A[_], B[_]](nt: A ~> B): ([x] => ExprF[A,x]) ~> ([x] => ExprF[B,x]) = { |
| 32 | + new ~>[[x] => ExprF[A,x], [x] => ExprF[B,x]] { |
| 33 | + def apply[I](fa: ExprF[A,I]): ExprF[B,I] = fa match { |
| 34 | + case Const(i) => Const(i) |
| 35 | + case Neg(l) => Neg(nt(l)) |
| 36 | + case Eq(l, r) => Eq(nt(l), nt(r)) |
| 37 | + } |
| 38 | + } |
| 39 | + } |
| 40 | + } |
| 41 | + } |
0 commit comments