views:

222

answers:

1

I try to define the Reader monad with scalaz like this:

import scalaz._
import Scalaz._

final class Reader[E,A](private[Reader] val runReader: E => A)

object Reader {
  def apply[E,A](f: E => A) = new Reader[E,A](f)
  def env[E]: Reader[E,E] = Reader(identity _)

  implicit def ReaderMonad[E] = new Monad[PartialApply1Of2[Reader,E]#Apply] {
    def pure[A](a: => A) = Reader(_ => a)

    def bind[A,B](m: Reader[E,A], k: A => Reader[E,B]) =
      Reader(e => k(m.runReader(e)).runReader(e))
  }
}


object Test {
  import Reader._

  class Env(val s: String)

  def post(s: String): Reader[Env, Option[String]] =
    env >>= (e => if (e.s == s) some(s).pure else none.pure)
}

but I get a compiler error:

reader.scala:27: reassignment to val
     env >>= (e => if (e.s == s) some(s).pure else none.pure)
         ^

Why is that?

Thanks, Levi

+8  A: 

This error is fairly opaque, even by Scala's standards. Method names ending with = are treated specially -- they are first considered as a normal identifier, and failing that, they are expanded to a self assignment.

scala> def env[A] = 0
env: [A]Int

scala> env >>= 0
<console>:7: error: reassignment to val
       env >>= 0
           ^

scala> env = env >> 0
<console>:6: error: reassignment to val
       env = env >> 0
           ^

If you're confused about the syntactic interpretation of your program, it's a good idea to run scalac -Xprint:parser to see what's going on. Similarly, you can use -Xprint:typer or -Xprint:jvm to see later phases of the program transformation.

So, how do you call >>= on your Reader? First of all, you'll need to explicitly pass the type argument Env to env. The resulting Reader[Env, Env] must then be converted to a MA[M[_], A]. For simple type constructors, the implicit conversion MAs#ma will suffice. However the two param type constructor Reader must be partially applied -- this means it can't be inferred and instead you must provide a specific implicit conversion.

The situation would be vastly improved if Adriaan ever finds a spare afternoon to implement higher-order unification for type constructor inference. :)

Until then, here's your code. A few more comments are inline.

import scalaz._
import Scalaz._

final class Reader[E, A](private[Reader] val runReader: E => A)

object Reader {
  def apply[E, A](f: E => A) = new Reader[E, A](f)

  def env[E]: Reader[E, E] = Reader(identity _)

  implicit def ReaderMonad[E]: Monad[PartialApply1Of2[Reader, E]#Apply] = new Monad[PartialApply1Of2[Reader, E]#Apply] {
    def pure[A](a: => A) = Reader(_ => a)

    def bind[A, B](m: Reader[E, A], k: A => Reader[E, B]) =
      Reader(e => k(m.runReader(e)).runReader(e))
  }

  // No Higher Order Unification in Scala, so we need partially applied type constructors cannot be inferred.
  // That's the main reason for defining function in Scalaz on MA, we can create one implicit conversion
  // to extract the partially applied type constructor in the type parameter `M` of `MA[M[_], A]`.
  //
  // I'm in the habit of explicitly annotating the return types of implicit defs, it's not strictly necessary
  // but there are a few corner cases it pays to avoid.
  implicit def ReaderMA[E, A](r: Reader[E, A]): MA[PartialApply1Of2[Reader, E]#Apply, A] = ma[PartialApply1Of2[Reader, E]#Apply, A](r)
}


object Test {
  import Reader._

  class Env(val s: String)

  def post(s: String): Reader[Env, Option[String]] =
    // Need to pass the type arg `Env` explicitly here.
    env[Env] >>= {e =>
      // Intermediate value and type annotation not needed, just here for clarity.
      val o: Option[String] = (e.s === s).guard[Option](s)
      // Again, the partially applied type constructor can't be inferred, so we have to explicitly pass it.
      o.pure[PartialApply1Of2[Reader, Env]#Apply]
    }
}
retronym
Thank you. That does the trick. I have to confess though that Scala is really disappointing me when I try do actually use it as a functional language because it feels like one gigantic hack to me.
Levi Greenspan
You're coming from Haskell, I presume. Scala can't compete with Hindley-Milner inference, doesn't enforce purity, and is strict by default. It does have JVM interop, implicit params can encode a few things are tricky with type classes.
retronym