# Streams

*The stream operators relate the positions of a value along one dimension: `first`, `next` and `fby`, and Lucid's `wvr`, `asa` and `upon`, which read a value at the positions where a condition is true. Each entry gives the form, a program with its answers, every rule, and the mistakes with their messages. The examples are read as [How to read the examples](https://elucid8-lang.org/learn/tutorial.md#how-to-read-the-examples) says.*

## Along a dimension {#along-a-dimension}

**Does:** these rules hold for all six operators.

**Example:**

```el8
-- file stream-rules.el8
-- Readings kept from t = 3 (the origin of t is still 0), and how fby groups.
reading = input("reading", t : 3 .. 6)
opening = first.t reading
before = 0 fby.t reading
n = 0 fby.t n + 1
steps = 1 fby.t 2 fby.t 3
-- table reading = t,value; 3,30; 4,40; 5,50; 6,60
-- expect opening = eod
-- expect before[t = 3] = eod
-- expect before[t = 4] = 30
-- expect n[t = 2] = 2
-- expect steps[t = 1] = 2
-- expect steps[t = 2] = 3
```

**Answers:**

| point | what is read | answer |
|---|---|---|
| `opening` | `reading` at t = 0, outside its range 3 to 6 | eod |
| `before` at t = 3 | `reading` at t = 2, outside its range | eod |
| `before` at t = 4 | `reading` at t = 3 | 30 |
| `n` at t = 2 | `n + 1` at t = 1, as `0 fby.t (n + 1)` | 2 |
| `steps` at t = 1 | `2 fby.t 3` at t = 0 | 2 |
| `steps` at t = 2 | `2 fby.t 3` at t = 1 | 3 |

**Rules:**

1. Each operator names the dimension it moves along, after a dot: `first.t e`, `next.t e`, `a fby.t b`, `y wvr.t q`, `y asa.t q`, `y upon.t q`. Without the dimension, the compiler refuses the program (each entry's Mistakes give the message).
2. The dimension's coordinates are whole numbers. Along a dimension whose coordinates are texts, the compiler refuses each of the six (for `fby`: [fby](#fby), Mistakes, 5).
3. Every dimension's **origin** is position 0, whatever bound a value or an input has along it. An input over `t : 3 .. 6` is eod at t = 0, so its `first.t` is eod and `fby.t`'s right operand reads eod at t = 3 (the example).
4. `fby`, `wvr`, `asa` and `upon` bind as `if … then … else` does: more loosely than every other operator except `assert` and `in` ([Checking what a program assumes](https://elucid8-lang.org/learn/tutorial.md#19-checking-what-a-program-assumes-and-numbers-as-text), [Programs over several files](https://elucid8-lang.org/learn/tutorial.md#programs-over-several-files-in)). They group to the right. So in the example, `n` is `0 fby.t (n + 1)`, and `steps` is `1 fby.t (2 fby.t 3)`:

   ```el8 from=stream-rules.el8
   n = 0 fby.t n + 1
   steps = 1 fby.t 2 fby.t 3
   ```

   From [stream-rules.el8](#along-a-dimension).
5. `first.d` and `next.d` are prefixes, and bind as tightly as a minus sign before a number. So `rise` is `(next.t reading) - reading`:

   ```el8 from=next-rise.el8
   rise = next.t reading - reading
   ```

   From [next-rise.el8](#next).

## first {#first}

*Why:* a value measured against where it started: each reading's change since the first, each price against the opening price.

**Does:** `first.d e` is `e` at d = 0, the same at every coordinate of `d`.

**Form:** `first.d e`, where `d` is a dimension of whole numbers that `e` varies along, and `e` is an expression.

**Example:**

```el8
-- file first-start.el8
-- Each reading's change since the first reading.
reading = input("reading", t : 4)
start = first.t reading
change = reading - first.t reading
at_zero = reading[t = 0]
-- table reading = t,value,ts; 0,20,1000; 1,22,2000; 2,25,3000; 3,21,4000
-- expect start = 20, at time 1000 ms
-- expect start[t = 3] = 20, at time 1000 ms
-- expect change[t = 2] = 5, at time 3000 ms
-- expect change[t = 4] = eod, at time 1000 ms
-- expect at_zero = 20, at time 1000 ms
```

**Answers:**

| point | `reading` there | `start` | `change` | `at_zero` |
|---|---|---|---|---|
| no `t` | | 20 (at 1000 ms) | | 20 (at 1000 ms) |
| t = 2 | 25 (at 3000 ms) | | 5 (at 3000 ms) | |
| t = 3 | 21 (at 4000 ms) | 20 (at 1000 ms) | | |
| t = 4 | eod: past the bound | | eod (at 1000 ms) | |

**Rules:**

1. `first.d e` is `e` at the context with `d` set to 0, every other coordinate unchanged.
2. The result does not vary along `d`: it has `e`'s dimensions except `d`. A query may name `d` or leave it out, with the same answer (`start` and `start[t = 3]`).
3. `e` must vary along `d`; otherwise the compiler refuses the program (Mistakes, 2).
4. eod: where `e` at d = 0 is eod, the result is eod at every coordinate of `d` ([Along a dimension](#along-a-dimension), rule 3).
5. Missing data and errors: where `e` at d = 0 is an error, the result is that error, naming its origin. A reading with no row inside the input's bound is such an error ([Results](https://elucid8-lang.org/learn/tutorial.md#3-results-values-eod-and-error)):

   ```el8
   -- file first-gap.el8
   -- No row at t = 0: the first reading is missing.
   reading = input("reading", t : 4)
   start = first.t reading
   -- table reading = t,value,ts; 1,22,2000; 2,25,3000; 3,21,4000
   -- expect start = error missing from reading[t = 0]: input reading has no data at this point
   ```

6. The timestamp: [How operators combine them](https://elucid8-lang.org/learn/timestamps.md#how-operators-combine-them).

**Mistakes:**

1. *No dimension.* `start = first reading` gives `first works along a dimension, named after a dot: write first.d reading, with d that dimension`. Fix: name the dimension, `first.t reading`.
2. *A dimension `e` does not vary along.* `start = first.s reading` gives `first.s moves along s, but its argument reading does not vary along s: the move has no effect`. Fix: name a dimension of `reading`, `first.t`.

Each wrong line above is a fragment of a program under **Mistake programs**.

**Neighbours:** `reading[t = 0]` gives the same answer (`at_zero`; [Looking elsewhere](https://elucid8-lang.org/learn/tutorial.md#2-looking-elsewhere-d--e)). From [first-start.el8](#first). For the first position where a condition is true, rather than position 0, [asa](#asa).

*Lineage:* `first` is Lucid's (W. W. Wadge and E. A. Ashcroft, *Lucid, the Dataflow Programming Language*, Academic Press, 1985), with the dimension named.

**Mistake programs:**

```el8
-- file first-no-dimension.el8
-- Mistake 1: first with no dimension.
reading = input("reading", t : 4)
start = first reading
-- expect compile error: first works along a dimension, named after a dot: write first.d reading, with d that dimension
```

```el8
-- file first-other-dimension.el8
-- Mistake 2: first along a dimension its argument does not vary along.
reading = input("reading", t : 4)
start = first.s reading
-- expect compile error: first.s moves along s, but its argument reading does not vary along s: the move has no effect
```

## next {#next}

*Why:* a value compared with the one after it: how much each reading rises by the next, whether the next step is higher.

**Does:** `next.d e` is `e` one position ahead along `d`.

**Form:** `next.d e`, where `d` is a dimension of whole numbers that `e` varies along, and `e` is an expression.

**Example:**

```el8
-- file next-rise.el8
-- How much each reading rises by the next one; there is no row at t = 2.
reading = input("reading", t : 4)
ahead = next.t reading
rise = next.t reading - reading
ahead_too = reading[t = #t + 1]
-- table reading = t,value,ts; 0,20,1000; 1,22,2000; 3,21,4000
-- expect ahead[t = 0] = 22, at time 2000 ms
-- expect ahead[t = 1] = error missing from reading[t = 2]: input reading has no data at this point
-- expect ahead[t = 3] = eod
-- expect rise[t = 0] = 2, at time 2000 ms
-- expect rise[t = 2] = error missing from reading[t = 2]: input reading has no data at this point, at time 4000 ms
-- expect ahead_too[t = 0] = 22, at time 2000 ms
```

**Answers:**

| t | `reading` | `ahead` | `rise` | `ahead_too` |
|---|---|---|---|---|
| 0 | 20 (at 1000 ms) | 22 (at 2000 ms) | 2 (at 2000 ms) | 22 (at 2000 ms) |
| 1 | 22 (at 2000 ms) | Error via reading[t = 2]: missing | | |
| 2 | missing | | Error via reading[t = 2]: missing (at 4000 ms) | |
| 3 | 21 (at 4000 ms) | eod: t = 4 is past the bound | | |

**Rules:**

1. `next.d e` at d = c is `e` at the context with d = c + 1, every other coordinate unchanged.
2. The result has `e`'s dimensions.
3. `e` must vary along `d`; otherwise the compiler refuses the program with the message of [first](#first), Mistakes, 2, naming `next`.
4. eod: where c + 1 is past `e`'s bound along `d`, the result is eod (`ahead` at t = 3).
5. Missing data and errors: the result is the outcome of `e` at c + 1, unchanged (`ahead` at t = 1). An operator that reads it is strict ([Results](https://elucid8-lang.org/learn/tutorial.md#3-results-values-eod-and-error)): `rise` at t = 2 is the error too.
6. The timestamp: [How operators combine them](https://elucid8-lang.org/learn/timestamps.md#how-operators-combine-them).
7. Inside `fby`'s right operand, which is read one step back, `next.d e` is `e` at the current position ([fby](#fby), rule 3).

**Mistakes:**

1. *No dimension.* `ahead = next reading` gives `next works along a dimension, named after a dot: write next.d reading, with d that dimension`. Fix: `next.t reading`.
2. *A value defined from itself one step ahead.* `x = restrict next.t x + #t to (t : 10) end`: no message. Every point reads the point after it, up to the bound, where `x` is eod, so `x` is eod at every `t`. With no bound, a query never reaches a value: it stops at the demand-depth limit with a program error, not an answer. Fix: define a value from the step before, with [fby](#fby).

Each wrong line above is a fragment of a program under **Mistake programs**.

**Neighbours:** `reading[t = #t + 1]` gives the same answer (`ahead_too`; [Looking elsewhere](https://elucid8-lang.org/learn/tutorial.md#2-looking-elsewhere-d--e)). From [next-rise.el8](#next). To look back one step, [fby](#fby).

*Lineage:* `next` is Lucid's (Wadge and Ashcroft, 1985), with the dimension named.

**Mistake programs:**

```el8
-- file next-no-dimension.el8
-- Mistake 1: next with no dimension.
reading = input("reading", t : 4)
ahead = next reading
-- expect compile error: next works along a dimension, named after a dot: write next.d reading, with d that dimension
```

```el8
-- file next-ahead-of-itself.el8
-- Mistake 2: a value defined from itself one step ahead reads up to its bound, and is eod everywhere.
x = restrict next.t x + #t to (t : 10) end
-- expect x[t = 0] = eod
-- expect x[t = 9] = eod
```

## fby {#fby}

*Why:* anything that grows step by step: a counter, a running total, a state carried from one step to the next.

**Does:** `a fby.d b` ("followed by") is `a` at d = 0, and at every later position `b` read one step back.

**Form:** `a fby.d b`, where `d` is a dimension of whole numbers and `a` and `b` are expressions; `b` may name the value being defined.

**Example:**

```el8
-- file fby-total.el8
-- A count of the steps, the running total of the readings, the reading before, and the change since it.
reading = input("reading", t : 4)
n = 0 fby.t n + 1
total = reading fby.t total + next.t reading
previous = 0 fby.t reading
change = error("no reading before t = 0") fby.t next.t reading - reading
back = reading[t = #t - 1]
so_far = sum reading over (t : 0 .. #t) end
-- table reading = t,value,ts; 0,20,1000; 1,22,2000; 2,25,3000; 3,21,4000
-- expect n[t = 3] = 3
-- expect n[t = 50] = 50
-- expect total[t = 0] = 20, at time 1000 ms
-- expect total[t = 1] = 42, at time 2000 ms
-- expect total[t = 3] = 88, at time 4000 ms
-- expect total[t = 4] = eod, at time 4000 ms
-- expect previous[t = 0] = 0
-- expect previous[t = 1] = 20, at time 1000 ms
-- expect change[t = 0] = error raised: no reading before t = 0
-- expect change[t = 1] = 2, at time 2000 ms
-- expect back[t = 0] = eod
-- expect so_far[t = 3] = 88, at time 4000 ms
```

**Answers:**

| t | `reading` | `n` | `total` | `previous` | `change` | `back` | `so_far` |
|---|---|---|---|---|---|---|---|
| 0 | 20 (at 1000 ms) | | 20 (at 1000 ms) | 0 | Error: no reading before t = 0 | eod | |
| 1 | 22 (at 2000 ms) | | 42 (at 2000 ms) | 20 (at 1000 ms) | 2 (at 2000 ms) | | |
| 3 | 21 (at 4000 ms) | 3 | 88 (at 4000 ms) | | | | 88 (at 4000 ms) |
| 4 | eod: past the bound | | eod (at 4000 ms) | | | | |
| 50 | | 50 | | | | | |

**Rules:**

1. At d = 0, `a fby.d b` is `a` at the same context. At d = c, for c > 0, it is `b` at the context with d = c − 1, every other coordinate unchanged.
2. Only the operand selected is evaluated: `a` at d = 0, `b` at every later position. So `change` at t = 1 is 2: the error on its left is never evaluated there.
3. `b` is read one step back, coordinates included. Inside `b`, `#d` is c − 1, `next.d e` is `e` at c, and a name is its value at c − 1. So `total` adds `next.t reading`: one step back, `next` gives the reading at the current step.

   ```el8 from=fby-total.el8
   total = reading fby.t total + next.t reading
   ```

   From [fby-total.el8](#fby).
4. A value may name itself in `b`: it then reads itself one step back, and is an **iteration** (`n`, `total`). A value that depends on itself at the same context is refused before evaluation (Mistakes, 1).
5. The result varies along `d`, and along every dimension `a` and `b` vary along.
6. Precedence: [Along a dimension](#along-a-dimension), rule 4.
7. eod: the result is the selected operand's outcome. `total` at t = 4 is eod, because `next.t reading` at t = 3 reads past the bound. With no bound written, a value defined with `fby` has no end: `n` counts for ever ([Values over contexts](https://elucid8-lang.org/learn/tutorial.md#1-values-over-contexts)).
8. Missing data and errors: the result is the selected operand's outcome (`change` at t = 0). An error read at one step of an iteration is passed on at every later step, naming its origin:

   ```el8
   -- file fby-gap.el8
   -- A running total over readings with no row at t = 2.
   reading = input("reading", t : 4)
   total = reading fby.t total + next.t reading
   -- table reading = t,value,ts; 0,20,1000; 1,22,2000; 3,21,4000
   -- expect total[t = 1] = 42, at time 2000 ms
   -- expect total[t = 2] = error missing from reading[t = 2]: input reading has no data at this point, at time 2000 ms
   -- expect total[t = 3] = error missing from reading[t = 2]: input reading has no data at this point, at time 4000 ms
   ```

9. The timestamp: [How operators combine them](https://elucid8-lang.org/learn/timestamps.md#how-operators-combine-them).

**Mistakes:**

1. *An equation that names itself at the same point.* `total = total + reading` gives `unguarded cycle: total -> total (a value cannot depend on itself at the same context)`. Fix: `total = reading fby.t total + next.t reading`.
2. *The current reading on the right.* `wrong = reading fby.t wrong + reading`: no message. The right operand is read one step back (rule 3), so it adds the reading before, and `wrong` at t = 1 is 40, not 42. Fix: `next.t reading`.
3. *The coordinate on the right.* `fac = restrict 1 fby.n fac * #n to (n : 20) end`: no message. At n = 1 the right operand reads `#n` as 0, so `fac` is 0 from n = 1 on. Fix: `1 fby.n fac * (#n + 1)`, which gives 120 at n = 5.
4. *No dimension.* `n = 0 fby n + 1` gives `fby works along a dimension, named after a dot: write 0 fby.d n, with d that dimension`. Fix: `0 fby.t n + 1`.
5. *A dimension of texts.* `s = 0 fby.zone sales`, with `zone` listed as `{"north", "east"}`, gives `zone's coordinates are whole numbers here (fby.zone moves along it), but texts at 3:1 ("north" is listed as a coordinate of zone): a dimension's coordinates are all whole numbers or all texts, so use texts for zone here too, or give the whole numbers a dimension of their own`. Fix: move along a dimension of whole numbers.

Each wrong line above is a fragment of a program under **Mistake programs**.

**Neighbours:** `reading[t = #t - 1]` reads one step back too, but is eod at t = 0 (`back`), where `fby` gives its left operand ([Looking elsewhere](https://elucid8-lang.org/learn/tutorial.md#2-looking-elsewhere-d--e)). A total of every position so far is also `sum reading over (t : 0 .. #t) end` (`so_far`; [Regions and aggregates](https://elucid8-lang.org/learn/tutorial.md#6-regions-and-aggregates)). From [fby-total.el8](#fby). To read a value at a slower rate, [wvr](#wvr) and [upon](#upon).

*Lineage:* `fby` is Lucid's: in Wadge and Ashcroft (1985), the Lucid equation `x = 0 fby x + 1` is the counter 0, 1, 2, …. eLucid8 names the dimension, and calls a value defined this way an iteration.

**Mistake programs:**

```el8
-- file fby-same-point.el8
-- Mistake 1: an equation that names itself at the same point.
reading = input("reading", t : 4)
total = total + reading
-- expect compile error: unguarded cycle: total -> total (a value cannot depend on itself at the same context)
```

```el8
-- file fby-current-reading.el8
-- Mistake 2: the current reading on the right of fby is read one step back.
reading = input("reading", t : 4)
wrong = reading fby.t wrong + reading
-- table reading = t,value,ts; 0,20,1000; 1,22,2000; 2,25,3000; 3,21,4000
-- expect wrong[t = 1] = 40, at time 1000 ms
```

```el8
-- file fby-coordinate.el8
-- Mistake 3: #n on the right of fby is read one step back too.
fac = restrict 1 fby.n fac * #n to (n : 20) end
good = restrict 1 fby.n good * (#n + 1) to (n : 20) end
-- expect fac[n = 5] = 0
-- expect good[n = 5] = 120
```

```el8
-- file fby-no-dimension.el8
-- Mistake 4: fby with no dimension.
n = 0 fby n + 1
-- expect compile error: fby works along a dimension, named after a dot: write 0 fby.d n, with d that dimension
```

```el8
-- file fby-text-dimension.el8
-- Mistake 5: fby along a dimension of texts.
sales = input("sales", zone : {"north", "east"})
s = 0 fby.zone sales
-- expect compile error: zone's coordinates are whole numbers here (fby.zone moves along it), but texts at 3:1 ("north" is listed as a coordinate of zone)
```

## The walk of wvr, asa and upon {#walk}

**Does:** `wvr`, `asa` and `upon` find the positions along `d` where a condition `q` is true by a walk, with these rules.

**Example:**

```el8
-- file walk-gaps.el8
-- No row at t = 2 (missing) and an error at t = 4.
q = input("q", t : 8)
y = restrict 100 + #t to (t : 8) end
w = y wvr.t q
u = y upon.t q
p = restrict default(q, false, "missing") to (t : 8) end
w2 = y wvr.t p
-- table q = t,value,ts; 0,false,1000; 1,true,2000; 3,true,4000; 4,error: bad reading,5000; 5,true,6000; 6,false,7000; 7,true,8000
-- expect w[t = 0] = 101, at time 2000 ms
-- expect w[t = 1] = error missing from q[t = 2]: input q has no data at this point, at time 2000 ms
-- expect u[t = 2] = 101, at time 2000 ms
-- expect u[t = 4] = error missing from q[t = 2]: input q has no data at this point, at time 2000 ms
-- expect w2[t = 1] = 103, at time 4000 ms
-- expect w2[t = 2] = error data from q[t = 4]: bad reading, at time 5000 ms
```

**Answers:**

| point | the walk | answer |
|---|---|---|
| `w` at t = 0 | q at 0 false, at 1 true: the first true position is 1 | 101 (at 2000 ms) |
| `w` at t = 1 | q at 2 is missing, before a second true position | Error via q[t = 2]: missing (at 2000 ms) |
| `u` at t = 2 | q at 0 and 1: one true position, so `y` at 1 | 101 (at 2000 ms) |
| `u` at t = 4 | q at 0 to 3: q at 2 is missing | Error via q[t = 2]: missing (at 2000 ms) |
| `w2` at t = 1 | p passes over the gap: true at 1 and 3 | 103 (at 4000 ms) |
| `w2` at t = 2 | p at 4 is the error in the data | Error via p ← q[t = 4]: bad reading (at 5000 ms) |

**Rules:**

1. At a point with coordinate c along `d`, the walk examines `q` at positions 0, 1, 2, … along `d`, in order, every other coordinate as at the point. Each entry says how far it walks and which position it reads `y` at.
2. A position where `q` is `true` counts; one where `q` is `false` is passed over.
3. An eod in `q` ends the walk, and the result is eod: the condition's data has ended, so there is no later position to find.
4. An error in `q` is the result, naming its origin: the walk cannot know whether that position counts. A missing reading is such an error (`w` at t = 1). So the first of an eod and an error that the walk meets is the result.
5. A `q` that is not a boolean gives the error `wvr needs a boolean condition` (`asa needs …`, `upon needs …`).
6. Past `q`'s bound along `d`, the result is eod.
7. Along a dimension declared `inf` in an input that `q` reads, the walk looks only up to the last row along `d` ([Results](https://elucid8-lang.org/learn/tutorial.md#3-results-values-eod-and-error)):

   ```el8
   -- file walk-inf.el8
   -- Readings with no end: the walk stops at the last row along t.
   reading = input("reading", t : inf)
   passed = reading wvr.t reading >= 20
   -- table reading = t,value; 0,18; 1,21; 2,19; 3,24
   -- expect passed[t = 1] = 24
   -- expect passed[t = 2] = eod
   ```

8. `wvr` and `asa` need `q` bounded along `d`, or reading an input along `d`; otherwise the compiler refuses the program ([wvr](#wvr), Mistakes, 1). `upon` needs neither: it examines only positions before c.
9. To pass over missing readings rather than stop at them, `default(q, false, "missing")` (`w2`; [default](https://elucid8-lang.org/learn/tutorial.md#16-the-standard-library-default-and-dates)).
10. The timestamp: [How operators combine them](https://elucid8-lang.org/learn/timestamps.md#how-operators-combine-them).

## wvr {#wvr}

*Why:* a value read only where a condition holds, renumbered: the readings that passed a check, one after another; the positions of the events.

**Does:** `y wvr.d q` ("whenever") at d = c is `y` at the (c + 1)th position along `d` where `q` is true.

**Form:** `y wvr.d q`, where `d` is a dimension of whole numbers, `y` an expression and `q` a condition.

**Example:**

```el8
-- file wvr-passed.el8
-- The readings of at least 20, one after another, and where each was.
reading = input("reading", t : 8)
ok = reading >= 20
passed = reading wvr.t ok
position = #t wvr.t ok
-- table reading = t,value,ts; 0,18,1000; 1,21,2000; 2,19,3000; 3,24,4000; 4,22,5000; 5,17,6000; 6,25,7000; 7,16,8000
-- expect passed[t = 0] = 21, at time 2000 ms
-- expect passed[t = 1] = 24, at time 4000 ms
-- expect passed[t = 3] = 25, at time 7000 ms
-- expect passed[t = 4] = eod, at time 8000 ms
-- expect position[t = 2] = 4, at time 5000 ms
```

**Answers:**

| t | `reading` | `ok` | `passed` | `position` |
|---|---|---|---|---|
| 0 | 18 | false | 21 (at 2000 ms) | |
| 1 | 21 | true | 24 (at 4000 ms) | |
| 2 | 19 | false | | 4 (at 5000 ms) |
| 3 | 24 | true | 25 (at 7000 ms) | |
| 4 | 22 | true | eod (at 8000 ms): no fifth | |
| 5 | 17 | false | | |
| 6 | 25 | true | | |
| 7 | 16 | false | | |

**Rules:**

1. At d = c, the walk ([The walk of wvr, asa and upon](#walk)) goes on until it has met c + 1 true positions; the result is `y` at the last of them, s, every other coordinate unchanged.
2. If the walk passes `q`'s bound first, the result is eod (`passed` at t = 4: `ok` is true four times).
3. The result varies along `d`.
4. eod, missing data and errors in `q`: [The walk](#walk), rules 3 to 6. In `y`: the outcome of `y` at s, unchanged.
5. The timestamp: [How operators combine them](https://elucid8-lang.org/learn/timestamps.md#how-operators-combine-them).

**Mistakes:**

1. *A condition with no bound along `d`.* `picked = level wvr.t every_third`, with `every_third = mod(#t, 3) == 0`, gives `wvr.t searches along t, which has no end, and its condition reads no data along t; bound it: restrict … to (t : n) end`. Fix: `every_third = restrict mod(#t, 3) == 0 to (t : 20) end`.
2. *A condition that is not a boolean.* `kept = reading wvr.t reading`: no compile message; the answer is `Error: wvr needs a boolean condition`. Fix: a comparison, `reading wvr.t reading >= 20`.
3. *No dimension.* `kept = reading wvr ok` gives `wvr works along a dimension, named after a dot: write reading wvr.d ok, with d that dimension`. Fix: `reading wvr.t ok`.

Each wrong line above is a fragment of a program under **Mistake programs**.

**Neighbours:** for the first true position only, [asa](#asa); to read `y` more slowly rather than skip its positions, [upon](#upon). To combine the positions where a condition holds rather than list them, an aggregate with an `if` filter ([Regions and aggregates](https://elucid8-lang.org/learn/tutorial.md#6-regions-and-aggregates)).

*Lineage:* `wvr` ("whenever") is Lucid's (Wadge and Ashcroft, 1985). Where Lucid's `wvr` waits for ever for a true position that never comes, eLucid8 gives eod past the condition's bound.

**Mistake programs:**

```el8
-- file wvr-no-bound.el8
-- Mistake 1: a condition with no bound along t.
level = restrict 10 * #t to (t : 20) end
every_third = mod(#t, 3) == 0
picked = level wvr.t every_third
-- expect compile error: wvr.t searches along t, which has no end, and its condition reads no data along t; bound it: restrict … to (t : n) end
```

```el8
-- file wvr-not-boolean.el8
-- Mistake 2: a condition that is a number.
reading = input("reading", t : 4)
kept = reading wvr.t reading
-- table reading = t,value; 0,18; 1,21; 2,19; 3,24
-- expect kept[t = 0] = error type: wvr needs a boolean condition
```

```el8
-- file wvr-no-dimension.el8
-- Mistake 3: wvr with no dimension.
reading = input("reading", t : 4)
ok = reading >= 20
kept = reading wvr ok
-- expect compile error: wvr works along a dimension, named after a dot: write reading wvr.d ok, with d that dimension
```

## asa {#asa}

*Why:* the first time something happens: the first reading above a threshold, the first step of an iteration that is good enough.

**Does:** `y asa.d q` ("as soon as") is `y` at the first position along `d` where `q` is true, the same at every coordinate of `d`.

**Form:** `y asa.d q`, where `d` is a dimension of whole numbers, `y` an expression and `q` a condition.

**Example:**

```el8
-- file asa-first-pass.el8
-- The first reading of at least 20, and where it was.
reading = input("reading", t : 8)
ok = reading >= 20
first_ok = reading asa.t ok
where_ok = #t asa.t ok
never = reading asa.t reading > 100
first_ok_too = first.t (reading wvr.t ok)
where_ok_too = min #t over (t : 8) if ok end
-- table reading = t,value,ts; 0,18,1000; 1,21,2000; 2,19,3000; 3,24,4000; 4,22,5000; 5,17,6000; 6,25,7000; 7,16,8000
-- expect first_ok = 21, at time 2000 ms
-- expect first_ok[t = 5] = 21, at time 2000 ms
-- expect where_ok = 1, at time 2000 ms
-- expect never = eod, at time 8000 ms
-- expect first_ok_too = 21, at time 2000 ms
-- expect where_ok_too = 1, at time 8000 ms
```

**Answers:**

| value | the walk | answer |
|---|---|---|
| `first_ok` | `ok` false at 0, true at 1 | 21 (at 2000 ms), at every `t` |
| `where_ok` | the same | 1 (at 2000 ms) |
| `never` | no reading above 100 at 0 to 7, then past the bound | eod (at 8000 ms) |
| `first_ok_too` | `wvr`'s walk at t = 0 | 21 (at 2000 ms) |
| `where_ok_too` | not a walk: an aggregate over all eight readings | 1 (at 8000 ms) |

**Rules:**

1. `y asa.d q` is `y wvr.d q` at d = 0: the walk ([The walk of wvr, asa and upon](#walk)) goes on until the first true position s, and the result is `y` at s.
2. The result does not vary along `d`: it has `y`'s and `q`'s dimensions except `d`. A query may name `d` or leave it out, with the same answer (`first_ok` and `first_ok[t = 5]`).
3. If the walk passes `q`'s bound first, the result is eod (`never`).
4. eod, missing data and errors in `q`: [The walk](#walk), rules 3 to 6. In `y`: the outcome of `y` at s, unchanged.
5. The timestamp: [How operators combine them](https://elucid8-lang.org/learn/timestamps.md#how-operators-combine-them).

**Mistakes:**

1. *A condition with no bound along `d`.* As for [wvr](#wvr), Mistakes, 1, with `asa.t` in the message: `asa.t searches along t, which has no end, and its condition reads no data along t; bound it: restrict … to (t : n) end`.
2. *No dimension.* `first_ok = reading asa ok` gives `asa works along a dimension, named after a dot: write reading asa.d ok, with d that dimension`. Fix: `reading asa.t ok`.

Each wrong line above is a fragment of a program under **Mistake programs**.

**Neighbours:** `first.t (reading wvr.t ok)` gives the same answer (`first_ok_too`). For the position, `#t asa.t ok` (`where_ok`), or `min #t over (t : 8) if ok end` (`where_ok_too`; [Regions and aggregates](https://elucid8-lang.org/learn/tutorial.md#6-regions-and-aggregates)), which reads every reading and so carries the latest timestamp. From [asa-first-pass.el8](#asa). For position 0 whatever the condition, [first](#first).

*Lineage:* `asa` ("as soon as") is Lucid's; Wadge and Ashcroft (1985, p. 71) define `y asa q` as `first(y wvr q)`.

**Mistake programs:**

```el8
-- file asa-no-bound.el8
-- Mistake 1: a condition with no bound along t.
level = restrict 10 * #t to (t : 20) end
every_third = mod(#t, 3) == 0
picked = level asa.t every_third
-- expect compile error: asa.t searches along t, which has no end, and its condition reads no data along t; bound it: restrict … to (t : n) end
```

```el8
-- file asa-no-dimension.el8
-- Mistake 2: asa with no dimension.
reading = input("reading", t : 4)
ok = reading >= 20
first_ok = reading asa ok
-- expect compile error: asa works along a dimension, named after a dot: write reading asa.d ok, with d that dimension
```

## upon {#upon}

*Why:* one stream consumed at the pace of another: the task worked on, moving to the next task after each tick where the current one finished.

**Does:** `y upon.d q` at d = c is `y` at the number of positions before c along `d` where `q` is true.

**Form:** `y upon.d q`, where `d` is a dimension of whole numbers, `y` an expression and `q` a condition.

**Example:**

```el8
-- file upon-tasks.el8
-- The task worked on at each tick: the next one after each tick where the task finished.
task = input("task", t : 8)
finished = input("finished", t : 8)
current = task upon.t finished
done = 0 fby.t done + (if finished then 1 else 0)
by_hand = task[t = done]
-- table task = t,value; 0,101; 1,102; 2,103; 3,104; 4,105; 5,106; 6,107; 7,108
-- table finished = t,value,ts; 0,false,1000; 1,true,2000; 2,false,3000; 3,true,4000; 4,true,5000; 5,false,6000; 6,true,7000; 7,false,8000
-- expect current[t = 0] = 101
-- expect current[t = 1] = 101, at time 1000 ms
-- expect current[t = 2] = 102, at time 2000 ms
-- expect current[t = 4] = 103, at time 4000 ms
-- expect current[t = 5] = 104, at time 5000 ms
-- expect current[t = 8] = 105, at time 8000 ms
-- expect current[t = 9] = eod, at time 8000 ms
-- expect by_hand[t = 5] = 104, at time 5000 ms
```

**Answers:**

| t | `finished` | true positions before t | `current` |
|---|---|---|---|
| 0 | false | 0 | 101 |
| 1 | true | 0 | 101 (at 1000 ms) |
| 2 | false | 1 | 102 (at 2000 ms) |
| 4 | true | 2 | 103 (at 4000 ms) |
| 5 | false | 3 | 104 (at 5000 ms); `by_hand` the same |
| 8 | eod: past the bound | 4 | 105 (at 8000 ms) |
| 9 | eod | needs `finished` at 8 | eod (at 8000 ms) |

**Rules:**

1. At d = c, the walk ([The walk of wvr, asa and upon](#walk)) examines positions 0 to c − 1 and counts the true positions, m; the result is `y` at d = m, every other coordinate unchanged. At c = 0 nothing is examined, and the result is `y` at 0.
2. So `y` moves on one position after each position where `q` is true, and stays where it is after each where `q` is false.
3. If the walk would examine a position past `q`'s bound, the result is eod (`current` at t = 9 needs `finished` at 8).
4. `upon` needs no bound on `q`: it examines only positions before c.
5. The result varies along `d`.
6. eod, missing data and errors in `q`: [The walk](#walk), rules 3 to 5. In `y`: the outcome of `y` at m, unchanged.
7. The timestamp: [How operators combine them](https://elucid8-lang.org/learn/timestamps.md#how-operators-combine-them).

**Mistakes:**

1. *A condition that is not a boolean.* `moved = task upon.t task`: no compile message; from t = 1 the answer is `Error: upon needs a boolean condition`. Fix: a condition, `task upon.t finished`.
2. *No dimension.* `current = task upon finished` gives `upon works along a dimension, named after a dot: write task upon.d finished, with d that dimension`. Fix: `task upon.t finished`.

Each wrong line above is a fragment of a program under **Mistake programs**.

**Neighbours:** the same count written by hand is `done = 0 fby.t done + (if finished then 1 else 0)`, read by `task[t = done]` (`by_hand`). From [upon-tasks.el8](#upon). To skip `y`'s positions where `q` is false rather than wait at them, [wvr](#wvr).

*Lineage:* `upon` is Lucid's (Wadge and Ashcroft, 1985).

**Mistake programs:**

```el8
-- file upon-not-boolean.el8
-- Mistake 1: a condition that is a number.
task = input("task", t : 4)
moved = task upon.t task
-- table task = t,value; 0,101; 1,102; 2,103; 3,104
-- expect moved[t = 0] = 101
-- expect moved[t = 1] = error type: upon needs a boolean condition
```

```el8
-- file upon-no-dimension.el8
-- Mistake 2: upon with no dimension.
task = input("task", t : 4)
finished = input("finished", t : 4)
current = task upon finished
-- expect compile error: upon works along a dimension, named after a dot: write task upon.d finished, with d that dimension
```
