Skip to content

Instantly share code, notes, and snippets.

@hexx
Created December 11, 2012 16:07
Show Gist options
  • Save hexx/4259779 to your computer and use it in GitHub Desktop.
Save hexx/4259779 to your computer and use it in GitHub Desktop.
MonoidalLaw
trait MonoidalLaw extends FunctorLaw {
def iso1[A] = new (A <=> (Unit, A)) {
def to = a => ((), a)
def from = { case (_, a) => a }
}
def iso2[A] = new (A <=> (A, Unit)) {
def to = a => (a, ())
def from = { case (a, _) => a }
}
def iso3[A, B, C] = new (((A, B), C) <=> (A, (B, C))) {
def to = { case ((a, b), c) => (a, (b, c)) }
def from = { case (a, (b, c)) => ((a, b), c) }
}
def naturality[A, B](a: F[A], b: F[B], f: B => A, g: A => B)(implicit FAB: Equal[F[(A,B)]]): Boolean =
FAB.equal(**(map(b)(f), map(a)(g)), map(**(a, b)){case (x, y) => (f(y), g(x))})
def leftIdentity[A](fa: F[A])(implicit FA: Equal[F[A]], FUA: Equal[F[(Unit, A)]]): Boolean =
FA.equal(map(**(unit, fa))(iso1.from), fa) && FUA.equal(**(unit, fa), map(fa)(iso1.to))
def rightIdentity[A](fa: F[A])(implicit FA: Equal[F[A]], FAU: Equal[F[(A, Unit)]]): Boolean =
FA.equal(map(**(fa, unit))(iso2.from), fa) && FAU.equal(**(fa, unit), map(fa)(iso2.to))
def associativity[A, B, C](a: F[A], b: F[B], c: F[C])(implicit FABC1: Equal[F[((A, B), C)]], FABC2: Equal[F[(A, (B, C))]]): Boolean =
FABC1.equal(map(**(a, **(b, c)))(iso3.from), **(**(a, b), c)) && FABC2.equal(**(a, **(b, c)), map(**(**(a, b), c))(iso3.to))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment