0

I'm getting a <<loop>> Exception for my State Monad instance and I suppose this refers to an infinite loop, however I don't see how my code could lead to one upon use:

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))

If anyone has a clue and could help me out it's much appreciated!

Best regards, Skyfe.

EDIT: For reference the one<SomeMonadicFunc> are for increasing the counter properly when bound with a current Counts value -

oneBind   = Counts 1 0 0 0
oneReturn = Counts 0 1 0 0
oneGet    = Counts 0 0 1 0
onePut    = Counts 0 0 0 1
1
  • You may not remove your content in this manner. It is wholly unfair to the people who spent time and effort answering your question. As for other students committing "fraud"; If it was not "fraud" for you to post here in the first place, how could it be fraud for others to also use the same information? Commented Oct 19, 2014 at 19:44

1 Answer 1

5
        let (a, s', c) = runState' st (s,c)

This is a recursive definition: the c in the result is used to compute the result, which is used to...

You probably mean let (a,s',c') = runState' st (s,c) in runState' (k a) (s',(c' <> oneBind) ), without the shadowed c variable.

Sign up to request clarification or add additional context in comments.

1 Comment

That fixed it indeed, I hadn't noticed it, thank you! The difference one ' accent can make hah.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.