Learn

Contents

eLucid8 tutorial

The language as it stands in October 2026, in its notation v0.2: values over contexts, restrictions, streams, functions, aggregates, timestamps and alignment by time, inputs, outputs and data answered on demand. Every example below is checked by a test and runs as written in the playground.

eLucid8 is a Lucid-family language. A program does not run from top to bottom: it defines values over contexts, and you ask for one value at one context. The runtime then computes only what that answer depends on: this is demand-driven evaluation, or eduction.

This is intensional programming, "in which the meaning of an expression depends on the context in which it is being evaluated" (J. Plaice and J. Paquet, "Introduction to Intensional Programming", in M. A. Orgun and E. A. Ashcroft (eds.), Intensional Programming I, World Scientific, 1996, p. 1). A value is what that tradition calls an intension, "an indexed family of data values": one result at each context (W. Wadge and A. Yoder, "The Possible-World Wide Web", in the same book, p. 207).

Every program here is in the language's notation, v0.2.

How to read the examples

Each example is a complete program. Its comment lines (--) also say what data it uses and what it gives:

To try one in the playground: paste the program, set each input to "formula" or "table" with the given text (one line per row) and press Update: the question shown is answered by itself. To ask for another value or point, choose it and press Run query. Press Why? beside the answer to see what it depended on. The playground shows every answer's timestamp after it ("at time 0" when nothing it read carries one).

1. Values over contexts

A dimension is a named axis, such as t; it exists where a program uses it. Its coordinates are whole numbers 0, 1, 2, …, or texts such as "north" (section 17); each dimension's coordinates are all of one kind. A context gives each dimension a coordinate. A value is defined by an equation, and has a result at every context.

-- A first program: one dimension, one input, one value.
x = input("x", t : 10)
twice = 2 * x
-- data x = #t + 10
-- expect x[t = 0] = 10
-- expect twice[t = 3] = 26

Open in playground

input("x", t : 10) is data from outside the program, an input, read from the channel "x": where that data comes from (a table, a file, a database, a program answering on demand) is set in the deployment, never in the program (Where data comes from and where it goes says more). An input names its dimensions, each with its bound: here t, from 0 to 9; it is eod (end of data: there is no data there) elsewhere. Read twice = 2 * x as "at every t, twice is twice x at the same t". Inside an equation, a name such as x means that value at the current context.

An input that names no dimension is one value: rate = input("rate").

-- An input with no dimensions: one value.
rate = input("rate")
x = input("x", t : 10)
cost = rate * x
-- data rate = 3
-- data x = #t
-- expect rate = 3
-- expect cost[t = 4] = 12

Open in playground

#t is the current coordinate on dimension t; asking for it is querying the context (section 2):

-- The current coordinate, #t.
square = restrict #t * #t to (t : 100) end
-- expect square[t = 7] = 49
-- expect square[t = 100] = eod

Open in playground

Every value is bounded, along every dimension it has: by a restriction of its own, through the values it reads (twice is bounded by x's bound), or by none: a dimension with no bound written has no end. n below counts for ever, and restrict 0 fby.t n + 1 to (t : inf) end is the same value:

-- A value with no bound written: no end along t.
n = 0 fby.t n + 1
-- expect n[t = 50] = 50

Open in playground

A value's dimensions are never declared: they are those its equation depends on. #t depends on t, a reference to x depends on x's dimensions, an input has the dimensions it names, and so on:

-- Dimensions come from the equations.
reading = input("reading", t : 10)
twice = 2 * reading
distance = restrict #x + #y to (x : 10, y : 10) end
twelve = 3 * 4
-- data reading = #t + 10
-- expect twice[t = 3] = 26
-- expect distance[x = 3, y = 4] = 7
-- expect twelve = 12

Open in playground

Here twice is over t, distance over x and y, and twelve over no dimension at all.

Restrictions

restrict e to (d : n, …) end is e where every named coordinate #d is below its n, and eod elsewhere; outside the restriction nothing more is computed. A restriction may name several dimensions. Here w exists only for z from 0 to 5, and v reads it at z = x + y, so v is eod wherever the sum leaves that region:

-- A value that exists only for z from 0 to 5.
w = restrict 10 * #z to (z : 6) end
v = restrict w[z = #x + #y] to (x : 10, y : 10) end
-- expect w[z = 3] = 30
-- expect w[z = 7] = eod
-- expect v[x = 2, y = 3] = 50
-- expect v[x = 4, y = 3] = eod

Open in playground

A restriction's region is written as an aggregate's is (section 6): a size t : 10 (0 to 9), t : inf for a stream with no end (section 9), a range t : 3 .. 9, t : 3 .. inf, or a list t : {1, 3, 7}. The ends of a range may use the current coordinates, as everywhere:

-- A restriction over a range: eod outside 3 to 9.
x = input("x", t : 20)
middle = restrict x to (t : 3 .. 9) end
-- data x = #t
-- expect middle[t = 3] = 3
-- expect middle[t = 2] = eod
-- expect middle[t = 10] = eod

Open in playground

A restriction can only name dimensions its expression has. 1 has no dimension t, so restrict 1 to (t : 10) end is a compile error, and so is a restriction of an input over no dimension, s = input("s"), to (t : 10): an input names its dimensions itself, input("s", t : 10). A value that should exist over a range must depend on the dimension, for example restrict 1 fby.t 1 to (t : 10) end, which is 1 for t from 0 to 9 and eod after; a constant that needs no dimension is written without a bound, one = 1. Along a dimension of texts (section 17), fby and counts would make it a dimension of numbers, so the compiler suggests a test of the coordinate instead, if #zone == #zone then 1 else eod.

-- A restriction that names a dimension its expression does not have.
z = restrict 1 to (t : 10) end
-- expect compile error: this restriction names t, but 1 does not vary along t
-- A constant over a range depends on the dimension.
z = restrict 1 fby.t 1 to (t : 10) end
-- expect z[t = 9] = 1
-- expect z[t = 10] = eod

Open in playground

A region of another shape is written with a condition and eod:

-- A triangle: eod where j is above i.
tri = restrict if #j <= #i then #i + #j else eod
  to (i : 10, j : 10) end
-- expect tri[i = 3, j = 2] = 5
-- expect tri[i = 3, j = 4] = eod

Open in playground

A restriction leaves the dimensions it does not name open. restrict e to (t : 10) end bounds t and nothing else: along every other dimension of e it is e wherever e has data. Outside the restriction there is no data: eod, at time 0.

-- x stays open.
reading = input("reading", t : 10, x : 4)
v = restrict reading to (t : 10) end
-- data reading = #x
-- expect v[t = 2, x = 1] = 1
-- expect v[t = 12, x = 1] = eod

Open in playground

To draw a value exactly as wide as its data, restrict it to their span: the playground's queue example writes waiting = restrict length(line) to (m : 20) end, so its row ends with the 20 minutes of arrivals, where a value computed through fby, an if with a constant branch, a shift or a window could otherwise be drawn past its data, as eod.

A dimension named alone says the same thing out loud: restrict reading to (t : 10, x) end is v above. A dimension of texts is bounded by its list:

-- Bound depot by its list.
sales = input("sales", depot : {"north", "south"}, day : 4)
v = restrict sales * 2 to (day : 2, depot : {"north"}) end
-- data sales = #day + 1
-- expect v[depot = "north", day = 1] = 4
-- expect v[depot = "south", day = 1] = eod
-- expect v[depot = "north", day = 3] = eod

Open in playground

end closes a restriction, as it closes an aggregate: restrict a + b * 2 to (t : 10) end restricts the whole of a + b * 2, and a restriction written inside an expression needs no parentheses:

-- Precedence.
a = input("a", t : 20)
b = input("b", t : 20)
total = restrict a + b * 2 to (t : 10) end
part = restrict a to (t : 10) end + b * 2
-- data a = #t
-- data b = 1
-- expect total[t = 3] = 5
-- expect total[t = 12] = eod
-- expect part[t = 12] = eod
-- expect part[t = 3] = 5

Open in playground

2. Looking elsewhere: [d = e]

v[d = e] is v at another context: the current one with coordinate d replaced by e. The brackets follow what they move (v[t = 0], (a + b)[t = 0]; spaces never change the meaning, so v [t = 0] is the same); they move to another context and do not index an array.

#d and v[d = e] are the two operations every intensional language is built on. Lucid writes them # and @: "The # and @ operators are in fact the two fundamental operators of intensional programming. The first is used for querying and the second for navigating" (Plaice and Paquet, p. 6). #d queries the context; v[d = e] navigates to another one.

-- Reading a value at another context.
x = input("x", t : 10)
previous = x[t = #t - 1]
change = x - x[t = #t - 1]
first_one = x[t = 0]
-- data x = #t * #t
-- expect previous[t = 5] = 16
-- expect change[t = 5] = 9
-- expect first_one[t = 5] = 0
-- expect previous[t = 0] = eod

Open in playground

Coordinates are never below 0: reading a value at a coordinate below 0, as previous does at t = 0, gives eod.

Projection. A value can be used in a context with more dimensions than it has; the extra coordinates are simply ignored. Here offset depends on t only, but is used inside a value over t and x:

-- Projection: a value over t used inside a value over t and x.
reading = input("reading", t : 10, x : 10)
offset = input("offset", t : 10)
corrected = reading - offset
-- data reading = 100 * #t + #x
-- data offset = #t
-- expect corrected[t = 2, x = 5] = 203

Open in playground

Moving axes. The coordinates in […] may be any expressions of the current context, so […] can lay one dimension onto another: read a plane along its diagonal, or swap two axes. All of them are computed before any is set, so x = #y, y = #x swaps them at once:

-- The diagonal and the transpose.
grid = restrict #x * 10 + #y to (x : 10, y : 10) end
diagonal = restrict grid[x = #y] to (y : 10) end
transposed = restrict grid[x = #y, y = #x] to (x : 10, y : 10) end
-- expect diagonal[y = 4] = 44
-- expect grid[x = 2, y = 7] = 27
-- expect transposed[x = 2, y = 7] = 72

Open in playground

3. Results: values, eod and error

Every value at every context has exactly one result: ordinary data, eod (end of data: the point is outside the data, such as past the end of an input's data, outside a bound, below 0 or before fby's origin), or an error (something failed or is missing, and the error names where). A missing or faulty reading is never eod: it is an error that says which.

-- eod: an input with no data at some contexts.
x = input("x", t : 10)
plus_one = x + 1
-- data x = if #t == 2 then eod else #t
-- expect plus_one[t = 1] = 2
-- expect plus_one[t = 2] = eod

Open in playground

Operators are strict: an eod or error operand makes the result eod or an error. Division by zero is an error, and so is any result that is infinite or not a number:

-- Errors propagate, and name their origin and its context.
x = input("x", t : 10)
ratio = 10 / x
scaled = ratio * 2
-- data x = #t
-- expect ratio[t = 5] = 2
-- expect ratio[t = 0] = error division: division by zero
-- expect scaled[t = 0] = error division from ratio[t = 0]: division by zero

Open in playground

When an operator meets both, eod wins over an error: past the end of the data there is no answer to have failed, so the result is eod (with the latest timestamp of what it read). An error where there is data still passes on, and so does a gap in the data: missing data is an error (of kind missing, below), not eod.

-- eod wins over an error.
a = input("a", t : 10)
b = input("b", t : 10)
both = a + b
-- data a = if #t == 0 then eod else 2
-- data b = 1 / 0
-- expect both[t = 0] = eod
-- expect both[t = 1] = error division from b[t = 1]: division by zero

Open in playground

Testing for them. iseod(e) and iserror(e) are true or false, never eod or an error, so a program can decide what to do past the end of the data or at a failure. Here the data of x (over t : inf) end at its last row, t = 4, so x is eod from t = 5 on. At t = 2, inside the data, there is no row: that is missing data, an error of kind missing (below), not eod:

-- iseod and iserror.
x = input("x", t : inf)
ended = iseod(x)
failed = iserror(x)
filled = if iseod(x) then 0 else x
-- table x = t,value; 0,0; 1,1; 3,error: sensor fault; 4,4
-- expect ended[t = 5] = true
-- expect ended[t = 2] = false
-- expect failed[t = 2] = true
-- expect failed[t = 3] = true
-- expect filled[t = 5] = 0
-- expect filled[t = 4] = 4
-- expect filled[t = 2] = error missing from x[t = 2]: input x has no data at this point
-- expect filled[t = 3] = error data from x[t = 3]: sensor fault

Open in playground

iseod finds the end of the data, never a gap in it: to find or fill a gap, test the kind of error, as the next example does.

What kind of error. Every error has a kind: missing (no data where an input should have some, below), late (a data service answered after its deadline), failed (a data service or a connector failed), division (division by zero), non-finite (a result such as exp(1000) that is not a finite number), empty (the mean, min or max of no elements, section 6, or head or tail of the empty list), assert (a check that failed, section 19), type (a value of the wrong sort where it is used, such as 1 + "a", if 3 then …, or a coordinate that is not one), index (a list position outside the list, or a record without the field), data (an error: … cell in the data), raised (error("…") in the program) or version (a pinned version of an input that is no longer available). An error passed on keeps the kind it had where it arose. iserror(e, "missing") is true for an error of that kind only; the kind is written as text, and must be one of the twelve:

-- Telling missing readings from other errors.
reading = input("reading", t : 6)
gap = iserror(reading, "missing")
fixed = default(reading, 0, "missing") * 2
-- table reading = t,value; 0,5; 1,7; 3,error: sensor fault; 4,2
-- expect gap[t = 2] = true
-- expect gap[t = 3] = false
-- expect fixed[t = 2] = 0
-- expect fixed[t = 3] = error data from reading[t = 3]: sensor fault
-- expect fixed[t = 4] = 4

Open in playground

Missing data. Where an input should have data and has none (a point inside its bounds with no row, an empty cell, or a data service answering "nothing here"), the value is an error of kind missing. eod means something else: end of data. It is past the end of an input's data, outside a bound, below 0, before fby's origin, wherever a program writes eod, and where the data says eod. Data can say eod only where data can end: a table writes the word eod as the last row of a line along a dimension of whole numbers declared inf; inside a bound, before that last row, or along texts, the word is refused, with the file, the row and the column (files eLucid8 writes record eod in a column of their own, and read back as written anywhere). A missing reading is never eod. Operators are strict on missing data, as on any error, so every answer that needed the missing reading says so:

-- A gap is an error of kind missing; aggregates leave it out.
reading = input("reading", t : 5)
doubled = restrict reading * 2 to (t : 5) end
total = sum reading over (t : 5) end
counted = count reading over (t : 5) end
gap = sum reading over (t : 1 .. 2) end
none = sum reading over (t : 3 .. 2) end
-- table reading = t,value; 0,4; 1,; 3,6; 4,2
-- expect doubled[t = 0] = 8
-- expect doubled[t = 1] = error missing from reading[t = 1]: input reading has no data at this point
-- expect total = 12
-- expect counted = 3
-- expect gap = error missing from reading[t = 1]: input reading has no data at this point
-- expect none = 0

Open in playground

Every aggregate (over a region, a neighbourhood or a window) leaves missing points out, as total and counted do, and the answer says how many it left out: here "aggregates left out 1 missing point of input reading", as a warning beside the answer (in the playground, under it; from elucid8 run, on standard error and in its report). A value computed from a missing point alone, as doubled at t = 1, is that error. If every element an aggregate had was missing, as for gap (t = 1 has an empty value and t = 2 no row), the aggregate is missing too, naming the first of them; an aggregate with no elements at all, as none, gives its ordinary answer: 0 for sum and count. To fill a gap, default(x, 0, "missing") (section 16). A search, such as asof (section 9), never finds a point with no row of an input whose rows carry their own times: such a point has no time. Where no row means nothing happened, a deployment may give an input's missing points a value instead ("fill": {"missing": 0}, section 9).

Where an input's data end. Every dimension of an input says its extent: t : inf (the data decide where it ends), a count or range (t : 10, t : 0 .. 9), or a list (sensor : {"a", "b"}); a dimension named alone is refused, with these forms. An input's data end only along a dimension declared inf whose coordinates are whole numbers: past the last row along it, every point is eod: there is no more data yet. Along texts declared inf (region : inf), a key the data never mention is outside the data, so it is eod too: with rows for "north" and "south", sales[region = "west"] is eod, while a key the data mention keeps its gaps as missing data. Inside a finite bound or list (t : 5, sensor : {"a", "b"}) the program has said which points there are, so a point there with no row is missing data, never eod. So write inf for "as many as there are" (readings over time, a prompt's tokens, a customer's purchases), and a number only where the program means exactly that many: input("reading", t : 5) with rows at t = 0 to 2 has missing points at t = 3 and 4, and input("reading", t : inf) with the same rows is eod from t = 3 on. A sensor that missed the last tick still has a missing point there, since another sensor's row reaches it (the last row along t is the input's). An input with no rows at all is eod along each dimension with no end, and missing data inside finite ones. Where lines of the data end at different places, such as each account's own list of payments, the deployment says "ragged": true (section 12): each line (the input's other coordinates fixed) then ends at its own last row along t. A fill ("fill": {"missing": 0}) reaches only the points inside the data, never past their end.

-- The kind of an error passed on is its origin's.
x = restrict 6 / (#t - 2) to (t : 5) end
y = x + 1
by_zero = iserror(y, "division")
raised = iserror(y, "raised")
-- expect by_zero[t = 2] = true
-- expect raised[t = 2] = false
-- expect by_zero[t = 3] = false

Open in playground

Saying that data is invalid. error("message") is an error with that message, a text (section 17): a program can reject an input it cannot use. Like eod, the literal on its own has timestamp 0, so a result containing it has the timestamp of the data it was computed from:

-- An error literal.
reading = input("reading", t : 4)
checked = if reading > 100 then error("reading too high") else reading
doubled = checked * 2
-- table reading = t,value,ts; 0,20,1000; 1,150,2000; 2,30,3000; 3,40,4000
-- expect checked[t = 0] = 20, at time 1000 ms
-- expect checked[t = 1] = error raised: reading too high, at time 2000 ms
-- expect doubled[t = 1] = error raised from checked[t = 1]: reading too high, at time 2000 ms

Open in playground

4. Conditionals and logic

if c then a else b evaluates the condition, then only the branch taken. The other branch is never demanded, so an error there does no harm:

-- Only the branch taken is evaluated.
x = input("x", t : 10)
safe = if x == 0 then 0 else 10 / x
-- data x = #t
-- expect safe[t = 0] = 0
-- expect safe[t = 5] = 2

Open in playground

and and or short-circuit from left to right: false and X is false without evaluating X. Comparisons give true or false; not negates.

-- Short-circuit and/or, and not.
x = input("x", t : 10)
in_band = x > 0 and 100 / x > 10
small = x == 0 or 1 / x < 0.5
big = not (x > 3)
-- data x = #t
-- expect in_band[t = 0] = false
-- expect in_band[t = 5] = true
-- expect small[t = 0] = true
-- expect small[t = 4] = true
-- expect small[t = 1] = false
-- expect big[t = 5] = false

Open in playground

Data are numbers, booleans and texts; texts are compared only with == and != (section 17).

5. Streams: first, next and fby

Along a dimension, Lucid's stream operators relate positions to each other. Every dimension starts at 0, its origin:

Every rule of the stream operators, with their mistakes and messages, is in the Streams reference. If you know Lustre or Lucid, From Lustre to eLucid8 and From Lucid (pLucid) to eLucid8 put their programs and eLucid8's side by side.

-- first, next and fby.
x = input("x", t : 10)
start = first.t x
ahead = next.t x
delayed = 0 fby.t x
-- data x = 10 * #t
-- expect start[t = 4] = 0
-- expect ahead[t = 4] = 50
-- expect delayed[t = 0] = 0
-- expect delayed[t = 4] = 30

Open in playground

fby makes a value that refers to itself well founded: it reads itself one step back. A value defined this way is an iteration.

-- A counter and a running sum.
x = input("x", t : 10)
n = restrict 0 fby.t n + 1 to (t : 10) end
running = x fby.t running + next.t x
-- data x = 2 * #t + 1
-- expect n[t = 6] = 6
-- expect running[t = 0] = 1
-- expect running[t = 4] = 25

Open in playground

x fby.t running + next.t x means x fby.t (running + next.t x): fby binds more loosely than + (Along a dimension, rule 4). Read running as: at the origin, x; afterwards, the previous running plus the current x. (The odd numbers sum to squares.)

A value that refers to itself at the same context is refused before evaluation (fby, rule 4):

-- An unguarded cycle is rejected.
loop = restrict loop + #t to (t : 10) end
-- expect compile error: unguarded cycle

The fby trap. fby's right operand is read one step back, coordinates included (fby, rule 3). Factorial is 1 fby.n fac * (#n + 1), not fac * #n:

-- Factorial: #n is one step back too.
fac = restrict 1 fby.n fac * (#n + 1) to (n : 20) end
-- expect fac[n = 5] = 120

Open in playground

Changing a stream's rate: wvr, asa and upon

Three more of Lucid's operators read a stream at the positions where a condition q is true:

-- wvr, asa and upon: q is true at 0, 3, 6, …, 18.
y = restrict 10 * #t to (t : 20) end
q = restrict mod(#t, 3) == 0 to (t : 20) end
w = y wvr.t q
a = y asa.t q
u = y upon.t q
-- expect w[t = 1] = 30
-- expect w[t = 6] = 180
-- expect w[t = 7] = eod
-- expect a = 0
-- expect u[t = 2] = 10
-- expect u[t = 4] = 20
-- expect u[t = 20] = 70
-- expect u[t = 21] = eod

Open in playground

q is true seven times within its bound, so w at t = 7 is eod: there is no eighth time. u at t = 21 would need q at 20, past its bound: eod.

Each of the three finds its positions by a walk along t. A missing reading or an error in q that the walk meets is the result, and default(q, false, "missing") passes over a missing one; The walk of wvr, asa and upon gives every rule of the walk, its timestamps and the bound wvr and asa need:

-- 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

Open in playground

6. Regions and aggregates

A region is a set of positions, one constraint per dimension, written in parentheses: (t : 0 .. 9) is t from 0 to 9, (x : #x - 1 .. #x + 1, y : #y - 1 .. #y + 1) the 3×3 block around the current point. A constraint is one of:

constraintpositions
d : n0 to n − 1
d : a .. ba to b (none if b < a)
d : a .. infa and every position after it
d : infevery position from 0
d : {1, 3, 7}the coordinates listed, in the order written, each once
d alonein an aggregate, the coordinates the data has along d; in a restriction, every coordinate (d is left open)

The ends a and b, the count n and the positions listed can be any expressions of the current point, as in […]. The aggregates sum, mean, min, max and count take an expression over a region, closed by end: sum e over R end evaluates e at every position of the region (with the region's dimensions set there, the others as at the current point) and combines the results.

-- A 3×3 block, at the edges of a 10 × 10 grid.
grid = input("h", x : 10, y : 10)
smooth = mean grid over (x : #x - 1 .. #x + 1, y : #y - 1 .. #y + 1) end
cells = count grid over (x : #x - 1 .. #x + 1, y : #y - 1 .. #y + 1) end
highest = max grid over (x : #x - 1 .. #x + 1, y : #y - 1 .. #y + 1) end
-- data h = 10 * #x + #y
-- expect smooth[x = 5, y = 5] = 55
-- expect cells[x = 5, y = 5] = 9
-- expect cells[x = 0, y = 0] = 4
-- expect highest[x = 0, y = 0] = 11

Open in playground

Positions outside what is aggregated are left out. At the corner (0, 0) the block reaches x = −1 and y = −1: positions below 0, and positions past the bound of what is aggregated (here grid's 10 × 10), are not part of the aggregate: they are neither computed nor counted, so cells is 4 there and smooth divides by 4. Inside the bound every position counts, also one that is eod or an error.

eod wins; missing data is left out. An eod element (past the end of the data) makes the result eod, even beside an error; otherwise an error element makes it an error, except missing data. A missing element (a point inside the data with no row, or an empty cell) is left out, and the answer warns how many points it left out of each input. Write skip missing after the region, before end, to leave missing elements out without the warning, where the gaps are expected; write skip eod to leave eod elements out too (an error element still makes the result an error). Here the data of day end at its last row, t = 6, and t = 4, inside the data, has no row:

-- Missing data left out; eod past the end of the data.
day = input("r", t : inf)
mean3 = mean day over (t : #t - 1 .. #t + 1) end
quiet3 = mean day over (t : #t - 1 .. #t + 1) skip missing end
ahead3 = mean day over (t : #t - 1 .. #t + 1) skip eod end
-- table r = t,value; 0,0; 1,1; 2,2; 3,3; 5,5; 6,6
-- expect mean3[t = 4] = 4
-- expect quiet3[t = 4] = 4
-- expect mean3[t = 6] = eod
-- expect ahead3[t = 6] = 5.5
-- expect mean3[t = 0] = 0.5

Open in playground

mean3[t = 4] is the mean of 3 and 5, with the warning "aggregates left out 1 missing point of input r"; quiet3 gives the same 4 with no warning. At t = 6 the region reaches t = 7, past the end of the data, so mean3 is eod there; ahead3 leaves it out and gives the mean of 5 and 6.

A region is not a value. It is written only after over, in an aggregate or a restriction; its ends may be any expressions, computed at each point.

-- Regions written in place, with ends computed at each point.
x = input("x", t : 10)
moving = mean x over (t : #t - 2 .. #t) end
growing = if #t < 5 then sum x over (t : 0 .. #t) end else sum x over (t : #t - 4 .. #t) end
picked = sum x over (t : {1, 3, 3, 0}) end
-- data x = #t
-- expect moving[t = 5] = 4
-- expect growing[t = 3] = 6
-- expect growing[t = 9] = 35
-- expect picked = 4

Open in playground

(t : {1, 3, 3, 0}) lists 1, 3 and 0, each once, in the order written: 1 + 3 + 0.

A dimension named alone ranges over the data. sum sales over (region) end adds sales at every coordinate the data has along region: the coordinates the rows of the inputs sales reads list along it, each once, whole numbers in ascending order and then texts in the order of their characters. Where an input has no row at one of those coordinates (east on day 0 above), the point is missing data (section 3), which the aggregate leaves out. The same rule says which rows an output writes along a dimension the program does not bound (section 12).

-- A total over the regions the data has.
sales = input("sales", region : inf, day : 3)
total = sum sales over (region) end
best = argmax.region sales over (region) end
-- table sales = region,day,value; "north",0,3; "south",0,4; "north",1,5; "east",1,1
-- expect total[day = 0] = 7
-- expect total[day = 1] = 6
-- expect best[day = 1] = "north"

Open in playground

Every aggregate gives the same result under every cache, compiled loop and number of threads: sum and mean are exact (section 13), and the others do not depend on the order. An empty region gives 0 for sum and count, and for mean, min and max an error of kind empty ("mean of no elements"): there is no answer, as for 0 / 0.

An infinite region (inf, or a .. inf) is allowed only over something bounded along that dimension, so that the aggregate ends: sum x over (t : inf) end is the sum over x's bound. Over something with no bound there, the compiler says so.

7. Time and space together

Regions and fby combine into iterations over time and space. Heat diffusion: each step, every cell becomes the mean of the 3×3 block around it at the previous step.

-- Heat diffusion: a recurrence over time and a 2D grid.
initial = input("initial", x : 16, y : 16)
heat = restrict
  initial fby.time mean heat over (x : #x - 1 .. #x + 1, y : #y - 1 .. #y + 1) end
  to (time : 1000, x : 16, y : 16) end
-- data initial = if #x == 8 and #y == 8 then 100 else 0
-- expect heat[time = 0, x = 8, y = 8] = 100
-- expect heat[time = 1, x = 8, y = 8] = 11.1111
-- expect heat[time = 5, x = 8, y = 8] = 4.40482

Open in playground

heat is over time, x and y, from its bound, fby.time, the region and initial. The plate is 16 × 16: off the plate heat is eod, and the region leaves out the cells beyond the edges. A query for heat at step 5 evaluates only the 286 points that can affect it (its "light cone"), not the whole grid at every step.

Conway's Game of Life has the same shape:

-- The Game of Life, from a glider.
seed = input("seed", x : 10, y : 10)
alive = restrict seed fby.time (if n == 3 or (alive == 1 and n == 2) then 1 else 0)
  to (time : 100, x : 10, y : 10) end
n = sum alive over (x : #x - 1 .. #x + 1, y : #y - 1 .. #y + 1) end - alive
-- data seed = if (#x == 1 and #y == 0) or (#x == 2 and #y == 1) or (#y == 2 and #x <= 2) then 1 else 0
-- expect alive[time = 0, x = 1, y = 0] = 1
-- expect alive[time = 4, x = 1, y = 0] = 0
-- expect alive[time = 4, x = 2, y = 1] = 1
-- expect alive[time = 4, x = 3, y = 3] = 1

Open in playground

After four generations the glider has moved one cell diagonally.

8. Functions and where

A function applies to whole histories: its arguments are values, not single numbers. Functions are defined at the top level of a program.

-- A function over histories: the previous value.
x = input("x", t : 10)
fun prev(v) = v fby.t v
moving3 = (x + prev(x) + prev(prev(x))) / 3
-- data x = #t * #t
-- expect moving3[t = 4] = 9.66667

Open in playground

(prev(prev(x)) is two steps back; (x fby.t x) fby.t x would not be, because fby's right operand is read one step back, so it is x one step back again.)

where … end adds local definitions:

-- A function with local definitions: an exponential moving average.
x = input("x", t : 10)
fun ema(v, a) = s
  where
    s = v fby.t a * next.t v + (1 - a) * s
  end
smoothed = ema(x, 0.5)
-- data x = if #t == 0 then 0 else 8
-- expect smoothed[t = 0] = 0
-- expect smoothed[t = 1] = 4
-- expect smoothed[t = 3] = 7

Open in playground

A computation can walk a second dimension. Here acc walks z, a running total of x, and prefix reads it at z = t:

-- A second dimension: the prefix sum of x up to t.
x = input("x", t : 6)
acc = restrict x[t = 0] fby.z acc + x[t = #z + 1] to (z : 6) end
prefix = restrict acc[z = #t] to (t : 6) end
-- data x = #t + 1
-- expect prefix[t = 0] = 1
-- expect prefix[t = 4] = 15

Open in playground

A function can take a dimension. A dimension a function works along is a parameter, written after the function's name with a dot, and given the same way in a call:

-- A function along a dimension of the caller's choice.
x = input("x", t : 5)
fun before.d(v) = v[d = #d - 1]
prev = before.t(x)
-- data x = 10 * #t
-- expect prev[t = 3] = 20

Open in playground

A function may take several dimensions, after the one dot and separated by commas, in its definition and in each call:

-- A function along two dimensions of the caller's choice.
g = input("g", t : 4, x : 3)
fun shift.a,b(v) = v[a = #a - 1, b = #b + 1]
back = shift.t,x(g)
across = shift.x,t(g)
-- data g = 10 * #t + #x
-- expect back[t = 2, x = 1] = 12
-- expect across[t = 2, x = 1] = 30

Open in playground

shift.t,x(g) is g one step back along t and one on along x; shift.x,t(g) the other way round.

Each call is the function's body with its parameters replaced by the call's dimensions, so the call's dimensions must differ from each other and from every dimension the function's body names itself; otherwise two dimensions the function keeps apart would become one, and the call is refused:

-- Refused: k is the function's own dimension, so it cannot be a parameter's too.
v = input("v", k : 8)
fun shift_sum.a(x) = sum x[a = #a + #k] over (k : 0 .. 3) end
y = shift_sum.k(v)
-- expect compile error: shift_sum.k(…): k is already a dimension of shift_sum

Functions may recurse. A function may call itself, directly or through other functions. The values it uses inside need no bound of their own: the bound is the caller's.

-- A recursive function: factorial.
fun fac(k) = if k < 1 then 1 else k * fac(k - 1)
answer = restrict fac(#n) to (n : 10) end
-- expect answer[n = 0] = 1
-- expect answer[n = 5] = 120

Open in playground

-- Two functions that call each other.
fun even(k) = if k == 0 then 1 else odd(k - 1)
fun odd(k) = if k == 0 then 0 else even(k - 1)
answer = restrict even(#n) to (n : 10) end
-- expect answer[n = 6] = 1
-- expect answer[n = 7] = 0

Open in playground

Each call is computed afresh: a function that calls itself twice, as Fibonacci's does, repeats the work of every call below it. Over a dimension, each point is computed once and shared, so the same sequence costs one evaluation per number:

-- Fibonacci over a dimension: each point once.
fib = restrict 0 fby.n (1 fby.n (fib + next.n fib)) to (n : 100) end
-- expect fib[n = 10] = 55

Open in playground

Long histories are written with fby. A recursive function can walk a history, shifting its call one step back each time, as fun runmax(v) = if #t == 0 then v else max(v, (runmax(v))[t = #t - 1]) does. A parameter it passes on unchanged, as runmax passes v, is read where the first call gave it, so the work grows with the length of the history. A parameter moved at each call (runmax(v[t = #t - 1])) is read back through every call before it, so its work grows with the square of the length. Every call is also a level of nested demand, so a recursion deeper than the demand-depth limit stops with a program error. The same running maximum as a stream has neither cost:

-- A running maximum over a history, as a stream.
x = input("x", t : 10)
best = x fby.t max(best, next.t x)
-- data x = if #t == 3 then 9 else #t
-- expect best[t = 2] = 2
-- expect best[t = 5] = 9
-- expect best[t = 9] = 9

Open in playground

Programs over several files: in

A program can read the definitions of another file. Here is a file, stats.el8, that runs on its own:

-- file stats.el8: a running total and a ratio of the readings x
x = input("x", t : 6)
fun clamp(v, lo, hi) = if v < lo then lo else if v > hi then hi else v
running = x fby.t (running + next.t x)
ratio = running / x
output("running") = running
-- table x = t,value; 0,3; 1,1; 2,4; 3,0; 4,5; 5,9
-- expect running[t = 4] = 13
-- expect ratio[t = 3] = error division: division by zero
-- expect output "running" over t = 0..5

Open in playground

A program in the same folder names the file with a text, stats = "stats.el8", and reads its definitions with in: running in stats is running evaluated in that file, and (clamp in stats)(…) calls its function:

-- A program that reads stats.el8.
stats = "stats.el8"
x = input("x", t : 6)
capped = (clamp in stats)(running in stats, 0, 10)
share = x / (running in stats)
ratio = ratio in stats
-- table x = t,value; 0,3; 1,1; 2,4; 3,0; 4,5; 5,9
-- expect capped[t = 2] = 8
-- expect capped[t = 4] = 10
-- expect share[t = 4] = 0.384615
-- expect ratio[t = 3] = error division from (ratio in "stats.el8")[t = 3]: division by zero

Before in, any expression. N in S is the expression N evaluated in the file S names, at the same point: every name in N is that file's. So running / x in stats is stats.el8's running divided by stats.el8's x, and scale * (running in stats) mixes the program's own scale with the file's running.

in binds more loosely than every other operator, on both sides: everything before it in the expression it ends (the whole right-hand side of the equation, or what is inside the parentheses, brackets or argument it is written in) is N, and everything after it is the file's name. running + 1 in stats is (running + 1) in stats, the file's running plus 1; to add 1 in the program, write (running in stats) + 1. A point or a call of another file's value is written around parentheses: (running in stats)[t = 4], (clamp in stats)(…). The file may also be written in place, running in "stats.el8", or joined from texts with +, running in lib + "stats.el8" with lib = "lib/": after in, and only there, + joins texts. It is always a constant: a name after in must be defined by a text, never by a value that varies.

What a program over several files means is simple: it is the program you would get by copying each file's definitions in, each named after its file: (running in "stats.el8"), (ratio in "stats.el8"). The answers are that program's: the same values, eod, errors and timestamps, and an error names the values it passed through by those names: ratio at t = 3 is Error via (ratio in "stats.el8")[t = 3]: division by zero. A query can ask for another file's value the same way: (running in stats)[t = 4], or (running in "stats.el8")[t = 4].

The expression before in may be as large as you like. Here pct is computed wholly in stats.el8, from its running and its x; mixed multiplies the program's scale by the file's ratio:

-- An expression evaluated in another file.
stats = "stats.el8"
x = input("x", t : 6)
scale = 100
pct = running / x * 100 in stats
mixed = scale * (ratio in stats)
-- table x = t,value; 0,3; 1,1; 2,4; 3,0; 4,5; 5,9
-- expect pct[t = 4] = 260
-- expect mixed[t = 4] = 260

Each refusal names the file and line, and how the file was reached. The likeliest slip is to forget how loosely in binds; the message says what to write:

-- A file defines only its own names: in took the program's limit into stats.el8.
stats = "stats.el8"
limit = 10
capped = (clamp in stats)(running in stats, 0, limit)
oops = limit + running in stats
-- expect compile error: limit is not defined in stats.el8 (in evaluates all of limit + running there): to use the program's limit, write limit + (running in stats)

Where both files define the name, the program compiles and reads the file's, as the rule says, and the compiler warns, once per name in each in, with where each definition is and what to write for the program's own:

-- A name both files define: in reads the file's, and the compiler says so.
stats = "stats.el8"
x = input("x", t : 6)
running = x * 2
both = running + ratio in stats
-- table x = t,value; 0,3; 1,1; 2,4; 3,0; 4,5; 5,9
-- expect both[t = 4] = 15.6
-- expect warning: in running + ratio in stats it is stats.el8's running; write running + (ratio in stats) for this file's

The warning reads running is defined here (main.el8:4) and in stats.el8 (stats.el8:4); in running + ratio in stats it is stats.el8's running; write running + (ratio in stats) for this file's. It covers every name in the expression before in, a filter's or an aggregate's condition included; a name the expression binds itself (the r of those r from L if … end) is its own, and gives none. A parameter or a local value of the equation is not seen before in, so one the file defines too warns. There is no warning where the expression before in is a name alone (running in stats) or written wholly in parentheses ((running + ratio) in stats), nor for an input both files declare, which is one input.

9. Timestamps, and combining streams by time

Every value has a timestamp: a time in milliseconds. A value read from data has the time its data give (in the playground, a table's ts column; 0 when the data have none). A computed value has the latest timestamp of everything it read. time(e) is e's timestamp, as a number; where e is eod, time(e) is eod too.

-- Every value carries the time of the latest data it read.
x = input("x", t : 6)
running = x fby.t running + next.t x
delta = next.t x - x
n = restrict 0 fby.t n + 1 to (t : 6) end
when = time(running)
-- table x = t,value,ts; 0,3,1000; 1,1,2000; 2,4,3000; 3,0,4000; 4,5,5000; 5,9,6000
-- expect running[t = 4] = 13, at time 5000 ms
-- expect delta[t = 4] = 4, at time 6000 ms
-- expect n[t = 4] = 4
-- expect when[t = 4] = 5000, at time 5000 ms

Open in playground

running at t = 4 read x up to t = 4, so its timestamp is 5000; delta read x at t = 5; n reads no data, so its timestamp is 0. A conditional counts its condition and the branch it took, and an aggregate every element it combined. Values and timestamps gives every rule, and what timestamps are good for.

Two streams at different rates meet through their timestamps. Three operators look along a dimension d of v for data by its timestamp. Each is closed by end; T and the other parts can be any expressions:

within M and skip eod also work on asof. Durations can be written with a unit: 250ms, 30s, 5min, 2h, 1d, 1w; window.c 5min at T is the same as window.c 300000 at T. A moment is written utc("2026-10-18T12:00:00Z"), in milliseconds since 1970-01-01T00:00Z. There is no now: a program knows the time from a clock input, whose rows are a clock's ticks (Data that ticks: a clock), so time(clock) - time(order) is how long ago an order was placed.

-- Two streams, combined by their timestamps.
reading = input("reading", s : 4)
factor = input("factor", c : inf)
asof_value = reading * asof.c factor at time(reading) end
held_value = reading * hold.c factor at time(reading) within 100 end
held_longer = reading * hold.c factor at time(reading) within 200 end
windowed = mean factor over (c : window.c 250 at time(reading)) end
windowed_skip = mean factor over (c : window.c 250 at time(reading)) skip eod end
-- table reading = s,value,ts; 0,10,0; 1,10,100; 2,10,200; 3,10,300
-- table factor = c,value,ts; 0,1,0; 1,2,150; 2,eod,280
-- expect asof_value[s = 2] = 20, at time 200 ms
-- expect asof_value[s = 3] = eod, at time 300 ms
-- expect held_value[s = 3] = eod, at time 300 ms
-- expect held_longer[s = 3] = 20, at time 300 ms
-- expect windowed[s = 3] = eod, at time 300 ms
-- expect windowed_skip[s = 3] = 2, at time 300 ms

Open in playground

The factors end with an eod row at 280 ms (a table may write eod only as the last row of a line along a dimension declared inf, here c). At reading s = 3 (300 ms) the latest factor is that eod, so asof gives eod. hold steps back to the factor at 150 ms; that is 150 ms old, more than 100 ms but within 200. window takes the factors in (50, 300] ms: the 2 at 150 ms and the eod at 280 ms, so its mean is eod; with skip eod it leaves the eod out, and the mean is 2.

A reading too old: stale. within M says how old the data may be. When the newest reading at or before T is M milliseconds or more older than T, it is stale: hold gives missing data, an error of kind missing, never the old value. Its timestamp is that last reading's, so the answer also says how old the last good reading is. Before the first reading there is nothing to be stale, and hold gives eod. A heater that must never act on an old reading:

-- A heater reads a room thermometer that reports now and then.
-- It must never act on an old reading: one older than 2 seconds is stale.

temp = input("temp", r : inf)      -- the readings, in order; each row's ts is when it was taken

-- Every half second (step k is k × 500 ms), the current temperature,
-- taken only from a reading made within the last 2 seconds.
current = restrict hold.r temp at #k * 500 within 2000 end to (k : 25) end

-- Heat when cold. When the reading is stale there is no current temperature,
-- so the heater's state is missing too, never a guess from an old reading.
heat = if current < 20 then 1 else 0
quiet = restrict iserror(current, "missing") to (k : 25) end   -- the thermometer went quiet

-- table temp = r,value,ts; 0,19.5,500; 1,19.8,1500; 2,20.4,2500; 3,21.0,9500; 4,20.6,10500
-- expect current[k = 4] = 19.8, at time 1500 ms
-- expect heat[k = 4] = 1, at time 1500 ms
-- expect current[k = 6] = 20.4, at time 2500 ms
-- expect heat[k = 6] = 0, at time 2500 ms
-- expect current[k = 10] = error missing: hold: no data within 2000 ms before 5000, at time 2500 ms
-- expect heat[k = 10] = error missing from current[k = 10]: hold: no data within 2000 ms before 5000, at time 2500 ms
-- expect quiet[k = 10] = true, at time 2500 ms
-- expect current[k = 20] = 21, at time 9500 ms
-- expect current[k = 0] = eod

Open in playground

The thermometer reports at 2500 ms and then not until 9500 ms. At step 10 (5000 ms) the newest reading is 2.5 seconds old, so current is missing data stamped 2500 ms, heat passes that on, and quiet is true: iserror(current, "missing") is the signal to act on. From step 19 (9500 ms) the readings are fresh again. At step 0 (0 ms) no reading has been taken yet: eod. Change within 2000 to within 8000 and the gap disappears: current holds the reading of 2500 ms through it. The playground's example "A heater that never acts on a stale reading" draws current, heat and quiet along k.

Two more operators give the position itself, as a number:

asof.d v at T end is v at the position that has the latest timestamp, which is latest's position when timestamps do not decrease along d.

-- Positions found by timestamp.
reading = input("reading", s : 4)
before_250 = latest.s reading at 250 end
after_250 = after.s reading at 250 end
-- table reading = s,value,ts; 0,10,0; 1,10,100; 2,10,200; 3,10,300
-- expect before_250 = 2, at time 200 ms
-- expect after_250 = 3, at time 300 ms

Open in playground

Sparse data: a missing row that reads 0. In some data a missing row means nothing happened: an hour with no purchase spent 0. What a missing row reads is the deployment's to say, not the program's: "fill": {"missing": 0} in the input's entry in deploy.json (in the playground, on its card; in the examples here, a line -- fill purchases = 0) makes a point with no row inside the data read as 0 (with timestamp 0) instead of missing data, and an empty cell read as 0 at its row's time. A row whose value is eod is still eod, and past the last row along a dimension declared inf a point is eod whatever the fill says. Those zeros are data, at time 0, so asof, hold and window take them as candidates too: before the first purchase, asof finds a zero, not eod:

-- Purchases by hour; an hour with no purchase has no row.
purchases = input("purchases", hour : 6)
spent = sum purchases over (hour : #hour - 2 .. #hour) end
last = restrict asof.hour purchases at #h * 1000 end * 1.2
  to (h : 6) end
cost = purchases * 1.2
same = restrict asof.hour cost at #h * 1000 end to (h : 6) end
bought = input("bought", hour : 6)
found = restrict asof.hour bought at #h * 1000 end to (h : 6) end
-- table purchases = hour,value,ts; 1,10,1000; 3,4,3000
-- fill purchases = 0
-- table bought = hour,value,ts; 1,10,1000; 3,4,3000
-- expect purchases[hour = 2] = 0
-- expect spent[hour = 2] = 10, at time 1000 ms
-- expect spent[hour = 0] = 0
-- expect last[h = 5] = 4.8, at time 3000 ms
-- expect last[h = 0] = 0
-- expect same[h = 0] = 0
-- expect same[h = 2] = 12, at time 1000 ms
-- expect found[h = 0] = eod
-- expect found[h = 2] = 10, at time 1000 ms

Open in playground

spent at hour 0 sums hours −2 to 0: positions below 0 are left out, so it is the 0 of hour 0. last at h = 0 (0 ms) finds the zeros of the hours with no purchase, all at time 0, and takes the latest position among them. Searching the input and then calculating (last) or calculating and then searching (same) give the same answers. A search finds those zeros because the fill makes them values, the same as zeros the data list: they are not eod, so skip eod does not leave them out either. Where a search should find only the hours that had a purchase, leave "fill" out, as bought does: a point with no row is then missing data (section 3), which has no time, so a search never finds it, and found at h = 0 is eod. (A row that is there with an empty value is found, at its row's time, as missing data; a search that says skip missing leaves it out.)

A stream with no end. A sensor that reports for as long as it runs, or a training run of any length, has no last position: its bound is inf, as in input("reading", tick : inf). asof, hold and window along such a dimension look at the data so far: over the input, up to its last row along it; over a value computed from it, as far as the data it is computed from has reached, line by line: at given coordinates, up to the last position at which every line of an input it reads (at the same other coordinates, such as one sensor) has a row, so past the data its eods are not candidates. A sensor that missed the latest reading stops at its own last row; the others are unaffected. New data makes a new version, and the answers follow it.

-- A stream with no end, and the latest running total.
reading = input("reading", tick : inf)
total = reading fby.tick total + next.tick reading
last_reading = asof.tick reading at 100000 end
latest_total = asof.tick total at 100000 end
-- table reading = tick,value,ts; 0,5,1000; 1,7,2000; 2,4,3000
-- expect total[tick = 2] = 16, at time 3000 ms
-- expect total[tick = 3] = eod, at time 3000 ms
-- expect last_reading = 4, at time 3000 ms
-- expect latest_total = 16, at time 3000 ms

Open in playground

total at tick 2 is the sum of the readings at ticks 0 to 2. Past the last row along tick it is eod, carrying the last timestamp, and the search does not look there, so latest_total is the last real total. A query or a view names a finite part of such a dimension: the playground shows it up to the last row, and the elucid8 command takes last for the last position (tick = last - 9..last).

10. Data answered on demand

A data source is data that already exists, which the program reads: a table, a file, a database. Some data is made on request instead: at each point an outside program (an AI model, a web API, a simulator) is asked, and its answer is data stamped with the time it arrived. In a program the two are the same thing, an input, x = input("x", t : 10): which one it is, and where the data comes from, is set in the deployment (in the playground, on the input's card), never in the program. Where data comes from and where it goes says which to use when, and where results go.

An input answered on demand is asked at a point, as any value is read: sum_service = input("sum_service", n : inf) is over a dimension n, and score = sum_service[n = x] asks it, at each t, for its answer at n = x. The coordinates of the point asked are the question's inputs. A question is asked only where an answer is wanted, at most once a point in a query, never speculatively, and it is recorded in the trace. An eod or error coordinate (x eod here) means no question, and that eod or error is the answer.

The deadline (how long an answer may take), which failures to try again and whether an answer may be reused by later queries are not part of the program either: they are the input's settings, on its card in the playground (and in deploy.json in a deployment, Command reference). In the examples below, a line -- service sum_service: deadline 10 ms gives them.

The playground supplies a stand-in, sum_service, which answers with the sum of the coordinates it is asked at, after the latency set in the playground; the Services reference describes it. A question starts at the latest timestamp of the coordinates it is asked with (0 ms here: the formula's data carry none), and the deadline counts from there. Its answer is stamped with the time it completed, so it is later than the data it was computed from. A simulated program stands in for any other: it answers a sum, a score or a formula, optionally with a delay and a failure rate, and a line -- service quick_sum: simulated sum; takes 5 ms makes one (in the playground, on the input's card).

-- An input answered on demand, with a deadline.
x = input("x", t : 10)
sum_service = input("sum_service", n : inf)
score = sum_service[n = x]
alarm = score > 20
-- latency 5
-- service sum_service: deadline 10 ms; reuse successes
-- data x = 7 * #t
-- expect score[t = 4] = 28, at time 5 ms
-- expect alarm[t = 4] = true, at time 5 ms
-- expect alarm[t = 2] = false, at time 5 ms

Open in playground

If an answer comes after the deadline, the result is a late error, never ordinary data, and it propagates like any other error:

-- A late answer.
x = input("x", t : 10)
sum_service = input("sum_service", n : inf)
score = sum_service[n = x]
alarm = score > 20
-- latency 5
-- service sum_service: deadline 3 ms
-- data x = 7 * #t
-- expect alarm[t = 4] = error late from sum_service[n = 28]: late (5 ms > deadline 3 ms), at time 3 ms

Open in playground

From 0.22.0, a program may also write a channel it reads: here the output x writes each reading doubled. So that it never replaces the data read, that output's file is x-output.csv by default, and a to naming the input's own file is refused:

-- An output that writes a channel the program reads.
x = input("x", t : 10)
twice = 2 * x
output("x") = twice
-- data x = #t
-- expect twice[t = 3] = 6
-- expect output "x" over t = 0..9

Open in playground

Two uses of this. Filling gaps: a file of readings has no row at t = 2 and an empty value at t = 4, both missing data (section 3); each missing reading is filled with the one before it, and written back under the same channel. The restrict makes the output write every t from 0 to 5: without it, the output writes the rows the file lists, and t = 2 would be left out. Copy readings-output.csv over readings.csv, and the next run reads it as its input with no configuration:

-- Fill each missing reading with the one before it.
readings = input("readings", t : 6)
filled = readings fby.t default(next.t readings, filled, "missing")
output("readings") = restrict filled to (t : 6) end
-- table readings = t,value; 0,12.5; 1,13.0; 3,14.2; 4,; 5,15.1
-- expect filled[t = 2] = 13
-- expect filled[t = 4] = 14.2
-- expect output "readings" over t = 0..5

Open in playground

A ledger: today's balances are yesterday's plus today's payments. Each day, mv balances-output.csv balances.csv, put the day's payments in payments.csv, and run the program again: the balances carry over from one day to the next, and an account with no payments keeps its balance. Each account has as many payments as it has (n : inf), and its list ends at its own last one: the deployment says "ragged": true for payments (here, the line -- ragged payments), so past an account's last payment its points are eod, which skip eod leaves out, never missing data. The region n : 10 takes up to ten payments an account a day.

-- Today's balance: yesterday's plus today's payments.
balances = input("balances", account : inf)
payments = input("payments", account : inf, n : inf)
paid = sum payments over (n : 10) skip eod end
today = balances + paid
output("balances") = today
-- table balances = account,value; "ann",100; "bob",250; "cy",0
-- table payments = account,n,value; "ann",0,20; "ann",1,-5; "bob",0,-50; "cy",0,10; "cy",1,10; "cy",2,10
-- ragged payments
-- expect today[account = "ann"] = 115
-- expect today[account = "bob"] = 200
-- expect today[account = "cy"] = 30
-- expect output "balances" over account = *

Open in playground

The mv is the one step done by hand: it is how yesterday's output becomes today's input, without eLucid8 ever writing over a file it reads.

An echo: a table with a column per field, a,b,c, read as one input over its rows and its columns, and written back in the same shape. The program is two lines, and the file comes back as it was read, byte for byte:

-- Read a table and write it back as it was.
data = input("data", row : inf, col : inf)
output("data") = data
-- table data = row,col,value; 0,"a",1; 0,"b",2; 0,"c",3; 1,"a",4; 1,"b",5; 1,"c",6
-- expect data[row = 1, col = "b"] = 5
-- expect output "data" over row = *, col = *

Open in playground

Its shape on disk, a column per coordinate of col, is the deployment's: "wide": "col" on the input and on the output. Section 12 runs it, with the file and deploy.json (An output written wide).

11. What the compiler checks

Before anything runs, the compiler warns of reads of a dimension the context does not supply (section 1): those reads are eod.

It rejects:

x = restrict #t * 10 to (t : 4) end
y = restrict (x + 1)[s = 2] to (t : 4) end
-- expect compile error: [s = …] sets s on (x + 1), which does not vary along s

Inside a function, such a move depends on the argument, so it is a warning naming the call and the argument, and the move is left out: fun at2(a) = a[s = 2] called as at2(x) gives x.

It also warns of a local value in a where that nothing reads, and of a region whose range starts below 0 (positions below 0 are always left out).

Reserved names

Two kinds of words have a fixed meaning, so they cannot name your own values, functions, parameters, local values or the element of a those. A name has one meaning: exp = 5 is refused, so exp always means the library's exp.

wordswhat they are
true, false, eod, infliterals
if, then, else, where, end, fun, in, assert, input, output, along, aswords of forms (as is reserved for a later form)
fby, wvr, asa, upon, next, first, and, or, not, elt, isin, addto, xbyoperators
over, to, restrict, skip, within, at, those, fromwords of regions, restrictions, searches and filters
exp, log, sqrt, tanh, abs, min, max, floor, ceil, trunc, sign, pow, div, mod, round, sin, cos, atan2, tan, asin, acos, atan, log10, log2, log1p, expm1, cbrt, sinpi, cospi, tanpi, degrees, radians, hypot, bf16, fp16, fp32, fp8_e4m3, fp8_e5m2, pi, tau, e, phithe library: maths (section 15) and constants
sum, mean, count, approx_sum, approx_mean, variance, sample_variance, stddev, argmax, argmin, product, approx_product, list, collectthe library: aggregates, read by their form
iseod, iserror, error, time, utc, asof, hold, latest, after, windowthe library: outcomes, times and searches
head, tail, length, elements, enumeration, codes, from_codes, text, utc_textthe library: lists, records, sets and texts (sections 16, 17 and 18)
default, year, month, day_of_month, day_of_week, hour_of_day, start_of_day, start_of_month, tab_focus, follow, jointhe library written in eLucid8 (sections 16 and 17)
formatthe library: reserved for a function to come

The first four rows are the keywords, a closed list: a word joins it only when the language gains a form that uses it. The rest are the library's names, and the message suggests another name. A dimension or a record's field may still be called count or length, since #count and .length are never the library's; a keyword never names one.

-- A library name cannot be redefined.
x = input("x", t : 10)
count = sum x over (t : 0 .. 9) end
-- data x = #t
-- expect compile error: count is a library name; choose another name, such as n_count

12. Running programs

Outputs named in the program

A program can name what it gives out, as it names what it reads. An output is an equation output("channel") = e: it sends the value e out on a channel, one value per point.

-- A running sum and a ratio, written out together.
x = input("x", t : 6)
running = x fby.t (running + next.t x)
ratio = running / x
output("running") = running
output("ratio") = ratio
-- table x = t,value; 0,3; 1,1; 2,4; 3,0; 4,5; 5,9
-- expect output "running" over t = 0..5
-- expect output "ratio" over t = 0..5
-- expect running[t = 4] = 13
-- expect ratio[t = 3] = error division: division by zero

Open in playground

From the terminal: elucid8 run

The elucid8 command runs a program on your computer, with your files, exactly as the playground does (Deploy tutorial says how to install it). The programs in this section and the next need elucid8 0.22.0 or later. Section 10's gap filler and ledger are run this way: elucid8 run reads each input from the file named after its channel, and writes each output to a file.

A program that splits rows in two: the amounts of at least 40, and the others. wvr (wvr) keeps the amounts whose row passes, one after another, so passed at t = 0 is the first amount that passes, at t = 1 the second, and so on; there are seven, so passed is eod from t = 7:

split.el8:

-- Split amounts into those of at least 40 and the others.
amount = input("amount", t : 10)
pass = amount >= 40
passed = amount wvr.t pass
failed = amount wvr.t not pass
output("passed") = passed
output("failed") = failed
-- table amount = t,value; 0,42; 1,75; 2,18; 3,90; 4,55; 5,30; 6,66; 7,12; 8,81; 9,47
-- expect passed[t = 0] = 42
-- expect passed[t = 6] = 47
-- expect passed[t = 7] = eod
-- expect failed[t = 2] = 12
-- expect failed[t = 3] = eod
-- expect output "passed" over t = 0..9
-- expect output "failed" over t = 0..9

Open in playground

The data is beside it, in a file named after the input, so it needs no configuration:

amount.csv:

t,value
0,42
1,75
2,18
3,90
4,55
5,30
6,66
7,12
8,81
9,47

deploy.json says where each output goes: passed to a CSV file, failed to an Arrow file (any tool that reads Arrow reads it: pandas, Polars, DuckDB). Each output is written over t from 0 to 9, like its input, so without more it would end in rows of eod; "skip": "eod" leaves out the rows whose value is eod, so each file holds exactly its rows:

deploy.json:

{ "format": "elucid8-deploy/2",
  "outputs": { "passed": { "to": "passed.csv", "skip": "eod" },
               "failed": { "to": "failed.arrow", "skip": "eod" } } }

elucid8 run compiles the program, reads deploy.json beside it, and writes both files:

elucid8 run split.el8
cat passed.csv
t,passed_value,passed_kind,passed_message
0,42,value,
1,75,value,
2,90,value,
3,55,value,
4,66,value,
5,81,value,
6,47,value,

The column is named after its channel, passed_value, so another program's input named passed reads this file as it is. In Python, with pandas:

read_failed.py:

import pandas as pd

failed = pd.read_feather("failed.arrow")
print(failed[["t", "failed_value"]])
python3 read_failed.py
   t  failed_value
0  0          18.0
1  1          30.0
2  2          12.0

Change amount.csv and run again: the files are written afresh from the new data.

Leaving out eod records

An output is written over its rows, one record per point, and a point where its value is eod gives a record that says eod. That is often what you want: the file shows where there was no answer. When it is not, "skip": "eod" on the output's entry in deploy.json leaves those records out, as the split did. Unlike wvr, it renumbers nothing: each record keeps its own t.

Here high is a reading where it is above 30, and eod elsewhere:

high.el8:

-- The readings above 30, each at its own t.
reading = input("reading", t : 8)
high = if reading > 30 then reading else eod
output("high") = high
-- table reading = t,value; 0,12; 1,35; 2,8; 3,41; 4,20; 5,33; 6,7; 7,19
-- expect high[t = 1] = 35
-- expect high[t = 2] = eod
-- expect output "high" over t = 0..7

Open in playground

reading.csv:

t,value
0,12
1,35
2,8
3,41
4,20
5,33
6,7
7,19

With no deploy.json, elucid8 run writes high.csv over every t:

elucid8 run high.el8
cat high.csv
t,high_value,high_kind,high_message
0,,eod,
1,35,value,
2,,eod,
3,41,value,
4,,eod,
5,33,value,
6,,eod,
7,,eod,

With this configuration, skip.json, the records that are eod are left out:

skip.json:

{ "format": "elucid8-deploy/2",
  "outputs": { "high": { "skip": "eod" } } }
elucid8 run high.el8 --config skip.json
cat high.csv
t,high_value,high_kind,high_message
1,35,value,
3,41,value,
5,33,value,

An output written wide

Many tables have a column per field: a,b,c, a row per record. Section 10's echo reads such a table and writes it back. In the program the table is one input over two dimensions, its rows and its columns; on disk its shape is set in deploy.json, with "wide":

echo.el8:

-- Read a table and write it back as it was.
data = input("data", row : inf, col : inf)
output("data") = data
-- table data = row,col,value; 0,"a",1; 0,"b",2; 0,"c",3; 1,"a",4; 1,"b",5; 1,"c",6
-- expect data[row = 1, col = "b"] = 5
-- expect output "data" over row = *, col = *

Open in playground

data.csv:

a,b,c
1,2,3
4,5,6

deploy.json:

{ "format": "elucid8-deploy/2",
  "inputs":  { "data": { "from": "data.csv", "row": "row", "wide": "col" } },
  "outputs": { "data": { "to": "echo.csv", "row": "row", "wide": "col", "csvWords": true } } }
elucid8 run echo.el8
cat echo.csv
cmp data.csv echo.csv && echo "the same, byte for byte"
a,b,c
1,2,3
4,5,6
the same, byte for byte

Asked for a point, it gives the cell of that row and column:

elucid8 run echo.el8 --query 'data[row = 1, col = "b"]'
{"name":"data","context":{"row":1,"col":"b"},"outcome":{"kind":"value","value":5,"ts":0}}

Without "wide" on the output, the same channel is written long, a record per point, a column per dimension: col,row,data_value, then a,0,1 and so on. Written wide in CSV, each coordinate has its value column and, beside it, <coordinate>_kind and <coordinate>_message (an eod cell is empty, its kind eod), with "timestamps": true each cell's timestamp in a column <coordinate>_ts, and in Arrow and Parquet each coordinate has its _kind, _error and _ts columns, so a file written wide reads back as the points it was written from (Command reference).

A live stream: elucid8 serve and elucid8 repl

elucid8 run reads its data, writes its outputs and stops. elucid8 serve keeps a program running: data arrives while it runs, and you ask it for values whenever you like (The long-running service has every detail). Here readings arrive one a second, and a running total, a mean of the last three and an alert follow them:

stream.el8:

-- Readings arrive one at a time.
reading = input("reading", t : inf)
-- The running total of every reading so far.
total = reading fby.t (total + next.t reading)
-- The mean of the last three readings.
avg3 = mean reading over (t : #t - 2 .. #t) end
-- An alert when a reading is above 30.
alert = reading > 30
-- table reading = t,value; 0,12; 1,35; 2,8; 3,41; 4,20
-- expect total[t = 4] = 116
-- expect avg3[t = 0] = 12
-- expect avg3[t = 4] = 23
-- expect alert[t = 3] = true

Open in playground

reading has no end along t (inf): it holds what has arrived so far. The first reading is in the file named after it:

reading.csv:

t,value
0,12

Start the service in a terminal. It listens for requests on a port of this computer, and keeps running until you stop it with Ctrl-C:

elucid8 serve stream.el8 --http 127.0.0.1:8080

In a second terminal, start elucid8 repl, a prompt that asks the service for values, and watch the total. watch prints the answer, then a line each time a reading arrives and changes it; last is the last t there is:

elucid8/… at http://127.0.0.1:8080/: total, avg3, alert; input reading. Type help for commands, quit to leave.
elucid8> watch total[t = last]
t = 0: 12
(watching total[t = last]: each change is printed as data arrives; Ctrl-C to stop)
t = 1: 47
t = 2: 55
t = 3: 96
t = 4: 116
^C
(stopped watching)

The readings come from a third terminal. feed.sh sends four, a second apart, each as a row of CSV posted to the input reading:

feed.sh:

# Sends one reading a second, at t = 1, 2, 3, 4.
t=1
for v in 35 8 41 20; do
  printf '%s,%s\n' "$t" "$v" | curl -s -o /dev/null -H 'Content-Type: text/csv' \
    --data-binary @- http://127.0.0.1:8080/inputs/reading/rows
  echo "t = $t: reading $v"
  t=$((t + 1))
  sleep 1
done
sh feed.sh
t = 1: reading 35
t = 2: reading 8
t = 3: reading 41
t = 4: reading 20

As each line appears here, the total appears in the second terminal. Any program can send readings this way: a sensor's script, a Python program, another service. Once they have arrived, ask for any value at any point, at the same prompt:

elucid8> total[t = last]
116
elucid8> avg3[t = last]
23
elucid8> alert[t = 0..last]
t = 0: false
t = 1: true
t = 2: false
t = 3: true
t = 4: false
elucid8> spark total[t = 0..last]
▁▃▄▇█
t from 0 to 4; min 12, max 116
elucid8> quit

spark draws an answer in the terminal; show total[t = 0..last] opens it in the eLucid8 Player, in the browser, and show total[t = last] follows it there as readings arrive.

A person's moves can arrive the same way: a guess, a move in a game or a sale at a counter is a row sent to an input, and what they see is a value asked for. Interactive programs builds games and a shop counter this way, with no actions in the program.

feed.sh is the way for data you produce yourself, as it is produced. Data that already exists, or that only needs to tick, needs no script: the service gives the input its rows itself, over time. The next two parts show how.

Readings you already have: a replay

Stop the service with Ctrl-C. Readings that are already in a file can arrive as if they were live: a replay gives the service the file's rows one t at a time. In the same folder, put all five readings in history.csv, and say in deploy.json that the input reading is a replay of it, one t a second:

history.csv:

t,value
0,12
1,35
2,8
3,41
4,20

deploy.json:

{ "format": "elucid8-deploy/2",
  "inputs": { "reading": { "from": { "builtin": "replay", "file": "history.csv", "every": "1s" } } } }

elucid8 check says what the input will be given:

elucid8 check stream.el8
stream.el8: plan version 26, 1 input, 0 outputs
input "reading": history.csv replayed: one t's rows every 1000 ms, 5 coordinates in order; over t (whole numbers); a missing point is missing data (eod past the last row along t); elucid8 serve gives it its rows over time, elucid8 run cannot read it
ok
4 expectations hold (reading from the program's tables, the rest from deploy.json)

Start the service again:

elucid8 serve stream.el8 --http 127.0.0.1:8080

and watch the total in the second terminal, as before. With no script, the readings arrive a second apart:

elucid8> watch total[t = last]
t = 0: 12
(watching total[t = last]: each change is printed as data arrives; Ctrl-C to stop)
t = 1: 47
t = 2: 55
t = 3: 96
t = 4: 116
^C
(stopped watching)

Once the five have arrived, the replay has ended, and every value is there to ask for, at the same prompt:

elucid8> alert[t = 0..last]
t = 0: false
t = 1: true
t = 2: false
t = 3: true
t = 4: false
elucid8> quit

Data that ticks: a clock

Some programs need only time to pass: a sine wave, a schedule, a simulation that runs at its own pace. A clock gives an input a row a tick, at t = 0, 1, 2, …, its value and its timestamp the tick's UTC time in milliseconds. It is also how a program knows the time now: eLucid8 has no now, since a value that changed with the moment of asking would give two answers to one question. x has a value at every tick there is:

wave.el8:

-- A sine wave with a value at every tick of the clock.
clock = input("clock", t : inf)
x = if iseod(clock) then eod else sin(2 * pi * #t / 100)
-- data clock = 20 * #t
-- expect x[t = 25] = 1
-- expect x[t = 0] = 0

Open in playground

In its own folder, with this deploy.json: a tick every 20 ms, 50 ticks in all (until), the first at time 0 (start; without it, the time the service starts):

deploy.json:

{ "format": "elucid8-deploy/2",
  "inputs": { "clock": { "from": { "builtin": "clock", "every": "20ms", "until": 50, "start": 0 } } } }
elucid8 check wave.el8
wave.el8: plan version 26, 1 input, 0 outputs
input "clock": a built-in clock: a row every 20 ms (t = 0, 1, 2, …, each row's value and timestamp its time in milliseconds), 50 rows in all; over t (whole numbers); a missing point is missing data (eod past the last row along t); elucid8 serve gives it its rows over time, elucid8 run cannot read it
ok
2 expectations not run: deploy.json gives clock (a built-in clock) its rows over time, and check computes only with the data there are when it runs
elucid8 serve wave.el8 --http 127.0.0.1:8080

A second later, the clock has given its 50 ticks:

elucid8> x[t = 25]
1
elucid8> clock[t = last]
980
elucid8> spark x[t = 0..last]
▁▁▂▂▃▃▄▄▄▅▅▅▆▆▆▇▇▇▇█████████████▇▇▇▇▆▆▆▅▅▅▄▄▄▃▃▂▂▁
t from 0 to 49; min 0, max 1
elucid8> quit

Without until the clock ticks until the service stops, and show x[t = last] follows the wave in the eLucid8 Player as it grows. A clock, a replay and feed.sh all give the service rows in the same way, so the program, its questions and the Player are the same whichever brings the data (Command reference).

13. More aggregates over regions, and values with no dimensions

Section 6 introduced regions and sum e over R end. Here are more of their uses.

A total over a fixed region. Here total has no dimension of its own: the region supplies every t.

-- The sum of a series over a fixed region.
x = input("x", t : 10)
total = sum x over (t : 0 .. 9) end
squares = sum x * x over (t : 0 .. 9) end
-- data x = #t + 1
-- expect total = 55
-- expect squares = 385

Open in playground

The argument can be any expression (x * x), not only a named value.

A moving window. The ends are expressions of the current point, as in […], and a region over a dimension the point already has sets it inside the aggregate. Near the start the window reaches below 0: those positions are left out, so the window is shorter there and mean divides by the positions left. A region wholly below 0 is empty.

-- A moving mean over the last three ticks, shorter at the start.
x = input("x", t : 10)
moving = mean x over (t : #t - 2 .. #t) end
before = sum x over (t : #t - 5 .. #t - 3) end
-- data x = 3 * #t
-- expect moving[t = 5] = 12
-- expect moving[t = 1] = 1.5
-- expect moving[t = 0] = 0
-- expect before[t = 1] = 0
-- expect before[t = 4] = 3

Open in playground

Supplying a coordinate. row_total is over i only, but m is over i and j: the region supplies j, as [j = …] would.

-- A per-row total over a two-dimensional table.
m = input("m", i : 3, j : 4)
row_total = sum m over (j : 0 .. 3) end
row_max = max m over (j : 0 .. 3) end
grand = sum m over (i : 0 .. 2, j : 0 .. 3) end
-- data m = 10 * #i + #j
-- expect row_total[i = 2] = 86
-- expect row_max[i = 1] = 13
-- expect grand = 138

Open in playground

Matrix multiplication is one line: the sum over k of A's row times B's column. It agrees with the version that walks a third dimension with a running sum, Multidimensional Programming's own example:

-- Matrix multiplication, with an aggregate and with a running sum.
A = input("A", i : 4, j : 4)
B = input("B", i : 4, j : 4)
C = sum A[j = #k] * B[i = #k] over (k : 0 .. 3) end
acc = restrict A[j = 0] * B[i = 0] fby.z acc + A[j = #z + 1] * B[i = #z + 1]
  to (i : 4, j : 4, z : 4) end
C_walk = acc[z = 3]
-- data A = #i + #j
-- data B = #i - #j
-- expect C[i = 2, j = 1] = 12
-- expect C_walk[i = 2, j = 1] = 12
-- expect C[i = 3, j = 0] = 32
-- expect C_walk[i = 3, j = 0] = 32

Open in playground

A value with no dimensions is computed once per query and can be used from any context: here, a data mean used to centre every point.

-- A value with no dimensions: computed once per query, used anywhere.
x = input("x", t : 10)
average = mean x over (t : 0 .. 9) end
centred = x - average
-- data x = 2 * #t
-- expect average = 9
-- expect centred[t = 0] = -9
-- expect centred[t = 9] = 9

Open in playground

Statistics, and where the largest is. variance e over R end is the mean squared distance from the mean, sample_variance divides by one fewer (it is eod for a single element), and stddev is the square root of variance. Each is computed exactly and rounded once, so it never comes out negative. argmax.d e over R end is the coordinate along d of the largest element (the first, if several are equal), and argmin of the smallest: over (t : 3 .. 9) a coordinate from 3 to 9, not a place in the range, and along a text dimension a name. Like every aggregate, they leave missing data out and are eod if an element is eod, unless skip eod.

-- Spread of a series, and where it peaks.
x = input("x", t : 10)
spread = variance x over (t : 0 .. 9) end
spread_s = sample_variance x over (t : 0 .. 9) end
sd = stddev x over (t : 0 .. 9) end
peak = argmax.t x over (t : 0 .. 9) end
low = argmin.t x over (t : 0 .. 9) end
-- data x = if #t == 4 then 20 else #t + 1
-- expect spread = 27
-- expect spread_s = 30
-- expect sd = 5.19615
-- expect peak = 4
-- expect low = 0

Open in playground

Results follow section 6. An empty region gives 0 for sum and count, and for mean, min, max, argmax, argmin and the statistics an error of kind empty ("mean of no elements"): there is no answer, as for 0 / 0. default(e, v, "empty") fills it. Every element at a position of 0 or more, inside the bound of what is aggregated, is demanded: an eod element makes the result eod, which wins over an error element; otherwise an error element makes it an error. An aggregate over more than a million elements (for example (i : 0 .. 1000000000000) over something with no bound along i) stops the query with a program error before computing any element; the limit can be changed with the query option maxAggregate.

-- Empty regions, eod elements and error elements.
x = input("x", t : 10)
none = sum x over (t : 1 .. 0) end
nothing = mean x over (t : 1 .. 0) end
filled = default(nothing, 0, "empty")
upto = sum x over (t : 0 .. #t) end
-- data x = if #t == 3 then eod else if #t == 6 then 1 / 0 else #t
-- expect none = 0
-- expect nothing = error empty: mean of no elements
-- expect filled = 0
-- expect upto[t = 2] = 3
-- expect upto[t = 5] = eod
-- expect upto[t = 7] = eod

Open in playground

Sums are exact: sum gives the exact sum of the elements, rounded once to the nearest number, and mean the exact sum divided by the count, rounded once. Floating-point addition rounds at every step, so adding one element after another can lose digits, and the result can depend on the order; an exact sum does not. Where the elements are written as a product inside the aggregate, sum a * b over R end or with more factors, sum a * b * c over R end, the products are exact too, so a dot product is the exact value rounded once. A product reached any other way (through a variable, a function's parameter or a local definition) is an ordinary rounded number, which sum then adds exactly, so sum a * b over R end and sum p over R end with p = a * b can differ in the last digits. A sum of only −0 is −0. Here 10¹⁶ + 1 would round to 10¹⁶ if added first, but the exact sum keeps the 1, in either order:

-- An exact sum: the order of the elements does not matter.
g = input("g", x : 2, y : 2)
xy = sum g over (x : 0 .. 1, y : 0 .. 1) end
yx = sum g over (y : 0 .. 1, x : 0 .. 1) end
-- data g = if #x == 1 and #y == 0 then -10000000000000000 else if #y == 0 then 10000000000000000 else 1
-- expect xy = 2
-- expect yx = 2

Open in playground

A sum is non-finite (an error) only if its exact value is too large for a number: mean of 10³⁰⁸ and 10³⁰⁸ is 10³⁰⁸, though adding them one after the other would overflow.

The exact form and the fast form. A sum or mean whose element is a product of three or more factors written in place, sum w * x * y over R end, is exact, and is computed point by point: on a large region it is much slower than a plain sum (about 3.6 s against 0.03 s for 40,000 points of 700 terms). Naming the product first, p = w * x * y and then sum p over R end, gives an ordinary sum, exact over the products each rounded once, so the answer may differ in the last place. Choose the form in place where every digit matters, and the named product where speed does. Here every product is exact, so the two agree:

-- The exact form and the fast form: the same answer here.
w = input("w", t : 4)
x = input("x", t : 4)
y = input("y", t : 4)
p = w * x * y
exact = sum w * x * y over (t : 4) end
fast = sum p over (t : 4) end
-- data w = #t + 1
-- data x = 2
-- data y = 0.5
-- expect exact = 10
-- expect fast = 10

Open in playground

approx_sum and approx_mean add in any order, the fastest the engine has (split across threads, in vector lanes), as NumPy, pandas and DuckDB do. Their results may differ in the last digits between machines, numbers of threads and runs; every other value is the same everywhere. Use them where speed matters more than the last digits.

Products are exact too: product e over R end is the exact product of the elements, rounded once, so the order does not matter: multiplying 0.1, 3 and 0.7 one after the other gives 0.21000000000000002, the exact product rounded once is 0.21. Over no elements it is 1, as a sum of none is 0, and it is an error only where the exact product is too large for a number. approx_product multiplies in the region's order, for speed; here 10⁻³⁰⁰ × 10⁻³⁰⁰ is too small for a number and becomes 0 before the large factors come:

-- The exact product, and the product in order.
f = restrict if #t < 2 then 1e-300 else 1e300 to (t : 4) end
exact = product f over (t : 4) end
in_order = approx_product f over (t : 4) end
none = product f over (t : 0) end
-- expect exact = 1
-- expect in_order = 0
-- expect none = 1

Open in playground

product and approx_product are computed point by point too.

An aggregate does not guard a reference to the value being defined, since its region may include the current point:

-- An aggregate is not a guard.
x = input("x", t : 10)
total = restrict x + sum total over (t : 0 .. #t - 1) end
  to (t : 10) end
-- data x = 1
-- expect compile error: unguarded cycle

(Write a running total with fby, as in section 5, or sum the input directly: sum x over (t : 0 .. #t) end.)

14. Real time: what it means in eLucid8, and its limits

eLucid8 keeps logical time and physical time apart, and connects them only explicitly.

-- A sensor, a slower calibration feed, and inputs answered with deadlines.
reading = input("reading", s : 4)
gain = input("gain", c : 2)
calibrated = reading * asof.c gain at time(reading) end
sum_service = input("sum_service", n : inf)
score = sum_service[n = calibrated]
quick_sum = input("quick_sum", n : inf)
hurried = quick_sum[n = calibrated]
alert = score > 50
hurried_alert = hurried > 50
-- table reading = s,value,ts; 0,10,1000; 1,20,1100; 2,30,1200; 3,40,1300
-- table gain = c,value,ts; 0,2,950; 1,3,1250
-- latency 5
-- service sum_service: deadline 20 ms
-- service quick_sum: simulated sum; takes 5 ms; deadline 2 ms
-- expect calibrated[s = 1] = 40, at time 1100 ms
-- expect calibrated[s = 3] = 120, at time 1300 ms
-- expect alert[s = 3] = true, at time 1305 ms
-- expect alert[s = 1] = false, at time 1105 ms
-- expect hurried[s = 3] = error late from quick_sum[n = 120]: late (1305 ms > deadline 1302 ms), at time 1302 ms
-- expect hurried_alert[s = 3] = error late from quick_sum[n = 120]: late (1305 ms > deadline 1302 ms), at time 1302 ms

Open in playground

How to read it:

Limitations. eLucid8 states timing; it does not guarantee it.

These limits are deliberate for a prototype. Wadge and Ashcroft judged Lucid unsuitable for real-time work (p. 111). eLucid8's timestamps, alignment and deadlines are a new, experimental answer, designed so that timing is explicit and every miss is visible.

For more, with examples of each alignment and an input answered on demand following live data, see Time and real time in eLucid8.

15. Maths functions

exp, expm1, log (the natural logarithm), log10, log2, log1p, sqrt, cbrt, tanh, sin, cos, tan, asin, acos, atan, sinpi, cospi, tanpi, degrees, radians, abs, floor, ceil, trunc, round, sign, and the rounding functions fp32, fp16, bf16, fp8_e4m3 and fp8_e5m2 take one number; min(a, b), max(a, b), pow(x, y), atan2(y, x), hypot(x, y), div(a, b) and mod(a, b) take two. pi, tau (2π), e and phi (the golden ratio) are constants: each the double nearest its value. Nothing may be named after them (a dimension may still be called e). Like + and *, they are strict and pointwise: every argument is demanded, an error argument wins over an eod one, and an argument that is not a number is an error.

Activations. A sigmoid is built from exp, and a ReLU from the two-argument max:

-- A sigmoid from exp, a ReLU from max, and tanh.
x = input("x", t : 10)
sigmoid = 1 / (1 + exp(-x))
relu = max(x - 3, 0)
squash = tanh(x)
-- data x = #t
-- expect sigmoid[t = 0] = 0.5
-- expect sigmoid[t = 2] = 0.880797
-- expect relu[t = 1] = 0
-- expect relu[t = 7] = 4
-- expect squash[t = 1] = 0.761594

Open in playground

min and max: function or aggregate? max e over R end is the aggregate of section 6, written without parentheses; max(a, b) with two arguments is the function. They nest freely:

-- A root-mean-square with sqrt and an aggregate; the largest absolute value, two ways.
x = input("x", t : 4)
rms = sqrt(mean x * x over (t : 0 .. 3) end)
peak = max abs(x) over (t : 0 .. 3) end
peak_of_ends = max(max(abs(x[t = 0]), abs(x[t = 3])), 0)
-- data x = 2 * #t - 3
-- expect rms = 2.23607
-- expect peak = 3
-- expect peak_of_ends = 3

Open in playground

Here x is −3, −1, 1 and 3, so the mean square is 5 and rms is √5.

Results outside the real numbers are errors that name the function, under the same rule as overflowing arithmetic (section 3): log(0) is −∞, log(-1) and sqrt(-1) are not numbers, and exp of a large argument overflows. An underflow to 0, as in exp(-1000), is an ordinary 0.

-- log, sqrt and exp outside the real numbers, and how the errors travel.
x = input("x", t : 10)
lg = log(x)
root = sqrt(x - 1)
grow = exp(1000 * x)
later = lg + 1
either = max(eod, log(x))
neither = max(1, log(x))
-- data x = #t
-- expect lg[t = 1] = 0
-- expect lg[t = 0] = error non-finite: log gave -Infinity (non-finite)
-- expect root[t = 0] = error non-finite: sqrt gave NaN (non-finite)
-- expect grow[t = 0] = 1
-- expect grow[t = 1] = error non-finite: exp gave Infinity (non-finite)
-- expect later[t = 0] = error non-finite from lg[t = 0]: log gave -Infinity (non-finite)
-- expect either[t = 0] = eod
-- expect neither[t = 0] = error non-finite: log gave -Infinity (non-finite)

Open in playground

either shows eod winning: at t = 0 the log argument is an error, but max's other argument is eod, so the result is eod; neither has no eod argument, so the error passes on.

Exact functions. floor, ceil and trunc round a number to a whole number (down, up, and towards 0), and round to the nearest one, halves away from 0 (round(2.5) is 3, round(-2.5) is −3); sign gives −1, 0 or 1; and pow(x, y) raises x to the power y. They give the same bits on every machine. Rounding keeps the sign of zero: trunc(-0.5), ceil(-0.5) and round(-0.4) are −0, and sign(-0) is −0. round has no argument for decimal places: write round(x * 100) / 100. pow(x, 0) is 1 for every x. A negative number to a power that is not a whole number is not a real number, so an error.

-- floor, ceil, trunc and sign; pow with whole-number and fractional exponents.
x = input("x", t : 6)
down = floor(x)
up = ceil(x)
toward0 = trunc(x)
nearest = round(x)
s = sign(x)
square = pow(x, 2)
inverse = restrict pow(2, -#t) to (t : 6) end
half = pow(x, 0.5)
-- data x = #t - 2.5
-- expect down[t = 0] = -3
-- expect up[t = 0] = -2
-- expect toward0[t = 0] = -2
-- expect toward0[t = 3] = 0
-- expect nearest[t = 0] = -3
-- expect nearest[t = 3] = 1
-- expect s[t = 1] = -1
-- expect square[t = 0] = 6.25
-- expect inverse[t = 3] = 0.125
-- expect half[t = 3] = 0.707107
-- expect half[t = 0] = error non-finite: pow gave NaN (non-finite)

Open in playground

x is −2.5, −1.5, −0.5, 0.5, 1.5 and 2.5. With a whole-number exponent, pow multiplies by repeated squaring in a fixed order, so pow(1.1, 1000) is the same number everywhere; with any other exponent it is the maths library's pow, the same bits everywhere too. A result too large, or pow(0, -1), is an error, as for exp.

Angles. sin and cos take an angle in radians, and atan2(y, x) gives the angle of the point (x, y), from −π to π, with the quadrant from the signs of both (atan(y / x) would lose it). Like exp, log and tanh, they come from one maths library, so they give the same bits on every machine, for small angles and huge ones alike.

-- A point turning on a circle, and its angle back.
x = restrict cos(#t * 0.5) to (t : 8) end
y = restrict sin(#t * 0.5) to (t : 8) end
angle = atan2(y, x)
radius = sqrt(x * x + y * y)
-- expect x[t = 0] = 1
-- expect y[t = 1] = 0.479426
-- expect angle[t = 2] = 1
-- expect angle[t = 7] = -2.78319
-- expect radius[t = 5] = 1

Open in playground

At t = 7 the angle 3.5 is past π, so atan2 gives it as 3.5 − 2π, about −2.78319.

tan, and asin, acos and atan, which give an angle back, come from the same library. degrees and radians convert, each rounding the exact product once, so radians(180) is pi and degrees(pi) is 180. Half-turns. sinpi(x), cospi(x) and tanpi(x) are sin(πx), cos(πx) and tan(πx), with πx formed exactly inside: so sinpi(1) is exactly 0 and cospi(0.5) exactly 0, where sin(pi) is 1.22465e-16 (pi is not exactly π). At an odd half, tanpi is infinite, so an error.

-- Angles in half-turns and in degrees, and the constants.
x = input("x", t : 5)
s = sinpi(x / 2)
c = cospi(x / 2)
t2 = tanpi(x / 4)
plain = sin(pi * x)
corner = degrees(atan2(1, 1))
right = radians(90) == pi / 2
circle = 2 * pi * x
-- data x = #t
-- expect s[t = 1] = 1
-- expect s[t = 2] = 0
-- expect c[t = 1] = 0
-- expect c[t = 2] = -1
-- expect t2[t = 1] = 1
-- expect t2[t = 2] = error non-finite: tanpi gave Infinity (non-finite)
-- expect plain[t = 1] = 1.22465e-16
-- expect corner = 45
-- expect right = true
-- expect circle[t = 1] = 6.28319

Open in playground

More functions. log10 and log2 are the logarithms in base 10 and base 2 (log2 of a power of two is exactly its exponent: log2(1024) is 10); log1p(x) is log(1 + x) and expm1(x) is exp(x) − 1, accurate where x is tiny (where log(1 + x) would lose x to rounding); cbrt is the real cube root, negative numbers included; and hypot(x, y) is √(x² + y²) without overflowing on the way.

-- Inverse angles, logarithms near 1 and in bases 10 and 2, cube roots, and hypot.
x = input("x", t : 5)
up = asin(x / 4)
across = acos(x / 4)
slope = atan(x)
digits = log10(1000 * x)
bits = log2(x)
tiny = log1p(x / 1000000000000000000)
lost = log(1 + x / 1000000000000000000)
side = cbrt(-x * x * x)
diagonal = hypot(3 * x, 4 * x)
-- data x = #t
-- expect up[t = 2] = 0.523599
-- expect across[t = 4] = 0
-- expect slope[t = 1] = 0.785398
-- expect digits[t = 1] = 3
-- expect bits[t = 4] = 2
-- expect tiny[t = 1] = 1e-18
-- expect lost[t = 1] = 0
-- expect side[t = 3] = -3
-- expect diagonal[t = 2] = 10

Open in playground

Whole-number division. div(a, b) is a / b rounded down to a whole number, and mod(a, b) is what is left over, with the sign of b, so that a = b * div(a, b) + mod(a, b). Rounding down, not towards 0, keeps a pattern regular before 0: the hours before 1970 are hours of the day too.

-- div and mod: floor division, and the remainder with b's sign.
x = input("x", t : 6)
d = div(x, 3)
m = mod(x, 3)
back = 3 * d + m
neg = mod(x, -3)
-- data x = 2 * #t - 5
-- expect d[t = 0] = -2
-- expect m[t = 0] = 1
-- expect back[t = 0] = -5
-- expect d[t = 4] = 1
-- expect m[t = 4] = 0
-- expect neg[t = 5] = -1

Open in playground

x is −5, −3, −1, 1, 3 and 5. A divisor of 0 is an error, as for /. For whole numbers up to 2^53 − 1 both are exact; for other numbers each is the exact answer rounded once, the same on every machine.

Narrower number formats. eLucid8's numbers are 64-bit. Five functions give the nearest value of a narrower format, as a 64-bit number: fp32 (IEEE single precision), fp16 (IEEE half precision), bf16 (bfloat16: single precision's range with 8 significant bits), and the two 8-bit formats fp8_e4m3 and fp8_e5m2. Use them where a computation must give exactly what a value stored in that format holds: a model's weights in 8 bits, a sensor that records half precision, or a check against a program that computes in single precision. Halfway between two values, they round to the one whose last bit is 0, as IEEE 754's conversions do.

-- The nearest value of each narrower format.
x = input("x", t : 4)
third = x / 3
single = fp32(third)
brain = bf16(third)
eight = fp8_e4m3(x * 100)
past = fp8_e4m3(x * 150)
half = fp16(x * 20000)
-- data x = #t + 1
-- expect single[t = 0] = 0.333333
-- expect brain[t = 0] = 0.333984
-- expect eight[t = 0] = 96
-- expect eight[t = 1] = 192
-- expect past[t = 2] = 448
-- expect past[t = 3] = error non-finite: fp8_e4m3 gave NaN (non-finite)
-- expect half[t = 2] = 60000
-- expect half[t = 3] = error non-finite: fp16 gave Infinity (non-finite)

Open in playground

bf16(1/3) is 0.333984375, the nearest number with 8 significant bits. fp8_e4m3(100) is 96: 100 is halfway between 96 and 104, and 96's last bit is 0. A number past a format's largest value is out of its range, as IEEE 754's conversion has it: infinity, or for fp8_e4m3, which has no infinity, not a number; either is an error, by the rule for any result that is not finite. The largest values are about 3.4 × 10^38 (fp32 and bf16), 65504 (fp16), 448 (fp8_e4m3) and 57344 (fp8_e5m2); 450 is 448 in fp8_e4m3, the nearest, and 600 is out of range. Numbers too small for a format become 0, keeping their sign.

These names are reserved for the functions: fun abs(v) = … is a compile error. A value may still be called log; only log(…) is the function.

16. The standard library: default and dates

A few functions come with eLucid8, written in eLucid8 itself. A call is replaced by the function's equations, as for a function of your own, so it is as fast as writing them out, and gives the same answers on every engine.

default(x, v) is v where x is eod, and x everywhere else: an error stays an error. default(x, v, "missing") is v where x is missing data (section 3); the third argument names what is filled, "eod" or a kind of error as iserror writes it ("late", "failed", …), and default(x, v) is default(x, v, "eod"). v is computed only where it is needed. Its timestamp is x's, or, where it is filled, the latest of x's and v's.

-- default: a value where data end, or where they are missing.
x = input("x", t : inf)
filled = restrict default(x, 0) to (t : 6) end
gaps = restrict default(x, 0, "missing") to (t : 6) end
-- table x = t,value; 0,3; 2,5; 3,eod
-- expect filled[t = 1] = error missing from x[t = 1]: input x has no data at this point
-- expect filled[t = 2] = 5
-- expect filled[t = 3] = 0
-- expect filled[t = 4] = 0
-- expect filled[t = 7] = eod
-- expect gaps[t = 1] = 0
-- expect gaps[t = 3] = eod
-- expect gaps[t = 4] = eod

Open in playground

x is eod at t = 3 (its last row says so) and at t = 4 and 5 (past its last row along t, declared inf), so filled is 0 there. At t = 1 there is no row among the rows: that is missing data, which default(x, 0) leaves as it is and default(x, 0, "missing") fills. To fill both, default(default(x, 0), 0, "missing"). default fills every point where x is eod, past its data for ever; the restriction to (t : 6) keeps filled to t from 0 to 5.

Dates. year, month (1 to 12), day_of_month (1 to 31), day_of_week (Monday = 1 … Sunday = 7, as ISO numbers them) and hour_of_day (0 to 23) read a time in milliseconds since 1970-01-01 00:00 UTC, the unit of timestamps (section 9). start_of_day and start_of_month give the first millisecond of its day and of its month. They count in UTC, with the Gregorian calendar extended back before 1582, and accept times before 1970 (negative ones). They are exact for every whole number of milliseconds up to 2^53 − 1 either way, about 285,000 years.

-- UTC date fields of a time in milliseconds.
when = input("when", t : 3)
y = year(when)
m = month(when)
d = day_of_month(when)
wd = day_of_week(when)
h = hour_of_day(when)
day0 = start_of_day(when)
month0 = start_of_month(when)
-- table when = t,value; 0,1709211900000; 1,-1; 2,0
-- expect y[t = 0] = 2024
-- expect m[t = 0] = 2
-- expect d[t = 0] = 29
-- expect wd[t = 0] = 4
-- expect h[t = 0] = 13
-- expect day0[t = 0] = 1709164800000
-- expect month0[t = 0] = 1706745600000
-- expect y[t = 1] = 1969
-- expect d[t = 1] = 31
-- expect wd[t = 1] = 3
-- expect wd[t = 2] = 4

Open in playground

1709211900000 is 13:05 UTC on Thursday 29 February 2024, and −1 the last millisecond of Wednesday 31 December 1969. To read the date of a value's data, give its timestamp: hour_of_day(time(reading)).

A time as text. utc_text(t) writes a time in milliseconds as ISO 8601 text in UTC, the form utc("…") reads (section 9): seconds always, and the milliseconds, three digits, only when they are not 0. utc reads every such text back as the same time. A number that is not a whole number of milliseconds, or outside the years 0000 to 9999, is an error of kind type; for a mean of times, write utc_text(floor(t)). Like text(x), it is strict and has its argument's timestamp. To show when a value's data arrived, utc_text(time(reading)).

-- Times in milliseconds as UTC text: seconds always, milliseconds when not 0.
when = input("when", t : 4)
stamp = utc_text(when)
-- table when = t,value; 0,1709211900000; 1,-1; 2,1709211900250; 3,1.5
-- expect stamp[t = 0] = "2024-02-29T13:05:00Z"
-- expect stamp[t = 1] = "1969-12-31T23:59:59.999Z"
-- expect stamp[t = 2] = "2024-02-29T13:05:00.250Z"
-- expect stamp[t = 3] = error type: utc_text needs a whole number of milliseconds in the years 0000 to 9999, found 1.5

Open in playground

For windows. Two functions serve programs that run in a window of controls and text. tab_focus(kind, key, shift, node, next, prev, takes, before) is the node with focus after an event: the event's fields as the window sends them (its kind, its key, whether Shift was held, the node it landed on, the focusable nodes after and before the focused one, and whether the node pressed takes focus), and the focus before it, read with fby. Tab moves the focus to next, Shift-Tab to prev, a press to the node pressed if it takes focus; any other event leaves it where it was. follow(scrolled, cursor, rows) is the first line to show: where the user scrolled to, unless the cursor moved after the scroll (the later timestamp decides), in which case the least change of view that shows the cursor with two lines to spare above and below, in rows lines.

An editor's document. A code editor in a window gives the program its document as an input. Mark the editor's text node "editor": true in deploy.json, and declare the input named after its id, over the turn, the line and the column: at each turn it is the document after that turn's change, one code point a cell, as a text part shows it, and eod past the end of a line and below the last line. The window answers it, so it needs no entry under inputs. To react to a change, read its row of kind "edit" on the window's event input: from, the first line it replaced (counted from 0), removed, how many lines it replaced, length, how many took their place, and by, who made it: "person", "file" (a file opened in it) or "program". Where the cursor is comes as a row of kind "cursor": line and col the cursor's, anchorLine and anchorCol the other end of the selection (the cursor's own place when nothing is selected). An editor whose text is a value of the program shows that value: the window records each change of it as a row by "program", and after a change by the person or a file it compares the value with the document again, so a change the program does not take is put back.

{ "text": "{editor}", "id": "editor", "editor": true, "width": "fill", "grow": 1 }
-- An editor's document, an input the window answers, and the change at each turn.
ev = input("ev", t : inf, f : {"node", "kind", "from", "removed", "length", "by"})
editor = input("editor", t : inf, line : inf, col : inf)
edited = ev[f = "kind"] == "edit" and ev[f = "node"] == "editor"
first_changed = editor[line = ev[f = "from"], col = 0]
-- table ev = t,f,value; 0,node,editor; 0,kind,edit; 0,from,0; 0,removed,1; 0,length,2; 0,by,person; 1,node,editor; 1,kind,edit; 1,from,1; 1,removed,1; 1,length,2; 1,by,person
-- table editor = t,line,col,value; 0,0,0,97; 0,1,0,98; 1,0,0,97; 1,1,0,98; 1,1,1,99; 1,2,0,100
-- expect edited[t = 1] = true
-- expect editor[t = 1, line = 2, col = 0] = 100
-- expect first_changed[t = 1] = 98

Open in playground

The tables stand for what the window answers: the document is a, b after turn 0 and a, bc, d after turn 1 (97 is a). At turn 1 the change replaced line 1 by two lines, so first_changed, the first character of the first line it changed, is 98, b.

Like a function of your own, a date field is strict: an eod or an error time gives eod or that error, and a time that is not a number gives the error of the operator inside the function that refused it (for example / needs numbers, found text "x"). An error that passes through a date field names the function's local values on its way (y.year#1.z), as one passing through your own function's where would.

These names are reserved: fun year(ms) = … is a compile error. A value may still be called year; only year(…) is the function.

17. Text

Text values. "high" is a text value. Texts are compared only with == and !=: two texts are equal when they have the same characters (case counts), and a text is never equal to a number or a boolean ("3" == 3 is false). + joins two texts ("C" + "♯" is "C♯"); a text and a number is an error that says how to join them ("a" + 1 is the error + needs two numbers, two sets or two texts, found text "a" and 1: write text(1) to join it). Anywhere else a number or a boolean is needed, text is an error (<, -, sums and maths functions of text). length(s) is a text's number of characters (code points: length("C♯4") is 3), and the library's join(L, sep) joins a list of texts with sep between them:

-- Texts joined.
notes = ["C", "E", "G"]
chord = join(notes, " ") + " major"
size = length(chord)
-- expect chord = "C E G major"
-- expect size = 11

Open in playground

Texts as coordinates. A coordinate along a dimension is a whole number (0, 1, 2, …) or a text, and each dimension's coordinates are all of one kind: all whole numbers or all texts. A list in a bound or a region names coordinates, in the order written:

-- zone's coordinates are the three names listed.
sales = input("sales", zone : {"north", "south", "east"}, month : 3)
share = sales / sum sales over (zone : {"north", "south", "east"}) end
where_it_is = restrict #zone
  to (zone : {"north", "south", "east"}) end
east = sales[zone = "east"]
size = if share > 0.4 then "big" else "small"
-- table sales = zone,month,value; north,0,10; south,0,30; east,0,60
-- expect share[month = 0, zone = "south"] = 0.3
-- expect where_it_is[zone = "east"] = "east"
-- expect east[month = 0, zone = "north"] = 60
-- expect size[month = 0, zone = "east"] = "big"
-- expect share[month = 0, zone = "west"] = eod

Open in playground

sales = input("sales", zone : {"north", "south", "east"}, month : 3)
grow = next.zone sales
-- expect compile error: zone's coordinates are whole numbers here (next.zone moves along it), but texts at 1:1

18. Lists, records and sets

A value may also be a list (a sequence of values, in order), a record (named fields) or a set (values with no order and no repeats). Each is one value with one timestamp: its elements and fields are values, never eod or an error, and carry no timestamp of their own.

l = [3, 1, 4]
s = {3, 1, 4, 1}
r = {x: 40, y: 12, due: 172}
first_one = head(l)
rest = tail(l)
longer = 9 :: l
both = l <> [5, 9]
third = l elt 2
n = length(l)
x_of_r = r.x
later = {due: 200} xby r
-- expect l = [3, 1, 4]
-- expect s = {1, 3, 4}
-- expect r = {due: 172, x: 40, y: 12}
-- expect first_one = 3
-- expect rest = [1, 4]
-- expect longer = [9, 3, 1, 4]
-- expect both = [3, 1, 4, 5, 9]
-- expect third = 4
-- expect n = 3
-- expect x_of_r = 40
-- expect later = {due: 172, x: 40, y: 12}

Open in playground

Filtering, those. those x from L if c end is the list of the elements of L for which c is true, in their order; inside c, x is the element, and may be passed to any function, a recursive one included. Over a set it gives a set: those s from {3, 8, 1} if s > 2 end is {3, 8}. (from, not in: in reads another file, section 8.) It is written for you as the aggregate below, list L elt #k over (k : length(L)) if c end with x read as L elt #k, so its rules are an aggregate's: every condition is evaluated, false or eod leaves the element out, and the first error is the result.

readings = [12, 40, 7, 55]
high = those r from readings if r > 30 end
how_many = length(high)
none = those r from readings if r > 100 end == []
big = those s from {3, 8, 1} if s > 2 end
-- expect high = [40, 55]
-- expect how_many = 2
-- expect none = true
-- expect big = {3, 8}

Open in playground

Lists and regions. Aggregates range over regions, never over lists. list e over R end is the list of e's values over the region R, in its order, and collect e over R end their set; both take if and skip eod, as every aggregate does. To aggregate a list's elements, lay the list along a dimension, L elt #k, and aggregate over a region:

squares = list #k * #k over (k : 5) end
total = sum squares elt #k over (k : length(squares)) end
evens = collect mod(#k, 2) over (k : 7) end
-- expect squares = [0, 1, 4, 9, 16]
-- expect total = 30
-- expect evens = {0, 1}

Open in playground

Recursion over a list. A function may walk a list by head and tail:

fun total(a) = if a == [] then 0 else head(a) + total(tail(a))
sum_of = total([1, 2, 3, 4])
-- expect sum_of = 10

Open in playground

eod and errors. Every operation is strict, as + is: a list, set or record with an element that is eod is eod; one with an element that is an error is that error (eod wins over an error, as everywhere). The result's timestamp is the latest of everything read, and head(L) and L elt i carry the list's. Errors say what was wrong:

l = [3, 1, 4]
a = head([])
b = l elt 5
c = {x: 1}.z
d = {1} + 3
-- expect a = error empty: head of the empty list
-- expect b = error index: elt: position 5 is past the end of a list of 3
-- expect c = error index: the record has no field z; its fields are x
-- expect d = error type: + needs two numbers or two sets, found a set and 3

Open in playground

All three at work. The playground's group Lists, records and sets has four examples. Visitors follows a shop's website minute by minute: each visit is a record of who came and which page, visits is the list of every visit so far, latest first, built with :: and fby, and seen the set of visitors, each counted once, built with addto. new_visitor asks whether this visitor is in the set before this minute, and mine counts this visitor's visits with those over the list. A to-do list with undo keeps the open tasks as a list of records and undoes an action by keeping a stack of the states before each one: undo takes its head. Who can reach whom grows a set of people step by step with + and isin, until a step adds no one (== on two sets), found with asa. A queue at a counter adds each customer to the end of a line with <> and serves from the front with tail; length is how long the line is, and elt finds who is last.

19. Checking what a program assumes, and numbers as text

e assert p is e where p is true, and an error of kind assert where p is false or eod: a program says what it assumes about its data, and every answer that would rest on a broken assumption is an error that says which one. p is checked first, so e is not computed where the check fails; an error in p passes on as it is; a p that is not true or false is the error assert needs a boolean condition. Where it holds, the answer's timestamp is the latest of e's and p's:

-- A balance that must never go below zero.
deposit = input("deposit", t : 5)
balance = deposit fby.t balance + next.t deposit
checked = balance assert balance >= 0
lasting = balance assert iseod(balance) or balance >= 0
-- table deposit = t,value; 0,50; 1,-20; 2,-40; 3,10; 4,5
-- expect checked[t = 1] = 30
-- expect checked[t = 2] = error assert: assert failed: balance >= 0 was false
-- expect checked[t = 3] = 0
-- expect checked[t = 5] = error assert: assert failed: balance >= 0 was eod
-- expect lasting[t = 5] = eod

Open in playground

Numbers as text. text(x) writes a number as elucid8 run writes it, a boolean as true or false, and gives a text as it is; a list, a record or a set is an error. A number may be written with an exponent, 1e21 or 2.5e-3, so the text of every number reads back as the same number:

-- A label for each step.
step = restrict #t to (t : 3) end
label = "step " + text(step) + " of " + text(3)
tiny = text(1 / 10000000)
-- expect label[t = 1] = "step 1 of 3"
-- expect tiny = "1e-7"

Open in playground

x along (t) = e checks a value's dimensions. A value's dimensions are never written: the compiler finds them from its equation (section 1). along (t) says which ones you expect, and the program compiles only if the compiler finds exactly those, in any order; along () says a value has none. It changes nothing else: a program that compiles with along computes exactly what it computes without it.

It matters because a value gains every dimension of what it reads, and so does every value that reads it. A reset meant for the whole grid, once a turn, that reads something depending on the cell's position becomes a value of every cell, and so does the count that reads it. The program still runs:

-- A reset meant for the whole grid, once a turn.
press = input("press", t : 4)
px = 1 fby.t 2
on_cell = #x == px and #y == px
reset = on_cell or press > 0
turns = restrict if reset then 0 else (1 fby.t turns + 1) to (t : 4) end
-- table press = t,value; 0,0; 1,0; 2,1; 3,0
-- expect turns[t = 3, x = 0, y = 0] = 1
-- expect turns[t = 3, x = 2, y = 2] = 0

Open in playground

turns is now a count per cell: 1 at most cells, 0 where the pointer was. Nothing says so. Write the dimensions you mean on the value that matters:

-- The same program, with the count of turns checked.
press = input("press", t : 4)
px = 1 fby.t 2
on_cell = #x == px and #y == px
reset = on_cell or press > 0
turns along (t) = restrict if reset then 0 else (1 fby.t turns + 1) to (t : 4) end
-- data press = 0
-- expect compile error: turns is written along (t), but its dimensions are (t, x, y): it depends on x and y through reset, which reads on_cell, which uses #x at 4:11 and #y at 4:24

The message names each extra dimension, the read that brought it in, and every read after it down to where it started (here #x and #y in on_cell), and each dimension listed that the value does not depend on (it does not depend on s). Written as meant, the reset reads the turn alone:

-- The same reset, once a turn.
press = input("press", t : 4)
reset along (t) = press > 0
turns along (t) = restrict if reset then 0 else (1 fby.t turns + 1) to (t : 4) end
-- table press = t,value; 0,0; 1,0; 2,1; 3,0
-- expect turns[t = 1] = 2
-- expect turns[t = 2] = 0
-- expect turns[t = 3] = 1

Open in playground

along may be written on a value, on a local value in a where block (l along () = …), on an output (output("level") along (t) = level) and on an input (x along (t) = input("x", t : 10)). Not on a function or its local values: their dimensions come from each call, so write along on the value that calls it.

20. The playground on your computer

Every program in this tutorial runs in elucid8 playground too: the playground in a window on your computer (macOS, Windows and Linux), with your own data of any size. It opens on a blank program: describe what you want to compute, or choose one from Examples ▾; change the program and press Update (Cmd-Enter on macOS, Ctrl-Enter on Windows and Linux) to run it, and ask for a value with Ask for, its coordinates and Run query: total at t = 4 gives total[t = 4] is 14, the Visualize tab draws the view around it, and Why? shows every demand behind it. Each update is kept as a version in the project's folder, where elucid8 run runs it as it is; New program and Open ▾ keep your projects. With your own Anthropic API key (Settings ▸ Add key…, or elucid8 key set), Generate program writes a program from a description. The playground on your computer shows the rest.

Quick reference

The Quick reference has every construct with an example and what it means; in brief:

ConstructExample
inputfield = input("field", time : 100, x : 16, y : 16): data from outside; where it comes from, and what a missing row reads, are the deployment's
valuesmooth = …: its dimensions are those its equation depends on; smooth along (t) = … checks them (section 19)
restrictionrestrict e to (x : 16, y : 16) end: e for x and y from 0 to 15, eod elsewhere; only x and y are defined inside; also ranges x : 3 .. 9 and lists x : {1, 3}
current coordinate#x
another contextv[x = #x + 1, time = 0] (a coordinate below 0 gives eod)
operators+ - * /, < <= == != > >=, and or not
conditionalif c then a else b
no dataeod; tests iseod(e), iserror(e), iserror(e, "missing"); e assert p (section 19)
streamsfirst.t e, next.t e, a fby.t b; y wvr.t q, y asa.t q, y upon.t q (Streams)
region (after over, in an aggregate or a restriction)(x : #x - 1 .. #x + 1, y : 16); also d : a .. inf, d : inf, d : {1, 3, 7}, d : window.d W at T
aggregatesmean, sum, min, max, count of e, written sum e over R end; skip eod before end leaves eod elements out; positions outside what is aggregated are left out; also variance, sample_variance, stddev, and argmax.d e over R end, argmin.d
maths functionsexp(e), expm1(e), log(e), log10(e), log2(e), log1p(e), sqrt(e), cbrt(e), tanh(e), sin(e), cos(e), tan(e), asin(e), acos(e), atan(e), sinpi(e), cospi(e), tanpi(e), degrees(e), radians(e), abs(e), floor(e), ceil(e), trunc(e), round(e), sign(e), min(a, b), max(a, b), pow(x, y), atan2(y, x), hypot(x, y), div(a, b), mod(a, b) (floor division); fp32(e), fp16(e), bf16(e), fp8_e4m3(e), fp8_e5m2(e) (the nearest value of a narrower format)
constantspi, tau (2π), e, phi (the golden ratio): each the double nearest its value
no dimensionsaverage = mean x over (t : 0 .. 9) end
text"north", compared with == and !=; a coordinate along any dimension, listed in a bound, restrict e to (zone : {"north", "south"}) end, #zone the coordinate (section 17)
lists, records, sets[1, 2], [], {x: 1, y: 2}, {1, 2}, {}; head(L), tail(L), x :: L, a <> b, L elt i, length(L), K.x, S xby T, s + t, s - t, s ^ t, s <= t, x isin c, x addto s, elements(L), enumeration(s), codes(s), from_codes(L); those x from L if c end; list e over R end, collect e over R end (section 18)
librarydefault(x, v); year(ms), month(ms), day_of_month(ms), day_of_week(ms) (Monday = 1), hour_of_day(ms), start_of_day(ms), start_of_month(ms), in UTC; tab_focus(…) and follow(scrolled, cursor, rows), for windows (section 16)
functionfun f(v, a) = e where … end, at the top level; it may call itself (section 8)
timestampstime(e); asof.c v at T end, hold.c v at T within 100 end, latest.c v at T end, after.c v at T end, the region item window.c 200 at T; durations 250ms, 30s, 5min, 2h, 1d, 1w; moments utc("2026-10-18T12:00:00Z"), and back to text, utc_text(t); the time now, a clock input (no now)
asked on demandmodel = input("model", n : inf), asked as model[n = x]: an input the deployment answers on demand; the deadline, retries (1 to 10 attempts in all) and reuse are its settings, not the program's
outputoutput("report") = e: one value per point, over its own bounds; where it goes, and in what format, are the deployment's (section 12)
in deploy.jsoninputs: a file, "wide" (a column per coordinate), "row", "fill" (section 9), "ragged" (section 3); a built-in "clock" or a "replay" of a file, rows given over time by elucid8 serve. Outputs: "to", "format", channels side by side, "skip": "eod" (eod records left out), "wide" (section 12)
comment-- …
reserved namesfun if not, true false eod; built-ins can't be redefined by fun (section 11)