Purely functional distributed Universidade NOVA de Lisboa ... · 4-20 minutes to communicate. 24...

58
Purely functional distributed programming Adriaan Leijnse Universidade NOVA de Lisboa 1 for collaborative applications. @aidylns @parenthetical

Transcript of Purely functional distributed Universidade NOVA de Lisboa ... · 4-20 minutes to communicate. 24...

Page 1: Purely functional distributed Universidade NOVA de Lisboa ... · 4-20 minutes to communicate. 24 The Truth. 25. 26. 27 ... Crdt Bool Bool ... 2501 sols since Curiosity Landing, Mars

Purely functional distributed programming

Adriaan LeijnseUniversidade NOVA de Lisboa

1

for collaborative applications.

@aidylns@parenthetical

Page 2: Purely functional distributed Universidade NOVA de Lisboa ... · 4-20 minutes to communicate. 24 The Truth. 25. 26. 27 ... Crdt Bool Bool ... 2501 sols since Curiosity Landing, Mars

2

Page 3: Purely functional distributed Universidade NOVA de Lisboa ... · 4-20 minutes to communicate. 24 The Truth. 25. 26. 27 ... Crdt Bool Bool ... 2501 sols since Curiosity Landing, Mars

3

Why is writing this application hard?

Not enough functional programming!

Get eggs✘ ✔

Fix solar panels✘ ✔

Send post card✘

Astro-do

New task

Page 4: Purely functional distributed Universidade NOVA de Lisboa ... · 4-20 minutes to communicate. 24 The Truth. 25. 26. 27 ... Crdt Bool Bool ... 2501 sols since Curiosity Landing, Mars

4

Please ask questions!

Page 5: Purely functional distributed Universidade NOVA de Lisboa ... · 4-20 minutes to communicate. 24 The Truth. 25. 26. 27 ... Crdt Bool Bool ... 2501 sols since Curiosity Landing, Mars

client(Pid) -> {server, Pid} ! {ping,self()}; receive pong -> client(Pid) end.

server() receive {ping, Pid} -> Pid ! pong; server() end.

5

ping

Page 6: Purely functional distributed Universidade NOVA de Lisboa ... · 4-20 minutes to communicate. 24 The Truth. 25. 26. 27 ... Crdt Bool Bool ... 2501 sols since Curiosity Landing, Mars

client(Pid) -> {server, Pid} ! {ping,self()}; receive pong -> client(Pid) end.

server() receive {ping, Pid} -> Pid ! pong; server() end.

6

ImpureickinessCan we do better than ickiness?

ping

Page 7: Purely functional distributed Universidade NOVA de Lisboa ... · 4-20 minutes to communicate. 24 The Truth. 25. 26. 27 ... Crdt Bool Bool ... 2501 sols since Curiosity Landing, Mars

7

What is a distributed program?

Page 8: Purely functional distributed Universidade NOVA de Lisboa ... · 4-20 minutes to communicate. 24 The Truth. 25. 26. 27 ... Crdt Bool Bool ... 2501 sols since Curiosity Landing, Mars

8

“places” “moments”

Space Time

Page 9: Purely functional distributed Universidade NOVA de Lisboa ... · 4-20 minutes to communicate. 24 The Truth. 25. 26. 27 ... Crdt Bool Bool ... 2501 sols since Curiosity Landing, Mars

9

Spacetime → a

Page 10: Purely functional distributed Universidade NOVA de Lisboa ... · 4-20 minutes to communicate. 24 The Truth. 25. 26. 27 ... Crdt Bool Bool ... 2501 sols since Curiosity Landing, Mars

10

temperature :: Spacetime → Kelvin

Page 11: Purely functional distributed Universidade NOVA de Lisboa ... · 4-20 minutes to communicate. 24 The Truth. 25. 26. 27 ... Crdt Bool Bool ... 2501 sols since Curiosity Landing, Mars

11

Relativistic Functional Reactive Programming

Behavior a = Spacetime → a

Event a = Map Spacetime a

Page 12: Purely functional distributed Universidade NOVA de Lisboa ... · 4-20 minutes to communicate. 24 The Truth. 25. 26. 27 ... Crdt Bool Bool ... 2501 sols since Curiosity Landing, Mars

12

Relativistic Functional Reactive Programming

Behavior a = Spacetime → a

Event a = Map Spacetime a

Conal Elliott & Paul Hudak, 1997

Page 13: Purely functional distributed Universidade NOVA de Lisboa ... · 4-20 minutes to communicate. 24 The Truth. 25. 26. 27 ... Crdt Bool Bool ... 2501 sols since Curiosity Landing, Mars

13

What about communication?

Page 14: Purely functional distributed Universidade NOVA de Lisboa ... · 4-20 minutes to communicate. 24 The Truth. 25. 26. 27 ... Crdt Bool Bool ... 2501 sols since Curiosity Landing, Mars

14

Page 15: Purely functional distributed Universidade NOVA de Lisboa ... · 4-20 minutes to communicate. 24 The Truth. 25. 26. 27 ... Crdt Bool Bool ... 2501 sols since Curiosity Landing, Mars

15

perception

Page 16: Purely functional distributed Universidade NOVA de Lisboa ... · 4-20 minutes to communicate. 24 The Truth. 25. 26. 27 ... Crdt Bool Bool ... 2501 sols since Curiosity Landing, Mars

16

Page 17: Purely functional distributed Universidade NOVA de Lisboa ... · 4-20 minutes to communicate. 24 The Truth. 25. 26. 27 ... Crdt Bool Bool ... 2501 sols since Curiosity Landing, Mars

17

Page 18: Purely functional distributed Universidade NOVA de Lisboa ... · 4-20 minutes to communicate. 24 The Truth. 25. 26. 27 ... Crdt Bool Bool ... 2501 sols since Curiosity Landing, Mars

18

perceptionall-knowing,transitive

of factsand derivations

Page 19: Purely functional distributed Universidade NOVA de Lisboa ... · 4-20 minutes to communicate. 24 The Truth. 25. 26. 27 ... Crdt Bool Bool ... 2501 sols since Curiosity Landing, Mars

19

“Can you hear me?”

(CYHM)

Client Server

perceived CYHM

perceived (perceived

CYHM)

ping through the lens of perception

ping : Event ()ping = perceptionsAt client (perceptionsAt server (canYouHearMe <> ping)

perceived (perceived

(perceived CYHM))

canYouHearMe : Event ()canYouHearMe = { c1 }

c1

s1

c2

s2ping : Event ()ping = { c2, c3, c4, … }

Page 20: Purely functional distributed Universidade NOVA de Lisboa ... · 4-20 minutes to communicate. 24 The Truth. 25. 26. 27 ... Crdt Bool Bool ... 2501 sols since Curiosity Landing, Mars

20

Client Server

Location-free programmingSomewhere

Else

pingish : Event () → Event ()pingish start = perceptions (perceptions (start <> pingish)

ping : Event () → Event ()ping start = at client (restrict [client, server] (pingish start))

Page 21: Purely functional distributed Universidade NOVA de Lisboa ... · 4-20 minutes to communicate. 24 The Truth. 25. 26. 27 ... Crdt Bool Bool ... 2501 sols since Curiosity Landing, Mars

21

Client Server

Location-free programmingSomewhere

Else

pingish : Event () → Event ()pingish start = perceptions (perceptions (start <> pingish)

ping : Event () → Event ()ping start = at client (restrict [client, server] (pingish start))

Page 22: Purely functional distributed Universidade NOVA de Lisboa ... · 4-20 minutes to communicate. 24 The Truth. 25. 26. 27 ... Crdt Bool Bool ... 2501 sols since Curiosity Landing, Mars

22

Relativistic Functional Reactive Programming

Behavior a = Spacetime → aEvent a = Map Spacetime a

a ≺ b ∧ b ≺ c ⇒ a ≺ cPerception is transitive

Page 23: Purely functional distributed Universidade NOVA de Lisboa ... · 4-20 minutes to communicate. 24 The Truth. 25. 26. 27 ... Crdt Bool Bool ... 2501 sols since Curiosity Landing, Mars

23

Get eggs✘ ✔

Fix solar panels✘ ✔

Send post card✘

Astro-do

New task

4-20 minutes to communicate

Page 24: Purely functional distributed Universidade NOVA de Lisboa ... · 4-20 minutes to communicate. 24 The Truth. 25. 26. 27 ... Crdt Bool Bool ... 2501 sols since Curiosity Landing, Mars

24

The Truth

Page 25: Purely functional distributed Universidade NOVA de Lisboa ... · 4-20 minutes to communicate. 24 The Truth. 25. 26. 27 ... Crdt Bool Bool ... 2501 sols since Curiosity Landing, Mars

25

Page 26: Purely functional distributed Universidade NOVA de Lisboa ... · 4-20 minutes to communicate. 24 The Truth. 25. 26. 27 ... Crdt Bool Bool ... 2501 sols since Curiosity Landing, Mars

26

Page 27: Purely functional distributed Universidade NOVA de Lisboa ... · 4-20 minutes to communicate. 24 The Truth. 25. 26. 27 ... Crdt Bool Bool ... 2501 sols since Curiosity Landing, Mars

27

Strong Eventual Consistency

“predictably derive a result from known operations”

Page 28: Purely functional distributed Universidade NOVA de Lisboa ... · 4-20 minutes to communicate. 24 The Truth. 25. 26. 27 ... Crdt Bool Bool ... 2501 sols since Curiosity Landing, Mars

28

@ = perceived { } { }

“predictably derive a result from known operations”

Page 29: Purely functional distributed Universidade NOVA de Lisboa ... · 4-20 minutes to communicate. 24 The Truth. 25. 26. 27 ... Crdt Bool Bool ... 2501 sols since Curiosity Landing, Mars

29

@ = fold (+) 0 { }3 2

52

1

4 { }3 2

52

1

4

= 3 + 2 + 2 + 5 = 12

“predictably derive a result from known operations”

fold :: (a → a → a) → a → Event a → Behavior aCommutative & associative

combining functioninitial value

Page 30: Purely functional distributed Universidade NOVA de Lisboa ... · 4-20 minutes to communicate. 24 The Truth. 25. 26. 27 ... Crdt Bool Bool ... 2501 sols since Curiosity Landing, Mars

30

Fix solar panels✘✔

Get eggs✘

Fix solar panels✘ ✔

Send post card✘

Astro-do

New task

Page 31: Purely functional distributed Universidade NOVA de Lisboa ... · 4-20 minutes to communicate. 24 The Truth. 25. 26. 27 ... Crdt Bool Bool ... 2501 sols since Curiosity Landing, Mars

31

Fix solar panels✘✔

Page 32: Purely functional distributed Universidade NOVA de Lisboa ... · 4-20 minutes to communicate. 24 The Truth. 25. 26. 27 ... Crdt Bool Bool ... 2501 sols since Curiosity Landing, Mars

32

Is “Fix solar panels” deleted?

True True False

= True || True || False = True

{ }fold (||) False @

Page 33: Purely functional distributed Universidade NOVA de Lisboa ... · 4-20 minutes to communicate. 24 The Truth. 25. 26. 27 ... Crdt Bool Bool ... 2501 sols since Curiosity Landing, Mars

Conflict-FreeReplicated

DataTypes

33

Page 34: Purely functional distributed Universidade NOVA de Lisboa ... · 4-20 minutes to communicate. 24 The Truth. 25. 26. 27 ... Crdt Bool Bool ... 2501 sols since Curiosity Landing, Mars

34

type Crdt operations values =

Event operations → Behavior values

Page 35: Purely functional distributed Universidade NOVA de Lisboa ... · 4-20 minutes to communicate. 24 The Truth. 25. 26. 27 ... Crdt Bool Bool ... 2501 sols since Curiosity Landing, Mars

35

eoflag :: Crdt Bool Bool

eoflag = fold (||) False

Enable-Once Flag CRDT

Page 36: Purely functional distributed Universidade NOVA de Lisboa ... · 4-20 minutes to communicate. 24 The Truth. 25. 26. 27 ... Crdt Bool Bool ... 2501 sols since Curiosity Landing, Mars

36

Fix solar panels✘✔

Page 37: Purely functional distributed Universidade NOVA de Lisboa ... · 4-20 minutes to communicate. 24 The Truth. 25. 26. 27 ... Crdt Bool Bool ... 2501 sols since Curiosity Landing, Mars

37

Do the solar panels need fixing?

True True False

= True || True || False = True

{ }fold (||) False @

Page 38: Purely functional distributed Universidade NOVA de Lisboa ... · 4-20 minutes to communicate. 24 The Truth. 25. 26. 27 ... Crdt Bool Bool ... 2501 sols since Curiosity Landing, Mars

38

“Overriding” decisions?

Page 39: Purely functional distributed Universidade NOVA de Lisboa ... · 4-20 minutes to communicate. 24 The Truth. 25. 26. 27 ... Crdt Bool Bool ... 2501 sols since Curiosity Landing, Mars

39

FalseTrue 2 hours,2501 sols since Curiosity Landing,Mars

15:50,August 20th 2019,

Earth

15:50,August 20th 2019,

Earth

2 hours,2501 sols since Curiosity Landing,Mars

>

⇒ True

Last-Writer-Wins

Page 40: Purely functional distributed Universidade NOVA de Lisboa ... · 4-20 minutes to communicate. 24 The Truth. 25. 26. 27 ... Crdt Bool Bool ... 2501 sols since Curiosity Landing, Mars

40

False

True{ , }

= False

Concurrent events

Page 41: Purely functional distributed Universidade NOVA de Lisboa ... · 4-20 minutes to communicate. 24 The Truth. 25. 26. 27 ... Crdt Bool Bool ... 2501 sols since Curiosity Landing, Mars

41

concurrent :: Event a → Behavior (Event a)

@ = concurrent{ }

@ = concurrent{ }

{ }

{ }

Page 42: Purely functional distributed Universidade NOVA de Lisboa ... · 4-20 minutes to communicate. 24 The Truth. 25. 26. 27 ... Crdt Bool Bool ... 2501 sols since Curiosity Landing, Mars

42

Do the solar panels need fixing?

True True

False(eoflag =<< concurrent)

= False { }(=<<) :: Behavior a → (a → Behavior b) → Behavior b

@

Page 43: Purely functional distributed Universidade NOVA de Lisboa ... · 4-20 minutes to communicate. 24 The Truth. 25. 26. 27 ... Crdt Bool Bool ... 2501 sols since Curiosity Landing, Mars

43

eoflag :: Crdt Bool Bool

eoflag = fold (||) False

Enable-Once Flag CRDT

ewflag :: Crdt Bool Bool

ewflag = (eoflag =<< concurrent)

Enable-Wins Flag CRDT

Page 44: Purely functional distributed Universidade NOVA de Lisboa ... · 4-20 minutes to communicate. 24 The Truth. 25. 26. 27 ... Crdt Bool Bool ... 2501 sols since Curiosity Landing, Mars

44

Fix solar panels✘✔

Page 45: Purely functional distributed Universidade NOVA de Lisboa ... · 4-20 minutes to communicate. 24 The Truth. 25. 26. 27 ... Crdt Bool Bool ... 2501 sols since Curiosity Landing, Mars

45

Fix solar panels

1. Insert ‘x’ @ 10

2. Insert ‘y’ @ 10

1. Insert ‘y’ @ 10

2. Insert ‘x’ @ 10≠

Fix solar xypanels Fix solar yxpanels

Page 46: Purely functional distributed Universidade NOVA de Lisboa ... · 4-20 minutes to communicate. 24 The Truth. 25. 26. 27 ... Crdt Bool Bool ... 2501 sols since Curiosity Landing, Mars

46

a1 b2 c3

insert ‘x’ between (1,2) insert ‘y’ between (2,3)

a1 b2 c3x4 y5

Page 47: Purely functional distributed Universidade NOVA de Lisboa ... · 4-20 minutes to communicate. 24 The Truth. 25. 26. 27 ... Crdt Bool Bool ... 2501 sols since Curiosity Landing, Mars

47

[ ]a

bc

topologicalSort

= [ a b c ]

“start” “end”

Page 48: Purely functional distributed Universidade NOVA de Lisboa ... · 4-20 minutes to communicate. 24 The Truth. 25. 26. 27 ... Crdt Bool Bool ... 2501 sols since Curiosity Landing, Mars

48

[a b

]topologicalSort

[ a b ] [ b a ]or ?

Tie breaking on identifiers

Page 49: Purely functional distributed Universidade NOVA de Lisboa ... · 4-20 minutes to communicate. 24 The Truth. 25. 26. 27 ... Crdt Bool Bool ... 2501 sols since Curiosity Landing, Mars

49

Let’s program sequences!

Page 50: Purely functional distributed Universidade NOVA de Lisboa ... · 4-20 minutes to communicate. 24 The Truth. 25. 26. 27 ... Crdt Bool Bool ... 2501 sols since Curiosity Landing, Mars

50

sequence :: Event (Pos, Pos, a) → Behavior [(Id, a)]

data Pos = Start | Middle Id | End

type Id = Spacetime

tagWithSpacetime :: Event a → Event (Spacetime, a)

Page 51: Purely functional distributed Universidade NOVA de Lisboa ... · 4-20 minutes to communicate. 24 The Truth. 25. 26. 27 ... Crdt Bool Bool ... 2501 sols since Curiosity Landing, Mars

51

sequence :: Event (Pos, Pos, a) → Behavior [(Id, a)]sequence e =

let idsE = tagWithSpacetime e

graphB = fold Set.union Set.empty (mapE Set.singleton idsE) in mapB topologicalSort graphB

mapE :: (a → b) → Event a → Event b

mapB :: (a → b) → Behavior a → Behavior b

topologicalSort :: Set (Id, (Pos, Pos, a)) → [(Id, a)]

Page 52: Purely functional distributed Universidade NOVA de Lisboa ... · 4-20 minutes to communicate. 24 The Truth. 25. 26. 27 ... Crdt Bool Bool ... 2501 sols since Curiosity Landing, Mars

52

sequence :: Event (Pos, Pos, a) → Behavior [(Id, a)]

a = Behavior (Bool, a’)

Sequence with deletion

Behavior [(Id, Behavior Bool a)]

filter + (=<<) Behavior [(Id, a)]

Page 53: Purely functional distributed Universidade NOVA de Lisboa ... · 4-20 minutes to communicate. 24 The Truth. 25. 26. 27 ... Crdt Bool Bool ... 2501 sols since Curiosity Landing, Mars

53

Fix solar panels✘✔

Get eggs✘

Fix solar panels✘ ✔

Send post card✘

Astro-do

New task

enable-wins flag

enable-onceflag

sequence + enable-once flag

Page 54: Purely functional distributed Universidade NOVA de Lisboa ... · 4-20 minutes to communicate. 24 The Truth. 25. 26. 27 ... Crdt Bool Bool ... 2501 sols since Curiosity Landing, Mars

54

WIPEventually ConsistentRFRP

● Library UX for GUI

● Extending Reflex! https://github.com/reflex-frp/reflex=> Production quality P2P apps

● Hard problems (PhD)

Page 55: Purely functional distributed Universidade NOVA de Lisboa ... · 4-20 minutes to communicate. 24 The Truth. 25. 26. 27 ... Crdt Bool Bool ... 2501 sols since Curiosity Landing, Mars

55

sequence :: Event (Pos, Pos, a) → Behavior [(Id, a)]

a = Behavior (Bool, a’)

Sequence with deletion

Behavior [(Id, Behavior Bool a)]

filter + (=<<) Behavior [(Id, a)]

Page 56: Purely functional distributed Universidade NOVA de Lisboa ... · 4-20 minutes to communicate. 24 The Truth. 25. 26. 27 ... Crdt Bool Bool ... 2501 sols since Curiosity Landing, Mars

56

Relativistic Functional Reactive Programming

Behavior a = Spacetime → aEvent a = Map Spacetime a

fold :: (a → a → a) → a → Event a → Behavior a

(Commutative & associative)

concurrent :: Event a → Behavior (Event a)

Peer-to-peer apps for free!

@aidylns@parenthetical

Page 57: Purely functional distributed Universidade NOVA de Lisboa ... · 4-20 minutes to communicate. 24 The Truth. 25. 26. 27 ... Crdt Bool Bool ... 2501 sols since Curiosity Landing, Mars

Behavior a → (Spacetime, Probability, a)

57

temperature :: Spacetime → Kelvin

ProbabilisticRelativisticFunctional Reactive Programming

Page 58: Purely functional distributed Universidade NOVA de Lisboa ... · 4-20 minutes to communicate. 24 The Truth. 25. 26. 27 ... Crdt Bool Bool ... 2501 sols since Curiosity Landing, Mars

58

Get eggs✘ ✔

Get eggs✘

Fix solar panels✘ ✔

Send post card✘

Astro-do

New task

Fix solar panels✘✔