Learn

Contents

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

LucideLucid8
x fby yx fby.t yx at t = 0, then y one step back
next xnext.t xx one step ahead
first xfirst.t xx at t = 0, at every t
if c then a else b fiif c then a else bonly the branch taken is computed
e where … ende where … endlocal definitions
eod, iseod xeod, iseod(x)end of data: the point is outside the data
error, iserror xerror("…"), iserror(x)an error, which names where it started
an input variable xx = 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 writteneod beyond the bound; with no bound, no end
y asa qy asa.t qthe value of y the first time q is true
y upon qy upon.t qy advances only after a point where q is true
y wvr qy wvr.t qy at the points where q is true, renumbered
x @.d e, #.d (Indexical Lucid)x[d = e], #dx at coordinate e along d; the current coordinate along d
time onlyany 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

Open in playground

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

Open in playground

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

Open in playground

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

Open in playground

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

Open in playground

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

Open in playground

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

Open in playground

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:

What does not carry over

More: the Tutorial, and From Lustre to eLucid8 for the synchronous language of the same family.