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

halt command #1256

Merged
merged 10 commits into from
May 19, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 16 additions & 0 deletions data/entities.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1249,3 +1249,19 @@
example `key "Down"` or `key "C-S-x"`.
properties: [portable]
capabilities: [handleinput]

- name: halting oracle
display:
attr: device
char: '?'
description:
- A device to solve the halting problem. When asked if a
particular robot program will halt, it always answers YES.
And it is always correct... or else!
Comment on lines +1302 to +1304
Copy link
Member

Choose a reason for hiding this comment

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

This is great.

- |
Enables the command `halt : actor -> cmd unit` which takes
a robot as an argument and, if it is up to one cell away,
cancels its currently running program (if any). In creative mode,
there is no distance limitation.
properties: [portable]
capabilities: [halt]
1 change: 1 addition & 0 deletions editors/emacs/swarm-mode.el
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@
"view"
"appear"
"create"
"halt"
"time"
"scout"
"whereami"
Expand Down
2 changes: 1 addition & 1 deletion editors/vscode/syntaxes/swarm.tmLanguage.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
},
{
"name": "keyword.other",
"match": "\\b(?i)(self|parent|base|if|inl|inr|case|fst|snd|force|undefined|fail|not|format|chars|split|charat|tochar|key|noop|wait|selfdestruct|move|push|stride|turn|grab|harvest|place|give|equip|unequip|make|has|equipped|count|drill|build|salvage|reprogram|say|listen|log|view|appear|create|time|scout|whereami|detect|resonate|sniff|chirp|watch|surveil|heading|blocked|scan|upload|ishere|isempty|meet|meetall|whoami|setname|random|run|return|try|swap|atomic|instant|installkeyhandler|teleport|as|robotnamed|robotnumbered|knows)\\b"
"match": "\\b(?i)(self|parent|base|if|inl|inr|case|fst|snd|force|undefined|fail|not|format|chars|split|charat|tochar|key|noop|wait|selfdestruct|move|push|stride|turn|grab|harvest|place|give|equip|unequip|make|has|equipped|count|drill|build|salvage|reprogram|say|listen|log|view|appear|create|halt|time|scout|whereami|detect|resonate|sniff|chirp|watch|surveil|heading|blocked|scan|upload|ishere|isempty|meet|meetall|whoami|setname|random|run|return|try|swap|atomic|instant|installkeyhandler|teleport|as|robotnamed|robotnumbered|knows)\\b"
}
]
},
Expand Down
18 changes: 18 additions & 0 deletions src/Swarm/Game/Step.hs
Original file line number Diff line number Diff line change
Expand Up @@ -1682,6 +1682,24 @@ execConst c vs s k = do

return $ Out VUnit s k
_ -> badConst
Halt -> case vs of
[VRobot targetID] -> do
myID <- use robotID
case myID == targetID of
-- To halt ourselves, just return a cancelled CESK machine.
-- It will be reinstalled as our current machine; then,
-- based on the fact that our CESK machine is done we will
-- be put to sleep and the REPL will be reset if we are the
-- base robot.
True -> return $ cancel $ Out VUnit s k
False -> do
-- Make sure the other robot exists and is close enough.
_other <- getRobotWithinTouch targetID
-- Cancel its CESK machine, and put it to sleep.
robotMap . at targetID . _Just . machine %= cancel
sleepForever targetID
return $ Out VUnit s k
_ -> badConst
Ishere -> case vs of
[VText name] -> do
loc <- use robotLocation
Expand Down
3 changes: 3 additions & 0 deletions src/Swarm/Language/Capability.hs
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,8 @@ data Capability
CDebug
| -- | Capability to handle keyboard input.
CHandleinput
| -- | Capability to make other robots halt.
CHalt
| -- | God-like capabilities. For e.g. commands intended only for
-- checking challenge mode win conditions, and not for use by
-- players.
Expand Down Expand Up @@ -243,6 +245,7 @@ constCaps = \case
Heading -> Just COrient
Key -> Just CHandleinput
InstallKeyHandler -> Just CHandleinput
Halt -> Just CHalt
-- ----------------------------------------------------------------
-- Text operations
Format -> Just CText
Expand Down
3 changes: 3 additions & 0 deletions src/Swarm/Language/Syntax.hs
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,8 @@ data Const
| -- | Create an entity out of thin air. Only
-- available in creative mode.
Create
| -- | Tell a robot to halt.
Halt
| -- Sensing / generation

-- | Get current time
Expand Down Expand Up @@ -644,6 +646,7 @@ constInfo c = case c of
Create ->
command 1 short . doc "Create an item out of thin air." $
["Only available in creative mode."]
Halt -> command 1 short "Tell a robot to halt."
Time -> command 0 Intangible "Get the current time."
Scout ->
command 1 short . doc "Detect whether a robot is within line-of-sight in a direction." $
Expand Down
1 change: 1 addition & 0 deletions src/Swarm/Language/Typecheck.hs
Original file line number Diff line number Diff line change
Expand Up @@ -566,6 +566,7 @@ inferConst c = case c of
View -> [tyQ| actor -> cmd unit |]
Appear -> [tyQ| text -> cmd unit |]
Create -> [tyQ| text -> cmd unit |]
Halt -> [tyQ| actor -> cmd unit |]
Time -> [tyQ| cmd int |]
Scout -> [tyQ| dir -> cmd bool |]
Whereami -> [tyQ| cmd (int * int) |]
Expand Down