functional reactive programming for c++ - ivan Čukić · functional reactive programming for c++ -...

57
Functional reactive programming for C++ CEFP Summer School, Budapest 2015 Ivan ˇ Cuki´ c KDE University of Belgrade [email protected] [email protected]

Upload: others

Post on 29-Jul-2020

29 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: Functional reactive programming for C++ - Ivan Čukić · Functional reactive programming for C++ - Author: Ivan Cukic Created Date: 11/13/2016 10:21:39 AM

Functional reactive programming for C++

CEFP Summer School, Budapest 2015

Ivan Cukic

KDE University of [email protected] [email protected]

Page 2: Functional reactive programming for C++ - Ivan Čukić · Functional reactive programming for C++ - Author: Ivan Cukic Created Date: 11/13/2016 10:21:39 AM

Futures Ranges Streams Further evolution of C++ Epilogue

About me

KDE developmentTalks and teachingFunctional programming enthusiast, but not a purist

2

Page 3: Functional reactive programming for C++ - Ivan Čukić · Functional reactive programming for C++ - Author: Ivan Cukic Created Date: 11/13/2016 10:21:39 AM

Futures Ranges Streams Further evolution of C++ Epilogue

Disclaimer

Make your code readable. Pretend the next personwho looks at your code is a psychopath and they knowwhere you live.

Philip Wadler

3

Page 4: Functional reactive programming for C++ - Ivan Čukić · Functional reactive programming for C++ - Author: Ivan Cukic Created Date: 11/13/2016 10:21:39 AM

Futures Ranges Streams Further evolution of C++ Epilogue

Disclaimer

The code snippets are optimized for presentation, it is notproduction-ready code.std namespace is omitted, value arguments used instead of const-refs orforwarding refs, etc.

4

Page 5: Functional reactive programming for C++ - Ivan Čukić · Functional reactive programming for C++ - Author: Ivan Cukic Created Date: 11/13/2016 10:21:39 AM

.

Page 6: Functional reactive programming for C++ - Ivan Čukić · Functional reactive programming for C++ - Author: Ivan Cukic Created Date: 11/13/2016 10:21:39 AM

Futures Ranges Streams Further evolution of C++ Epilogue

Why C++

6

Page 7: Functional reactive programming for C++ - Ivan Čukić · Functional reactive programming for C++ - Author: Ivan Cukic Created Date: 11/13/2016 10:21:39 AM

FUTURES

Concurrency

Futures

Page 8: Functional reactive programming for C++ - Ivan Čukić · Functional reactive programming for C++ - Author: Ivan Cukic Created Date: 11/13/2016 10:21:39 AM

Futures Ranges Streams Further evolution of C++ Epilogue

Concurrency

ThreadsMultiple processesDistributed systems

Note: "concurrency" will mean that different tasks are executed atoverlapping times.

8

Page 9: Functional reactive programming for C++ - Ivan Čukić · Functional reactive programming for C++ - Author: Ivan Cukic Created Date: 11/13/2016 10:21:39 AM

Futures Ranges Streams Further evolution of C++ Epilogue

Plain threads are bad

A large fraction of the flaws in software developmentare due to programmers not fully understanding all thepossible states their code may execute in. In amultithreaded environment, the lack of understandingand the resulting problems are greatly amplified,almost to the point of panic if you are paying attention.

John CarmackIn-depth: Functional programming in C++

9

Page 10: Functional reactive programming for C++ - Ivan Čukić · Functional reactive programming for C++ - Author: Ivan Cukic Created Date: 11/13/2016 10:21:39 AM

Futures Ranges Streams Further evolution of C++ Epilogue

Plain threads are bad

Threads are not composableParallelism can’t be ‘disabled’Difficult to ensure balanced load manually

Hartmut KaiserPlain Threads are the GOTO of Today’s Computing

10

Page 11: Functional reactive programming for C++ - Ivan Čukić · Functional reactive programming for C++ - Author: Ivan Cukic Created Date: 11/13/2016 10:21:39 AM

Futures Ranges Streams Further evolution of C++ Epilogue

Plain synchronization primitives are bad

You will likely get it wrongS.L.O.W. (starvation, latency, overhead, wait)

Sean ParentBetter Code: Concurrency

11

Page 12: Functional reactive programming for C++ - Ivan Čukić · Functional reactive programming for C++ - Author: Ivan Cukic Created Date: 11/13/2016 10:21:39 AM

Futures Ranges Streams Further evolution of C++ Epilogue

Amdahl’s Law

1(1−P )+ PN

12

Page 13: Functional reactive programming for C++ - Ivan Čukić · Functional reactive programming for C++ - Author: Ivan Cukic Created Date: 11/13/2016 10:21:39 AM

Futures Ranges Streams Further evolution of C++ Epilogue

Locks are the main problem

The biggest of all the big problems with recursive mutexes is thatthey encourage you to completely lose track of your locking schemeand scope. This is deadly. Evil. It’s the "thread eater". You hold locksfor the absolutely shortest possible time. Period. Always. If you’recalling something with a lock held simply because you don’t know it’sheld, or because you don’t know whether the callee needs the mutex,then you’re holding it too long. You’re aiming a shotgun at yourapplication and pulling the trigger. You presumably started usingthreads to get concurrency; but you’ve just PREVENTED concurrency.

I’ve often joked that instead of picking up Djikstra’s cuteacronym we should have called the basic synchronizationobject "the bottleneck". Bottlenecks are useful at times,sometimes indispensible – but they’re never GOOD.

David ButenhofRe: recursive mutexes

13

Page 14: Functional reactive programming for C++ - Ivan Čukić · Functional reactive programming for C++ - Author: Ivan Cukic Created Date: 11/13/2016 10:21:39 AM

Futures Ranges Streams Further evolution of C++ Epilogue

Futures

Futures should be the lowest level concurrency abstractions.

std::futureboost::futureQFutureFolly Future

any continuation - *.then([] . . .)

14

Page 15: Functional reactive programming for C++ - Ivan Čukić · Functional reactive programming for C++ - Author: Ivan Cukic Created Date: 11/13/2016 10:21:39 AM

Futures Ranges Streams Further evolution of C++ Epilogue

Future

T value = function();

future<T> value = function(); . . .; value.get();)

15

Page 16: Functional reactive programming for C++ - Ivan Čukić · Functional reactive programming for C++ - Author: Ivan Cukic Created Date: 11/13/2016 10:21:39 AM

Futures Ranges Streams Further evolution of C++ Epilogue

Future

future<T> value = function(); . . .; value.get();

future<T2> value = function().then(continuation);

16

Page 17: Functional reactive programming for C++ - Ivan Čukić · Functional reactive programming for C++ - Author: Ivan Cukic Created Date: 11/13/2016 10:21:39 AM

Futures Ranges Streams Further evolution of C++ Epilogue

Futures

get_page("http://people.inf.elte.hu/cefp/").then(

[] (auto &&result) {cout << result.headers();

})

17

Page 18: Functional reactive programming for C++ - Ivan Čukić · Functional reactive programming for C++ - Author: Ivan Cukic Created Date: 11/13/2016 10:21:39 AM

Futures Ranges Streams Further evolution of C++ Epilogue

Futures

get("http://people.inf.elte.hu/cefp/").then(

[] (auto &&result) {cout << result.headers();

for (image: result.image_tags) {image.get().then(

[] (auto &&image_result) {// do something// with image_result

});

}}

)18

Page 19: Functional reactive programming for C++ - Ivan Čukić · Functional reactive programming for C++ - Author: Ivan Cukic Created Date: 11/13/2016 10:21:39 AM

Futures Ranges Streams Further evolution of C++ Epilogue

Imperative chaining of futures

result = get("http://people.inf.elte.hu/cefp/"),for_(image = result.image_tags) (

image_result = image.get(),// do something with image_result. . .

)

19

Page 20: Functional reactive programming for C++ - Ivan Čukić · Functional reactive programming for C++ - Author: Ivan Cukic Created Date: 11/13/2016 10:21:39 AM

Futures Ranges Streams Further evolution of C++ Epilogue

Imperative chaining of futureswhile_(

// Wait until we get a connection.client = ws::server::accept(server),

// Start a detached execution path to process the client.detach_([] {

. . .

serial_(// WebSocket handshakeheader = ws::client::get_header(),server_key = ws::server::create_key(header),ws::client::send_header(client, server_key),

// Sending the initial greeting messagews::client::message_write(client, "Hello, I'm Echo"),

// Connection establishedwhile_(

// getting and echoing the messagemessage = ws::client::message_read(client),ws::client::message_write(client, message)

))

}))

Check out "Monads in chains" from Meeting C++ 201420

Page 21: Functional reactive programming for C++ - Ivan Čukić · Functional reactive programming for C++ - Author: Ivan Cukic Created Date: 11/13/2016 10:21:39 AM

RANGES

STL algorithms

Ranges

Page 22: Functional reactive programming for C++ - Ivan Čukić · Functional reactive programming for C++ - Author: Ivan Cukic Created Date: 11/13/2016 10:21:39 AM

Futures Ranges Streams Further evolution of C++ Epilogue

Ranges in C++

vector<int> xs;int sum = 0;

for (x: xs) {sum += x;

}

return sum;

22

Page 23: Functional reactive programming for C++ - Ivan Čukić · Functional reactive programming for C++ - Author: Ivan Cukic Created Date: 11/13/2016 10:21:39 AM

Futures Ranges Streams Further evolution of C++ Epilogue

Ranges in C++

return accumulate(xs.cbegin(), xs.cend(),0);

23

Page 24: Functional reactive programming for C++ - Ivan Čukić · Functional reactive programming for C++ - Author: Ivan Cukic Created Date: 11/13/2016 10:21:39 AM

Futures Ranges Streams Further evolution of C++ Epilogue

Ranges in C++

return accumulate(xs.cbegin(), xs.cend(),1,_1 * _2);

24

Page 25: Functional reactive programming for C++ - Ivan Čukić · Functional reactive programming for C++ - Author: Ivan Cukic Created Date: 11/13/2016 10:21:39 AM

Futures Ranges Streams Further evolution of C++ Epilogue

Ranges in C++

How to do an aggregation on a transformed list?

vector<int> xs;int sum = 0;

for (x: xs) {sum += x * x;

}

return sum;

25

Page 26: Functional reactive programming for C++ - Ivan Čukić · Functional reactive programming for C++ - Author: Ivan Cukic Created Date: 11/13/2016 10:21:39 AM

Futures Ranges Streams Further evolution of C++ Epilogue

Ranges in C++

How to do an aggregation on a transformed list?

sum $ map (λ x → x * x) xs

26

Page 27: Functional reactive programming for C++ - Ivan Čukić · Functional reactive programming for C++ - Author: Ivan Cukic Created Date: 11/13/2016 10:21:39 AM

Futures Ranges Streams Further evolution of C++ Epilogue

Ranges in C++

How to do an aggregation on a transformed list?

vector<int> temp;

std::transform(xs.cbegin(), xs.cend(),std::back_inserter(temp),_1 * _1);

return std::accumulate(temp.cbegin(),temp.cend());

27

Page 28: Functional reactive programming for C++ - Ivan Čukić · Functional reactive programming for C++ - Author: Ivan Cukic Created Date: 11/13/2016 10:21:39 AM

Futures Ranges Streams Further evolution of C++ Epilogue

Ranges in C++, boost.range, N4128

How to do an aggregation on a transformed list?

return accumulate(xs | transformed(_1 * _1));

28

Page 29: Functional reactive programming for C++ - Ivan Čukić · Functional reactive programming for C++ - Author: Ivan Cukic Created Date: 11/13/2016 10:21:39 AM

Futures Ranges Streams Further evolution of C++ Epilogue

Example

transactions| filter(Transactions::price() > 1000)| groupBy(Transactions::customerId())| sort(

Transactions::price().desc() |Transactions::customerName()

);

29

Page 30: Functional reactive programming for C++ - Ivan Čukić · Functional reactive programming for C++ - Author: Ivan Cukic Created Date: 11/13/2016 10:21:39 AM

Futures Ranges Streams Further evolution of C++ Epilogue

Example boilerplate

namespace Transactions {struct Record {

int customerId;. . .

};

DECL_COLUMN(customerId). . .

}

Column meta-type has all operators implemented, asc(),desc(), etc.

30

Page 31: Functional reactive programming for C++ - Ivan Čukić · Functional reactive programming for C++ - Author: Ivan Cukic Created Date: 11/13/2016 10:21:39 AM

STREAMS

Futures, again

Page 32: Functional reactive programming for C++ - Ivan Čukić · Functional reactive programming for C++ - Author: Ivan Cukic Created Date: 11/13/2016 10:21:39 AM

Futures Ranges Streams Further evolution of C++ Epilogue

Anything you think that you could ever be

for (item: items) {// do something

}

for_each(items, [] (item i) {// do something

});

32

Page 33: Functional reactive programming for C++ - Ivan Čukić · Functional reactive programming for C++ - Author: Ivan Cukic Created Date: 11/13/2016 10:21:39 AM

Futures Ranges Streams Further evolution of C++ Epilogue

Just passing our time

33

Page 34: Functional reactive programming for C++ - Ivan Čukić · Functional reactive programming for C++ - Author: Ivan Cukic Created Date: 11/13/2016 10:21:39 AM

Futures Ranges Streams Further evolution of C++ Epilogue

Oh we’ll keep on trying

34

Page 35: Functional reactive programming for C++ - Ivan Čukić · Functional reactive programming for C++ - Author: Ivan Cukic Created Date: 11/13/2016 10:21:39 AM

Futures Ranges Streams Further evolution of C++ Epilogue

Flow of information

35

Page 36: Functional reactive programming for C++ - Ivan Čukić · Functional reactive programming for C++ - Author: Ivan Cukic Created Date: 11/13/2016 10:21:39 AM

Futures Ranges Streams Further evolution of C++ Epilogue

Through the eons, and on and on

Web server client connection requestsUser interface eventsDatabase accessI/OAnything and everything

36

Page 37: Functional reactive programming for C++ - Ivan Čukić · Functional reactive programming for C++ - Author: Ivan Cukic Created Date: 11/13/2016 10:21:39 AM

Futures Ranges Streams Further evolution of C++ Epilogue

Till the end of time

Message passing:continuation!newClientMessageCall-callback:onNewMessage(continuation)Signals-slots:connect(socket, &Socket::newConnection,

receiver, &Receiver::continuation)Any data collection:for_each(xs, continuation)

37

Page 38: Functional reactive programming for C++ - Ivan Čukić · Functional reactive programming for C++ - Author: Ivan Cukic Created Date: 11/13/2016 10:21:39 AM

Futures Ranges Streams Further evolution of C++ Epilogue

Stream transformation

Streams can only be transformed with algorithms that acceptinput ranges.

map, bind, filter, take, drop, etc.

38

Page 39: Functional reactive programming for C++ - Ivan Čukić · Functional reactive programming for C++ - Author: Ivan Cukic Created Date: 11/13/2016 10:21:39 AM

Futures Ranges Streams Further evolution of C++ Epilogue

Stream transformation

39

Page 40: Functional reactive programming for C++ - Ivan Čukić · Functional reactive programming for C++ - Author: Ivan Cukic Created Date: 11/13/2016 10:21:39 AM

Futures Ranges Streams Further evolution of C++ Epilogue

Map / Transform

We have a stream of 2D coordinates (mouse coordinates).

// Projecting on the x-axismouse_position >>=

map(λ point → (point.x, 0))

// Projecting on the y-axismouse_position >>=

map(λ point → (0, point.y))

40

Page 41: Functional reactive programming for C++ - Ivan Čukić · Functional reactive programming for C++ - Author: Ivan Cukic Created Date: 11/13/2016 10:21:39 AM

Futures Ranges Streams Further evolution of C++ Epilogue

Implementation detail

namespace stream {template <typename Stream, typename Cont>auto operator >>= (Stream &&stream,

Cont &&cont){

return stream.then(cont);}

template <typename Under>auto make_stream(Under &&under);

}

41

Page 42: Functional reactive programming for C++ - Ivan Čukić · Functional reactive programming for C++ - Author: Ivan Cukic Created Date: 11/13/2016 10:21:39 AM

Futures Ranges Streams Further evolution of C++ Epilogue

Map

template <typename Func, typename Cont>struct map_cont {

map_cont(Func f, Cont c) : f(f), c(c) { }

template <typename InType>void operator () (const InType &in) {

c(f(in));}

Func f;Cont c;

};

42

Page 43: Functional reactive programming for C++ - Ivan Čukić · Functional reactive programming for C++ - Author: Ivan Cukic Created Date: 11/13/2016 10:21:39 AM

Futures Ranges Streams Further evolution of C++ Epilogue

Fork (or parallel), tee

tee(print) >>=fork(

receiver1,receiver2

)

43

Page 44: Functional reactive programming for C++ - Ivan Čukić · Functional reactive programming for C++ - Author: Ivan Cukic Created Date: 11/13/2016 10:21:39 AM

Futures Ranges Streams Further evolution of C++ Epilogue

Fork (or parallel), tee

template <typename ... Conts>struct fork_impl;

template <typename Cont, typename ... Conts>struct fork_impl<Cont, Conts...>: fork_impl<Conts...>{

using parent_type = fork_impl<Conts...>;

fork_impl(Cont c, Conts... cs): parent_type(cs...), c(c)

{ }

template <typename InType>void operator() (const InType &in) {

c(in);parent_type::operator()(in);

}

Cont c;};

44

Page 45: Functional reactive programming for C++ - Ivan Čukić · Functional reactive programming for C++ - Author: Ivan Cukic Created Date: 11/13/2016 10:21:39 AM

Futures Ranges Streams Further evolution of C++ Epilogue

Stateful function objects

class gravity_object {public:

gravity_object() { }

template <typename Cont>void then(Cont &&c) { _f = std::forward<Cont>(c); }

QPointF operator() (const QPointF &new_point) {m_point.setX(m_point.x() * .99 + new_point.x() * .01);m_point.setY(m_point.y() * .99 + new_point.y() * .01);return m_point;

}

private:std::function<void(QPointF)> _f;QPointF m_point;

};

45

Page 46: Functional reactive programming for C++ - Ivan Čukić · Functional reactive programming for C++ - Author: Ivan Cukic Created Date: 11/13/2016 10:21:39 AM

Futures Ranges Streams Further evolution of C++ Epilogue

Stateful function objects

46

Page 47: Functional reactive programming for C++ - Ivan Čukić · Functional reactive programming for C++ - Author: Ivan Cukic Created Date: 11/13/2016 10:21:39 AM

Futures Ranges Streams Further evolution of C++ Epilogue

Can stateful function objects be pure?

Like actors changing behaviourOr, treating the function object like its argument is thepast part of the stream (a finite list of elements)

47

Page 48: Functional reactive programming for C++ - Ivan Čukić · Functional reactive programming for C++ - Author: Ivan Cukic Created Date: 11/13/2016 10:21:39 AM

Futures Ranges Streams Further evolution of C++ Epilogue

Filter

bool pointFilter(const QPointF &point) {return int(point.y()) % 100 == 0;

}

events >>=filter(predicate) >>=

. . .

48

Page 49: Functional reactive programming for C++ - Ivan Čukić · Functional reactive programming for C++ - Author: Ivan Cukic Created Date: 11/13/2016 10:21:39 AM

Futures Ranges Streams Further evolution of C++ Epilogue

Flat map

template <typename Func, typename Cont>struct flatmap_cont {

flatmap_cont(Func f, Cont c): f(f), c(c)

{}

template <typename InType>void operator () (const InType &in) {

boost::for_each(f(in), c);}

Func f;Cont c;

};49

Page 50: Functional reactive programming for C++ - Ivan Čukić · Functional reactive programming for C++ - Author: Ivan Cukic Created Date: 11/13/2016 10:21:39 AM

Futures Ranges Streams Further evolution of C++ Epilogue

Flat mapclass more_precision {public:

more_precision() { }

template <typename Cont>void then(Cont &&c) { _f = std::forward<Cont>(c); }

std::vector<QPointF> operator() (const QPointF &new_point) {std::vector<QPointF> result;

int stepX = (m_previous_point.x() < new_point.x()) ? 1 : -1;for (int i = (int)m_previous_point.x(); i != (int)new_point.x(); i += stepX) {

result.emplace_back(i, m_previous_point.y());}

int stepY = (m_previous_point.y() < new_point.y()) ? 1 : -1;for (int i = (int)m_previous_point.y(); i != (int)new_point.y(); i += stepY) {

result.emplace_back(new_point.x(), i);}

m_previous_point = new_point;return result;

}

private:std::function<void(QPointF)> _f;QPointF m_previous_point;

};

50

Page 51: Functional reactive programming for C++ - Ivan Čukić · Functional reactive programming for C++ - Author: Ivan Cukic Created Date: 11/13/2016 10:21:39 AM

FURTHER EVOLUTION OF C++

Ranges

Await

Page 52: Functional reactive programming for C++ - Ivan Čukić · Functional reactive programming for C++ - Author: Ivan Cukic Created Date: 11/13/2016 10:21:39 AM

Futures Ranges Streams Further evolution of C++ Epilogue

Ranges

Some call it STL 2.o, provides separate views and actionsFilter a container using a predicate and transform it.

std::vector<int> vi{1,2,3,4,5,6,7,8,9,10};auto rng = vi | view::remove_if([](int i){return i % 2 == 1;})

| view::transform([](int i){return std::to_string(i);});

Generate an infinite list of integers starting at 1, square them, take the first 10, and sum them:

int sum = accumulate(view::ints(1)| view::transform([](int i){return i*i;})| view::take(10), 0);

Generate a sequence on the fly with a range comprehension and initialize a vector with it:

std::vector<int> vi =view::for_each(view::ints(1,10), [](int i){

return yield_from(view::repeat(i,i));});

52

Page 53: Functional reactive programming for C++ - Ivan Čukić · Functional reactive programming for C++ - Author: Ivan Cukic Created Date: 11/13/2016 10:21:39 AM

Futures Ranges Streams Further evolution of C++ Epilogue

Futures

get("http://people.inf.elte.hu/cefp/").then(

[] (auto &&result) {for (image: result.image_tags) {

image.get().then([] (auto &&image_result) {

. . .}

);}

})

53

Page 54: Functional reactive programming for C++ - Ivan Čukić · Functional reactive programming for C++ - Author: Ivan Cukic Created Date: 11/13/2016 10:21:39 AM

Futures Ranges Streams Further evolution of C++ Epilogue

Imperative chaining of futures

result = get(. . .),

for_(image = result.image_tags) (image_result = image.get(),// do something with image_result. . .

)

54

Page 55: Functional reactive programming for C++ - Ivan Čukić · Functional reactive programming for C++ - Author: Ivan Cukic Created Date: 11/13/2016 10:21:39 AM

Futures Ranges Streams Further evolution of C++ Epilogue

Monadic await

result = await get(. . .);

for (image: result.image_tags) (image_result = await image.get();// do something with image_result. . .

)

55

Page 56: Functional reactive programming for C++ - Ivan Čukić · Functional reactive programming for C++ - Author: Ivan Cukic Created Date: 11/13/2016 10:21:39 AM

Futures Ranges Streams Further evolution of C++ Epilogue

Monadic await

await expression is equivalent to:

{auto && tmp = <expr>;if (!await_ready(tmp)) {

await_suspend(tmp, continuation);

}return await_resume(tmp);

}

56

Page 57: Functional reactive programming for C++ - Ivan Čukić · Functional reactive programming for C++ - Author: Ivan Cukic Created Date: 11/13/2016 10:21:39 AM

Futures Ranges Streams Further evolution of C++ Epilogue

Answers? Questions! Questions? Answers!

Kudos:

Friends at KDE, Dr Sasa Malkov, basysKom

Worth reading and watching:

Iterators Must Go, Andrei AlexandrescuValue Semantics and Range Algorithms, Chandler CarruthSystematic Error Handling in C++, Andrei AlexandrescuAwait 2.o, Gor NishanovRanges proposal, Eric NieblerReactive manifesto, Books on Erlang or Scala/Akka

57