November 2013 Functional Reactive Programming Simplifying Interaction John Peterson Western State...

25
November 2013 Functional Reactive Programming Simplifying Interaction John Peterson Western State Colorado University Gunnison, CO

description

November 2013 Reactive Programming Reactive programming is used to make time flow implicit – dataflow languages and spreadsheets are examples. Things get harder when you add external stimulus (GUI) and incorporate stateful behaviors.

Transcript of November 2013 Functional Reactive Programming Simplifying Interaction John Peterson Western State...

Page 1: November 2013 Functional Reactive Programming Simplifying Interaction John Peterson Western State Colorado University Gunnison, CO.

November 2013

Functional Reactive ProgrammingSimplifying InteractionJohn PetersonWestern State Colorado UniversityGunnison, CO

Page 2: November 2013 Functional Reactive Programming Simplifying Interaction John Peterson Western State Colorado University Gunnison, CO.

November 2013

WHERE THE HECK??

That’s Us

Page 3: November 2013 Functional Reactive Programming Simplifying Interaction John Peterson Western State Colorado University Gunnison, CO.

November 2013

Reactive ProgrammingReactive programming is used to make time flow implicit – dataflow languages and spreadsheets are examples.Things get harder when you add external stimulus (GUI) and incorporate stateful behaviors.

Page 4: November 2013 Functional Reactive Programming Simplifying Interaction John Peterson Western State Colorado University Gunnison, CO.

November 2013

Why Reactive?More declarative - no explicit

notification schemes (observer pattern)

Values rather than actionsTime management (glitch

avoidance) – logical time steps instead of wall clock time.

Time-aware constructs (stateful)

Page 5: November 2013 Functional Reactive Programming Simplifying Interaction John Peterson Western State Colorado University Gunnison, CO.

November 2013

Synchronous Circuits

You can’t see any order in the evaluations of the d’s

Page 6: November 2013 Functional Reactive Programming Simplifying Interaction John Peterson Western State Colorado University Gunnison, CO.

November 2013

FRPFRP is a functional take on reactive programmingOriginally developed by Conal Elliot and Paul HudakEmbeds reactivity in a purely functional context (Haskell)Type signatures are very descriptive!

Page 7: November 2013 Functional Reactive Programming Simplifying Interaction John Peterson Western State Colorado University Gunnison, CO.

November 2013

FRP, Behaviors, and Events

Event a Denotes an event stream. Each occurrence delivers value of type akeyboard :: Event Char

Behavior a Denotes a continuously available value of type amouse :: Behavior Point2

Page 8: November 2013 Functional Reactive Programming Simplifying Interaction John Peterson Western State Colorado University Gunnison, CO.

November 2013

Classic FRP Operationslift0 :: a -> Behavior alift1 :: (a -> b) -> Behavior a -> Behavior bhold :: a -> Event a -> Behavior awhen :: Behavior Bool -> Event ()snap :: Event a -> Behavior b -> Event b(-=>) :: Event b -> a -> Event atags :: Event b -> [a] -> Event a(.|.) :: Event a -> Event a -> Event aswitch :: Behavior a -> Event (Behavior a) -> Behavior aintegral :: Behavior Double -> Behavior Doubleclock :: Double -> Event Doubleonce :: Event a -> Event a

Page 9: November 2013 Functional Reactive Programming Simplifying Interaction John Peterson Western State Colorado University Gunnison, CO.

November 2013

Reactive AlgebraWe can understand FRP by looking at the underlying algebra:

lift2 f . lift2 g = lift2 (f . g)once . once = oncee -=> x -=> y = e -=> ye .|. e = esnap e (lift0 x) = e -=> x

Page 10: November 2013 Functional Reactive Programming Simplifying Interaction John Peterson Western State Colorado University Gunnison, CO.

November 2013

Principles of FRPSingle value: signals have only one value

at each time stepEvaluation order independence: the

update order at each time step is non-observable

Purity: Identical signals initialized at the same time have the same value

Convergence: continuous signals should converge to an “ideal value” as time steps decrease

Immediacy: computations should not be delayed to a later time step

Page 11: November 2013 Functional Reactive Programming Simplifying Interaction John Peterson Western State Colorado University Gunnison, CO.

November 2013

FRP Escapes!As great as Haskell is, sometimes you need to be an another worldWe have developed an embedding of FRP in Python for use by novice programmers

This FRP must co-exist with an underlying object library which captures an interactive domain

Page 12: November 2013 Functional Reactive Programming Simplifying Interaction John Peterson Western State Colorado University Gunnison, CO.

November 2013

O-O FRPObject identity is importantAugment events / behaviors with reactive objects.There is an implicit “world” – the set of active objects (name supply)Includes primitives for object creation / deletion (object factories)Objects have reactive componentsObjects are extensibleComponents are observable

Page 13: November 2013 Functional Reactive Programming Simplifying Interaction John Peterson Western State Colorado University Gunnison, CO.

November 2013

3D Game Engines

Page 14: November 2013 Functional Reactive Programming Simplifying Interaction John Peterson Western State Colorado University Gunnison, CO.

November 2013

The Panda3D EngineOur FRP library sits on top of a game engine libraryModels in the game engine are represented by “reactive proxy” objects

panda(hpr = hpr(time, 0, 0))

Page 15: November 2013 Functional Reactive Programming Simplifying Interaction John Peterson Western State Colorado University Gunnison, CO.

November 2013

The WSCU Computer CampOne week, grades 8 – 9About 7 hours of

computing a dayRafting, kayaking,

climbing, biking, and hikingMany small projects rather

than one big oneLots of helpers so students

don’t get bogged down with bugs / errors

Page 16: November 2013 Functional Reactive Programming Simplifying Interaction John Peterson Western State Colorado University Gunnison, CO.

November 2013

Camp StructureCombine programming with math,

physics, and 3-D designAvoid “drag and drop” style

programmingTeach basic python but not O-O

ProgrammingDon’t avoid the math: 3-D, vectors,

polar coords, integral, trigCombine analytical and creative

activities

Page 17: November 2013 Functional Reactive Programming Simplifying Interaction John Peterson Western State Colorado University Gunnison, CO.

November 2013

Examples: Basic ReactivityA reactive panda:s = slider(min = -3, max = 2)p = panda( hpr = hpr(time, 0, 0), size = time, position = P3(s, 0, 1))

Page 18: November 2013 Functional Reactive Programming Simplifying Interaction John Peterson Western State Colorado University Gunnison, CO.

November 2013

Reaction FunctionsEvents are used to trigger reactions in objects – each reaction has a triggering event and a handler.def blowUp(m, v): fire(position = now(m.position), duration = 1) play(“explosion.wav”) exit(m)p = panda(position = P3(time-3, 0,0))p.react(leftClick(p), blowUp)

Page 19: November 2013 Functional Reactive Programming Simplifying Interaction John Peterson Western State Colorado University Gunnison, CO.

November 2013

AssessmentPython + FRP is a good

combinationTypes would help a lotFunctional programming is not as

easy as it should be in PythonA pre-processor would make error

messages much better and allow some custom syntax

Not suitable for unassisted use

Page 20: November 2013 Functional Reactive Programming Simplifying Interaction John Peterson Western State Colorado University Gunnison, CO.

November 2013

Kinetic SculptureErik Brunvand visited Western last Spring and got everyone excited about kinetic sculpture.

Page 22: November 2013 Functional Reactive Programming Simplifying Interaction John Peterson Western State Colorado University Gunnison, CO.

November 2013

Reactive Sensors / Controlsinput(pin = 1)

light(pin = 1, value = hold(false, tags([true, false], clock(step=1))))Also step motors, distance sensors, relay control (fan / lights)

Page 23: November 2013 Functional Reactive Programming Simplifying Interaction John Peterson Western State Colorado University Gunnison, CO.

November 2013

Work in Progress

Page 24: November 2013 Functional Reactive Programming Simplifying Interaction John Peterson Western State Colorado University Gunnison, CO.

November 2013

Work in Progress

Page 25: November 2013 Functional Reactive Programming Simplifying Interaction John Peterson Western State Colorado University Gunnison, CO.

November 2013

WSCU Computer Camp

western.edu/computerscience