Functional Reactive Programming Bob Reynders PLaNES, 4 Februari - - PowerPoint PPT Presentation

functional reactive programming
SMART_READER_LITE
LIVE PREVIEW

Functional Reactive Programming Bob Reynders PLaNES, 4 Februari - - PowerPoint PPT Presentation

Functional Reactive Programming Bob Reynders PLaNES, 4 Februari 2015 1/19 Functional Reactive Programming Event discrete values Mouse.click: Event[Click] List[(Time, A)] Behavior time-varying values Mouse.position:


slide-1
SLIDE 1

1/19

Functional Reactive Programming

Bob Reynders PLaNES, 4 Februari 2015

slide-2
SLIDE 2

2/19

Functional Reactive Programming

◮ Event discrete values

◮ Mouse.click: Event[Click] ◮ ≈ List[(Time, A)]

◮ Behavior time-varying values

◮ Mouse.position: Behavior[Coordinate] ◮ ≈ Time => A

slide-3
SLIDE 3

3/19

FRP API (subset)

Event[T].map[A](f: T => A): Event[A] Beh[T].map[A](f: T => A): Beh[A] // Filter Event[T].keepIf(f: T => Boolean): Event[T] // Lift Beh[T].combine[A, B](b: Beh[A])(f: (T, A) => B): Beh[B] Event[T].merge(e: Event[T]): Event[T] // Sampling Beh[T].sampledBy(e: Event[_]): Event[T] // State Event[T].fold[A](initial: A)(fun: (A, T) => A): Beh[A]

slide-4
SLIDE 4

4/19

GUI Example

// input val plusE: Event[Int] = plusButton.click.map(_ => 1) val minE: Event[Int] = minButton.click.map(_ => -1) val merged: Event[Int] = plusE.merge(minE) // state val counter: Beh[Int] = merged.fold(0) { (acc, e) => acc + e } // view def template(data: Int): GUI = ... val main: Beh[GUI] = counter.map(template)

slide-5
SLIDE 5

5/19

Static Dependency Graph

+1

  • 1

fold 0 main (span(0), span(-1), ...) merge (0, -1, +1, +1, +1, ...) counter (0, -1, 0, 1, 2, …) World

Figure 1: Static graph

◮ First order FRP ◮ Static guarantees

slide-6
SLIDE 6

6/19

Dynamic Dependency Graph

New

Figure 2: Switch

◮ Higher order FRP ◮ Event[Behavior[T]].switch: Behavior[T]

slide-7
SLIDE 7

7/19

Glitch-freedom

x x2 always True

val x: Beh[Int] val x2 = x.map(_ * 2) val alwaysTrue = x.combine(x2)(_ <= _)

slide-8
SLIDE 8

8/19

Multi-tier Functional Reactive Programming

slide-9
SLIDE 9

9/19

Multi-tier Functional Reactive Programming

// One language ServerEvent[T].map[A](f: T => A): ServerEvent[A] ClientEvent[T].map[A](f: Rep[T] => Rep[A]): ClientEvent[A] ...

◮ Embed Javascript as a DSL in Scala

◮ Rep[String] = (Client) Javascript String ◮ String = (Server) Scala String

slide-10
SLIDE 10

10/19

Multi-tier API Extension

// Replication between tiers ClientEvent[T].toServerAnon: ServerEvent[T] ServerEvent[T].toAllClients: ClientEvent[T] // Server -> Client ServerBeh[T].toAllClients: ClientBeh[T]

slide-11
SLIDE 11

11/19

Chat Application

// prepare input val nameB: ClientBeh[String] = nameInput.text val msgB: ClientBeh[String] = msgInput.text val clickE: ClientEvent[MouseData] = sendBtn.click // model entries (across tiers!) val entryB = nameB.combine(msgB)(_ + ": " + _) val submitE = entryB.sampledBy(clickE).toServerAnon // model server application state val chatB: ServerBeh[List[String]] = submitE.fold(List.empty[String]) { (acc, entry) => entry :: acc } // view (template on the client) def template(view: Rep[List[String]]): Rep[GUI] = ... val main: ClientBeh[GUI] = chatB.toAllClients.map(template)

slide-12
SLIDE 12

12/19

Chat Application

slide-13
SLIDE 13

13/19

Multi-tier API Extension

// Client-aware replication // - Track sources ClientEvent[T].toServer: ServerEvent[(Client, T)] ServerEvent[Client => Option[T]].toClient: ClientEvent[T] ServerBeh[Client => T].toClient: ClientBeh[T]

slide-14
SLIDE 14

14/19

Multi-tier API Extension

// Generalise to categories of programs Event[C1, T].anonTo[C2]: Event[C2, T] // - Track sources Event[C1, Id[C2] => Option[T]].to[C2]: Event[C2, (Id[C1], T)]

slide-15
SLIDE 15

15/19

Glitch-freedom

val src: ClientEvent[Int] val x2 = src.map(_ * 2).toServer val alwaysTrue = src.hold(0).combine(x2.hold(0))(_ < _)

slide-16
SLIDE 16

16/19

Glitch-freedom: naive

◮ 1 propagation on the client → 2 on the server Multi-tier FRP toServer Client Src Server Src’ x2 x2’ _ < _

slide-17
SLIDE 17

17/19

Glitch-freedom: glitch-free tiers

◮ 1 propagation on the client → 1 on the server Multi-tier FRP toServer Client Src Server Src’ x2 x2’ _ < _

slide-18
SLIDE 18

18/19

Glitch-freedom: total

Figure 3: Distributed REScala: An Update Algorithm for Distributed Reactive Programming

slide-19
SLIDE 19

19/19

Network Overhead

val serverB: Behavior[List[Behavior[Int]]] // changes of Int val serverB: Behavior[List[Int]] // changes of List[Int] val incServerB: IncBehavior[List[Int], Delta] // changes of Delta