我得到一个<< 循环>>我的State Monad实例的例外,我想这是指一个无限循环,但我不知道我的代码如何在使用时导致一个:
instance Monad (State' s) where
-- return :: a -> State' s a
return x = State' (\(s,c) -> (x, s, (c <> oneReturn) ))
-- (>>=) :: State' s a -> (a -> State' s b) -> State' s b
st >>= k = State' $ \(s,c) -> let (a, s', c) = runState' st (s,c)
in runState' (k a) (s',(c <> oneBind) )
instance MonadState (State' s) s where
-- get :: State' s s
get = State' $ \(s,c) -> (s,s, (c <> oneGet))
-- put :: s -> State' s ()
put s = State' $ \(_,c) -> ((),s, (c <> onePut))
如果有人有线索可以帮助我,我非常感激!
祝你好运, Skyfe。
编辑:供参考一个&lt; SomeMonadicFunc &gt;用于在与当前计数值绑定时正确增加计数器 -
oneBind = Counts 1 0 0 0
oneReturn = Counts 0 1 0 0
oneGet = Counts 0 0 1 0
onePut = Counts 0 0 0 1
答案 0 :(得分:5)
let (a, s', c) = runState' st (s,c)
这是一个递归定义:结果中的c
用于计算结果,用于...
您可能指的是let (a,s',c') = runState' st (s,c) in runState' (k a) (s',(c' <> oneBind) )
,没有阴影c
变量。