Layer by Layer: combining monads Fredrik Dahlqvist, Alexandra Silva, - - PowerPoint PPT Presentation

layer by layer combining monads
SMART_READER_LITE
LIVE PREVIEW

Layer by Layer: combining monads Fredrik Dahlqvist, Alexandra Silva, - - PowerPoint PPT Presentation

Layer by Layer: combining monads Fredrik Dahlqvist, Alexandra Silva, Louis Parlant October 16, 2018 1/22 Motivation: ProbNetKAT 2/22 Motivation: ProbNetKAT S 2 A simple network: S 1 S 4 S 3 2/22 Motivation: ProbNetKAT S 2 A simple network:


slide-1
SLIDE 1

1/22

Layer by Layer: combining monads

Fredrik Dahlqvist, Alexandra Silva, Louis Parlant October 16, 2018

slide-2
SLIDE 2

2/22

Motivation: ProbNetKAT

slide-3
SLIDE 3

2/22

Motivation: ProbNetKAT

A simple network: S1 S2 S3 S4

slide-4
SLIDE 4

2/22

Motivation: ProbNetKAT

A simple network: S1 S2 S3 S4 Topology: t =(sw = S1; pt = 2; (sw ← S2; pt ← 1) ⊕.9 drop)

&(sw = S1; pt = 3; sw ← S3; p ← 1) &(sw = S2; pt = 4; sw ← S4; p ← 2) &(sw = S3; pt = 4; sw ← S4; p ← 3)

slide-5
SLIDE 5

2/22

Motivation: ProbNetKAT

A simple network: S1 S2 S3 S4 Topology: t =(sw = S1; pt = 2; (sw ← S2; pt ← 1) ⊕.9 drop)

&(sw = S1; pt = 3; sw ← S3; p ← 1) &(sw = S2; pt = 4; sw ← S4; p ← 2) &(sw = S3; pt = 4; sw ← S4; p ← 3)

Forwarding policy: p = (sw = S1; pt ← 2)&(sw = S2; pt ← 4)

slide-6
SLIDE 6

2/22

Motivation: ProbNetKAT

A simple network: S1 S2 S3 S4 Topology: t =(sw = S1; pt = 2; (sw ← S2; pt ← 1) ⊕.9 drop)

&(sw = S1; pt = 3; sw ← S3; p ← 1) &(sw = S2; pt = 4; sw ← S4; p ← 2) &(sw = S3; pt = 4; sw ← S4; p ← 3)

Forwarding policy: p = (sw = S1; pt ← 2)&(sw = S2; pt ← 4) A packet reaches S4: (t; p)∗; (sw = S4)

slide-7
SLIDE 7

3/22

Motivation: ProbNetKAT

Syntactically, ProbNetKAT is a kind of Kleene algebra with probabilistic choice ⊕λ

slide-8
SLIDE 8

3/22

Motivation: ProbNetKAT

Syntactically, ProbNetKAT is a kind of Kleene algebra with probabilistic choice ⊕λ BUT! The denotation of the operator & is odd...

slide-9
SLIDE 9

3/22

Motivation: ProbNetKAT

Syntactically, ProbNetKAT is a kind of Kleene algebra with probabilistic choice ⊕λ BUT! The denotation of the operator & is odd... & isn’t idempotent.

slide-10
SLIDE 10

3/22

Motivation: ProbNetKAT

Syntactically, ProbNetKAT is a kind of Kleene algebra with probabilistic choice ⊕λ BUT! The denotation of the operator & is odd... & isn’t idempotent. ; does not distribute over &

slide-11
SLIDE 11

3/22

Motivation: ProbNetKAT

Syntactically, ProbNetKAT is a kind of Kleene algebra with probabilistic choice ⊕λ BUT! The denotation of the operator & is odd... & isn’t idempotent. ; does not distribute over & Why? What’s going on?

slide-12
SLIDE 12

3/22

Motivation: ProbNetKAT

Syntactically, ProbNetKAT is a kind of Kleene algebra with probabilistic choice ⊕λ BUT! The denotation of the operator & is odd... & isn’t idempotent. ; does not distribute over & Why? What’s going on? General question: How can we add features in a principled and controllable manner?

slide-13
SLIDE 13

4/22

Building languages layer by layer: shopping list

slide-14
SLIDE 14

4/22

Building languages layer by layer: shopping list

First layer:

p ::= skip | p; p | a ∈ At p; skip = skip; p = p, . . .

Monad: (−)∗

slide-15
SLIDE 15

4/22

Building languages layer by layer: shopping list

Second layer:

p ::= abort | p + p | a ∈ At p + abort = abort + p = p, p + q = q + p, p + p = p, . . .

Monad: P First layer:

p ::= skip | p; p | a ∈ At p; skip = skip; p = p, . . .

Monad: (−)∗

slide-16
SLIDE 16

4/22

Building languages layer by layer: shopping list

Topping:

p ::= p ⊕r p | a ∈ At p ⊕r q = q ⊕1−r p, . . .

Monad: D Second layer:

p ::= abort | p + p | a ∈ At p + abort = abort + p = p, p + q = q + p, p + p = p, . . .

Monad: P First layer:

p ::= skip | p; p | a ∈ At p; skip = skip; p = p, . . .

Monad: (−)∗

slide-17
SLIDE 17

5/22

Combining the layers: things can go wrong!

slide-18
SLIDE 18

5/22

Combining the layers: things can go wrong!

The composition of two monads is not necessarily a monad

slide-19
SLIDE 19

5/22

Combining the layers: things can go wrong!

The composition of two monads is not necessarily a monad Combine monads S, T via distributive law

λ : ST → TS

slide-20
SLIDE 20

5/22

Combining the layers: things can go wrong!

The composition of two monads is not necessarily a monad Combine monads S, T via distributive law

λ : ST → TS

No distributive law PD → DP

slide-21
SLIDE 21

5/22

Combining the layers: things can go wrong!

The composition of two monads is not necessarily a monad Combine monads S, T via distributive law

λ : ST → TS

No distributive law PD → DP But there exists a distributive law

(−)∗P → P(−)∗

slide-22
SLIDE 22

5/22

Combining the layers: things can go wrong!

The composition of two monads is not necessarily a monad Combine monads S, T via distributive law

λ : ST → TS

No distributive law PD → DP But there exists a distributive law

(−)∗P → P(−)∗

How do we deal with this systematically?

slide-23
SLIDE 23

6/22

This paper

A general and modular approach for determining :

(a) if a monad combination by distributive law is possible; (b) if it is not possible, exactly which features are broken by the extension;

and

(c) suggests a way to fix the composition by modifying one of the monads.

slide-24
SLIDE 24

7/22

Monads

Monads: a categorical way to encode computational effects:

slide-25
SLIDE 25

7/22

Monads

Monads: a categorical way to encode computational effects: Non-determinism, probabilities, side-effects . . .

slide-26
SLIDE 26

7/22

Monads

Monads: a categorical way to encode computational effects: Non-determinism, probabilities, side-effects . . . Applications of monads include programming language semantics, automata theory, etc. It is convenient to compositionally combine several effects.

slide-27
SLIDE 27

8/22

Definitions

Definition

A Monad (T, η, µ) on a category C is: An endofunctor T : C → C A natural transformation η : 1 → T A natural transformation µ : TT → T (Verifying some structural properties) We will consider monads on Set.

slide-28
SLIDE 28

9/22

Examples

Finite Powerset

P(A) = {B | B ⊆ A, B finite}

Free Monoid (List) A∗ = {w1 . . . wn | n ∈ N, wi ∈ A} Distributions

D(A) = {f | f probability distribution on A,

and Supp(f) finite}

slide-29
SLIDE 29

10/22

Algebras

Definition

An algebra for the monad T is an object A together with a morphism

α : TA → A.

(Verifying some structural properties involving η and µ)

slide-30
SLIDE 30

10/22

Algebras

Definition

An algebra for the monad T is an object A together with a morphism

α : TA → A.

(Verifying some structural properties involving η and µ)

Definition

For a signature Σ and a set of equations E we can define a monad T such that EM(T) ≃ Alg(Σ, E)

slide-31
SLIDE 31

11/22

Examples

Σ

E

P 0, + x+0=0+x=x x+y=y+x (x+y)+z=x+(y+z) x+x=x

(join-semilattice)

(−)∗ 1, ; x;1=1;x=x (x;y);z=x;(y;z)

(monoid)

slide-32
SLIDE 32

12/22

S, T monads, EM(T) ≃ Alg(ΣT, ET), EM(S) ≃ Alg(ΣS, ES)

Definition

A distributive law of T over S is a natural transformation λ : ST → TS (verifying structural properties)

slide-33
SLIDE 33

12/22

S, T monads, EM(T) ≃ Alg(ΣT, ET), EM(S) ≃ Alg(ΣS, ES)

Definition

A distributive law of T over S is a natural transformation λ : ST → TS (verifying structural properties) If T distributes over S, then: TS is a monad

X

ηT

X

u

  • TX

ηS

TX STX

STSTX

SλTX m

  • SSTTX

µS

TTX STTX

SµT

X STX

Operations in ΣS distribute over those of ΣT We call S the inner layer, T the outer layer.

slide-34
SLIDE 34

13/22

Remarks and questions: Distributive laws are one of the go-to methods to compose monads Implements a one-way distributivity of algebraic operations For two given monads, how to know whether there exists a distributive law? How to build it?

slide-35
SLIDE 35

13/22

Remarks and questions: Distributive laws are one of the go-to methods to compose monads Implements a one-way distributivity of algebraic operations For two given monads, how to know whether there exists a distributive law? How to build it?

Theorem

Let T be a monoidal monad, then for any finitary signature Σ, there exists a distributive law λΣ : HΣT → THΣ of the polynomial functor associated with

Σ over T.

slide-36
SLIDE 36

13/22

Remarks and questions: Distributive laws are one of the go-to methods to compose monads Implements a one-way distributivity of algebraic operations For two given monads, how to know whether there exists a distributive law? How to build it?

Theorem

Let T be a monoidal monad, then for any finitary signature Σ, there exists a distributive law λΣ : HΣT → THΣ of the polynomial functor associated with

Σ over T.

Monoidal helps with lifting operations but not equations.

slide-37
SLIDE 37

14/22

The procedure: 1. Build ‘candidate’ λ : ST → TS

slide-38
SLIDE 38

14/22

The procedure: 1. Build ‘candidate’ λ : ST → TS

S always given by signature Σ and equations E

slide-39
SLIDE 39

14/22

The procedure: 1. Build ‘candidate’ λ : ST → TS

S always given by signature Σ and equations E Use monoidal ‘tensor’

⊗−,− : T(−) × T(−) → T(− × −)

slide-40
SLIDE 40

14/22

The procedure: 1. Build ‘candidate’ λ : ST → TS

S always given by signature Σ and equations E Use monoidal ‘tensor’

⊗−,− : T(−) × T(−) → T(− × −)

Define a lifting T of T on Σ-algebras

(A, σ : Aar(σ) → A)σ∈Σ → (TA, Tσ ◦ ⊗ar(σ) : (TA)ar(σ) → TA)σ∈Σ

slide-41
SLIDE 41

14/22

The procedure: 1. Build ‘candidate’ λ : ST → TS

S always given by signature Σ and equations E Use monoidal ‘tensor’

⊗−,− : T(−) × T(−) → T(− × −)

Define a lifting T of T on Σ-algebras

(A, σ : Aar(σ) → A)σ∈Σ → (TA, Tσ ◦ ⊗ar(σ) : (TA)ar(σ) → TA)σ∈Σ ˆ ; : (P(At)∗)2 → P(At∗), (U, V) → {u; v | u ∈ U, v ∈ V}, skip = {ǫ}

slide-42
SLIDE 42

14/22

The procedure: 1. Build ‘candidate’ λ : ST → TS

S always given by signature Σ and equations E Use monoidal ‘tensor’

⊗−,− : T(−) × T(−) → T(− × −)

Define a lifting T of T on Σ-algebras

(A, σ : Aar(σ) → A)σ∈Σ → (TA, Tσ ◦ ⊗ar(σ) : (TA)ar(σ) → TA)σ∈Σ ˆ ; : (P(At)∗)2 → P(At∗), (U, V) → {u; v | u ∈ U, v ∈ V}, skip = {ǫ} Theorem

If T sends (Σ, E)-algebras to (Σ, E)-algebras, then it defines a distributive law λ : ST → TS.

slide-43
SLIDE 43

15/22

The procedure: 2. Check if T : Alg(Σ, E) → Alg(Σ, E)

slide-44
SLIDE 44

15/22

The procedure: 2. Check if T : Alg(Σ, E) → Alg(Σ, E)

Illustration with idempotency, (A, • : A2 → A) |

= x • x = x

slide-45
SLIDE 45

15/22

The procedure: 2. Check if T : Alg(Σ, E) → Alg(Σ, E)

Illustration with idempotency, (A, • : A2 → A) |

= x • x = x

A

  • IdA
  • A2
  • A
slide-46
SLIDE 46

15/22

The procedure: 2. Check if T : Alg(Σ, E) → Alg(Σ, E)

Illustration with idempotency, (A, • : A2 → A) |

= x • x = x

TA

T∆ IdA

  • T(A2)

T•

  • TA
slide-47
SLIDE 47

15/22

The procedure: 2. Check if T : Alg(Σ, E) → Alg(Σ, E)

Illustration with idempotency, (A, • : A2 → A) |

= x • x = x (TA)2

⊗A,A

  • TA

T∆ IdA

  • T(A2)

T•

  • TA
slide-48
SLIDE 48

15/22

The procedure: 2. Check if T : Alg(Σ, E) → Alg(Σ, E)

Illustration with idempotency, (A, • : A2 → A) |

= x • x = x (TA)2

⊗A,A

  • TA

T∆ IdA

  • T(A2)

T•

  • TA

We call the upper triangle the residual diagram of x • x = x.

slide-49
SLIDE 49

15/22

The procedure: 2. Check if T : Alg(Σ, E) → Alg(Σ, E)

Illustration with idempotency, (A, • : A2 → A) |

= x • x = x (TA)2

⊗A,A

  • TA

T∆ IdA

  • T(A2)

T•

  • TA

We call the upper triangle the residual diagram of x • x = x. If it commutes then (TA, ˆ

  • : (TA)2 → TA) |

= xˆ

  • x = x.
slide-50
SLIDE 50

15/22

The procedure: 2. Check if T : Alg(Σ, E) → Alg(Σ, E)

Illustration with idempotency, (A, • : A2 → A) |

= x • x = x (TA)2

⊗A,A

  • TA

T∆ IdA

  • T(A2)

T•

  • TA

We call the upper triangle the residual diagram of x • x = x. If it commutes then (TA, ˆ

  • : (TA)2 → TA) |

= xˆ

  • x = x.

If it doesn’t, we know exactly where the obstacle is and can troubleshoot accordingly.

slide-51
SLIDE 51

16/22

Application: combining (−)∗, P and D

P sends monoids to monoids ⇒ P(−)∗ is a monad: the free

idempotent semiring monad.

slide-52
SLIDE 52

16/22

Application: combining (−)∗, P and D

P sends monoids to monoids ⇒ P(−)∗ is a monad: the free

idempotent semiring monad. Distributivity and absorption law are enforced by design!

slide-53
SLIDE 53

16/22

Application: combining (−)∗, P and D

P sends monoids to monoids ⇒ P(−)∗ is a monad: the free

idempotent semiring monad. Distributivity and absorption law are enforced by design!

D with ⊗ given by the product measure makes the residual diagrams

for associativity, units and commutativity commute...

slide-54
SLIDE 54

16/22

Application: combining (−)∗, P and D

P sends monoids to monoids ⇒ P(−)∗ is a monad: the free

idempotent semiring monad. Distributivity and absorption law are enforced by design!

D with ⊗ given by the product measure makes the residual diagrams

for associativity, units and commutativity commute... but neither for IDEMPOTENCY, nor for DISTRIBUTIVITY!

slide-55
SLIDE 55

16/22

Application: combining (−)∗, P and D

P sends monoids to monoids ⇒ P(−)∗ is a monad: the free

idempotent semiring monad. Distributivity and absorption law are enforced by design!

D with ⊗ given by the product measure makes the residual diagrams

for associativity, units and commutativity commute... but neither for IDEMPOTENCY, nor for DISTRIBUTIVITY! Troubleshooting: remove those axioms

slide-56
SLIDE 56

16/22

Application: combining (−)∗, P and D

P sends monoids to monoids ⇒ P(−)∗ is a monad: the free

idempotent semiring monad. Distributivity and absorption law are enforced by design!

D with ⊗ given by the product measure makes the residual diagrams

for associativity, units and commutativity commute... but neither for IDEMPOTENCY, nor for DISTRIBUTIVITY! Troubleshooting: remove those axioms There IS a distributive law over D of the monad defined by

p; skip = skip; p = p, (p; q); r = p(q; r), p + abort = abort + p = p, p + q = q + p, (p + q) + r = p + (q + r), p; abort = abort; p = abort

slide-57
SLIDE 57

16/22

Application: combining (−)∗, P and D

P sends monoids to monoids ⇒ P(−)∗ is a monad: the free

idempotent semiring monad. Distributivity and absorption law are enforced by design!

D with ⊗ given by the product measure makes the residual diagrams

for associativity, units and commutativity commute... but neither for IDEMPOTENCY, nor for DISTRIBUTIVITY! Troubleshooting: remove those axioms There IS a distributive law over D of the monad defined by

p; skip = skip; p = p, (p; q); r = p(q; r), p + abort = abort + p = p, p + q = q + p, (p + q) + r = p + (q + r), p; abort = abort; p = abort

Completely consistent with the semantics of ProbNetKAT

slide-58
SLIDE 58

17/22

Theorem

Let T be a commutative, relevant and affine monad. For all u and v, T preserves u = v.

slide-59
SLIDE 59

18/22

Fixing composition – Method 1: changing the inner layer

Idea: remove the faulty laws from the inner layer.

slide-60
SLIDE 60

18/22

Fixing composition – Method 1: changing the inner layer

Idea: remove the faulty laws from the inner layer. EM(S) ≃ Alg(ΣS, ES), EM(T) ≃ Alg(ΣT, ET). Let E ′

S be the subset of ES

containing the equations preserved by T. Obtain S ′ from Alg(ΣS, E ′

S)

Compose T with S ′, obtain a (Σ, E) algebra, where:

Σ = (ΣT ∪ ΣS)

E = (ET ∪ E ′

S ∪ distributivity of ΣS over ΣT)

slide-61
SLIDE 61

19/22

Method 1: fix the inner layer

Example D does not preserve idempotency nor distributivity. Drop them and obtain

a (Σ, E) algebra where Σ = {; , 1, +, 0, ⊕λ} and E = associativity, commutativity, unit laws for + equations of (−)∗ absorption p; 0 = 0; p = 0 equations of D (convex algebras) p ; (q ⊕λ r) = (p ; q) ⊕λ (p ; r)

(q ⊕λ r) ; p = (q ; p) ⊕λ (r ; p)

p + (q ⊕λ r) = (p + q) ⊕λ (p + r)

(q ⊕λ r) + p = (q + p) ⊕λ (r + p)

slide-62
SLIDE 62

20/22

Method 2: Change the outer layer

Idea: consider the largest submonad preserving the faulty equations.

slide-63
SLIDE 63

20/22

Method 2: Change the outer layer

Idea: consider the largest submonad preserving the faulty equations.

Example PD is not a monad as P does not preserve idempotency. The largest

submonad of P preserving it is the convex powerset Pc

slide-64
SLIDE 64

20/22

Method 2: Change the outer layer

Idea: consider the largest submonad preserving the faulty equations.

Example PD is not a monad as P does not preserve idempotency. The largest

submonad of P preserving it is the convex powerset Pc Two options to fix PD:

1 Build a monad PD that preserves the relevant equations. 2 Replace P by Pc and then composition works: PcD.

slide-65
SLIDE 65

21/22

Conclusions

A principled approach to constructing (equational) languages layer by layer. Conditions on existence of distributive laws and potential fixing strategies. Note: other troubleshooting strategies are possible!

slide-66
SLIDE 66

22/22

Thanks

Thank you! Questions?