reactive functional programmingee380.stanford.edu/abstracts/110518-slides.pdf · reactive...

55
Reactive Functional Programming (or, Is That a Parenthesis in Your Pocket?) Shriram Krishnamurthi

Upload: others

Post on 03-Jun-2020

12 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Reactive Functional Programmingee380.stanford.edu/Abstracts/110518-slides.pdf · Reactive Functional Programming (or, Is That a Parenthesis in Your Pocket?) Shriram Krishnamurthi

Reactive Functional Programming

(or, Is That a Parenthesis in Your Pocket?)

Shriram Krishnamurthi

Page 2: Reactive Functional Programmingee380.stanford.edu/Abstracts/110518-slides.pdf · Reactive Functional Programming (or, Is That a Parenthesis in Your Pocket?) Shriram Krishnamurthi

PascalPascal

Page 3: Reactive Functional Programmingee380.stanford.edu/Abstracts/110518-slides.pdf · Reactive Functional Programming (or, Is That a Parenthesis in Your Pocket?) Shriram Krishnamurthi

begin

beginbegin

begin

begin

… begin

end;

end;

…end;

end;

begin

begin

end;end;

end;

end;

end.

3

Page 4: Reactive Functional Programmingee380.stanford.edu/Abstracts/110518-slides.pdf · Reactive Functional Programming (or, Is That a Parenthesis in Your Pocket?) Shriram Krishnamurthi

“Pascal is part of the same machinery as hall passesPascal is part of the same machinery as hall passes, dress codes, advisors’ signatures, single-sex dorms, and so on.”

—Brian Harvey

4

Page 5: Reactive Functional Programmingee380.stanford.edu/Abstracts/110518-slides.pdf · Reactive Functional Programming (or, Is That a Parenthesis in Your Pocket?) Shriram Krishnamurthi

The Flow of ControlThe Flow of Control

ModelThe world is in charge,h Modelnot the program

5

Page 6: Reactive Functional Programmingee380.stanford.edu/Abstracts/110518-slides.pdf · Reactive Functional Programming (or, Is That a Parenthesis in Your Pocket?) Shriram Krishnamurthi

flights from PVD to CDGmust

remember

list of flightslist of flights

the third outward flightmust

remember

restricted list of return flights

remember restricted

list of return

the second return flight

flights

ok

6

Page 7: Reactive Functional Programmingee380.stanford.edu/Abstracts/110518-slides.pdf · Reactive Functional Programming (or, Is That a Parenthesis in Your Pocket?) Shriram Krishnamurthi

user typed ‘(’

ok

user moved mousedifferentkinds ofhandlers

ok

user clicked mouse button

ok

7

Page 8: Reactive Functional Programmingee380.stanford.edu/Abstracts/110518-slides.pdf · Reactive Functional Programming (or, Is That a Parenthesis in Your Pocket?) Shriram Krishnamurthi

user moved right 10 yards loc: (0,0)

ok loc: (10,0)

user tilted phone left loc: (10,0)

ok loc: (10,0)

user moved left 10 yards loc: (10,0)

ok loc: (0,0)

8

Page 9: Reactive Functional Programmingee380.stanford.edu/Abstracts/110518-slides.pdf · Reactive Functional Programming (or, Is That a Parenthesis in Your Pocket?) Shriram Krishnamurthi

9

Page 10: Reactive Functional Programmingee380.stanford.edu/Abstracts/110518-slides.pdf · Reactive Functional Programming (or, Is That a Parenthesis in Your Pocket?) Shriram Krishnamurthi

var timerID = null;

timers, initialization,overlapvar timerID null;

var elapsedTime = 0;

function doEverySecond() {

overlap,interference,callbacks… oh my!

elapsedTime += 1;document.getElementById('curTime').innerHTML = elapsedTime; }

f ti t tTi () {function startTimer() {timerId = setInterval(doEverySecond, 1000); }

function resetElapsed() {elapsedTime = 0; }elapsedTime = 0; }

<body onload="startTimer()"><input id="reset" type="button" value="Reset" p yp

onclick="resetElapsed()" /><div id='curTime'> </div></body>

10

Page 11: Reactive Functional Programmingee380.stanford.edu/Abstracts/110518-slides.pdf · Reactive Functional Programming (or, Is That a Parenthesis in Your Pocket?) Shriram Krishnamurthi

Callback

idvoid

d b

void

database,memory store,Web

Event Loop

service,hidden fields,cont’ns

11OS Program

p cont ns

Page 12: Reactive Functional Programmingee380.stanford.edu/Abstracts/110518-slides.pdf · Reactive Functional Programming (or, Is That a Parenthesis in Your Pocket?) Shriram Krishnamurthi

(define gauge(define gauge(new gauge% [label "Elapsed"]

[range 150]))

(new timer%[interval 100][callback (lambda (event)

(send gauge set-value(add1 (send gauge get-value))))])

( b tt %(new button%[label "Reset"][callback (lambda (event)

(send gauge set-value 0))])(send gauge set value 0))])

12

Page 13: Reactive Functional Programmingee380.stanford.edu/Abstracts/110518-slides.pdf · Reactive Functional Programming (or, Is That a Parenthesis in Your Pocket?) Shriram Krishnamurthi

(define gauge(define gauge(new gauge% [label "Elapsed"]

[range 150]))

(new timer%[interval 100][callback (lambda (event)

(send gauge set-value(add1 (send gauge get-value))))])

( b tt %(new button%[label "Reset"][callback (lambda (event)

(send gauge set-value 0))])(send gauge set value 0))])

13

Page 14: Reactive Functional Programmingee380.stanford.edu/Abstracts/110518-slides.pdf · Reactive Functional Programming (or, Is That a Parenthesis in Your Pocket?) Shriram Krishnamurthi

interface ChangeListener extends EventListener {void stateChanged(ChangeEvent e) { ... } }

interface ActionListener extends EventListener {void actionPerformed(ActionEvent e) { ... } }

interface MouseListener extends EventListener {void mouseClicked(MouseEvent e) { }void mouseClicked(MouseEvent e) { ... }void mouseEntered(MouseEvent e) { ... }void mouseExited(MouseEvent e) { ... }void mousePressed(MouseEvent e) { }void mousePressed(MouseEvent e) { ... }void mouseReleased(MouseEvent e) { ... } }

14

Page 15: Reactive Functional Programmingee380.stanford.edu/Abstracts/110518-slides.pdf · Reactive Functional Programming (or, Is That a Parenthesis in Your Pocket?) Shriram Krishnamurthi

interface ChangeListener extends EventListener {void stateChanged(ChangeEvent e) { ... } }

interface ActionListener extends EventListener {void actionPerformed(ActionEvent e) { ... } }

interface MouseListener extends EventListener {void mouseClicked(MouseEvent e) { }void mouseClicked(MouseEvent e) { ... }void mouseEntered(MouseEvent e) { ... }void mouseExited(MouseEvent e) { ... }void mousePressed(MouseEvent e) { }void mousePressed(MouseEvent e) { ... }void mouseReleased(MouseEvent e) { ... } }

15

Page 16: Reactive Functional Programmingee380.stanford.edu/Abstracts/110518-slides.pdf · Reactive Functional Programming (or, Is That a Parenthesis in Your Pocket?) Shriram Krishnamurthi

mainLoop : unit -> unit pcloseTk : unit -> unit

destroy : 'a Widget.widget -> unit update : unit -> unit

pack : ... -> 'd Widget.widget list -> unit grid : ... -> 'b Widget.widget list -> unit

raise_window : ?above:'a Widget.widget-> 'b Widget.widget -> unit> b Widget.widget > unit

bind : events:event list-> 'a Widget.widget -> unit

16

Page 17: Reactive Functional Programmingee380.stanford.edu/Abstracts/110518-slides.pdf · Reactive Functional Programming (or, Is That a Parenthesis in Your Pocket?) Shriram Krishnamurthi

mainLoop : unit -> unitpcloseTk : unit -> unit

destroy : 'a Widget.widget -> unitupdate : unit -> unit

pack : ... -> 'd Widget.widget list -> unitgrid : ... -> 'b Widget.widget list -> unit

raise_window : ?above:'a Widget.widget-> 'b Widget.widget -> unit> b Widget.widget > unit

bind : events:event list-> 'a Widget.widget -> unit

17

Page 18: Reactive Functional Programmingee380.stanford.edu/Abstracts/110518-slides.pdf · Reactive Functional Programming (or, Is That a Parenthesis in Your Pocket?) Shriram Krishnamurthi

propagateEvent :: IO () p p gselect :: Selecting w => Event w (IO ()) mouse :: Reactive w => Event w (EventMouse -> IO ())keyboard :: Reactive w => y

Event w (EventKey -> IO ())resize :: Reactive w => Event w (IO ())focus :: Reactive w => Event w (Bool -> IO ())activate :: Reactive w => Event w (Bool -> IO ())enter :: Reactive w => Event w (Point -> IO ())leave :: Reactive w => Event w (Point -> IO ())( ())motion :: Reactive w => Event w (Point -> IO ())drag :: Reactive w => Event w (Point -> IO ())click :: Reactive w => Event w (Point -> IO ())c c :: eact e e t ( o t O ())unclick :: Reactive w => Event w (Point -> IO ())doubleClick :: Reactive w =>

Event w (Point -> IO ())

18

( ())

Page 19: Reactive Functional Programmingee380.stanford.edu/Abstracts/110518-slides.pdf · Reactive Functional Programming (or, Is That a Parenthesis in Your Pocket?) Shriram Krishnamurthi

propagateEvent :: IO ()p p gselect :: Selecting w => Event w (IO ()) mouse :: Reactive w => Event w (EventMouse -> IO ())keyboard :: Reactive w => y

Event w (EventKey -> IO ())resize :: Reactive w => Event w (IO ())focus :: Reactive w => Event w (Bool -> IO ())activate :: Reactive w => Event w (Bool -> IO ())enter :: Reactive w => Event w (Point -> IO ())leave :: Reactive w => Event w (Point -> IO ())( ())motion :: Reactive w => Event w (Point -> IO ())drag :: Reactive w => Event w (Point -> IO ())click :: Reactive w => Event w (Point -> IO ())c c :: eact e e t ( o t O ())unclick :: Reactive w => Event w (Point -> IO ())doubleClick :: Reactive w =>

Event w (Point -> IO ())

19

( ())

Page 20: Reactive Functional Programmingee380.stanford.edu/Abstracts/110518-slides.pdf · Reactive Functional Programming (or, Is That a Parenthesis in Your Pocket?) Shriram Krishnamurthi

So What?So What?

We care deeply about functions because ofWe care deeply about functions because of

T iTesting

Composition

Education

20

Page 21: Reactive Functional Programmingee380.stanford.edu/Abstracts/110518-slides.pdf · Reactive Functional Programming (or, Is That a Parenthesis in Your Pocket?) Shriram Krishnamurthi

SolutionsSolutions

Lazy evaluation(H k ll FRP)(Haskell FRP)Continuations

(PLT Scheme Web Server)Dataflow + state + GUIs in call-by-valueDataflow + state + GUIs in call by value

(FrTime, Flapjax, …)

21

Page 22: Reactive Functional Programmingee380.stanford.edu/Abstracts/110518-slides.pdf · Reactive Functional Programming (or, Is That a Parenthesis in Your Pocket?) Shriram Krishnamurthi

Why void?

The hostilevoid

The hostileoperating

system

Why hostile?

voidBecause

it’s neutral

Butneutrality hostility

void

y y

Neutrals can cooperate

(impartially)22

Page 23: Reactive Functional Programmingee380.stanford.edu/Abstracts/110518-slides.pdf · Reactive Functional Programming (or, Is That a Parenthesis in Your Pocket?) Shriram Krishnamurthi

User

Callback

OS

“W ld”“World”

“World”

“World”

“World”

23

Page 24: Reactive Functional Programmingee380.stanford.edu/Abstracts/110518-slides.pdf · Reactive Functional Programming (or, Is That a Parenthesis in Your Pocket?) Shriram Krishnamurthi

Current value of World

Will invokeevent-specific

functions

Might be enriched with

additional event

New value f W ld

additional event information

of World

The information you need to restore the computation pfrom a crash

24

Page 25: Reactive Functional Programmingee380.stanford.edu/Abstracts/110518-slides.pdf · Reactive Functional Programming (or, Is That a Parenthesis in Your Pocket?) Shriram Krishnamurthi

What’s changing?

Th l i f h UFOple

The location of the UFO.How do we represent it?

The modelinci

p

As a coordinate pair.The model

d Pr

iw

ood

Modelviewtransformation

olly

w

Modelmodelt f tihe

Ho

transformation

25Th

Page 26: Reactive Functional Programmingee380.stanford.edu/Abstracts/110518-slides.pdf · Reactive Functional Programming (or, Is That a Parenthesis in Your Pocket?) Shriram Krishnamurthi

26

Page 27: Reactive Functional Programmingee380.stanford.edu/Abstracts/110518-slides.pdf · Reactive Functional Programming (or, Is That a Parenthesis in Your Pocket?) Shriram Krishnamurthi

(define width 400)(define height 300)(define initial world 0)(define initial-world 0)(define PLANE )

world sceneworld scene(define (render w)(place-image PLANE

(* w 10)(image-height PLANE)(empty-scene width height)))

world world(define (incr-time w)(add1 w))

(big-bang initial-world(on-tick incr-time)(t d d ))

model modeld l (to-draw render))

27

modelview

Page 28: Reactive Functional Programmingee380.stanford.edu/Abstracts/110518-slides.pdf · Reactive Functional Programming (or, Is That a Parenthesis in Your Pocket?) Shriram Krishnamurthi

(define width 400)(define height 300)(define initial world 0)(define initial-world 0)(define PLANE )

world sceneworld scene(define (render w)(place-image PLANE

(* w 10)(image-height PLANE)(empty-scene width height)))

(animate initial-world render)

Functions as arguments (sssh!)

28

Page 29: Reactive Functional Programmingee380.stanford.edu/Abstracts/110518-slides.pdf · Reactive Functional Programming (or, Is That a Parenthesis in Your Pocket?) Shriram Krishnamurthi

World

World

World

Scene

World

W ldWorld

29

Page 30: Reactive Functional Programmingee380.stanford.edu/Abstracts/110518-slides.pdf · Reactive Functional Programming (or, Is That a Parenthesis in Your Pocket?) Shriram Krishnamurthi

World

World

World

Scene

World

Bool

30

Page 31: Reactive Functional Programmingee380.stanford.edu/Abstracts/110518-slides.pdf · Reactive Functional Programming (or, Is That a Parenthesis in Your Pocket?) Shriram Krishnamurthi

k k

on-click :: w btn w

on-move :: w dist w

on-tilt :: w incl w

on-key :: w key w

on move :: w dist w

to-draw :: w scene

on-tick :: w w

stop-when :: w boolstop when :: w bool

31

Page 32: Reactive Functional Programmingee380.stanford.edu/Abstracts/110518-slides.pdf · Reactive Functional Programming (or, Is That a Parenthesis in Your Pocket?) Shriram Krishnamurthi

32

Page 33: Reactive Functional Programmingee380.stanford.edu/Abstracts/110518-slides.pdf · Reactive Functional Programming (or, Is That a Parenthesis in Your Pocket?) Shriram Krishnamurthi

(define WIDTH 300)(define HEIGHT 300)

(define-struct vel (x y))

(define target (make-posn (random WIDTH) …))

(define-struct world (posn r vel))

(define initial-world (make-world (make-posn …)

30 (make-vel 0 0)))

world bool(define (game-ends? w)( (< ( ld ) 1)(or (<= (world-r w) 1)

(collide? w)))

33

Page 34: Reactive Functional Programmingee380.stanford.edu/Abstracts/110518-slides.pdf · Reactive Functional Programming (or, Is That a Parenthesis in Your Pocket?) Shriram Krishnamurthi

world world(define (tick w)(make world (posn+vel (world posn w) (world vel w))(make-world (posn+vel (world-posn w) (world-vel w))

(- (world-r w) 1/3)(world-vel w)))

world number number number world(define (tilt w azimuth pitch roll)(update-world-vel w

(big bang initial world

(update world vel w(make-vel roll (- pitch))))

(big-bang initial-world(to-draw render)(on-tick tick)(on-tilt tilt) (stop-when game-ends?))

34

Page 35: Reactive Functional Programmingee380.stanford.edu/Abstracts/110518-slides.pdf · Reactive Functional Programming (or, Is That a Parenthesis in Your Pocket?) Shriram Krishnamurthi

35

Page 36: Reactive Functional Programmingee380.stanford.edu/Abstracts/110518-slides.pdf · Reactive Functional Programming (or, Is That a Parenthesis in Your Pocket?) Shriram Krishnamurthi

36

Page 37: Reactive Functional Programmingee380.stanford.edu/Abstracts/110518-slides.pdf · Reactive Functional Programming (or, Is That a Parenthesis in Your Pocket?) Shriram Krishnamurthi

2006 PISA USA t i t 20 i th2006 PISA scores: USA not in top 20 in math, science, or reading

“Economic Time Bomb”—June Kronholz, WSJ

37

Page 38: Reactive Functional Programmingee380.stanford.edu/Abstracts/110518-slides.pdf · Reactive Functional Programming (or, Is That a Parenthesis in Your Pocket?) Shriram Krishnamurthi

Salient Curricular FactsSalient Curricular Facts

STEM “sorting” occurs in middle-school

Functions are a major barrier

Algebra only over numbers is boringAlgebra only over numbers is boring

38

Page 39: Reactive Functional Programmingee380.stanford.edu/Abstracts/110518-slides.pdf · Reactive Functional Programming (or, Is That a Parenthesis in Your Pocket?) Shriram Krishnamurthi

A train leaves Chicago at 6pm, traveling east at 50mph…

39

Page 40: Reactive Functional Programmingee380.stanford.edu/Abstracts/110518-slides.pdf · Reactive Functional Programming (or, Is That a Parenthesis in Your Pocket?) Shriram Krishnamurthi

40

Page 41: Reactive Functional Programmingee380.stanford.edu/Abstracts/110518-slides.pdf · Reactive Functional Programming (or, Is That a Parenthesis in Your Pocket?) Shriram Krishnamurthi

41

Page 42: Reactive Functional Programmingee380.stanford.edu/Abstracts/110518-slides.pdf · Reactive Functional Programming (or, Is That a Parenthesis in Your Pocket?) Shriram Krishnamurthi

5 + 3

17 = □ - 11

x2 = 25 - 16 process

17 33 3 20

1729 = x3 - 1000

17 + 33 = 3x - 20

1729 x 1000

f(x) = x2 objectf( ) j

• with attributes

42• with multiple representations

Page 43: Reactive Functional Programmingee380.stanford.edu/Abstracts/110518-slides.pdf · Reactive Functional Programming (or, Is That a Parenthesis in Your Pocket?) Shriram Krishnamurthi

FunctionsFunctionsf(x)

30

40

50

60

70

f(x)

0

10

20

x 1.4 1.9 2.4 2.9 3.4 3.9 4.4 4.9 5.4 5.9 6.4 6.9 7.4 7.9

f(x)

f(x) = x2Program

x 1 2 3 4 5 6

f(x) 1 4 9 16 25 36Tests

43

f( )

Page 44: Reactive Functional Programmingee380.stanford.edu/Abstracts/110518-slides.pdf · Reactive Functional Programming (or, Is That a Parenthesis in Your Pocket?) Shriram Krishnamurthi

44

Page 45: Reactive Functional Programmingee380.stanford.edu/Abstracts/110518-slides.pdf · Reactive Functional Programming (or, Is That a Parenthesis in Your Pocket?) Shriram Krishnamurthi

45

Page 46: Reactive Functional Programmingee380.stanford.edu/Abstracts/110518-slides.pdf · Reactive Functional Programming (or, Is That a Parenthesis in Your Pocket?) Shriram Krishnamurthi

Vital StatisticsVital Statistics

Middle-school, after-school (predominantly)Middle school, after school (predominantly)All teaching by volunteers (professionals, students)Day 1: design your own game; 9 weeks: implement itDay 1: design your own game; 9 weeks: implement it

24% female; 70% reporting race are minority;24% female; 70% reporting race are minority; 70% on free or reduced-price lunch

Emphasis on: testing, pair-programming, code-review

46

Page 47: Reactive Functional Programmingee380.stanford.edu/Abstracts/110518-slides.pdf · Reactive Functional Programming (or, Is That a Parenthesis in Your Pocket?) Shriram Krishnamurthi

Over 700 students (average age: 11y9m) so farAbout 200 new students this past year, growing /year

47

Page 48: Reactive Functional Programmingee380.stanford.edu/Abstracts/110518-slides.pdf · Reactive Functional Programming (or, Is That a Parenthesis in Your Pocket?) Shriram Krishnamurthi

48

Page 49: Reactive Functional Programmingee380.stanford.edu/Abstracts/110518-slides.pdf · Reactive Functional Programming (or, Is That a Parenthesis in Your Pocket?) Shriram Krishnamurthi

TechnologyTechnology

Scheme (Racket) on the desktopScheme (Racket) on the desktopScheme J2ME compiler for AndroidS h J S iScheme JavaScript

Works on both phones and browsersNeed to deal with asynchronyNeed to deal with re-entranceNeed to deal with re entrance(Futures, continuations, …)

W ld d R k J J S iWorld ported to Racket, Java, JavaScript…49

Page 50: Reactive Functional Programmingee380.stanford.edu/Abstracts/110518-slides.pdf · Reactive Functional Programming (or, Is That a Parenthesis in Your Pocket?) Shriram Krishnamurthi

50

Page 51: Reactive Functional Programmingee380.stanford.edu/Abstracts/110518-slides.pdf · Reactive Functional Programming (or, Is That a Parenthesis in Your Pocket?) Shriram Krishnamurthi

The World is Not EnoughThe World is Not Enough

Page 52: Reactive Functional Programmingee380.stanford.edu/Abstracts/110518-slides.pdf · Reactive Functional Programming (or, Is That a Parenthesis in Your Pocket?) Shriram Krishnamurthi

A t ti d t ti f t t f l APIAutomatic adaptation of stateful APIs

In-place updates of world state

“Universe” for distributed computing

Moore versus Mealy

52

Page 53: Reactive Functional Programmingee380.stanford.edu/Abstracts/110518-slides.pdf · Reactive Functional Programming (or, Is That a Parenthesis in Your Pocket?) Shriram Krishnamurthi

Take-Away MessagesTake Away Messages

• Programming with functions creates aProgramming with functions creates a virtuous cycle

• Middle-schoolers can write tests, i d isurvive code-reviews, etc.

• Programming models matter, especially across platformsp y p

53

Page 54: Reactive Functional Programmingee380.stanford.edu/Abstracts/110518-slides.pdf · Reactive Functional Programming (or, Is That a Parenthesis in Your Pocket?) Shriram Krishnamurthi

54

Page 55: Reactive Functional Programmingee380.stanford.edu/Abstracts/110518-slides.pdf · Reactive Functional Programming (or, Is That a Parenthesis in Your Pocket?) Shriram Krishnamurthi

www.bootstrapworld.orgwww.bootstrapworld.org

www.facebook.com/BootstrapWorld/ p

Thanks:

Danny YooKathi Fisler

Emmanuel SchanzerMatthias Felleisen

Zhe Zhang (rest )

55