Making the type signature independent of a specifi

2019-07-07 11:14发布

问题:

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.