functional reactive programming by gerold meisinger
TRANSCRIPT
inSeminar Methods and Applications of Functional Programming
Johannes Kepler University, Linz SS2012
by Gerold Meisinger
Functional reactive programming
Background
FH Hagenberg Digital Media
FRP diploma thesis Gamedev startup
”Modern Alchemists”
Types of programs
Transformative
Interactive
Reactive
What is FRP?
New paradigm Time-varying values
Behavior a :: Time → a Hybrid modeling
Continous-time Discrete events
React to input
Paradigms (by Van Roy)
Similarities
Paradigms Dataflow
Common Lisp/Cells Temporal logic Control theory ...
Languages Esterel Lustre Lucid Signal Synchrone Simulink Modelica ...
Dataflow programming
FRP implementations
Classic FRP e.g. Fran, FrTime
Push-pull FRP e.g. Reactive
Arrowized FRP e.g. Yampa, Elera
Yampa examples
Robotics GUIs Games Music synthesis Computer vision Networking ...
Classic game programming
interface IUpdateablevoid update( float elapsedTime )
class GameObject : IUpdateableVector3 positionVector3 velocityvoid update( float elapsedTime ) position += elapsedTime * velocity
Yampa semantics
Signal a :: Time → a Only used internally
SF a b :: Signal a → Signal b SF … signal function
data Event t = NoEvent | Event t
Yampa design goals
Continous-time & discrete events Denotative semantics Dynamic structures Higher-orderness Avoid time and space leaks ⇒ Arrow typeclass Synchronous (vs concurrent)
Transformative
f :: a → b
Interactive
Reactive
Temporal
Loop >
Loop > Processing (black-box)
Loop > Processing
Loop > Processing >
Loop > Processing > SF (BB)
Loop > Processing > SF
Loop > Processing > SF
Loop > Processing > SF
Loop > Processing > SF
Loop > Processing > SF Mover
Loop > Processing > Mover
Loop > Processing > Mover
Loop > Processing >
Loop > Processing
Loop > Processing
Loop > Processing Player
Example game: Fran
Haskell typeclasses (by Yorgey)
Arrow class
class Arrow a wherearr :: (b → c) → a b c(<<<) :: a b c → a c d → a b dfirst :: a b c → a (b, d) (c, d)
+ lots of arrow lawsderive combinators: second, (***), (&&&)
Arrow combinators
Derived Arrow combinators
second :: (Arrow a) a b c → a (d, b) (d, c)⇒
second f = arr swap >>> first f >>> arr swapwhere swap (a, b) = (b, a)
(***) :: (Arrow a) a b c → a b' c' → a (b, b') (c, c')⇒
f ⋆⋆⋆ g = first f >>> second g
(&&&) :: (Arrow a) a b c → a b c' → a b (c, c')⇒
f &&& g = arr (λx → (x, x)) >>> (f *** g)
ArrowCircuit
class Arrow a ArrowCircuit a where⇒
loop :: a (b, d) (c, d) → a b cinit :: b → a b b
+ even more laws
Arrow syntax
myArrow x y = proc (a, b) → do c1 ← arrow1 −< a c2 ← arrow3 <<< arrow2 −< b c ← arrow4 −< (c1, c2) rec d ← arrow5 −< (c, b, d) returnA (c, d)
Signal function primitives
Events
Yampa switch
switch :: SF in (out, Event t) → (t → SF in out) → SF in out
Yampa parallel switch
pSwitch :: Functor col => (forall sf. (in → col sf → col (ext, sf))) → col (SF ext out) → SF (in, col out) (Event mng) → (col (SF ext out) → mng → SF in (col out)) → SF in (col out)
Yampa switches
immediate delayedonce switch dSwitchrecurring rSwitch drSwitchparallel broadcasted pSwitchB
rpSwitchBdpSwitchBdrpSwitchB
parallel routed pSwitchrpSwitch
dpSwitchdrpSwitch
continuation kSwitch dkSwitch
Processing
reactimate :: IO (DTime, a) –- input → (b → IO ()) –- output → SF a b → IO ()
Game-Engine
Ongoing research
Semantics "Garbage collecting the semantics of FRP" "Why classic FRP does not fit interactive behavior"
Performance Causal Commutative Arrows (CCA) Concurrency Arrow tuples vs. signal combining
Yampa alternatives
ELM Web runtime and IDE => converts to JavaScripthttp://elm-lang.org
Reactive-Banana Many exampleshttp://apfelmus.nfshost.com