Download presentation
Presentation is loading. Please wait.
Published byBarrie Moore Modified over 9 years ago
1
November 2013 Functional Reactive Programming Simplifying Interaction John Peterson Western State Colorado University Gunnison, CO
2
November 2013 WHERE THE HECK?? That’s Us
3
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.
4
November 2013 Why Reactive? More declarative - no explicit notification schemes (observer pattern) Values rather than actions Time management (glitch avoidance) – logical time steps instead of wall clock time. Time-aware constructs (stateful)
5
November 2013 Synchronous Circuits You can’t see any order in the evaluations of the d’s
6
November 2013 FRP FRP is a functional take on reactive programming Originally developed by Conal Elliot and Paul Hudak Embeds reactivity in a purely functional context (Haskell) Type signatures are very descriptive!
7
November 2013 FRP, Behaviors, and Events Event a Denotes an event stream. Each occurrence delivers value of type a keyboard :: Event Char Behavior a Denotes a continuously available value of type a mouse :: Behavior Point2
8
November 2013 Classic FRP Operations lift0 :: a -> Behavior a lift1 :: (a -> b) -> Behavior a -> Behavior b hold :: a -> Event a -> Behavior a when :: Behavior Bool -> Event () snap :: Event a -> Behavior b -> Event b (-=>) :: Event b -> a -> Event a tags :: Event b -> [a] -> Event a (.|.) :: Event a -> Event a -> Event a switch :: Behavior a -> Event (Behavior a) -> Behavior a integral :: Behavior Double -> Behavior Double clock :: Double -> Event Double once :: Event a -> Event a
9
November 2013 Reactive Algebra We can understand FRP by looking at the underlying algebra: lift2 f. lift2 g = lift2 (f. g) once. once = once e -=> x -=> y = e -=> y e.|. e = e snap e (lift0 x) = e -=> x
10
November 2013 Principles of FRP Single value: signals have only one value at each time step Evaluation 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
11
November 2013 FRP Escapes! As great as Haskell is, sometimes you need to be an another world We 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
12
November 2013 O-O FRP Object identity is important Augment 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 components Objects are extensible Components are observable
13
November 2013 3D Game Engines
14
November 2013 The Panda3D Engine Our FRP library sits on top of a game engine library Models in the game engine are represented by “reactive proxy” objects panda(hpr = hpr(time, 0, 0))
15
November 2013 The WSCU Computer Camp One week, grades 8 – 9 About 7 hours of computing a day Rafting, kayaking, climbing, biking, and hiking Many small projects rather than one big one Lots of helpers so students don’t get bogged down with bugs / errors
16
November 2013 Camp Structure Combine programming with math, physics, and 3-D design Avoid “drag and drop” style programming Teach basic python but not O-O Programming Don’t avoid the math: 3-D, vectors, polar coords, integral, trig Combine analytical and creative activities
17
November 2013 Examples: Basic Reactivity A reactive panda: s = slider(min = -3, max = 2) p = panda( hpr = hpr(time, 0, 0), size = time, position = P3(s, 0, 1))
18
November 2013 Reaction Functions Events 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)
19
November 2013 Assessment Python + FRP is a good combination Types would help a lot Functional programming is not as easy as it should be in Python A pre-processor would make error messages much better and allow some custom syntax Not suitable for unassisted use
20
November 2013 Kinetic Sculpture Erik Brunvand visited Western last Spring and got everyone excited about kinetic sculpture.
21
November 2013 Raspberry Pi
22
November 2013 Reactive Sensors / Controls input(pin = 1) light(pin = 1, value = hold(false, tags([true, false], clock(step=1)))) Also step motors, distance sensors, relay control (fan / lights)
23
November 2013 Work in Progress
24
November 2013 Work in Progress
25
November 2013 WSCU Computer Camp western.edu/computerscience
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.