68
The Essence of Reactive Programming A Theoretical Approach Eddy Bertoluzzo

The essence of Reactive Programming

Embed Size (px)

Citation preview

Page 1: The essence of Reactive Programming

The Essence of Reactive ProgrammingA Theoretical Approach

Eddy Bertoluzzo

Page 2: The essence of Reactive Programming

What does it mean to be Reactive?

Page 3: The essence of Reactive Programming

● Reactive Programming - Wikipedia

Let’s ask Google

Page 4: The essence of Reactive Programming

Reactive Programming - Wikipedia

Page 5: The essence of Reactive Programming

● Reactive Programming - Wikipedia

● ReactiveX - Rx*

● Functional Reactive Programming

Let’s ask Google

Page 6: The essence of Reactive Programming

Reactive Programming

???

Functional Reactive Programming

Page 7: The essence of Reactive Programming

Reactive Programming

!=

Functional Reactive Programming

Page 8: The essence of Reactive Programming

● Reactive Programming - Wikipedia

● ReactiveX - Rx*

● Functional Reactive Programming

● Reactive Manifesto - Reactive

Streams

Let’s ask Google

Page 9: The essence of Reactive Programming
Page 10: The essence of Reactive Programming

● Reactive Programming - Wikipedia

● ReactiveX - Rx*

● Functional Reactive Programming

● Reactive Manifesto - Reactive

Streams

Let’s ask Google

Page 11: The essence of Reactive Programming
Page 12: The essence of Reactive Programming

Quoting Leslie Lamport

“There’s something about the culture of software that has impeded the use

of specification.

We have a wonderful way of describing things precisely that’s

been developed over the last couple of millennia, called mathematics.

I think that’s what we should be using as a way of thinking about

what we build.”

Page 13: The essence of Reactive Programming

The Four Fundamental Effects

One Many

Sync a Iterable a

Async Future a ???

Page 14: The essence of Reactive Programming

The Four Fundamental Effects

One Many

Sync a Iterable a

Async Future a Reactive Programming

Page 15: The essence of Reactive Programming

Can you feel the Duality tonight?

Interactive vs Reactive

Sync vs Async

Pull vs Push

Iterable vs ???

Page 16: The essence of Reactive Programming

Iterable

data Iterator a = Iterator

{ moveNext :: () -> IO Bool

, current :: () -> a

}

newtype Iterable a = Iterable

{ getIterator :: () -> IO (Iterator a)

}

Page 17: The essence of Reactive Programming

Optional Values

data Maybe a = Nothing | Just a

Page 18: The essence of Reactive Programming

Iterable

data Iterator a = Iterator

{ moveNext :: () -> IO Bool

, current :: () -> a

}

newtype Iterable a = Iterable

{ getIterator :: () -> IO (Iterator a)

}

Page 19: The essence of Reactive Programming

Iterable

data Iterator a = Iterator

{ moveNext :: () -> IO (Maybe a)

}

newtype Iterable a = Iterable

{ getIterator :: () -> IO (Iterator a)

}

Page 20: The essence of Reactive Programming

Coproducts

data Either a b = Left a | Right b

Page 21: The essence of Reactive Programming

Iterable

data Iterator a = Iterator

{ moveNext :: () -> IO (Maybe a)

}

newtype Iterable a = Iterable

{ getIterator :: () -> IO (Iterator a)

}

Page 22: The essence of Reactive Programming

Iterable

data Iterator a = Iterator

{ moveNext :: () -> IO (Either SomeException (Maybe a))

}

newtype Iterable a = Iterable

{ getIterator :: () -> IO (Iterator a)

}

Page 23: The essence of Reactive Programming

Iterable

type Iterator a = () -> IO (Either SomeException (Maybe a))

type Iterable a = () -> IO (Iterator a)

Page 24: The essence of Reactive Programming

Iterable

type Iterator a = () -> IO a

type Iterable a = () -> IO (Iterator a)

Page 25: The essence of Reactive Programming

Iterable

type Iterator a = () -> IO a

type Iterable a = () -> IO (() -> IO a)

Page 26: The essence of Reactive Programming

Iterator == Getter

() -> IO a

Page 27: The essence of Reactive Programming

Covariance

a <: b

Iterator a <: Iterator b

Page 28: The essence of Reactive Programming

Covariance

a <: b

Iterator a <: Iterator b

Page 29: The essence of Reactive Programming

Covariance

Coke <: Drink

VCoke

<: VDrink

Page 30: The essence of Reactive Programming

Functor

fmap :: (a -> b)

-> Iterator a

-> Iterator b

fmap f ia = \() -> f (ia ())

fmap f ia = f . ia -- g (f (x)) = g . f

fmap = (.)

Page 31: The essence of Reactive Programming

Iterable == Getter of Getters

() -> IO (() -> IO a)

Page 32: The essence of Reactive Programming

Iterable == Getter of Getters

() -> IO (() -> IO a)

Page 33: The essence of Reactive Programming

Lifting

Transform a function into a corresponding function within another - usually more general - setting.

lift :: (Iterator a -> Iterator b)

-> Iterable a

-> Iterable b

lift f iia = \() -> f (iia ())

lift = (.)

Page 34: The essence of Reactive Programming

Functor

fmap' :: (a -> b)

-> Iterable a

-> Iterable b

fmap' f iia = lift (fmap f) iia

Page 35: The essence of Reactive Programming

Applying Duality

==

Reverse all the ->

Page 36: The essence of Reactive Programming

Observer

type Iterator a = () -> IO a

= () IO <- a

type Observer a = a -> IO ()

Page 37: The essence of Reactive Programming

Observer

type Iterator a = () -> IO a

= () IO <- a

type Observer a = a -> IO ()

Page 38: The essence of Reactive Programming

Observer

type Iterator a = () -> IO a

= () IO <- a

type Observer a = a -> IO ()

Page 39: The essence of Reactive Programming

Observable

type Iterable a = () -> IO (() -> IO a)

= () IO <- (() IO <- a)

type Observable a = (a -> IO ()) -> IO ()

Page 40: The essence of Reactive Programming

Observable

type Iterable a = () -> IO (() -> IO a)

= () IO <- (() IO <- a)

type Observable a = (a -> IO ()) -> IO ()

Page 41: The essence of Reactive Programming

Observable

type Iterable a = () -> IO (() -> IO a)

= () IO <- (() IO <- a)

type Observable a = (a -> IO ()) -> IO ()

Page 42: The essence of Reactive Programming

Observable

type Observer a = a -> IO ()

type Observable a = (a -> IO ()) -> IO ()

Page 43: The essence of Reactive Programming

Observer == Setter

a -> IO ()

Page 44: The essence of Reactive Programming

Contravariance

a <: b

Observer b <: Observer a

Page 45: The essence of Reactive Programming

Contravariance

a <: b

Observer b <: Observer a

Page 46: The essence of Reactive Programming

Contravariance

Coke <: Drink

RDrink

<: RCoke

Page 47: The essence of Reactive Programming

Contravariant Functor

contramap :: (a -> b)

-> Observer b

-> Observer a

contramap f ob = \a -> ob (f a)

contramap f ob = ob . f

contramap = flip (.)

Page 48: The essence of Reactive Programming

Observable == Setter of Setters

(a -> IO ()) -> IO ()

Page 49: The essence of Reactive Programming

Observable == Setter of Setters

(a -> IO ()) -> IO ()

Page 50: The essence of Reactive Programming

Lifting

lift :: (Observer b -> Observer a)

-> Observable a

-> Observable b

lift f ooa = \ob -> ooa (f ob)

lift f ooa = ooa . f

lift = flip (.)

Page 51: The essence of Reactive Programming

Functor

fmap :: (a -> b)

-> Observable a

-> Observable b

fmap f ooa = lift (contramap f) ooa

Page 52: The essence of Reactive Programming

Continuation Passing Style (CPS)Suspended computation taking a continuation as argument. Instead of returning a result, they will push it to the continuation.

cps :: (a -> r) -> r

add :: Int -> Int -> Int

add x y = x + y

add_cps :: Int -> Int -> ((Int -> r) -> r)

add_cps x y = \k -> k (x + y)

Page 53: The essence of Reactive Programming

Observable == CPS function

cps :: (a -> r ) -> r

observable :: (a -> IO ()) -> IO ()

Page 54: The essence of Reactive Programming

Observable

type Observer a = a -> IO ()

type Observable a = (a -> IO ()) -> IO ()

Page 55: The essence of Reactive Programming

Observable

type Observer a =

Either SomeException (Maybe a) -> IO ()

type Observable a = (Observer a) -> IO ()

Page 56: The essence of Reactive Programming

Observable

newtype Observer a = Observer

{ onNext :: Either SomeException (Maybe a) -> IO ()

}

newtype Observable a =

{ subscribe :: (Observer a) -> IO ()

}

Page 57: The essence of Reactive Programming

Observable

data Observer a = Observer

{ onNext :: a -> IO ()

, onError :: SomeException -> IO ()

, onCompleted :: IO ()

}

newtype Observable a =

{ subscribe :: (Observer a) -> IO ()

}

Page 58: The essence of Reactive Programming

The Four Fundamental Effects

One Many

Sync a Iterable a

Async Future a Observable a

Page 59: The essence of Reactive Programming

What about backpressure?

Page 60: The essence of Reactive Programming

Quoting Gérard Berry

Interactive programs interact at their own speed with users or with other programs…

Reactive programs also maintain a continuous interaction with their environment, but at a speed which is determined by the environment, not by the program itself.

Page 61: The essence of Reactive Programming

Producer Consumer

Reactive

Interactive

Page 62: The essence of Reactive Programming

Reactive Streamspublic interface Publisher<T> {

public void subscribe(Subscriber<? super T> s);

}

public interface Subscriber<T> {

public void onSubscribe(Subscription s);

public void onNext(T t);

public void onError(Throwable t);

public void onComplete();

}

Page 63: The essence of Reactive Programming

Reactive Streams

public interface Subscription {

public void request(long n);

public void cancel();

}

Page 64: The essence of Reactive Programming

AsyncIterable

data AsyncIterator a = AsyncIterator

{ moveNext :: () -> IO (Future Bool)

, current :: () -> a

}

newtype AsyncIterable a = AsyncIterable

{ getAsyncIterable :: () -> AsyncIterable a

}

Page 65: The essence of Reactive Programming

The Four Fundamental Effects

One Many

Sync a Iterable a

Async Future a Observable a

AsyncIterable a

Page 66: The essence of Reactive Programming

Use the Right Tool for the Right Job

Page 67: The essence of Reactive Programming
Page 68: The essence of Reactive Programming

Thank you

Eddy [email protected]