Title: Compiling RealTime Functional Reactive Programming RTFRP
1Compiling Real-Time Functional Reactive
Programming (RT-FRP)
- Dana N. Xu and Siau-Cheng Khoo
- National University of Singapore
2Compilation
RT-FRP program
Functional Code
Partial evaluation
Automata Code
Tupling
Tupled Automaton Code
Contribution Systematic compilation via
high-level source to source transformation
3Contents
- Reactive System
- Introduction to RT-FRP
- Translating RT-FRP to Functional Code
- Compiling Functional Code to
- Automaton Code
- Conclusion and Future Work
4Reactive System
- Reactive systems have to react to an physical
environment which cannot wait. - It requires cost (Time Space) of running a
program to be bounded and known before run-time.
5Contents
- Reactive System
- Introduction to RT-FRP
- Translating RT-FRP to Functional Code
- Compiling Functional Code to Automata Code
- Conclusion and Future Work
6RT-FRP
- Reactive Part
- Captures the essential ingredients of FRP
programs - Allows recursion and higher order functions
- Resource-bounded (both time and space)
- Base Language
- Choose a terminating and resource-bounded language
7RT-FRP Syntax
e x c () (e1, e2) e? ? ?x.e e1
e2 v c () (v1, v2) v? ? ?x.e
a. Base language syntax
s, ev input time ext e delay v s
let snapshot x ? s1 in s2
s1 switch on x ? ev in s2 let
continue kj xj uj in s u u
s until ltevj ? kjgt
b. Reactive language syntax
Figure 2. RT-FRP language syntax
8Example 1 - The when operator
- when s
- let snapshot x1 ? s in
- let snapshot x2 ? delay False s in
- ext (if ?x2?x1 then ()? else ?)
9Contents
- Reactive System
- Introduction to RT-FRP
- Translating RT-FRP to Functional Code
- Compiling Functional Code to Automata Code
- Conclusion and Future Work
10Event and Behavior in RT-FRP
- type Behavior a Time ? a
- Event a ? Behavior (Maybe a)
- data Maybe a Nothing Just a
- type Event a Time ? Maybe a
time
t1
t2
t3
t4
t5
t6
t7
b
v1
v2
v3
v4
v5
v6
v7
e
j1?
j5?
j7?
j4?
?
?
?
11Stream Based Implementation
- time Behavior Time
- time \ts -gt ts
- delay a -gt Behavior a -gt Behavior a
- delay v s \ts -gt v(s ts)
- gt Event a -gt (a-gtb) -gt Event b
- .. Event a -gt Event a -gt Event a
- untilB Behavior a -gt
- Event (Behavior a) -gt Behavior a
- switcher Behavior a -gt
- Event (Behavior a) -gt Behavior a
12Lifting
- Lifting values/operations at base level to
- stream of values/operations at reactive level.
- Example 1
- lift0 constantB
- constantB 5 5, 5, 5,
- Example 2
- w1 let snapshot x lt- time
- in ext (x1)
- w1 lift1 (\x -gt x1) time
13Translating RT-FRP to Functional Code
- tr RExp -gt VEnv -gt BExp
- ext etr ?
- liftk (\v1..vk. e) w1..wk
- where (v1..vk, w1..wk) lookup e ?
- delay v str ?
- delay v s
- where s s ?
-
14Compiling RT-FRP to Functional Code
- let snapshot x s1 s2 ?
- time ? time
- s1 switch on x ev s2 ?
- s until ltevj gt kjgtj1..n ?
- let continue
- kj xj ujj1..n in s ?
15Equivalent Functional Representation at Abstract
Level
- z let snapshot a ? delay True x in
- let snapshot b ? delay True z in
- let snapshot c ? input in
- ext (if a then b else ((a and b) or c))
- z lift3 (\a-gt\b-gt\c-gt
- if a then b else ((a b) c))
- (delay True x) (delay True z) input
- x let snapshot a ? delay True x in
- let snapshot b ? z in
- ext (if a then False else b)
- x lift2 (\a-gt\b-gtif a then False else b)
- (delay True x) z
- n let snapshot a ? delay True x in
- let snapshot b ? delay 0 n in
- ext (if a then 0 else b1)
- n lift2 (\a-gt\b-gtif a then 0 else b1)
- (delay True x) (delay 0 n)
Translate to
Translate to
Translate to
16Contents
- Reactive System Synchronous Language
- Introduction to RT-FRP
- Translating RT-FRP to Functional Code
- Compiling Functional Code to Automata Code
- Conclusion and Future Work
17Our Algorithm
- Generalize all constants that may result in
infinite variations. - Apply
- a.Define configuration with state variables
- b.Instantiate signals
- c.Unfold Unify
- d.Fold or goto 2a
- to the body of the definition of a signal
18From Previous Example
- z lift3 (\a-gt\b-gt\c-gt
- if a then b else ((a b) c))
- (delay True x) (delay True z) input
- x lift2 (\a-gt\b-gtif a then False else b)
- (delay True x) z
- n lift2 (\a-gt\b-gtif a then 0 else b1)
- (delay True x) (delay 0 n)
19Generalize Integer Constants
- n lift2 (\a-gt\b-gtif a then 0 else b1)
- (delay True x) (delay 0 n)
- nconfig-1c1,c2,c3
- n lift2 (\a-gt\b-gtif a then c1 else bc2)
- (delay True x) (delay c3 n)
20Instantiate signals
- Instantiate
- x delay x x_
- n delay n n_ 1
- n lift2 (\a-gt\b-gtif a then c1 else bc2)
- (delay True x) (delay c3 n)
- n lift2 (\a-gt\b-gtif a then c1 else bc2)
- (delay True (delay x x_)) (delay c3 (delay n
n_))
21Unfold Once and Partial Evaluate
The unfolding rule used is lift2 (\a-gt\b -gt e)
(delay v1 v) (delay w1 w) gt delay
ev1/a,w1/b (lift2 (\a-gt\b -gt e) v w)
- n lift2 (\a-gt\b-gtif a then c1 else bc2)
- (delay True (delay x x_)) (delay c3 (delay n
n_)) - n delay c1 (lift2 (\a-gt\b-gtif a then c1 else
bc2) - (delay x x_) (delay n n_)) 2
22Unification
- n delay n n_ 1
- n delay c1 (lift2 (\a-gt\b-gtif a then c1 else
bc2) - (delay x x_) (delay n n_)) 2
- n delay c1 n_
- n_ (lift2 (\a-gt\b-gtif a then c1 else bc2)
- (delay x x_) (delay c1 n_))
23Adjust Time Backward (Future ? Current)
- n_ (lift2 (\a-gt\b-gtif a then c1 else bc2)
- (delay x x_) (delay c1 n_))
- n (lift2 (\a-gt\b-gtif a then c1 else bc2)
- (delay xb x) (delay c1 n))
x gt xb x_ gt x n_ gt n
24Try Folding
- n (lift2 (\a-gt\b-gtif a then c1 else bc2)
- (delay xb x) (delay c1 n))
- nconfig-1c1,c2,c3
- n lift2 (\a-gt\b-gtif a then c1 else bc2)
- (delay True x) (delay c3 n)
Folding fails
25Define a New Configuration
- n (lift2 (\a-gt\b-gtif a then c1 else bc2)
- (delay xb x) (delay c1 n))
- n delay c1 nconfig-2c1,c2,c1
- nconfig-2b1,b2,b3
- n (lift2 (\a-gt\b-gtif a then b1 else bb2)
- (delay xb x) (delay b3 n))
from n_
original n
26Instantiate
- Instantiate
- x delay x x_
- n delay n n_
- n (lift2 (\a-gt\b-gtif a then c1 else bc2)
- (delay xb x) (delay c3 n)
- n (lift2 (\a-gt\b-gtif a then c1 else bc2)
- (delay xb (delay x x_))
- (delay c3 (delay n n_)))
27Instantiate Unknown Boolean
- case xb False
- n (lift2 (\a-gt\b-gtif a then c1 else bc2)
- (delay False (delay x x_))
- (delay c3 (delay n n_)))
- n delay (c3c2)
- (lift2 (\a-gt\b-gtif a then c1 else bc2)
- (delay x x_) (delay n n_))
- unification with instantiation nc3c2
- n delay (c3c2)
- (lift2 (\a-gt\b-gtif a then c1 else bc2)
- (delay x x_) (delay (c3c2) n_))
- n delay (c3c2) nconfig-2c1,c2,c3c2
28(cont.)
- case xb True
- n (lift2 (\a-gt\b-gtif a then c1 else bc2)
- (delay True (delay x x_))
- (delay c3 (delay n n_)))
- n delay c1
- (lift2 (\a-gt\b-gtif a then c1 else bc2)
- (delay x x_) (delay n n_))
- unification with instantiation nc1
- n delay c1
- (lift2 (\a-gt\b-gtif a then c1 else bc2)
- (delay x x_) (delay c1 n_))
- n delay c1 nconfig-2c1,c2,c1
29Automaton for n
- nconfig-1c1,c2,c3
- ndelay c1 nconfig-2c1,c2,c1
- nconfig-2c1,c2,c3
- case xb False
- ndelay (c3c2) nconfig-2c1,c2,c3c2
- case xb True
- ndelay c1 nconfig-2c1,c2,c1
30Automata for n, x and z
31Tupling
- To combine multiple automata into a single
automaton - Benefits
- More efficient
- Less control logic
- More specialization across automata
32Tupling
- Combine three signals together
- mconfig-111c1,c2,c3
- (n,x,z)(nconfig-1c1,c2,c3,xconfig-1,zconfig-1)
- delay (c1,False,True)
- (nconfig-2c1,c2,c1,xconfig2,zconfig-2)
- delay (c1,False,True)
- mconfig-222c1,c2,c1
33Define, Unfold and Fold (twice)
- mconfig-222c1,c2,c3 st xb False, zb True
- (n,x,z)(nconfig-2c1,c2,c3,xconfig-2,zconfig-2)
-
- delay (c3c2,True,True)
- mconfig-233c1,c2,c3c2
- mconfig-233c1,c2,c3 st xb True, zb True
- (n,x,z) (nconfig-2c1,c2,c3,xconfig-3,zconfig-3
) -
- delay (c1,False,True) mconfig-223c1,c2,c1
34Define, Unfold and Fold
- mconfig-223c1,c2,c3 st xb False, zb True
- (n,x,z) (nconfig-2c1,c2,c3,xconfig-2,zconfig-3)
-
- delay (c3c2,z,input)
- mconfig-233ac1,c2,c3c2
35Define, Unfold and Fold
- mconfig-233ac1,c2,c3 st xzinput
- (n,x,z)
- (nconfig-2c1,c2,c3,xconfig-3,zconfig-3)
- case xzFalse
- delay (c3c2,z,input)
- (nconfig-2c1,c2,c3c2,xconfig-3,zconfig-3)
- delay (c3c2,z,input) mconfig-233ac1,c2,c3c
2 - case xzTrue
- delay (c1,False,True)
- (nconfig-2c1,c2,c1,xconfig-2,zconfig-3)
- delay (c1,False,True) mconfig-223c1,c2,c1
36Final Tupled Automaton
- mconfig-111c1,c2,c3
- (n,x,z) delay (c1,False,True)
- mconfig-222c1,c2,c1
- mconfig-222c1,c2,c3
- (n,x,z) delay (c3c2,True,True)
- mconfig-233c1,c2,c3c2
- mconfig-233c1,c2,c3
- (n,x,z) delay (c1,False,True)
- mconfig-223c1,c2,c1
- mconfig-223c1,c2,c3
- (n,x,z) delay (c3c2,input,input)
- mconfig-233ac1,c2,c3c2
- mconfig-233ac1,c2,c3 st xzinput
- (n,x,z) (nconfig-2c1,c2,c3,xconfig-3,zconfig-3
) - case xzFalse
- delay (c3c2,input,input)
- mconfig-233ac1,c2,c3c2
- case xzTrue
- delay (c1,False,True) mconfig-223c1,c2,c1
37Final Tupled Automaton
38Automata for n, x and z
39Final Tupled Automaton
40Conclusion
- Introduce an approach to building reactive
systems based on RT-FRP - A two-stage compiler for RT-FRP
- RT-FRP ? Functional Code
- Functional Code ? Automaton
- Partial Evaluation
- Tupling
41Future Work
- Re-design RT-FRP
- event-driven
- deterministic
- concurrent
- compile to hardware
42Unfolding Rules
- .. Event a -gt Event a -gt Event a
- (delay ? v) .. (delay w1 w)
- gt delay w1 (v .. w)
- (delay (Just a) v) .. (delay w1 w)
- gt delay (Just a) (v .. w)
- gt Event a -gt (a-gtb) -gt Event b
- (delay ? v) gt f
- gt (delay ? (v gt f))
- (delay (Just a) v) gt f
- gt (delay (Just (f a))) bot)
43Unfolding Rules
- lift2 (\a-gt\b -gt e) (delay v1 v) (delay w1 w)
- gt delay ev1/a,w1/b (lift (\a b -gt e) v w)
- switcher Behavior a-gtEvent (Behavior
a)-gtBehavior a - switcher (delay v1 v) (delay ? w)
- gt delay v1 (switcher v w)
- switcher (delay v1 v) (delay (Just k) w)
- gt delay v1 (switcher k w)
- untilB Behavior a-gtEvent (Behavior
a)-gtBehavior a - untilB (delay v1 v) (delay ? w)
- gt delay v1 (untilB v w)
- untilB (delay v1 v) (delay (Just k) w)
- gt delay v1 k
44Compiling RT-FRP to Functional Code
- extrs ? ?
- let v1..vn freevar s - ?
- if (n0) then s ? ?
- else lams v1..vn
- where lams \ v1..vn -gt s ? ?
-
45RT-FRP Example 1
- s1 let snapshot x ?? time
- in ext (sin x)
0
46FRP vs. RT-FRP
- FRP
- type Behavior a Time ? a
- type Event a (Time, a)
- RT-FRP
- Event a ? Behavior (Maybe a)
- data Maybe a Nothing Just a
- type Behavior a Time ? a
- type Event a Time ? Maybe a
47Stream Based Implementation
- gt Event a -gt (a-gtb) -gt Event b
- e1 gt f \ts ? loop ts (e1 ts)
- where loop (_ts) (Nothingys)
- Nothing(loop ts ys)
- loop (_ts) (Just ays)
- (Just (f a))(loop ts ys)
- fe gt f map (map f).fe
48Two Schemes for Reactive System Implementation
49Stream Based Implementation
- time Behavior Time
- time \ts -gt ts
- delay a -gt Behavior a -gt Behavior a
- delay v s \ts -gt v(s ts)
- gt Event a -gt (a-gtb) -gt Event b
- e gt f map (map f) . e
50Stream Based Implementation
- gt Event a -gt (a-gtb) -gt Event b
- e1 gt f \ts ? loop ts (e1 ts)
- where loop (_ts) (Nothinges)
- Nothing(loop ts es)
- loop (_ts) (Just aes)
- (Just (f a))(loop ts es)
51Stream Based Implementation
- .. Event a -gt Event a -gt Event a
- e1 .. e2 \ts -gt zipWith aux (e1 ts) (e2 ts)
- where aux Nothing Nothing Nothing
- aux (Just x) _ Just x
- aux _ (Just x) Just x
52Stream Based Implementation
- untilB Behavior a -gt
- Event (Behavior a) -gt Behavior a
- b untilB e
- \ts -gt loop ts (b ts) (e ts)
- where loop (_ts) (xxs) (Nothingmb)
- x(loop ts xs mb)
- loop ts (xxs)(Just bn_) x(bn ts)
-
53Stream Based Implementation
- switcher Behavior a -gt
- Event (Behavior a) -gt Behavior a
-
- s switcher e
- \ts -gt loop ts (s ts) (e ts)
- where loop (_ts) (xxs) (Nothingmb)
- x(loop ts xs mb)
- loop (_ts) (xxs) (Just bnmb)
- x(loop ts (bn ts) mb)
54Compiling RT-FRP to Functional Code
- let snapshot x s1 s2 ? ?
- s2 ? ?
- where x s1 ? ?
- ? ? ? (x,x)
- time ? ? time
55Compiling RT-FRP to Functional Code
- s1 switch on x ev s2 ? ?
- switcher s1 (ev gt \x -gt s2)
- where ev ev ? ?
- s1 s1 ? ?
- s2 s2 ? ?
56Compiling RT-FRP to Functional Code
- s until ltevj gt kjgtj1..n ? ?
- s untilB (ev1 gt k1
- .. evj gt kj
- .. evn gt kn)
- where evi evi ? ?
- s s ? ?
- kj lookup ? kj
57Compiling RT-FRP to Functional Code
- let continue kj xj ujj1..n in s ? ?
- s ? ?
- where ? ? ? (kj,kj)j1..n
- kj \ xj -gt uj ? ?
-
58RT-FRP Examples 2
- S3 (ext 0) switch on x ? ev in (ext x)
59Synchronous Programming
- Automaton
- Simple structure?
- Good coverage?
- Efficient?
- Difficult to design by hand ?
- Synchronous Programming
- High level ?
- Modularity ?
- Simple to re-use and compose ?
- Efficiency is not sacrificed ?
60Compilation
RT-FRP program
- Why intermediate functional code is needed?
- Easier to validate its correctness
- Allows High-level s-to-s transformation
Functional Code
Partial Evaluation to propagate constant values
to perform more aggressive specialization
Automata Code
Tupling to combine mutually dependent automata
into a composite automaton
Why Automaton? Simple, efficient, expressive
HalbwachsCAV98
Tupled Automaton Code
61Lifting
- fb xb \ts ? zipWith ()
- (fb ts) (xb ts))
- lift0 constantB
- lift1 f b1 lift0 f b1
- lift2 f b1 b2 lift1 f b1 b2
- lift3 f b1 b2 b3 lift2 f b1 b2 b3
-
-
62Code in RT-FRP(Example)
- z let snapshot a ? delay True x in
- let snapshot b ? delay True z in
- let snapshot c ? input in
- ext (if a then b else ((a and b) or c))
- x let snapshot a ? delay True x in
- let snapshot b ? z in
- ext (if a then False else b)
- n let snapshot a ? delay True x in
- let snapshot b ? delay 0 n in
- ext (if a then 0 else b1)