November 2013 Functional Reactive Programming Simplifying Interaction John Peterson Western State...
-
Upload
barrie-moore -
Category
Documents
-
view
224 -
download
0
description
Transcript of November 2013 Functional Reactive Programming Simplifying Interaction John Peterson Western State...
November 2013
Functional Reactive ProgrammingSimplifying InteractionJohn PetersonWestern State Colorado UniversityGunnison, CO
November 2013
WHERE THE HECK??
That’s Us
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.
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)
November 2013
Synchronous Circuits
You can’t see any order in the evaluations of the d’s
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!
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
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
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
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
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
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
November 2013
3D Game Engines
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))
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
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
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))
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)
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
November 2013
Kinetic SculptureErik Brunvand visited Western last Spring and got everyone excited about kinetic sculpture.
November 2013
Raspberry Pi
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)
November 2013
Work in Progress
November 2013
Work in Progress
November 2013
WSCU Computer Camp
western.edu/computerscience