functional reactivity: eschewing the imperativepsznhn/g54pdc/lecturenotes/yampa.pdf · functional...
TRANSCRIPT
Functional Reactivity:Eschewing the Imperative
An Overview of Functional ReactiveProgramming in the Context of Yampa
Henrik Nilsson
University of Nottingham, UK
Functional Reactivity:Eschewing the Imperative – p.1/48
Reactive programming (1)Reactive systems :
Functional Reactivity:Eschewing the Imperative – p.2/48
Reactive programming (1)Reactive systems :
• Input arrives incrementally while system isrunning.
Functional Reactivity:Eschewing the Imperative – p.2/48
Reactive programming (1)Reactive systems :
• Input arrives incrementally while system isrunning.
• Output is generated in response to input in aninterleaved and timely fashion.
Functional Reactivity:Eschewing the Imperative – p.2/48
Reactive programming (1)Reactive systems :
• Input arrives incrementally while system isrunning.
• Output is generated in response to input in aninterleaved and timely fashion.
Contrast transformational systems .
Functional Reactivity:Eschewing the Imperative – p.2/48
Reactive programming (1)Reactive systems :
• Input arrives incrementally while system isrunning.
• Output is generated in response to input in aninterleaved and timely fashion.
Contrast transformational systems .
The notions of• time• time-varying values, or signals
are inherent and central to reactive systems.Functional Reactivity:Eschewing the Imperative – p.2/48
Reactive Programming (2)
Reactive systems are• generally concurrent• often parallel• often distributed
Functional Reactivity:Eschewing the Imperative – p.3/48
Reactive Programming (2)
Reactive systems are• generally concurrent• often parallel• often distributed
Thus, besides timeliness, difficulties related todevelopment of concurrent, parallel, anddistributed programming are also inherent.
Functional Reactivity:Eschewing the Imperative – p.3/48
The Synchronous Approach (1)
The “synchronous realisation” (France, 1980s):
If we heed the observation that time-varyingvalues are central to reactive programming and
Functional Reactivity:Eschewing the Imperative – p.4/48
The Synchronous Approach (1)
The “synchronous realisation” (France, 1980s):
If we heed the observation that time-varyingvalues are central to reactive programming and• express systems directly as
transformations of such entities
Functional Reactivity:Eschewing the Imperative – p.4/48
The Synchronous Approach (1)
The “synchronous realisation” (France, 1980s):
If we heed the observation that time-varyingvalues are central to reactive programming and• express systems directly as
transformations of such entities• adopt system-wide logical time,
abstracting away processing delays(hence synchronous )
. . .
Functional Reactivity:Eschewing the Imperative – p.4/48
The Synchronous Approach (2)
. . . then:
Functional Reactivity:Eschewing the Imperative – p.5/48
The Synchronous Approach (2)
. . . then:• systems can be described declaratively
at a very high level of abstraction
Functional Reactivity:Eschewing the Imperative – p.5/48
The Synchronous Approach (2)
. . . then:• systems can be described declaratively
at a very high level of abstraction• simple, deterministic semantics,
facilitates reasoning
Functional Reactivity:Eschewing the Imperative – p.5/48
The Synchronous Approach (2)
. . . then:• systems can be described declaratively
at a very high level of abstraction• simple, deterministic semantics,
facilitates reasoning• many problems related to imperative
idioms for concurrency andsynchronisation simply vanishes.
Functional Reactivity:Eschewing the Imperative – p.5/48
The Synchronous Approach (2)
. . . then:• systems can be described declaratively
at a very high level of abstraction• simple, deterministic semantics,
facilitates reasoning• many problems related to imperative
idioms for concurrency andsynchronisation simply vanishes.
Contrast programming with values at isolatedpoints in time in a fundamentally temporallyagnostic setting.
Functional Reactivity:Eschewing the Imperative – p.5/48
The Synchronous Approach (3)
The synchronous languages were invented inFrance in the 1980s. The first ones were:
• Esterel• Lustre• Signal
Have been very successful; e.g. lots of industrialapplications.
Many new languages and variations since then.
Functional Reactivity:Eschewing the Imperative – p.6/48
Functional Reactive Programming
Functional Reactive Programming (FRP):• Paradigm for reactive, concurrent
programming in purely declarative (functional)setting.
Functional Reactivity:Eschewing the Imperative – p.7/48
Functional Reactive Programming
Functional Reactive Programming (FRP):• Paradigm for reactive, concurrent
programming in purely declarative (functional)setting.
• Originated from Functional ReactiveAnimation (Fran) (Elliott & Hudak).
Functional Reactivity:Eschewing the Imperative – p.7/48
Functional Reactive Programming
Functional Reactive Programming (FRP):• Paradigm for reactive, concurrent
programming in purely declarative (functional)setting.
• Originated from Functional ReactiveAnimation (Fran) (Elliott & Hudak).
• Has evolved in a number of directions andinto different concrete implementations.
Functional Reactivity:Eschewing the Imperative – p.7/48
Functional Reactive Programming
Functional Reactive Programming (FRP):• Paradigm for reactive, concurrent
programming in purely declarative (functional)setting.
• Originated from Functional ReactiveAnimation (Fran) (Elliott & Hudak).
• Has evolved in a number of directions andinto different concrete implementations.
• (Usually) continuous notion of time andadditional support for discrete events.
Functional Reactivity:Eschewing the Imperative – p.7/48
FRP applicationsSome domains where FRP or FRP-like ideashave been used:
• Graphical Animation• Robotics• Vision• GUIs• Hybrid modeling• Video games• Sensor networks• Audio processing and generation• Financial, event-based systems
Functional Reactivity:Eschewing the Imperative – p.8/48
Example: Robotics (1)
[PPDP’02, with Izzet Pembeci and Greg Hager,Johns Hopkins University]
Hardware setup:
Functional Reactivity:Eschewing the Imperative – p.9/48
Example: Robotics (2)
Functional Reactivity:Eschewing the Imperative – p.10/48
Related approaches
FRP related to:• Synchronous languages, like Esterel, Lucid
Synchrone.• Modeling languages, like Simulink, Modelica.
Functional Reactivity:Eschewing the Imperative – p.11/48
Related approaches
FRP related to:• Synchronous languages, like Esterel, Lucid
Synchrone.• Modeling languages, like Simulink, Modelica.
Distinguishing features of FRP:• First class reactive components.• Allows highly dynamic system structure.• Supports hybrid (mixed continuous and
discrete) systems.
Functional Reactivity:Eschewing the Imperative – p.11/48
Yampa
• An FRP system originating at Yale
Functional Reactivity:Eschewing the Imperative – p.12/48
Yampa
• An FRP system originating at Yale• Embedding in Haskell (a Haskell library).
Functional Reactivity:Eschewing the Imperative – p.12/48
Yampa
• An FRP system originating at Yale• Embedding in Haskell (a Haskell library).• Arrows used as the basic structuring
framework.
Functional Reactivity:Eschewing the Imperative – p.12/48
Yampa
• An FRP system originating at Yale• Embedding in Haskell (a Haskell library).• Arrows used as the basic structuring
framework.• Notionally continuous time .
Functional Reactivity:Eschewing the Imperative – p.12/48
Yampa
• An FRP system originating at Yale• Embedding in Haskell (a Haskell library).• Arrows used as the basic structuring
framework.• Notionally continuous time .• Discrete-time signals modelled by
continuous-time signals and an option type,allowing for hybrid systems.
Functional Reactivity:Eschewing the Imperative – p.12/48
Yampa
• An FRP system originating at Yale• Embedding in Haskell (a Haskell library).• Arrows used as the basic structuring
framework.• Notionally continuous time .• Discrete-time signals modelled by
continuous-time signals and an option type,allowing for hybrid systems.
• Advanced switching constructs allows forhighly dynamic system structure.
Functional Reactivity:Eschewing the Imperative – p.12/48
Yampa?
Functional Reactivity:Eschewing the Imperative – p.13/48
Yampa?
Y etA notherM ostlyP ointlessA cronym
Functional Reactivity:Eschewing the Imperative – p.13/48
Yampa?
Y etA notherM ostlyP ointlessA cronym
???
Functional Reactivity:Eschewing the Imperative – p.13/48
Yampa?
Y etA notherM ostlyP ointlessA cronym
???
No . . .
Functional Reactivity:Eschewing the Imperative – p.13/48
Yampa?
Yampa is a river . . .
Functional Reactivity:Eschewing the Imperative – p.13/48
Yampa?
. . . with long calmly flowing sections . . .
Functional Reactivity:Eschewing the Imperative – p.13/48
Yampa?
. . . and abrupt whitewater transitions in between.
A good metaphor for hybrid systems!Functional Reactivity:Eschewing the Imperative – p.13/48
Signal functionsKey concept: functions on signals .
Functional Reactivity:Eschewing the Imperative – p.14/48
Signal functionsKey concept: functions on signals .
Intuition:
Signal α ≈ Time→α
x :: Signal T1y :: Signal T2f :: Signal T1 →Signal T2
Functional Reactivity:Eschewing the Imperative – p.14/48
Signal functionsKey concept: functions on signals .
Intuition:
Signal α ≈ Time→α
x :: Signal T1y :: Signal T2f :: Signal T1 →Signal T2
Additionally: causality requirement.
Functional Reactivity:Eschewing the Imperative – p.14/48
Signal functionsKey concept: functions on signals .
Intuition:
Signal α ≈ Time→α
x :: Signal T1y :: Signal T2f :: Signal T1 →Signal T2
Additionally: causality requirement.
Signal functions are first class entities in Yampa:SF α β ≈ Signal α →Signal β
Functional Reactivity:Eschewing the Imperative – p.14/48
Signal functions and state
Alternative view:
Functional Reactivity:Eschewing the Imperative – p.15/48
Signal functions and state
Alternative view:
Signal functions can encapsulate state .
state(t) summarizes input history x(t′), t′ ∈ [0, t].
Functional Reactivity:Eschewing the Imperative – p.15/48
Signal functions and state
Alternative view:
Signal functions can encapsulate state .
state(t) summarizes input history x(t′), t′ ∈ [0, t].
Functions on signals are either:• Stateful : y(t) depends on x(t) and state(t)
• Stateless : y(t) depends only on x(t)
Functional Reactivity:Eschewing the Imperative – p.15/48
Example: Video tracker
Video trackers are typically stateful signalfunctions:
Functional Reactivity:Eschewing the Imperative – p.16/48
Building systems (1)
How to build systems? Think of a signal functionas a block . Blocks have inputs and outputs andcan be combined into larger blocks. For example,serial composition:
Functional Reactivity:Eschewing the Imperative – p.17/48
Building systems (1)
How to build systems? Think of a signal functionas a block . Blocks have inputs and outputs andcan be combined into larger blocks. For example,serial composition:
A combinator can be defined that captures thisidea:
(>>>) :: SF a b -> SF b c -> SF a c
Functional Reactivity:Eschewing the Imperative – p.17/48
Building systems (2)
But systems can be complex:
Functional Reactivity:Eschewing the Imperative – p.18/48
Building systems (2)
But systems can be complex:
How many and what combinators do we needto be able to describe arbitrary systems?
Functional Reactivity:Eschewing the Imperative – p.18/48
Arrows
Yampa uses John Hughes’ arrow framework:• Abstract data type interface for function-like
types (or “blocks”, if you prefer).
Functional Reactivity:Eschewing the Imperative – p.19/48
Arrows
Yampa uses John Hughes’ arrow framework:• Abstract data type interface for function-like
types (or “blocks”, if you prefer).• Particularly suitable for types representing
process-like computations.
Functional Reactivity:Eschewing the Imperative – p.19/48
Arrows
Yampa uses John Hughes’ arrow framework:• Abstract data type interface for function-like
types (or “blocks”, if you prefer).• Particularly suitable for types representing
process-like computations.• Provides a minimal set of “wiring”
combinators.
Functional Reactivity:Eschewing the Imperative – p.19/48
What is an arrow? (1)
• A type constructor a of arity two.
Functional Reactivity:Eschewing the Imperative – p.20/48
What is an arrow? (1)
• A type constructor a of arity two.• Three operators:
Functional Reactivity:Eschewing the Imperative – p.20/48
What is an arrow? (1)
• A type constructor a of arity two.• Three operators:
- lifting :arr :: (b->c) -> a b c
Functional Reactivity:Eschewing the Imperative – p.20/48
What is an arrow? (1)
• A type constructor a of arity two.• Three operators:
- lifting :arr :: (b->c) -> a b c
- composition :(>>>) :: a b c -> a c d -> a b d
Functional Reactivity:Eschewing the Imperative – p.20/48
What is an arrow? (1)
• A type constructor a of arity two.• Three operators:
- lifting :arr :: (b->c) -> a b c
- composition :(>>>) :: a b c -> a c d -> a b d
- widening :first :: a b c -> a (b,d) (c,d)
Functional Reactivity:Eschewing the Imperative – p.20/48
What is an arrow? (1)
• A type constructor a of arity two.• Three operators:
- lifting :arr :: (b->c) -> a b c
- composition :(>>>) :: a b c -> a c d -> a b d
- widening :first :: a b c -> a (b,d) (c,d)
• A set of algebraic laws that must hold.
Functional Reactivity:Eschewing the Imperative – p.20/48
What is an arrow? (2)
These diagrams convey the general idea:
arr f f >>> g
first f
Functional Reactivity:Eschewing the Imperative – p.21/48
Some arrow laws
(f >>> g) >>> h = f >>> (g >>> h)
arr (f >>> g) = arr f >>> arr g
arr id >>> f = f
f = f >>> arr id
first (arr f) = arr (first f)
first (f >>> g) = first f >>> first g
Functional Reactivity:Eschewing the Imperative – p.22/48
Some arrow laws
(f >>> g) >>> h = f >>> (g >>> h)
arr (f >>> g) = arr f >>> arr g
arr id >>> f = f
f = f >>> arr id
first (arr f) = arr (first f)
first (f >>> g) = first f >>> first g
Being able to use simple algebraic laws likethese greatly facilitates reasoning aboutprograms.
Functional Reactivity:Eschewing the Imperative – p.22/48
The loop combinator
Another important operator is loop : a fixed-pointoperator used to express recursive arrows orfeedback :
loop f
Functional Reactivity:Eschewing the Imperative – p.23/48
The loop combinator
Another important operator is loop : a fixed-pointoperator used to express recursive arrows orfeedback :
loop f
Remarkably, the four combinators arr , >>>,first , and loop suffice for expressing anyconceivable wiring!
Functional Reactivity:Eschewing the Imperative – p.23/48
Some more arrow combinators (1)
second :: Arrow a =>a b c -> a (d,b) (d,c)
( *** ) :: Arrow a =>a b c -> a d e -> a (b,d) (c,e)
(&&&) :: Arrow a =>a b c -> a b d -> a b (c,d)
Functional Reactivity:Eschewing the Imperative – p.24/48
Some more arrow combinators (2)
As diagrams:
second ff *** g
f &&& gFunctional Reactivity:Eschewing the Imperative – p.25/48
Example: A Simple NetworkA simple network:
a1, a2, a3 :: A Double Double
One way to express it using arrow combinators:circuit_v1 :: A Double Doublecircuit_v1 = (a1 &&& arr id)
>>> (a2 *** a3)>>> arr (uncurry (+))
Functional Reactivity:Eschewing the Imperative – p.26/48
The arrow do notation (1)Using the basic combinators directly can becumbersome. Ross Paterson’s do -notation forarrows provides a convenient alternative. Onlysyntactic sugar !
proc pat -> do [ rec ]pat1 <- sfexp1 -< exp1
pat2 <- sfexp2 -< exp2
. . .pat
n<- sfexp
n-< exp
n
returnA -< exp
Also: let pat = exp ≡ pat <- arr id -< expFunctional Reactivity:Eschewing the Imperative – p.27/48
The arrow do notation (2)
Let us redo the example using this notation:
circuit_v4 :: A Double Doublecircuit_v4 = proc x -> do
y1 <- a1 -< xy2 <- a2 -< y1y3 <- a3 -< xreturnA -< y2 + y3
Functional Reactivity:Eschewing the Imperative – p.28/48
Yampa and Arrows
The Yampa signal function type is an arrow.
Signal function instances of the corecombinators:
• arr :: (a -> b) -> SF a b
• >>> :: SF a b -> SF b c -> SF a c
• first :: SF a b -> SF (a,c) (b,c)
• loop :: SF (a,c) (b,c) -> SF a b
Functional Reactivity:Eschewing the Imperative – p.29/48
Some further basic signal functions
• identity :: SF a aidentity = arr id
Functional Reactivity:Eschewing the Imperative – p.30/48
Some further basic signal functions
• identity :: SF a aidentity = arr id
• constant :: b -> SF a bconstant b = arr (const b)
Functional Reactivity:Eschewing the Imperative – p.30/48
Some further basic signal functions
• identity :: SF a aidentity = arr id
• constant :: b -> SF a bconstant b = arr (const b)
• integral :: VectorSpace a s=>SF a a
Functional Reactivity:Eschewing the Imperative – p.30/48
Some further basic signal functions
• identity :: SF a aidentity = arr id
• constant :: b -> SF a bconstant b = arr (const b)
• integral :: VectorSpace a s=>SF a a
• time :: SF a Timetime = constant 1.0 >>> integral
Functional Reactivity:Eschewing the Imperative – p.30/48
Some further basic signal functions
• identity :: SF a aidentity = arr id
• constant :: b -> SF a bconstant b = arr (const b)
• integral :: VectorSpace a s=>SF a a
• time :: SF a Timetime = constant 1.0 >>> integral
• (^<<) :: (b->c) -> SF a b -> SF a cf (^<<) sf = sf >>> arr f
Functional Reactivity:Eschewing the Imperative – p.30/48
A bouncing ball
y = y0 +
∫v dt
v = v0 +
∫−9.81
On impact:
v = −v(t−)
(fully elastic collision)
Functional Reactivity:Eschewing the Imperative – p.31/48
Modelling the bouncing ball: part 1
Free-falling ball:
type Pos = Double
type Vel = Double
fallingBall ::
Pos -> Vel -> SF () (Pos, Vel)
fallingBall y0 v0 = proc () -> do
v <- (v0 +) ˆ<< integral -< -9.81
y <- (y0 +) ˆ<< integral -< v
returnA -< (y, v)
Functional Reactivity:Eschewing the Imperative – p.32/48
Events
Conceptually, discrete-time signals are onlydefined at discrete points in time, oftenassociated with the occurrence of some event .
Functional Reactivity:Eschewing the Imperative – p.33/48
Events
Conceptually, discrete-time signals are onlydefined at discrete points in time, oftenassociated with the occurrence of some event .
Yampa models discrete-time signals by lifting therange of continuous-time signals:
data Event a = NoEvent | Event a
Functional Reactivity:Eschewing the Imperative – p.33/48
Events
Conceptually, discrete-time signals are onlydefined at discrete points in time, oftenassociated with the occurrence of some event .
Yampa models discrete-time signals by lifting therange of continuous-time signals:
data Event a = NoEvent | Event a
Discrete-time signal = Signal (Event α).
Functional Reactivity:Eschewing the Imperative – p.33/48
Events
Conceptually, discrete-time signals are onlydefined at discrete points in time, oftenassociated with the occurrence of some event .
Yampa models discrete-time signals by lifting therange of continuous-time signals:
data Event a = NoEvent | Event a
Discrete-time signal = Signal (Event α).
Associating information with an eventoccurrence:
tag :: Event a -> b -> Event bFunctional Reactivity:Eschewing the Imperative – p.33/48
Modelling the bouncing ball: part 2
Detecting when the ball goes through the floor:
fallingBall’ ::
Pos -> Vel
-> SF () ((Pos,Vel), Event (Pos,Vel))
fallingBall’ y0 v0 = proc () -> do
yv@(y, _) <- fallingBall y0 v0 -< ()
hit <- edge -< y <= 0
returnA -< (yv, hit ‘tag‘ yv)
Functional Reactivity:Eschewing the Imperative – p.34/48
Switching
Q: How and when do signal functions “start”?
Functional Reactivity:Eschewing the Imperative – p.35/48
Switching
Q: How and when do signal functions “start”?
A: • Switchers “apply” a signal functions to itsinput signal at some point in time.
Functional Reactivity:Eschewing the Imperative – p.35/48
Switching
Q: How and when do signal functions “start”?
A: • Switchers “apply” a signal functions to itsinput signal at some point in time.
• This creates a “running” signal functioninstance .
Functional Reactivity:Eschewing the Imperative – p.35/48
Switching
Q: How and when do signal functions “start”?
A: • Switchers “apply” a signal functions to itsinput signal at some point in time.
• This creates a “running” signal functioninstance .
• The new signal function instance oftenreplaces the previously running instance.
Functional Reactivity:Eschewing the Imperative – p.35/48
Switching
Q: How and when do signal functions “start”?
A: • Switchers “apply” a signal functions to itsinput signal at some point in time.
• This creates a “running” signal functioninstance .
• The new signal function instance oftenreplaces the previously running instance.
Switchers thus allow systems with varyingstructure to be described.
Functional Reactivity:Eschewing the Imperative – p.35/48
The basic switch
Idea:• Allows one signal function to be replaced by
another.• Switching takes place on the first occurrence
of the switching event source.
switch ::
SF a (b, Event c)
-> (c -> SF a b)
-> SF a b
Functional Reactivity:Eschewing the Imperative – p.36/48
The basic switch
Idea:• Allows one signal function to be replaced by
another.• Switching takes place on the first occurrence
of the switching event source.
switch :: Initial SF with event source
SF a (b, Event c)
-> (c -> SF a b)
-> SF a b
Functional Reactivity:Eschewing the Imperative – p.36/48
The basic switch
Idea:• Allows one signal function to be replaced by
another.• Switching takes place on the first occurrence
of the switching event source.
switch :: Function yielding SF to switch into
SF a (b, Event c)
-> (c -> SF a b)
-> SF a b
Functional Reactivity:Eschewing the Imperative – p.36/48
Modelling the bouncing ball: part 3
Making the ball bounce:
bouncingBall :: Pos -> SF () (Pos, Vel)
bouncingBall y0 = bbAux y0 0.0
where
bbAux y0 v0 =
switch (fallingBall’ y0 v0) $ \(y,v) ->
bbAux y (-v)
Functional Reactivity:Eschewing the Imperative – p.37/48
Simulation of bouncing ball
-5
-4
-3
-2
-1
0
1
2
3
4
5
0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5
ydy/dt
Functional Reactivity:Eschewing the Imperative – p.38/48
Highly dynamic system structure?
Basic switch allows one signal function to bereplaced by another.
Functional Reactivity:Eschewing the Imperative – p.39/48
Highly dynamic system structure?
Basic switch allows one signal function to bereplaced by another.
• What about more general structural changes?
Functional Reactivity:Eschewing the Imperative – p.39/48
Highly dynamic system structure?
Basic switch allows one signal function to bereplaced by another.
• What about more general structural changes?
• What about state?
Functional Reactivity:Eschewing the Imperative – p.39/48
Dynamic signal function collections
Idea:• Switch over collections of signal functions.• On event, “freeze” running signal functions
into collection of signal function continuations ,preserving encapsulated state .
• Modify collection as needed and switch back in.
Functional Reactivity:Eschewing the Imperative – p.40/48
Example: Space Invaders
Functional Reactivity:Eschewing the Imperative – p.41/48
Overall game structure
gun
alien
bullet
alien
...
killOrSpawn
ObjInput ObjOutput
} [ObjectOutput]
dpSwitch
route
Functional Reactivity:Eschewing the Imperative – p.42/48
Describing the alien behavior (1)
type Object = SF ObjInput ObjOutput
alien :: RandomGen g =>
g -> Position2 -> Velocity -> Object
alien g p0 vyd = proc oi -> do
rec
-- Pick a desired horizontal position
rx <- noiseR (xMin, xMax) g -< ()
smpl <- occasionally g 5 () -< ()
xd <- hold (point2X p0) -< smpl ‘tag‘ rx
...
Functional Reactivity:Eschewing the Imperative – p.43/48
Describing the alien behavior (2)
...
-- Controller
let axd = 5 * (xd - point2X p)
- 3 * (vector2X v)
ayd = 20 * (vyd - (vector2Y v))
ad = vector2 axd ayd
h = vector2Theta ad
...
Functional Reactivity:Eschewing the Imperative – p.44/48
Describing the alien behavior (3)
...
-- Physics
let a = vector2Polar
(min alienAccMax
(vector2Rho ad))
h
vp <- iPre v0 -< v
ffi <- forceField -< (p, vp)
v <- (v0 ˆ+ˆ) ˆ<< impulseIntegral
-< (gravity ˆ+ˆ a, ffi)
p <- (p0 .+ˆ) ˆ<< integral -< v
...Functional Reactivity:Eschewing the Imperative – p.45/48
Describing the alien behavior (4)
...
-- Shields
sl <- shield -< oiHit oi
die <- edge -< sl <= 0
returnA -< ObjOutput {
ooObsObjState = oosAlien p h v,
ooKillReq = die,
ooSpawnReq = noEvent
}
where
v0 = zeroVectorFunctional Reactivity:Eschewing the Imperative – p.46/48
State in alien
Each of the following signal functions used inalien encapsulate state:
• noiseR
• occasionally
• hold
• iPre
• forceField
• impulseIntegral
• integral
• shield
• edge
Functional Reactivity:Eschewing the Imperative – p.47/48
Why not imperative, then?
If state is so important, why not stick toimperative/object-oriented programming wherewe have “state for free”?
Functional Reactivity:Eschewing the Imperative – p.48/48
Why not imperative, then?
If state is so important, why not stick toimperative/object-oriented programming wherewe have “state for free”?
• Advantages of declarative programmingretained:- High abstraction level.- Referential transparency, algebraic laws:
formal reasoning is simpler.
Functional Reactivity:Eschewing the Imperative – p.48/48
Why not imperative, then?
If state is so important, why not stick toimperative/object-oriented programming wherewe have “state for free”?
• Advantages of declarative programmingretained:- High abstraction level.- Referential transparency, algebraic laws:
formal reasoning is simpler.• Synchronous approach avoids
“event-call-back soup”, meaning robust,easy-to-understand semantics.
Functional Reactivity:Eschewing the Imperative – p.48/48