I'm trying to stack monad transfromers of scalaz
in a haskell way:
statyReader :: (MonadReader Int m, MonadState Int m) => m Int
scala:
def statyReader[F[_]](implicit r: MonadReader[F, Int], s: MonadState[F, Int]): F[Int] = for {
counter <- s.get
secret <- r.ask
_ <- s.put(counter + secret)
} yield counter
It compiles with 1 implicit passed, but not with 2:
Error:(13, 18) value flatMap is not a member of type parameter F[Int]
counter <- s.get
^
Error:(14, 18) value flatMap is not a member of type parameter F[Int]
secret <- r.ask
^
Error:(15, 21) value map is not a member of type parameter F[Unit]
_ <- s.put(counter + secret)
^
Why is this happening? My guess is that compiler is now confused which "monadic instance of F[_]
" it should pick(both MonadReader and MonadState extend Monad[F[_]
). Is this a right guess?
How to overcome this?
StateT s (ReaderT r IO) a
. It is true, though, that in such stack we would have multiple type constraints, so it IS related. – InexorablejustReader[F[_]](r: MonadReader[F, Int])
vsjustReader[F[_]](implicit r: MonadReader[F, Int])
), so I'm completely lost now. – Minstrelsy