Skip to content

Instantly share code, notes, and snippets.

@wedens
Last active August 29, 2015 14:24
Show Gist options
  • Save wedens/b968e4b9a403ce136c64 to your computer and use it in GitHub Desktop.
Save wedens/b968e4b9a403ce136c64 to your computer and use it in GitHub Desktop.
two free monads
object FreeCoproduct {
import scalaz._, Scalaz._
import shapeless._
import ops.coproduct.{Inject, Selector}
import Shapoyo._
import Free._
sealed trait Test1Op[A]
case object Test1Alg extends Test1Op[Int]
type Test1[A] = Free.FreeC[Test1Op, A]
implicit val MonadTest1: Monad[Test1] =
Free.freeMonad[({type λ[α] = Coyoneda[Test1Op, α]})#λ]
val test1Alg = Free.liftFC(Test1Alg)
val test1ToId: Test1Op ~> Id =
new (Test1Op ~> Id) {
def apply[A](fa: Test1Op[A]) =
fa match {
case Test1Alg => 1234
}
}
sealed trait Test2Op[A]
case object Test2Alg extends Test2Op[String]
type Test2[A] = Free.FreeC[Test2Op, A]
implicit val MonadTest2: Monad[Test2] =
Free.freeMonad[({type λ[α] = Coyoneda[Test2Op, α]})#λ]
val test2Alg = Free.liftFC(Test2Alg)
val test2ToId: Test2Op ~> Id =
new (Test2Op ~> Id) {
def apply[A](fa: Test2Op[A]) =
fa match {
case Test2Alg => "string"
}
}
type App[A] = Test1[A] :+: Test2[A] :+: CNil
type CoyoApp[A] = Coyoneda[App, A]
type FreeApp[A] = Free.FreeC[App, A]
def f: FreeApp[String] =
for {
a <- test1Alg
b <- test2Alg
} yield a.show + b
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment