Haskell 函数依赖和类型变量错误
我试图弄清楚为什么在这个代码示例中无法识别返回类型,请参阅Haskell 函数依赖和类型变量错误,haskell,Haskell,我试图弄清楚为什么在这个代码示例中无法识别返回类型,请参阅 当您定义Foo(StateT s w)实例时,您没有将s约束为Int,而是希望从栏返回它。以后如何使用实例并不重要,因为实例本身必须有效 也许您想定义: instance (MonadIO w) => Foo (StateT Int w) where type Inner (StateT Int w) = w bar current = do a <- get return a
当您定义
Foo(StateT s w)
实例时,您没有将s
约束为Int
,而是希望从栏返回它。以后如何使用实例并不重要,因为实例本身必须有效
也许您想定义:
instance (MonadIO w) => Foo (StateT Int w) where
type Inner (StateT Int w) = w
bar current = do
a <- get
return a
instance(MonadIO w)=>Foo(StateT Int w)其中
类型内部(StateT Int w)=w
巴电流=do
a感谢@Koterpillar的快速回复,效果非常好!
• Couldn't match type ‘s’ with ‘Int’
arising from a functional dependency between:
constraint ‘MonadState Int (StateT s w)’
arising from a use of ‘get’
instance ‘MonadState s1 (StateT s1 m)’ at <no location info>
‘s’ is a rigid type variable bound by
the instance declaration
at fsm-try-5-3-q.hs:16:10-55
• In a stmt of a 'do' block: a <- get
In the expression:
do a <- get
return a
In an equation for ‘bar’:
bar current
= do a <- get
return a
instance (MonadIO w) => Foo (StateT Int w) where
type Inner (StateT Int w) = w
bar current = do
a <- get
return a