mirror of
https://github.com/swarm-game/swarm.git
synced 2024-09-11 22:58:21 +03:00
Closes #1547 , which was caused by misspelled/nonexistent types being interpreted as type variables. In #1550 I proposed one solution to the problem, namely, to stop implicitly quantifying types and require explicit `forall` on any polymorphic type. This PR represents an alternative solution: to keep implicit quantification but require all types to start with an uppercase letter, as in Haskell. I think I like this one better but I'm open to feedback. Specifically, with this PR: - All built-in type constructors (`Int`, `Cmd`, `Unit`, etc.) must start with a capital letter - Type variables: - Must start with a lowercase letter or underscore - May not be named a lowercase version of a type constructor, *e.g.* `int` - This is important so that old code with lowercase types is not silently accepted by parsing the former types as implicitly quantified type variables; even in cases where old code no longer typechecks, this will give better error messages. - Term variables: - May start with upper- or lowercase - May be named lowercase versions of type names, *e.g.* `let f : Int -> Int = \int. int + 1` is fine This PR obviously represents a bigger breaking change. Once we merge this we might consider adding an option to `swarm format` to be able to parse old code with lowercase types and then reformat it with uppercase, to aid in porting code. Once this is merged I will also regenerate the wiki pages that mention types. Closes #1550.
67 lines
1.3 KiB
XML
67 lines
1.3 KiB
XML
// Quickly harvesting an entire forest in parallel using breadth-first
|
|
// search, with robots spawning more robots. Fun, though not very practical
|
|
// in classic mode.
|
|
|
|
def repeat : Int -> Cmd Unit -> Cmd Unit = \n.\c.
|
|
if (n == 0)
|
|
{}
|
|
{c ; repeat (n-1) c}
|
|
end;
|
|
def while : Cmd Bool -> Cmd Unit -> Cmd Unit = \test.\c.
|
|
b <- test;
|
|
if b {c ; while test c} {}
|
|
end;
|
|
def getX : Cmd Int =
|
|
pos <- whereami;
|
|
return (fst pos);
|
|
end;
|
|
def getY : Cmd Int =
|
|
pos <- whereami;
|
|
return (snd pos);
|
|
end;
|
|
def gotoX : Int -> Cmd Unit = \tgt.
|
|
cur <- getX;
|
|
if (cur == tgt)
|
|
{}
|
|
{if (cur < tgt)
|
|
{turn east}
|
|
{turn west};
|
|
try {move} {turn south; move};
|
|
gotoX tgt
|
|
}
|
|
end;
|
|
def gotoY : Int -> Cmd Unit = \tgt.
|
|
cur <- getY;
|
|
if (cur == tgt)
|
|
{}
|
|
{if (cur < tgt)
|
|
{turn north}
|
|
{turn south};
|
|
try {move} {turn east; move};
|
|
gotoY tgt
|
|
}
|
|
end;
|
|
def goto : Int -> Int -> Cmd Unit = \x. \y. gotoX x; gotoY y; gotoX x; gotoY y end;
|
|
def spawnfwd : {Cmd Unit} -> Cmd Unit = \c.
|
|
try {
|
|
move;
|
|
b <- isHere "tree";
|
|
if b
|
|
{ build c; return () }
|
|
{};
|
|
turn back;
|
|
move
|
|
} { turn back }
|
|
end;
|
|
def clear : Cmd Unit =
|
|
grab;
|
|
repeat 4 (
|
|
spawnfwd {clear};
|
|
turn left
|
|
);
|
|
goto 0 0;
|
|
give base "tree";
|
|
selfdestruct;
|
|
end;
|
|
def start : Cmd Actor = build {turn west; repeat 7 move; clear} end
|