A library for Elgot Monadsober:talk-bay.pdfThe project This project consists of two parts: A generic...

53
A library for Elgot Monads Christian Bay FAU Erlangen-N¨ urnberg [email protected] 24 Oktober 2017 Christian Bay (FAU) 24 Oktober 2017 1 / 40

Transcript of A library for Elgot Monadsober:talk-bay.pdfThe project This project consists of two parts: A generic...

Page 1: A library for Elgot Monadsober:talk-bay.pdfThe project This project consists of two parts: A generic library for Monads equipped with iteration. An implementation of Robin Milners

A library for Elgot Monads

Christian Bay

FAU Erlangen-Nurnberg

[email protected]

24 Oktober 2017

Christian Bay (FAU) 24 Oktober 2017 1 / 40

Page 2: A library for Elgot Monadsober:talk-bay.pdfThe project This project consists of two parts: A generic library for Monads equipped with iteration. An implementation of Robin Milners

1 Introduction

2 Development

3 Future work

4 Other works

5 Acknowledgement

6 Sources

Christian Bay (FAU) 24 Oktober 2017 1 / 40

Page 3: A library for Elgot Monadsober:talk-bay.pdfThe project This project consists of two parts: A generic library for Monads equipped with iteration. An implementation of Robin Milners

Agenda

1 Introduction

2 Development

3 Future work

4 Other works

5 Acknowledgement

6 Sources

Christian Bay (FAU) 24 Oktober 2017 2 / 40

Page 4: A library for Elgot Monadsober:talk-bay.pdfThe project This project consists of two parts: A generic library for Monads equipped with iteration. An implementation of Robin Milners

The project

This project consists of two parts:

A generic library for Monads equipped with iteration.

An implementation of Robin Milners CCS-Processes with thecoinductive generalized resumption transformer.

Christian Bay (FAU) 24 Oktober 2017 3 / 40

Page 5: A library for Elgot Monadsober:talk-bay.pdfThe project This project consists of two parts: A generic library for Monads equipped with iteration. An implementation of Robin Milners

What are Monads?

In category theory a monad is described as Kleisli Triple pT , η, ˚qover category C forming the category CT :

T is a function on objects T : ObjC Ñ ObjC

Unit η is the morphism family: ηX : X Ñ TX

Kleisli lifting ˚ maps f : X Ñ TY to f ˚ : TX Ñ TY

The objects and morphisms of CT have following defintions:ObjpCT q “ ObjpC q

HomCTpX ,Y q “ HomC pX ,TY q

idTX “ η˚X pg˚ ˝ f q˚ “ g˚ ˝ f ˚

pf ˚ ˝ ηX q “ fwhere f : X Ñ TY and g : Y Ñ TZ

Christian Bay (FAU) 24 Oktober 2017 4 / 40

Page 6: A library for Elgot Monadsober:talk-bay.pdfThe project This project consists of two parts: A generic library for Monads equipped with iteration. An implementation of Robin Milners

What are Monads?

In programming languages a monad is a way to representcomputational side-effects by generalizing functions of type:

f : X Ñ TYIn Haskell monads are defined as an interface:

class Monad m where

return :: a -> m a

(>>=) :: m a -> (a -> m b) -> m b

We can instantiate the optional monad TX “ 1` X as following:

data Maybe a = Nothing | Just a

instance Monad Maybe where

return a = Just a

(Just x) >>= f = f x

Nothing >>= f = Nothing

Christian Bay (FAU) 24 Oktober 2017 5 / 40

Page 7: A library for Elgot Monadsober:talk-bay.pdfThe project This project consists of two parts: A generic library for Monads equipped with iteration. An implementation of Robin Milners

What are Monads?

This allows to write compact code:

f::Int -> Maybe Int

f 0 = Nothing

f x = Just x

g :: Int -> Maybe Int

g 100 = Nothing

g x = Just x

h :: Int -> Maybe Int

h x = do n <- f x

g n

Christian Bay (FAU) 24 Oktober 2017 6 / 40

Page 8: A library for Elgot Monadsober:talk-bay.pdfThe project This project consists of two parts: A generic library for Monads equipped with iteration. An implementation of Robin Milners

Iteration operation for monads

We are especially interested in recursive definitions over monads.These are morphisms of type:

f : X Ñ T pY ` X q

fxx

y

We wish to equip those functions with a Conway Operator ::f : : X Ñ TY

Christian Bay (FAU) 24 Oktober 2017 7 / 40

Page 9: A library for Elgot Monadsober:talk-bay.pdfThe project This project consists of two parts: A generic library for Monads equipped with iteration. An implementation of Robin Milners

(Complete) Elgot Monad

So what is this (complete) Elgot Monad about? It is a strong monadequipped with an Conway Operator which satisfies naturality

f gxy z

“ f gxy z

xand uniformity

fhz x

y

x“ g

z

y

zh

x

ó

fhz x

y

x

“ gz

y

z

. . . and three other laws: unfolding, dinaturality and codiagonal.

Christian Bay (FAU) 24 Oktober 2017 8 / 40

Page 10: A library for Elgot Monadsober:talk-bay.pdfThe project This project consists of two parts: A generic library for Monads equipped with iteration. An implementation of Robin Milners

Elgot Monad

In Haskell an Elgot Monad can be defined as follows:

class Monad m => ElgotMonad m where

-- | Default implementation as the least fixpoint.

iteration :: (a -> m (Either b a)) -> a -> m b

iteration f a = f a >>= either return (iteration f)

-- | Divergent computation which must be overloaded.

nil :: a -> m b

nil = iteration (return . Right)

An example with list:

instance ElgotMonad [] where

nil _ = []

elgotTest :: Int -> [Either Int Int]

elgotTest n

| n <= 0 = [Left 0]

| otherwise = [Right (n-1), Left n]

>>> elgotTest ‘iteration ‘ 5 = [0,1,2,3,4,5]

Christian Bay (FAU) 24 Oktober 2017 9 / 40

Page 11: A library for Elgot Monadsober:talk-bay.pdfThe project This project consists of two parts: A generic library for Monads equipped with iteration. An implementation of Robin Milners

CCS Process Repetition

Syntax:P ,Q ::“ K

ˇ

ˇ α.Pˇ

ˇ

ÿ

iPI

Pi

ˇ

ˇ P | Qˇ

ˇ Prf sˇ

ˇ PzL

Semantic (SOS-Rules):(ACT )

α.PαÝÑ P

PjαjÝÑ Q

(SUMj)ř

iPI PiαjÝÑ Q

PaÝÑ P 1(COM1)

P | QaÝÑ P 1 | Q

QaÝÑ Q 1

(COM2)P | Q

aÝÑ P | Q 1

PaÝÑ P 1 Q

aÝÑ Q 1

(COM3)P | Q

τÝÑ P 1 | Q 1

PαÝÑ P 1(RES) (α, α R L)

P\L αÝÑ P 1\L

PαÝÑ P 1(REL)

Prf sf pαqÝÝÑ P 1rf s

PiαÝÑ P 1

(CON)Ki

αÝÑ P 1

Christian Bay (FAU) 24 Oktober 2017 10 / 40

Page 12: A library for Elgot Monadsober:talk-bay.pdfThe project This project consists of two parts: A generic library for Monads equipped with iteration. An implementation of Robin Milners

CCS Process Repetition

Syntax:P ,Q ::“ K

ˇ

ˇ α.Pˇ

ˇ

ÿ

iPI

Pi

ˇ

ˇ P | Qˇ

ˇ Prf sˇ

ˇ PzL

Semantic (SOS-Rules):(ACT )

α.PαÝÑ P

PjαjÝÑ Q

(SUMj)ř

iPI PiαjÝÑ Q

PaÝÑ P 1(COM1)

P | QaÝÑ P 1 | Q

QaÝÑ Q 1

(COM2)P | Q

aÝÑ P | Q 1

PaÝÑ P 1 Q

aÝÑ Q 1

(COM3)P | Q

τÝÑ P 1 | Q 1

PαÝÑ P 1(RES) (α, α R L)

P\L αÝÑ P 1\L

PαÝÑ P 1(REL)

Prf sf pαqÝÝÑ P 1rf s

PiαÝÑ P 1

(CON)Ki

αÝÑ P 1

Christian Bay (FAU) 24 Oktober 2017 10 / 40

Page 13: A library for Elgot Monadsober:talk-bay.pdfThe project This project consists of two parts: A generic library for Monads equipped with iteration. An implementation of Robin Milners

CCS Process Repetition

CCS style processes can be imagined as a computation tree whereeach node represents a state and each edge a transition between twostates.

For instance:pa.H` b.Hq | c .H

Can be displayed as a tree:

¨

¨ ¨ ¨

¨ ¨ ¨ ¨

a b c

c c b a

Christian Bay (FAU) 24 Oktober 2017 11 / 40

Page 14: A library for Elgot Monadsober:talk-bay.pdfThe project This project consists of two parts: A generic library for Monads equipped with iteration. An implementation of Robin Milners

CCS Process Repetition

CCS style processes can be imagined as a computation tree whereeach node represents a state and each edge a transition between twostates.For instance:

pa.H` b.Hq | c .HCan be displayed as a tree:

¨

¨ ¨ ¨

¨ ¨ ¨ ¨

a b c

c c b a

Christian Bay (FAU) 24 Oktober 2017 11 / 40

Page 15: A library for Elgot Monadsober:talk-bay.pdfThe project This project consists of two parts: A generic library for Monads equipped with iteration. An implementation of Robin Milners

CCS Process Extension

Choice operation ` may be extended with probabilities:P ,Q ::“ ...,

ÿ

iPI

riPi , ... (1)

Where ri P r0, 1s andř

jPI rj “ 1.

Christian Bay (FAU) 24 Oktober 2017 12 / 40

Page 16: A library for Elgot Monadsober:talk-bay.pdfThe project This project consists of two parts: A generic library for Monads equipped with iteration. An implementation of Robin Milners

Agenda

1 Introduction

2 Development

3 Future work

4 Other works

5 Acknowledgement

6 Sources

Christian Bay (FAU) 24 Oktober 2017 13 / 40

Page 17: A library for Elgot Monadsober:talk-bay.pdfThe project This project consists of two parts: A generic library for Monads equipped with iteration. An implementation of Robin Milners

Coinductive generalized resumption transformer

To represent computation trees with side effects the coinductivegeneralizing resumption transformer (suggested by Uustalu et al.)can be used:

TFX “ νγ.T pX ` Fγq, where F is an endofunctor and T an Elgot Monad.

-- The free monad transformer

newtype IterFT f m a =

IterFT {

runIterFT :: m (StepEither f a (IterFT f m a))

}

-- | A bifunctor which either returns an instance of type @a@ or

applies the functor to some instance of type @b@.

data StepEither f a b = Done a | Resume (f b)

Christian Bay (FAU) 24 Oktober 2017 14 / 40

Page 18: A library for Elgot Monadsober:talk-bay.pdfThe project This project consists of two parts: A generic library for Monads equipped with iteration. An implementation of Robin Milners

Coinductive generalized resumption transformer

To represent computation trees with side effects the coinductivegeneralizing resumption transformer (suggested by Uustalu et al.)can be used:

TFX “ νγ.T pX ` Fγq, where F is an endofunctor and T an Elgot Monad.

-- The free monad transformer

newtype IterFT f m a =

IterFT {

runIterFT :: m (StepEither f a (IterFT f m a))

}

-- | A bifunctor which either returns an instance of type @a@ or

applies the functor to some instance of type @b@.

data StepEither f a b = Done a | Resume (f b)

Christian Bay (FAU) 24 Oktober 2017 14 / 40

Page 19: A library for Elgot Monadsober:talk-bay.pdfThe project This project consists of two parts: A generic library for Monads equipped with iteration. An implementation of Robin Milners

Coinductive generalized resumption transformer

CCS style processes can be represented with this transformer by usingthe Functor ppa ˆ q ` b ` q:

Sending messages of type a

Receiving messages of type b (We consider a “ b)

Make a silent transition

Co-inductive generalized resumption transformer :T a

aX “ νγ.T pX ` pa ˆ γq ` γa ` γqqIn Haskell the functor is modelled as following:

data Step a x =

Out a x

| In (a -> x)

| Tau x

type Process a = IterFT (Step a)

Christian Bay (FAU) 24 Oktober 2017 15 / 40

Page 20: A library for Elgot Monadsober:talk-bay.pdfThe project This project consists of two parts: A generic library for Monads equipped with iteration. An implementation of Robin Milners

Processalgebra implementation

ACT rule:

Out-Action: Produce process with outgoing action a.

outAct :: ElgotMonad r => a -> Process a r ()

outAct a = IterFT . return . Resume $ Out a (return ())

In-Action: Produce process waiting for the correct input action.

inAct :: (Eq a, ElgotMonad r) => a -> Process a r ()

inAct a =

IterFT . return . Resume $

In (\b -> unless (b == a) (IterFT ( nil () ) ) )

Christian Bay (FAU) 24 Oktober 2017 16 / 40

Page 21: A library for Elgot Monadsober:talk-bay.pdfThe project This project consists of two parts: A generic library for Monads equipped with iteration. An implementation of Robin Milners

Processalgebra implementation

Choice `: Processes can’t choose different paths yet.ñ Offer interface for each Elgot Monad T to flip a coin:

class TossMonad a m | m -> a where

toss :: a -> m Bool

-- | Instanciate []

instance TossMonad () [] where

toss _ = [True , False]

Now the choice operation is easy:

choice :: (ElgotMonad m, TossMonad t m) => t -> m b -> m b ->

m b

choice prob p q = do

coin <- toss prob

if coin

then p

else q

Christian Bay (FAU) 24 Oktober 2017 17 / 40

Page 22: A library for Elgot Monadsober:talk-bay.pdfThe project This project consists of two parts: A generic library for Monads equipped with iteration. An implementation of Robin Milners

Processalgebra implementation

Parallelization P |Q:Considerations:

- The order is arbitrary Ñ use (multiple) toss.- Produce τ actions if synchronization is possible.

Realization is done via iteration:

comCoproductIter :: (TossMonad t r, ElgotMonad r)

=> t

-> (Process a r x, Process a r y)

-> Process a r ((x + y) + (Process a r x, Process a r y))

Christian Bay (FAU) 24 Oktober 2017 18 / 40

Page 23: A library for Elgot Monadsober:talk-bay.pdfThe project This project consists of two parts: A generic library for Monads equipped with iteration. An implementation of Robin Milners

Processalgebra implementation

Rename: Apply a renaming function f which holds:f pxq “ f pf pxqq, @x P Act

Hiding: After evaluation remove processes which use hiddenprocessnames.

Christian Bay (FAU) 24 Oktober 2017 19 / 40

Page 24: A library for Elgot Monadsober:talk-bay.pdfThe project This project consists of two parts: A generic library for Monads equipped with iteration. An implementation of Robin Milners

Example: University

CM “ coin.coffee.CM ` coin.tea.CM

coffeeMachine t () = choice t (inAct "Coin" >> outAct "Coffee" >>

resume ()) (inAct "Coin" >> outAct "Tea" >> resume ())

CS “ coin.coffee.pub.CS

computerStudent () =

outAct "Coin" >> inAct "Coffee" outAct "Pub" >> resume ()

UNI “ pCM |CSq\tcoffee, tea, coinu

university parallelToss choiceToss =

hiding ["Coffee", "Coin", "Tea"] (comCoproduct

parallelToss (iteration computerStudent (),

iteration (coffeeMachine choiceToss) ()))

Evaluate the process and observe outgoing actions:

>>> evalOut [] testUni

[[pubs,[pub.pubs,[pub.pub.pubs, . . .]

Christian Bay (FAU) 24 Oktober 2017 20 / 40

Page 25: A library for Elgot Monadsober:talk-bay.pdfThe project This project consists of two parts: A generic library for Monads equipped with iteration. An implementation of Robin Milners

Unguardedness

Let’s observe an unguarded process:P “ a.P` P

unguardedEx t () = choice t (inAct "a" >> resume ()) (resume ())

Evaluate the possible endprocesses:

>>> eval [a] unguardedList

[[a.H],[a.H],[a.H],. . .]

ñ This infinite stream of the same result isn’t quite handy. We wantthe least fixpoint behaviour instead.

Christian Bay (FAU) 24 Oktober 2017 21 / 40

Page 26: A library for Elgot Monadsober:talk-bay.pdfThe project This project consists of two parts: A generic library for Monads equipped with iteration. An implementation of Robin Milners

ElgotList

Introduce a list wrapper ElgotList:

newtype ElgotList a = EL { runEL :: [a] }

The iteration should act like this:

emulateSet previousArguments f a =

if a ‘elem ‘ previousArguments

then Stop Iteration

else Continue

But this aquires a comparison and therefore a strighter interface,because Haskell doesn’t allow restrictions for some instances only.

class ElgotMonad m => ElgotMonadEq m where

iterationEq :: Eq a => (a -> m (Either b a)) -> a -> m b

Christian Bay (FAU) 24 Oktober 2017 22 / 40

Page 27: A library for Elgot Monadsober:talk-bay.pdfThe project This project consists of two parts: A generic library for Monads equipped with iteration. An implementation of Robin Milners

ElgotList

Define unguarded process for ElgotList:

>>> eval [a] unguardedElgotList

[[a.H],[a.H],[a.H],. . .]

But why is it still not working?

Answer: Iterations over T aa don’t call the iteration defined for T yet!

ñ Morphisms need to be guarded first. Guardedness assures thatafter each iteration step a monadic side-effect of T takes place.

Christian Bay (FAU) 24 Oktober 2017 23 / 40

Page 28: A library for Elgot Monadsober:talk-bay.pdfThe project This project consists of two parts: A generic library for Monads equipped with iteration. An implementation of Robin Milners

ElgotList

Define unguarded process for ElgotList:

>>> eval [a] unguardedElgotList

[[a.H],[a.H],[a.H],. . .]

But why is it still not working?

Answer: Iterations over T aa don’t call the iteration defined for T yet!

ñ Morphisms need to be guarded first. Guardedness assures thatafter each iteration step a monadic side-effect of T takes place.

Christian Bay (FAU) 24 Oktober 2017 23 / 40

Page 29: A library for Elgot Monadsober:talk-bay.pdfThe project This project consists of two parts: A generic library for Monads equipped with iteration. An implementation of Robin Milners

ElgotList

Define unguarded process for ElgotList:

>>> eval [a] unguardedElgotList

[[a.H],[a.H],[a.H],. . .]

But why is it still not working?

Answer: Iterations over T aa don’t call the iteration defined for T yet!

ñ Morphisms need to be guarded first. Guardedness assures thatafter each iteration step a monadic side-effect of T takes place.

Christian Bay (FAU) 24 Oktober 2017 23 / 40

Page 30: A library for Elgot Monadsober:talk-bay.pdfThe project This project consists of two parts: A generic library for Monads equipped with iteration. An implementation of Robin Milners

ElgotList

In the paper unguarded recursion on coinductive resumptions[Goncharov, Rauch, Schroder] an operator Ź is defined for anyf : X Ñ T a

a pY ` X q with Ź f : X Ñ T aa pY ` X q to guard processes:

unguardedEx t () = choice t (inAct a >> resume ()) (resume ())

1 Make only one step to lift inner monad:oneStep ::

pX Ñ T aa pY ` X qq

loooooooooomoooooooooon

f

Ñ X Ñ T ppY ` T aa pY ` X qaaq ` X q

ElgotList [Left (Right (In (λactÑ unless (act == a) (IterFT (nil()))))), Right ()]

Christian Bay (FAU) 24 Oktober 2017 24 / 40

Page 31: A library for Elgot Monadsober:talk-bay.pdfThe project This project consists of two parts: A generic library for Monads equipped with iteration. An implementation of Robin Milners

ElgotList

In the paper unguarded recursion on coinductive resumptions[Goncharov, Rauch, Schroder] an operator Ź is defined for anyf : X Ñ T a

a pY ` X q with Ź f : X Ñ T aa pY ` X q to guard processes:

unguardedEx t () = choice t (inAct a >> resume ()) (resume ())

1 Make only one step to lift inner monad:oneStep ::

pX Ñ T aa pY ` X qq

loooooooooomoooooooooon

f

Ñ X Ñ T ppY ` T aa pY ` X qaaq ` X q

ElgotList [Left (Right (In (λactÑ unless (act == a) (IterFT (nil()))))), Right ()]

Christian Bay (FAU) 24 Oktober 2017 24 / 40

Page 32: A library for Elgot Monadsober:talk-bay.pdfThe project This project consists of two parts: A generic library for Monads equipped with iteration. An implementation of Robin Milners

ElgotList

2 Iterate over the inner monad:helper :: pX Ñ T a

a pY ` X qq Ñ X Ñ T pY ` T aa pY ` X qaaq

helper f x “ iterationponeStep fq xApply Right () to unguardedEx again. Iteration of ElgotListis called and therefore stops after one cycle.

ElgotList [(Right (In (λactÑ unless (act == a) (IterFT (nil ())))))]

3 Reorder the coproducts, re-lift and composite:Ź“ out´1 ˝ T pinl ` idq ˝ helper

IterFT ( ElgotList [Resume (In (λactÑ unless (act == a) (IterFT (nil())))))])

Christian Bay (FAU) 24 Oktober 2017 25 / 40

Page 33: A library for Elgot Monadsober:talk-bay.pdfThe project This project consists of two parts: A generic library for Monads equipped with iteration. An implementation of Robin Milners

ElgotList

2 Iterate over the inner monad:helper :: pX Ñ T a

a pY ` X qq Ñ X Ñ T pY ` T aa pY ` X qaaq

helper f x “ iterationponeStep fq xApply Right () to unguardedEx again. Iteration of ElgotListis called and therefore stops after one cycle.

ElgotList [(Right (In (λactÑ unless (act == a) (IterFT (nil ())))))]

3 Reorder the coproducts, re-lift and composite:Ź“ out´1 ˝ T pinl ` idq ˝ helper

IterFT ( ElgotList [Resume (In (λactÑ unless (act == a) (IterFT (nil())))))])

Christian Bay (FAU) 24 Oktober 2017 25 / 40

Page 34: A library for Elgot Monadsober:talk-bay.pdfThe project This project consists of two parts: A generic library for Monads equipped with iteration. An implementation of Robin Milners

ElgotList

After assigning this algorithm to TFX we finally get the rightbehavior:

>>> eval [a] unguardedElgotList

ElgotList [[a.H]]

Christian Bay (FAU) 24 Oktober 2017 26 / 40

Page 35: A library for Elgot Monadsober:talk-bay.pdfThe project This project consists of two parts: A generic library for Monads equipped with iteration. An implementation of Robin Milners

ElgotList

Problems:

Equality of Processesñ Possible with BisimulationBut comparison of functions in the Step functor?

data Step a x =

...

| In (a -> x)

...

ñ Impossible while iteration of parallelization :(

Code duplication

Christian Bay (FAU) 24 Oktober 2017 27 / 40

Page 36: A library for Elgot Monadsober:talk-bay.pdfThe project This project consists of two parts: A generic library for Monads equipped with iteration. An implementation of Robin Milners

Probabilistic processes

Another instance for an Elgot Monad are probabilistic processes andtherefore equation systems with probabilities:

p x “ 0.3x` 0.3y` 0.4z

p y “ 0.3False` 0.3y` 0.4z

p z “ 0.3x` 0.3y` 0.4Trueñ As for ElgotList, some kind of fixpoint behaviour is necessary.

Christian Bay (FAU) 24 Oktober 2017 28 / 40

Page 37: A library for Elgot Monadsober:talk-bay.pdfThe project This project consists of two parts: A generic library for Monads equipped with iteration. An implementation of Robin Milners

The Probability Elgot monad

Our solution: A Gauß like algorithm1 Search for selfreferencing calls, distribute their probability on

remaining items and save it in a history.p x “ 0.3x` 0.3y` 0.4z

History “ rx Ñ0.3

p1´ 0.3qy `

0.4

p1´ 0.3qzs

2 Follow recursive calls and multiply them with the givenprobability:

p y “ 0.3 ¨ p0.3False` 0.3y` 0.4zq

History “ ry Ñ 0.42False` 0.58z, x Ñ 0.42y` 0.58zs3 Substitute new variable in History:

History “ ry Ñ 0.42False` 0.58z, x Ñ 0.18y` 0.82zs4 Repeat until all paths were traversed (omit already visited ones)

and collect results:rp0.5594405594405594,Trueq, p0.4405594405594407,Falseqs

Christian Bay (FAU) 24 Oktober 2017 29 / 40

Page 38: A library for Elgot Monadsober:talk-bay.pdfThe project This project consists of two parts: A generic library for Monads equipped with iteration. An implementation of Robin Milners

The Probability Elgot monad

Our solution: A Gauß like algorithm1 Search for selfreferencing calls, distribute their probability on

remaining items and save it in a history.p x “ 0.3x` 0.3y` 0.4z

History “ rx Ñ0.3

p1´ 0.3qy `

0.4

p1´ 0.3qzs

2 Follow recursive calls and multiply them with the givenprobability:

p y “ 0.3 ¨ p0.3False` 0.3y` 0.4zq

History “ ry Ñ 0.42False` 0.58z, x Ñ 0.42y` 0.58zs

3 Substitute new variable in History:History “ ry Ñ 0.42False` 0.58z, x Ñ 0.18y` 0.82zs

4 Repeat until all paths were traversed (omit already visited ones)and collect results:rp0.5594405594405594,Trueq, p0.4405594405594407,Falseqs

Christian Bay (FAU) 24 Oktober 2017 29 / 40

Page 39: A library for Elgot Monadsober:talk-bay.pdfThe project This project consists of two parts: A generic library for Monads equipped with iteration. An implementation of Robin Milners

The Probability Elgot monad

Our solution: A Gauß like algorithm1 Search for selfreferencing calls, distribute their probability on

remaining items and save it in a history.p x “ 0.3x` 0.3y` 0.4z

History “ rx Ñ0.3

p1´ 0.3qy `

0.4

p1´ 0.3qzs

2 Follow recursive calls and multiply them with the givenprobability:

p y “ 0.3 ¨ p0.3False` 0.3y` 0.4zq

History “ ry Ñ 0.42False` 0.58z, x Ñ 0.42y` 0.58zs3 Substitute new variable in History:

History “ ry Ñ 0.42False` 0.58z, x Ñ 0.18y` 0.82zs

4 Repeat until all paths were traversed (omit already visited ones)and collect results:rp0.5594405594405594,Trueq, p0.4405594405594407,Falseqs

Christian Bay (FAU) 24 Oktober 2017 29 / 40

Page 40: A library for Elgot Monadsober:talk-bay.pdfThe project This project consists of two parts: A generic library for Monads equipped with iteration. An implementation of Robin Milners

The Probability Elgot monad

Our solution: A Gauß like algorithm1 Search for selfreferencing calls, distribute their probability on

remaining items and save it in a history.p x “ 0.3x` 0.3y` 0.4z

History “ rx Ñ0.3

p1´ 0.3qy `

0.4

p1´ 0.3qzs

2 Follow recursive calls and multiply them with the givenprobability:

p y “ 0.3 ¨ p0.3False` 0.3y` 0.4zq

History “ ry Ñ 0.42False` 0.58z, x Ñ 0.42y` 0.58zs3 Substitute new variable in History:

History “ ry Ñ 0.42False` 0.58z, x Ñ 0.18y` 0.82zs4 Repeat until all paths were traversed (omit already visited ones)

and collect results:rp0.5594405594405594,Trueq, p0.4405594405594407,Falseqs

Christian Bay (FAU) 24 Oktober 2017 29 / 40

Page 41: A library for Elgot Monadsober:talk-bay.pdfThe project This project consists of two parts: A generic library for Monads equipped with iteration. An implementation of Robin Milners

Biased coin example

Von Neumann gave the following procedure to get fair results from abiased coin:

1 Toss the coin twice.2 If the results match, start over, forgetting both results.3 If the results differ, use the first result, forgetting the second.

biasedCoin :: (TossMonad a m, ElgotMonad m) => a -> () -> m (

Either Bool ())

biasedCoin p () = do

a <- toss p; b <- toss p;

if a == b

then resume ()

else done a

>>> biasedCoin 0.2 ()

([(0.16,Left True),(0.16,Left False)looooooooooooooooooooooooooomooooooooooooooooooooooooooon

results have same equality

,(0.64,Right ()),(0.04,Right ())looooooooooooooooooooooooomooooooooooooooooooooooooon

recursive calls

])

>>> iterationEq (biasedCoin 0.2) ()

[(0.5, Left True), (0.5 Left Right)]

Christian Bay (FAU) 24 Oktober 2017 30 / 40

Page 42: A library for Elgot Monadsober:talk-bay.pdfThe project This project consists of two parts: A generic library for Monads equipped with iteration. An implementation of Robin Milners

Biased coin example

Von Neumann gave the following procedure to get fair results from abiased coin:

1 Toss the coin twice.2 If the results match, start over, forgetting both results.3 If the results differ, use the first result, forgetting the second.

biasedCoin :: (TossMonad a m, ElgotMonad m) => a -> () -> m (

Either Bool ())

biasedCoin p () = do

a <- toss p; b <- toss p;

if a == b

then resume ()

else done a

>>> biasedCoin 0.2 ()

([(0.16,Left True),(0.16,Left False)looooooooooooooooooooooooooomooooooooooooooooooooooooooon

results have same equality

,(0.64,Right ()),(0.04,Right ())looooooooooooooooooooooooomooooooooooooooooooooooooon

recursive calls

])

>>> iterationEq (biasedCoin 0.2) ()

[(0.5, Left True), (0.5 Left Right)]

Christian Bay (FAU) 24 Oktober 2017 30 / 40

Page 43: A library for Elgot Monadsober:talk-bay.pdfThe project This project consists of two parts: A generic library for Monads equipped with iteration. An implementation of Robin Milners

Example of a probabilistic process

Processes can be equipped with theProbability monad too:T a

aX “ νγ.T pX ` pa ˆ γq ` γa ` γqqwith T “ Probability

>>> evalOutProb [a, a] testProcess

[( 0.25loomoon

Probability of

, rsloomoon

outputs

) ,(0.75,[b])]

¨

¨

¨

¨

a

τ

τ0.5

b0.5

Christian Bay (FAU) 24 Oktober 2017 31 / 40

Page 44: A library for Elgot Monadsober:talk-bay.pdfThe project This project consists of two parts: A generic library for Monads equipped with iteration. An implementation of Robin Milners

Example of a probabilistic process

But for processes with parallelization like University,CM “ coin.coffee.CM ` coin.tea.CM

CS “ coin.coffee.pub.CS

UNI “ pCM |CSq\tcoffee, tea, coinu

testUniProb :: Double -> Process String Probability (Either () ())

we end up in a endless loop, because the processes can’t becompared:

>>> evalOutProb [] (testUniProb 0.1)

Probability (

Christian Bay (FAU) 24 Oktober 2017 32 / 40

Page 45: A library for Elgot Monadsober:talk-bay.pdfThe project This project consists of two parts: A generic library for Monads equipped with iteration. An implementation of Robin Milners

Summary

A functional Elgot Monad library in Haskell has beenimplemented with a comprehensive documentation

Instances are the coinductive generalized resumption transformerused for (probabilistic) processes

Fixpoint behavior

Christian Bay (FAU) 24 Oktober 2017 33 / 40

Page 46: A library for Elgot Monadsober:talk-bay.pdfThe project This project consists of two parts: A generic library for Monads equipped with iteration. An implementation of Robin Milners

Agenda

1 Introduction

2 Development

3 Future work

4 Other works

5 Acknowledgement

6 Sources

Christian Bay (FAU) 24 Oktober 2017 34 / 40

Page 47: A library for Elgot Monadsober:talk-bay.pdfThe project This project consists of two parts: A generic library for Monads equipped with iteration. An implementation of Robin Milners

Future work

Find a way to

Compare processes for parallelization

Get around the code duplication introduced through additionalinterfaces (e.g. ElgotMonadEq)

Christian Bay (FAU) 24 Oktober 2017 35 / 40

Page 48: A library for Elgot Monadsober:talk-bay.pdfThe project This project consists of two parts: A generic library for Monads equipped with iteration. An implementation of Robin Milners

Agenda

1 Introduction

2 Development

3 Future work

4 Other works

5 Acknowledgement

6 Sources

Christian Bay (FAU) 24 Oktober 2017 36 / 40

Page 49: A library for Elgot Monadsober:talk-bay.pdfThe project This project consists of two parts: A generic library for Monads equipped with iteration. An implementation of Robin Milners

Other works

Haskell library free

Contains an implementation for (co-) free (co-)monads.Differences:

Has no interface like the Elgot Monad class

Only way to iterate is to write own function or use the default onñ No fix-point behaviour

Huge library which covers several free monads and provides forinstance functions for natural transformations and monadhomomorphisms on them.

Christian Bay (FAU) 24 Oktober 2017 37 / 40

Page 50: A library for Elgot Monadsober:talk-bay.pdfThe project This project consists of two parts: A generic library for Monads equipped with iteration. An implementation of Robin Milners

Agenda

1 Introduction

2 Development

3 Future work

4 Other works

5 Acknowledgement

6 Sources

Christian Bay (FAU) 24 Oktober 2017 38 / 40

Page 51: A library for Elgot Monadsober:talk-bay.pdfThe project This project consists of two parts: A generic library for Monads equipped with iteration. An implementation of Robin Milners

Thanks

Thanks to my tutor Dr. Sergey Goncharov for supervisingthis project!

Christian Bay (FAU) 24 Oktober 2017 39 / 40

Page 52: A library for Elgot Monadsober:talk-bay.pdfThe project This project consists of two parts: A generic library for Monads equipped with iteration. An implementation of Robin Milners

Thanks

Thanks to my tutor Dr. Sergey Goncharov for supervisingthis project!

Christian Bay (FAU) 24 Oktober 2017 39 / 40

Page 53: A library for Elgot Monadsober:talk-bay.pdfThe project This project consists of two parts: A generic library for Monads equipped with iteration. An implementation of Robin Milners

Quellen I

§ Venanzio Capretta, Thorsten Altenkirch, and Tarmo Uustalu.Partiality is an effect.In Slides for a talk given by Uustalu at the 22nd meeting of IFIPWorking Group, volume 2, 2005.

§ Sergey Goncharov, Christoph Rauch, and Lutz Schroder.Unguarded recursion on coinductive resumptions.Electronic Notes in Theoretical Computer Science, 319:183–198,2015.

§ Sergey Goncharov and Lutz Schroder.A coinductive calculus for asynchronous side-effecting processes.In FCT, volume 11, pages 276–287. Springer, 2011.

Christian Bay (FAU) 24 Oktober 2017 40 / 40