Scala: bad output type for option consisting of StateT monad transformer

I am a little familiar with Haskell monad transformers, but new to Scalaz (version 7). I made (as I thought) a simple translation from the following Haskell code:

import Control.Monad.State

type Pass a = StateT String Maybe a

monadTest :: Pass String
monadTest = do
    s <- get
    return s

to this Scala code:

import scalaz._
import Scalaz._

object StateTest {
  type Pass[A] = StateT[Option, String, A]

  def monadTest: Pass[String] =
    for {
      s <- get[String]
    } yield s
}

Haskell code compiles. Scala does not compile with the following error:

[error] .../StateTest.scala:9: type mismatch;
[error]  found   : scalaz.IndexedStateT[scalaz.Id.Id,String,String,String]
[error]  required: StateTest.Pass[String]
[error]     (which expands to)  scalaz.IndexedStateT[Option,String,String,String]
[error]       s <- get[String]
[error]         ^

Firstly, it seems that scalaz implements StateTin terms IndexedStateT. OK. But it seems that monadic meaning get[String]has type StateT[Id, String, String]instead StateT[Option, String, String]. Why?

I am using Scala 2.10.1, scalaz 7.0.0.

+5
source share
1 answer

In your example, the call get[String]calls the method get StateFunctionsreproduced here:

def get[S]: State[S, S] = init

State[S, A] - StateT[Id, S, A], IndexedStateT[Id, S, S, A].

StateT, get StateTMonadState[S, F] StateTMonadState[String, Option] . :

import scalaz._
import Scalaz._

object StateTest {
  type Pass[A] = StateT[Option, String, A]

  val sm = StateT.stateTMonadState[String, Option]

  def monadTest: Pass[String] =
    for {
      s <- sm.get
    } yield s
}

MonadState MonadState[F[_, _], S], - . . MonadState.scala StateT.scala.

+8

All Articles