Title: EFRP Programming EventDriven Systems AssignmentFree
1E-FRPProgramming Event-Driven SystemsAssignment-
Free
- Zhanyong Wan, Walid Taha, and Paul Hudak
- Yale University
- Department of Computer Science
2Motivating Problem
3Simple RoboCup Controller (SRC)
behavior event delay
0-100
duty cycle
desired speed
SRC
0/1
power
speed
count
IR
0-100
ClkFast
ClkSlow
IncSpd
DecSpd
4Green Boxes
event delay
- Reactive behaviors
- Output change triggered by event(s)
- Immediate reaction o changes when e occurs
- Delayed reaction o changes right after e occurs
5Yellow Boxes
- Non-reactive behaviors
- Not directly triggered by events
- Output is a pure function of input behavior(s)
- Output changes when and only when any of the
inputs changes
i1
o
f(i1, i2)
o f(i1, i2)
i2
6Simple RoboCup Controller (SRC)
behavior event delay
0-100
duty cycle
desired speed
SRC
0/1
power
speed
count
IR
0-100
ClkFast
ClkSlow
IncSpd
DecSpd
7SRC in C
init() ds s dc count power 0
onIncSpd() ds onDecSpd() ds--
onIR() s onClkFast() count
count gt 100 ? 0 count 1 power count lt
dc ? 1 0 onClkSlow() dc s lt ds ? dc
1 s gt ds ? dc 1 dc power
count lt dc ? 1 0 s 0
8SRC in C Problem 1
init() ds s dc count power 0
onIncSpd() ds onDecSpd() ds--
onIR() s onClkFast() count
count gt 100 ? 0 count 1 power count lt
dc ? 1 0 onClkSlow() dc s lt ds ? dc
1 s gt ds ? dc 1 dc power
count lt dc ? 1 0 s 0
duty cycle
desired speed
SRC
power
speed
count
9SRC in C Problem 2
init() ds s dc count power 0
onIncSpd() ds onDecSpd() ds--
onIR() s onClkFast() count
count gt 100 ? 0 count 1 power count lt
dc ? 1 0 onClkSlow() dc s lt ds ? dc
1 s gt ds ? dc 1 dc power
count lt dc ? 1 0 s 0
duty cycle
desired speed
SRC
power
speed
count
10Source of the Problems
11SRC in C Problem 3
Change is global!
12Solution Behavior-Oriented View
13Our Contributions
- The E-FRP (Event-Driven Functional Reactive
Programming) language - Fits the model
- Behavior-oriented rather than event-oriented
- Doesnt suffer from the problems C has
- Formal semantics
- An E-FRP compiler
- Generates efficient C code for PIC16C66 MCU
- Provably correct
- Preserves semantics
- Resource bounded target code
- Optimizations
14E-FRP by Examples
- Immediate reaction
- Delayed reaction
- One definition for each block in diagram
- Formal syntax in the paper
- Non-reactive behavior
f(i, o-)
o
i
e
o init c on e gt f(i,o)
i1
f(i1,i2)
f(i, o-)
o
o
i
i2
e
o f(i1,i2)
o init c on e gt f(i,o) later
15What about Semantics?
- Operational semantics
- Input an (infinite) event sequence
- Output an (infinite) store sequence
- On an event, a program yields a store and is
updated by a new program - Details in the paper
- Important design choices
- No user defined events
- No simultaneous events
- Simplifies semantics
- Generates more efficient code
16SRC in Two Languages
- E-FRP
- ds init 0 on IncSpd gt ds1,
- DecSpd gt ds-1
- s init 0 on IR gt s1,
- ClkSlow gt 0 later
- dc init 0 on ClkSlow gt
- if s lt ds then dc1
- else if s gt ds then dc-1
- else dc
- count init 0 on ClkFast gt
- if count gt 100 then 0
- else count1
- power if count lt dc then 1
- else 0
- C
- init()
- ds s dc count power 0
-
- onIncSpd() ds
- onDecSpd() ds--
- onIR() s
- onClkFast()
- count count gt 100 ? 0
- count 1
- power count lt dc ? 1 0
-
- onClkSlow()
- dc s lt ds ? dc 1
17SRC in Two Languages
duty cycle
desired speed
SRC
power
speed
count
- E-FRP
- ds init 0 on IncSpd gt ds1,
- DecSpd gt ds-1
- s init 0 on IR gt s1,
- ClkSlow gt 0 later
- dc init 0 on ClkSlow gt
- if s lt ds then dc1
- else if s gt ds then dc-1
- else dc
- count init 0 on ClkFast gt
- if count gt 100 then 0
- else count1
- power if count lt dc then 1
- else 0
- C
- init()
- ds s dc count power 0
-
- onIncSpd() ds
- onDecSpd() ds--
- onIR() s
- onClkFast()
- count count gt 100 ? 0
- count 1
- power count lt dc ? 1 0
-
- onClkSlow()
- dc s lt ds ? dc 1
18SRC in Two Languages
duty cycle
desired speed
SRC
power
speed
count
- E-FRP
- ds init 0 on IncSpd gt ds1,
- DecSpd gt ds-1
- s init 0 on IR gt s1,
- ClkSlow gt 0 later
- dc init 0 on ClkSlow gt
- if s lt ds then dc1
- else if s gt ds then dc-1
- else dc
- count init 0 on ClkFast gt
- if count gt 100 then 0
- else count1
- power if count lt dc then 1
- else 0
- C
- init()
- ds s dc count power 0
-
- onIncSpd() ds
- onDecSpd() ds--
- onIR() s
- onClkFast()
- count count gt 100 ? 0
- count 1
- power count lt dc ? 1 0
-
- onClkSlow()
- dc s lt ds ? dc 1
19Compile E-FRP to C
- Compilation
- Data representation
- Each behavior variable x in E-FRP ? two global
variable x and x in C - Translation
- For each event in E-FRP
- Collect data dependencies
- Determine order for assignments
- Provably correct
- Optimizations
20Castling
- Uses knowledge about compiler
- Why is castling useful?
- Fewer right-hand-side uses of x
- Easier to eliminate x later
21Unoptimized Target Code for SRC
- Target code before optimization
onIncSpd() ds ds 1 power if count lt dc
then 1 else 0 ds ds power
if count lt dc then 1 else 0 onDecSpd() ds
ds - 1 power if count lt dc then 1 else 0
ds ds power if count lt dc then
1 else 0 onIR() s s 1 power if
count lt dc then 1 else 0 s s
power if count lt dc then 1 else 0
onClkFast() count count gt 100 ? 0
count 1 power count lt dc ? 1 0
count count power count lt dc ? 1
0 onClkSlow() s 0 dc s lt ds ? dc
1 s gt ds ? dc 1 dc power
count lt dc ? 1 0 s s dc dc power
count lt dc ? 1 0
22Optimized Target Code for SRC
- Target code after optimization
onIncSpd() ds ds 1 onDecSpd() ds
ds - 1 onIR() s s 1
onClkFast() count count gt 100 ? 0
count 1 power count lt dc ? 1
0 onClkSlow() dc s lt ds ? dc 1
s gt ds ? dc 1 dc power count lt dc ? 1
0 s 0
23Related Work
- Languages for event-driven systems
Languages for reactive systems
Declarative
Resource-bounded
Fit forevent-drivensystems
Esterel
FRP
RT-FRP
E-FRP
Lustre
24End of Talk
- Video of Yale soccer-bots