I'm trying to get the code below working. It's a finite state machine where I pass in a function-as-next-state. The function is called with r
and returns a list of results + the next function-as-next state. Keep on calling until the list runs out, and return the concatenation of the results. The monad is an error monad to allow me to throw an error if needed.
fsm f [] = return []
fsm f (r:rs) = do
(xs, f') <- f r
rest <- fsm f' rs
return $ xs ++ rest
The error is:
Occurs check: cannot construct the infinite type: t1 = t0 -> m0 ([a0], t1)
In the first argument of `fsm', namely f'
I've seen the infinite type error before and I understand the way around it is to wrap a type with a newtype
. But I cannot figure out how to get this done.
Can someone point out the insight?
f
? Can you write it down?f :: String -> m ([String], t)
wheret
would be the type off
. I understand this is why it is an infinite type but cannot figure out how to wrap it up correctly.