├── project ├── build.properties ├── plugins.sbt └── Dependencies.scala ├── core └── src │ └── main │ └── scala │ └── eleutheros │ ├── Fixed.scala │ ├── EitherF.scala │ └── Capabilities.scala ├── .gitignore ├── README.md ├── examples └── src │ ├── test │ └── scala │ │ ├── FreeMonadSpec.scala │ │ └── SeqParSpec.scala │ └── main │ └── scala │ ├── FreeMonadEitherF.scala │ ├── SeqPar.scala │ └── FreeMonadIota.scala ├── bench └── src │ └── main │ └── scala │ ├── SeqParBench.scala │ └── FreeMonadBench.scala └── LICENSE /project/build.properties: -------------------------------------------------------------------------------- 1 | sbt.version=0.13.15 2 | -------------------------------------------------------------------------------- /project/plugins.sbt: -------------------------------------------------------------------------------- 1 | addSbtPlugin("pl.project13.scala" % "sbt-jmh" % "0.2.25") 2 | -------------------------------------------------------------------------------- /core/src/main/scala/eleutheros/Fixed.scala: -------------------------------------------------------------------------------- 1 | package eleutheros 2 | 3 | /** 4 | * Fix point type for a Capability 5 | */ 6 | final case class Fixed[ 7 | T[_, _[_], _[_], _], 8 | E, F[_], A 9 | ](unfix: T[E, Fixed[T, E, F, ?], F, A]) 10 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | project/boot 2 | target 3 | .ensime 4 | .ensime_lucene 5 | .ensime_cache 6 | TAGS 7 | \#*# 8 | *~ 9 | .#* 10 | .lib 11 | .history 12 | .*.swp 13 | .idea 14 | .idea/* 15 | .idea_modules 16 | .DS_Store 17 | .sbtrc 18 | *.sublime-project 19 | *.sublime-workspace 20 | tests.iml 21 | -------------------------------------------------------------------------------- /project/Dependencies.scala: -------------------------------------------------------------------------------- 1 | import sbt._ 2 | 3 | object Dependencies { 4 | lazy val scalatest = "org.scalatest" %% "scalatest" % "3.0.1" 5 | lazy val cats = "org.typelevel" %% "cats" % "0.9.0" 6 | lazy val freestyle = "io.frees" %% "freestyle" % "0.3.0" 7 | lazy val iota = "com.47deg" %% "iota-core" % "0.2.1-SNAPSHOT" 8 | lazy val kindProjector = "org.spire-math" % "kind-projector" % "0.9.4" 9 | } 10 | -------------------------------------------------------------------------------- /core/src/main/scala/eleutheros/EitherF.scala: -------------------------------------------------------------------------------- 1 | package eleutheros 2 | 3 | sealed trait EitherF[ 4 | T1[_, _[_], _[_], _], 5 | T2[_, _[_], _[_], _], 6 | +E, Z[_], F[_], A 7 | ] 8 | 9 | object EitherF { 10 | type H[ 11 | T1[_, _[_], _[_], _], 12 | T2[_, _[_], _[_], _] 13 | ] = { 14 | type Out[E0, Z0[_], F0[_], A0] = EitherF[T1, T2, E0, Z0, F0, A0] 15 | } 16 | } 17 | 18 | final case class LeftF[ 19 | T1[_, _[_], _[_], _], 20 | T2[_, _[_], _[_], _], 21 | E, Z[_], F[_], A 22 | ](value: T1[E, Z, F, A]) extends EitherF[T1, T2, E, Z, F, A] 23 | 24 | final case class RightF[ 25 | T1[_, _[_], _[_], _], 26 | T2[_, _[_], _[_], _], 27 | E, Z[_], F[_], A 28 | ](value: T2[E, Z, F, A]) extends EitherF[T1, T2, E, Z, F, A] 29 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Eleutheros 2 | 3 | Free structures a la carte 4 | 5 | This is an experimental POC of the "fixed free" from John De Goes' talk "Post-Free: Life After Free Monads" ([slides][post-free-slides], [video][post-free-video]). 6 | 7 | ## Example : Free monad 8 | 9 | ```scala 10 | import eleutheros._ 11 | import iota._ 12 | import iota.QList.:: 13 | 14 | type MonadCapabilities = Pure :: Effect :: Sequence :: QNil 15 | 16 | type FreeMonad[F[_], A] = Fixed[CopQ.OnlyL[MonadCapabilities]#Out, Nothing, F, A] 17 | ``` 18 | 19 | Eleutheros uses an unpublished branch of [iota][iota] which contains `CopQ`, a coproduct for capabilities like `Pure` and `Effect` (of type `T[_, _[_], _[_], _]`). 20 | 21 | 22 | [iota]: https://github.com/47deg/iota 23 | [post-free-slides]: https://www.slideshare.net/jdegoes/postfree-life-after-free-monads 24 | [post-free-video]: https://www.youtube.com/watch?v=A-lmrvsUi2Y 25 | -------------------------------------------------------------------------------- /examples/src/test/scala/FreeMonadSpec.scala: -------------------------------------------------------------------------------- 1 | package eleutheros 2 | 3 | import cats.arrow.FunctionK 4 | import cats.instances.option._ 5 | import cats.syntax.flatMap._ 6 | import cats.syntax.functor._ 7 | import org.scalatest.{FlatSpec, Matchers} 8 | 9 | class FreeMonadSpec extends FlatSpec with Matchers { 10 | 11 | "eleutheros" should "be possible to create a Free monad with iota's CopQ" in { 12 | import FreeMonadIota._ 13 | import FreeMonad._ 14 | 15 | val one = FreeMonad.pure[Option, Int](1) 16 | val two = FreeMonad.liftF[Option, Int](Some(2)) 17 | val f: Int => FreeMonad[Option, Int] = i => pure(i + 1) 18 | 19 | val xyz = for { 20 | x <- one // 1 21 | y <- two // 2 22 | z <- f(y) // 2 + 1 = 3 23 | } yield x + y + z // 1 + 2 + 3 = 6 24 | 25 | 26 | val result: Option[Int] = FreeMonad.foldMap(xyz)(FunctionK.id) 27 | 28 | result should be (Some(6)) 29 | } 30 | 31 | it should "be possible to create a Free monad with EitherF" in { 32 | import FreeMonadEitherF._ 33 | import FreeMonad._ 34 | 35 | val one = FreeMonad.pure[Option, Int](1) 36 | val two = FreeMonad.liftF[Option, Int](Some(2)) 37 | val f: Int => FreeMonad[Option, Int] = i => pure(i + 1) 38 | 39 | val xyz = for { 40 | x <- one // 1 41 | y <- two // 2 42 | z <- f(y) // 2 + 1 = 3 43 | } yield x + y + z // 1 + 2 + 3 = 6 44 | 45 | 46 | val result: Option[Int] = FreeMonad.foldMap(xyz)(FunctionK.id) 47 | 48 | result should be (Some(6)) 49 | } 50 | 51 | } 52 | 53 | 54 | 55 | -------------------------------------------------------------------------------- /bench/src/main/scala/SeqParBench.scala: -------------------------------------------------------------------------------- 1 | package eleutheros.bench 2 | 3 | import cats.arrow.FunctionK 4 | import cats.instances.option._ 5 | import cats.syntax.apply._ 6 | import cats.syntax.flatMap._ 7 | import cats.syntax.functor._ 8 | import org.openjdk.jmh.annotations.{Benchmark, Scope, State} 9 | 10 | @State(Scope.Benchmark) 11 | class SeqParBench { 12 | 13 | @Benchmark 14 | def eleutherosIotaSeqPar: Option[Int] = { 15 | import eleutheros.SeqParExample._ 16 | import SeqPar._ 17 | 18 | val one = SeqPar.pure[Option, Int](1) 19 | val two = SeqPar.liftF[Option, Int](Some(2)) 20 | val f: Int => SeqPar[Option, Int] = i => SeqPar.pure(i + 1) 21 | 22 | val xyz: SeqPar[Option, Int] = for { 23 | x <- one // 1 24 | y <- two // 2 25 | z <- f(y).map2(two)(_ + _) // (2 + 1) + 2 = 5 26 | } yield x + y + z // 1 + 2 + 5 = 8 27 | 28 | val result: Option[Int] = SeqPar.foldMap(xyz)(FunctionK.id) 29 | 30 | result 31 | } 32 | 33 | @Benchmark 34 | def freestyleFreeS: Option[Int] = { 35 | import cats.~> 36 | import freestyle._ 37 | 38 | val one = FreeS.pure[Option, Int](1) 39 | val two = FreeS.liftFA[Option, Int](Some(2)) 40 | val f: Int => FreeS[Option, Int] = i => FreeS.pure(i + 1) 41 | 42 | val xyz = for { 43 | x <- one // 1 44 | y <- two // 2 45 | z <- f(y).map2(two)(_ + _) // (2 + 1) + 2 = 5 46 | } yield x + y + z // 1 + 2 + 5 = 8 47 | 48 | 49 | implicit val fk: Option ~> Option = FunctionK.id[Option] 50 | val result: Option[Int] = xyz.interpret[Option] 51 | 52 | result 53 | } 54 | 55 | } -------------------------------------------------------------------------------- /core/src/main/scala/eleutheros/Capabilities.scala: -------------------------------------------------------------------------------- 1 | package eleutheros 2 | 3 | /** 4 | * Capabilities 5 | * 6 | * A Capability has type `T[E, Z[_], F[_], A]` 7 | * 8 | * E = error 9 | * Z[_] = self type combined structure 10 | * F[_] = effect type 11 | * A = value type 12 | */ 13 | 14 | final case class Pure[E, Z[_], F[_], A](a: A) 15 | 16 | final case class Effect[E, Z[_], F[_], A](fa: F[A]) 17 | 18 | trait Sequence[E, Z[_], F[_], A] { 19 | type A0 20 | 21 | def a: Z[A0] 22 | def f: A0 => Z[A] 23 | 24 | override def toString: String = 25 | "Sequence(" + a.toString + ", " + f.toString + ")" 26 | } 27 | 28 | object Sequence { 29 | type Aux[E, Z[_], F[_], A, I] = Sequence[E, Z, F, A] { type A0 = I } 30 | 31 | def apply[E, Z[_], F[_], A, I](zi: Z[I], iza: I => Z[A]): Aux[E, Z, F, A, I] = 32 | new Sequence[E, Z, F, A] { 33 | type A0 = I 34 | val a = zi 35 | val f = iza 36 | } 37 | } 38 | 39 | trait Parallel[E, Z[_], F[_], A] { 40 | type B0 41 | type C0 42 | 43 | def left: Z[B0] 44 | def right: Z[C0] 45 | def join: (B0, C0) => A 46 | 47 | override def toString: String = 48 | "Parallel(" + left.toString + ", " + right.toString + ", " + join.toString + ")" 49 | } 50 | 51 | object Parallel { 52 | type Aux[E, Z[_], F[_], A, B, C] = Parallel[E, Z, F, A] { type B0 = B; type C0 = C } 53 | 54 | def apply[E, Z[_], F[_], A, B, C](l: Z[B], r: Z[C])(j: (B, C) => A): Aux[E, Z, F, A, B, C] = 55 | new Parallel[E, Z, F, A] { 56 | type B0 = B 57 | type C0 = C 58 | val left = l 59 | val right = r 60 | val join = j 61 | } 62 | } 63 | 64 | case class Failure[E, Z[_], F[_], A](error: E) 65 | 66 | case class Recover[E, Z[_], F[_], A](value: Z[A], f: E => Z[A]) 67 | 68 | case class FirstSuccess[E, Z[_], F[_], A](first: Z[A], second: Z[A]) 69 | -------------------------------------------------------------------------------- /examples/src/test/scala/SeqParSpec.scala: -------------------------------------------------------------------------------- 1 | package eleutheros 2 | 3 | import cats.arrow.FunctionK 4 | import cats.instances.option._ 5 | import cats.syntax.apply._ 6 | import cats.syntax.flatMap._ 7 | import cats.syntax.functor._ 8 | import org.scalatest.{FlatSpec, Matchers} 9 | 10 | class SeqParSpec extends FlatSpec with Matchers { 11 | 12 | "eleutheros" should "be possible to create a SeqPar with iota's CopQ" in { 13 | 14 | import SeqParExample._ 15 | import SeqPar._ 16 | 17 | val one = SeqPar.pure[Option, Int](1) 18 | val two = SeqPar.liftF[Option, Int](Some(2)) 19 | val f: Int => SeqPar[Option, Int] = i => SeqPar.pure(i + 1) 20 | 21 | val xyz = for { 22 | x <- one 23 | y <- two 24 | z <- f(y).map2(two)(_ + _) // (2 + 1) + 2 = 5 25 | } yield x + y + z // 1 + 2 + 5 = 8 26 | 27 | val result: Option[Int] = SeqPar.foldMap(xyz)(FunctionK.id) 28 | 29 | result should be (Some(8)) 30 | } 31 | 32 | it should "analyze" in { 33 | 34 | import cats.~> 35 | import cats.instances.list._ 36 | import cats.syntax.cartesian._ 37 | import SeqParExample._ 38 | import SeqPar._ 39 | 40 | sealed abstract class GetOp[A] extends Product with Serializable 41 | case class Get(id: Int) extends GetOp[String] 42 | 43 | val lift: Int => SeqPar[GetOp, String] = 44 | i => SeqPar.liftF[GetOp, String](Get(i)) 45 | 46 | val a = (lift(1) |@| lift(2) |@| lift(3)).map(_ + _ + _) 47 | val b = a.flatMap(s => lift(s.length)) 48 | val c = (lift(0) |@| b).map(_ + _) 49 | 50 | val ids: GetOp ~> λ[α => List[Int]] = 51 | λ[GetOp ~> λ[α => List[Int]]]{ case Get(x) => x :: Nil } 52 | 53 | SeqPar.analyze(a)(ids) should be (List(1, 2, 3)) 54 | SeqPar.analyze(b)(ids) should be (List(1, 2, 3)) 55 | SeqPar.analyze(c)(ids) should be (List(0, 1, 2, 3)) 56 | } 57 | 58 | } 59 | -------------------------------------------------------------------------------- /bench/src/main/scala/FreeMonadBench.scala: -------------------------------------------------------------------------------- 1 | package eleutheros.bench 2 | 3 | import cats.arrow.FunctionK 4 | import cats.instances.option._ 5 | import cats.syntax.flatMap._ 6 | import cats.syntax.functor._ 7 | import org.openjdk.jmh.annotations.{Benchmark, Scope, State} 8 | 9 | @State(Scope.Benchmark) 10 | class FreeMonadBench { 11 | 12 | @Benchmark 13 | def eleutherosIotaFreeMonad: Option[Int] = { 14 | import eleutheros.FreeMonadIota._ 15 | import FreeMonad._ 16 | 17 | val one = FreeMonad.pure[Option, Int](1) 18 | val two = FreeMonad.liftF[Option, Int](Some(2)) 19 | val f: Int => FreeMonad[Option, Int] = i => pure(i + 1) 20 | 21 | val xyz = for { 22 | x <- one // 1 23 | y <- two // 2 24 | z <- f(y) // 2 + 1 = 3 25 | } yield x + y + z // 1 + 2 + 3 = 6 26 | 27 | FreeMonad.foldMap(xyz)(FunctionK.id) 28 | } 29 | 30 | // @Benchmark 31 | // def eleutherosIotaFreeMonad2: Option[Int] = { 32 | // import eleutheros.FreeMonadIota._ 33 | // import FreeMonad._ 34 | 35 | // val one = FreeMonad.pure[Option, Int](1) 36 | // val two = FreeMonad.liftF[Option, Int](Some(2)) 37 | // val f: Int => FreeMonad[Option, Int] = i => pure(i + 1) 38 | 39 | // val xyz = for { 40 | // x <- one // 1 41 | // y <- two // 2 42 | // z <- f(y) // 2 + 1 = 3 43 | // } yield x + y + z // 1 + 2 + 3 = 6 44 | 45 | // FreeMonad.foldMap2(xyz)(FunctionK.id) 46 | // } 47 | 48 | @Benchmark 49 | def eleutherosEitherFFreeMonad: Option[Int] = { 50 | import eleutheros.FreeMonadEitherF._ 51 | import FreeMonad._ 52 | 53 | val one = FreeMonad.pure[Option, Int](1) 54 | val two = FreeMonad.liftF[Option, Int](Some(2)) 55 | val f: Int => FreeMonad[Option, Int] = i => pure(i + 1) 56 | 57 | val xyz = for { 58 | x <- one // 1 59 | y <- two // 2 60 | z <- f(y) // 2 + 1 = 3 61 | } yield x + y + z // 1 + 2 + 3 = 6 62 | 63 | FreeMonad.foldMap(xyz)(FunctionK.id) 64 | } 65 | 66 | @Benchmark 67 | def catsFree: Option[Int] = { 68 | import cats.free.Free 69 | 70 | val one = Free.pure[Option, Int](1) 71 | val two = Free.liftF[Option, Int](Some(2)) 72 | val f: Int => Free[Option, Int] = i => Free.pure(i + 1) 73 | 74 | val xyz = for { 75 | x <- one // 1 76 | y <- two // 2 77 | z <- f(y) // 2 + 1 = 3 78 | } yield x + y + z // 1 + 2 + 3 = 6 79 | 80 | xyz.foldMap(FunctionK.id) 81 | } 82 | } 83 | -------------------------------------------------------------------------------- /examples/src/main/scala/FreeMonadEitherF.scala: -------------------------------------------------------------------------------- 1 | package eleutheros 2 | 3 | import cats.~> 4 | import cats.Monad 5 | import cats.syntax.flatMap._ 6 | import scala.annotation.tailrec 7 | 8 | object FreeMonadEitherF { 9 | 10 | type PureSeqF[E0, Z0[_], F0[_], A0] = EitherF[Pure, Sequence, E0, Z0, F0, A0] 11 | type FreeMonadF[E0, Z0[_], F0[_], A0] = EitherF[Effect, PureSeqF, E0, Z0, F0, A0] 12 | 13 | type FreeMonad[F[_], A] = Fixed[FreeMonadF, Nothing, F, A] 14 | 15 | object FreeMonad { 16 | def pure[F[_], A](a: A): FreeMonad[F, A] = 17 | Fixed[FreeMonadF, Nothing, F, A]( 18 | RightF[Effect, PureSeqF, Nothing, FreeMonad[F, ?], F, A]( 19 | LeftF[Pure, Sequence, Nothing, FreeMonad[F, ?], F, A]( 20 | Pure[Nothing, FreeMonad[F, ?], F, A](a)))) 21 | 22 | def liftF[F[_], A](fa: F[A]): FreeMonad[F, A] = 23 | Fixed[FreeMonadF, Nothing, F, A]( 24 | LeftF[Effect, PureSeqF, Nothing, FreeMonad[F, ?], F, A]( 25 | Effect[Nothing, FreeMonad[F, ?], F, A](fa))) 26 | 27 | implicit def freeMonadMonad[F[_]]: Monad[FreeMonad[F, ?]] = 28 | new Monad[FreeMonad[F, ?]] { 29 | def pure[A](a: A): FreeMonad[F, A] = FreeMonad.pure(a) 30 | 31 | def flatMap[A, B](fa: FreeMonad[F, A])(ff: A => FreeMonad[F, B]) : FreeMonad[F, B] = 32 | Fixed[FreeMonadF, Nothing, F, B]( 33 | RightF[Effect, PureSeqF, Nothing, FreeMonad[F, ?], F, B]( 34 | RightF[Pure, Sequence, Nothing, FreeMonad[F, ?], F, B]( 35 | new Sequence[Nothing, FreeMonad[F, ?], F, B] { 36 | type A0 = A 37 | 38 | val a: FreeMonad[F, A0] = fa 39 | val f: A0 => FreeMonad[F, B] = ff 40 | }))) 41 | 42 | def tailRecM[A, B](a: A)(f: A => FreeMonad[F, Either[A, B]]): FreeMonad[F, B] = 43 | flatMap(f(a)) { 44 | case Left(a1) => tailRecM(a1)(f) 45 | case Right(b) => pure(b) 46 | } 47 | } 48 | 49 | def foldMap[F[_], M[_], A] 50 | (fa: FreeMonad[F, A]) 51 | (fk: F ~> M) 52 | (implicit M: Monad[M]) 53 | : M[A] = 54 | M.tailRecM(fa)(step(_).unfix match { 55 | case LeftF(Effect(ef)) => M.map(fk(ef))(Right(_)) 56 | case RightF(LeftF(Pure(a))) => M.pure(Right(a)) 57 | case RightF(RightF(seq)) => M.map(foldMap(seq.a)(fk))(cc => Left(seq.f(cc))) 58 | }) 59 | 60 | @tailrec 61 | final def step[F[_], A](fa: FreeMonad[F, A]): FreeMonad[F, A] = fa.unfix match { 62 | case RightF(RightF(seq: Sequence[Nothing, Fixed[FreeMonadF, Nothing, F, ?], F, _])) => 63 | seq.a.unfix match { 64 | case RightF(RightF(seq2: Sequence[Nothing, Fixed[FreeMonadF, Nothing, F, ?], F, _])) => 65 | step(seq2.a.flatMap(seq2.f(_).flatMap(seq.f))) 66 | case RightF(LeftF(Pure(a))) => 67 | step(seq.f(a)) 68 | case _ => fa 69 | } 70 | case _ => fa 71 | } 72 | 73 | } 74 | 75 | } -------------------------------------------------------------------------------- /examples/src/main/scala/SeqPar.scala: -------------------------------------------------------------------------------- 1 | package eleutheros 2 | 3 | import cats.{~>, Monad, Monoid} 4 | import cats.data.Const 5 | import cats.syntax.flatMap._ 6 | import iota._ 7 | import iota.QList.:: 8 | import scala.annotation.tailrec 9 | 10 | object SeqParExample { 11 | 12 | type Capabilities = Pure :: Effect :: Parallel :: Sequence :: QNil 13 | 14 | type NoError = Nothing { type T = Unit } 15 | 16 | type SeqPar[F[_], A] = Fixed[CopQ.OnlyL[Capabilities]#Out, NoError, F, A] 17 | 18 | object SeqPar { 19 | 20 | val IPure: CopQ.Inject[Pure, CopQ.OnlyL[Capabilities]#Out] = scala.Predef.implicitly 21 | val IEffect: CopQ.Inject[Effect, CopQ.OnlyL[Capabilities]#Out] = scala.Predef.implicitly 22 | val IParallel: CopQ.Inject[Parallel, CopQ.OnlyL[Capabilities]#Out] = scala.Predef.implicitly 23 | val ISequence: CopQ.Inject[Sequence, CopQ.OnlyL[Capabilities]#Out] = scala.Predef.implicitly 24 | 25 | def pure[F[_], A](a: A): SeqPar[F, A] = 26 | Fixed(IPure.inj(Pure(a))) 27 | 28 | def liftF[F[_], A](fa: F[A]): SeqPar[F, A] = 29 | Fixed(IEffect.inj(Effect(fa))) 30 | 31 | implicit def monadInstance[F[_]]: Monad[SeqPar[F, ?]] = 32 | new Monad[SeqPar[F, ?]] { 33 | def pure[A](a: A): SeqPar[F, A] = SeqPar.pure(a) 34 | 35 | override def map2[A, B, C](fa: SeqPar[F, A], fb: SeqPar[F, B])(f: (A, B) => C): SeqPar[F, C] = 36 | Fixed(IParallel.inj(Parallel(fa, fb)(f))) 37 | 38 | override def ap[A, B](ff: SeqPar[F, A => B])(fa: SeqPar[F, A]): SeqPar[F, B] = 39 | map2(ff, fa)(_.apply(_)) 40 | 41 | override def product[A, B](fa: SeqPar[F, A], fb: SeqPar[F, B]): SeqPar[F, (A, B)] = 42 | map2(fa, fb)(Tuple2.apply) 43 | 44 | override def tuple2[A, B](fa: SeqPar[F, A], fb: SeqPar[F, B]): SeqPar[F, (A, B)] = 45 | product(fa, fb) 46 | 47 | def flatMap[A, B](fa: SeqPar[F, A])(ff: A => SeqPar[F, B]) : SeqPar[F, B] = 48 | Fixed(ISequence.inj(Sequence(fa, ff))) 49 | 50 | def tailRecM[A, B](a: A)(f: A => SeqPar[F, Either[A, B]]): SeqPar[F, B] = 51 | flatMap(f(a)) { 52 | case Left(a1) => tailRecM(a1)(f) 53 | case Right(b) => pure(b) 54 | } 55 | } 56 | 57 | 58 | final def foldMap[F[_], G[_], A](spa: SeqPar[F, A])(f: F ~> G)(implicit G: Monad[G]): G[A] = 59 | step(spa).unfix match { 60 | case IPure(Pure(a)) => G.pure(a) 61 | case IEffect(Effect(fa)) => f(fa) 62 | 63 | // not stack safe 64 | case IParallel(par) => 65 | G.map2(foldMap(par.left)(f), foldMap(par.right)(f))(par.join) 66 | 67 | // in a Free monad seq.a should always be a Suspend / Effect 68 | // here this can also be a Parallel though 69 | case ISequence(seq) => 70 | G.flatMap(foldMap(seq.a)(f))(c => foldMap(seq.f(c))(f)) 71 | } 72 | 73 | @tailrec 74 | final def step[F[_], A](fa: SeqPar[F, A]): SeqPar[F, A] = fa.unfix match { 75 | case ISequence(seq) => 76 | seq.a.unfix match { 77 | case ISequence(seq2) => 78 | step(seq2.a.flatMap(seq2.f(_).flatMap(seq.f))) 79 | case IPure(Pure(a)) => 80 | step(seq.f(a)) 81 | case _ => fa 82 | } 83 | case _ => fa 84 | } 85 | 86 | final def analyze[F[_], M, A] 87 | (spa: SeqPar[F, A]) 88 | (fk: F ~> λ[α => M]) 89 | (implicit M: Monoid[M]) 90 | : M = 91 | (spa.unfix match { 92 | case IPure(_) => Const.empty[M, A] 93 | case IEffect(Effect(ef)) => Const(fk(ef)) 94 | case IParallel(par) => Const(M.combine(analyze(par.left)(fk), analyze(par.right)(fk))) 95 | case ISequence(seq) => Const(analyze(seq.a)(fk)) 96 | }).getConst 97 | 98 | } 99 | 100 | } 101 | -------------------------------------------------------------------------------- /examples/src/main/scala/FreeMonadIota.scala: -------------------------------------------------------------------------------- 1 | package eleutheros 2 | 3 | import cats.{~>, Monad} 4 | import cats.syntax.flatMap._ 5 | import iota._ 6 | import iota.QList.:: 7 | import scala.annotation.switch 8 | import scala.annotation.tailrec 9 | 10 | object FreeMonadIota { 11 | type MonadCapabilities = Pure :: Effect :: Sequence :: QNil 12 | 13 | // workaround around Nothing not being inferred 14 | // (also Nothing <: E, but class CopQ is invariant in type E) 15 | // https://issues.scala-lang.org/browse/SI-9453 16 | // https://stackoverflow.com/questions/32097291/bizzare-type-inference-limitation-multiple-type-params 17 | type NoError = Nothing { type T = Unit } 18 | 19 | type FreeMonad[F[_], A] = Fixed[CopQ.OnlyL[MonadCapabilities]#Out, NoError, F, A] 20 | 21 | object FreeMonad { 22 | 23 | val IPure: CopQ.Inject[Pure, CopQ.OnlyL[MonadCapabilities]#Out] = scala.Predef.implicitly 24 | val IEffect: CopQ.Inject[Effect, CopQ.OnlyL[MonadCapabilities]#Out] = scala.Predef.implicitly 25 | val ISequence: CopQ.Inject[Sequence, CopQ.OnlyL[MonadCapabilities]#Out] = scala.Predef.implicitly 26 | 27 | def pure[F[_], A](a: A): FreeMonad[F, A] = 28 | Fixed(IPure.inj(Pure(a))) 29 | 30 | def liftF[F[_], A](fa: F[A]): FreeMonad[F, A] = 31 | Fixed(IEffect.inj(Effect(fa))) 32 | 33 | implicit def monadInstance[F[_]]: Monad[FreeMonad[F, ?]] = 34 | new Monad[FreeMonad[F, ?]] { 35 | def pure[A](a: A): FreeMonad[F, A] = FreeMonad.pure(a) 36 | 37 | def flatMap[A, B](fa: FreeMonad[F, A])(ff: A => FreeMonad[F, B]) : FreeMonad[F, B] = 38 | Fixed(ISequence.inj(Sequence(fa, ff))) 39 | 40 | def tailRecM[A, B](a: A)(f: A => FreeMonad[F, Either[A, B]]): FreeMonad[F, B] = 41 | flatMap(f(a)) { 42 | case Left(a1) => tailRecM(a1)(f) 43 | case Right(b) => pure(b) 44 | } 45 | } 46 | 47 | // def foldMap[F[_], M[_], A] 48 | // (fa: FreeMonad[F, A]) 49 | // (fk: F ~> M) 50 | // (implicit M: Monad[M]) 51 | // : M[A] = 52 | // M.tailRecM(fa)(step(_).unfix match { 53 | // case IEffect(Effect(ef)) => M.map(fk(ef))(Right(_)) 54 | // case IPure(Pure(a)) => M.pure(Right(a)) 55 | // case ISequence(seq) => M.map(foldMap(seq.a)(fk))(cc => Left(seq.f(cc))) 56 | // }) 57 | 58 | // @tailrec 59 | // final def step[F[_], A](fa: FreeMonad[F, A]): FreeMonad[F, A] = fa.unfix match { 60 | // case ISequence(seq) => 61 | // seq.a.unfix match { 62 | // case ISequence(seq2) => 63 | // step(seq2.a.flatMap(seq2.f(_).flatMap(seq.f))) 64 | // case IPure(Pure(a)) => 65 | // step(seq.f(a)) 66 | // case _ => fa 67 | // } 68 | // case _ => fa 69 | // } 70 | 71 | // foldMap using @switch 72 | def foldMap[F[_], M[_], A] 73 | (fa: FreeMonad[F, A]) 74 | (fk: F ~> M) 75 | (implicit M: Monad[M]) 76 | : M[A] = 77 | M.tailRecM(fa){ a => 78 | val copq = step(a).unfix 79 | (copq.index: @switch) match { 80 | case 0 => 81 | val IPure(Pure(a)) = copq 82 | M.pure(Right(a)) 83 | case 1 => 84 | val IEffect(Effect(ef)) = copq 85 | M.map(fk(ef))(Right(_)) 86 | case 2 => 87 | val ISequence(seq) = copq 88 | M.map(foldMap(seq.a)(fk))(cc => Left(seq.f(cc))) 89 | } 90 | } 91 | 92 | // step using @switch 93 | @tailrec 94 | final def step[F[_], A](fa: FreeMonad[F, A]): FreeMonad[F, A] = { 95 | val copq = fa.unfix 96 | if (copq.index == 2) { 97 | val ISequence(seq) = copq 98 | val copq2 = seq.a.unfix 99 | (copq2.index: @switch) match { 100 | case 2 => 101 | val ISequence(seq2) = copq2 102 | step(seq2.a.flatMap(seq2.f(_).flatMap(seq.f))) 103 | case 0 => 104 | val IPure(Pure(a)) = copq2 105 | step(seq.f(a)) 106 | case _ => fa 107 | } 108 | } else fa 109 | } 110 | 111 | 112 | // // foldMap using @switch and casting 113 | // def foldMap2[F[_], M[_], A] 114 | // (fa: FreeMonad[F, A]) 115 | // (fk: F ~> M) 116 | // (implicit M: Monad[M]) 117 | // : M[A] = 118 | // M.tailRecM(fa){ a => 119 | // val copq = step2(a).unfix 120 | // (copq.index: @switch) match { 121 | // case 0 => 122 | // // val Pure(a) = copq.value.asInstanceOf[Pure[NoError, FreeMonad[F, ?], F, A]] 123 | // val a = copq.value.asInstanceOf[Pure[NoError, FreeMonad[F, ?], F, A]].a 124 | // M.pure(Right(a)) 125 | // case 1 => 126 | // // val Effect(ef) = copq.value.asInstanceOf[Effect[NoError, FreeMonad[F, ?], F, A]] 127 | // val ef = copq.value.asInstanceOf[Effect[NoError, FreeMonad[F, ?], F, A]].fa 128 | // M.map(fk(ef))(Right(_)) 129 | // case 2 => 130 | // val seq = copq.value.asInstanceOf[Sequence[NoError, FreeMonad[F, ?], F, A]] 131 | // M.map(foldMap(seq.a)(fk))(cc => Left(seq.f(cc))) 132 | // } 133 | // } 134 | 135 | // // step using @switch and casting 136 | // @tailrec 137 | // final def step2[F[_], A](fa: FreeMonad[F, A]): FreeMonad[F, A] = { 138 | // val copq = fa.unfix 139 | // if (copq.index == 2) { 140 | // val seq = copq.value.asInstanceOf[Sequence[NoError, FreeMonad[F, ?], F, A]] 141 | // val copq2 = seq.a.unfix 142 | // (copq2.index: @switch) match { 143 | // case 2 => 144 | // val seq2 = copq2.value.asInstanceOf[Sequence[NoError, FreeMonad[F, ?], F, A]] 145 | // step2(seq2.a.flatMap(seq2.f(_).flatMap(seq.f))) 146 | // case 0 => 147 | // // val Pure(a) = copq2.value.asInstanceOf[Pure[NoError, FreeMonad[F, ?], F, Any]] 148 | // val a = copq2.value.asInstanceOf[Pure[NoError, FreeMonad[F, ?], F, Any]].a 149 | // val f: Any => FreeMonad[F, A] = seq.f.asInstanceOf[Any => FreeMonad[F, A]] 150 | // step2(f(a)) 151 | // case _ => fa 152 | // } 153 | // } else fa 154 | // } 155 | 156 | // without @switch 157 | // [info] Benchmark Mode Cnt Score Error Units 158 | // [info] FreeMonadBench.catsFree thrpt 10 5303847.922 ± 131820.906 ops/s 159 | // [info] FreeMonadBench.eleutherosEitherFFreeMonad thrpt 10 2326453.816 ± 24621.420 ops/s 160 | // [info] FreeMonadBench.eleutherosIotaFreeMonad thrpt 10 2205078.867 ± 43958.456 ops/s 161 | 162 | // with @switch 163 | // [info] Benchmark Mode Cnt Score Error Units 164 | // [info] FreeMonadBench.catsFree thrpt 10 5202059.547 ± 48599.987 ops/s 165 | // [info] FreeMonadBench.eleutherosEitherFFreeMonad thrpt 10 2366568.224 ± 17667.708 ops/s 166 | // [info] FreeMonadBench.eleutherosIotaFreeMonad thrpt 10 2745629.222 ± 38607.882 ops/s 167 | 168 | // IotaFreeMonad -> 25% speed up 169 | 170 | } 171 | } -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "{}" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright (C) 2016-2017 47 Degrees. 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. --------------------------------------------------------------------------------