ibm pl day, dec 4, 2017 synthesizing...synthesizing functional reactive programs bernd finkbeiner1,...

25
Synthesizing Functional Reactive Programs Bernd Finkbeiner 1 , Felix Klein 1 , Ruzica Piskac 2 , Mark Santolucito 2 Saarland University 1 , Yale University 2 IBM PL Day, Dec 4, 2017

Upload: others

Post on 24-May-2020

2 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: IBM PL Day, Dec 4, 2017 Synthesizing...Synthesizing Functional Reactive Programs Bernd Finkbeiner1, Felix Klein1, Ruzica Piskac2, Mark Santolucito2 Saarland University1, Yale University2

SynthesizingFunctional Reactive Programs

Bernd Finkbeiner

1

, Felix Klein

1

,

Ruzica Piskac

2

, Mark Santolucito

2

Saarland University

1

, Yale University

2

IBM PL Day, Dec 4, 2017

Page 2: IBM PL Day, Dec 4, 2017 Synthesizing...Synthesizing Functional Reactive Programs Bernd Finkbeiner1, Felix Klein1, Ruzica Piskac2, Mark Santolucito2 Saarland University1, Yale University2

Functional Reactive Programming Applications Embedded System Networking and Web Interactive Media

Page 3: IBM PL Day, Dec 4, 2017 Synthesizing...Synthesizing Functional Reactive Programs Bernd Finkbeiner1, Felix Klein1, Ruzica Piskac2, Mark Santolucito2 Saarland University1, Yale University2

Synthesizing a music player appSys.leaveApp() {

if (MP.musicPlaying())MP.pause();

}

Sys.resumeApp() {pos = MP.trackPos();MP.play(Tr,pos);

}

Finding resume and restart errors in androidapplications Shan, Z., Azim, T., Neamtiu, I OOPSLA 2016 / IBM PL Day 2016

Page 4: IBM PL Day, Dec 4, 2017 Synthesizing...Synthesizing Functional Reactive Programs Bernd Finkbeiner1, Felix Klein1, Ruzica Piskac2, Mark Santolucito2 Saarland University1, Yale University2

Temporal Stream Logic (TSL)Sys.leaveApp() {

if (MP.musicPlaying())MP.pause();

}

Sys.resumeApp() {pos = MP.trackPos();MP.play(Tr,pos);

}

ALWAYS (leaveApp(Sys) ∧ musicPlaying(MP)⇒ [MP ⊲ pause(MP))

ALWAYS (resumeApp(Sys) ⇒ [MP ⊲ play(Tr,trackPos(MP))

Page 5: IBM PL Day, Dec 4, 2017 Synthesizing...Synthesizing Functional Reactive Programs Bernd Finkbeiner1, Felix Klein1, Ruzica Piskac2, Mark Santolucito2 Saarland University1, Yale University2

A music player app in TSL

Time

leaveApp resumeAppmusicPlaying = True

Sys.leaveApp() {if (MP.musicPlaying())

MP.pause();}

Sys.resumeApp() {pos = MP.trackPos();MP.play(Tr,pos);

}

ALWAYS (leaveApp(Sys) ∧ musicPlaying(MP)⇒ [MP ⊲ pause(MP))

ALWAYS (resumeApp(Sys) ⇒ [MP ⊲ play(Tr,trackPos(MP)))

Page 6: IBM PL Day, Dec 4, 2017 Synthesizing...Synthesizing Functional Reactive Programs Bernd Finkbeiner1, Felix Klein1, Ruzica Piskac2, Mark Santolucito2 Saarland University1, Yale University2

A music player app in TSL

Time

leaveApp resumeAppmusicPlaying = False

Sys.leaveApp() {if (MP.musicPlaying())

MP.pause();}

Sys.resumeApp() {pos = MP.trackPos();MP.play(Tr,pos);

}

ALWAYS (leaveApp(Sys) ∧ musicPlaying(MP)⇒ [MP ⊲ pause(MP))

ALWAYS (resumeApp(Sys) ⇒ [MP ⊲ play(Tr,trackPos(MP)))

Page 7: IBM PL Day, Dec 4, 2017 Synthesizing...Synthesizing Functional Reactive Programs Bernd Finkbeiner1, Felix Klein1, Ruzica Piskac2, Mark Santolucito2 Saarland University1, Yale University2

A music player app in TSL

Time

leaveApp resumeAppmusicPlaying = False

Sys.leaveApp() {if (MP.musicPlaying())

MP.pause();}

Sys.resumeApp() {pos = MP.trackPos();MP.play(Tr,pos);

}

ALWAYS (leaveApp(Sys) ∧ musicPlaying(MP)⇒ [MP ⊲ pause(MP))

ALWAYS (resumeApp(Sys) ⇒ [MP ⊲ play(Tr,trackPos(MP)))

Page 8: IBM PL Day, Dec 4, 2017 Synthesizing...Synthesizing Functional Reactive Programs Bernd Finkbeiner1, Felix Klein1, Ruzica Piskac2, Mark Santolucito2 Saarland University1, Yale University2

A music player app in TSLOn resume app, only play music if the

music was already playing when paused.

bool wasPlaying = false;

Sys.leaveApp() {if (MP.musicPlaying()){

wasPlaying = true;MP.pause();}

else {wasPlaying = false;}

}

Sys.resumeApp() {if (wasPlaying) {

pos = MP.trackPos();MP.play(Tr,pos);}

}

Page 9: IBM PL Day, Dec 4, 2017 Synthesizing...Synthesizing Functional Reactive Programs Bernd Finkbeiner1, Felix Klein1, Ruzica Piskac2, Mark Santolucito2 Saarland University1, Yale University2

On resume app, only play music if the

music was already playing when paused.

A music player app in TSL

ALWAYS (leaveApp(Sys) ∧ musicPlaying(MP)⇒ [MP ⊲ pause(MP))

ALWAYS (resumeApp(Sys) ⇒ [MP ⊲ play(Tr,trackPos(MP)))

bool wasPlaying = false;

Sys.leaveApp() {if (MP.musicPlaying()){

wasPlaying = true;MP.pause();}

else {wasPlaying = false;}

}

Sys.resumeApp() {if (wasPlaying) {

pos = MP.trackPos();MP.play(Tr,pos);}

}

Page 10: IBM PL Day, Dec 4, 2017 Synthesizing...Synthesizing Functional Reactive Programs Bernd Finkbeiner1, Felix Klein1, Ruzica Piskac2, Mark Santolucito2 Saarland University1, Yale University2

A music player app in TSL

ALWAYS (leaveApp(Sys) ∧ musicPlaying(MP)⇒ [MP ⊲ pause(MP))

ALWAYS (leaveApp(Sys) ∧ musicPlaying(MP) ⇒ [MP ⊲ play(Tr,trackPos(MP)]

AS_SOON_AS resumeApp(Sys))

AS_SOON_AS : φ A ψ ≡ ¬ψ W(ψ � φ)

bool wasPlaying = false;

Sys.leaveApp() {if (MP.musicPlaying()){

wasPlaying = true;MP.pause();}

else {wasPlaying = false;}

}

Sys.resumeApp() {if (wasPlaying) {

pos = MP.trackPos();MP.play(Tr,pos);}

}

Page 11: IBM PL Day, Dec 4, 2017 Synthesizing...Synthesizing Functional Reactive Programs Bernd Finkbeiner1, Felix Klein1, Ruzica Piskac2, Mark Santolucito2 Saarland University1, Yale University2

Control

Pure Data Transformations

Function abstraction ALWAYS (leaveApp(Sys) ∧ musicPlaying(MP)

⇒ [MP ⊲ pause(MP))

ALWAYS (leaveApp(Sys) ∧ musicPlaying(MP) ⇒ [MP ⊲ play(Tr,trackPos(MP))

AS_SOON_AS resumeApp(Sys) )

leaveApp(Sys){ ... }

musicPlaying(MP) { ... }

play(Tr,trackPos(MP)) { ... }

resumeApp(Sys) { ... }

Page 12: IBM PL Day, Dec 4, 2017 Synthesizing...Synthesizing Functional Reactive Programs Bernd Finkbeiner1, Felix Klein1, Ruzica Piskac2, Mark Santolucito2 Saarland University1, Yale University2

yampaButton :: SF (Event MouseClick) PictureyampaButton = proc click → do

reccount ← init 0 ⤙ newCountnewCount ← arr f1 ⤙ (click, count)pic ← arr f2 ⤙ count

returnA ⤙ pic

f1 :: (Event MouseClick, Int) → Intf1 (click, count)

| isEvent click = count + 1| otherwise = count

f2 :: Int → Picturef2 count = render count

Control

Pure Data Transformations

A button counter in Arrowized FRP

Page 13: IBM PL Day, Dec 4, 2017 Synthesizing...Synthesizing Functional Reactive Programs Bernd Finkbeiner1, Felix Klein1, Ruzica Piskac2, Mark Santolucito2 Saarland University1, Yale University2

yampaButton :: SF (Event MouseClick) PictureyampaButton = proc click → do

reccount ← init 0 ⤙ newCountnewCount ← arr f1 ⤙ (click, count)pic ← arr f2 ⤙ count

returnA ⤙ pic

Control

Arrow combinators for control

Page 14: IBM PL Day, Dec 4, 2017 Synthesizing...Synthesizing Functional Reactive Programs Bernd Finkbeiner1, Felix Klein1, Ruzica Piskac2, Mark Santolucito2 Saarland University1, Yale University2

TSL/FRP synthesis goal

TSL formula φ ⇒

Page 15: IBM PL Day, Dec 4, 2017 Synthesizing...Synthesizing Functional Reactive Programs Bernd Finkbeiner1, Felix Klein1, Ruzica Piskac2, Mark Santolucito2 Saarland University1, Yale University2

In summary

Page 16: IBM PL Day, Dec 4, 2017 Synthesizing...Synthesizing Functional Reactive Programs Bernd Finkbeiner1, Felix Klein1, Ruzica Piskac2, Mark Santolucito2 Saarland University1, Yale University2

ALWAYS (leaveApp(Sys) ∧ musicPlaying(MP)⇒ [MP ⊲ pause(MP))

ALWAYS (leaveApp(Sys) ∧ musicPlaying(MP) ⇒ [MP ⊲ play(Tr,trackPos(MP))

AS_SOON_AS resumeApp(Sys) )

In summary

Page 17: IBM PL Day, Dec 4, 2017 Synthesizing...Synthesizing Functional Reactive Programs Bernd Finkbeiner1, Felix Klein1, Ruzica Piskac2, Mark Santolucito2 Saarland University1, Yale University2

ALWAYS (leaveApp(Sys) ∧ musicPlaying(MP)⇒ [MP ⊲ pause(MP))

ALWAYS (leaveApp(Sys) ∧ musicPlaying(MP) ⇒ [MP ⊲ play(Tr,trackPos(MP))

AS_SOON_AS resumeApp(Sys) )

In summary

Page 18: IBM PL Day, Dec 4, 2017 Synthesizing...Synthesizing Functional Reactive Programs Bernd Finkbeiner1, Felix Klein1, Ruzica Piskac2, Mark Santolucito2 Saarland University1, Yale University2

ALWAYS (leaveApp(Sys) ∧ musicPlaying(MP)⇒ [MP ⊲ pause(MP))

ALWAYS (leaveApp(Sys) ∧ musicPlaying(MP) ⇒ [MP ⊲ play(Tr,trackPos(MP))

AS_SOON_AS resumeApp(Sys) )

In summary

Page 19: IBM PL Day, Dec 4, 2017 Synthesizing...Synthesizing Functional Reactive Programs Bernd Finkbeiner1, Felix Klein1, Ruzica Piskac2, Mark Santolucito2 Saarland University1, Yale University2

Overview of synthesis procedure

Page 20: IBM PL Day, Dec 4, 2017 Synthesizing...Synthesizing Functional Reactive Programs Bernd Finkbeiner1, Felix Klein1, Ruzica Piskac2, Mark Santolucito2 Saarland University1, Yale University2

Overview of synthesis procedure

Page 21: IBM PL Day, Dec 4, 2017 Synthesizing...Synthesizing Functional Reactive Programs Bernd Finkbeiner1, Felix Klein1, Ruzica Piskac2, Mark Santolucito2 Saarland University1, Yale University2

ALWAYS (leaveApp(Sys) ∧ musicPlaying(MP)⇒ [MP ⊲ pause(MP))

ALWAYS (leaveApp(Sys) ∧ musicPlaying(MP) ⇒ [MP ⊲ play(Tr,trackPos(MP))

AS_SOON_AS resumeApp(Sys))

Overview of synthesis procedure

Page 22: IBM PL Day, Dec 4, 2017 Synthesizing...Synthesizing Functional Reactive Programs Bernd Finkbeiner1, Felix Klein1, Ruzica Piskac2, Mark Santolucito2 Saarland University1, Yale University2

ALWAYS (leaveApp(Sys) ∧ musicPlaying(MP)⇒ [MP ⊲ pause(MP))

ALWAYS (leaveApp(Sys) ∧ musicPlaying(MP) ⇒ [MP ⊲ play(Tr,trackPos(MP))

AS_SOON_AS resumeApp(Sys))

Overview of synthesis procedure

(wasPlaying)

(wasPlaying)

Page 23: IBM PL Day, Dec 4, 2017 Synthesizing...Synthesizing Functional Reactive Programs Bernd Finkbeiner1, Felix Klein1, Ruzica Piskac2, Mark Santolucito2 Saarland University1, Yale University2

discreteEvent a :: (Time,a)

continuousSignal a :: Time → a

SF a a :: Signal a → Signal a

Time in FRP

Page 24: IBM PL Day, Dec 4, 2017 Synthesizing...Synthesizing Functional Reactive Programs Bernd Finkbeiner1, Felix Klein1, Ruzica Piskac2, Mark Santolucito2 Saarland University1, Yale University2

yampaButton :: SF (Event MouseClick, Event MouseClick) PictureyampaButton = proc (click, toggle) → do

reccountMode ← accumHold True ⤙ isEvent togglecount ← init 0 ⤙ newCount

t ← init 0 ⤙ t’t’ ← time ⤙ ()

newCount ← if countModethen integral ⤙ (t’-t)else arr id ⤙ count

pic ← arr render ⤙ countreturnA ⤙ pic

A discrete button-continuous counter in FRP

// Constant sum over time, // regardless of sample rate

Page 25: IBM PL Day, Dec 4, 2017 Synthesizing...Synthesizing Functional Reactive Programs Bernd Finkbeiner1, Felix Klein1, Ruzica Piskac2, Mark Santolucito2 Saarland University1, Yale University2

fallingBall :: Pos -> Vel -> SF () (Pos, Vel)fallingBall y0 v0 = proc _ -> do v ← integral >>^ (+ v0) ⤙ (-9.81) y ← integral >>^ (+ y0) ⤙ v returnA ⤙ (y, v)

bouncingBall :: Pos -> Vel -> SF () (Pos, Vel)bouncingBall y0 v0 = switch (bb y0 v0) bswitch where bb y0' v0' = proc input -> do (pos, vel) ← fallingBall y0' v0' ⤙ input event' ← edge ⤙ pos <= 0 returnA ⤙ ((pos, vel), event' `tag` (pos, vel)) bswitch (pos, vel) | abs vel < 1.0 = constant (0.0, 0.0) | otherwise = bouncingBall pos (-vel * 0.6)

A bouncing ball in FRP