introduction to functional reactive programming

31
Introduction to Functional Reactive Programming Edward Amsden Introduction History of FRP Classic and Signal-Function FRP Implementing FRP Applications of FRP Ongoing Work Summary Introduction to Functional Reactive Programming Edward Amsden PL Wonks September 28, 2012

Upload: edward-amsden

Post on 18-Dec-2014

5.903 views

Category:

Documents


6 download

DESCRIPTION

 

TRANSCRIPT

Page 1: Introduction to Functional Reactive Programming

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

Page 2: Introduction to Functional Reactive Programming

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

Page 3: Introduction to Functional Reactive Programming

Introduction toFunctionalReactive

Programming

Edward Amsden

Introduction

History of FRP

Classic andSignal-FunctionFRP

Implementing FRP

Applications ofFRP

Ongoing Work

Summary

Introduction

Page 4: Introduction to Functional Reactive Programming

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?

Page 5: Introduction to Functional Reactive Programming

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.

Page 6: Introduction to Functional Reactive Programming

Introduction toFunctionalReactive

Programming

Edward Amsden

Introduction

History of FRP

Classic andSignal-FunctionFRP

Implementing FRP

Applications ofFRP

Ongoing Work

Summary

History of FRP

Page 7: Introduction to Functional Reactive Programming

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).

Page 8: Introduction to Functional Reactive Programming

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.

Page 9: Introduction to Functional Reactive Programming

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.

Page 10: Introduction to Functional Reactive Programming

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.

Page 11: Introduction to Functional Reactive Programming

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.

Page 12: Introduction to Functional Reactive Programming

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.

Page 13: Introduction to Functional Reactive Programming

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.

Page 14: Introduction to Functional Reactive Programming

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.

Page 15: Introduction to Functional Reactive Programming

Introduction toFunctionalReactive

Programming

Edward Amsden

Introduction

History of FRP

Classic andSignal-FunctionFRP

Implementing FRP

Applications ofFRP

Ongoing Work

Summary

Classic and Signal-Function FRP

Page 16: Introduction to Functional Reactive Programming

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

Page 17: Introduction to Functional Reactive Programming

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.

Page 18: Introduction to Functional Reactive Programming

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.

Page 19: Introduction to Functional Reactive Programming

Introduction toFunctionalReactive

Programming

Edward Amsden

Introduction

History of FRP

Classic andSignal-FunctionFRP

Implementing FRP

Applications ofFRP

Ongoing Work

Summary

Implementing FRP

Page 20: Introduction to Functional Reactive Programming

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.”

Page 21: Introduction to Functional Reactive Programming

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.

Page 22: Introduction to Functional Reactive Programming

Introduction toFunctionalReactive

Programming

Edward Amsden

Introduction

History of FRP

Classic andSignal-FunctionFRP

Implementing FRP

Applications ofFRP

Ongoing Work

Summary

Applications of FRP

Page 23: Introduction to Functional Reactive Programming

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)

Page 24: Introduction to Functional Reactive Programming

Introduction toFunctionalReactive

Programming

Edward Amsden

Introduction

History of FRP

Classic andSignal-FunctionFRP

Implementing FRP

Applications ofFRP

Ongoing Work

Summary

Audio

I YampaSynth

Page 25: Introduction to Functional Reactive Programming

Introduction toFunctionalReactive

Programming

Edward Amsden

Introduction

History of FRP

Classic andSignal-FunctionFRP

Implementing FRP

Applications ofFRP

Ongoing Work

Summary

Robotics

I FRob

Page 26: Introduction to Functional Reactive Programming

Introduction toFunctionalReactive

Programming

Edward Amsden

Introduction

History of FRP

Classic andSignal-FunctionFRP

Implementing FRP

Applications ofFRP

Ongoing Work

Summary

Ongoing Work

Page 27: Introduction to Functional Reactive Programming

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?

Page 28: Introduction to Functional Reactive Programming

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?

Page 29: Introduction to Functional Reactive Programming

Introduction toFunctionalReactive

Programming

Edward Amsden

Introduction

History of FRP

Classic andSignal-FunctionFRP

Implementing FRP

Applications ofFRP

Ongoing Work

Summary

Summary

Page 30: Introduction to Functional Reactive Programming

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.

Page 31: Introduction to Functional Reactive Programming

Introduction toFunctionalReactive

Programming

Edward Amsden

Introduction

History of FRP

Classic andSignal-FunctionFRP

Implementing FRP

Applications ofFRP

Ongoing Work

Summary

Questions?