Last active
March 24, 2018 03:06
-
-
Save Mzk-Levi/752d1e0f2f7f30cd3bda to your computer and use it in GitHub Desktop.
Horizontal & Vertical Compositions of Natural Transformations
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
trait Functor[F[_]] { | |
def map[A, B](as: F[A])(f: A => B): F[B] | |
} | |
object Functor { | |
def apply[F[_]](implicit e: Functor[F]): Functor[F] = e | |
} | |
trait ~>[F[_], G[_]] { | |
def apply[A](x: F[A]): G[A] | |
} | |
def comp[A[_], B[_], C[_]](g: B ~> C)(f: A ~> B): A ~> C = | |
new (A ~> C) { | |
def apply[X](a: A[X]) = g(f(a)) | |
} | |
/** Vertical composition of natural transformations */ | |
def vert[F[_]: Functor, F1[_]: Functor, G[_]: Functor, G1[_]: Functor](a: F1 ~> G1)(b: F ~> G): ~>[({ type l[x] = F1[F[x]] })#l, ({ type l[x] = G1[G[x]] })#l] = | |
new (({ type l[x] = F1[F[x]] })#l ~> ({ type l[x] = G1[G[x]] })#l) { | |
def apply[A](x: F1[F[A]]) = | |
a(Functor[F1].map(x)(b(_))) | |
} | |
/** Horizontal composition of natural transformations */ | |
def horiz[F[_]: Functor, F1[_]: Functor, G[_]: Functor, G1[_]: Functor](a: F1 ~> G1)(b: F ~> G): ~>[({ type l[x] = F1[F[x]] })#l, ({ type l[x] = G1[G[x]] })#l] = | |
new (({ type l[x] = F1[F[x]] })#l ~> ({ type l[x] = G1[G[x]] })#l) { | |
def apply[A](x: F1[F[A]]): G1[G[A]] = | |
Functor[G1].map(a(x))(b(_)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment