Lucid (programming language): Difference between revisions
imported>Widefox code markup with a plausible lang, section names are never linked |
imported>Cedar101 →Details: ; : |
||
| Line 35: | Line 35: | ||
The values in a stream can be addressed by these operators (assuming x is the variable being used): | The values in a stream can be addressed by these operators (assuming x is the variable being used): | ||
;{{code|first x}} : fetches the first value in the stream x, | |||
;{{code|x}} : the current value of the stream, | |||
;{{code|next x}} : fetches the next value in the stream. | |||
;{{code|asa}} : an operator that does some thing 'as soon as' the condition given becomes true. | |||
;{{code|x upon p}} : upon is an operator that repeats the old value of the stream x, and updates to the new values only when the stream p makes a <code>true</code> value available. (It serves to slow down the stream x) | |||
i.e.: <code>x upon p</code> is the stream x with new values appearing upon the truth of p. | i.e.: <code>x upon p</code> is the stream x with new values appearing upon the truth of p. | ||
| Line 51: | Line 51: | ||
===Factorial=== | ===Factorial=== | ||
{{main|Factorial}} | {{main|Factorial}} | ||
<syntaxhighlight lang=" | <syntaxhighlight lang="text"> | ||
fac | fac | ||
where | where | ||
| Line 61: | Line 61: | ||
===Fibonacci sequence=== | ===Fibonacci sequence=== | ||
{{main|Fibonacci sequence}} | {{main|Fibonacci sequence}} | ||
<syntaxhighlight lang=" | <syntaxhighlight lang="text"> | ||
fib | fib | ||
where | where | ||
| Line 69: | Line 69: | ||
===Total of a Sequence=== | ===Total of a Sequence=== | ||
<syntaxhighlight lang=" | <syntaxhighlight lang="text"> | ||
total | total | ||
where | where | ||
| Line 78: | Line 78: | ||
===Running average=== | ===Running average=== | ||
{{main|Running average}} | {{main|Running average}} | ||
<syntaxhighlight lang=" | <syntaxhighlight lang="text"> | ||
running_avg | running_avg | ||
where | where | ||
| Line 89: | Line 89: | ||
===Prime numbers=== | ===Prime numbers=== | ||
{{main|Prime number}} | {{main|Prime number}} | ||
<syntaxhighlight lang=" | <syntaxhighlight lang="text"> | ||
prime | prime | ||
where | where | ||
| Line 108: | Line 108: | ||
===Quick sort=== | ===Quick sort=== | ||
{{main|Quick sort}} | {{main|Quick sort}} | ||
<syntaxhighlight lang=" | <syntaxhighlight lang="text" highlight="1"> | ||
qsort(a) = if eof(first a) then | qsort(a) = if eof(first a) then a else follow(qsort(b0),qsort(b1)) fi | ||
where | where | ||
p = first a < a; | p = first a < a; | ||
| Line 144: | Line 144: | ||
===Root mean square=== | ===Root mean square=== | ||
{{main|Root mean square}} | {{main|Root mean square}} | ||
<syntaxhighlight lang=" | <syntaxhighlight lang="text"> | ||
sqroot(avg(square(a))) | sqroot(avg(square(a))) | ||
where | where | ||
| Line 165: | Line 165: | ||
===Hamming problem=== | ===Hamming problem=== | ||
{{main|Hamming problem}} | {{main|Hamming problem}} | ||
<syntaxhighlight lang=" | <syntaxhighlight lang="text"> | ||
h | h | ||
where | where | ||
Latest revision as of 08:09, 14 August 2025
Template:Short description Script error: No such module "For". Script error: No such module "Unsubst". Script error: No such module "Infobox".Template:Template otherScript error: No such module "Check for unknown parameters".
Lucid is a dataflow programming language designed to experiment with non-von Neumann programming models. It was designed by Bill Wadge and Ed Ashcroft and described in the 1985 book Lucid, the Dataflow Programming Language.[1]
pLucid was the first interpreter for Lucid.
Model
Lucid uses a demand-driven model for data computation. Each statement can be understood as an equation defining a network of processors and communication lines between them through which data flows. Each variable is an infinite stream of values and every function is a filter or a transformer. Iteration is simulated by 'current' values and 'fby' (read as 'followed by') operator allowing composition of streams.
Lucid is based on an algebra of histories, a history being an infinite sequence of data items. Operationally, a history can be thought of as a record of the changing values of a variable, history operations such as first and next can be understood in ways suggested by their names. Lucid was originally conceived as a disciplined, mathematically pure, single-assignment language, in which verification would be simplified. However, the dataflow interpretation has been an important influence on the direction in which Lucid has evolved.[1]
Details
In Lucid (and other dataflow languages) an expression that contains a variable that has not yet been bound waits until the variable has been bound, before proceeding. An expression like x + y will wait until both x and y are bound before returning with the output of the expression. An important consequence of this is that explicit logic for updating related values is avoided, which results in substantial code reduction, compared to mainstream languages.
Each variable in Lucid is a stream of values. An expression n = 1 fby n + 1 defines a stream
using the operator 'fby' (a mnemonic for "followed by"). fby defines what comes after the previous
expression. (In this instance the stream produces 1,2,3,...).
The values in a stream can be addressed by these operators (assuming x is the variable being used):
first x- fetches the first value in the stream x,
x- the current value of the stream,
next x- fetches the next value in the stream.
asa- an operator that does some thing 'as soon as' the condition given becomes true.
x upon p- upon is an operator that repeats the old value of the stream x, and updates to the new values only when the stream p makes a
truevalue available. (It serves to slow down the stream x)
i.e.: x upon p is the stream x with new values appearing upon the truth of p.
The computation is carried out by defining filters or transformation functions that act on these time-varying streams of data.
Examples
Factorial
Script error: No such module "Labelled list hatnote".
fac
where
n = 0 fby (n + 1);
fac = 1 fby ( fac * (n + 1) );
end
Fibonacci sequence
Script error: No such module "Labelled list hatnote".
fib
where
fib = 0 fby ( 1 fby fib + next fib );
end
Total of a Sequence
total
where
total = 0 fby total + x
end;
Running average
Script error: No such module "Labelled list hatnote".
running_avg
where
sum = first(input) fby sum + next(input);
n = 1 fby n + 1;
running_avg = sum / n;
end;
Prime numbers
Script error: No such module "Labelled list hatnote".
prime
where
prime = 2 fby (n whenever [[isprime]](n));
n = 3 fby n+1;
isprime(n) = not(divs) asa divs or prime*prime > N
where
N is current n;
divs = N mod prime eq 0;
end;
end
Dataflow diagram
Script error: No such module "Labelled list hatnote".
Quick sort
Script error: No such module "Labelled list hatnote".
qsort(a) = if eof(first a) then a else follow(qsort(b0),qsort(b1)) fi
where
p = first a < a;
b0 = a whenever p;
b1 = a whenever not p;
follow(x,y) = if xdone then y upon xdone else x fi
where
xdone = iseod x fby xdone or iseod x;
end
end
Data flow diagram
--------> whenever -----> qsort ---------
| ^ |
| | |
| not |
| ^ |
|---> first | |
| | | |
| V | |
|---> less --- |
| | |
| V V
---+--------> whenever -----> qsort -----> conc -------> ifthenelse ----->
| ^ ^
| | |
--------> next ----> first ------> iseod -------------- |
| |
-----------------------------------------------------------
Root mean square
Script error: No such module "Labelled list hatnote".
sqroot(avg(square(a)))
where
square(x) = x*x;
avg(y) = mean
where
n = 1 fby n+1;
mean = first y fby mean + d;
d = (next y - mean)/(n+1);
end;
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;
end
Hamming problem
Script error: No such module "Labelled list hatnote".
h
where
h = 1 fby merge(merge(2 * h, 3 * h), 5 * h);
merge(x,y) = if xx <= yy then xx else yy fi
where
xx = x upon xx <= yy;
yy = y upon yy <= xx;
end;
end;
Dataflow Diagram
References
<templatestyles src="Reflist/styles.css" />
- ↑ Script error: No such module "citation/CS1".
Script error: No such module "Check for unknown parameters".