There are a few minor mistakes in simpleswap4
. Here is a corrected version:
simpleswap4 :: String -> State String String
simpleswap4 inp =
state $ \s1 ->
-- (>>=)
let (z1, s2) = runState ( {- get -} state $ \sg -> (sg,sg) ) s1
in runState (rhs1 z1) s2
where
rhs1 z1 =
-- (>>)
state $ \s3 ->
let (_, s4) = runState ( {- put inp -} state $ \_ -> ((), inp) ) s3
in runState rhs2 s4
where
rhs2 = return z1
I've renamed a2
to z1
(in lines 5 and 6). This doesn't change the semantics, but emphasized that the first component of the pair returned by the desugared get
call is actually the result that gets bound to z1
in the previous versions of simpleswap
.
The type of rhs1
should be String -> State String String
. In your version, it gets an additional lambda-bound variable. It's unclear what the difference between a2
and z1
should be in your version. Removing the lambda (in line 8) also has the advantage of fixing your scoping problem. You are using z1
in the nested where
-clause, but the where
can only see variables bound on the left hand side of the declaration it is attached to.
In line 11, I've replaced a4
with _
. This is to emphasize that (>>)
does discard the result of the first action. As a consequence, rhs2
is not parameterized over this result either.