a brief introduction to functional reactive …psznhn/fopad2007/talks/nhn-fopad2007.pdffunctional...
Post on 27-May-2020
22 Views
Preview:
TRANSCRIPT
A Brief Introduction toFunctional Reactive Programming
and YampaFoP Away Day 17 January 2007
Henrik Nilsson
School of Computer Science and Information Technology
University of Nottingham, UK
FoPAD: Brief Introduction to FRP & Yampa – p.1/14
Functional Reactive Programming
What is Functional Reactive Programming (FRP)?• Umbrella-term for functional approach to
programming reactive systems.
FoPAD: Brief Introduction to FRP & Yampa – p.2/14
Functional Reactive Programming
What is Functional Reactive Programming (FRP)?• Umbrella-term for functional approach to
programming reactive systems.• Originated from Functional Reactive
Animation (Fran) (Elliott & Hudak).
FoPAD: Brief Introduction to FRP & Yampa – p.2/14
Functional Reactive Programming
What is Functional Reactive Programming (FRP)?• Umbrella-term for functional approach to
programming reactive systems.• Originated from Functional Reactive
Animation (Fran) (Elliott & Hudak).• Has evolved in a number of directions and
into different concrete implementations.
FoPAD: Brief Introduction to FRP & Yampa – p.2/14
Functional Reactive Programming
What is Functional Reactive Programming (FRP)?• Umbrella-term for functional approach to
programming reactive systems.• Originated from Functional Reactive
Animation (Fran) (Elliott & Hudak).• Has evolved in a number of directions and
into different concrete implementations.• Yampa: An FRP implementation in the form
of a Haskell combinator library , a.k.a.Domain-Specific Embedded Language(DSEL).
FoPAD: Brief Introduction to FRP & Yampa – p.2/14
Signal functions
Key concept: functions on signals .
FoPAD: Brief Introduction to FRP & Yampa – p.3/14
Signal functions
Key concept: functions on signals .
Intuition:
Signal α ≈ Time → α
x :: Signal T1y :: Signal T2SF α β ≈ Signal α → Signal β
f :: SF T1 T2
FoPAD: Brief Introduction to FRP & Yampa – p.3/14
Signal functions
Key concept: functions on signals .
Intuition:
Signal α ≈ Time → α
x :: Signal T1y :: Signal T2SF α β ≈ Signal α → Signal β
f :: SF T1 T2
Additionally, causality required: output at time t
must be determined by input on interval [0, t].FoPAD: Brief Introduction to FRP & Yampa – p.3/14
Signal functions and state
Alternative view:
FoPAD: Brief Introduction to FRP & Yampa – p.4/14
Signal functions and state
Alternative view:
Signal functions can encapsulate state .
state(t) summarizes input history x(t′), t′ ∈ [0, t].
FoPAD: Brief Introduction to FRP & Yampa – p.4/14
Signal functions and state
Alternative view:
Signal functions can encapsulate state .
state(t) summarizes input history x(t′), t′ ∈ [0, t].
From this perspective, signal functions are:• stateful if y(t) depends on x(t) and state(t)
• stateless if y(t) depends only on x(t)
FoPAD: Brief Introduction to FRP & Yampa – p.4/14
Programming with signal functions
In Yampa, systems are described by combiningsignal functions (forming new signal functions).
FoPAD: Brief Introduction to FRP & Yampa – p.5/14
Programming with signal functions
In Yampa, systems are described by combiningsignal functions (forming new signal functions).
For example, serial composition:
FoPAD: Brief Introduction to FRP & Yampa – p.5/14
Programming with signal functions
In Yampa, systems are described by combiningsignal functions (forming new signal functions).
For example, serial composition:
A combinator can be defined that captures thisidea:
(≫) :: SF a b → SF b c → SF a c
FoPAD: Brief Introduction to FRP & Yampa – p.5/14
Programming with signal functions (2)
What about larger networks?How many combinators are needed?
FoPAD: Brief Introduction to FRP & Yampa – p.6/14
Programming with signal functions (2)
What about larger networks?How many combinators are needed?
John Hughes’s Arrow framework provides agood answer!
FoPAD: Brief Introduction to FRP & Yampa – p.6/14
The Arrow framework (1)
These diagrams convey the general idea:
arrf≫
firstf loopf
first :: SF a b → SF (a, c) (b, c)
loop :: SF (a, c) (b, c)→ SF a b
FoPAD: Brief Introduction to FRP & Yampa – p.7/14
The Arrow framework (2)
Some derived combinators:
secondff ∗∗∗ g
f&&&gFoPAD: Brief Introduction to FRP & Yampa – p.8/14
Example: Constructing a network
FoPAD: Brief Introduction to FRP & Yampa – p.9/14
Example: Constructing a network
FoPAD: Brief Introduction to FRP & Yampa – p.9/14
Example: Constructing a network
loop (arr (λ(x , y)→ ((x , y), x ))
≫ (fst f
≫ (arr (λ(x , y)→ (x , (x , y))) ≫ (g ∗∗∗ h))))
FoPAD: Brief Introduction to FRP & Yampa – p.9/14
The Arrow notation
FoPAD: Brief Introduction to FRP & Yampa – p.10/14
The Arrow notation
FoPAD: Brief Introduction to FRP & Yampa – p.10/14
The Arrow notation
proc x → do
rec
u ← f −≺ (x , v)
y ← g−≺ u
v ← h−≺ (u, x )
returnA−≺ y
FoPAD: Brief Introduction to FRP & Yampa – p.10/14
How does it work?
• Essentially:
newtype SF a b =
SF (DeltaTime → a → (SF a b, b))
FoPAD: Brief Introduction to FRP & Yampa – p.11/14
How does it work?
• Essentially:
newtype SF a b =
SF (DeltaTime → a → (SF a b, b))
• A top-level loop, reactimate , drives thecomputation.
FoPAD: Brief Introduction to FRP & Yampa – p.11/14
How does it work?
• Essentially:
newtype SF a b =
SF (DeltaTime → a → (SF a b, b))
• A top-level loop, reactimate , drives thecomputation.
Note that the system representation in principleis reconstructed at every time step.
FoPAD: Brief Introduction to FRP & Yampa – p.11/14
Related languages and paradigms
FRP/Yampa related to:• Synchronous dataflow languages, like
Esterel, Lucid Synchrone.
FoPAD: Brief Introduction to FRP & Yampa – p.12/14
Related languages and paradigms
FRP/Yampa related to:• Synchronous dataflow languages, like
Esterel, Lucid Synchrone.• Modeling languages, like Simulink, Modelica.
FoPAD: Brief Introduction to FRP & Yampa – p.12/14
What makes Yampa interesting?
• First class reactive components (signalfunctions).
FoPAD: Brief Introduction to FRP & Yampa – p.13/14
What makes Yampa interesting?
• First class reactive components (signalfunctions).
• Supports hybrid (mixed continuous anddiscrete time) systems: option type Eventrepresents discrete-time signals.
FoPAD: Brief Introduction to FRP & Yampa – p.13/14
What makes Yampa interesting?
• First class reactive components (signalfunctions).
• Supports hybrid (mixed continuous anddiscrete time) systems: option type Eventrepresents discrete-time signals.
• Supports dynamic system structure throughswitching combinators :
FoPAD: Brief Introduction to FRP & Yampa – p.13/14
Example: Space Invaders
FoPAD: Brief Introduction to FRP & Yampa – p.14/14
top related