-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add
imap
to world DSL, with examples (#1990)
Adds a new primitive `imap`, aka index map, primitive to the world DSL. `imap` has type `World int -> World int -> World a -> World a`. Think of it like `(Coords -> Coords) -> World a -> World a`, i.e. given a coordinate mapping, it creates a new world by looking up the cell at the transformed coordinates in the given base world. However, since there are no lambdas we cannot directly give it that type; instead, the first `World int` represents a function `Coords -> int` which gives an x coordinate, and the second gives the y coordinate. All told, `imap wx wy wa` is like `\c -> wa (wx c, wy c)`. For example, `imap (-x) y w` is a reflection of `w` across the y-axis. Adds a description of `imap` to the language reference, as well as adding a few examples. Also removes the `rot` and `reflect` primitives, since they can now be simply implemented in terms of `imap`. Depends on merging #1989 first. Closes #1584.
- Loading branch information
Showing
14 changed files
with
178 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,3 +8,4 @@ Speedruns | |
Testing | ||
Vignettes | ||
Mechanics | ||
World Examples |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,3 +2,4 @@ constant.yaml | |
erase.yaml | ||
override.yaml | ||
coords.yaml | ||
reflect.yaml |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
version: 1 | ||
name: Reflection (imap) test | ||
description: | | ||
A world with both horizontal and vertical reflection symmetry, | ||
created with 'imap'. | ||
creative: false | ||
objectives: | ||
- goal: | ||
- Pick up four trees | ||
condition: | | ||
as base {n <- count "tree"; return (n >= 4)} | ||
robots: | ||
- name: base | ||
loc: [0, 0] | ||
dir: north | ||
devices: | ||
- logger | ||
- grabber | ||
- treads | ||
- branch predictor | ||
- scanner | ||
- ADT calculator | ||
- comparator | ||
- GPS receiver | ||
- bitcoin | ||
solution: | | ||
def x = \n. \c. if (n==0) {} {c; x (n-1) c} end | ||
def ifC = \p. \t. \e. b <- p; if b t e end | ||
def findTree = ifC (ishere "tree") {whereami} {move; findTree} end | ||
def ell = \d. turn right; x (2*d) move; grab; return () end | ||
def grabTrees = \loc. let x = fst loc in let y = snd loc in grab; ell y; ell x; ell y end | ||
n <- random 10; | ||
x (n+1) move; turn right; move; | ||
loc <- findTree; | ||
grabTrees loc | ||
known: [tree] | ||
world: | ||
dsl: | | ||
let trees = if (hash % 4 == 0) then {tree, dirt} else {stone} | ||
in | ||
overlay | ||
[ mask (x >= 0 && y >= 0) trees | ||
, mask (x >= 0 && y < 0) (imap x (-y) trees) | ||
, mask (x < 0 && y >= 0) (imap (-x) y trees) | ||
, mask (x < 0 && y < 0) (imap (-x) (-y) trees) | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
clearing.yaml | ||
rorschach.yaml | ||
stretch.yaml | ||
translate.yaml |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
version: 1 | ||
name: Clearing | ||
description: | | ||
The base is in a clearing in the forest: the area within a certain | ||
radius of the base is completely clear of trees; then there are | ||
random trees at increasing density up to another radius; outside of | ||
the outer radius there are only trees. | ||
creative: true | ||
robots: | ||
- name: base | ||
display: | ||
char: Ω | ||
loc: [0, 0] | ||
dir: north | ||
world: | ||
dsl: | | ||
overlay | ||
[ {dirt} | ||
, mask ((x*x + 4*y*y) >= (6*6) && (x*x + 4*y*y) <= (30*30)) | ||
(let h = hash % 24 in if (36 + h*h) <= (x*x + 4*y*y) then {tree,dirt} else {dirt} ) | ||
, mask ((x*x + 4*y*y) > (30*30)) {tree, dirt} | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
version: 1 | ||
name: Rorschach | ||
description: | | ||
A world with both horizontal and vertical reflection symmetry, | ||
created with `imap`{=snippet}. | ||
creative: true | ||
robots: | ||
- name: base | ||
dir: north | ||
loc: [0, 0] | ||
known: [tree] | ||
world: | ||
dsl: | | ||
let trees = if (hash % 4 == 0) then {tree, dirt} else {stone} | ||
in | ||
overlay | ||
[ mask (x >= 0 && y >= 0) trees | ||
, mask (x >= 0 && y < 0) (imap x (-y) trees) | ||
, mask (x < 0 && y >= 0) (imap (-x) y trees) | ||
, mask (x < 0 && y < 0) (imap (-x) (-y) trees) | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
version: 1 | ||
name: Stretch | ||
description: | | ||
A world created by stretching a random pattern of trees, with the | ||
amount of stretching determined by the distance from the origin. | ||
creative: true | ||
robots: | ||
- name: base | ||
dir: north | ||
loc: [0, 0] | ||
known: [tree] | ||
world: | ||
dsl: | | ||
let trees = if (hash % 4 == 0) then {tree, dirt} else {stone} | ||
in | ||
imap (if (y == 0) then 0 else (x/abs(y))) (if (abs x <= 1) then 0 else (y/abs(x/2))) trees |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
version: 1 | ||
name: Translate | ||
description: | | ||
An illustration of how to use `imap`{=snippet} to translate. A basic patch is | ||
created and then overlaid at various translations. Note that since | ||
`imap`{=snippet} works by mapping a function over the coordinates, translation | ||
is "backwards": for example, `imap (x+4)`{=snippet} translates 4 units to the | ||
*left*. | ||
creative: true | ||
robots: | ||
- name: base | ||
dir: north | ||
loc: [0, 0] | ||
known: [rock] | ||
world: | ||
dsl: | | ||
let patch = mask (abs(x) <= 4 && abs(y) <= 4) (if ((x + y) % 2 == 0) then {rock, dirt} else {dirt}) | ||
in | ||
overlay | ||
[ patch | ||
, imap (x+6) (y+3) patch | ||
, imap (x-10) (y-7) patch | ||
, imap (x-14) (y+5) patch | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters