Esterel in Racket
~ / Posts / Esterel in Racket
Synchronous Reactive Programming
What is Esterel?
Esterel Esterel on Wikipedia is a synchronous reactive programming language developed in France in the 80s. In Esterel, operations are executed in parallel threads with specific synchronous timing.
There are a few implementations of Esterel or Esterel-like languages such as HipHop.js . Here, I'm using and adding onto the Racket implementation of Esterel Esterel in Racket .
Writing Esterel in Racket is be done using:
#lang racket
(require esterel/full)
(esterel
...)
In Esterel, signal
s are "variables"; they are emit
ed by Esterel programs
define-signal
defines signal
s globally while with-signal
allows you to locally define signal
s.
.
#lang racket
(require esterel/full)
(define-signal red)
(define forever-red
(esterel
(sustain red)))
Running this code doesn't visibly do anything.
You need to react!
to "ask" Esterel if there are any signals emit
ted.
> (react! forever-red)
'#hash((#<signal: red> . #t))
To control the timing of when Esterel signals are emit
ted, pause
is used.
#lang racket
(require esterel/full)
(define-signal red)
(define forever-red
(esterel
(pause)
(sustain red)))
> (react! forever-red)
'#hash()
> (react! forever-red)
'#hash((#<signal: red> . #t))
Since forever-red
is pause
d on the first step, there is no signal emit
ted.
react!
ing again walks to the next step and causes red
to be sustain
ed.
To run multiple threads in parallel, use par
:
#lang racket
(require esterel/full)
(define-signal red)
(define forever-red
(esterel
(par
(sustain red)
(sustain green))))
> (react! forever-red)
'#hash((#<signal: green> . #t) (#<signal: red> . #t))