Skip to content

Commit

Permalink
Rename return to pure (#2285)
Browse files Browse the repository at this point in the history
Closes #700.

The most interesting changes are in `swarm-lang/Swarm/Language/Parser/{Lex,Term}.hs`, where I remove the code for parsing swarm-0.5 and add an option to parse swarm-0.6 with `return` instead of `pure`, so `swarm format --v0.6` can be used to auto-convert code using `return` to code using `pure`.

Let me know how you feel about this change!  Another possible choice would be to leave `return` as a synonym for `pure`.  I could be convinced to do that as well.
  • Loading branch information
byorgey authored Jan 10, 2025
1 parent 0c285e8 commit 3d465b4
Show file tree
Hide file tree
Showing 246 changed files with 788 additions and 800 deletions.
2 changes: 1 addition & 1 deletion app/game/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ cliParser =
widthOpt = option auto (long "width" <> metavar "COLUMNS" <> help "Use layout with maximum width")

langVer :: Parser LanguageVersion
langVer = flag SwarmLangLatest SwarmLang0_5 (long "v0.5" <> help "Read (& convert) code from Swarm version 0.5")
langVer = flag SwarmLangLatest SwarmLang0_6 (long "v0.6" <> help "Read (& convert) code from Swarm version 0.6")

printKeyMode :: Parser KeybindingPrint
printKeyMode =
Expand Down
4 changes: 2 additions & 2 deletions data/entities.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1180,14 +1180,14 @@
- "A rubber band can tie multiple commands together so that other robots can't execute commands in between them. It can be used via the `atomic` command. For example, suppose robot A executes the following code:"
- |
```
b <- ishere "rock"; if b {grab; return ()} {}
b <- ishere "rock"; if b {grab; pure ()} {}
```
- "This seems like a safe way to execute `grab` only when there is a rock to grab. However, it is actually possible for the `grab` to fail, if some other robot B snatches the rock right after robot A sensed it and before robot A got around to grabbing it on the next game tick."
- "This will make robot A very sad and it will crash."
- "To prevent this situation, robot A can wrap the commands in `atomic`, like so:"
- |
```
atomic (b <- ishere "rock"; if b {grab; return ()} {})
atomic (b <- ishere "rock"; if b {grab; pure ()} {})
```
properties: [pickable]
capabilities: [atomic]
Expand Down
4 changes: 2 additions & 2 deletions data/scenarios/Challenges/2048.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ objectives:
condition: |
try {
as base {has "2048"}
} { return false }
} { pure false }
solution: |
def makeN : Int -> Cmd Unit = \n.
if (n == 1)
{harvest; return ()}
{harvest; pure ()}
{makeN (n/2); makeN (n/2); make (format n)}
end;
place "1";
Expand Down
2 changes: 1 addition & 1 deletion data/scenarios/Challenges/Mazes/easy_cave_maze.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ robots:
system: true
program: |
def until = \c. b <- c; if b {} {until c} end;
until (d <- scan down; return (d == inl ()));
until (d <- scan down; pure (d == inl ()));
create "goal"
known: [water]
entities:
Expand Down
2 changes: 1 addition & 1 deletion data/scenarios/Challenges/Mazes/easy_spiral_maze.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ robots:
system: true
program: |
def until = \c. b <- c; if b {} {until c} end;
until (d <- scan down; return (d == inl ()));
until (d <- scan down; pure (d == inl ()));
create "goal"
entities:
- name: wall
Expand Down
8 changes: 4 additions & 4 deletions data/scenarios/Challenges/Mazes/invisible_maze.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,17 @@ objectives:
try {
teleport self (27, -17);
b <- ishere "goal";
return (not b)
} { return false }
pure (not b)
} { pure false }
solution: |
def tL = turn left end;
def tR = turn right end;
def ifC = \test. \then. \else. b <- test; if b then else end;
def forever = \c. c ; forever c end;
def rightBlocked = tR; b <- blocked; tL; return b end;
def rightBlocked = tR; b <- blocked; tL; pure b end;
def mazeStep = ifC rightBlocked {ifC blocked {tL} {move}} {tR; move} end;
build {
forever (mazeStep; ifC (ishere "goal") {grab; return ()} {})
forever (mazeStep; ifC (ishere "goal") {grab; pure ()} {})
}
robots:
- name: base
Expand Down
4 changes: 2 additions & 2 deletions data/scenarios/Challenges/Mazes/loopy_maze.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ objectives:
try {
teleport self (27, -17);
b <- ishere "goal";
return (not b)
} { return false }
pure (not b)
} { pure false }
solution: |
run "scenarios/Challenges/Mazes/loopy_maze_sol.sw"
robots:
Expand Down
2 changes: 1 addition & 1 deletion data/scenarios/Challenges/Mazes/loopy_maze_sol.sw
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ def tR = turn right end;
def tB = turn back end;
def ifM = \p.\t.\e. b <- p; if b t e end;
def DFS =
ifM (ishere "goal") {grab; return ()} {};
ifM (ishere "goal") {grab; pure ()} {};
ifM (ishere "rock") {} {
place "rock";
tL; b <- blocked; if b {} {move; DFS};
Expand Down
12 changes: 6 additions & 6 deletions data/scenarios/Challenges/Ranching/_beekeeping/queenbee.sw
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def moveTuple = \tup.

def randomDir =
r <- random 4;
return $ if (r == 1) {north}
pure $ if (r == 1) {north}
$ elif (r == 2) {west}
$ elif (r == 3) {south}
$ else {east};
Expand Down Expand Up @@ -136,7 +136,7 @@ def takeStepTowardItem = \item.
// another bee gets here first
try {
harvest;
return ();
pure ();
} {};
} {
// Include some random motion
Expand All @@ -159,7 +159,7 @@ def workerProgram = \structureLoc.
goToHive structureLoc;
} {
takeStepTowardItem "wildflower";
return ();
pure ();
};
workerProgram structureLoc;
end;
Expand Down Expand Up @@ -206,15 +206,15 @@ def createWorkerForStructure = \loc.
require 1 "wax gland";
workerProgramInit beename loc;
};
return ();
pure ();
end;

def associateHive = \loc.
let beename = mkBeeName loc in
try {
// Fails if the robot does not exist
robotnamed beename;
return ();
pure ();
} {
createWorkerForStructure loc;

Expand All @@ -227,7 +227,7 @@ def associateHive = \loc.

def mapM_ : (a -> Cmd b) -> (rec l. Unit + a * l) -> Cmd Unit = \f. \l.
case l
(\_. return ())
(\_. pure ())
(\cons. f (fst cons); mapM_ f (snd cons))
end;

Expand Down
8 changes: 4 additions & 4 deletions data/scenarios/Challenges/Ranching/_beekeeping/solution.sw
Original file line number Diff line number Diff line change
Expand Up @@ -113,14 +113,14 @@ def collectContiguous = \maxdist. \item. \hadFound. \dist.
collectContiguous maxdist item true $ dist + 1;
} {
if hadFound {
return dist;
pure dist;
} {
move;
collectContiguous maxdist item false $ dist + 1;
}
}
} {
return dist;
pure dist;
}
end;

Expand Down Expand Up @@ -148,7 +148,7 @@ def collectAllHoneycombs = \targetCount.

collectAllHoneycombs targetCount;
} {
return currentCount;
pure currentCount;
};
end;

Expand Down Expand Up @@ -176,7 +176,7 @@ def pickRock =
isRock <- ishere "rock";
if isRock {
grab;
return ();
pure ();
} {};
end;

Expand Down
8 changes: 4 additions & 4 deletions data/scenarios/Challenges/Ranching/_capture/opponent.sw
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@ def countBlocked = \n.
isBlocked <- blocked;
turn left;
lastCount <- countBlocked $ n - 1;
return $ lastCount + boolToInt isBlocked;
pure $ lastCount + boolToInt isBlocked;
} {
return 0;
pure 0;
}
end;

Expand All @@ -54,7 +54,7 @@ def reWatch =
def locationIsOpen =
emptyHere <- isempty;
blockedCount <- countBlocked 4;
return $ emptyHere && blockedCount == 0;
pure $ emptyHere && blockedCount == 0;
end;

def faceAwayFrom = \loc.
Expand Down Expand Up @@ -84,7 +84,7 @@ def relocateAway =
findOpenArea;
try {
swap marker;
return ();
pure ();
} {
place marker;
};
Expand Down
8 changes: 4 additions & 4 deletions data/scenarios/Challenges/Ranching/_fishing/hauler.sw
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@ def isEnclosureFull : Int * Int -> Cmd Bool = \encl.
let notFull = c < area in

teleport self prevLoc;
return $ not notFull;
pure $ not notFull;
end;

def any : (a -> Cmd Bool) -> (rec l. Unit + a * l) -> Cmd Bool = \p. \l.
case l
(\_. return false)
(\c. b <- p (fst c); if b {return true} {any p (snd c)})
(\_. pure false)
(\c. b <- p (fst c); if b {pure true} {any p (snd c)})
end;

def isEitherEnclosureFull =
Expand All @@ -37,7 +37,7 @@ def isEitherEnclosureFull =
def tryGrab =
try {
grab;
return ()
pure ()
} {};
end;

Expand Down
14 changes: 7 additions & 7 deletions data/scenarios/Challenges/Ranching/_fishing/solution.sw
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def makeRoll =
def checkIngredients =
hasTuna <- has "crab";
hasSeaweed <- has "seaweed";
return $ hasTuna && hasSeaweed;
pure $ hasTuna && hasSeaweed;
end;

def catchFish = \rod.
Expand All @@ -39,9 +39,9 @@ Precondition:
At the top-right corner
*/
def harvestRectangle =
intersperse 4 move $ harvest; return ();
intersperse 4 move $ harvest; pure ();
turnAround left;
intersperse 4 move $ harvest; return ();
intersperse 4 move $ harvest; pure ();
end;

def harvestIngredients =
Expand All @@ -64,8 +64,8 @@ def harvestIngredients =

def find : (a -> Cmd Bool) -> (rec l. Unit + a * l) -> Cmd (Unit + a) = \p. \l.
case l
(\_. return (inl ()))
(\cons. h <- p (fst cons); if h {return $ inr (fst cons)} {find p (snd cons)})
(\_. pure (inl ()))
(\cons. h <- p (fst cons); if h {pure $ inr (fst cons)} {find p (snd cons)})
end

def tryPlace = \item.
Expand Down Expand Up @@ -108,7 +108,7 @@ def placeSerpentine = \placeFunc.

end;

def returnToCorner =
def pureToCorner =
turn back;
move; move;
turn right;
Expand Down Expand Up @@ -143,7 +143,7 @@ def burnTires =

intersperse 2 move $ (
placeSerpentine $ tryPlace "car tire";
returnToCorner;
pureToCorner;
ignite forward;

turn right;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
def isBlockedOrFenced =
b <- blocked;
return b;
pure b;
end;

def checkIsEnclosed =
maybePath <- path (inL ()) (inR "water");
case maybePath (\_. return True) (\_. return False);
case maybePath (\_. pure True) (\_. pure False);
end;

def boolToInt = \b. if (b) {return 1} {return 0}; end;
def boolToInt = \b. if (b) {pure 1} {pure 0}; end;

def countAdjacentBlockages =

Expand All @@ -28,7 +28,7 @@ def countAdjacentBlockages =
b4 <- isBlockedOrFenced;
c4 <- boolToInt b4;

return $ c1 + c2 + c3 + c4;
pure $ c1 + c2 + c3 + c4;
end;

// Step forward, observing left and right.
Expand All @@ -44,7 +44,7 @@ def observeLeftAndRight =

turn right;
move;
return $ val1 + val2;
pure $ val1 + val2;
end;


Expand All @@ -65,7 +65,7 @@ def countDiagonalBlockages =
// Second, step to both sides
fwdCount <- observeLeftAndRight;
backCount <- observeLeftAndRight;
return $ fwdCount + backCount;
pure $ fwdCount + backCount;
end;

def isStandingOnBridge =
Expand All @@ -74,12 +74,12 @@ def isStandingOnBridge =
if (onFence || onGate) {
adjCount <- countAdjacentBlockages;
if (adjCount > 1) {
return true;
pure true;
} {
diagCount <- countDiagonalBlockages;
return $ (adjCount + diagCount) > 1;
pure $ (adjCount + diagCount) > 1;
};
} {return false};
} {pure false};
end;

def getValForSheepIndex = \predicateCmd. \i.
Expand All @@ -91,15 +91,15 @@ def getValForSheepIndex = \predicateCmd. \i.

boolToInt didSucceed;
} {
return 0;
pure 0;
}
end;

def countSheepWith = \predicateCmd.
val1 <- getValForSheepIndex predicateCmd 1;
val2 <- getValForSheepIndex predicateCmd 2;
val3 <- getValForSheepIndex predicateCmd 3;
return $ val1 + val2 + val3;
pure $ val1 + val2 + val3;
end;

justFilledGap <- as base {
Expand All @@ -108,7 +108,7 @@ justFilledGap <- as base {

if (justFilledGap) {
enclosedCount <- countSheepWith checkIsEnclosed;
return $ enclosedCount >= 1;
pure $ enclosedCount >= 1;
} {
return false;
pure false;
}
Loading

0 comments on commit 3d465b4

Please sign in to comment.