a brief introduction to functional reactive …psznhn/fopad2007/talks/nhn-fopad2007.pdffunctional...

33
A Brief Introduction to Functional Reactive Programming and Yampa FoP Away Day 17 January 2007 Henrik Nilsson School of Computer Science and Information Technology University of Nottingham, UK FoPAD: Brief Introduction to FRP & Yampa – p.1/14

Upload: others

Post on 27-May-2020

23 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: A Brief Introduction to Functional Reactive …psznhn/FoPAD2007/Talks/nhn-FoPAD2007.pdfFunctional Reactive Programming What is Functional Reactive Programming (FRP)? •Umbrella-term

A Brief Introduction toFunctional Reactive Programming

and YampaFoP Away Day 17 January 2007

Henrik Nilsson

School of Computer Science and Information Technology

University of Nottingham, UK

FoPAD: Brief Introduction to FRP & Yampa – p.1/14

Page 2: A Brief Introduction to Functional Reactive …psznhn/FoPAD2007/Talks/nhn-FoPAD2007.pdfFunctional Reactive Programming What is Functional Reactive Programming (FRP)? •Umbrella-term

Functional Reactive Programming

What is Functional Reactive Programming (FRP)?• Umbrella-term for functional approach to

programming reactive systems.

FoPAD: Brief Introduction to FRP & Yampa – p.2/14

Page 3: A Brief Introduction to Functional Reactive …psznhn/FoPAD2007/Talks/nhn-FoPAD2007.pdfFunctional Reactive Programming What is Functional Reactive Programming (FRP)? •Umbrella-term

Functional Reactive Programming

What is Functional Reactive Programming (FRP)?• Umbrella-term for functional approach to

programming reactive systems.• Originated from Functional Reactive

Animation (Fran) (Elliott & Hudak).

FoPAD: Brief Introduction to FRP & Yampa – p.2/14

Page 4: A Brief Introduction to Functional Reactive …psznhn/FoPAD2007/Talks/nhn-FoPAD2007.pdfFunctional Reactive Programming What is Functional Reactive Programming (FRP)? •Umbrella-term

Functional Reactive Programming

What is Functional Reactive Programming (FRP)?• Umbrella-term for functional approach to

programming reactive systems.• Originated from Functional Reactive

Animation (Fran) (Elliott & Hudak).• Has evolved in a number of directions and

into different concrete implementations.

FoPAD: Brief Introduction to FRP & Yampa – p.2/14

Page 5: A Brief Introduction to Functional Reactive …psznhn/FoPAD2007/Talks/nhn-FoPAD2007.pdfFunctional Reactive Programming What is Functional Reactive Programming (FRP)? •Umbrella-term

Functional Reactive Programming

What is Functional Reactive Programming (FRP)?• Umbrella-term for functional approach to

programming reactive systems.• Originated from Functional Reactive

Animation (Fran) (Elliott & Hudak).• Has evolved in a number of directions and

into different concrete implementations.• Yampa: An FRP implementation in the form

of a Haskell combinator library , a.k.a.Domain-Specific Embedded Language(DSEL).

FoPAD: Brief Introduction to FRP & Yampa – p.2/14

Page 6: A Brief Introduction to Functional Reactive …psznhn/FoPAD2007/Talks/nhn-FoPAD2007.pdfFunctional Reactive Programming What is Functional Reactive Programming (FRP)? •Umbrella-term

Signal functions

Key concept: functions on signals .

FoPAD: Brief Introduction to FRP & Yampa – p.3/14

Page 7: A Brief Introduction to Functional Reactive …psznhn/FoPAD2007/Talks/nhn-FoPAD2007.pdfFunctional Reactive Programming What is Functional Reactive Programming (FRP)? •Umbrella-term

Signal functions

Key concept: functions on signals .

Intuition:

Signal α ≈ Time → α

x :: Signal T1y :: Signal T2SF α β ≈ Signal α → Signal β

f :: SF T1 T2

FoPAD: Brief Introduction to FRP & Yampa – p.3/14

Page 8: A Brief Introduction to Functional Reactive …psznhn/FoPAD2007/Talks/nhn-FoPAD2007.pdfFunctional Reactive Programming What is Functional Reactive Programming (FRP)? •Umbrella-term

Signal functions

Key concept: functions on signals .

Intuition:

Signal α ≈ Time → α

x :: Signal T1y :: Signal T2SF α β ≈ Signal α → Signal β

f :: SF T1 T2

Additionally, causality required: output at time t

must be determined by input on interval [0, t].FoPAD: Brief Introduction to FRP & Yampa – p.3/14

Page 9: A Brief Introduction to Functional Reactive …psznhn/FoPAD2007/Talks/nhn-FoPAD2007.pdfFunctional Reactive Programming What is Functional Reactive Programming (FRP)? •Umbrella-term

Signal functions and state

Alternative view:

FoPAD: Brief Introduction to FRP & Yampa – p.4/14

Page 10: A Brief Introduction to Functional Reactive …psznhn/FoPAD2007/Talks/nhn-FoPAD2007.pdfFunctional Reactive Programming What is Functional Reactive Programming (FRP)? •Umbrella-term

Signal functions and state

Alternative view:

Signal functions can encapsulate state .

state(t) summarizes input history x(t′), t′ ∈ [0, t].

FoPAD: Brief Introduction to FRP & Yampa – p.4/14

Page 11: A Brief Introduction to Functional Reactive …psznhn/FoPAD2007/Talks/nhn-FoPAD2007.pdfFunctional Reactive Programming What is Functional Reactive Programming (FRP)? •Umbrella-term

Signal functions and state

Alternative view:

Signal functions can encapsulate state .

state(t) summarizes input history x(t′), t′ ∈ [0, t].

From this perspective, signal functions are:• stateful if y(t) depends on x(t) and state(t)

• stateless if y(t) depends only on x(t)

FoPAD: Brief Introduction to FRP & Yampa – p.4/14

Page 12: A Brief Introduction to Functional Reactive …psznhn/FoPAD2007/Talks/nhn-FoPAD2007.pdfFunctional Reactive Programming What is Functional Reactive Programming (FRP)? •Umbrella-term

Programming with signal functions

In Yampa, systems are described by combiningsignal functions (forming new signal functions).

FoPAD: Brief Introduction to FRP & Yampa – p.5/14

Page 13: A Brief Introduction to Functional Reactive …psznhn/FoPAD2007/Talks/nhn-FoPAD2007.pdfFunctional Reactive Programming What is Functional Reactive Programming (FRP)? •Umbrella-term

Programming with signal functions

In Yampa, systems are described by combiningsignal functions (forming new signal functions).

For example, serial composition:

FoPAD: Brief Introduction to FRP & Yampa – p.5/14

Page 14: A Brief Introduction to Functional Reactive …psznhn/FoPAD2007/Talks/nhn-FoPAD2007.pdfFunctional Reactive Programming What is Functional Reactive Programming (FRP)? •Umbrella-term

Programming with signal functions

In Yampa, systems are described by combiningsignal functions (forming new signal functions).

For example, serial composition:

A combinator can be defined that captures thisidea:

(≫) :: SF a b → SF b c → SF a c

FoPAD: Brief Introduction to FRP & Yampa – p.5/14

Page 15: A Brief Introduction to Functional Reactive …psznhn/FoPAD2007/Talks/nhn-FoPAD2007.pdfFunctional Reactive Programming What is Functional Reactive Programming (FRP)? •Umbrella-term

Programming with signal functions (2)

What about larger networks?How many combinators are needed?

FoPAD: Brief Introduction to FRP & Yampa – p.6/14

Page 16: A Brief Introduction to Functional Reactive …psznhn/FoPAD2007/Talks/nhn-FoPAD2007.pdfFunctional Reactive Programming What is Functional Reactive Programming (FRP)? •Umbrella-term

Programming with signal functions (2)

What about larger networks?How many combinators are needed?

John Hughes’s Arrow framework provides agood answer!

FoPAD: Brief Introduction to FRP & Yampa – p.6/14

Page 17: A Brief Introduction to Functional Reactive …psznhn/FoPAD2007/Talks/nhn-FoPAD2007.pdfFunctional Reactive Programming What is Functional Reactive Programming (FRP)? •Umbrella-term

The Arrow framework (1)

These diagrams convey the general idea:

arrf≫

firstf loopf

first :: SF a b → SF (a, c) (b, c)

loop :: SF (a, c) (b, c)→ SF a b

FoPAD: Brief Introduction to FRP & Yampa – p.7/14

Page 18: A Brief Introduction to Functional Reactive …psznhn/FoPAD2007/Talks/nhn-FoPAD2007.pdfFunctional Reactive Programming What is Functional Reactive Programming (FRP)? •Umbrella-term

The Arrow framework (2)

Some derived combinators:

secondff ∗∗∗ g

f&&&gFoPAD: Brief Introduction to FRP & Yampa – p.8/14

Page 19: A Brief Introduction to Functional Reactive …psznhn/FoPAD2007/Talks/nhn-FoPAD2007.pdfFunctional Reactive Programming What is Functional Reactive Programming (FRP)? •Umbrella-term

Example: Constructing a network

FoPAD: Brief Introduction to FRP & Yampa – p.9/14

Page 20: A Brief Introduction to Functional Reactive …psznhn/FoPAD2007/Talks/nhn-FoPAD2007.pdfFunctional Reactive Programming What is Functional Reactive Programming (FRP)? •Umbrella-term

Example: Constructing a network

FoPAD: Brief Introduction to FRP & Yampa – p.9/14

Page 21: A Brief Introduction to Functional Reactive …psznhn/FoPAD2007/Talks/nhn-FoPAD2007.pdfFunctional Reactive Programming What is Functional Reactive Programming (FRP)? •Umbrella-term

Example: Constructing a network

loop (arr (λ(x , y)→ ((x , y), x ))

≫ (fst f

≫ (arr (λ(x , y)→ (x , (x , y))) ≫ (g ∗∗∗ h))))

FoPAD: Brief Introduction to FRP & Yampa – p.9/14

Page 22: A Brief Introduction to Functional Reactive …psznhn/FoPAD2007/Talks/nhn-FoPAD2007.pdfFunctional Reactive Programming What is Functional Reactive Programming (FRP)? •Umbrella-term

The Arrow notation

FoPAD: Brief Introduction to FRP & Yampa – p.10/14

Page 23: A Brief Introduction to Functional Reactive …psznhn/FoPAD2007/Talks/nhn-FoPAD2007.pdfFunctional Reactive Programming What is Functional Reactive Programming (FRP)? •Umbrella-term

The Arrow notation

FoPAD: Brief Introduction to FRP & Yampa – p.10/14

Page 24: A Brief Introduction to Functional Reactive …psznhn/FoPAD2007/Talks/nhn-FoPAD2007.pdfFunctional Reactive Programming What is Functional Reactive Programming (FRP)? •Umbrella-term

The Arrow notation

proc x → do

rec

u ← f −≺ (x , v)

y ← g−≺ u

v ← h−≺ (u, x )

returnA−≺ y

FoPAD: Brief Introduction to FRP & Yampa – p.10/14

Page 25: A Brief Introduction to Functional Reactive …psznhn/FoPAD2007/Talks/nhn-FoPAD2007.pdfFunctional Reactive Programming What is Functional Reactive Programming (FRP)? •Umbrella-term

How does it work?

• Essentially:

newtype SF a b =

SF (DeltaTime → a → (SF a b, b))

FoPAD: Brief Introduction to FRP & Yampa – p.11/14

Page 26: A Brief Introduction to Functional Reactive …psznhn/FoPAD2007/Talks/nhn-FoPAD2007.pdfFunctional Reactive Programming What is Functional Reactive Programming (FRP)? •Umbrella-term

How does it work?

• Essentially:

newtype SF a b =

SF (DeltaTime → a → (SF a b, b))

• A top-level loop, reactimate , drives thecomputation.

FoPAD: Brief Introduction to FRP & Yampa – p.11/14

Page 27: A Brief Introduction to Functional Reactive …psznhn/FoPAD2007/Talks/nhn-FoPAD2007.pdfFunctional Reactive Programming What is Functional Reactive Programming (FRP)? •Umbrella-term

How does it work?

• Essentially:

newtype SF a b =

SF (DeltaTime → a → (SF a b, b))

• A top-level loop, reactimate , drives thecomputation.

Note that the system representation in principleis reconstructed at every time step.

FoPAD: Brief Introduction to FRP & Yampa – p.11/14

Page 28: A Brief Introduction to Functional Reactive …psznhn/FoPAD2007/Talks/nhn-FoPAD2007.pdfFunctional Reactive Programming What is Functional Reactive Programming (FRP)? •Umbrella-term

Related languages and paradigms

FRP/Yampa related to:• Synchronous dataflow languages, like

Esterel, Lucid Synchrone.

FoPAD: Brief Introduction to FRP & Yampa – p.12/14

Page 29: A Brief Introduction to Functional Reactive …psznhn/FoPAD2007/Talks/nhn-FoPAD2007.pdfFunctional Reactive Programming What is Functional Reactive Programming (FRP)? •Umbrella-term

Related languages and paradigms

FRP/Yampa related to:• Synchronous dataflow languages, like

Esterel, Lucid Synchrone.• Modeling languages, like Simulink, Modelica.

FoPAD: Brief Introduction to FRP & Yampa – p.12/14

Page 30: A Brief Introduction to Functional Reactive …psznhn/FoPAD2007/Talks/nhn-FoPAD2007.pdfFunctional Reactive Programming What is Functional Reactive Programming (FRP)? •Umbrella-term

What makes Yampa interesting?

• First class reactive components (signalfunctions).

FoPAD: Brief Introduction to FRP & Yampa – p.13/14

Page 31: A Brief Introduction to Functional Reactive …psznhn/FoPAD2007/Talks/nhn-FoPAD2007.pdfFunctional Reactive Programming What is Functional Reactive Programming (FRP)? •Umbrella-term

What makes Yampa interesting?

• First class reactive components (signalfunctions).

• Supports hybrid (mixed continuous anddiscrete time) systems: option type Eventrepresents discrete-time signals.

FoPAD: Brief Introduction to FRP & Yampa – p.13/14

Page 32: A Brief Introduction to Functional Reactive …psznhn/FoPAD2007/Talks/nhn-FoPAD2007.pdfFunctional Reactive Programming What is Functional Reactive Programming (FRP)? •Umbrella-term

What makes Yampa interesting?

• First class reactive components (signalfunctions).

• Supports hybrid (mixed continuous anddiscrete time) systems: option type Eventrepresents discrete-time signals.

• Supports dynamic system structure throughswitching combinators :

FoPAD: Brief Introduction to FRP & Yampa – p.13/14

Page 33: A Brief Introduction to Functional Reactive …psznhn/FoPAD2007/Talks/nhn-FoPAD2007.pdfFunctional Reactive Programming What is Functional Reactive Programming (FRP)? •Umbrella-term

Example: Space Invaders

FoPAD: Brief Introduction to FRP & Yampa – p.14/14