Functional Reactive Programming Maximilian Krome Specification - - PowerPoint PPT Presentation

functional reactive programming
SMART_READER_LITE
LIVE PREVIEW

Functional Reactive Programming Maximilian Krome Specification - - PowerPoint PPT Presentation

Functional Reactive Programming Maximilian Krome Specification Languages for Verification Maximilian Krome SPLAV 1/30 Mathematical Roots Formal mathematical description Provability What versus How Maximilian Krome


slide-1
SLIDE 1

Functional Reactive Programming

Maximilian Krome Specification Languages for Verification

Maximilian Krome SPLAV 1/30

slide-2
SLIDE 2

Mathematical Roots

◮ Formal mathematical description ◮ Provability ◮ ”What” versus ”How”

Maximilian Krome SPLAV 2/30

slide-3
SLIDE 3

Concepts of Functional Programming

◮ Referential Transparency (No reassignment) ◮ Purity (No side effects) ◮ First Class or Higher Order Functions ◮ Recursion

Maximilian Krome SPLAV 3/30

slide-4
SLIDE 4

Functional Programming Example

Quicksort

qsort : : (Ord a ) => [ a ] −> [ a ] qsort [ ] = [ ] qsort ( x : xs ) = qsort less ++ [ x ] ++ qsort more where less = f i l t e r (<x ) xs more = f i l t e r (>=x ) xs

Maximilian Krome SPLAV 4/30

slide-5
SLIDE 5

Side Effects

So:

Side effects are against the programming paradigm.

But:

Side effects are required for a program to interact with its environment or users.

Maximilian Krome SPLAV 5/30

slide-6
SLIDE 6

Functional Reactive Animation

◮ Authors: Paul Hudak and Conal Elliott ◮ First appearance: International Conference of Functional

Programming 1997

◮ Purpose: Creation of (interactive) animation ◮ Signals: Behaviours and Events; both first class ◮ Implementation: Embedded in Haskell, running on HUGS

(Haskell User Gofer System)

Maximilian Krome SPLAV 6/30

slide-7
SLIDE 7

Features

◮ Time: Is a Behaviour ◮ liftn : Maps n signals to one other via a function parameter ◮ timeTransform: Accelerates/Delays behaviours ◮ integrate: Integrates a numeric behaviour ◮ untilB: Switching of signals

Maximilian Krome SPLAV 7/30

slide-8
SLIDE 8

Reactivity

Red-Green-Cycle

colorCycle t0 = red ’ untilB ’ lbp t0 ∗=> \ t1 −> green ’ untilB ’ lbp t1 ∗=> \ t2 −> colorCycle t2

Maximilian Krome SPLAV 8/30

slide-9
SLIDE 9

Integration

Mouse-Follower

followMouseRate im t0 = move o f f s e t im where

  • f f s e t = i n t e g r a l

rate t0 rate = mouse t0 . −. pos pos = o r i gi n 2 . + ˆ

  • f f s e t

s(t) =

  • v(t)dt + s0

Maximilian Krome SPLAV 9/30

slide-10
SLIDE 10

Behaviour attempts

  • 1. data Behavior a = Behavior (Time −> a )

Not efficient enough

  • 2. data Behavior a = Behavior

(Time −> (a , Behavior a ) ) Sampling produces (simplified) new behaviour

  • 3. data Behavior a = Behavior

(Time −> (a , Behavior a ) ) ( l v l Time −> ( l v l a , Behavior a ) )

Maximilian Krome SPLAV 10/30

slide-11
SLIDE 11

Predicate

predicate ( time ∗ exp (4 ∗ time ) ==∗ 10) 0 Evaluates to true at an infinitely small time span

Maximilian Krome SPLAV 11/30

slide-12
SLIDE 12

Interval Analysis

Remember: ( l v l Time −> ( l v l a , Behavior a ) ) returns an interval of values the behaviour assumes in a certain time span.

Maximilian Krome SPLAV 12/30

slide-13
SLIDE 13

Haskell is lazy

Only computes when the result is required

Advantages

◮ infinite data structures ◮ Spares unnecessary

computation

Disadvantages

◮ time and space leaks ◮ Hard to predict resource

requirements

Maximilian Krome SPLAV 13/30

slide-14
SLIDE 14

Real Time FRP

◮ Authors: Zhanyong Wan, Walid Taha and Paul Hudak ◮ Purpose: Real Time Applications ◮ Implementation: As Intermediate Language

Maximilian Krome SPLAV 14/30

slide-15
SLIDE 15

Properties for Real Time Development

◮ statically typed and type preserving ◮ signals are not first class ◮ bounded FRP term size ◮ constant time and space requirements for FRP commands

Maximilian Krome SPLAV 15/30

slide-16
SLIDE 16

Features

  • 1. ext is equivalent to lift0 .
  • 2. let signal x = s1 in s2 allows to access the current value
  • f the first signal in an ext term forming the second signal
  • 3. delay v s delays a signal by one tick. It displays v in the

first tick.

  • 4. s1 switch on x = ev in s2 switches from s1 to s2 whenever

event ev occurs. Starts out as s1.

Maximilian Krome SPLAV 16/30

slide-17
SLIDE 17

Syntax

Definition

e ::=x|c|()|(e1, e2)|e⊥| ⊥ |λx.e|e1 e2|fix x.e v ::=c|()|(v1, v2)|v⊥| ⊥ |λx.e s, ev ::=input|time|ext e|delay v s| let signal x = s1 in s2|s1 switch on x = ev in s

Maximilian Krome SPLAV 17/30

slide-18
SLIDE 18

Compiling FRP into RT-FRP

Lift

lift1 e s ≡ l e t signal x = s in ext ( e x ) lift2 e s1 s2 ≡ l e t signal x1 = s2 in l e t signal x2 = s2 in ext ( e x1 x2 )

Maximilian Krome SPLAV 18/30

slide-19
SLIDE 19

Elm

◮ Author: Evan Czaplicki ◮ Purpose: GUIs for Web applications ◮ Implementation: Compiles into an intermediate language

and then into JavaScript

Maximilian Krome SPLAV 19/30

slide-20
SLIDE 20

Improvements over classic FRP

Classic FRP

◮ Continuous

Signals

◮ Strict Event

Ordering

Resulting Problems

◮ Needless

Recomputation

◮ Global Delays

Solutions

◮ Only discrete

Signals

◮ Memoization ◮ non Strict Event

Ordering

Maximilian Krome SPLAV 20/30

slide-21
SLIDE 21

Features

  • 1. lift : Self explanatory
  • 2. async: Marks independent code
  • 3. foldp: It takes the current value of signal s and the

accumulator a and feeds them to the function f. The result then replaces the accumulator. foldp f a s itself evaluates to a signal that contains all accumulator values.

Maximilian Krome SPLAV 21/30

slide-22
SLIDE 22

Syntax

Definition

e ::=()|n|x|λx : η.e|e1 e2|e1 ⊕ e2| if e1 e2 e3|let x = e1 in e2|i| liftn e e1 , . . . en|foldp e1 e2 e3| async e τ ::=unit|int|τ → τ ′ σ ::=signalτ|τ → σ|σ → σ′ η ::=τ|σ

Maximilian Krome SPLAV 22/30

slide-23
SLIDE 23

Signals of Signals

l i f t ( foldp (+) 0) signalOfSignals Evaluating < <1,2,3,4,...>, <5,6,7,8,...>, ...>

Remembering Everything

<<1,3,6,10,...>, <5,11,18,26,...>, ...>

Evaluation only from the current time on

<<1,3>, <7, 15,...>, ...>

Maximilian Krome SPLAV 23/30

slide-24
SLIDE 24

Graph representation Figure: Program graph

Maximilian Krome SPLAV 24/30

slide-25
SLIDE 25

Automaton

Definition

data Automaton a b = Step ( a −> ( Automaton a b , b ) )

Maximilian Krome SPLAV 25/30

slide-26
SLIDE 26

Functions on Automatons

step : a −> Automaton a b −> ( Automaton a b , b ) step input ( Step f ) = f input run : Automaton a b −> b −> Signal a −> Signal b run automaton base inputs = l e t step ’ input ( Step f , ) = f input in l i f t snd ( foldp step ’ ( automaton , base ) inputs )

Maximilian Krome SPLAV 26/30

slide-27
SLIDE 27

”A Farewell to FRP”

Making signals unnecessary with The Elm Architecture

◮ Signals are hard to understand ◮ Signals are not used that much

Posted at the official website of Elm

Maximilian Krome SPLAV 27/30

slide-28
SLIDE 28

Conclusion

Ideas of FRP are useful and it is flexible enough to suit a variety

  • f applications, each approach to a different domain treats

signals differently:

◮ Fran: First class behaviours and events ◮ RT-FRP: Only behaviours ◮ Elm: Only events

Event processing is key to most GUI frameworks

Maximilian Krome SPLAV 28/30

slide-29
SLIDE 29

References I

  • J. Backus.

Can programming be liberated from the von neumann style?: A functional style and its algebra of programs.

  • Commun. ACM, 21(8):613–641, Aug. 1978.
  • C. Elliott and P

. Hudak. Functional reactive animation. In International Conference on Functional Programming, pages 163–173, June 1997.

  • M. Lipovaˇ

ca.

  • J. M. Synder.

Maximilian Krome SPLAV 29/30

slide-30
SLIDE 30

References II

  • Z. Wan, W. Taha, and P

. Hudak. Real-time FRP. In International Conference on Functional Programming (ICFP’01), 2001.

Maximilian Krome SPLAV 30/30