Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

resonate command #1204

Merged
merged 2 commits into from
Apr 9, 2023
Merged

resonate command #1204

merged 2 commits into from
Apr 9, 2023

Conversation

kostmo
Copy link
Member

@kostmo kostmo commented Apr 8, 2023

Towards #1171

The resonate command counts entities of a given type within a rectangle.

This PR also fixes a bug in detect when the rectangle coords are non-ascending.

@kostmo kostmo force-pushed the resonate-command branch from cda9300 to d3b3e72 Compare April 8, 2023 06:21
@kostmo kostmo force-pushed the resonate-command branch from d3b3e72 to 7b9fd82 Compare April 8, 2023 06:24
@kostmo kostmo marked this pull request as ready for review April 8, 2023 06:24
@kostmo kostmo requested a review from byorgey April 8, 2023 06:24
Copy link
Member

@byorgey byorgey left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like the new resonate command itself, that all looks good to go. We just need to fix up the way we do pattern-matching on values + the rectCells function so we can't possibly crash with a non-exhaustive pattern match.

pattern VRect :: Integer -> Integer -> Integer -> Integer -> VRect
pattern VRect x1 y1 x2 y2 = VPair (VPair (VInt x1) (VInt y1)) (VPair (VInt x2) (VInt y2))

{-# COMPLETE VRect #-}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we want this COMPLETE pragma. The meaning of such a pragma would be that pattern-matching on VRect is sufficient to cover all possible Values, but that is certainly not true. And indeed, the rectCells function is in fact a partial function, and we don't want to just paper over that with a bogus COMPLETE pragma. We never want partial functions (even in cases where bad inputs "shouldn't ever happen"), since as a matter of policy/quality control we want to try to make sure that the game can not possibly crash.

@@ -1243,14 +1243,21 @@ execConst c vs s k = do
let Location x y = loc
return $ Out (VPair (VInt (fromIntegral x)) (VInt (fromIntegral y))) s k
Detect -> case vs of
[VText name, VPair (VPair (VInt x1) (VInt y1)) (VPair (VInt x2) (VInt y2))] -> do
[VText name, r] -> do
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is a subtle issue here, which is that this pattern now matches any Value as the second argument to Detect instead of only pairs of pairs of ints. In theory, if typechecking works, that is the only possible value we could ever get here. But in case there is ever any kind of bug that causes the wrong kind of Value to end up here, previously it would have fallen through to the badConst case, which throws an appropriate in-game exception; with the current code, it will instead actually crash the whole game with a non-exhaustive pattern match error in the rectCells function.

We should be using a VRect pattern here, in the pattern for vs, and then pass the resulting four Int values to rectCells. That way rectCells will be a total function.

Copy link
Member

@byorgey byorgey left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, thanks!

@kostmo kostmo added the merge me Trigger the merge process of the Pull request. label Apr 9, 2023
@mergify mergify bot merged commit dc90996 into main Apr 9, 2023
@mergify mergify bot deleted the resonate-command branch April 9, 2023 04:13
mergify bot pushed a commit that referenced this pull request Apr 20, 2023
This was referenced May 29, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
merge me Trigger the merge process of the Pull request.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants