Making the type signature independent of a specific monad transformer stack (Scala)

岁酱吖の 提交于 2019-12-12 13:09:04

问题


I'm learning Scala by translating a Haskell function to Scala. I have a monad transformer stack containing the StateMonad

type TI a = ... 

One function using this monad transformer stack is:

fresh :: TI Int
fresh = do n <- get
           put (n + 1)
           return n

Since this function only depends on the State monad I may also change the type to:

fresh :: (MonadState Int m) => m Int

How does this translate to Scala? In Scala, I use a monad transformer stack composing the state and the identity monad:

type TI[A] = StateT[Id, scala.Int, A]

The fresh function in Scala looks like this:

def fresh:TI[Ty] = for {
  counter <- get[scala.Int]
  _ <- put[scala.Int] (counter + 1)
} yield {
  TyVar(counter)
}

How do I rewrite the type signature in Scala in such a way that in only depends on the State monad and not on the whole monad transformer stack?


回答1:


Scalaz also provides a MonadState type class, although the limitations of Scala's type inference means it's much less elegant. You could write the following, for example:

import scalaz._, Scalaz._

def fresh[F[_, _]](implicit m: MonadState[F, Int]): F[Int, Int] = for {
  counter <- m.get
  _       <- m.put(counter + 1)
} yield counter

And then for example the following:

scala> fresh[State]
res6: scalaz.State[Int,Int] = scalaz.IndexedStateT$$anon$10@6517b62

scala> type OptionState[s, a] = StateT[Option, s, a]
defined type alias OptionState

scala> fresh[OptionState]
res7: OptionState[Int,Int] = scalaz.IndexedStateT$$anon$10@43740ba6

Note that the first type parameter for MonadState needs to have holes for both the state and the value, so you'll need to adjust your TI if you want to use this approach—fresh[TI] won't work as is.



来源:https://stackoverflow.com/questions/22636075/making-the-type-signature-independent-of-a-specific-monad-transformer-stack-sca

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!