Adds type synonym declarations to the language, like so:
```
tydef Maybe a = Unit + a end
def lookdown : Cmd (Maybe Text) = scan down end
> lookdown
it2 : Maybe Text = inl ()
```
`tydef` behaves very similarly to `def` except that it defines a new (parameterized) type instead of a new term.
Note that higher-kinded types are not yet supported; for example we cannot say `tydef Foo f = Unit + f Int` (in fact this would be a syntax error since type variables cannot be at the head of an application). However there is nothing stopping us from adding that in the future; I just wanted to keep things simple for now.
If you know of any scenario code that would be a good candidate for using type synonyms, let me know --- it would be nice to update a few scenarios as a better stress test of type synonyms before merging this.
Closes#153.
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.
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.
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`.
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).
- 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.
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.
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.
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)
```
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.