I'm trying to understand Monad Transformers in Scala by porting some examples from this tutorial by Dan Piponi: http://blog.sigfpe.com/2006/05/grok-haskell-monad-transformers.html
I did a couple of easy ones:
import Control.Monad.State
import Control.Monad.Identity
test1 = do
a <- get
modify (+1)
b <- get
return (a,b)
test2 = do
a <- get
modify (++"1")
b <- get
return (a,b)
go1 = evalState test1 0
go2 = evalState test2 "0"
becomes:
import scalaz._, Scalaz._
val test1 = for {
a <- get[Int]
_ <- modify[Int](1+)
b <- get
} yield (a,b)
val test2 = for {
a <- get[String]
_ <- modify[String](_ + "1")
b <- get
} yield (a,b)
val go1 = test1.eval(0)
val go2 = test2.eval("0")
But how the heck can I port this next example to Scala?
test3 = do
modify (+ 1)
lift $ modify (++ "1")
a <- get
b <- lift get
return (a,b)
go3 = runIdentity $ evalStateT (evalStateT test3 0) "0"
I've gotten this far using scalaz 7.1.0-M6:
type SST[F[_],A] = StateT[F,String,A]
type IST[F[_],A] = StateT[F,Int,A]
val p1: StateT[Id,Int,Unit] = modify[Int](1+)
val p2: StateT[Id,String,Unit] = modify[String](_ + "1")
val p3: StateT[({type l[a]=StateT[Id,String,a]})#l,Int,Unit] = p2.liftM[IST]
but that's not even close yet, and may even be backwards for all I can tell.
Of course I can do this:
import scalaz.Lens._
val test3 = for {
_ <- firstLens[Int,String] lifts (modify (1+))
_ <- secondLens[Int,String] lifts (modify (_ + "1"))
a <- firstLens[Int,String] lifts get
b <- secondLens[Int,String] lifts get
} yield (a,b)
val go3 = test3.eval(0,"0")
but then I'm not using stacked StateT at all, so it doesn't answer the question.
Thanks in advance!
test3 :: StateT Integer (StateT [Char] Identity) (Integer, [Char])
. I vaguely understand that this is equivalent to the signature that you posted, but how would I get there myself? Or get from this signature to the scalaz equivalent? – Hardfeatured