我有以下类型:
newtype StateT' s m a =
StateT' { runStateT' :: s -> m (a,s) }
我实现了 MonadTrans 实例:
instance MonadTrans (StateT' s) where
lift ma = StateT' $ \s -> do
a <- ma
return (a, s)
我的问题是:
语句
return (a, s)
将返回哪种类型?一个元组 或者包裹在 monad 中的元组?如何使用
lift
函数并强制必须StateT'
类型?
最佳答案
Which type the statement
return (a, s)
is going to return? A tuple or the tuple wrapped in a monad?
包裹在单子(monad)中的元组。 return (a, s)::m (a, s)
这很好,因为 runStateT'::s -> m (a, s)
How to use the lift function and force to have to
StateT'
type?
你可能不需要这个。如果您在有类型签名的地方使用 lift
,它应该可以正常工作。
您可以制作一个带有类型签名的 lift 版本,该版本引入了您正在提升到 StateT' s 的事实,但这并没有多大帮助,因为类型推断仍然需要推断s
。
liftStateT' :: Monad m => m a -> StateT' s m a
liftStateT' = lift
关于haskell - 如何使用 monadtrans?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46427455/