I am trying to convert the following stateful imperative code into Haskell.
while (true) {
while (get()) {
if (put1()) {
failImmediately();
}
}
if (put2()) {
succeedImmediately();
}
}
Both the put1
and put2
read a state of the system and modify it. get
can for simplicity just read the state. failImmediately
should break out of the endless-loop and present one type of result, succeedImmediately
should also break out but present a different result.
What I tried to use was State Env Result
where Env
represented the state of environment and Result
was something like Either Failure Success
for some custom Failure
and Success
.
I struggle with the requirement that the whole resulting expression should collapse into the Failure
/Success
once one of them is produced (breaking the loop) and otherwise keep going.
One idea I had was use Either Exit ()
where data Exit = Success | Failure
and use StateT
somehow to behave upon Left
of the Either
as if Either
was the monad being chained, i.e. ignoring any subsequent actions.
I would really appreciate any inspiration or sample of haskell code that would achieve the same behaviour as the snippet above.
Edit: refined version moved to a separate question "Stateful computation with different types of short-circuit (Maybe, Either)".
EitherT (State Env Result)
. Let me know if that hint is not enough and you need more details :) – Cocaine