#Why eduction works
An eLucid8 program is a set of equations, and eduction is how eLucid8 computes with them: a question names a value at a point, and only what that answer depends on is computed. This page explains why that works, using the Mandelbrot set from the front page: four facts about equations, and what the runtime does with each.
#The running example
The front page's picture is computed from nine equations, the same text for every view:
-- The Mandelbrot set: for each point c of the plane,
-- repeat z ← z² + c from z = 0 and count the steps
-- until z is more than 1,024 from 0.
-- Each level is twice as close as the one before; x
-- and y have no end: the picture is the part of the
-- plane asked for, at the level asked for.
step = 0.005 / pow(2, #level)
steps = min(2000, round(200 * (1 + log10(pow(4, floor(#level / 2))))))
cx = -4 + #x * step
cy = -4 + #y * step
escaped = zr * zr + zi * zi > 1048576
zr = restrict 0 fby.k (if escaped then zr else zr * zr - zi * zi + cx) to (k : 2001) end
zi = restrict 0 fby.k (if escaped then zi else 2 * zr * zi + cy) to (k : 2001) end
n_steps = restrict 0 fby.k (if escaped then n_steps else n_steps + 1) to (k : 2001) end
-- The colour: the count made smooth between steps
-- (the steps in the set).
shade = if escaped then n_steps + 1 - log(log(zr * zr + zi * zi) / log(4)) / log(2) else steps
-- expect n_steps[level = 0, k = 200, x = 400, y = 600] = 5
-- expect escaped[level = 0, k = 200, x = 400, y = 600] = true
-- expect n_steps[level = 0, k = 3, x = 400, y = 600] = 3
-- expect n_steps[level = 0, k = 200, x = 500, y = 800] = 200
-- expect escaped[level = 0, k = 200, x = 500, y = 800] = false
A point of the plane has a whole-number address on x and one on y at each level: at level 0 the points are 0.005 apart, and each level is twice as close as the one before. k counts the steps, and steps says how many a level asks for: 200 at level 0, more as the picture zooms in. zr and zi are the real and imaginary parts of z, and 0 fby.k e means "0 at step 0, and at every later step, e at the step before". At (400, 600) on level 0, c = −2 − i: z is more than 1,024 from 0 after 5 steps, so n_steps stops at 5 and stays there. At (500, 800), c = −1.5: z never gets that far, and n_steps reaches 200: the point is in the set.
x and y have no end: the program describes the whole plane at every level, and the picture is only the part of it asked for. On a computer the front page asks for a grid of 1,440 × 960 points, with 200 steps at the first view and more as you zoom in.
#1. Equations have no order
The equations say what each value is, not when to compute it. They can be written in any order; escaped is used above before zr is defined. The only order is the one the data impose: zr at step k needs zr, zi and escaped at step k − 1, at the same point and level. Nothing else.
point (400, 600) point (401, 600) point (500, 800)
k = 200 n_steps zr zi escaped n_steps zr zi escaped n_steps zr zi escaped
↑ ↑ ↑
k = 199 n_steps zr zi escaped n_steps zr zi escaped n_steps zr zi escaped
↑ ↑ ↑
⋮ ⋮ ⋮ ⋮
k = 0 0 0 0 escaped 0 0 0 escaped 0 0 0 escaped
cx at x = 400, cy at y = 600 cx at x = 500, cy at y = 800
Read down a column and there is a dependency: a point's steps along k come one after another, and eLucid8 respects that. Read across and there is none: no equation lets one point read another. So every point can be computed at the same time as every other, and nothing in the program has to say so. The runtime finds this in the equations; the programmer does not.
Compare a loop in an imperative language:
for y in range(H):
for x in range(W):
zr = zi = 0.0; count = 0
for k in range(steps):
...
Here the order is part of the program: the loops say do this point, then that one. To run it in parallel, someone has to establish that the iterations are independent, split the work, coordinate the parts, and guard any state they share. That is engineering added on top of the program, and it is redone for each machine. In eLucid8 there is nothing to add: the front page asks for its picture as square tiles, each tile a separate question to one of several Web Workers in your browser, each running its own copy of the engine. The tiles finish in whatever order they finish. Any way of splitting the points is safe, because the equations already say that no point reads another.
The same program gives the same answers, to the last bit, on one thread or many, in the browser on a computer or a phone, or from the elucid8 command on a server. On the front page, Compute it again in your browser computes the first view in tiles on your machine and compares the result with the picture eLucid8 computed when the site was published, point by point.
Lucid, the language eLucid8 descends from, was designed as a dataflow language in this sense: equations, with the dependencies between values as the only order (W. W. Wadge and E. A. Ashcroft, Lucid, the Dataflow Programming Language, Academic Press, 1985).
#2. Computed values are never used up
An equation is pure: n_steps at step 200 at point (500, 800) of level 0 is 200, and it is 200 every time anyone asks. Nothing can change it after it is computed, and nothing it is used by can wear it out. A computed value is like a proven theorem: every reader may use it, as often as it likes, and it is still there, unchanged. Economists call goods like this non-rival. (The one kind of value outside this is one read from a model or a web API answered on demand: computed again, it asks the service again, unless the deployment reuses the service's answers. Its answers are recorded, and a question shows the calls it made.)
That is what lets eduction keep values and use them again. Asking for one value again costs nothing: it was kept. Asking for a neighbour costs only what the two do not share. The values of a question are kept, and reused, only while nothing they depend on has changed; when data are corrected, only the values that read the corrected data are computed again. In the features example, correcting one purchase recomputed 106 values out of hundreds of thousands (Inside eLucid8).
#3. Only what is asked for is computed
A question names one value at one point, and eduction starts there. Ask for n_steps[level = 0, k = 200, x = 500, y = 800], and eLucid8 computes 804 values: n_steps at each of the steps 0 to 200, zr, zi and escaped at steps 0 to 199, cx and cy at this one point, and step at level 0. No other point is touched, and the plane, which has no end, is never computed as a whole. Ask the same question again and it computes nothing: the answer was kept. Ask next for n_steps[level = 0, k = 200, x = 502, y = 800] and it computes 802: everything but cy at y = 800 and step at level 0, which the two points share.
In the playground, ask for one of these values and press Why?: the tree shows exactly the values the answer read, each labelled computed, already computed in this question, or reused from an earlier one. On the front page, clicking a spot asks for n_steps at that one point, and Why? shows the last steps of z before it escaped and how many values the answer took. When the page zooms in, it asks only for the points of the new view, in tiles, nearest the centre first; when you drag the picture, it asks only for the tiles that came into view.
This is eduction, the demand-driven evaluation of the Lucid family (E. A. Ashcroft, A. A. Faustini, R. Jagannathan and W. W. Wadge, Multidimensional Programming, Oxford University Press, 1995, ch. 5). Ask for one cell of a large simulation and only what that cell depends on is computed.
#4. Memory as a hybrid car's battery
A hybrid car runs on its engine when it must and on its battery when it can. Eduction treats memory the same way:
- The first trip needs the engine. A value never asked for before has to be computed: a new zoom level, a new point.
- Stop-and-go runs on the battery. Exploring, asking again, going back to an earlier question, and correcting data run on values already kept, computing only what is new.
- A bigger battery keeps more.
elucid8 serve --cache-bytes 256Mgives the caches a budget of 256 MiB; a larger budget keeps more answers ready for later questions. - Charging has a cost. Keeping a value takes memory, and records of what it was computed from. So the runtime decides, value by value, what to keep. Within one question, it knows from the equations how many times each point will be read, and once what it holds passes about 1 GiB, it lets a point go after its last reader. Across questions, the playground keeps every value it computes;
elucid8 servekeeps answers in a point cache when that is switched on, and over its budget first compacts those records, then lets the least recently used answers go. The front page keeps the tiles it computed, at every level you visit, within a budget in each of its workers: after a drag its status line says how many tiles were new and how many were reused from earlier, and over the budget the least recently used tiles go.
Where the analogy stops: a battery runs down; stored values do not. A kept value is as good on its millionth use as on its first. When one is let go, by a budget or after its last reader, it is never lost: asked for again, it is computed again from the same data, with the same answer to the last bit.
#Why it works
The four facts hold together. Because equations have no order, the runtime is free to choose when and where to compute each value. Because values never change, any value can be kept, shared, dropped and computed again. Because only what is asked for is computed, the work follows the question. The runtime's choices (caches, compiled loops, threads, tiles, what memory keeps) decide how fast an answer comes, never what it is.
To try it, open the Mandelbrot example in the playground, ask for n_steps at a few points and press Why?. Inside eLucid8 says how the engine does each of these things, and Why eLucid8 when to choose it.