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

Evaluate argument to instant/atomic atomically, as a special case #2271

Merged
merged 5 commits into from
Jan 6, 2025
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions data/scenarios/Challenges/_gallery/setup.sw
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
instant (

def doN = \n. \f. if (n > 0) {f; doN (n - 1) f} {}; end;

def isDivisibleBy = \dividend. \divisor.
Expand Down Expand Up @@ -99,7 +101,7 @@ def length : (rec l. Unit + a * l) -> Int = \l.
end

def busts : (rec l. Unit + Text * l) = tagmembers "bust" end
def bustCount : {Int} = {length busts} end
def bustCount : Int = length busts end

def placeThing = \entIdx.
let entName = index entIdx busts in
Expand All @@ -125,7 +127,7 @@ bust in the base's inventory increases monotonically.
*/
def populateInventory = \baseCount. \idx.

if (idx < force bustCount) {
if (idx < bustCount) {

let item = index idx busts in

Expand All @@ -150,14 +152,16 @@ def populateInventory = \baseCount. \idx.
def setup =
populateInventory 0 0;

naiveRandomStack placeEntByIndex (force bustCount) 0 (force bustCount);
naiveRandomStack placeEntByIndex bustCount 0 bustCount;
turn back;
move;
create "bitcoin";
end;

def go =
instant setup;
setup;
end;

go;

)
42 changes: 42 additions & 0 deletions data/scenarios/Testing/2270-instant-defs.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
version: 1
name: Instant wrapped defs
description: |
`instant` should work when wrapped around definitions
creative: false
objectives:
- goal:
- Grab the rock
condition: |
as base { has "rock" }
robots:
- name: base
dir: east
devices:
- logger
- treads
- grabber
- name: judge
dir: east
system: true
program: |
instant (
def fib : Int -> Int = \n.
if (n <= 1) {n} {fib (n-1) + fib (n-2)}
end

def x = fib 10 end

create "rock"; place "rock"
)
solution: |
move; grab
known: [rock]
world:
dsl: |
{grass}
palette:
'B': [grass, null, base]
'j': [grass, null, judge]
upperleft: [0, 0]
map: |
Bj
9 changes: 9 additions & 0 deletions src/swarm-engine/Swarm/Game/Step.hs
Original file line number Diff line number Diff line change
Expand Up @@ -603,6 +603,15 @@ stepCESK cesk = case cesk of
Out v2 s (FFst v1 : k) -> return $ Out (VPair v1 v2) s k
-- Lambdas immediately turn into closures.
In (TLam x _ t) e s k -> return $ Out (VClo x t e) s k
-- Special case for evaluating an application of Instant or Atomic:
-- set the runningAtomic flag and push a stack frame to unset it
-- when done evaluating. We do this here so that even /evaluating/
-- the argument to instant/atomic will happen atomically (#2270).
-- Execution will also happen atomically; that is handled in execConst.
In (TApp (TConst c) t2) e s k
| c `elem` [Atomic, Instant] -> do
runningAtomic .= True
return $ In t2 e s (FApp (VCApp c []) : FFinishAtomic : k)
-- To evaluate an application, start by focusing on the left-hand
-- side and saving the argument for later.
In (TApp t1 t2) e s k -> return $ In t1 e s (FArg t2 e : k)
Expand Down
1 change: 1 addition & 0 deletions test/integration/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -509,6 +509,7 @@ testScenarioSolutions rs ui key =
not (any ("- treads" `T.isInfixOf`) msgs)
&& any ("- tank treads" `T.isInfixOf`) msgs
, testSolution Default "Testing/2253-halt-waiting"
, testSolution Default "Testing/2270-instant-defs"
]
where
-- expectFailIf :: Bool -> String -> TestTree -> TestTree
Expand Down
Loading