【发布时间】:2014-10-14 21:14:38
【问题描述】:
我需要使用 modify 函数来表达 State Monad 的 put,但是我得到一个“无法推断”我的以下代码出错:
class Monad m => MonadState m s | m -> s where
get :: m s
get = modify (\s -> s)
put :: s -> m ()
put = modify $ const ()
modify :: (s -> s) -> m s
modify f = undefined --do { x <- get; put (f x) }
(*我还没有定义修改,因为我在它旁边的评论中的实现也返回了类似的错误,但是在预期的返回类型 m s 而不是实际的返回类型 m ( ))
我得到的确切错误是:
Could not deduce (MonadState m ()) arising from a use of 'modify'
from context (MonadState m s)
bound by the class declaration for 'MonadState'
at <..>
Possible fix: add an instance declaration for (MonadState m ())
In the expression: modify
In the expression: modify $ const ()
In an equation for 'put': put = modify const ()
但是我不确定这个错误试图告诉我什么以及我在这里做错了什么。如果有人可以帮助我,将不胜感激!
最好的问候, Skyfe。
【问题讨论】: