Skip to content

Commit

Permalink
properly handle suspend with more continuation remaining
Browse files Browse the repository at this point in the history
  • Loading branch information
byorgey committed Jun 15, 2024
1 parent 437a4c1 commit c8a9c82
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 9 deletions.
2 changes: 1 addition & 1 deletion src/swarm-engine/Swarm/Game/CESK.hs
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ instance FromJSON CESK where
finalValue :: CESK -> Maybe Value
{-# INLINE finalValue #-}
finalValue (Out v _ []) = Just v
finalValue (Suspended v _ _ _) = Just v
finalValue (Suspended v _ _ []) = Just v
finalValue _ = Nothing

-- | Extract the environment from a suspended CESK machine (/e.g./ to
Expand Down
19 changes: 17 additions & 2 deletions src/swarm-engine/Swarm/Game/Step.hs
Original file line number Diff line number Diff line change
Expand Up @@ -757,6 +757,22 @@ stepCESK cesk = case cesk of
-- never happen).
Out _ s (FExec : _) -> badMachineState s "FExec frame with non-executable value"
------------------------------------------------------------
-- Suspension
------------------------------------------------------------

-- If we're suspended and see the env restore frame, we can discard
-- it: it was only there in case an exception was thrown.
Suspended v e s (FRestoreEnv _ : k) -> return $ Suspended v e s k
-- If we're suspended but we were on the LHS of a bind, switch to
-- evaluating that, except with the environment from the suspension
-- instead of the environment stored in the FBind frame, as if the
-- RHS of the bind had been grafted in right where the suspend was.
Suspended _ e s (FBind _ _ t2 _ : k) -> return $ In t2 e s (FExec : k)
-- Otherwise, if we're suspended with nothing else left to do,
-- return the machine unchanged (but throw away the rest of the
-- continuation).
Suspended v e s _ -> return $ Suspended v e s []
------------------------------------------------------------
-- Exception handling
------------------------------------------------------------

Expand All @@ -780,9 +796,8 @@ stepCESK cesk = case cesk of
-- Otherwise, keep popping from the continuation stack.
Up exn s (_ : k) -> return $ Up exn s k
-- Finally, if we're done evaluating and the continuation stack is
-- empty, OR if we've hit a suspend, return the machine unchanged.
-- empty, return the machine unchanged.
done@(Out _ _ []) -> return done
suspended@(Suspended {}) -> return suspended
where
badMachineState s msg =
let msg' =
Expand Down
9 changes: 3 additions & 6 deletions src/swarm-engine/Swarm/Game/Step/Const.hs
Original file line number Diff line number Diff line change
Expand Up @@ -1226,13 +1226,10 @@ execConst runChildProg c vs s k = do

case mt of
Nothing -> return $ mkReturn ()
Just pt -> do
Just t -> do
void $ traceLog CmdStatus Info "run: OK."
case k of
[] -> return $ In (insertSuspend (prepareTerm mempty pt)) mempty s [FExec]
_ -> return $ In (prepareTerm mempty pt) mempty s (FExec : k)
-- mempty above means we will wipe out all our current definitions every time
-- we execute 'run'...
cesk <- use machine
return $ continue t cesk
_ -> badConst
Not -> case vs of
[VBool b] -> return $ Out (VBool (not b)) s k
Expand Down

0 comments on commit c8a9c82

Please sign in to comment.