Skip to content

Commit 436efdb

Browse files
authored
Tutorial updates, + new loops tutorial (#2622)
- Various minor tutorial improvements - Move conditional tutorial after `world101` (world101 does not require conditionals) - Add new loops tutorial after conditionals and before `farming`, which prompts you to implement a while loop using recursion.
1 parent b2c08b6 commit 436efdb

File tree

11 files changed

+145
-26
lines changed

11 files changed

+145
-26
lines changed

data/scenarios/Tutorials/00-ORDER.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ def.yaml
1515
lambda.yaml
1616
require.yaml
1717
stock.yaml
18-
conditionals.yaml
1918
world101.yaml
19+
conditionals.yaml
20+
loops.yaml
2021
farming.yaml

data/scenarios/Tutorials/build.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ objectives:
1616
- |
1717
**TIP:** Newly built robots start out facing the same
1818
direction as their parent, which in the tutorials will always be north.
19+
- |
20+
**TIP:** If you make a mistake, not to worry. You have enough materials
21+
for several tries; or, you can always hit **Ctrl+Q** and
22+
select "Start over".
1923
condition: |
2024
try {
2125
teleport self (0,-1);

data/scenarios/Tutorials/conditionals.yaml

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,15 @@ description: |
55
objectives:
66
- goal:
77
- |
8-
The 4x4 gray square contains 4 `very small rock`{=entity}s --- so
9-
small they cannot be seen! Your goal is to collect all of
10-
them and bring them back to your base; you win when you have
11-
all 4. There is one rock in each row and column, but
12-
otherwise you can't be sure where they are. Your best bet is
13-
to sweep over the entire 4x4 square and pick up a `very small rock`{=entity}
14-
any time you detect one.
8+
Before making more progress in the world, you'll need to learn
9+
how to use a few more language features. The 4x4 gray square
10+
contains 4 `very small rock`{=entity}s --- so small they
11+
cannot be seen! Your goal is to collect all of them and bring
12+
them back to your base; you win when you have all 4. There is
13+
one rock in each row and column, but otherwise you can't be
14+
sure where they are. Your best bet is to sweep over the
15+
entire 4x4 square and pick up a `very small rock`{=entity} any
16+
time you detect one.
1517
- |
1618
The `ishere` command, with type `Text -> Cmd Bool`{=type}, can be used
1719
for detecting the presence of a specific item such as a `very small rock`{=entity}.

data/scenarios/Tutorials/farming.yaml

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,21 +11,16 @@ objectives:
1111
are somewhat rare in the wild. Therefore, it makes sense to farm
1212
them in order to create a reliable supply.
1313
- |
14-
In this scenario, you are a bit farther along: in particular,
14+
In this example scenario, you are a bit farther along: in particular,
1515
you now have a few `harvester`{=entity}s, a few `lambda`{=entity}s, a few `logger`{=entity}s,
1616
some `branch predictor`{=entity}s which
1717
allow robots to evaluate conditional expressions, and some
18-
`strange loop`{=entity}s which enable recursive functions. For example,
19-
one simple, useful recursive function is
18+
`strange loop`{=entity}s which enable recursive functions.
2019
- |
21-
```
22-
def forever = \c. c ; forever c end
23-
```
24-
- |
25-
Your goal is to acquire 256 `lambda`{=entity}s. Of course, in order to
26-
accomplish this in a reasonable amount of time, it makes sense to plant
20+
Your goal is to acquire 256 `lambda`{=entity}s. Of course,
21+
now that you know how to write conditionals and loops, it makes sense to plant
2722
a field of `lambda`{=entity}s and then program one or more robots to
28-
harvest them in an automated way.
23+
harvest them in an automated way!
2924
- |
3025
**TIP:** the `ishere` command can be used to test for the presence of a
3126
(fully-grown) `lambda`{=entity}, and the `has` command can be used to test whether
@@ -41,7 +36,7 @@ objectives:
4136
- Congratulations! You have completed the most difficult simulated exercise and are ready to begin exploring the new planet in earnest. Of course there is much more remaining to explore in the world, and many additional programming language features to unlock.
4237
- |
4338
To finally complete this tutorial, there is only one thing left for you to do:
44-
use one of your lambdas to make some delicious `curry`{=entity}.
39+
use your `lambda`{=entity}s to make some delicious `curry`{=entity}.
4540
- Afterwards, you will return to the menu where you can select "Classic game" for the complete game experience. Or, play a "Creative game" if you just want to play around with programming robots, without any constraints or need to collect resources. You could also choose to redo some tutorial scenarios, or explore the other challenge scenarios listed in the menu.
4641
- Now go forth and build your swarm!
4742
condition: |
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
version: 1
2+
name: Loops
3+
description: |
4+
Learn how to implement loops.
5+
objectives:
6+
- goal:
7+
- |
8+
The previous tutorial taught you how to use conditional
9+
expressions; this one will guide you through using loops.
10+
- |
11+
To the east of your base lies a path of `rock`{=entity}s. The
12+
path extends in a straight line for an unknown distance, and at
13+
the end of the path lies a lump of `gold`{=entity}. You must
14+
program a robot to go and fetch it!
15+
- |
16+
Some kind of `while`{=snippet} loop would obviously come in
17+
handy here. However, the Swarm language does not have loops,
18+
only recursion, which is enabled by the `strange loop`{=entity}
19+
device. You can use recursion to implement your own control
20+
structures. For example, a simple loop that repeats forever
21+
can be written like this:
22+
```
23+
def forever : Cmd Unit -> Cmd Unit = \body.
24+
body; forever body
25+
end
26+
```
27+
- |
28+
Now replace the `pure ()` below with your own code to
29+
implement a while loop function:
30+
```
31+
def while : Cmd Bool -> Cmd Unit -> Cmd Unit = \cond. \body.
32+
pure ()
33+
end
34+
```
35+
- |
36+
This function should take a condition (a `Cmd Bool`{=type}, for
37+
example, `ishere "rock"`) and a command, and execute the
38+
command repeatedly as long as the condition returns `True`.
39+
condition: |
40+
try {as base {has "gold"}} {pure false}
41+
solution: |
42+
def while : Cmd Bool -> Cmd a -> Cmd Unit = \test. \body.
43+
b <- test;
44+
if b {body; while test body} {}
45+
end
46+
47+
def follow = while (ishere "rock") move end
48+
49+
build {
50+
turn east; move;
51+
follow;
52+
grab; turn back; move;
53+
follow;
54+
give base "gold"
55+
}
56+
robots:
57+
- name: base
58+
heavy: true
59+
loc: [0, 0]
60+
dir: north
61+
display:
62+
char: Ω
63+
attr: robot
64+
devices:
65+
- logger
66+
- 3D printer
67+
- dictionary
68+
- toolkit
69+
inventory:
70+
- [8, compass]
71+
- [8, solar panel]
72+
- [8, logger]
73+
- [8, treads]
74+
- [8, grabber]
75+
- [8, scanner]
76+
- [8, lambda]
77+
- [8, strange loop]
78+
- [8, branch predictor]
79+
- [8, toolkit]
80+
entities:
81+
known: [rock, gold]
82+
world:
83+
scrollable: false
84+
dsl: |
85+
let path_len = 173 in
86+
overlay
87+
[ {grass}
88+
, mask (y == 0 && x > 0 && x < path_len) {rock}
89+
, mask (y == 0 && x == path_len) {gold}
90+
]

data/scenarios/Tutorials/require.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ objectives:
1515
```
1616
build {require "boat"; move}
1717
```
18-
Then a `boat`{=entity} will be equipped on the new robot in addition to `treads`{=entity}.
18+
then a `boat`{=entity} will be equipped on the new robot in addition to `treads`{=entity}.
1919
- |
2020
Unlike other devices used so far in the tutorial, `boat`{=entity} does not provide any commands,
2121
but robots that have it equipped will not drown in the `water`{=entity}.

data/scenarios/Tutorials/stock.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,14 @@ objectives:
77
- |
88
In the previous tutorial challenge, you learned how to use `require`{=snippet}
99
to require specific devices to be equipped.
10-
Sometimes, instead of requiring entities to be equiped as devices, you need them to be stocked in your inventory.
10+
Sometimes, instead of requiring entities to be equipped as devices, you need them to be stocked in your inventory.
1111
- |
1212
In this case, you can write `stock <int> <name>`{=snippet} to ensure that a certain number
1313
of copies of a certain entity are placed in your inventory. For example:
1414
```
1515
build {stock 2 "flower"; move; place "flower"; move; place "flower"}
1616
```
17-
This would build a robot with two `flower`{=entity}s in its inventory which the robot can then place.
17+
This would build a robot with two `flower`{=entity}s in its inventory which the robot can then `place`.
1818
- Your goal in this challenge is to cover the entire 4x4 gray area with `rock`{=entity}s!
1919
- |
2020
**TIP:** You can define commands to simplify your task, for example:

data/scenarios/Tutorials/type-errors.yaml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ objectives:
1212
- "Some other type errors for you to try:"
1313
- |
1414
`turn move`{=snippet}
15+
- |
16+
`3 + "hi"`{=snippet}
1517
- |
1618
`place tree`{=snippet} (without double quotes around `tree`{=snippet})
1719
- |
@@ -30,6 +32,17 @@ entities:
3032
char: 'W'
3133
description:
3234
- Do `place "win"` once you are done with this challenge.
35+
- "Some expressions for you to try:"
36+
- |
37+
`turn 1`{=snippet}
38+
- |
39+
`turn move`{=snippet}
40+
- |
41+
`3 + "hi"`{=snippet}
42+
- |
43+
`place tree`{=snippet}
44+
- |
45+
`move move`{=snippet}
3346
properties: [known, pickable]
3447
solution: |
3548
place "win"

data/scenarios/Tutorials/types.yaml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,20 @@ entities:
3434
char: 'W'
3535
description:
3636
- Do `place "win"` once you are done with this challenge.
37+
- |
38+
Some expressions for you to try:
39+
- |
40+
`north`
41+
- |
42+
`move; move`
43+
- |
44+
`grab`
45+
- |
46+
`make`
47+
- |
48+
`3`
49+
- |
50+
`"tree"`
3751
properties: [known, pickable]
3852
solution: |
3953
place "win"

src/swarm-lang/Swarm/Language/Typecheck.hs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -518,7 +518,7 @@ instance PrettyPrec TypeErr where
518518
EscapedSkolem x ->
519519
"Skolem variable" <+> ppr x <+> "would escape its scope"
520520
UnboundVar x ->
521-
"Unbound variable" <+> ppr x
521+
"Undefined variable" <+> ppr x
522522
UnboundType x ->
523523
"Undefined type" <+> ppr x
524524
DefNotTopLevel t ->
@@ -1287,11 +1287,11 @@ check s@(CSyntax l t cs) expected = addLocToTypeErr l $ case t of
12871287
-- > let y : Int = 3 in y + 2
12881288
-- 5
12891289
-- > y
1290-
-- 1:1: Unbound variable y
1290+
-- 1:1: Undefined variable y
12911291
-- > let y = 3 in def x = 5 end; pure (x + y)
12921292
-- 8
12931293
-- > y
1294-
-- 1:1: Unbound variable y
1294+
-- 1:1: Undefined variable y
12951295
-- > x
12961296
-- 5
12971297
let mreqs = case ls of

0 commit comments

Comments
 (0)