Download Event (Behavior a) - Wiki - Western State Colorado University

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Functional Reactive Programming
Simplifying Interaction
John Peterson
Western State
Colorado University
Gunnison, CO
November 2013
WHERE THE HECK??
That’s Us
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.
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)

November 2013
Synchronous Circuits
You can’t see any order in the evaluations of the d’s
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!
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
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
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
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

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
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
November 2013
3D Game Engines
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))
November 2013
The WSCU Computer Camp





November 2013
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
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

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))
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)
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
November 2013
Kinetic Sculpture
Erik Brunvand visited Western last
Spring and got everyone excited
about kinetic sculpture.
November 2013
Raspberry Pi
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)
November 2013
Work in Progress
November 2013
Work in Progress
November 2013
WSCU Computer Camp
western.edu/computerscience
November 2013
Related documents