swarm/example/pilotmode.sw
Brent Yorgey 1a4dcd82f0
Require types to start with an uppercase letter (#1583)
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.
2024-05-21 04:16:32 +00:00

27 lines
1003 B
XML

def cons : a * b -> (a -> b) -> (a -> b) = \p. \k. \a.
if (a == fst p) {snd p} {k a}
end
def nil : a -> Cmd Unit = \a. return () end
// Suitable to use as e.g.
// installKeyHandler "(S-)←↓↑→ [Del] [g]rab [h]arvest [d]rill [s]can [b]locked [u]pload" pilot
def pilot : Key -> Cmd Unit =
cons (key "Up", move) $
cons (key "Down", turn back) $
cons (key "Left", turn left) $
cons (key "Right", turn right) $
cons (key "S-Up", turn north) $
cons (key "S-Down", turn south) $
cons (key "S-Left", turn west) $
cons (key "S-Right", turn east) $
cons (key "Del", selfdestruct) $
cons (key "g", res <- grab; log res) $
cons (key "h", res <- harvest; log res) $
cons (key "d", res <- drill forward; case res (\_. return ()) log) $
cons (key "s", res <- scan forward; case res (\_. return ()) log) $
cons (key "b", b <- blocked; if b {log "blocked"} {log "not blocked"}) $
cons (key "u", upload base) $
nil
end