Introduction to FRP and Yampa through Games and Musicpsznhn/Talks/7digital-July2016... · Aims and...
Transcript of Introduction to FRP and Yampa through Games and Musicpsznhn/Talks/7digital-July2016... · Aims and...
![Page 1: Introduction to FRP and Yampa through Games and Musicpsznhn/Talks/7digital-July2016... · Aims and Overview (1) •Introduction to Functional Reactive Programming •Demonstrating](https://reader033.fdocuments.in/reader033/viewer/2022060208/5f0417187e708231d40c456a/html5/thumbnails/1.jpg)
Introduction to FRP and Yampathrough Games and Music
Henrik Nilsson, Ivan Perez, and George Giorgidze
School of Computer Science
University of Nottingham, UK
Introduction to FRP and Yampa through Games and Music – p.1/47
![Page 2: Introduction to FRP and Yampa through Games and Musicpsznhn/Talks/7digital-July2016... · Aims and Overview (1) •Introduction to Functional Reactive Programming •Demonstrating](https://reader033.fdocuments.in/reader033/viewer/2022060208/5f0417187e708231d40c456a/html5/thumbnails/2.jpg)
Aims and Overview (1)
• Introduction to Functional Reactive Programming
Introduction to FRP and Yampa through Games and Music – p.2/47
![Page 3: Introduction to FRP and Yampa through Games and Musicpsznhn/Talks/7digital-July2016... · Aims and Overview (1) •Introduction to Functional Reactive Programming •Demonstrating](https://reader033.fdocuments.in/reader033/viewer/2022060208/5f0417187e708231d40c456a/html5/thumbnails/3.jpg)
Aims and Overview (1)
• Introduction to Functional Reactive Programming
• Demonstrating that a pure, declarative,reactive approach covers more applicationsareas than what one might think.
Introduction to FRP and Yampa through Games and Music – p.2/47
![Page 4: Introduction to FRP and Yampa through Games and Musicpsznhn/Talks/7digital-July2016... · Aims and Overview (1) •Introduction to Functional Reactive Programming •Demonstrating](https://reader033.fdocuments.in/reader033/viewer/2022060208/5f0417187e708231d40c456a/html5/thumbnails/4.jpg)
Aims and Overview (1)
• Introduction to Functional Reactive Programming
• Demonstrating that a pure, declarative,reactive approach covers more applicationsareas than what one might think.
• Through the above, give you a hopefullysomewhat different and useful perspective onthe reactive programming technologies thatyou are already using.
Introduction to FRP and Yampa through Games and Music – p.2/47
![Page 5: Introduction to FRP and Yampa through Games and Musicpsznhn/Talks/7digital-July2016... · Aims and Overview (1) •Introduction to Functional Reactive Programming •Demonstrating](https://reader033.fdocuments.in/reader033/viewer/2022060208/5f0417187e708231d40c456a/html5/thumbnails/5.jpg)
Aims and Overview (2)
Much will be familiar from reactive frameworkslike Cycle.js or XStream:
Introduction to FRP and Yampa through Games and Music – p.3/47
![Page 6: Introduction to FRP and Yampa through Games and Musicpsznhn/Talks/7digital-July2016... · Aims and Overview (1) •Introduction to Functional Reactive Programming •Demonstrating](https://reader033.fdocuments.in/reader033/viewer/2022060208/5f0417187e708231d40c456a/html5/thumbnails/6.jpg)
Aims and Overview (2)
Much will be familiar from reactive frameworkslike Cycle.js or XStream:
• Circuit-like programming metaphor.
Introduction to FRP and Yampa through Games and Music – p.3/47
![Page 7: Introduction to FRP and Yampa through Games and Musicpsznhn/Talks/7digital-July2016... · Aims and Overview (1) •Introduction to Functional Reactive Programming •Demonstrating](https://reader033.fdocuments.in/reader033/viewer/2022060208/5f0417187e708231d40c456a/html5/thumbnails/7.jpg)
Aims and Overview (2)
Much will be familiar from reactive frameworkslike Cycle.js or XStream:
• Circuit-like programming metaphor.
• Transforming streams by e.g. mapping oraccumulation.
Introduction to FRP and Yampa through Games and Music – p.3/47
![Page 8: Introduction to FRP and Yampa through Games and Musicpsznhn/Talks/7digital-July2016... · Aims and Overview (1) •Introduction to Functional Reactive Programming •Demonstrating](https://reader033.fdocuments.in/reader033/viewer/2022060208/5f0417187e708231d40c456a/html5/thumbnails/8.jpg)
Aims and Overview (2)
Much will be familiar from reactive frameworkslike Cycle.js or XStream:
• Circuit-like programming metaphor.
• Transforming streams by e.g. mapping oraccumulation.
• Similar primitive streams, e.g. never, periodic.
Introduction to FRP and Yampa through Games and Music – p.3/47
![Page 9: Introduction to FRP and Yampa through Games and Musicpsznhn/Talks/7digital-July2016... · Aims and Overview (1) •Introduction to Functional Reactive Programming •Demonstrating](https://reader033.fdocuments.in/reader033/viewer/2022060208/5f0417187e708231d40c456a/html5/thumbnails/9.jpg)
Aims and Overview (3)
However, much is also different:
Introduction to FRP and Yampa through Games and Music – p.4/47
![Page 10: Introduction to FRP and Yampa through Games and Musicpsznhn/Talks/7digital-July2016... · Aims and Overview (1) •Introduction to Functional Reactive Programming •Demonstrating](https://reader033.fdocuments.in/reader033/viewer/2022060208/5f0417187e708231d40c456a/html5/thumbnails/10.jpg)
Aims and Overview (3)
However, much is also different:
• Time (notionally) continuous.
Introduction to FRP and Yampa through Games and Music – p.4/47
![Page 11: Introduction to FRP and Yampa through Games and Musicpsznhn/Talks/7digital-July2016... · Aims and Overview (1) •Introduction to Functional Reactive Programming •Demonstrating](https://reader033.fdocuments.in/reader033/viewer/2022060208/5f0417187e708231d40c456a/html5/thumbnails/11.jpg)
Aims and Overview (3)
However, much is also different:
• Time (notionally) continuous.
• But events (discrete time), also supported,allowing for hybrid systems.
Introduction to FRP and Yampa through Games and Music – p.4/47
![Page 12: Introduction to FRP and Yampa through Games and Musicpsznhn/Talks/7digital-July2016... · Aims and Overview (1) •Introduction to Functional Reactive Programming •Demonstrating](https://reader033.fdocuments.in/reader033/viewer/2022060208/5f0417187e708231d40c456a/html5/thumbnails/12.jpg)
Aims and Overview (3)
However, much is also different:
• Time (notionally) continuous.
• But events (discrete time), also supported,allowing for hybrid systems.
• Synchronous (no race conditions)
Introduction to FRP and Yampa through Games and Music – p.4/47
![Page 13: Introduction to FRP and Yampa through Games and Musicpsznhn/Talks/7digital-July2016... · Aims and Overview (1) •Introduction to Functional Reactive Programming •Demonstrating](https://reader033.fdocuments.in/reader033/viewer/2022060208/5f0417187e708231d40c456a/html5/thumbnails/13.jpg)
Aims and Overview (3)
However, much is also different:
• Time (notionally) continuous.
• But events (discrete time), also supported,allowing for hybrid systems.
• Synchronous (no race conditions)
• Declarative (emphasis on what, not how;fewer operational concerns)
Introduction to FRP and Yampa through Games and Music – p.4/47
![Page 14: Introduction to FRP and Yampa through Games and Musicpsznhn/Talks/7digital-July2016... · Aims and Overview (1) •Introduction to Functional Reactive Programming •Demonstrating](https://reader033.fdocuments.in/reader033/viewer/2022060208/5f0417187e708231d40c456a/html5/thumbnails/14.jpg)
Aims and Overview (4)
And specific to Yampa:
Introduction to FRP and Yampa through Games and Music – p.5/47
![Page 15: Introduction to FRP and Yampa through Games and Musicpsznhn/Talks/7digital-July2016... · Aims and Overview (1) •Introduction to Functional Reactive Programming •Demonstrating](https://reader033.fdocuments.in/reader033/viewer/2022060208/5f0417187e708231d40c456a/html5/thumbnails/15.jpg)
Aims and Overview (4)
And specific to Yampa:
• “Stream processors” is the central, first-class,abstraction, while “streams” are secondary(very close to the circuit metaphor).
Introduction to FRP and Yampa through Games and Music – p.5/47
![Page 16: Introduction to FRP and Yampa through Games and Musicpsznhn/Talks/7digital-July2016... · Aims and Overview (1) •Introduction to Functional Reactive Programming •Demonstrating](https://reader033.fdocuments.in/reader033/viewer/2022060208/5f0417187e708231d40c456a/html5/thumbnails/16.jpg)
Aims and Overview (4)
And specific to Yampa:
• “Stream processors” is the central, first-class,abstraction, while “streams” are secondary(very close to the circuit metaphor).
• High-level support for highly dynamic systemstructure (generalisation of XStream’sflatten, no need for low-levelattaching/detaching of listeners).
Introduction to FRP and Yampa through Games and Music – p.5/47
![Page 17: Introduction to FRP and Yampa through Games and Musicpsznhn/Talks/7digital-July2016... · Aims and Overview (1) •Introduction to Functional Reactive Programming •Demonstrating](https://reader033.fdocuments.in/reader033/viewer/2022060208/5f0417187e708231d40c456a/html5/thumbnails/17.jpg)
Aims and Overview (4)
And specific to Yampa:
• “Stream processors” is the central, first-class,abstraction, while “streams” are secondary(very close to the circuit metaphor).
• High-level support for highly dynamic systemstructure (generalisation of XStream’sflatten, no need for low-levelattaching/detaching of listeners).
• Statically typed
Introduction to FRP and Yampa through Games and Music – p.5/47
![Page 18: Introduction to FRP and Yampa through Games and Musicpsznhn/Talks/7digital-July2016... · Aims and Overview (1) •Introduction to Functional Reactive Programming •Demonstrating](https://reader033.fdocuments.in/reader033/viewer/2022060208/5f0417187e708231d40c456a/html5/thumbnails/18.jpg)
Example: Feedback
Arranging feedback in XStream necessitatestaking operational concerns, how, into account:
var secondProxy$ = xs.create();
var first$ = secondProxy$.map ...;
var second$ = first$.map ...;
secondProxy$.imitate(second$);
Introduction to FRP and Yampa through Games and Music – p.6/47
![Page 19: Introduction to FRP and Yampa through Games and Musicpsznhn/Talks/7digital-July2016... · Aims and Overview (1) •Introduction to Functional Reactive Programming •Demonstrating](https://reader033.fdocuments.in/reader033/viewer/2022060208/5f0417187e708231d40c456a/html5/thumbnails/19.jpg)
Example: Feedback
Arranging feedback in XStream necessitatestaking operational concerns, how, into account:
var secondProxy$ = xs.create();
var first$ = secondProxy$.map ...;
var second$ = first$.map ...;
secondProxy$.imitate(second$);
Of course, the intent, what, is really:
var first$ = second$.map ...;
var second$ = first$.map ...;
with “=” denoting equality rather than assignment.
Introduction to FRP and Yampa through Games and Music – p.6/47
![Page 20: Introduction to FRP and Yampa through Games and Musicpsznhn/Talks/7digital-July2016... · Aims and Overview (1) •Introduction to Functional Reactive Programming •Demonstrating](https://reader033.fdocuments.in/reader033/viewer/2022060208/5f0417187e708231d40c456a/html5/thumbnails/20.jpg)
Why Program Games Declaratively?
Video games are not a major application area fordeclarative programming . . .
Introduction to FRP and Yampa through Games and Music – p.7/47
![Page 21: Introduction to FRP and Yampa through Games and Musicpsznhn/Talks/7digital-July2016... · Aims and Overview (1) •Introduction to Functional Reactive Programming •Demonstrating](https://reader033.fdocuments.in/reader033/viewer/2022060208/5f0417187e708231d40c456a/html5/thumbnails/21.jpg)
Why Program Games Declaratively?
Video games are not a major application area fordeclarative programming . . . or even a niche one.
Perhaps not so surprising:
Introduction to FRP and Yampa through Games and Music – p.7/47
![Page 22: Introduction to FRP and Yampa through Games and Musicpsznhn/Talks/7digital-July2016... · Aims and Overview (1) •Introduction to Functional Reactive Programming •Demonstrating](https://reader033.fdocuments.in/reader033/viewer/2022060208/5f0417187e708231d40c456a/html5/thumbnails/22.jpg)
Why Program Games Declaratively?
Video games are not a major application area fordeclarative programming . . . or even a niche one.
Perhaps not so surprising:
• Many pragmatical reasons: performance,legacy issues, . . .
Introduction to FRP and Yampa through Games and Music – p.7/47
![Page 23: Introduction to FRP and Yampa through Games and Musicpsznhn/Talks/7digital-July2016... · Aims and Overview (1) •Introduction to Functional Reactive Programming •Demonstrating](https://reader033.fdocuments.in/reader033/viewer/2022060208/5f0417187e708231d40c456a/html5/thumbnails/23.jpg)
Why Program Games Declaratively?
Video games are not a major application area fordeclarative programming . . . or even a niche one.
Perhaps not so surprising:
• Many pragmatical reasons: performance,legacy issues, . . .
• State and effects are pervasive in videogames: Is declarative programming even aconceptually good fit?
Introduction to FRP and Yampa through Games and Music – p.7/47
![Page 24: Introduction to FRP and Yampa through Games and Musicpsznhn/Talks/7digital-July2016... · Aims and Overview (1) •Introduction to Functional Reactive Programming •Demonstrating](https://reader033.fdocuments.in/reader033/viewer/2022060208/5f0417187e708231d40c456a/html5/thumbnails/24.jpg)
But Why NOT, Really?
Many eloquent and compelling cases forfunctional programming in general:
Introduction to FRP and Yampa through Games and Music – p.8/47
![Page 25: Introduction to FRP and Yampa through Games and Musicpsznhn/Talks/7digital-July2016... · Aims and Overview (1) •Introduction to Functional Reactive Programming •Demonstrating](https://reader033.fdocuments.in/reader033/viewer/2022060208/5f0417187e708231d40c456a/html5/thumbnails/25.jpg)
But Why NOT, Really?
Many eloquent and compelling cases forfunctional programming in general:
• John Backus, 1977 ACM Turing AwardLecture: Can Programming Be Liberated fromthe von Neumann Style?
Introduction to FRP and Yampa through Games and Music – p.8/47
![Page 26: Introduction to FRP and Yampa through Games and Musicpsznhn/Talks/7digital-July2016... · Aims and Overview (1) •Introduction to Functional Reactive Programming •Demonstrating](https://reader033.fdocuments.in/reader033/viewer/2022060208/5f0417187e708231d40c456a/html5/thumbnails/26.jpg)
But Why NOT, Really?
Many eloquent and compelling cases forfunctional programming in general:
• John Backus, 1977 ACM Turing AwardLecture: Can Programming Be Liberated fromthe von Neumann Style?
• John Hughes, recent retrospective: WhyFunctional Programming Matters(on YouTube, recommended)
Introduction to FRP and Yampa through Games and Music – p.8/47
![Page 27: Introduction to FRP and Yampa through Games and Musicpsznhn/Talks/7digital-July2016... · Aims and Overview (1) •Introduction to Functional Reactive Programming •Demonstrating](https://reader033.fdocuments.in/reader033/viewer/2022060208/5f0417187e708231d40c456a/html5/thumbnails/27.jpg)
But Why NOT, Really?
Many eloquent and compelling cases forfunctional programming in general:
• John Backus, 1977 ACM Turing AwardLecture: Can Programming Be Liberated fromthe von Neumann Style?
• John Hughes, recent retrospective: WhyFunctional Programming Matters(on YouTube, recommended)
One key point: Program with whole values, not aword-at-a-time.
Introduction to FRP and Yampa through Games and Music – p.8/47
![Page 28: Introduction to FRP and Yampa through Games and Musicpsznhn/Talks/7digital-July2016... · Aims and Overview (1) •Introduction to Functional Reactive Programming •Demonstrating](https://reader033.fdocuments.in/reader033/viewer/2022060208/5f0417187e708231d40c456a/html5/thumbnails/28.jpg)
But Why NOT, Really?
Many eloquent and compelling cases forfunctional programming in general:
• John Backus, 1977 ACM Turing AwardLecture: Can Programming Be Liberated fromthe von Neumann Style?
• John Hughes, recent retrospective: WhyFunctional Programming Matters(on YouTube, recommended)
One key point: Program with whole values, not aword-at-a-time. Which, of course, is the pointalso of frameworks like Cycle.js and XStreams.
Introduction to FRP and Yampa through Games and Music – p.8/47
![Page 29: Introduction to FRP and Yampa through Games and Musicpsznhn/Talks/7digital-July2016... · Aims and Overview (1) •Introduction to Functional Reactive Programming •Demonstrating](https://reader033.fdocuments.in/reader033/viewer/2022060208/5f0417187e708231d40c456a/html5/thumbnails/29.jpg)
Possible Gains (1)
With his Keera Studios hat on, Ivan’s top threereasons:
• Reliability.
• Lower long-term maintenance cost.
• Lower production cost and fasttime-to-prototype.
Introduction to FRP and Yampa through Games and Music – p.9/47
![Page 30: Introduction to FRP and Yampa through Games and Musicpsznhn/Talks/7digital-July2016... · Aims and Overview (1) •Introduction to Functional Reactive Programming •Demonstrating](https://reader033.fdocuments.in/reader033/viewer/2022060208/5f0417187e708231d40c456a/html5/thumbnails/30.jpg)
Possible Gains (2)
High profile people in the games industry havepointed out potential benefits:
Introduction to FRP and Yampa through Games and Music – p.10/47
![Page 31: Introduction to FRP and Yampa through Games and Musicpsznhn/Talks/7digital-July2016... · Aims and Overview (1) •Introduction to Functional Reactive Programming •Demonstrating](https://reader033.fdocuments.in/reader033/viewer/2022060208/5f0417187e708231d40c456a/html5/thumbnails/31.jpg)
Possible Gains (2)
High profile people in the games industry havepointed out potential benefits:
• John D. Carmack, id Software:Wolfenstein 3D, Doom, Quake
Introduction to FRP and Yampa through Games and Music – p.10/47
![Page 32: Introduction to FRP and Yampa through Games and Musicpsznhn/Talks/7digital-July2016... · Aims and Overview (1) •Introduction to Functional Reactive Programming •Demonstrating](https://reader033.fdocuments.in/reader033/viewer/2022060208/5f0417187e708231d40c456a/html5/thumbnails/32.jpg)
Possible Gains (2)
High profile people in the games industry havepointed out potential benefits:
• John D. Carmack, id Software:Wolfenstein 3D, Doom, Quake
• Tim Sweeney, Epic Games:The Unreal Engine
Introduction to FRP and Yampa through Games and Music – p.10/47
![Page 33: Introduction to FRP and Yampa through Games and Musicpsznhn/Talks/7digital-July2016... · Aims and Overview (1) •Introduction to Functional Reactive Programming •Demonstrating](https://reader033.fdocuments.in/reader033/viewer/2022060208/5f0417187e708231d40c456a/html5/thumbnails/33.jpg)
Possible Gains (2)
High profile people in the games industry havepointed out potential benefits:
• John D. Carmack, id Software:Wolfenstein 3D, Doom, Quake
• Tim Sweeney, Epic Games:The Unreal Engine
E.g. pure, declarative code:
• promotes parallelism
• eliminates many sources of errors
Introduction to FRP and Yampa through Games and Music – p.10/47
![Page 34: Introduction to FRP and Yampa through Games and Musicpsznhn/Talks/7digital-July2016... · Aims and Overview (1) •Introduction to Functional Reactive Programming •Demonstrating](https://reader033.fdocuments.in/reader033/viewer/2022060208/5f0417187e708231d40c456a/html5/thumbnails/34.jpg)
“Whole Values” for Games?
How should we go about writing video games“declaratively”?
Introduction to FRP and Yampa through Games and Music – p.11/47
![Page 35: Introduction to FRP and Yampa through Games and Musicpsznhn/Talks/7digital-July2016... · Aims and Overview (1) •Introduction to Functional Reactive Programming •Demonstrating](https://reader033.fdocuments.in/reader033/viewer/2022060208/5f0417187e708231d40c456a/html5/thumbnails/35.jpg)
“Whole Values” for Games?
How should we go about writing video games“declaratively”?
In particular, what should those “whole values” be?
Introduction to FRP and Yampa through Games and Music – p.11/47
![Page 36: Introduction to FRP and Yampa through Games and Musicpsznhn/Talks/7digital-July2016... · Aims and Overview (1) •Introduction to Functional Reactive Programming •Demonstrating](https://reader033.fdocuments.in/reader033/viewer/2022060208/5f0417187e708231d40c456a/html5/thumbnails/36.jpg)
“Whole Values” for Games?
How should we go about writing video games“declaratively”?
In particular, what should those “whole values” be?
• Could be conventional entities like vectors,arrays, lists and aggregates of such.
Introduction to FRP and Yampa through Games and Music – p.11/47
![Page 37: Introduction to FRP and Yampa through Games and Musicpsznhn/Talks/7digital-July2016... · Aims and Overview (1) •Introduction to Functional Reactive Programming •Demonstrating](https://reader033.fdocuments.in/reader033/viewer/2022060208/5f0417187e708231d40c456a/html5/thumbnails/37.jpg)
“Whole Values” for Games?
How should we go about writing video games“declaratively”?
In particular, what should those “whole values” be?
• Could be conventional entities like vectors,arrays, lists and aggregates of such.
• Could even be things like pictures.
Introduction to FRP and Yampa through Games and Music – p.11/47
![Page 38: Introduction to FRP and Yampa through Games and Musicpsznhn/Talks/7digital-July2016... · Aims and Overview (1) •Introduction to Functional Reactive Programming •Demonstrating](https://reader033.fdocuments.in/reader033/viewer/2022060208/5f0417187e708231d40c456a/html5/thumbnails/38.jpg)
“Whole Values” for Games?
How should we go about writing video games“declaratively”?
In particular, what should those “whole values” be?
• Could be conventional entities like vectors,arrays, lists and aggregates of such.
• Could even be things like pictures.
But we are going to go one step further and considerprogramming with time-varying entities.
Introduction to FRP and Yampa through Games and Music – p.11/47
![Page 39: Introduction to FRP and Yampa through Games and Musicpsznhn/Talks/7digital-July2016... · Aims and Overview (1) •Introduction to Functional Reactive Programming •Demonstrating](https://reader033.fdocuments.in/reader033/viewer/2022060208/5f0417187e708231d40c456a/html5/thumbnails/39.jpg)
Functional Reactive Programming
Introduction to FRP and Yampa through Games and Music – p.12/47
![Page 40: Introduction to FRP and Yampa through Games and Musicpsznhn/Talks/7digital-July2016... · Aims and Overview (1) •Introduction to Functional Reactive Programming •Demonstrating](https://reader033.fdocuments.in/reader033/viewer/2022060208/5f0417187e708231d40c456a/html5/thumbnails/40.jpg)
Functional Reactive Programming
• Key idea: Don’t program one-time-step-at-a-time,but describe an evolving entity as whole.
Introduction to FRP and Yampa through Games and Music – p.12/47
![Page 41: Introduction to FRP and Yampa through Games and Musicpsznhn/Talks/7digital-July2016... · Aims and Overview (1) •Introduction to Functional Reactive Programming •Demonstrating](https://reader033.fdocuments.in/reader033/viewer/2022060208/5f0417187e708231d40c456a/html5/thumbnails/41.jpg)
Functional Reactive Programming
• Key idea: Don’t program one-time-step-at-a-time,but describe an evolving entity as whole.
• FRP originated in Conal Elliott and Paul Hudak’swork on Functional Reactive Animation (Fran).
Introduction to FRP and Yampa through Games and Music – p.12/47
![Page 42: Introduction to FRP and Yampa through Games and Musicpsznhn/Talks/7digital-July2016... · Aims and Overview (1) •Introduction to Functional Reactive Programming •Demonstrating](https://reader033.fdocuments.in/reader033/viewer/2022060208/5f0417187e708231d40c456a/html5/thumbnails/42.jpg)
Functional Reactive Programming
• Key idea: Don’t program one-time-step-at-a-time,but describe an evolving entity as whole.
• FRP originated in Conal Elliott and Paul Hudak’swork on Functional Reactive Animation (Fran).Highly cited 1997 ICFP paper; ICFP award formost influential paper in 2007.
Introduction to FRP and Yampa through Games and Music – p.12/47
![Page 43: Introduction to FRP and Yampa through Games and Musicpsznhn/Talks/7digital-July2016... · Aims and Overview (1) •Introduction to Functional Reactive Programming •Demonstrating](https://reader033.fdocuments.in/reader033/viewer/2022060208/5f0417187e708231d40c456a/html5/thumbnails/43.jpg)
Functional Reactive Programming
• Key idea: Don’t program one-time-step-at-a-time,but describe an evolving entity as whole.
• FRP originated in Conal Elliott and Paul Hudak’swork on Functional Reactive Animation (Fran).Highly cited 1997 ICFP paper; ICFP award formost influential paper in 2007.
• FRP has evolved in a number of directionsand into different concrete implementations.
Introduction to FRP and Yampa through Games and Music – p.12/47
![Page 44: Introduction to FRP and Yampa through Games and Musicpsznhn/Talks/7digital-July2016... · Aims and Overview (1) •Introduction to Functional Reactive Programming •Demonstrating](https://reader033.fdocuments.in/reader033/viewer/2022060208/5f0417187e708231d40c456a/html5/thumbnails/44.jpg)
Functional Reactive Programming
• Key idea: Don’t program one-time-step-at-a-time,but describe an evolving entity as whole.
• FRP originated in Conal Elliott and Paul Hudak’swork on Functional Reactive Animation (Fran).Highly cited 1997 ICFP paper; ICFP award formost influential paper in 2007.
• FRP has evolved in a number of directionsand into different concrete implementations.
• We will use Yampa: an FRP systemembedded in Haskell.
Introduction to FRP and Yampa through Games and Music – p.12/47
![Page 45: Introduction to FRP and Yampa through Games and Musicpsznhn/Talks/7digital-July2016... · Aims and Overview (1) •Introduction to Functional Reactive Programming •Demonstrating](https://reader033.fdocuments.in/reader033/viewer/2022060208/5f0417187e708231d40c456a/html5/thumbnails/45.jpg)
Take-home Message # 1
Introduction to FRP and Yampa through Games and Music – p.13/47
![Page 46: Introduction to FRP and Yampa through Games and Musicpsznhn/Talks/7digital-July2016... · Aims and Overview (1) •Introduction to Functional Reactive Programming •Demonstrating](https://reader033.fdocuments.in/reader033/viewer/2022060208/5f0417187e708231d40c456a/html5/thumbnails/46.jpg)
Take-home Message # 1
Video games can be programmed declarativelyby describing what entities are over time.
Introduction to FRP and Yampa through Games and Music – p.13/47
![Page 47: Introduction to FRP and Yampa through Games and Musicpsznhn/Talks/7digital-July2016... · Aims and Overview (1) •Introduction to Functional Reactive Programming •Demonstrating](https://reader033.fdocuments.in/reader033/viewer/2022060208/5f0417187e708231d40c456a/html5/thumbnails/47.jpg)
Take-home Message # 1
Video games can be programmed declarativelyby describing what entities are over time.
Our whole values are things like:
• The totality of input from the player
• The animated graphics output
• The entire life of a game object
Introduction to FRP and Yampa through Games and Music – p.13/47
![Page 48: Introduction to FRP and Yampa through Games and Musicpsznhn/Talks/7digital-July2016... · Aims and Overview (1) •Introduction to Functional Reactive Programming •Demonstrating](https://reader033.fdocuments.in/reader033/viewer/2022060208/5f0417187e708231d40c456a/html5/thumbnails/48.jpg)
Take-home Message # 1
Video games can be programmed declarativelyby describing what entities are over time.
Our whole values are things like:
• The totality of input from the player
• The animated graphics output
• The entire life of a game object
We construct and work with pure functions on these:
• The game: function from input to animation
• In the game: fixed point of function oncollection of game objects
Introduction to FRP and Yampa through Games and Music – p.13/47
![Page 49: Introduction to FRP and Yampa through Games and Musicpsznhn/Talks/7digital-July2016... · Aims and Overview (1) •Introduction to Functional Reactive Programming •Demonstrating](https://reader033.fdocuments.in/reader033/viewer/2022060208/5f0417187e708231d40c456a/html5/thumbnails/49.jpg)
Take-home Message # 2
You too can program games declaratively . . .
Introduction to FRP and Yampa through Games and Music – p.14/47
![Page 50: Introduction to FRP and Yampa through Games and Musicpsznhn/Talks/7digital-July2016... · Aims and Overview (1) •Introduction to Functional Reactive Programming •Demonstrating](https://reader033.fdocuments.in/reader033/viewer/2022060208/5f0417187e708231d40c456a/html5/thumbnails/50.jpg)
Take-home Message # 2
You too can program games declaratively . . . today!
Introduction to FRP and Yampa through Games and Music – p.14/47
![Page 51: Introduction to FRP and Yampa through Games and Musicpsznhn/Talks/7digital-July2016... · Aims and Overview (1) •Introduction to Functional Reactive Programming •Demonstrating](https://reader033.fdocuments.in/reader033/viewer/2022060208/5f0417187e708231d40c456a/html5/thumbnails/51.jpg)
Take-home Game!
Or download one for free to your Android device!
Play Store: Pang-a-lambda (Keera Studios)Introduction to FRP and Yampa through Games and Music – p.15/47
![Page 52: Introduction to FRP and Yampa through Games and Musicpsznhn/Talks/7digital-July2016... · Aims and Overview (1) •Introduction to Functional Reactive Programming •Demonstrating](https://reader033.fdocuments.in/reader033/viewer/2022060208/5f0417187e708231d40c456a/html5/thumbnails/52.jpg)
Yampa
Introduction to FRP and Yampa through Games and Music – p.16/47
![Page 53: Introduction to FRP and Yampa through Games and Musicpsznhn/Talks/7digital-July2016... · Aims and Overview (1) •Introduction to Functional Reactive Programming •Demonstrating](https://reader033.fdocuments.in/reader033/viewer/2022060208/5f0417187e708231d40c456a/html5/thumbnails/53.jpg)
Yampa
• FRP implementation embedded in Haskell
Introduction to FRP and Yampa through Games and Music – p.16/47
![Page 54: Introduction to FRP and Yampa through Games and Musicpsznhn/Talks/7digital-July2016... · Aims and Overview (1) •Introduction to Functional Reactive Programming •Demonstrating](https://reader033.fdocuments.in/reader033/viewer/2022060208/5f0417187e708231d40c456a/html5/thumbnails/54.jpg)
Yampa
• FRP implementation embedded in Haskell
• Key notions:
- Signals: time-varying values (cf. streams)
- Signal Functions: pure functions on signals
- Switching: temporal composition of signalfunctions (cf. XStream’s flatten)
Introduction to FRP and Yampa through Games and Music – p.16/47
![Page 55: Introduction to FRP and Yampa through Games and Musicpsznhn/Talks/7digital-July2016... · Aims and Overview (1) •Introduction to Functional Reactive Programming •Demonstrating](https://reader033.fdocuments.in/reader033/viewer/2022060208/5f0417187e708231d40c456a/html5/thumbnails/55.jpg)
Yampa
• FRP implementation embedded in Haskell
• Key notions:
- Signals: time-varying values (cf. streams)
- Signal Functions: pure functions on signals
- Switching: temporal composition of signalfunctions (cf. XStream’s flatten)
• Programming model:
Introduction to FRP and Yampa through Games and Music – p.16/47
![Page 56: Introduction to FRP and Yampa through Games and Musicpsznhn/Talks/7digital-July2016... · Aims and Overview (1) •Introduction to Functional Reactive Programming •Demonstrating](https://reader033.fdocuments.in/reader033/viewer/2022060208/5f0417187e708231d40c456a/html5/thumbnails/56.jpg)
Yampa?
Introduction to FRP and Yampa through Games and Music – p.17/47
![Page 57: Introduction to FRP and Yampa through Games and Musicpsznhn/Talks/7digital-July2016... · Aims and Overview (1) •Introduction to Functional Reactive Programming •Demonstrating](https://reader033.fdocuments.in/reader033/viewer/2022060208/5f0417187e708231d40c456a/html5/thumbnails/57.jpg)
Yampa?
Yampa is a river with long calmly flowing sectionsand abrupt whitewater transitions in between.
A good metaphor for hybrid systems!Introduction to FRP and Yampa through Games and Music – p.17/47
![Page 58: Introduction to FRP and Yampa through Games and Musicpsznhn/Talks/7digital-July2016... · Aims and Overview (1) •Introduction to Functional Reactive Programming •Demonstrating](https://reader033.fdocuments.in/reader033/viewer/2022060208/5f0417187e708231d40c456a/html5/thumbnails/58.jpg)
Signal Functions
Introduction to FRP and Yampa through Games and Music – p.18/47
![Page 59: Introduction to FRP and Yampa through Games and Musicpsznhn/Talks/7digital-July2016... · Aims and Overview (1) •Introduction to Functional Reactive Programming •Demonstrating](https://reader033.fdocuments.in/reader033/viewer/2022060208/5f0417187e708231d40c456a/html5/thumbnails/59.jpg)
Signal Functions
Intuition:
Introduction to FRP and Yampa through Games and Music – p.18/47
![Page 60: Introduction to FRP and Yampa through Games and Musicpsznhn/Talks/7digital-July2016... · Aims and Overview (1) •Introduction to Functional Reactive Programming •Demonstrating](https://reader033.fdocuments.in/reader033/viewer/2022060208/5f0417187e708231d40c456a/html5/thumbnails/60.jpg)
Signal Functions
Intuition:
Time ≈ R
Introduction to FRP and Yampa through Games and Music – p.18/47
![Page 61: Introduction to FRP and Yampa through Games and Musicpsznhn/Talks/7digital-July2016... · Aims and Overview (1) •Introduction to Functional Reactive Programming •Demonstrating](https://reader033.fdocuments.in/reader033/viewer/2022060208/5f0417187e708231d40c456a/html5/thumbnails/61.jpg)
Signal Functions
Intuition:
Time ≈ R
Signal a ≈ Time → a
x :: Signal T1y :: Signal T2
Introduction to FRP and Yampa through Games and Music – p.18/47
![Page 62: Introduction to FRP and Yampa through Games and Musicpsznhn/Talks/7digital-July2016... · Aims and Overview (1) •Introduction to Functional Reactive Programming •Demonstrating](https://reader033.fdocuments.in/reader033/viewer/2022060208/5f0417187e708231d40c456a/html5/thumbnails/62.jpg)
Signal Functions
Intuition:
Time ≈ R
Signal a ≈ Time → a
x :: Signal T1y :: Signal T2SF a b ≈ Signal a → Signal bf :: SF T1 T2
Introduction to FRP and Yampa through Games and Music – p.18/47
![Page 63: Introduction to FRP and Yampa through Games and Musicpsznhn/Talks/7digital-July2016... · Aims and Overview (1) •Introduction to Functional Reactive Programming •Demonstrating](https://reader033.fdocuments.in/reader033/viewer/2022060208/5f0417187e708231d40c456a/html5/thumbnails/63.jpg)
Signal Functions
Intuition:
Time ≈ R
Signal a ≈ Time → a
x :: Signal T1y :: Signal T2SF a b ≈ Signal a → Signal bf :: SF T1 T2
Additionally, causality required: output at time tmust be determined by input on interval [0, t].
Introduction to FRP and Yampa through Games and Music – p.18/47
![Page 64: Introduction to FRP and Yampa through Games and Musicpsznhn/Talks/7digital-July2016... · Aims and Overview (1) •Introduction to Functional Reactive Programming •Demonstrating](https://reader033.fdocuments.in/reader033/viewer/2022060208/5f0417187e708231d40c456a/html5/thumbnails/64.jpg)
Some Basic Signal Functions
identity :: SF a a
Introduction to FRP and Yampa through Games and Music – p.19/47
![Page 65: Introduction to FRP and Yampa through Games and Musicpsznhn/Talks/7digital-July2016... · Aims and Overview (1) •Introduction to Functional Reactive Programming •Demonstrating](https://reader033.fdocuments.in/reader033/viewer/2022060208/5f0417187e708231d40c456a/html5/thumbnails/65.jpg)
Some Basic Signal Functions
identity :: SF a a
constant :: b → SF a b
Introduction to FRP and Yampa through Games and Music – p.19/47
![Page 66: Introduction to FRP and Yampa through Games and Musicpsznhn/Talks/7digital-July2016... · Aims and Overview (1) •Introduction to Functional Reactive Programming •Demonstrating](https://reader033.fdocuments.in/reader033/viewer/2022060208/5f0417187e708231d40c456a/html5/thumbnails/66.jpg)
Some Basic Signal Functions
identity :: SF a a
constant :: b → SF a b
integral :: VectorSpace a s ⇒ SF a a
y(t) =
t∫
0
x(τ) dτ
Introduction to FRP and Yampa through Games and Music – p.19/47
![Page 67: Introduction to FRP and Yampa through Games and Musicpsznhn/Talks/7digital-July2016... · Aims and Overview (1) •Introduction to Functional Reactive Programming •Demonstrating](https://reader033.fdocuments.in/reader033/viewer/2022060208/5f0417187e708231d40c456a/html5/thumbnails/67.jpg)
Composition
In Yampa, systems are described by combiningsignal functions (forming new signal functions).
Introduction to FRP and Yampa through Games and Music – p.20/47
![Page 68: Introduction to FRP and Yampa through Games and Musicpsznhn/Talks/7digital-July2016... · Aims and Overview (1) •Introduction to Functional Reactive Programming •Demonstrating](https://reader033.fdocuments.in/reader033/viewer/2022060208/5f0417187e708231d40c456a/html5/thumbnails/68.jpg)
Composition
In Yampa, systems are described by combiningsignal functions (forming new signal functions).
For example, serial composition:
Introduction to FRP and Yampa through Games and Music – p.20/47
![Page 69: Introduction to FRP and Yampa through Games and Musicpsznhn/Talks/7digital-July2016... · Aims and Overview (1) •Introduction to Functional Reactive Programming •Demonstrating](https://reader033.fdocuments.in/reader033/viewer/2022060208/5f0417187e708231d40c456a/html5/thumbnails/69.jpg)
Composition
In Yampa, systems are described by combiningsignal functions (forming new signal functions).
For example, serial composition:
A combinator that captures this idea:
(≫) :: SF a b → SF b c → SF a c
Introduction to FRP and Yampa through Games and Music – p.20/47
![Page 70: Introduction to FRP and Yampa through Games and Musicpsznhn/Talks/7digital-July2016... · Aims and Overview (1) •Introduction to Functional Reactive Programming •Demonstrating](https://reader033.fdocuments.in/reader033/viewer/2022060208/5f0417187e708231d40c456a/html5/thumbnails/70.jpg)
Composition
In Yampa, systems are described by combiningsignal functions (forming new signal functions).
For example, serial composition:
A combinator that captures this idea:
(≫) :: SF a b → SF b c → SF a c
Signal functions are the primary notion; signals asecondary one, only existing indirectly.
Introduction to FRP and Yampa through Games and Music – p.20/47
![Page 71: Introduction to FRP and Yampa through Games and Musicpsznhn/Talks/7digital-July2016... · Aims and Overview (1) •Introduction to Functional Reactive Programming •Demonstrating](https://reader033.fdocuments.in/reader033/viewer/2022060208/5f0417187e708231d40c456a/html5/thumbnails/71.jpg)
Systems
What about larger, more complicated networks?How many combinators are needed?
Introduction to FRP and Yampa through Games and Music – p.21/47
![Page 72: Introduction to FRP and Yampa through Games and Musicpsznhn/Talks/7digital-July2016... · Aims and Overview (1) •Introduction to Functional Reactive Programming •Demonstrating](https://reader033.fdocuments.in/reader033/viewer/2022060208/5f0417187e708231d40c456a/html5/thumbnails/72.jpg)
Systems
What about larger, more complicated networks?How many combinators are needed?
John Hughes’s Arrow framework provides agood answer!
Introduction to FRP and Yampa through Games and Music – p.21/47
![Page 73: Introduction to FRP and Yampa through Games and Musicpsznhn/Talks/7digital-July2016... · Aims and Overview (1) •Introduction to Functional Reactive Programming •Demonstrating](https://reader033.fdocuments.in/reader033/viewer/2022060208/5f0417187e708231d40c456a/html5/thumbnails/73.jpg)
The Arrow framework
arr f f ≫ g
first f loop f
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 bIntroduction to FRP and Yampa through Games and Music – p.22/47
![Page 74: Introduction to FRP and Yampa through Games and Musicpsznhn/Talks/7digital-July2016... · Aims and Overview (1) •Introduction to Functional Reactive Programming •Demonstrating](https://reader033.fdocuments.in/reader033/viewer/2022060208/5f0417187e708231d40c456a/html5/thumbnails/74.jpg)
Oscillator from Pang-a-lambda
This oscillator determines the movement ofblocks:
osci ampl period = proc → do
rec
let a = −(2.0 ∗ pi / period) ↑ 2 ∗ p
v ← integral−≺ a
p ← (ampl+) <̂< integral−≺ v
returnA−≺ p
Direct transliteration of standard equations.
Introduction to FRP and Yampa through Games and Music – p.23/47
![Page 75: Introduction to FRP and Yampa through Games and Musicpsznhn/Talks/7digital-July2016... · Aims and Overview (1) •Introduction to Functional Reactive Programming •Demonstrating](https://reader033.fdocuments.in/reader033/viewer/2022060208/5f0417187e708231d40c456a/html5/thumbnails/75.jpg)
A Bouncing Ball
Lots of bouncing balls in Pang-a-lambda!
y = y0 +
∫v dt
v = v0 +
∫−9.81
On impact:
v = −v(t−)
(fully elastic collision)
Introduction to FRP and Yampa through Games and Music – p.24/47
![Page 76: Introduction to FRP and Yampa through Games and Musicpsznhn/Talks/7digital-July2016... · Aims and Overview (1) •Introduction to Functional Reactive Programming •Demonstrating](https://reader033.fdocuments.in/reader033/viewer/2022060208/5f0417187e708231d40c456a/html5/thumbnails/76.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)
Introduction to FRP and Yampa through Games and Music – p.25/47
![Page 77: Introduction to FRP and Yampa through Games and Musicpsznhn/Talks/7digital-July2016... · Aims and Overview (1) •Introduction to Functional Reactive Programming •Demonstrating](https://reader033.fdocuments.in/reader033/viewer/2022060208/5f0417187e708231d40c456a/html5/thumbnails/77.jpg)
Events
Yampa models discrete-time signals by lifting theco-domain of signals using an option-type:
data Event a = NoEvent | Event a
Discrete-time signal = Signal (Eventα).
Introduction to FRP and Yampa through Games and Music – p.26/47
![Page 78: Introduction to FRP and Yampa through Games and Musicpsznhn/Talks/7digital-July2016... · Aims and Overview (1) •Introduction to Functional Reactive Programming •Demonstrating](https://reader033.fdocuments.in/reader033/viewer/2022060208/5f0417187e708231d40c456a/html5/thumbnails/78.jpg)
Events
Yampa models discrete-time signals by lifting theco-domain of signals using an option-type:
data Event a = NoEvent | Event a
Discrete-time signal = Signal (Eventα).
Some functions and event sources:
tag :: Event a → b → Event b
after :: Time → b → SF a (Event b)
edge :: SF Bool (Event ())
Introduction to FRP and Yampa through Games and Music – p.26/47
![Page 79: Introduction to FRP and Yampa through Games and Musicpsznhn/Talks/7digital-July2016... · Aims and Overview (1) •Introduction to Functional Reactive Programming •Demonstrating](https://reader033.fdocuments.in/reader033/viewer/2022060208/5f0417187e708231d40c456a/html5/thumbnails/79.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 6 0
returnA−≺ (yv , hit ‘tag ‘ yv)
Introduction to FRP and Yampa through Games and Music – p.27/47
![Page 80: Introduction to FRP and Yampa through Games and Musicpsznhn/Talks/7digital-July2016... · Aims and Overview (1) •Introduction to Functional Reactive Programming •Demonstrating](https://reader033.fdocuments.in/reader033/viewer/2022060208/5f0417187e708231d40c456a/html5/thumbnails/80.jpg)
Switching
Q: How and when do signal functions “start”?
Introduction to FRP and Yampa through Games and Music – p.28/47
![Page 81: Introduction to FRP and Yampa through Games and Musicpsznhn/Talks/7digital-July2016... · Aims and Overview (1) •Introduction to Functional Reactive Programming •Demonstrating](https://reader033.fdocuments.in/reader033/viewer/2022060208/5f0417187e708231d40c456a/html5/thumbnails/81.jpg)
Switching
Q: How and when do signal functions “start”?
A: • Switchers apply a signal functions to itsinput signal at some point in time.
Introduction to FRP and Yampa through Games and Music – p.28/47
![Page 82: Introduction to FRP and Yampa through Games and Musicpsznhn/Talks/7digital-July2016... · Aims and Overview (1) •Introduction to Functional Reactive Programming •Demonstrating](https://reader033.fdocuments.in/reader033/viewer/2022060208/5f0417187e708231d40c456a/html5/thumbnails/82.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 is temporal composition of signalfunctions.
Introduction to FRP and Yampa through Games and Music – p.28/47
![Page 83: Introduction to FRP and Yampa through Games and Musicpsznhn/Talks/7digital-July2016... · Aims and Overview (1) •Introduction to Functional Reactive Programming •Demonstrating](https://reader033.fdocuments.in/reader033/viewer/2022060208/5f0417187e708231d40c456a/html5/thumbnails/83.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 is temporal composition of signalfunctions.
Switchers thus allow systems with varyingstructure to be described.
Introduction to FRP and Yampa through Games and Music – p.28/47
![Page 84: Introduction to FRP and Yampa through Games and Musicpsznhn/Talks/7digital-July2016... · Aims and Overview (1) •Introduction to Functional Reactive Programming •Demonstrating](https://reader033.fdocuments.in/reader033/viewer/2022060208/5f0417187e708231d40c456a/html5/thumbnails/84.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 is temporal composition of signalfunctions.
Switchers thus allow systems with varyingstructure to be described.
Generalised switches allow composition ofcollections of signal functions. Can be used tomodel e.g. varying number of objects in a game.
Introduction to FRP and Yampa through Games and Music – p.28/47
![Page 85: Introduction to FRP and Yampa through Games and Musicpsznhn/Talks/7digital-July2016... · Aims and Overview (1) •Introduction to Functional Reactive Programming •Demonstrating](https://reader033.fdocuments.in/reader033/viewer/2022060208/5f0417187e708231d40c456a/html5/thumbnails/85.jpg)
The Basic Switch
Idea:
• Allows one signal function to be replaced byanother.
• Switching takes place on the first occurrenceof the switching event source.
switch::
SF a (b,Event c)
→ (c → SF a b)
→ SF a b
Introduction to FRP and Yampa through Games and Music – p.29/47
![Page 86: Introduction to FRP and Yampa through Games and Musicpsznhn/Talks/7digital-July2016... · Aims and Overview (1) •Introduction to Functional Reactive Programming •Demonstrating](https://reader033.fdocuments.in/reader033/viewer/2022060208/5f0417187e708231d40c456a/html5/thumbnails/86.jpg)
The Basic Switch
Idea:
• Allows one signal function to be replaced byanother.
• Switching takes place on the first occurrenceof the switching event source.
switch:: Initial SF with event source
SF a (b,Event c)
→ (c → SF a b)
→ SF a b
Introduction to FRP and Yampa through Games and Music – p.29/47
![Page 87: Introduction to FRP and Yampa through Games and Musicpsznhn/Talks/7digital-July2016... · Aims and Overview (1) •Introduction to Functional Reactive Programming •Demonstrating](https://reader033.fdocuments.in/reader033/viewer/2022060208/5f0417187e708231d40c456a/html5/thumbnails/87.jpg)
The Basic Switch
Idea:
• Allows one signal function to be replaced byanother.
• Switching takes place on the first occurrenceof the switching event source.
switch:: Function yielding SF to switch into
SF a (b,Event c)
→ (c → SF a b)
→ SF a b
Introduction to FRP and Yampa through Games and Music – p.29/47
![Page 88: Introduction to FRP and Yampa through Games and Musicpsznhn/Talks/7digital-July2016... · Aims and Overview (1) •Introduction to Functional Reactive Programming •Demonstrating](https://reader033.fdocuments.in/reader033/viewer/2022060208/5f0417187e708231d40c456a/html5/thumbnails/88.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)
Introduction to FRP and Yampa through Games and Music – p.30/47
![Page 89: Introduction to FRP and Yampa through Games and Musicpsznhn/Talks/7digital-July2016... · Aims and Overview (1) •Introduction to Functional Reactive Programming •Demonstrating](https://reader033.fdocuments.in/reader033/viewer/2022060208/5f0417187e708231d40c456a/html5/thumbnails/89.jpg)
Game Objectsdata Object = Object {objectName :: ObjectName
, objectKind :: ObjectKind
, objectPos :: Pos2D
, objectVel :: Vel2D
. . .
}
data ObjectKind = Ball . . . | Player . . . | . . .
data ObjectInput = ObjectInput
{userInput :: Controller
, collisions :: Collisions
}Introduction to FRP and Yampa through Games and Music – p.31/47
![Page 90: Introduction to FRP and Yampa through Games and Musicpsznhn/Talks/7digital-July2016... · Aims and Overview (1) •Introduction to Functional Reactive Programming •Demonstrating](https://reader033.fdocuments.in/reader033/viewer/2022060208/5f0417187e708231d40c456a/html5/thumbnails/90.jpg)
Overall Game StructuregamePlay :: [ListSF ObjectInput Object ]
→ SF Controller ([Object ],Time)
gamePlay objs = loopPre [ ] $
proc (input , cs)→ do
let oi = ObjectInput input cs
ol ← dlSwitch objs−≺ oi
let cs ′ = detectCollisions ol
tLeft ← time−≺ ()
returnA−≺ ((ol , tLeft), cs ′)
ListSF and dlSwitch are related abstractions thatallow objects to die or spawn new ones.
Introduction to FRP and Yampa through Games and Music – p.32/47
![Page 91: Introduction to FRP and Yampa through Games and Musicpsznhn/Talks/7digital-July2016... · Aims and Overview (1) •Introduction to Functional Reactive Programming •Demonstrating](https://reader033.fdocuments.in/reader033/viewer/2022060208/5f0417187e708231d40c456a/html5/thumbnails/91.jpg)
And now for something different . . .
Switched-on Yampa: Programming ModularSynthesizers in Haskell
Introduction to FRP and Yampa through Games and Music – p.33/47
![Page 92: Introduction to FRP and Yampa through Games and Musicpsznhn/Talks/7digital-July2016... · Aims and Overview (1) •Introduction to Functional Reactive Programming •Demonstrating](https://reader033.fdocuments.in/reader033/viewer/2022060208/5f0417187e708231d40c456a/html5/thumbnails/92.jpg)
And now for something different . . .
Switched-on Yampa: Programming ModularSynthesizers in Haskell
Introduction to FRP and Yampa through Games and Music – p.33/47
![Page 93: Introduction to FRP and Yampa through Games and Musicpsznhn/Talks/7digital-July2016... · Aims and Overview (1) •Introduction to Functional Reactive Programming •Demonstrating](https://reader033.fdocuments.in/reader033/viewer/2022060208/5f0417187e708231d40c456a/html5/thumbnails/93.jpg)
Modular synthesizers?
Introduction to FRP and Yampa through Games and Music – p.34/47
![Page 94: Introduction to FRP and Yampa through Games and Musicpsznhn/Talks/7digital-July2016... · Aims and Overview (1) •Introduction to Functional Reactive Programming •Demonstrating](https://reader033.fdocuments.in/reader033/viewer/2022060208/5f0417187e708231d40c456a/html5/thumbnails/94.jpg)
Modular synthesizers?
Introduction to FRP and Yampa through Games and Music – p.34/47
![Page 95: Introduction to FRP and Yampa through Games and Musicpsznhn/Talks/7digital-July2016... · Aims and Overview (1) •Introduction to Functional Reactive Programming •Demonstrating](https://reader033.fdocuments.in/reader033/viewer/2022060208/5f0417187e708231d40c456a/html5/thumbnails/95.jpg)
Modular synthesizers?
Steve Pocaro, Toto,with Polyfusion Syn-thesizer
Introduction to FRP and Yampa through Games and Music – p.34/47
![Page 96: Introduction to FRP and Yampa through Games and Musicpsznhn/Talks/7digital-July2016... · Aims and Overview (1) •Introduction to Functional Reactive Programming •Demonstrating](https://reader033.fdocuments.in/reader033/viewer/2022060208/5f0417187e708231d40c456a/html5/thumbnails/96.jpg)
Modern Modular Synthesizers
Introduction to FRP and Yampa through Games and Music – p.35/47
![Page 97: Introduction to FRP and Yampa through Games and Musicpsznhn/Talks/7digital-July2016... · Aims and Overview (1) •Introduction to Functional Reactive Programming •Demonstrating](https://reader033.fdocuments.in/reader033/viewer/2022060208/5f0417187e708231d40c456a/html5/thumbnails/97.jpg)
Where does Yampa enter the picture?
Introduction to FRP and Yampa through Games and Music – p.36/47
![Page 98: Introduction to FRP and Yampa through Games and Musicpsznhn/Talks/7digital-July2016... · Aims and Overview (1) •Introduction to Functional Reactive Programming •Demonstrating](https://reader033.fdocuments.in/reader033/viewer/2022060208/5f0417187e708231d40c456a/html5/thumbnails/98.jpg)
Where does Yampa enter the picture?
• Music can be seen as a hybrid phenomenon.Thus interesting to explore a hybrid approachto programming music and musical applications.
Introduction to FRP and Yampa through Games and Music – p.36/47
![Page 99: Introduction to FRP and Yampa through Games and Musicpsznhn/Talks/7digital-July2016... · Aims and Overview (1) •Introduction to Functional Reactive Programming •Demonstrating](https://reader033.fdocuments.in/reader033/viewer/2022060208/5f0417187e708231d40c456a/html5/thumbnails/99.jpg)
Where does Yampa enter the picture?
• Music can be seen as a hybrid phenomenon.Thus interesting to explore a hybrid approachto programming music and musical applications.
• Yampa’s programming model is very reminiscentof programming modular synthesizers:
Introduction to FRP and Yampa through Games and Music – p.36/47
![Page 100: Introduction to FRP and Yampa through Games and Musicpsznhn/Talks/7digital-July2016... · Aims and Overview (1) •Introduction to Functional Reactive Programming •Demonstrating](https://reader033.fdocuments.in/reader033/viewer/2022060208/5f0417187e708231d40c456a/html5/thumbnails/100.jpg)
Where does Yampa enter the picture?
• Music can be seen as a hybrid phenomenon.Thus interesting to explore a hybrid approachto programming music and musical applications.
• Yampa’s programming model is very reminiscentof programming modular synthesizers:
• Fun application! Useful for teaching?Introduction to FRP and Yampa through Games and Music – p.36/47
![Page 101: Introduction to FRP and Yampa through Games and Musicpsznhn/Talks/7digital-July2016... · Aims and Overview (1) •Introduction to Functional Reactive Programming •Demonstrating](https://reader033.fdocuments.in/reader033/viewer/2022060208/5f0417187e708231d40c456a/html5/thumbnails/101.jpg)
Example 1: Sine oscillator
oscSine fcv
oscSine :: Frequency → SF CV Sample
oscSine f0 = proc cv → do
let f = f0 ∗ (2 ∗∗ cv)
phi ← integral−≺ 2 ∗ pi ∗ f
returnA−≺ sin phi
constant 0 ≫ oscSine 440
Introduction to FRP and Yampa through Games and Music – p.37/47
![Page 102: Introduction to FRP and Yampa through Games and Musicpsznhn/Talks/7digital-July2016... · Aims and Overview (1) •Introduction to Functional Reactive Programming •Demonstrating](https://reader033.fdocuments.in/reader033/viewer/2022060208/5f0417187e708231d40c456a/html5/thumbnails/102.jpg)
Example 2: Vibrato
0oscSine 5.0 oscSine f*0.05
constant 0
≫ oscSine 5.0
≫ arr (∗0.05)
≫ oscSine 440
Introduction to FRP and Yampa through Games and Music – p.38/47
![Page 103: Introduction to FRP and Yampa through Games and Musicpsznhn/Talks/7digital-July2016... · Aims and Overview (1) •Introduction to Functional Reactive Programming •Demonstrating](https://reader033.fdocuments.in/reader033/viewer/2022060208/5f0417187e708231d40c456a/html5/thumbnails/103.jpg)
Example 3: 50’s Sci Fi
0oscSine 3.0
oscSine f
*0.2
-0.25+1.0
+
sciFi :: SF () Sample
sciFi = proc ()→ do
und ← arr (∗0.2) ≪ oscSine 3.0−≺ 0
swp ← arr (+1.0) ≪ integral −≺ −0.25
audio ← oscSine 440 −≺ und + swp
returnA−≺ audioIntroduction to FRP and Yampa through Games and Music – p.39/47
![Page 104: Introduction to FRP and Yampa through Games and Musicpsznhn/Talks/7digital-July2016... · Aims and Overview (1) •Introduction to Functional Reactive Programming •Demonstrating](https://reader033.fdocuments.in/reader033/viewer/2022060208/5f0417187e708231d40c456a/html5/thumbnails/104.jpg)
Envelope Generators (1)
A D S R
key on key off t
envGen :: CV → [(Time,CV )]→ (Maybe Int)
→ SF (Event ()) (CV ,Event ())
envEx = envGen 0 [(0.5, 1), (0.5, 0.5), (1.0, 0.5), (0.7, 0)]
(Just 3)
Introduction to FRP and Yampa through Games and Music – p.40/47
![Page 105: Introduction to FRP and Yampa through Games and Musicpsznhn/Talks/7digital-July2016... · Aims and Overview (1) •Introduction to Functional Reactive Programming •Demonstrating](https://reader033.fdocuments.in/reader033/viewer/2022060208/5f0417187e708231d40c456a/html5/thumbnails/105.jpg)
Envelope Generators (2)
How to implement?
Introduction to FRP and Yampa through Games and Music – p.41/47
![Page 106: Introduction to FRP and Yampa through Games and Musicpsznhn/Talks/7digital-July2016... · Aims and Overview (1) •Introduction to Functional Reactive Programming •Demonstrating](https://reader033.fdocuments.in/reader033/viewer/2022060208/5f0417187e708231d40c456a/html5/thumbnails/106.jpg)
Envelope Generators (2)
How to implement?
Integration of a step function yields suitableshapes:
t
∫−→
A D S R
key on key off t
Introduction to FRP and Yampa through Games and Music – p.41/47
![Page 107: Introduction to FRP and Yampa through Games and Musicpsznhn/Talks/7digital-July2016... · Aims and Overview (1) •Introduction to Functional Reactive Programming •Demonstrating](https://reader033.fdocuments.in/reader033/viewer/2022060208/5f0417187e708231d40c456a/html5/thumbnails/107.jpg)
Envelope Generators (3)
t
afterEach :: [(Time, b)]→ SF a (Event b)
hold :: a → SF (Event a) a
steps = afterEach [(0.7, 2), (0.5,−1), (0.5, 0), (1,−0.7), (0.7, 0)]
≫ hold 0
Introduction to FRP and Yampa through Games and Music – p.42/47
![Page 108: Introduction to FRP and Yampa through Games and Musicpsznhn/Talks/7digital-July2016... · Aims and Overview (1) •Introduction to Functional Reactive Programming •Demonstrating](https://reader033.fdocuments.in/reader033/viewer/2022060208/5f0417187e708231d40c456a/html5/thumbnails/108.jpg)
Example 4: Bell
0
*oscSine f*2.0oscSine (f*2.33)
envBell
bell :: Frequency → SF () (Sample,Event)
bell f = proc ()→ do
m ← oscSine (2.33 ∗ f )−≺ 0
audio ← oscSine f −≺ 2.0 ∗m
(ampl , end)← envBell −≺ noEvent
returnA−≺ (audio ∗ ampl , end)
Introduction to FRP and Yampa through Games and Music – p.43/47
![Page 109: Introduction to FRP and Yampa through Games and Musicpsznhn/Talks/7digital-July2016... · Aims and Overview (1) •Introduction to Functional Reactive Programming •Demonstrating](https://reader033.fdocuments.in/reader033/viewer/2022060208/5f0417187e708231d40c456a/html5/thumbnails/109.jpg)
Example 5: Tinkling Bell
tinkle :: SF () Sample
tinkle = (repeatedly 0.25 84
≫ constant ()
&&&arr (fmap (bell ◦midiNoteToFreq))
≫ rSwitch (constant 0))
Introduction to FRP and Yampa through Games and Music – p.44/47
![Page 110: Introduction to FRP and Yampa through Games and Musicpsznhn/Talks/7digital-July2016... · Aims and Overview (1) •Introduction to Functional Reactive Programming •Demonstrating](https://reader033.fdocuments.in/reader033/viewer/2022060208/5f0417187e708231d40c456a/html5/thumbnails/110.jpg)
Example 6: Playing simultaneous notes
mysterySong :: SF () (Sample,Event ())
mysterySong = proc → do
t ← tinkle −≺ ()
m ← mystery−≺ ()
returnA−≺ (0.4 ∗ t + 0.6 ∗m)
Introduction to FRP and Yampa through Games and Music – p.45/47
![Page 111: Introduction to FRP and Yampa through Games and Musicpsznhn/Talks/7digital-July2016... · Aims and Overview (1) •Introduction to Functional Reactive Programming •Demonstrating](https://reader033.fdocuments.in/reader033/viewer/2022060208/5f0417187e708231d40c456a/html5/thumbnails/111.jpg)
Switched-on Yampa?
Introduction to FRP and Yampa through Games and Music – p.46/47
![Page 112: Introduction to FRP and Yampa through Games and Musicpsznhn/Talks/7digital-July2016... · Aims and Overview (1) •Introduction to Functional Reactive Programming •Demonstrating](https://reader033.fdocuments.in/reader033/viewer/2022060208/5f0417187e708231d40c456a/html5/thumbnails/112.jpg)
Switched-on Yampa?
Software and paper: www.cs.nott.ac.uk/~gggIntroduction to FRP and Yampa through Games and Music – p.46/47
![Page 113: Introduction to FRP and Yampa through Games and Musicpsznhn/Talks/7digital-July2016... · Aims and Overview (1) •Introduction to Functional Reactive Programming •Demonstrating](https://reader033.fdocuments.in/reader033/viewer/2022060208/5f0417187e708231d40c456a/html5/thumbnails/113.jpg)
Conclusions
Introduction to FRP and Yampa through Games and Music – p.47/47
![Page 114: Introduction to FRP and Yampa through Games and Musicpsznhn/Talks/7digital-July2016... · Aims and Overview (1) •Introduction to Functional Reactive Programming •Demonstrating](https://reader033.fdocuments.in/reader033/viewer/2022060208/5f0417187e708231d40c456a/html5/thumbnails/114.jpg)
Conclusions
• FRP offers one way to write interactive gamesand similar software in a declarative way.
Introduction to FRP and Yampa through Games and Music – p.47/47
![Page 115: Introduction to FRP and Yampa through Games and Musicpsznhn/Talks/7digital-July2016... · Aims and Overview (1) •Introduction to Functional Reactive Programming •Demonstrating](https://reader033.fdocuments.in/reader033/viewer/2022060208/5f0417187e708231d40c456a/html5/thumbnails/115.jpg)
Conclusions
• FRP offers one way to write interactive gamesand similar software in a declarative way.
• It allows systems to be described in terms ofwhole values varying over time.
Introduction to FRP and Yampa through Games and Music – p.47/47
![Page 116: Introduction to FRP and Yampa through Games and Musicpsznhn/Talks/7digital-July2016... · Aims and Overview (1) •Introduction to Functional Reactive Programming •Demonstrating](https://reader033.fdocuments.in/reader033/viewer/2022060208/5f0417187e708231d40c456a/html5/thumbnails/116.jpg)
Conclusions
• FRP offers one way to write interactive gamesand similar software in a declarative way.
• It allows systems to be described in terms ofwhole values varying over time.
• Not covered in this talk:
Introduction to FRP and Yampa through Games and Music – p.47/47
![Page 117: Introduction to FRP and Yampa through Games and Musicpsznhn/Talks/7digital-July2016... · Aims and Overview (1) •Introduction to Functional Reactive Programming •Demonstrating](https://reader033.fdocuments.in/reader033/viewer/2022060208/5f0417187e708231d40c456a/html5/thumbnails/117.jpg)
Conclusions
• FRP offers one way to write interactive gamesand similar software in a declarative way.
• It allows systems to be described in terms ofwhole values varying over time.
• Not covered in this talk:
- Not everything fit easily into the FRPparadigm: e.g., interfacing to existing GUItoolkits, other imperative APIs.
Introduction to FRP and Yampa through Games and Music – p.47/47
![Page 118: Introduction to FRP and Yampa through Games and Musicpsznhn/Talks/7digital-July2016... · Aims and Overview (1) •Introduction to Functional Reactive Programming •Demonstrating](https://reader033.fdocuments.in/reader033/viewer/2022060208/5f0417187e708231d40c456a/html5/thumbnails/118.jpg)
Conclusions
• FRP offers one way to write interactive gamesand similar software in a declarative way.
• It allows systems to be described in terms ofwhole values varying over time.
• Not covered in this talk:
- Not everything fit easily into the FRPparadigm: e.g., interfacing to existing GUItoolkits, other imperative APIs.
- But also such APIs can be given a “whole-valuetreatment” to improve the fit within a declarativesetting. E.g. Reactive Values and Relations.
Introduction to FRP and Yampa through Games and Music – p.47/47