#From Lucid (pLucid) to eLucid8
eLucid8 is a language of the Lucid family, and a Lucid program written with fby, next, first and where reads almost the same in eLucid8. The differences are that each of those operators names the dimension it works along (fby.t, next.t, first.t), because an eLucid8 value may vary along many dimensions; that a value may be bounded along a dimension, by a bound such as restrict … to (t : n) end, where a Lucid history is always infinite (a dimension with no bound written has no end, as in Lucid). Lucid's asa, wvr (whenever) and upon are built in, with the dimension named as for fby: y asa.t q, y wvr.t q, y upon.t q. Where eLucid8 adds to Lucid (bounds, gaps and errors in the condition, timestamps), the Streams reference gives their rules.
The Lucid programs on this page are from W. W. Wadge and E. A. Ashcroft, Lucid, the Dataflow Programming Language, Academic Press, 1985, with the book's page numbers, or written in its notation where the book has none (the running sum and the moving average). Indexical Lucid's operators parameterised by a dimension, @.d and #.d among them, are as given by J. Plaice and J. Paquet, "Introduction to Intensional Programming", in M. A. Orgun and E. A. Ashcroft (eds.), Intensional Programming I, World Scientific, 1996, pp. 7–8. The matrix product follows E. A. Ashcroft, A. A. Faustini, R. Jagannathan and W. W. Wadge, Multidimensional Programming, Oxford University Press, 1995, §5.3.
#What carries over
| Lucid | eLucid8 | |
|---|---|---|
x fby y | x fby.t y | x at t = 0, then y one step back |
next x | next.t x | x one step ahead |
first x | first.t x | x at t = 0, at every t |
if c then a else b fi | if c then a else b | only the branch taken is computed |
e where … end | e where … end | local definitions |
eod, iseod x | eod, iseod(x) | end of data: the point is outside the data |
error, iserror x | error("…"), iserror(x) | an error, which names where it started |
an input variable x | x = input("x", t : n) | the data, bounded to its n points |
| a history (infinite) | a value bounded along t, restrict … to (t : n) end, or with no bound written | eod beyond the bound; with no bound, no end |
y asa q | y asa.t q | the value of y the first time q is true |
y upon q | y upon.t q | y advances only after a point where q is true |
y wvr q | y wvr.t q | y at the points where q is true, renumbered |
x @.d e, #.d (Indexical Lucid) | x[d = e], #d | x at coordinate e along d; the current coordinate along d |
| time only | any dimensions: fby.x, next.y, [x = …] | see the matrix product below |
Each example below gives the Lucid program, then eLucid8, with its data (a -- table line, as the playground's table) and its stated answers (-- expect). A test checks every answer on this page in both of eLucid8's engines.
#A running sum
The sum of the inputs so far. In Lucid's notation:
s
where
s = first x fby s + next x;
end
-- table x = t,value; 0,3; 1,1; 2,4; 3,1; 4,5; 5,9
x = input("x", t : 6)
total = first.t x fby.t (total + next.t x)
-- expect total[t = 0] = 3
-- expect total[t = 4] = 14
-- expect total[t = 5] = 23
-- expect total[t = 6] = eod
#A counter, and a running average
The book's running average (p. 47): s sums the inputs before each step and n counts the steps, so the average of the first t + 1 inputs is s/n one step ahead. (The book's earlier form of the same program, p. 46, writes it next quot(s, n): next applies to the quotient.)
next s/n
where
s = 0 fby s + x;
n = 0 fby n + 1;
end
-- table x = t,value; 0,3; 1,1; 2,4; 3,1; 4,5; 5,9
x = input("x", t : 6)
s = 0 fby.t s + x
n = 0 fby.t n + 1
average = next.t (s / n)
-- expect n[t = 4] = 4
-- expect average[t = 0] = 3
-- expect average[t = 1] = 2
-- expect average[t = 3] = 2.25
-- expect average[t = 5] = 3.83333
#A moving average
The mean of each input and the next two, in Lucid's notation:
(x + next x + next next x)/3
-- table x = t,value; 0,3; 1,1; 2,4; 3,1; 4,5; 5,9
x = input("x", t : 6)
moving = (x + next.t x + next.t next.t x) / 3
-- expect moving[t = 1] = 2
-- expect moving[t = 3] = 5
-- expect moving[t = 4] = eod
At t = 4 the window reaches t = 6, past the data, so the answer is eod.
#Fibonacci
The book's Fibonacci program (p. 54), two loop variables updated together:
f
where
f = 1 fby f + g;
g = 0 fby f;
end
f = 1 fby.t f + g
g = 0 fby.t f
-- expect f[t = 0] = 1
-- expect f[t = 5] = 8
-- expect f[t = 10] = 89
-- expect f[t = 29] = 832040
#asa: a square root by Newton's method
The book's first program (p. 1) takes a square root with asa ("as soon as"): approx asa err < 0.0001 is the first approximation whose error is small enough. y asa q is first(y wvr q) (p. 71).
sqroot(z) = approx asa err < 0.0001
where
Z is current z;
approx = Z/2 fby (approx + Z/approx)/2;
err = abs(square(approx) - Z);
end
In eLucid8 the iterations are a dimension k, and asa.k looks along it. approx has no end along k, so the error is looked at over the first 20 steps (a bound the search needs; without one it would never stop when no step is good enough, and the compiler refuses it: The walk of wvr, asa and upon, rule 8): if none is good enough, root is eod.
z = 2
approx = z / 2 fby.k (approx + z / approx) / 2
err = restrict abs(approx * approx - z) to (k : 20) end
root = approx asa.k err < 0.0001
-- the same step, found by hand: the smallest k where the error is small enough
first_ok = min #k over (k : 20) if err < 0.0001 end
-- expect first_ok = 3
-- expect root = 1.41422
#upon and wvr: changing a stream's rate
y upon q advances y only after a point where q is true: it repeats a value of y until q lets it move on. y wvr q ("whenever") keeps the values of y at the points where q is true and drops the others, so its t-th value comes from a later point of y. Here y is 10 times the step, and q is true at every third step among the first 20:
y = 10 * #t
q = restrict mod(#t, 3) == 0 to (t : 20) end
up = y upon.t q
w = y wvr.t q
-- the same, by hand: c counts the points before t where q was true,
-- and y is read at a computed position.
c = 0 fby.t c + (if q then 1 else 0)
up_by_hand = y[t = c]
-- expect up[t = 0] = 0
-- expect up[t = 1] = 10
-- expect up[t = 3] = 10
-- expect up[t = 4] = 20
-- expect w[t = 0] = 0
-- expect w[t = 1] = 30
-- expect w[t = 4] = 120
-- expect w[t = 7] = eod
-- expect up_by_hand[t = 4] = 20
w looks for q's points within q's bound, the first 20 steps: there are seven (0, 3, …, 18), so w at t = 7 is eod. Where Lucid's wvr would wait for ever for a point that never comes, eLucid8 gives eod. The bound wvr and asa need, and what an eod, an error or a missing reading in q gives: The walk of wvr, asa and upon.
#A matrix product: more than one dimension
A Lucid history has one dimension; eLucid8 values can have several. The matrix product of Multidimensional Programming (§5.3) gives C at (i, j) as the sum over z of A at (i, z) times B at (z, j); a query for one entry of C reads one row of A and one column of B, nothing else. With a sum over a region it is one line, as in the playground's Matrix multiplication example:
-- data A = #i + #j
-- data B = #i * #j - 1
A = input("A", i : 4, j : 4)
B = input("B", i : 4, j : 4)
C = sum A[j = #z] * B[i = #z] over (z : 0 .. 3) end
-- expect C[i = 1, j = 2] = 30
-- expect C[i = 0, j = 0] = -6
-- expect C[i = 3, j = 3] = 78
#What eLucid8 leaves out
Lucid-family research has gone further than eLucid8. In eLucid8 every expression denotes a first-order intension: at each context it has one result, a number, a boolean, a text, eod or an error. So eLucid8 leaves out values of other kinds, which the papers below explore:
- Higher-order functions. Functions that take functions as arguments or give them as results. An eLucid8 function takes values (and dimensions, below), never another function. J. Plaice and J. Paquet, "Introduction to Intensional Programming", in M. A. Orgun and E. A. Ashcroft (eds.), Intensional Programming I, World Scientific, 1996, pp. 1–14, §4.
- Dimensions as values. An eLucid8 function can take a dimension as a parameter (
fun before.d(v) = v[d = #d - 1], Tutorial, section 8), but a dimension is never a value: there is no stream of dimensions for a function to work along one after another. Plaice and Paquet, §5. - Definitions as values. A definition that itself varies with the context, rather than one definition whose result varies. Plaice and Paquet, §6. In eLucid8 a changed definition is a new version of the program.
- Relations as values. Tables of tuples, with their attribute names, as values that relational operators combine. J. Paquet and J. Plaice, "The Intensional Relation", in the same book, pp. 214–227. In eLucid8 a table is an input: a value over its row coordinates, read at a point.
- Versions as contexts. Versions of a program or of data as possible worlds, with one version inheriting from another. W. Wadge, "Possible WOOrlds", in the same book, pp. 56–62. In eLucid8 versions are kept outside the logical context: each query reads one version of every input, pinned for the whole query, under one version of the program, and a version is never a coordinate.
#What does not carry over
- Infinite histories. A Lucid program may run for ever; an eLucid8 value is bounded, or has a dimension with no end (
t : inf, or no bound written), for data that keeps arriving. is current, and nested iteration in time. Lucid'sis currentfreezes a value inside an inner loop. In eLucid8, an inner loop is a dimension of its own, askis in the square root above.
More: the Tutorial, and From Lustre to eLucid8 for the synchronous language of the same family.