Skip to content

Instantly share code, notes, and snippets.

@joshcough
Last active January 1, 2016 07:38
Show Gist options
  • Save joshcough/aef3fc7dbdc67cad8010 to your computer and use it in GitHub Desktop.
Save joshcough/aef3fc7dbdc67cad8010 to your computer and use it in GitHub Desktop.
//libraryDependencies += "org.scalaz" %% "scalaz-core" % "7.2.0"
import scala.language.higherKinds
import scalaz.{MonadState, MonadListen, MonadReader, MonadError, \/}
import scalaz.std.list._
// All I want to do in this code is invoke the eval function. That's it.
// This code compiles, so it's close, just needs the finishing touch.
object HowDoICallEval {
type Mem = Map[Int, Int]
type Env = Map[String, Int]
type Output = List[String]
type T[F[_]] =
MonadError [F,String] with MonadListen[F,List[String]] with
MonadReader[F,Env] with MonadState [F,Mem]
// it doesn't matter what eval does. i just want to call it.
def eval[F[_]](i: Int)(implicit m: T[F]): F[Int] =
m.raiseError("don't care")
// TODO: how do I call eval?
def callEval(e:Int): (Output, String \/ Int, Mem) = {
import scalaz.{Id, EitherT, ReaderWriterState}
import Id.Id, EitherT._, ReaderWriterState._
type S[F[_]] = MonadListen[F,Output] with MonadReader[F,Env] with MonadState [F,Mem]
type R[A] = ReaderWriterState[Env, Output, Mem, A]
type E[A] = EitherT[R, String, A]
val rm: S[R] = ReaderWriterState.rwstMonad[Id, Env, Output, Mem]
//val em: T[E] = EitherT.eitherTMonad[R, String](rm)
val x: E[Int] = eval(e)(???)
???
}
}
// Contrast that code with this code, where I can call eval
// Because I have no MonadError.
object HereICanCallEval {
type Mem = Map[Int, Int]
type Env = Map[String, Int]
type Output = List[String]
type T[F[_]] =
MonadListen[F,List[String]] with
MonadReader[F,Env] with MonadState [F,Mem]
def eval[F[_]](i: Int)(implicit m: T[F]): F[Int] =
m.pure(i)
def callEval(e:Int): (Output, Int, Mem) = {
import scalaz.{Id, ReaderWriterState}
import Id.Id
type R[A] = ReaderWriterState[Env, Output, Mem, A]
val rm: T[R] = ReaderWriterState.rwstMonad[Id, Env, Output, Mem]
eval(e)(rm).run(Map(), Map())
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment