Playground
Write DS, compile it, and run it. The compiler and the engine
are the ones 1.1.0 publishes, both compiled to WebAssembly —
dsc with its standard library, and libds — from
the release's own tools package, so this runs entirely in your tab. Nothing
is uploaded.
The thermometer reports Celsius; the rule says 85 °F. DS knows both are temperatures and converts, and would refuse to compare a temperature with anything that is not one.
Try this: Change 85_°F to 85_m and compile: a temperature cannot be compared with a length, and nothing runs.
Each row is fed in @at order, one cycle per
row. The output is what ds -b prints.
Each row is one room's reading. The temperature column is in °C, the unit the schema declares; the page converts it to kelvin, the base unit, for the fact file ds reads, so the kitchen's 33 °C is 306.15 there. Edit as JSON shows the file itself, in kelvin.
Run executes
playground.js as a module. Its @ds-lang/ds is
the release's own binding, whose compileFile gives the
program as Compile built it. From playground,
draw(row) adds a row of cells to the grid, and
inputs holds what is in the boxes beside Run.
Test runs the cases in
playground.dstest, as ds test does.
No board yet.
Shaded squares mark the 2×2 boxes. Given digits are bold, filled-in ones italic. A clash is filled red.
Nothing to inspect yet.
The cycle's events and the engine's state after the run.
Working memory
Why matched
What supports it
Timers pending
Pump it again
Activations that have fired and will not fire again for the same facts (refraction).
No network yet.
Facts enter at the top. An alpha tests one fact, a beta
joins what reaches it (a dashed one is a not), and each
terminal at the bottom is a rule.
use default
from units.si import celsius
from units.customary import fahrenheit
schema Thermometer scope logical:
"A room's thermometer, which reports in Celsius."
room: String key
temperature: celsius mutable
schema TooHot scope ttl 1_h:
"A room found too hot."
room: String
rule too_hot:
"A room over 85 °F is too hot, whatever unit its thermometer reports in."
Thermometer t where t.temperature > 85_°F
->
log warning "{t.room} is too hot"
publish TooHot room: t.room
{
"Thermometer": [
{"room": "kitchen", "temperature": 306.15},
{"room": "bedroom", "temperature": 294.15}
]
}
test "33 °C is too hot against a rule in °F":
inputs:
at 0_s:
Thermometer room: "kitchen" temperature: 306.15
produces:
published TooHot room: "kitchen"
logged warning "kitchen is too hot"
test "21 °C is not":
inputs:
at 0_s:
Thermometer room: "bedroom" temperature: 294.15
produces:
never published TooHot room: "bedroom"
use default
schema Motion scope logical:
"Whether someone is in a room."
room: String key
present: Bool mutable
schema LightOn scope logical:
"A room whose light is on."
room: String key
rule light_follows_people:
"A room's light is on while someone is in it."
Motion m where m.present
->
log info "light on in the {m.room}"
emit LightOn logical room: m.room
rule house_empty:
"Nobody is in any room."
no Motion m where m.present
->
log info "nobody home"
{
"Motion": [
{"@at": "0_s", "room": "kitchen", "present": true},
{"@at": "1_min", "room": "hall", "present": true},
{"@at": "2_min", "room": "kitchen", "present": false},
{"@at": "3_min", "room": "hall", "present": false}
]
}
test "the light comes on when someone comes in":
inputs:
at 0_s:
Motion room: "kitchen" present: true
produces:
emitted LightOn room: "kitchen"
wm has LightOn room: "kitchen"
test "nothing turns it off: it goes when nobody is there":
inputs:
at 0_s:
Motion room: "kitchen" present: true
at 1_min:
Motion room: "kitchen" present: false
produces:
emitted LightOn room: "kitchen"
wm count LightOn == 0
logged info "nobody home"
use default
schema Plant scope logical:
"A plant, and how dry its soil may get."
name: String key
thirsty_below: Float mutable
schema Soil scope logical:
"How moist a plant's soil is."
plant: String key
moisture: Float mutable
schema Water scope ttl 1_d:
"A plant to water."
plant: String
rule needs_water:
"A plant whose soil is drier than it can take needs water."
Plant p
Soil s where s.plant == p.name and s.moisture < p.thirsty_below
->
publish Water plant: p.name
{
"Plant": [
{"name": "desert sage", "thirsty_below": 10.0},
{"name": "tomato", "thirsty_below": 40.0},
{"name": "fern", "thirsty_below": 60.0}
],
"Soil": [
{"plant": "desert sage", "moisture": 25.0},
{"plant": "tomato", "moisture": 45.0},
{"plant": "fern", "moisture": 25.0}
]
}
test "a plant whose soil is drier than it can take is watered":
inputs:
at 0_s:
Plant name: "fern" thirsty_below: 60.0
Soil plant: "fern" moisture: 25.0
produces:
published Water plant: "fern"
test "a plant whose soil is wet enough is not":
inputs:
at 0_s:
Plant name: "tomato" thirsty_below: 40.0
Soil plant: "tomato" moisture: 45.0
produces:
never published Water plant: "tomato"
use default
schema Load scope logical:
"A server's CPU use, in percent."
server: String key
cpu: Float mutable
series Load.cpu (by: server):
mean sliding 5_min
service pager.page:
kwarg server: String required
kwarg summary: String required
no response
rule sustained_load:
"A server whose CPU has averaged over 90% for five minutes pages."
Load l where mean(l) > 90.0
->
log critical "{l.server} has averaged over 90% across the last 5 minutes"
call pager.page server: l.server summary: "CPU averaged over 90% for 5 minutes" cooldown: 30_min
{
"Load": [
{"@at": "0_s", "server": "web-1", "cpu": 20.0},
{"@at": "0_s", "server": "web-2", "cpu": 85.0},
{"@at": "1_min", "server": "web-1", "cpu": 99.0},
{"@at": "1_min", "server": "web-2", "cpu": 92.0},
{"@at": "2_min", "server": "web-2", "cpu": 97.0},
{"@at": "2_min", "server": "web-1", "cpu": 22.0}
]
}
test "a server hot for five minutes pages":
inputs:
at 0_s:
Load server: "web-2" cpu: 95.0
at 1_min:
Load server: "web-2" cpu: 96.0
at 2_min:
Load server: "web-2" cpu: 97.0
responses:
for call pager.page server: "web-2" summary: "CPU averaged over 90% for 5 minutes":
no response
produces:
called pager.page
logged critical "web-2 has averaged over 90%"
test "one spike does not":
inputs:
at 0_s:
Load server: "web-1" cpu: 20.0
at 1_min:
Load server: "web-1" cpu: 99.0
at 2_min:
Load server: "web-1" cpu: 22.0
produces:
never called pager.page
use default
schema Link scope logical:
"Whether a link is up, reported when it changes."
name: String key
up: Bool mutable
schema Latency scope logical:
"A link's round-trip time, in milliseconds."
link: String key
rtt: Float mutable
schema Flapping scope logical:
"A link that has gone down three times in ten minutes."
link: String key
series Link.up:
count_of sliding 10_min
series Latency.rtt:
p95, max sliding 10_min
rule flapping:
"Three drops in ten minutes is flapping, for as long as they stay in the window."
Link l where count_of(l, false) >= 3
->
let drops = count_of(l, false)
log warning "{l.name} has gone down {drops} times in 10 minutes: flapping"
emit Flapping logical link: l.name
rule in_service:
"A link up with no drop in ten minutes is in service."
Link l where l.up and count_of(l, false) == 0
->
log info "{l.name} in service: up, with no drop in 10 minutes"
rule slow:
"A link whose 95th percentile round trip is over 200 ms is slow."
Latency t where p95(t) > 200.0
->
let p = p95(t)
let worst = max(t)
log warning "{t.link} is slow: p95 {p} ms over 10 minutes, worst {worst} ms"
{
"Link": [
{"@at": "0_s", "name": "uplink-a", "up": true},
{"@at": "0_s", "name": "uplink-b", "up": true},
{"@at": "0_s", "name": "uplink-c", "up": true},
{"@at": "1_min", "name": "uplink-a", "up": false},
{"@at": "1_min", "name": "uplink-c", "up": false},
{"@at": "90_s", "name": "uplink-a", "up": true},
{"@at": "2_min", "name": "uplink-a", "up": false},
{"@at": "150_s", "name": "uplink-a", "up": true},
{"@at": "5_min", "name": "uplink-a", "up": false},
{"@at": "330_s", "name": "uplink-a", "up": true},
{"@at": "7_min", "name": "uplink-c", "up": true}
],
"Latency": [
{"@at": "3_min", "link": "uplink-b", "rtt": 40.0},
{"@at": "6_min", "link": "uplink-b", "rtt": 45.0},
{"@at": "9_min", "link": "uplink-b", "rtt": 300.0},
{"@at": "11_min", "link": "uplink-b", "rtt": 320.0}
]
}
test "three drops in ten minutes is flapping, until they slide out":
inputs:
at 0_s:
Link name: "uplink-a" up: true
at 1_min:
Link name: "uplink-a" up: false
at 90_s:
Link name: "uplink-a" up: true
at 2_min:
Link name: "uplink-a" up: false
at 150_s:
Link name: "uplink-a" up: true
at 5_min:
Link name: "uplink-a" up: false
at 330_s:
Link name: "uplink-a" up: true
until 12_min
produces:
emitted Flapping link: "uplink-a"
logged warning "uplink-a has gone down 3 times in 10 minutes"
wm count Flapping == 0
test "one long drop is not flapping":
inputs:
at 0_s:
Link name: "uplink-c" up: true
at 1_min:
Link name: "uplink-c" up: false
at 7_min:
Link name: "uplink-c" up: true
until 12_min
produces:
never emitted Flapping link: "uplink-c"
test "a 95th percentile over 200 ms is slow":
inputs:
at 3_min:
Latency link: "uplink-b" rtt: 40.0
at 6_min:
Latency link: "uplink-b" rtt: 45.0
at 9_min:
Latency link: "uplink-b" rtt: 300.0
at 11_min:
Latency link: "uplink-b" rtt: 320.0
produces:
logged warning "uplink-b is slow"
use default
schema Incident scope logical:
"A problem someone must see to."
id: String key
summary: String mutable
schema Ack scope ttl 1_h:
"The pager's answer to a page: who acknowledged it."
corr: String
responder: String
service pager.page:
kwarg who: String required
kwarg summary: String required
response Ack
service pager.notify:
kwarg who: String required
kwarg summary: String required
no response
rule page_on_call:
"Each incident pages on-call, then the backup at 5 minutes and the manager at 15, until someone answers."
Incident i
->
call pager.page who: "on-call" summary: i.summary nocooldown as "page-{i.id}"
on response.responder != "":
log info "{i.id}: {response.responder} acknowledged"
error adapter:
log error "{i.id}: the pager failed ({error.code}): {error.message}"
timeout 5_min:
log warning "{i.id}: no answer in 5 minutes, paging the backup"
call pager.notify who: "backup" summary: i.summary nocooldown as "backup-{i.id}"
timeout 15_min:
log critical "{i.id}: no answer in 15 minutes, calling the manager"
call pager.notify who: "manager" summary: i.summary nocooldown as "manager-{i.id}"
{
"Incident": [
{"@at": "0_s", "id": "web-2", "summary": "Checkout returning errors"},
{"@at": "0_s", "id": "disk-3", "summary": "Disk 95% full"},
{"@at": "5_min", "id": "api-4", "summary": "API latency over 2 seconds"},
{"@at": "5_min", "id": "net-5", "summary": "Packet loss on the uplink"}
],
"Ack": [
{"@at": "6_min", "corr": "page-api-4", "responder": "sam"},
{"@at": "8_min", "corr": "page-web-2", "responder": "alex"}
],
"CallError": [
{"@at": "7_min", "corr": "page-net-5", "code": "adapter.unreachable", "message": "the SMS gateway did not answer"}
]
}
test "an acknowledgement in time stops the escalation":
inputs:
at 0_s:
Incident id: "db-1" summary: "Database replica lagging"
responses:
for call pager.page as "page-db-1" after 2_min:
Ack responder: "sam"
until 20_min
produces:
logged info "db-1: sam acknowledged"
never called pager.notify
test "no answer pages the backup, then calls the manager":
inputs:
at 0_s:
Incident id: "disk-3" summary: "Disk 95% full"
responses:
for call pager.page as "page-disk-3":
no response
for call pager.notify as "backup-disk-3":
no response
for call pager.notify as "manager-disk-3":
no response
until 20_min
produces:
called pager.notify as "backup-disk-3"
called pager.notify as "manager-disk-3"
logged critical "disk-3: no answer in 15 minutes"
test "a failed page is logged, and ends the escalation":
inputs:
at 0_s:
Incident id: "net-5" summary: "Packet loss on the uplink"
responses:
for call pager.page as "page-net-5" after 2_min:
CallError code: "adapter.unreachable" message: "the SMS gateway did not answer"
until 20_min
produces:
logged error "net-5: the pager failed (adapter.unreachable)"
never called pager.notify
use default
schema Door scope logical:
"Whether a door is open."
door: String key
open: Bool mutable
schema LeftOpen scope ttl 1_h:
"A door left open too long."
door: String
rule left_open:
"A door open for over two minutes has been left open."
Door d where d.open and d.dwell > 2_min
->
log warning "the {d.door} door has been open over 2 minutes"
publish LeftOpen door: d.door
{
"Door": [
{"@at": "0_s", "door": "fridge", "open": true},
{"@at": "0_s", "door": "freezer", "open": true},
{"@at": "30_s", "door": "freezer", "open": false}
]
}
test "a door left open over two minutes is reported":
inputs:
at 0_s:
Door door: "fridge" open: true
until 5_min
produces:
published LeftOpen door: "fridge"
test "a door closed in time is not":
inputs:
at 0_s:
Door door: "freezer" open: true
at 30_s:
Door door: "freezer" open: false
until 5_min
produces:
never published LeftOpen door: "freezer"
use default
schema Num scope logical:
"A number from 1 to 4, naming a row or a column."
n: Int key
schema Square scope logical:
"A square's digit, and whether the puzzle gave it."
row: Int key
col: Int key
digit: Int mutable
given: Bool mutable
schema Clash scope logical:
"A square whose digit is repeated in its row, column or box."
row: Int key
col: Int key
schema Empty scope logical:
"A square with no digit."
row: Int key
col: Int key
rule clash:
"Two of a digit in one row, column or 2×2 box clash."
Square a
Square b where (b.digit == a.digit and (b.row != a.row or b.col != a.col)
and (b.row == a.row or b.col == a.col
or ((b.row - 1) // 2 == (a.row - 1) // 2 and (b.col - 1) // 2 == (a.col - 1) // 2)))
->
emit Clash logical row: a.row col: a.col
log warning "row {a.row}, column {a.col}: its {a.digit} is also at row {b.row}, column {b.col}"
rule unfilled:
"A square with no digit is empty."
Num r
Num c
no Square s where s.row == r.n and s.col == c.n
->
emit Empty logical row: r.n col: c.n
rule solved:
"With no empty square and no clash, the grid is solved."
Num one where one.n == 1
no Empty
no Clash
->
log info "solved: every square filled, and no clashes"
{
"Num": [
{"n": 1},
{"n": 2},
{"n": 3},
{"n": 4}
],
"Square": [
{"row": 1, "col": 1, "digit": 1, "given": false},
{"row": 1, "col": 2, "digit": 2, "given": true},
{"row": 1, "col": 3, "digit": 4, "given": false},
{"row": 1, "col": 4, "digit": 3, "given": false},
{"row": 2, "col": 1, "digit": 3, "given": false},
{"row": 2, "col": 2, "digit": 4, "given": false},
{"row": 2, "col": 3, "digit": 1, "given": true},
{"row": 2, "col": 4, "digit": 2, "given": false},
{"row": 3, "col": 1, "digit": 2, "given": true},
{"row": 3, "col": 2, "digit": 1, "given": false},
{"row": 3, "col": 3, "digit": 4, "given": false},
{"row": 3, "col": 4, "digit": 3, "given": true},
{"row": 4, "col": 1, "digit": 4, "given": false},
{"row": 4, "col": 2, "digit": 3, "given": true},
{"row": 4, "col": 3, "digit": 2, "given": true},
{"row": 4, "col": 4, "digit": 1, "given": false}
]
}
test "two of a digit in one row is a clash":
inputs:
at 0_s:
Square row: 1 col: 1 digit: 3 given: true
Square row: 1 col: 2 digit: 3 given: false
produces:
emitted Clash row: 1 col: 1
emitted Clash row: 1 col: 2
test "a full grid with no clash is solved":
inputs:
at 0_s:
Num n: 1
Num n: 2
Num n: 3
Num n: 4
Square row: 1 col: 1 digit: 1 given: true
Square row: 1 col: 2 digit: 2 given: false
Square row: 1 col: 3 digit: 3 given: false
Square row: 1 col: 4 digit: 4 given: false
Square row: 2 col: 1 digit: 3 given: false
Square row: 2 col: 2 digit: 4 given: true
Square row: 2 col: 3 digit: 1 given: false
Square row: 2 col: 4 digit: 2 given: false
Square row: 3 col: 1 digit: 2 given: false
Square row: 3 col: 2 digit: 1 given: false
Square row: 3 col: 3 digit: 4 given: true
Square row: 3 col: 4 digit: 3 given: false
Square row: 4 col: 1 digit: 4 given: false
Square row: 4 col: 2 digit: 3 given: false
Square row: 4 col: 3 digit: 2 given: false
Square row: 4 col: 4 digit: 1 given: true
produces:
logged info "solved"
never emitted Clash
test "a square with no digit is empty, and the grid is not solved":
inputs:
at 0_s:
Num n: 1
produces:
emitted Empty row: 1 col: 1
never logged info "solved"
use default
schema Rule scope logical:
"The rule's number, whose eight bits give each neighbourhood's next state."
id: String key
number: Int mutable
schema Ring scope logical:
"How many cells the ring has; the last cell's right neighbour is the first."
id: String key
width: Int mutable
schema Cell scope logical:
"A cell of one generation: 1 alive, 0 dead."
gen: Int key
pos: Int key
state: Int mutable
schema Next scope logical:
"A cell of the next generation, as the rule works it out."
gen: Int key
pos: Int key
state: Int mutable
rule next_state:
"A cell's next state is the rule number's bit for its left neighbour, itself and its right neighbour."
Rule r
Ring g
Cell c
Cell left where left.gen == c.gen and left.pos == (c.pos + g.width - 1) % g.width
Cell right where right.gen == c.gen and right.pos == (c.pos + 1) % g.width
->
let pattern = left.state * 4 + c.state * 2 + right.state
emit Next logical gen: c.gen + 1 pos: c.pos state: (r.number >> pattern) & 1
test "rule 30 turns one live cell into three":
inputs:
at 0_s:
Rule id: "ca" number: 30
Ring id: "ca" width: 5
Cell gen: 0 pos: 0 state: 0
Cell gen: 0 pos: 1 state: 0
Cell gen: 0 pos: 2 state: 1
Cell gen: 0 pos: 3 state: 0
Cell gen: 0 pos: 4 state: 0
produces:
emitted Next gen: 1 pos: 1 state: 1
emitted Next gen: 1 pos: 2 state: 1
emitted Next gen: 1 pos: 3 state: 1
never emitted Next n where n.state == 1 and (n.pos == 0 or n.pos == 4)
test "rule 90 lights the neighbours and puts the cell out":
inputs:
at 0_s:
Rule id: "ca" number: 90
Ring id: "ca" width: 5
Cell gen: 0 pos: 0 state: 0
Cell gen: 0 pos: 1 state: 0
Cell gen: 0 pos: 2 state: 1
Cell gen: 0 pos: 3 state: 0
Cell gen: 0 pos: 4 state: 0
produces:
emitted Next gen: 1 pos: 1 state: 1
emitted Next gen: 1 pos: 2 state: 0
emitted Next gen: 1 pos: 3 state: 1
test "the ring wraps: the last cell's right neighbour is the first":
inputs:
at 0_s:
Rule id: "ca" number: 30
Ring id: "ca" width: 5
Cell gen: 0 pos: 0 state: 1
Cell gen: 0 pos: 1 state: 0
Cell gen: 0 pos: 2 state: 0
Cell gen: 0 pos: 3 state: 0
Cell gen: 0 pos: 4 state: 0
produces:
emitted Next gen: 1 pos: 4 state: 1
emitted Next gen: 1 pos: 0 state: 1
emitted Next gen: 1 pos: 1 state: 1
// DS guarantees termination: the compiler refuses a rule that feeds its own
// pattern, since that could run without end. So DS cannot make generation
// n + 1 from generation n and then read it. This script carries each
// generation across: it asserts the cells, pumps the engine, and reads back
// the Next facts the rule emitted.
import { open } from '@ds-lang/ds';
import { draw, inputs } from 'playground';
const RULE = BigInt(inputs.rule);
const WIDTH = Number(inputs.width);
const GENERATIONS = Number(inputs.generations);
const ds = await open();
const engine = new ds.Engine();
engine.loadDsc(await ds.compileFile('playground.ds'));
engine.assert('Rule', { id: 'ca', number: RULE });
engine.assert('Ring', { id: 'ca', width: BigInt(WIDTH) });
let row = new Array(WIDTH).fill(0);
row[Math.floor(WIDTH / 2)] = 1;
for (let gen = 0; gen < GENERATIONS; gen++) {
draw(row);
const cells = row.map((state, pos) =>
engine.assert('Cell', { gen: BigInt(gen), pos: BigInt(pos), state: BigInt(state) }));
engine.pump();
const next = new Array(WIDTH).fill(0);
for (const fact of engine.facts({ schema: 'Next' })) {
const { gen: g, pos, state } = fact.read().slots;
if (g === BigInt(gen + 1)) next[Number(pos)] = Number(state);
}
// The rule's Next facts are logical: retracting the cells they came from
// withdraws them too.
for (const cell of cells) engine.retract(cell);
row = next;
}