Commit Graph

20 Commits

Author SHA1 Message Date
Brent Yorgey
599225f4d6
Key input handler (#1214)
Ability to code your own input handler routines.  Closes #102 .  Fixes #1210 .

* Adds a new type `key` to represent keypresses
* Adds a primitive function `key : text -> key` which can handle usual letters, numbers, etc. as well as special keys like `"Down"` etc, as well as modifier key prefixes like `key "A-C-Del"`.  `swarm generate keys` generates a list of all recognized special key names.
* New command `installKeyHandler : text -> (key -> cmd unit) -> cmd unit` which sets the "current key handler".  The `text` value is a hint line to display in the secondary key hints menu while the handler is running.  The global shortcut `M-k` toggles the currently installed handler.
* Add a `keyboard` device to provide these commands, as well as a `key` entity (the recipe for a `keyboard` is 16 `key`s + 1 `board`).
* Add a few examples in the `examples` folder.
* Add an installed `keyboard` to the `building-bridges` challenge.
2023-04-25 16:39:59 +00:00
Brent Yorgey
926cede91a
Meeting other robots (#920)
Closes #306. Closes #931.

- Renamed `robot` type to `actor` in anticipation of meeting other things besides robots.
- `meet : cmd (unit + actor)` returns an arbitrary actor within Manhattan distance 1, if any.
- `meetAll : (b -> actor -> cmd b) -> b -> cmd b` will run on every nearby actor.
- Added `antenna` device to provide the commands.
- Added "make a friend" challenge that requires the use of `meet`.
2022-12-22 20:36:11 +00:00
Brian Wignall
10af601ca3
Fix typos (#850)
All changes should be non-semantic (e.g., only in code comments).

I am brand new to Swarm, apologies if I misinterpreted something (thinking particularly of "entites") as a typo that is not; happy to revert those if so.

Code itself is not changed, so `ignoredEntites` remains, though I expect it should be `ignoredEntities` (with an "i" added).
2022-11-11 15:00:55 +00:00
Brent Yorgey
5a01658883
rename unit type to unit (#697)
Closes #696.
2022-09-26 20:10:12 +00:00
Ondřej Šebek
e85bf806c2
Retire raise command (#407)
- remove `raise` command
- rename `error` to `fail` - should suggest both that it is:
  - recoverable - can be caught with `try`
  - pure - `fail: string -> a` - so it can be used outside `cmd`

---

The problem with `raise` was that it was merely a specialized version of `error` (which was not limited to `cmd a`):
```
let raised: string -> cmd a = error in raised "ha"
```
I noticed this while making the list for #26.

On a meta-level, it also conflicts with the `raise` Haskell function we use to throw an error when a command (like `build`) fails.
2022-06-23 11:25:23 +00:00
Brent Yorgey
54e5e35bbe clean up examples
Closes #325.
2022-06-11 15:15:07 -05:00
Brent Yorgey
b62d27e566
Use a new opaque type for robots instead of strings (#303)
The basic idea of this change is to create a new `robot` type and use it to identify robots instead of `string` names.  Internally, a `robot` value is just a (unique) `Int`. 

Closes #212 .

This ended up turning into a sort of constellation of related changes.

- Add the `robot` type and change the type of various built-in functions which used to take a robot name so they now take a `robot` (`give`, `install`, `reprogram`, `view`, `upload`) and change `build` so it returns a `robot`.
- All internal data structures that store robots are now keyed by a unique (`Int`) robot ID rather than by name.
- Add a `setname` command for setting a robot's display name (which no longer needs to uniquely identify a robot).
- Import a big list of words which we can use to randomly pick names for robots, just for fun.  This is why the diff says "+31,050  -265"; I did not write 31 thousand lines of code.
- Add constants `base`, `parent`, and `self` for getting a `robot` value referring to the base, one's parent, and one's self, respectively.
- Top-level binders like `r <- build {move}` now export a variable binding which can be used in later expressions entered at the REPL; additionally, unlike Haskell, a binder can now appear as the last statement in a block.
- Fix the pretty-printer for `Value` by doubling down on our current strategy of injecting `Value`s back into `Term`s and then pretty-printing the result.  I am now convinced this is the Right Way (tm) to do this; it only required adding a couple additional kinds of `Term` which represent internal results of evaluation and cannot show up in the surface language (`TRef`, `TRobot`).
- Update the tutorial.
- While updating the tutorial, I noticed that #294 had introduced a bug, where the inventory display no longer updated when 0 copies of an entity are added to the inventory (as with `scan` + `upload`), so I fixed that by changing the way inventory hashes are computed.

I tried running the benchmarks both before & after this change.  I was hoping that it might speed things up to be using `IntMap` and `IntSet` instead of looking things up by `Text` keys in a `Map` all the time.  However, if I'm interpreting the results correctly, it seems like it didn't really make all that much difference, at least for the particular benchmarks we have.
2022-03-02 03:00:44 +00:00
Ondřej Šebek
bc480a045d
Add list example (#261)
- encodes `list int` inside `int`
- has unit tests written in swarm language
2021-10-26 19:54:04 +00:00
Brent Yorgey
c982e81790
Delay type (#223)
Make explicit in the type system when evaluation of a computation should be delayed.  This gives the user fine-grained control over selective laziness (for example, once we have sum types and recursive types, one could use this to define lazy infinite data structures).  It also allows us to guarantee that certain commands such as `build` and `reprogram` delay evaluation of their arguments, and lets the user e.g. define their own modified versions of `build` without compromising those guarantees.

- Delay is indicated by curly braces both at the value and type levels, that is, if `t : ty` then `{t} : {ty}`.
- `force : {ty} -> ty` is now exposed in the surface language.
- Change from a CEK machine to a CESK machine. Recursive `let` and `def` delay via allocating a cell in the store.  For now, there is no other way to allocate anything in the store, but see discussion at #150 for some possible future directions.
- change the types of `build` and `reprogram` to require a delayed program, e.g. `build : string -> {cmd a} -> cmd string`
- `if` and `try` also require delayed arguments.
- don't elaborate Build and Reprogram with extra Delay wrappers since one is now required by the type
- Division by zero, negative exponents, and bad comparisons now throw exceptions.

Closes #150.  Closes #226.
2021-10-25 13:28:41 +00:00
Ondřej Šebek
d21cbd6c43 Fix zigzag example 2021-10-16 14:52:39 +02:00
Ishan Bhanuka
a80d2904b4
Refactor getx and gety into whereami command (#220)
Closes #178
2021-10-12 20:19:38 +00:00
Paul Brauner
96641beaba
Only update robots that are not waiting. (#176)
Resolves #20 by introducing an active robots set and a waiting robots queue as discussed in the issue.

Growing trees benchmark before change:
```
benchmarking run 1000 game ticks/10 trees
time                 141.1 ms   (137.3 ms .. 146.4 ms)
                     0.997 R²   (0.995 R² .. 1.000 R²)
mean                 140.1 ms   (138.5 ms .. 142.2 ms)
std dev              3.807 ms   (2.703 ms .. 5.189 ms)
                   
benchmarking run 1000 game ticks/20 trees
time                 298.9 ms   (281.7 ms .. 326.0 ms)
                     0.989 R²   (0.972 R² .. 0.998 R²)
mean                 301.7 ms   (295.0 ms .. 311.0 ms)
std dev              13.93 ms   (10.02 ms .. 19.92 ms)
                   
benchmarking run 1000 game ticks/30 trees
time                 470.9 ms   (432.0 ms .. 513.0 ms)
                     0.991 R²   (0.978 R² .. 1.000 R²)
mean                 450.1 ms   (441.9 ms .. 468.4 ms)
std dev              18.85 ms   (5.941 ms .. 30.21 ms)
variance introduced by outliers: 11% (moderately inflated)
```

After change:
```
benchmarking run 1000 game ticks/10 trees
time                 4.666 ms   (4.529 ms .. 4.955 ms)
                     0.963 R²   (0.899 R² .. 0.999 R²)
mean                 4.447 ms   (4.361 ms .. 4.668 ms)
std dev              539.2 μs   (185.5 μs .. 1.097 ms)
variance introduced by outliers: 80% (severely inflated)
                   
benchmarking run 1000 game ticks/20 trees
time                 6.955 ms   (6.826 ms .. 7.122 ms)
                     0.996 R²   (0.993 R² .. 0.999 R²)
mean                 6.855 ms   (6.795 ms .. 6.936 ms)
std dev              262.8 μs   (197.0 μs .. 340.6 μs)
variance introduced by outliers: 25% (moderately inflated)
                   
benchmarking run 1000 game ticks/30 trees
time                 10.53 ms   (9.738 ms .. 11.47 ms)
                     0.954 R²   (0.914 R² .. 0.988 R²)
mean                 9.504 ms   (9.226 ms .. 9.974 ms)
std dev              1.211 ms   (809.9 μs .. 1.948 ms)
variance introduced by outliers: 76% (severely inflated)
```
2021-10-05 20:54:12 +00:00
Brent Yorgey
e61d8494b4 convenience definition 2021-09-13 10:04:48 -05:00
Brent Yorgey
2715cac2c6 fix BFS example to make use of try blocks 2021-09-12 14:44:47 -05:00
Brent Yorgey
cf89d8911f update example 2021-09-06 15:06:41 -05:00
Brent Yorgey
baf8fe005f add 'end' keyword to end of definitions
This way, we don't need parentheses around consecutive definitions.
2021-09-06 06:25:55 -05:00
Brent Yorgey
bcc7467ad3 introduce FLoadEnv frame
This both fixes 'run' commands so they can load modules with
definitions, and makes loading definitions and their associated
contexts much nicer and less ad-hoc.  Also get rid of processCmd which
we were no longer using.
2021-09-06 06:20:25 -05:00
Brent Yorgey
ce3af0afc0 more things for ROADMAP 2021-09-05 21:59:01 -05:00
Brent Yorgey
77ab03afad use x, y for robot position + direction instead of r,c
Still use (r,c) for World, since it's convenient to be able to draw by
rows then columns.
2021-08-29 22:11:40 -05:00
Brent Yorgey
6d2b678ff7 check in some random examples 2021-08-29 16:48:27 -05:00