first class signals
play

First-Class Signals Generators Memoization for Functional Reactive - PowerPoint PPT Presentation

First-Class Signals for FRP Wolfgang Jeltsch Introduction FRP concepts First-Class Signals Generators Memoization for Functional Reactive Programming Start time consistency Wolfgang Jeltsch TT U K uberneetika Instituut


  1. First-Class Signals for FRP Wolfgang Jeltsch Introduction FRP concepts First-Class Signals Generators Memoization for Functional Reactive Programming Start time consistency Wolfgang Jeltsch TT¨ U K¨ uberneetika Instituut Teooriaseminar October 13, 2011

  2. First-Class Signals Overview for FRP Wolfgang Jeltsch Introduction FRP concepts Introduction Generators Memoization Start time FRP concepts consistency Generators Memoization Start time consistency

  3. First-Class Signals Overview for FRP Wolfgang Jeltsch Introduction FRP concepts Introduction Generators Memoization FRP concepts Start time consistency Generators Memoization Start time consistency

  4. First-Class Signals Functional Reactive Programming for FRP Wolfgang Jeltsch Introduction ◮ the ideal reactive system: FRP concepts ◮ continuous change Generators ◮ immediate, atomic reactions on events Memoization ◮ not reflected by imperative implementations: Start time consistency ◮ discretization visible ◮ inconsistent intermediate states visible ◮ programmer confronted with technical details: ◮ polling loops ◮ event handlers ◮ goal of functional programming: problem description instead of execution plan ◮ Functional Reactive Programming (FRP): applying this principle to reactive systems

  5. First-Class Signals Implementations for FRP Wolfgang Jeltsch ◮ two ways of implementing FRP: Introduction pull-based system state is repeatedly recomputed FRP concepts push-based state changes are triggered by events Generators ◮ many Haskell EDSLs: Memoization ◮ pull-based: Start time ◮ Fran consistency ◮ Yampa etc. ◮ push-based: ◮ FranTk ◮ Reactive ◮ Grapefruit etc. ◮ EDSLs for other programming languages (all push-based): Java Frapp´ e Scheme FrTime JavaScript Flapjax

  6. First-Class Signals Grapefruit for FRP Wolfgang Jeltsch Introduction FRP concepts Generators Memoization ◮ originally geared towards GUI programming Start time consistency ◮ push-based, because change is rare in GUIs ◮ problem with existing push-based implementations: ◮ no first-class descriptions of temporal behavior ◮ performance problems ◮ a new implementation for solving these issues

  7. First-Class Signals Overview for FRP Wolfgang Jeltsch Introduction FRP concepts Introduction Generators Memoization FRP concepts Start time consistency Generators Memoization Start time consistency

  8. First-Class Signals Signals for FRP Wolfgang Jeltsch Introduction ◮ the heart of FRP FRP concepts ◮ describe temporal behavior Generators ◮ three flavors: Memoization Start time discrete values associated with discrete times: consistency � DSignal � α ≈ [( Time , α )] continuous arbitrary time-varying values: � CSignal � α ≈ Time → α segmented step functions over time: � SSignal � α = ( α, � DSignal � α )

  9. First-Class Signals Examples of signals for FRP Wolfgang Jeltsch Introduction FRP concepts discrete incoming network packets: Generators Memoization DSignal Packet Start time consistency continuous time since the program has started: CSignal DiffTime segmented amount of network traffic: SSignal Integer

  10. First-Class Signals Signal combinators for FRP Wolfgang Jeltsch ◮ functions for constructing signals Introduction FRP concepts ◮ some examples: Generators union :: DSignal α → DSignal α → DSignal α Memoization filter :: ( α → Bool ) → DSignal α → DSignal α Start time consistency scanl :: ( β → α → β ) → β → DSignal α → SSignal β ◮ application of these combinators: p :: DSignal Packet ¨ p = union ¨ ¨ p In ¨ p Out p TCP :: DSignal Packet ¨ p TCP = filter isTCPPacket ¨ ¨ p v :: SSignal Integer ¯ v = scanl ( λ v p → v + size p ) 0 ¨ ¯ p

  11. First-Class Signals Switching for FRP Wolfgang Jeltsch ◮ class Signal of all signal types Introduction ◮ switching combinator: FRP concepts Generators switch :: ( Signal σ ) ⇒ SSignal ( σ α ) → σ α Memoization ◮ possible application: Start time ◮ two segmented signals that represent amount consistency of incoming and outgoing traffic: v In , ¯ ¯ v Out :: SSignal Integer ◮ segmented signal that toggles between these two, depending on user selection: ¯ ¯ v :: SSignal ( SSignal Integer ) ◮ switching creates a signal that always gives the respective amount of traffic: ¯ v Sel :: SSignal Integer v Sel = switch ¯ ¯ v ¯ ◮ ¯ v Sel used as the input of a display component

  12. First-Class Signals Overview for FRP Wolfgang Jeltsch Introduction FRP concepts Introduction Generators Memoization FRP concepts Start time consistency Generators Memoization Start time consistency

  13. First-Class Signals A straightforward push-based implementation for FRP Wolfgang Jeltsch Introduction FRP concepts Generators ◮ updates shall be event-driven Memoization ◮ signal consumers register event handlers Start time consistency ◮ discrete signal is registration action (which yields unregistration action): type Handler α = α → IO () type DSignal α = Handler α → IO ( IO ()) ◮ SSignal implementation directly mirrors the semantics: type SSignal α = ( α, DSignal α )

  14. First-Class Signals Implementation of scanl for FRP Wolfgang Jeltsch Introduction FRP concepts scanl :: ( β → α → β ) → β → DSignal α → SSignal β Generators scanl f y 0 ¨ x = ( y 0 , ¨ y ) where Memoization y = λ h → do ¨ Start time consistency � y ← newIORef y 0 x ( λ x → do ¨ y ← readIORef � y let y ′ = f y x y y ′ writeIORef � h y ′ )

  15. First-Class Signals Generators, not signals for FRP Wolfgang Jeltsch Introduction FRP concepts ◮ registration actions executed once per consumer Generators ◮ when using scanl , every consumer Memoization ◮ creates a mutable variable, initialized at Start time consumption time consistency ◮ registers a handler that updates this variable ◮ two problems: 1. duplication of computations 2. signal values depending on consumption time ◮ intuition: ◮ values of signal types are in fact generators ◮ generator yields a new signal when consumed ◮ signals are not first-class anymore

  16. First-Class Signals Overview for FRP Wolfgang Jeltsch Introduction FRP concepts Introduction Generators Memoization FRP concepts Start time consistency Generators Memoization Start time consistency

  17. First-Class Signals Using native memoization for FRP Wolfgang Jeltsch ◮ Haskell caches computed parts of a data structure if a variable is bound to the structure Introduction FRP concepts ◮ problem: Generators values of DSignal do not contain event values Memoization ◮ changing the data structure: Start time consistency type DSignal α = [( Time , α )] ◮ event streams must be interleaved when computing signal unions: union (( t 1 , x 1 ) : ¨ x 1 ) (( t 2 , x 2 ) : ¨ x 2 ) | t 1 < t 2 = · · · | t 1 ≡ t 2 = · · · | t 1 > t 2 = · · · ◮ problem: comparison of occurrence times must succeed when the first event occurs ◮ our solution: delegate event ordering to the consumers

  18. First-Class Signals Representing discrete signals by vistas for FRP Wolfgang Jeltsch ◮ vista covers every possible event stream interleaving ◮ future behavior depends on which external event source Introduction FRP concepts fires next: Generators type Vista α = Map EventSrc ( Variant α ) Memoization type Variant α = ( α, Vista α ) Start time consistency ◮ vista for union ¨ p In ¨ p Out : e In . . . p In , 2 e In p In , 1 . . . e Out p Out , 1 e In . . . p In , 1 e Out p Out , 1 . . . e Out p Out , 2

  19. First-Class Signals Consuming vistas for FRP Wolfgang Jeltsch Introduction ◮ consumer knows about the order in which FRP concepts event sources fire Generators ◮ evaluates only the relevant path: Memoization Start time e In consistency . . . p In , 2 e In p In , 1 . . . e Out p Out , 1 e In . . . p In , 1 e Out p Out , 1 . . . e Out p Out , 2

  20. First-Class Signals Implementation of combinators for FRP Wolfgang Jeltsch ◮ functional representation of discrete signals leads to functional implementations of combinators Introduction FRP concepts ◮ implementation of scanl : Generators scanl :: ( β → α → β ) → β → DSignal α Memoization → SSignal β Start time consistency scanl f y 0 ¨ x = ( y 0 , a y 0 ¨ x ) where a y = fmap ( λ ( x , ¨ x ) → let y ′ = f y x in ( y ′ , a y ′ ¨ x )) ◮ problem with filter : removing nodes would destroy structure of the vista ◮ solution: make event values optional ◮ modified Variant type: type Variant α = ( Maybe α, DSignal α )

  21. First-Class Signals Overview for FRP Wolfgang Jeltsch Introduction FRP concepts Introduction Generators Memoization FRP concepts Start time consistency Generators Memoization Start time consistency

Recommend


More recommend