Skip to content

Instantly share code, notes, and snippets.

@chrilves
Last active August 4, 2017 13:40
Show Gist options
  • Save chrilves/85ba64b3988afc9ba999a78a70e3eea2 to your computer and use it in GitHub Desktop.
Save chrilves/85ba64b3988afc9ba999a78a70e3eea2 to your computer and use it in GitHub Desktop.
trait NaturalTransformation[F[_], G[_]] {
import cats.syntax.eq._
import cats.Functor
implicit val F : Functor[F]
implicit val G : Functor[G]
def apply[A](fa : F[A]) : G[A]
/** For an implementation to be valid, this function must
* always returns true.
*
* This function is the code equivalent of the following
* diagram.
*/
def law[B, C](f : B => C)(fb : F[B])(implicit GCEQ : Eq[G[C]]) : Boolean =
apply[C](F.map(fb)(f)) === G.map(apply[B](fb))(f)
/*
forall f : B => C .
apply[B]
F[B] ------------> G[B]
| |
| |
F.map(_)(f) | | G.map(_)(f)
| |
V V
F[C] ------------> G[C]
apply[C]
the diagram must commute, i.e. the functions F.map(_)(f) `andThen` apply[C]
and apply[B] `andThen` G.map(_)(f) must be equal.
*/
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment