Create a gist now

Instantly share code, notes, and snippets.

@jeslg /monoid.scala
Last active Jan 10, 2017

What would you like to do?
Encodings generated by @algebra for `Monoid`
trait Monoid[A] {
def mzero(): A;
def mappend(a1: A, a2: A): A
};
trait MonoidFAlgebra {
import scalaz.Isomorphism.$less$tilde$greater;
import scalaz.$tilde$greater;
sealed abstract class Σ[_];
case class Mzero[A]() extends Σ[A];
case class Mappend[A](a1: A, a2: A) extends Σ[A];
object Σ {
import scalaz.Functor;
import org.hablapps.azucar.DerivingFunctor._;
implicit val functorInstance = Functor[Σ]
};
trait FAlgebra[X] extends matryoshka.Algebra[Σ, X];
object FAlgebra {
def apply[A](implicit FA: FAlgebra[A]) = FA
};
val iso: <~>[Monoid, FAlgebra] = {
final class $anon extends <~>[Monoid, FAlgebra] {
def to: ~>[Monoid, FAlgebra] = {
final class $anon extends ~>[Monoid, FAlgebra] {
def apply[A](algebra: Monoid[A]): FAlgebra[A] = {
final class $anon extends FAlgebra[A] {
def apply(fx: Σ[A]): A = fx match {
case Mzero() => algebra.mzero()
case Mappend((a1 @ _), (a2 @ _)) => algebra.mappend(a1, a2)
}
};
new $anon()
}
};
new $anon()
};
def from: ~>[FAlgebra, Monoid] = {
final class $anon extends ~>[FAlgebra, Monoid] {
def apply[A](falgebra: FAlgebra[A]): Monoid[A] = {
final class $anon extends Monoid[A] {
def mzero(): A = falgebra(Mzero());
def mappend(a1: A, a2: A): A = falgebra(Mappend(a1, a2))
};
new $anon()
}
};
new $anon()
}
};
new $anon()
};
implicit def fromFAlgebra[A](implicit FAlgebra: FAlgebra[A]): Monoid[A] = iso.from(FAlgebra);
implicit def fromOAlgebra[A](implicit OAlgebra: Monoid[A]): FAlgebra[A] = iso.to(OAlgebra)
};
object Monoid extends MonoidFAlgebra {
def apply[A](implicit ev: Monoid[A]) = ev
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment