introduction to functional reactive programming
DESCRIPTION
TRANSCRIPT
Introduction toFunctionalReactive
Programming
Edward Amsden
Introduction
History of FRP
Classic andSignal-FunctionFRP
Implementing FRP
Applications ofFRP
Ongoing Work
Summary
Introduction to Functional ReactiveProgramming
Edward Amsden
PL WonksSeptember 28, 2012
Introduction toFunctionalReactive
Programming
Edward Amsden
Introduction
History of FRP
Classic andSignal-FunctionFRP
Implementing FRP
Applications ofFRP
Ongoing Work
Summary
Overview
Introduction
History of FRP
Classic and Signal-Function FRP
Implementing FRP
Applications of FRP
Ongoing Work
Summary
Introduction toFunctionalReactive
Programming
Edward Amsden
Introduction
History of FRP
Classic andSignal-FunctionFRP
Implementing FRP
Applications ofFRP
Ongoing Work
Summary
Introduction
Introduction toFunctionalReactive
Programming
Edward Amsden
Introduction
History of FRP
Classic andSignal-FunctionFRP
Implementing FRP
Applications ofFRP
Ongoing Work
Summary
Motivation
I Real programs have to deal with the real world.
I The real world is constrained by time.
I The real world produces unpredictable inputsto your program.
I Usual solution: Callbacks and event loops.
I Can we extend the benefits of functional programmingto handling these characteristics of the world?
Introduction toFunctionalReactive
Programming
Edward Amsden
Introduction
History of FRP
Classic andSignal-FunctionFRP
Implementing FRP
Applications ofFRP
Ongoing Work
Summary
Basic Abstractions
I FRP divides inputs into two basic classes:I Behaviors or signals: Functions of time.I Events: Temporal sequences of discrete values.
I An FRP language must include a means of altering orreplacing a program based on event occurrences.
I This is the basis of FRP’s reactivity.
I These abstractions may be reified in an FRP languageor may form the basis of other abstractions, but theymust be present.
Introduction toFunctionalReactive
Programming
Edward Amsden
Introduction
History of FRP
Classic andSignal-FunctionFRP
Implementing FRP
Applications ofFRP
Ongoing Work
Summary
History of FRP
Introduction toFunctionalReactive
Programming
Edward Amsden
Introduction
History of FRP
Classic andSignal-FunctionFRP
Implementing FRP
Applications ofFRP
Ongoing Work
Summary
People
I Conal ElliottI Previously at Microsoft Research.I Denotational semantics and implementations.
I Paul HudakI Yale University.I Applications of FRP to multimedia.I Euterpea.I Haskell School of Expression.
I Antony CourtneyI Student at Yale, graduated 2004.I Arrowized FRP.I Frappe (FRP in Java).
Introduction toFunctionalReactive
Programming
Edward Amsden
Introduction
History of FRP
Classic andSignal-FunctionFRP
Implementing FRP
Applications ofFRP
Ongoing Work
Summary
People
I Henrik NilssonI Previously at Yale University.I Collaborated on FRP with Hudak.I Now at University of Nottingham.I Created Yampa, an optimized implementation of AFRP.
I Neil SculthorpeI Student of Nilsson at Nottingham.I Now at University of Kansas.I Creator of N-Ary FRP.
Introduction toFunctionalReactive
Programming
Edward Amsden
Introduction
History of FRP
Classic andSignal-FunctionFRP
Implementing FRP
Applications ofFRP
Ongoing Work
Summary
“Functional Reactive Animation”
I Conal Elliott and Paul Hudak
I The original description of FRP.
I Proposed behaviors and events as abstractions.
I Evaluated a behavior carrying a pictureto form an animation.
Introduction toFunctionalReactive
Programming
Edward Amsden
Introduction
History of FRP
Classic andSignal-FunctionFRP
Implementing FRP
Applications ofFRP
Ongoing Work
Summary
“Genuinely Functional User Interfaces”
I Antony Courtney and Conal Elliott.
I Described a GUI toolkit based on “AFRP,”FRP formulated as an Arrow.
I Proposed “signal functions” as an FRP abstraction.
I Signal functions are reactive constructs with events andsignals as inputs and outputs.
I Signal functions are programs, and may be replacedin response to events.
Introduction toFunctionalReactive
Programming
Edward Amsden
Introduction
History of FRP
Classic andSignal-FunctionFRP
Implementing FRP
Applications ofFRP
Ongoing Work
Summary
“Push-pull Functional Reactive Programming”
I Conal Elliott
I Gave denotational semantics for FRP.
I Used to prove soundness of transformationto a normal form.
I Normal form lifts all reactivity to top level.I Evaluate with concurrent threads:
I Block waiting for next event to react to.I Spin evaluating current program with time steps.
I Advantage: no spinning sampling of events.
I Advantage: no sampling latency in reacting to events.
I Disadvantage: not a purely functional implementation.
I Disadvantage: Requires kludge to merge events.
Introduction toFunctionalReactive
Programming
Edward Amsden
Introduction
History of FRP
Classic andSignal-FunctionFRP
Implementing FRP
Applications ofFRP
Ongoing Work
Summary
“Dynamic Optimization for Functional ReactiveProgramming using Generalized Algebraic DataTypes”
I Henrik Nilsson
I Dynamically optimizing implementation of AFRP.
I Eliminate redundant composition introduced byswitching.
Introduction toFunctionalReactive
Programming
Edward Amsden
Introduction
History of FRP
Classic andSignal-FunctionFRP
Implementing FRP
Applications ofFRP
Ongoing Work
Summary
“Toward Safe and Efficient Functional ReactiveProgramming”
I Neil Sculthorpe(PhD Thesis, University of Nottingham)
I Embedded AFRP in Agda(a dependently-typed total functional language).
I Introduced signal vectors as a new abstractionto avoid embedding events in signals.
Introduction toFunctionalReactive
Programming
Edward Amsden
Introduction
History of FRP
Classic andSignal-FunctionFRP
Implementing FRP
Applications ofFRP
Ongoing Work
Summary
Upcoming: “Push-pull Signal FunctionFunctional Reactive Programming”
I Edward Amsden(M.S. Thesis, Rochester Institute of Technology)
I Using signal vectors to permit push-based evaluationof events in a signal-function FRP system.
Introduction toFunctionalReactive
Programming
Edward Amsden
Introduction
History of FRP
Classic andSignal-FunctionFRP
Implementing FRP
Applications ofFRP
Ongoing Work
Summary
Classic and Signal-Function FRP
Introduction toFunctionalReactive
Programming
Edward Amsden
Introduction
History of FRP
Classic andSignal-FunctionFRP
Implementing FRP
Applications ofFRP
Ongoing Work
Summary
Families of FRP Systems
I Classic FRPI FranI ReactiveI reactive-bananaI elmI Frappe
I Signal-Function FRPI FruitI AFRPI YampaI N-Ary FRPI time-flies
Introduction toFunctionalReactive
Programming
Edward Amsden
Introduction
History of FRP
Classic andSignal-FunctionFRP
Implementing FRP
Applications ofFRP
Ongoing Work
Summary
Classic FRP
I Events and behaviors are first-class abstractions.
I Primitive events and behaviors must be provided by thesystem.
I Programs are constructed by composing events andbehaviors and by lifting functions and values to simplebehaviors.
Introduction toFunctionalReactive
Programming
Edward Amsden
Introduction
History of FRP
Classic andSignal-FunctionFRP
Implementing FRP
Applications ofFRP
Ongoing Work
Summary
Signal-Function FRP
I Signal functions are first-class abstractions.
I An evaluation interface provides inputs to the systemwithout discrimination as to their origin.
I Programs are constructed by lifting functions to simplesignal functions and by composing signal functions.
Introduction toFunctionalReactive
Programming
Edward Amsden
Introduction
History of FRP
Classic andSignal-FunctionFRP
Implementing FRP
Applications ofFRP
Ongoing Work
Summary
Implementing FRP
Introduction toFunctionalReactive
Programming
Edward Amsden
Introduction
History of FRP
Classic andSignal-FunctionFRP
Implementing FRP
Applications ofFRP
Ongoing Work
Summary
Classic FRP
I Signals are implemented as functions from time to asample value and a new behavior.
I This permits a behavior to throw away history andavoid time-space leaks.
I Events are represented as lazy lists of time-value pairs.
I Some implementations use more complexrepresentations of improving lists.
I Either way, must have a representation of “Nooccurrence yet.”
Introduction toFunctionalReactive
Programming
Edward Amsden
Introduction
History of FRP
Classic andSignal-FunctionFRP
Implementing FRP
Applications ofFRP
Ongoing Work
Summary
Signal-Function FRP
I Signal functions are represented as one or morecontinuations.
I Continuations take signal sample update or eventoccurrence.
I Continuations produce output updates/occurrences andreplacement signal function.
Introduction toFunctionalReactive
Programming
Edward Amsden
Introduction
History of FRP
Classic andSignal-FunctionFRP
Implementing FRP
Applications ofFRP
Ongoing Work
Summary
Applications of FRP
Introduction toFunctionalReactive
Programming
Edward Amsden
Introduction
History of FRP
Classic andSignal-FunctionFRP
Implementing FRP
Applications ofFRP
Ongoing Work
Summary
Animations and Games
I Bouncing Balls etc. (Fran)
I Space Invaders (Yampa)
I Frag (Yampa)
Introduction toFunctionalReactive
Programming
Edward Amsden
Introduction
History of FRP
Classic andSignal-FunctionFRP
Implementing FRP
Applications ofFRP
Ongoing Work
Summary
Audio
I YampaSynth
Introduction toFunctionalReactive
Programming
Edward Amsden
Introduction
History of FRP
Classic andSignal-FunctionFRP
Implementing FRP
Applications ofFRP
Ongoing Work
Summary
Robotics
I FRob
Introduction toFunctionalReactive
Programming
Edward Amsden
Introduction
History of FRP
Classic andSignal-FunctionFRP
Implementing FRP
Applications ofFRP
Ongoing Work
Summary
Ongoing Work
Introduction toFunctionalReactive
Programming
Edward Amsden
Introduction
History of FRP
Classic andSignal-FunctionFRP
Implementing FRP
Applications ofFRP
Ongoing Work
Summary
Semantics
I Denotational semantics given for Classic FRP. (Elliott)
I Little work on formal semantics of signal-function FRP.
I Courtney and Elliott give informal semantics.
I Category-theoretic semantics for signal-function FRP?How does composition with switch work?
Introduction toFunctionalReactive
Programming
Edward Amsden
Introduction
History of FRP
Classic andSignal-FunctionFRP
Implementing FRP
Applications ofFRP
Ongoing Work
Summary
Performance
I Push-based event handling should yield performanceincrease.
I Dynamic optimizations (Yampa).
I Static optimizations (Causal Commutative Arrows).
I Staged code generation?
I Parallelism?
Introduction toFunctionalReactive
Programming
Edward Amsden
Introduction
History of FRP
Classic andSignal-FunctionFRP
Implementing FRP
Applications ofFRP
Ongoing Work
Summary
Summary
Introduction toFunctionalReactive
Programming
Edward Amsden
Introduction
History of FRP
Classic andSignal-FunctionFRP
Implementing FRP
Applications ofFRP
Ongoing Work
Summary
Summary
I Motivation: Reacting to real world
I Abstractions: Behaviors/signals and events.
I Implementation: Continuations.
I Applications: Anything reactive.
Introduction toFunctionalReactive
Programming
Edward Amsden
Introduction
History of FRP
Classic andSignal-FunctionFRP
Implementing FRP
Applications ofFRP
Ongoing Work
Summary
Questions?