functional reactivity: eschewing the imperativepsznhn/g54pdc/lecturenotes/yampa.pdf · functional...
TRANSCRIPT
![Page 1: Functional Reactivity: Eschewing the Imperativepsznhn/G54PDC/LectureNotes/yampa.pdf · Functional Reactivity: Eschewing the Imperative An Overview of Functional Reactive Programming](https://reader031.vdocuments.us/reader031/viewer/2022020316/5b5a4d147f8b9a905c8ba993/html5/thumbnails/1.jpg)
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
![Page 2: Functional Reactivity: Eschewing the Imperativepsznhn/G54PDC/LectureNotes/yampa.pdf · Functional Reactivity: Eschewing the Imperative An Overview of Functional Reactive Programming](https://reader031.vdocuments.us/reader031/viewer/2022020316/5b5a4d147f8b9a905c8ba993/html5/thumbnails/2.jpg)
Reactive programming (1)Reactive systems :
Functional Reactivity:Eschewing the Imperative – p.2/48
![Page 3: Functional Reactivity: Eschewing the Imperativepsznhn/G54PDC/LectureNotes/yampa.pdf · Functional Reactivity: Eschewing the Imperative An Overview of Functional Reactive Programming](https://reader031.vdocuments.us/reader031/viewer/2022020316/5b5a4d147f8b9a905c8ba993/html5/thumbnails/3.jpg)
Reactive programming (1)Reactive systems :
• Input arrives incrementally while system isrunning.
Functional Reactivity:Eschewing the Imperative – p.2/48
![Page 4: Functional Reactivity: Eschewing the Imperativepsznhn/G54PDC/LectureNotes/yampa.pdf · Functional Reactivity: Eschewing the Imperative An Overview of Functional Reactive Programming](https://reader031.vdocuments.us/reader031/viewer/2022020316/5b5a4d147f8b9a905c8ba993/html5/thumbnails/4.jpg)
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
![Page 5: Functional Reactivity: Eschewing the Imperativepsznhn/G54PDC/LectureNotes/yampa.pdf · Functional Reactivity: Eschewing the Imperative An Overview of Functional Reactive Programming](https://reader031.vdocuments.us/reader031/viewer/2022020316/5b5a4d147f8b9a905c8ba993/html5/thumbnails/5.jpg)
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
![Page 6: Functional Reactivity: Eschewing the Imperativepsznhn/G54PDC/LectureNotes/yampa.pdf · Functional Reactivity: Eschewing the Imperative An Overview of Functional Reactive Programming](https://reader031.vdocuments.us/reader031/viewer/2022020316/5b5a4d147f8b9a905c8ba993/html5/thumbnails/6.jpg)
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
![Page 7: Functional Reactivity: Eschewing the Imperativepsznhn/G54PDC/LectureNotes/yampa.pdf · Functional Reactivity: Eschewing the Imperative An Overview of Functional Reactive Programming](https://reader031.vdocuments.us/reader031/viewer/2022020316/5b5a4d147f8b9a905c8ba993/html5/thumbnails/7.jpg)
Reactive Programming (2)
Reactive systems are• generally concurrent• often parallel• often distributed
Functional Reactivity:Eschewing the Imperative – p.3/48
![Page 8: Functional Reactivity: Eschewing the Imperativepsznhn/G54PDC/LectureNotes/yampa.pdf · Functional Reactivity: Eschewing the Imperative An Overview of Functional Reactive Programming](https://reader031.vdocuments.us/reader031/viewer/2022020316/5b5a4d147f8b9a905c8ba993/html5/thumbnails/8.jpg)
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
![Page 9: Functional Reactivity: Eschewing the Imperativepsznhn/G54PDC/LectureNotes/yampa.pdf · Functional Reactivity: Eschewing the Imperative An Overview of Functional Reactive Programming](https://reader031.vdocuments.us/reader031/viewer/2022020316/5b5a4d147f8b9a905c8ba993/html5/thumbnails/9.jpg)
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
![Page 10: Functional Reactivity: Eschewing the Imperativepsznhn/G54PDC/LectureNotes/yampa.pdf · Functional Reactivity: Eschewing the Imperative An Overview of Functional Reactive Programming](https://reader031.vdocuments.us/reader031/viewer/2022020316/5b5a4d147f8b9a905c8ba993/html5/thumbnails/10.jpg)
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
![Page 11: Functional Reactivity: Eschewing the Imperativepsznhn/G54PDC/LectureNotes/yampa.pdf · Functional Reactivity: Eschewing the Imperative An Overview of Functional Reactive Programming](https://reader031.vdocuments.us/reader031/viewer/2022020316/5b5a4d147f8b9a905c8ba993/html5/thumbnails/11.jpg)
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
![Page 12: Functional Reactivity: Eschewing the Imperativepsznhn/G54PDC/LectureNotes/yampa.pdf · Functional Reactivity: Eschewing the Imperative An Overview of Functional Reactive Programming](https://reader031.vdocuments.us/reader031/viewer/2022020316/5b5a4d147f8b9a905c8ba993/html5/thumbnails/12.jpg)
The Synchronous Approach (2)
. . . then:
Functional Reactivity:Eschewing the Imperative – p.5/48
![Page 13: Functional Reactivity: Eschewing the Imperativepsznhn/G54PDC/LectureNotes/yampa.pdf · Functional Reactivity: Eschewing the Imperative An Overview of Functional Reactive Programming](https://reader031.vdocuments.us/reader031/viewer/2022020316/5b5a4d147f8b9a905c8ba993/html5/thumbnails/13.jpg)
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
![Page 14: Functional Reactivity: Eschewing the Imperativepsznhn/G54PDC/LectureNotes/yampa.pdf · Functional Reactivity: Eschewing the Imperative An Overview of Functional Reactive Programming](https://reader031.vdocuments.us/reader031/viewer/2022020316/5b5a4d147f8b9a905c8ba993/html5/thumbnails/14.jpg)
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
![Page 15: Functional Reactivity: Eschewing the Imperativepsznhn/G54PDC/LectureNotes/yampa.pdf · Functional Reactivity: Eschewing the Imperative An Overview of Functional Reactive Programming](https://reader031.vdocuments.us/reader031/viewer/2022020316/5b5a4d147f8b9a905c8ba993/html5/thumbnails/15.jpg)
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
![Page 16: Functional Reactivity: Eschewing the Imperativepsznhn/G54PDC/LectureNotes/yampa.pdf · Functional Reactivity: Eschewing the Imperative An Overview of Functional Reactive Programming](https://reader031.vdocuments.us/reader031/viewer/2022020316/5b5a4d147f8b9a905c8ba993/html5/thumbnails/16.jpg)
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
![Page 17: Functional Reactivity: Eschewing the Imperativepsznhn/G54PDC/LectureNotes/yampa.pdf · Functional Reactivity: Eschewing the Imperative An Overview of Functional Reactive Programming](https://reader031.vdocuments.us/reader031/viewer/2022020316/5b5a4d147f8b9a905c8ba993/html5/thumbnails/17.jpg)
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
![Page 18: Functional Reactivity: Eschewing the Imperativepsznhn/G54PDC/LectureNotes/yampa.pdf · Functional Reactivity: Eschewing the Imperative An Overview of Functional Reactive Programming](https://reader031.vdocuments.us/reader031/viewer/2022020316/5b5a4d147f8b9a905c8ba993/html5/thumbnails/18.jpg)
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
![Page 19: Functional Reactivity: Eschewing the Imperativepsznhn/G54PDC/LectureNotes/yampa.pdf · Functional Reactivity: Eschewing the Imperative An Overview of Functional Reactive Programming](https://reader031.vdocuments.us/reader031/viewer/2022020316/5b5a4d147f8b9a905c8ba993/html5/thumbnails/19.jpg)
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
![Page 20: Functional Reactivity: Eschewing the Imperativepsznhn/G54PDC/LectureNotes/yampa.pdf · Functional Reactivity: Eschewing the Imperative An Overview of Functional Reactive Programming](https://reader031.vdocuments.us/reader031/viewer/2022020316/5b5a4d147f8b9a905c8ba993/html5/thumbnails/20.jpg)
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
![Page 21: Functional Reactivity: Eschewing the Imperativepsznhn/G54PDC/LectureNotes/yampa.pdf · Functional Reactivity: Eschewing the Imperative An Overview of Functional Reactive Programming](https://reader031.vdocuments.us/reader031/viewer/2022020316/5b5a4d147f8b9a905c8ba993/html5/thumbnails/21.jpg)
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
![Page 22: Functional Reactivity: Eschewing the Imperativepsznhn/G54PDC/LectureNotes/yampa.pdf · Functional Reactivity: Eschewing the Imperative An Overview of Functional Reactive Programming](https://reader031.vdocuments.us/reader031/viewer/2022020316/5b5a4d147f8b9a905c8ba993/html5/thumbnails/22.jpg)
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
![Page 23: Functional Reactivity: Eschewing the Imperativepsznhn/G54PDC/LectureNotes/yampa.pdf · Functional Reactivity: Eschewing the Imperative An Overview of Functional Reactive Programming](https://reader031.vdocuments.us/reader031/viewer/2022020316/5b5a4d147f8b9a905c8ba993/html5/thumbnails/23.jpg)
Example: Robotics (1)
[PPDP’02, with Izzet Pembeci and Greg Hager,Johns Hopkins University]
Hardware setup:
Functional Reactivity:Eschewing the Imperative – p.9/48
![Page 24: Functional Reactivity: Eschewing the Imperativepsznhn/G54PDC/LectureNotes/yampa.pdf · Functional Reactivity: Eschewing the Imperative An Overview of Functional Reactive Programming](https://reader031.vdocuments.us/reader031/viewer/2022020316/5b5a4d147f8b9a905c8ba993/html5/thumbnails/24.jpg)
Example: Robotics (2)
Functional Reactivity:Eschewing the Imperative – p.10/48
![Page 25: Functional Reactivity: Eschewing the Imperativepsznhn/G54PDC/LectureNotes/yampa.pdf · Functional Reactivity: Eschewing the Imperative An Overview of Functional Reactive Programming](https://reader031.vdocuments.us/reader031/viewer/2022020316/5b5a4d147f8b9a905c8ba993/html5/thumbnails/25.jpg)
Related approaches
FRP related to:• Synchronous languages, like Esterel, Lucid
Synchrone.• Modeling languages, like Simulink, Modelica.
Functional Reactivity:Eschewing the Imperative – p.11/48
![Page 26: Functional Reactivity: Eschewing the Imperativepsznhn/G54PDC/LectureNotes/yampa.pdf · Functional Reactivity: Eschewing the Imperative An Overview of Functional Reactive Programming](https://reader031.vdocuments.us/reader031/viewer/2022020316/5b5a4d147f8b9a905c8ba993/html5/thumbnails/26.jpg)
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
![Page 27: Functional Reactivity: Eschewing the Imperativepsznhn/G54PDC/LectureNotes/yampa.pdf · Functional Reactivity: Eschewing the Imperative An Overview of Functional Reactive Programming](https://reader031.vdocuments.us/reader031/viewer/2022020316/5b5a4d147f8b9a905c8ba993/html5/thumbnails/27.jpg)
Yampa
• An FRP system originating at Yale
Functional Reactivity:Eschewing the Imperative – p.12/48
![Page 28: Functional Reactivity: Eschewing the Imperativepsznhn/G54PDC/LectureNotes/yampa.pdf · Functional Reactivity: Eschewing the Imperative An Overview of Functional Reactive Programming](https://reader031.vdocuments.us/reader031/viewer/2022020316/5b5a4d147f8b9a905c8ba993/html5/thumbnails/28.jpg)
Yampa
• An FRP system originating at Yale• Embedding in Haskell (a Haskell library).
Functional Reactivity:Eschewing the Imperative – p.12/48
![Page 29: Functional Reactivity: Eschewing the Imperativepsznhn/G54PDC/LectureNotes/yampa.pdf · Functional Reactivity: Eschewing the Imperative An Overview of Functional Reactive Programming](https://reader031.vdocuments.us/reader031/viewer/2022020316/5b5a4d147f8b9a905c8ba993/html5/thumbnails/29.jpg)
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
![Page 30: Functional Reactivity: Eschewing the Imperativepsznhn/G54PDC/LectureNotes/yampa.pdf · Functional Reactivity: Eschewing the Imperative An Overview of Functional Reactive Programming](https://reader031.vdocuments.us/reader031/viewer/2022020316/5b5a4d147f8b9a905c8ba993/html5/thumbnails/30.jpg)
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
![Page 31: Functional Reactivity: Eschewing the Imperativepsznhn/G54PDC/LectureNotes/yampa.pdf · Functional Reactivity: Eschewing the Imperative An Overview of Functional Reactive Programming](https://reader031.vdocuments.us/reader031/viewer/2022020316/5b5a4d147f8b9a905c8ba993/html5/thumbnails/31.jpg)
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
![Page 32: Functional Reactivity: Eschewing the Imperativepsznhn/G54PDC/LectureNotes/yampa.pdf · Functional Reactivity: Eschewing the Imperative An Overview of Functional Reactive Programming](https://reader031.vdocuments.us/reader031/viewer/2022020316/5b5a4d147f8b9a905c8ba993/html5/thumbnails/32.jpg)
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
![Page 33: Functional Reactivity: Eschewing the Imperativepsznhn/G54PDC/LectureNotes/yampa.pdf · Functional Reactivity: Eschewing the Imperative An Overview of Functional Reactive Programming](https://reader031.vdocuments.us/reader031/viewer/2022020316/5b5a4d147f8b9a905c8ba993/html5/thumbnails/33.jpg)
Yampa?
Functional Reactivity:Eschewing the Imperative – p.13/48
![Page 34: Functional Reactivity: Eschewing the Imperativepsznhn/G54PDC/LectureNotes/yampa.pdf · Functional Reactivity: Eschewing the Imperative An Overview of Functional Reactive Programming](https://reader031.vdocuments.us/reader031/viewer/2022020316/5b5a4d147f8b9a905c8ba993/html5/thumbnails/34.jpg)
Yampa?
Y etA notherM ostlyP ointlessA cronym
Functional Reactivity:Eschewing the Imperative – p.13/48
![Page 35: Functional Reactivity: Eschewing the Imperativepsznhn/G54PDC/LectureNotes/yampa.pdf · Functional Reactivity: Eschewing the Imperative An Overview of Functional Reactive Programming](https://reader031.vdocuments.us/reader031/viewer/2022020316/5b5a4d147f8b9a905c8ba993/html5/thumbnails/35.jpg)
Yampa?
Y etA notherM ostlyP ointlessA cronym
???
Functional Reactivity:Eschewing the Imperative – p.13/48
![Page 36: Functional Reactivity: Eschewing the Imperativepsznhn/G54PDC/LectureNotes/yampa.pdf · Functional Reactivity: Eschewing the Imperative An Overview of Functional Reactive Programming](https://reader031.vdocuments.us/reader031/viewer/2022020316/5b5a4d147f8b9a905c8ba993/html5/thumbnails/36.jpg)
Yampa?
Y etA notherM ostlyP ointlessA cronym
???
No . . .
Functional Reactivity:Eschewing the Imperative – p.13/48
![Page 37: Functional Reactivity: Eschewing the Imperativepsznhn/G54PDC/LectureNotes/yampa.pdf · Functional Reactivity: Eschewing the Imperative An Overview of Functional Reactive Programming](https://reader031.vdocuments.us/reader031/viewer/2022020316/5b5a4d147f8b9a905c8ba993/html5/thumbnails/37.jpg)
Yampa?
Yampa is a river . . .
Functional Reactivity:Eschewing the Imperative – p.13/48
![Page 38: Functional Reactivity: Eschewing the Imperativepsznhn/G54PDC/LectureNotes/yampa.pdf · Functional Reactivity: Eschewing the Imperative An Overview of Functional Reactive Programming](https://reader031.vdocuments.us/reader031/viewer/2022020316/5b5a4d147f8b9a905c8ba993/html5/thumbnails/38.jpg)
Yampa?
. . . with long calmly flowing sections . . .
Functional Reactivity:Eschewing the Imperative – p.13/48
![Page 39: Functional Reactivity: Eschewing the Imperativepsznhn/G54PDC/LectureNotes/yampa.pdf · Functional Reactivity: Eschewing the Imperative An Overview of Functional Reactive Programming](https://reader031.vdocuments.us/reader031/viewer/2022020316/5b5a4d147f8b9a905c8ba993/html5/thumbnails/39.jpg)
Yampa?
. . . and abrupt whitewater transitions in between.
A good metaphor for hybrid systems!Functional Reactivity:Eschewing the Imperative – p.13/48
![Page 40: Functional Reactivity: Eschewing the Imperativepsznhn/G54PDC/LectureNotes/yampa.pdf · Functional Reactivity: Eschewing the Imperative An Overview of Functional Reactive Programming](https://reader031.vdocuments.us/reader031/viewer/2022020316/5b5a4d147f8b9a905c8ba993/html5/thumbnails/40.jpg)
Signal functionsKey concept: functions on signals .
Functional Reactivity:Eschewing the Imperative – p.14/48
![Page 41: Functional Reactivity: Eschewing the Imperativepsznhn/G54PDC/LectureNotes/yampa.pdf · Functional Reactivity: Eschewing the Imperative An Overview of Functional Reactive Programming](https://reader031.vdocuments.us/reader031/viewer/2022020316/5b5a4d147f8b9a905c8ba993/html5/thumbnails/41.jpg)
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
![Page 42: Functional Reactivity: Eschewing the Imperativepsznhn/G54PDC/LectureNotes/yampa.pdf · Functional Reactivity: Eschewing the Imperative An Overview of Functional Reactive Programming](https://reader031.vdocuments.us/reader031/viewer/2022020316/5b5a4d147f8b9a905c8ba993/html5/thumbnails/42.jpg)
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
![Page 43: Functional Reactivity: Eschewing the Imperativepsznhn/G54PDC/LectureNotes/yampa.pdf · Functional Reactivity: Eschewing the Imperative An Overview of Functional Reactive Programming](https://reader031.vdocuments.us/reader031/viewer/2022020316/5b5a4d147f8b9a905c8ba993/html5/thumbnails/43.jpg)
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
![Page 44: Functional Reactivity: Eschewing the Imperativepsznhn/G54PDC/LectureNotes/yampa.pdf · Functional Reactivity: Eschewing the Imperative An Overview of Functional Reactive Programming](https://reader031.vdocuments.us/reader031/viewer/2022020316/5b5a4d147f8b9a905c8ba993/html5/thumbnails/44.jpg)
Signal functions and state
Alternative view:
Functional Reactivity:Eschewing the Imperative – p.15/48
![Page 45: Functional Reactivity: Eschewing the Imperativepsznhn/G54PDC/LectureNotes/yampa.pdf · Functional Reactivity: Eschewing the Imperative An Overview of Functional Reactive Programming](https://reader031.vdocuments.us/reader031/viewer/2022020316/5b5a4d147f8b9a905c8ba993/html5/thumbnails/45.jpg)
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
![Page 46: Functional Reactivity: Eschewing the Imperativepsznhn/G54PDC/LectureNotes/yampa.pdf · Functional Reactivity: Eschewing the Imperative An Overview of Functional Reactive Programming](https://reader031.vdocuments.us/reader031/viewer/2022020316/5b5a4d147f8b9a905c8ba993/html5/thumbnails/46.jpg)
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
![Page 47: Functional Reactivity: Eschewing the Imperativepsznhn/G54PDC/LectureNotes/yampa.pdf · Functional Reactivity: Eschewing the Imperative An Overview of Functional Reactive Programming](https://reader031.vdocuments.us/reader031/viewer/2022020316/5b5a4d147f8b9a905c8ba993/html5/thumbnails/47.jpg)
Example: Video tracker
Video trackers are typically stateful signalfunctions:
Functional Reactivity:Eschewing the Imperative – p.16/48
![Page 48: Functional Reactivity: Eschewing the Imperativepsznhn/G54PDC/LectureNotes/yampa.pdf · Functional Reactivity: Eschewing the Imperative An Overview of Functional Reactive Programming](https://reader031.vdocuments.us/reader031/viewer/2022020316/5b5a4d147f8b9a905c8ba993/html5/thumbnails/48.jpg)
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
![Page 49: Functional Reactivity: Eschewing the Imperativepsznhn/G54PDC/LectureNotes/yampa.pdf · Functional Reactivity: Eschewing the Imperative An Overview of Functional Reactive Programming](https://reader031.vdocuments.us/reader031/viewer/2022020316/5b5a4d147f8b9a905c8ba993/html5/thumbnails/49.jpg)
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
![Page 50: Functional Reactivity: Eschewing the Imperativepsznhn/G54PDC/LectureNotes/yampa.pdf · Functional Reactivity: Eschewing the Imperative An Overview of Functional Reactive Programming](https://reader031.vdocuments.us/reader031/viewer/2022020316/5b5a4d147f8b9a905c8ba993/html5/thumbnails/50.jpg)
Building systems (2)
But systems can be complex:
Functional Reactivity:Eschewing the Imperative – p.18/48
![Page 51: Functional Reactivity: Eschewing the Imperativepsznhn/G54PDC/LectureNotes/yampa.pdf · Functional Reactivity: Eschewing the Imperative An Overview of Functional Reactive Programming](https://reader031.vdocuments.us/reader031/viewer/2022020316/5b5a4d147f8b9a905c8ba993/html5/thumbnails/51.jpg)
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
![Page 52: Functional Reactivity: Eschewing the Imperativepsznhn/G54PDC/LectureNotes/yampa.pdf · Functional Reactivity: Eschewing the Imperative An Overview of Functional Reactive Programming](https://reader031.vdocuments.us/reader031/viewer/2022020316/5b5a4d147f8b9a905c8ba993/html5/thumbnails/52.jpg)
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
![Page 53: Functional Reactivity: Eschewing the Imperativepsznhn/G54PDC/LectureNotes/yampa.pdf · Functional Reactivity: Eschewing the Imperative An Overview of Functional Reactive Programming](https://reader031.vdocuments.us/reader031/viewer/2022020316/5b5a4d147f8b9a905c8ba993/html5/thumbnails/53.jpg)
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
![Page 54: Functional Reactivity: Eschewing the Imperativepsznhn/G54PDC/LectureNotes/yampa.pdf · Functional Reactivity: Eschewing the Imperative An Overview of Functional Reactive Programming](https://reader031.vdocuments.us/reader031/viewer/2022020316/5b5a4d147f8b9a905c8ba993/html5/thumbnails/54.jpg)
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
![Page 55: Functional Reactivity: Eschewing the Imperativepsznhn/G54PDC/LectureNotes/yampa.pdf · Functional Reactivity: Eschewing the Imperative An Overview of Functional Reactive Programming](https://reader031.vdocuments.us/reader031/viewer/2022020316/5b5a4d147f8b9a905c8ba993/html5/thumbnails/55.jpg)
What is an arrow? (1)
• A type constructor a of arity two.
Functional Reactivity:Eschewing the Imperative – p.20/48
![Page 56: Functional Reactivity: Eschewing the Imperativepsznhn/G54PDC/LectureNotes/yampa.pdf · Functional Reactivity: Eschewing the Imperative An Overview of Functional Reactive Programming](https://reader031.vdocuments.us/reader031/viewer/2022020316/5b5a4d147f8b9a905c8ba993/html5/thumbnails/56.jpg)
What is an arrow? (1)
• A type constructor a of arity two.• Three operators:
Functional Reactivity:Eschewing the Imperative – p.20/48
![Page 57: Functional Reactivity: Eschewing the Imperativepsznhn/G54PDC/LectureNotes/yampa.pdf · Functional Reactivity: Eschewing the Imperative An Overview of Functional Reactive Programming](https://reader031.vdocuments.us/reader031/viewer/2022020316/5b5a4d147f8b9a905c8ba993/html5/thumbnails/57.jpg)
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
![Page 58: Functional Reactivity: Eschewing the Imperativepsznhn/G54PDC/LectureNotes/yampa.pdf · Functional Reactivity: Eschewing the Imperative An Overview of Functional Reactive Programming](https://reader031.vdocuments.us/reader031/viewer/2022020316/5b5a4d147f8b9a905c8ba993/html5/thumbnails/58.jpg)
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
![Page 59: Functional Reactivity: Eschewing the Imperativepsznhn/G54PDC/LectureNotes/yampa.pdf · Functional Reactivity: Eschewing the Imperative An Overview of Functional Reactive Programming](https://reader031.vdocuments.us/reader031/viewer/2022020316/5b5a4d147f8b9a905c8ba993/html5/thumbnails/59.jpg)
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
![Page 60: Functional Reactivity: Eschewing the Imperativepsznhn/G54PDC/LectureNotes/yampa.pdf · Functional Reactivity: Eschewing the Imperative An Overview of Functional Reactive Programming](https://reader031.vdocuments.us/reader031/viewer/2022020316/5b5a4d147f8b9a905c8ba993/html5/thumbnails/60.jpg)
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
![Page 61: Functional Reactivity: Eschewing the Imperativepsznhn/G54PDC/LectureNotes/yampa.pdf · Functional Reactivity: Eschewing the Imperative An Overview of Functional Reactive Programming](https://reader031.vdocuments.us/reader031/viewer/2022020316/5b5a4d147f8b9a905c8ba993/html5/thumbnails/61.jpg)
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
![Page 62: Functional Reactivity: Eschewing the Imperativepsznhn/G54PDC/LectureNotes/yampa.pdf · Functional Reactivity: Eschewing the Imperative An Overview of Functional Reactive Programming](https://reader031.vdocuments.us/reader031/viewer/2022020316/5b5a4d147f8b9a905c8ba993/html5/thumbnails/62.jpg)
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
![Page 63: Functional Reactivity: Eschewing the Imperativepsznhn/G54PDC/LectureNotes/yampa.pdf · Functional Reactivity: Eschewing the Imperative An Overview of Functional Reactive Programming](https://reader031.vdocuments.us/reader031/viewer/2022020316/5b5a4d147f8b9a905c8ba993/html5/thumbnails/63.jpg)
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
![Page 64: Functional Reactivity: Eschewing the Imperativepsznhn/G54PDC/LectureNotes/yampa.pdf · Functional Reactivity: Eschewing the Imperative An Overview of Functional Reactive Programming](https://reader031.vdocuments.us/reader031/viewer/2022020316/5b5a4d147f8b9a905c8ba993/html5/thumbnails/64.jpg)
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
![Page 65: Functional Reactivity: Eschewing the Imperativepsznhn/G54PDC/LectureNotes/yampa.pdf · Functional Reactivity: Eschewing the Imperative An Overview of Functional Reactive Programming](https://reader031.vdocuments.us/reader031/viewer/2022020316/5b5a4d147f8b9a905c8ba993/html5/thumbnails/65.jpg)
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
![Page 66: Functional Reactivity: Eschewing the Imperativepsznhn/G54PDC/LectureNotes/yampa.pdf · Functional Reactivity: Eschewing the Imperative An Overview of Functional Reactive Programming](https://reader031.vdocuments.us/reader031/viewer/2022020316/5b5a4d147f8b9a905c8ba993/html5/thumbnails/66.jpg)
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
![Page 67: Functional Reactivity: Eschewing the Imperativepsznhn/G54PDC/LectureNotes/yampa.pdf · Functional Reactivity: Eschewing the Imperative An Overview of Functional Reactive Programming](https://reader031.vdocuments.us/reader031/viewer/2022020316/5b5a4d147f8b9a905c8ba993/html5/thumbnails/67.jpg)
Some more arrow combinators (2)
As diagrams:
second ff *** g
f &&& gFunctional Reactivity:Eschewing the Imperative – p.25/48
![Page 68: Functional Reactivity: Eschewing the Imperativepsznhn/G54PDC/LectureNotes/yampa.pdf · Functional Reactivity: Eschewing the Imperative An Overview of Functional Reactive Programming](https://reader031.vdocuments.us/reader031/viewer/2022020316/5b5a4d147f8b9a905c8ba993/html5/thumbnails/68.jpg)
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
![Page 69: Functional Reactivity: Eschewing the Imperativepsznhn/G54PDC/LectureNotes/yampa.pdf · Functional Reactivity: Eschewing the Imperative An Overview of Functional Reactive Programming](https://reader031.vdocuments.us/reader031/viewer/2022020316/5b5a4d147f8b9a905c8ba993/html5/thumbnails/69.jpg)
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
![Page 70: Functional Reactivity: Eschewing the Imperativepsznhn/G54PDC/LectureNotes/yampa.pdf · Functional Reactivity: Eschewing the Imperative An Overview of Functional Reactive Programming](https://reader031.vdocuments.us/reader031/viewer/2022020316/5b5a4d147f8b9a905c8ba993/html5/thumbnails/70.jpg)
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
![Page 71: Functional Reactivity: Eschewing the Imperativepsznhn/G54PDC/LectureNotes/yampa.pdf · Functional Reactivity: Eschewing the Imperative An Overview of Functional Reactive Programming](https://reader031.vdocuments.us/reader031/viewer/2022020316/5b5a4d147f8b9a905c8ba993/html5/thumbnails/71.jpg)
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
![Page 72: Functional Reactivity: Eschewing the Imperativepsznhn/G54PDC/LectureNotes/yampa.pdf · Functional Reactivity: Eschewing the Imperative An Overview of Functional Reactive Programming](https://reader031.vdocuments.us/reader031/viewer/2022020316/5b5a4d147f8b9a905c8ba993/html5/thumbnails/72.jpg)
Some further basic signal functions
• identity :: SF a aidentity = arr id
Functional Reactivity:Eschewing the Imperative – p.30/48
![Page 73: Functional Reactivity: Eschewing the Imperativepsznhn/G54PDC/LectureNotes/yampa.pdf · Functional Reactivity: Eschewing the Imperative An Overview of Functional Reactive Programming](https://reader031.vdocuments.us/reader031/viewer/2022020316/5b5a4d147f8b9a905c8ba993/html5/thumbnails/73.jpg)
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
![Page 74: Functional Reactivity: Eschewing the Imperativepsznhn/G54PDC/LectureNotes/yampa.pdf · Functional Reactivity: Eschewing the Imperative An Overview of Functional Reactive Programming](https://reader031.vdocuments.us/reader031/viewer/2022020316/5b5a4d147f8b9a905c8ba993/html5/thumbnails/74.jpg)
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
![Page 75: Functional Reactivity: Eschewing the Imperativepsznhn/G54PDC/LectureNotes/yampa.pdf · Functional Reactivity: Eschewing the Imperative An Overview of Functional Reactive Programming](https://reader031.vdocuments.us/reader031/viewer/2022020316/5b5a4d147f8b9a905c8ba993/html5/thumbnails/75.jpg)
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
![Page 76: Functional Reactivity: Eschewing the Imperativepsznhn/G54PDC/LectureNotes/yampa.pdf · Functional Reactivity: Eschewing the Imperative An Overview of Functional Reactive Programming](https://reader031.vdocuments.us/reader031/viewer/2022020316/5b5a4d147f8b9a905c8ba993/html5/thumbnails/76.jpg)
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
![Page 77: Functional Reactivity: Eschewing the Imperativepsznhn/G54PDC/LectureNotes/yampa.pdf · Functional Reactivity: Eschewing the Imperative An Overview of Functional Reactive Programming](https://reader031.vdocuments.us/reader031/viewer/2022020316/5b5a4d147f8b9a905c8ba993/html5/thumbnails/77.jpg)
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
![Page 78: Functional Reactivity: Eschewing the Imperativepsznhn/G54PDC/LectureNotes/yampa.pdf · Functional Reactivity: Eschewing the Imperative An Overview of Functional Reactive Programming](https://reader031.vdocuments.us/reader031/viewer/2022020316/5b5a4d147f8b9a905c8ba993/html5/thumbnails/78.jpg)
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
![Page 79: Functional Reactivity: Eschewing the Imperativepsznhn/G54PDC/LectureNotes/yampa.pdf · Functional Reactivity: Eschewing the Imperative An Overview of Functional Reactive Programming](https://reader031.vdocuments.us/reader031/viewer/2022020316/5b5a4d147f8b9a905c8ba993/html5/thumbnails/79.jpg)
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
![Page 80: Functional Reactivity: Eschewing the Imperativepsznhn/G54PDC/LectureNotes/yampa.pdf · Functional Reactivity: Eschewing the Imperative An Overview of Functional Reactive Programming](https://reader031.vdocuments.us/reader031/viewer/2022020316/5b5a4d147f8b9a905c8ba993/html5/thumbnails/80.jpg)
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
![Page 81: Functional Reactivity: Eschewing the Imperativepsznhn/G54PDC/LectureNotes/yampa.pdf · Functional Reactivity: Eschewing the Imperative An Overview of Functional Reactive Programming](https://reader031.vdocuments.us/reader031/viewer/2022020316/5b5a4d147f8b9a905c8ba993/html5/thumbnails/81.jpg)
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
![Page 82: Functional Reactivity: Eschewing the Imperativepsznhn/G54PDC/LectureNotes/yampa.pdf · Functional Reactivity: Eschewing the Imperative An Overview of Functional Reactive Programming](https://reader031.vdocuments.us/reader031/viewer/2022020316/5b5a4d147f8b9a905c8ba993/html5/thumbnails/82.jpg)
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
![Page 83: Functional Reactivity: Eschewing the Imperativepsznhn/G54PDC/LectureNotes/yampa.pdf · Functional Reactivity: Eschewing the Imperative An Overview of Functional Reactive Programming](https://reader031.vdocuments.us/reader031/viewer/2022020316/5b5a4d147f8b9a905c8ba993/html5/thumbnails/83.jpg)
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
![Page 84: Functional Reactivity: Eschewing the Imperativepsznhn/G54PDC/LectureNotes/yampa.pdf · Functional Reactivity: Eschewing the Imperative An Overview of Functional Reactive Programming](https://reader031.vdocuments.us/reader031/viewer/2022020316/5b5a4d147f8b9a905c8ba993/html5/thumbnails/84.jpg)
Switching
Q: How and when do signal functions “start”?
Functional Reactivity:Eschewing the Imperative – p.35/48
![Page 85: Functional Reactivity: Eschewing the Imperativepsznhn/G54PDC/LectureNotes/yampa.pdf · Functional Reactivity: Eschewing the Imperative An Overview of Functional Reactive Programming](https://reader031.vdocuments.us/reader031/viewer/2022020316/5b5a4d147f8b9a905c8ba993/html5/thumbnails/85.jpg)
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
![Page 86: Functional Reactivity: Eschewing the Imperativepsznhn/G54PDC/LectureNotes/yampa.pdf · Functional Reactivity: Eschewing the Imperative An Overview of Functional Reactive Programming](https://reader031.vdocuments.us/reader031/viewer/2022020316/5b5a4d147f8b9a905c8ba993/html5/thumbnails/86.jpg)
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
![Page 87: Functional Reactivity: Eschewing the Imperativepsznhn/G54PDC/LectureNotes/yampa.pdf · Functional Reactivity: Eschewing the Imperative An Overview of Functional Reactive Programming](https://reader031.vdocuments.us/reader031/viewer/2022020316/5b5a4d147f8b9a905c8ba993/html5/thumbnails/87.jpg)
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
![Page 88: Functional Reactivity: Eschewing the Imperativepsznhn/G54PDC/LectureNotes/yampa.pdf · Functional Reactivity: Eschewing the Imperative An Overview of Functional Reactive Programming](https://reader031.vdocuments.us/reader031/viewer/2022020316/5b5a4d147f8b9a905c8ba993/html5/thumbnails/88.jpg)
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
![Page 89: Functional Reactivity: Eschewing the Imperativepsznhn/G54PDC/LectureNotes/yampa.pdf · Functional Reactivity: Eschewing the Imperative An Overview of Functional Reactive Programming](https://reader031.vdocuments.us/reader031/viewer/2022020316/5b5a4d147f8b9a905c8ba993/html5/thumbnails/89.jpg)
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
![Page 90: Functional Reactivity: Eschewing the Imperativepsznhn/G54PDC/LectureNotes/yampa.pdf · Functional Reactivity: Eschewing the Imperative An Overview of Functional Reactive Programming](https://reader031.vdocuments.us/reader031/viewer/2022020316/5b5a4d147f8b9a905c8ba993/html5/thumbnails/90.jpg)
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
![Page 91: Functional Reactivity: Eschewing the Imperativepsznhn/G54PDC/LectureNotes/yampa.pdf · Functional Reactivity: Eschewing the Imperative An Overview of Functional Reactive Programming](https://reader031.vdocuments.us/reader031/viewer/2022020316/5b5a4d147f8b9a905c8ba993/html5/thumbnails/91.jpg)
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
![Page 92: Functional Reactivity: Eschewing the Imperativepsznhn/G54PDC/LectureNotes/yampa.pdf · Functional Reactivity: Eschewing the Imperative An Overview of Functional Reactive Programming](https://reader031.vdocuments.us/reader031/viewer/2022020316/5b5a4d147f8b9a905c8ba993/html5/thumbnails/92.jpg)
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
![Page 93: Functional Reactivity: Eschewing the Imperativepsznhn/G54PDC/LectureNotes/yampa.pdf · Functional Reactivity: Eschewing the Imperative An Overview of Functional Reactive Programming](https://reader031.vdocuments.us/reader031/viewer/2022020316/5b5a4d147f8b9a905c8ba993/html5/thumbnails/93.jpg)
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
![Page 94: Functional Reactivity: Eschewing the Imperativepsznhn/G54PDC/LectureNotes/yampa.pdf · Functional Reactivity: Eschewing the Imperative An Overview of Functional Reactive Programming](https://reader031.vdocuments.us/reader031/viewer/2022020316/5b5a4d147f8b9a905c8ba993/html5/thumbnails/94.jpg)
Highly dynamic system structure?
Basic switch allows one signal function to bereplaced by another.
Functional Reactivity:Eschewing the Imperative – p.39/48
![Page 95: Functional Reactivity: Eschewing the Imperativepsznhn/G54PDC/LectureNotes/yampa.pdf · Functional Reactivity: Eschewing the Imperative An Overview of Functional Reactive Programming](https://reader031.vdocuments.us/reader031/viewer/2022020316/5b5a4d147f8b9a905c8ba993/html5/thumbnails/95.jpg)
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
![Page 96: Functional Reactivity: Eschewing the Imperativepsznhn/G54PDC/LectureNotes/yampa.pdf · Functional Reactivity: Eschewing the Imperative An Overview of Functional Reactive Programming](https://reader031.vdocuments.us/reader031/viewer/2022020316/5b5a4d147f8b9a905c8ba993/html5/thumbnails/96.jpg)
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
![Page 97: Functional Reactivity: Eschewing the Imperativepsznhn/G54PDC/LectureNotes/yampa.pdf · Functional Reactivity: Eschewing the Imperative An Overview of Functional Reactive Programming](https://reader031.vdocuments.us/reader031/viewer/2022020316/5b5a4d147f8b9a905c8ba993/html5/thumbnails/97.jpg)
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
![Page 98: Functional Reactivity: Eschewing the Imperativepsznhn/G54PDC/LectureNotes/yampa.pdf · Functional Reactivity: Eschewing the Imperative An Overview of Functional Reactive Programming](https://reader031.vdocuments.us/reader031/viewer/2022020316/5b5a4d147f8b9a905c8ba993/html5/thumbnails/98.jpg)
Example: Space Invaders
Functional Reactivity:Eschewing the Imperative – p.41/48
![Page 99: Functional Reactivity: Eschewing the Imperativepsznhn/G54PDC/LectureNotes/yampa.pdf · Functional Reactivity: Eschewing the Imperative An Overview of Functional Reactive Programming](https://reader031.vdocuments.us/reader031/viewer/2022020316/5b5a4d147f8b9a905c8ba993/html5/thumbnails/99.jpg)
Overall game structure
gun
alien
bullet
alien
...
killOrSpawn
ObjInput ObjOutput
} [ObjectOutput]
dpSwitch
route
Functional Reactivity:Eschewing the Imperative – p.42/48
![Page 100: Functional Reactivity: Eschewing the Imperativepsznhn/G54PDC/LectureNotes/yampa.pdf · Functional Reactivity: Eschewing the Imperative An Overview of Functional Reactive Programming](https://reader031.vdocuments.us/reader031/viewer/2022020316/5b5a4d147f8b9a905c8ba993/html5/thumbnails/100.jpg)
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
![Page 101: Functional Reactivity: Eschewing the Imperativepsznhn/G54PDC/LectureNotes/yampa.pdf · Functional Reactivity: Eschewing the Imperative An Overview of Functional Reactive Programming](https://reader031.vdocuments.us/reader031/viewer/2022020316/5b5a4d147f8b9a905c8ba993/html5/thumbnails/101.jpg)
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
![Page 102: Functional Reactivity: Eschewing the Imperativepsznhn/G54PDC/LectureNotes/yampa.pdf · Functional Reactivity: Eschewing the Imperative An Overview of Functional Reactive Programming](https://reader031.vdocuments.us/reader031/viewer/2022020316/5b5a4d147f8b9a905c8ba993/html5/thumbnails/102.jpg)
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
![Page 103: Functional Reactivity: Eschewing the Imperativepsznhn/G54PDC/LectureNotes/yampa.pdf · Functional Reactivity: Eschewing the Imperative An Overview of Functional Reactive Programming](https://reader031.vdocuments.us/reader031/viewer/2022020316/5b5a4d147f8b9a905c8ba993/html5/thumbnails/103.jpg)
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
![Page 104: Functional Reactivity: Eschewing the Imperativepsznhn/G54PDC/LectureNotes/yampa.pdf · Functional Reactivity: Eschewing the Imperative An Overview of Functional Reactive Programming](https://reader031.vdocuments.us/reader031/viewer/2022020316/5b5a4d147f8b9a905c8ba993/html5/thumbnails/104.jpg)
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
![Page 105: Functional Reactivity: Eschewing the Imperativepsznhn/G54PDC/LectureNotes/yampa.pdf · Functional Reactivity: Eschewing the Imperative An Overview of Functional Reactive Programming](https://reader031.vdocuments.us/reader031/viewer/2022020316/5b5a4d147f8b9a905c8ba993/html5/thumbnails/105.jpg)
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
![Page 106: Functional Reactivity: Eschewing the Imperativepsznhn/G54PDC/LectureNotes/yampa.pdf · Functional Reactivity: Eschewing the Imperative An Overview of Functional Reactive Programming](https://reader031.vdocuments.us/reader031/viewer/2022020316/5b5a4d147f8b9a905c8ba993/html5/thumbnails/106.jpg)
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
![Page 107: Functional Reactivity: Eschewing the Imperativepsznhn/G54PDC/LectureNotes/yampa.pdf · Functional Reactivity: Eschewing the Imperative An Overview of Functional Reactive Programming](https://reader031.vdocuments.us/reader031/viewer/2022020316/5b5a4d147f8b9a905c8ba993/html5/thumbnails/107.jpg)
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