Learn

Contents

From Lustre to eLucid8

Lustre and eLucid8 are both languages of streams defined by equations: a program is a set of definitions, each saying what a stream is at every instant, and pre in Lustre, like fby in eLucid8, reads a stream one instant back. A Lustre program translates line by line into eLucid8, as the examples below show. What does not carry over is Lustre's model of time. Lustre derives every stream's rate from one base clock (when samples a stream on a slower clock, current holds its last value), and a Lustre program compiles to a loop whose time and memory per cycle are bounded, the basis of its real-time guarantees. eLucid8 has no clocks of that kind and makes no such guarantee: streams at different rates are combined by the timestamps their data carry, with asof, hold and window, and a value may vary along many dimensions, not only time.

The Lustre fragments on this page are those of the Lustre V6 reference manual (Jahier, Raymond and Halbwachs, The Lustre V6 Reference Manual, Verimag), or written in its syntax where the manual has none (the watchdog and the stopwatch). More about Lustre: the Lustre programming language, Verimag.

Lustre and eLucid8, side by side

LustreeLucid8
pre xfalse fby.t x (or 0 fby.t x)x one step back; fby gives the value at t = 0, which Lustre leaves undefined
a -> bif #t == 0 then a else ba at the first instant, b after it
node N (…) returns (…)fun n(…) = …, or equations over ta function of whole streams, with local definitions in where … end
an input stream x: intx = input("x", t : n)the data, bounded to its n instants
current (x when c)hold.t x at T within age end, or if c then x else (x0 fby.t held)the last value of a slower stream; by timestamps, or step by step

Each example below gives Lustre first, then eLucid8, with its data (a -- table or -- data line, as the playground's table or formula) and its stated answers (-- expect). A test checks every answer on this page in both of eLucid8's engines. The playground's Buttons and latches (Lustre style) example has the first four together.

A rising edge

True when the input goes from false to true. The manual's EDGE node:

node EDGE (X: bool) returns (Y: bool);
let
 Y = false -> X and not pre(X);
tel

In eLucid8 its two signals are named on and off:

-- table button = t,value; 0,false; 1,true; 2,true; 3,false; 4,true; 5,false; 6,true; 7,true
button = input("button", t : 8)

pre_button = false fby.t button
edge = if #t == 0 then false else button and not pre_button

-- expect edge[t = 0] = false
-- expect edge[t = 1] = true
-- expect edge[t = 2] = false
-- expect edge[t = 4] = true
-- expect edge[t = 6] = true
-- expect edge[t = 7] = false

Open in playground

A counter with init, increment and reset

The manual's COUNTER node (written there with a pragma, %MT:task%, left out here): it starts at init, adds incr at each instant where X is true, and goes back to init when reset is true.

node COUNTER (init, incr: int; X, reset: bool) returns (N: int);
var PN: int;
let
  PN = init -> pre N;
  N =
      if reset then init
      else if X then PN + incr
      else PN;
tel

In eLucid8, a function with its own local definitions. Counting the rising edges of button, reset at t = 5, and the manual's counter modulo 10, which resets itself after 9 (reset = true -> pre(mod10)=9):

-- table button = t,value; 0,false; 1,true; 2,true; 3,false; 4,true; 5,false; 6,true; 7,true
-- data reset = #t == 5
button = input("button", t : 8)
reset = input("reset", t : 8)

fun counter(init, incr, x, reset) = n
  where
    pn = init fby.t n
    n = if reset then init else if x then pn + incr else pn
  end

pre_button = false fby.t button
edge = if #t == 0 then false else button and not pre_button
presses = counter(0, 1, edge, reset)

mod10 = restrict counter(0, 1, true, wrap) to (t : 20) end
wrap = restrict if #t == 0 then true else (0 fby.t mod10) == 9
  to (t : 20) end

-- expect presses[t = 0] = 0
-- expect presses[t = 1] = 1
-- expect presses[t = 3] = 1
-- expect presses[t = 4] = 2
-- expect presses[t = 5] = 0
-- expect presses[t = 6] = 1
-- expect presses[t = 7] = 1
-- expect mod10[t = 0] = 0
-- expect mod10[t = 9] = 9
-- expect mod10[t = 10] = 0
-- expect mod10[t = 11] = 1

Open in playground

A set/reset latch

The manual's SWITCH node: set turns the level on (only when it is off), reset turns it off, and otherwise it keeps its value. Called with the same signal for both, it is a one-button switch: each rising edge toggles the light.

node SWITCH (set, reset, initial: bool) returns (level: bool);
let
  level = initial ->
    if set and not pre(level) then true
    else if reset then false
    else pre(level);
tel

In eLucid8 its two signals are named on and off:

-- table button = t,value; 0,false; 1,true; 2,true; 3,false; 4,true; 5,false; 6,true; 7,true
button = input("button", t : 8)

fun switch(on, off, initial) = level
  where
    prev = initial fby.t level
    level = if #t == 0 then initial else if on and not prev then true else if off then false else prev
  end

pre_button = false fby.t button
edge = if #t == 0 then false else button and not pre_button
light = switch(edge, edge, false)

-- expect light[t = 0] = false
-- expect light[t = 1] = true
-- expect light[t = 3] = true
-- expect light[t = 4] = false
-- expect light[t = 5] = false
-- expect light[t = 6] = true
-- expect light[t = 7] = true

Open in playground

A watchdog

An alarm when no event has come for delay steps. In Lustre, a count of the steps left, set back to delay by each event:

node WATCHDOG (event: bool; delay: int) returns (alarm: bool);
var left: int;
let
  left = if event then delay else (delay -> pre(left)) - 1;
  alarm = left <= 0;
tel

Step by step in eLucid8, with events at t = 0, 2 and 7 and a delay of 3:

-- data event = #t == 0 or #t == 2 or #t == 7
event = input("event", t : 12)

left = if event then 3 else (3 fby.t left) - 1
alarm = left <= 0

-- expect alarm[t = 4] = false
-- expect alarm[t = 5] = true
-- expect alarm[t = 6] = true
-- expect alarm[t = 7] = false
-- expect alarm[t = 10] = true

Open in playground

By timestamps instead of steps: the events are rows with their times in milliseconds (the ts column), and the alarm is raised when the latest event is more than 3 seconds old. hold.t seen at now within 3000 end is the latest event at or before now, if it is less than 3000 ms old; otherwise missing data while events have stopped and come again later, and eod past the newest event.

-- table seen = t,value,ts; 0,1,200; 2,1,2200; 7,1,7200
seen = input("seen", t : inf)

now = restrict #t * 1000 + 500 to (t : 12) end
recent = hold.t seen at now within 3000 end
alarm = iseod(recent) or iserror(recent, "missing")

-- expect alarm[t = 4] = false, at time 2200 ms
-- expect alarm[t = 5] = true, at time 2200 ms
-- expect alarm[t = 6] = true, at time 2200 ms
-- expect alarm[t = 7] = false, at time 7200 ms
-- expect alarm[t = 10] = true

Open in playground

While an event is recent, the answer carries its time, the later of the event's and now's (7200 ms at t = 7). When the alarm is raised between events (t = 5 and 6), hold finds no event in the window and gives missing data stamped with the last event's time (2200 ms); after the newest event (t = 10) it gives eod, whose timestamp is the later of the timestamps of now and of the 3000: both are computed from the step and a constant, so 0.

A stopwatch

One button starts and stops it, another resets it; while it runs, it counts one per step. Written in Lustre:

node STOPWATCH (start_stop, reset: bool) returns (time: int);
var running: bool;
let
  running = false -> if start_stop then not pre(running) else pre(running);
  time = if reset then 0 else (0 -> pre(time)) + (if running then 1 else 0);
tel
-- data start_stop = #t == 1 or #t == 4 or #t == 6
-- data reset = #t == 8
start_stop = input("start_stop", t : 10)
reset = input("reset", t : 10)

prev_running = false fby.t running
running = if #t == 0 then false else if start_stop then not prev_running else prev_running
elapsed = if reset then 0 else (0 fby.t elapsed) + (if running then 1 else 0)

-- expect elapsed[t = 3] = 3
-- expect elapsed[t = 5] = 3
-- expect elapsed[t = 7] = 5
-- expect elapsed[t = 8] = 0
-- expect elapsed[t = 9] = 1

Open in playground

An integrator

The manual's integrator node: the trapezoid rule over a stream F sampled every STEP.

node integrator(F,STEP,init: real) returns (Y: real);
let
 Y = init -> pre(Y) + ((F + pre(F))*STEP)/2.0;
tel
-- data f = #t
f = input("f", t : 10)

fun integrator(f, step, init) = y
  where
    y = if #t == 0 then init else (init fby.t y) + ((f + (0 fby.t f)) * step) / 2
  end

area = integrator(f, 0.5, 0)

-- expect area[t = 0] = 0
-- expect area[t = 1] = 0.25
-- expect area[t = 2] = 1
-- expect area[t = 3] = 2.25
-- expect area[t = 4] = 4

Open in playground

What else changes