How to implement `stateMonad` directly ?


State[S, A] can not be used in Monad[State], because trait Monad[M[_]]

my attempt:


You can express this in Scala but it’s very unpleasant out of the box. I recommend you use this compiler plugin which will allow you to write State[S, *].


Try adding scalacOptions += "-Ypartial-unification" to your build.sbt.

In Scala 2.13 it should work without it.


As @DavidGregory084 said, the kind-projector compiler plugin is probably what you want. Functional libraries like cats also use it, so it is pretty common to include when you are doing stuff with type classes.

Your current implementation should compile with partial unification, but implicitly looking up the Monad for State won’t work. But you don’t have to change much when using kind-projector:

implicit def stateMonad[S]: Monad[State[S, ?]] = new Monad[State[S, ?]] {
  def pure[A](a: A): State[S,A] = State(s => (a,s))
  def flatMap[A, B]
    (state: State[S,A])
    (f: A => State[S, B]): State[S, B] = State(s => {
      val (a:A ,s2) = state.mutation(s)

FWIW, without the plugin, you’d have to change the first line of that to:

implicit def stateMonad[S] = new Monad[({type T[A] = State[S, A]})#T] {

, which most people don’t find easily readable (it creates an anonymous object with a type member, which fixes the first parameter of State. Kind-projector does the same with nicer syntax)