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: S 1 S 4 S 3 Topology: t =( sw = S 1 ; pt = 2 ; ( sw ← S 2 ; pt ← 1 ) ⊕ . 9 drop ) & ( sw = S 1 ; pt = 3 ; sw ← S 3 ; p ← 1 ) & ( sw = S 2 ; pt = 4 ; sw ← S 4 ; p ← 2 ) & ( sw = S 3 ; pt = 4 ; sw ← S 4 ; p ← 3 ) 2/22
Motivation: ProbNetKAT S 2 A simple network: S 1 S 4 S 3 Topology: t =( sw = S 1 ; pt = 2 ; ( sw ← S 2 ; pt ← 1 ) ⊕ . 9 drop ) & ( sw = S 1 ; pt = 3 ; sw ← S 3 ; p ← 1 ) & ( sw = S 2 ; pt = 4 ; sw ← S 4 ; p ← 2 ) & ( sw = S 3 ; pt = 4 ; sw ← S 4 ; p ← 3 ) Forwarding policy: p = ( sw = S 1 ; pt ← 2 ) & ( sw = S 2 ; pt ← 4 ) 2/22
Motivation: ProbNetKAT S 2 A simple network: S 1 S 4 S 3 Topology: t =( sw = S 1 ; pt = 2 ; ( sw ← S 2 ; pt ← 1 ) ⊕ . 9 drop ) & ( sw = S 1 ; pt = 3 ; sw ← S 3 ; p ← 1 ) & ( sw = S 2 ; pt = 4 ; sw ← S 4 ; p ← 2 ) & ( sw = S 3 ; pt = 4 ; sw ← S 4 ; p ← 3 ) Forwarding policy: p = ( sw = S 1 ; pt ← 2 ) & ( sw = S 2 ; pt ← 4 ) A packet reaches S 4 : ( t ; p ) ∗ ; ( sw = S 4 ) 2/22
Motivation: ProbNetKAT Syntactically, ProbNetKAT is a kind of Kleene algebra with probabilistic choice ⊕ λ 3/22
Motivation: ProbNetKAT Syntactically, ProbNetKAT is a kind of Kleene algebra with probabilistic choice ⊕ λ BUT! The denotation of the operator & is odd... 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. 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 & 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? 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? 3/22
Building languages layer by layer: shopping list 4/22
Building languages layer by layer: shopping list First layer: p ::= skip | p ; p | a ∈ At p ; skip = skip ; p = p , . . . Monad: (−) ∗ 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: (−) ∗ 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: (−) ∗ 4/22
Combining the layers: things can go wrong! 5/22
Combining the layers: things can go wrong! The composition of two monads is not necessarily a monad 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 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 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 (−) ∗ 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? 5/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. 6/22
Monads Monads: a categorical way to encode computational effects: 7/22
Monads Monads: a categorical way to encode computational effects: Non-determinism, probabilities, side-effects . . . 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. 7/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. 8/22
Examples P ( A ) = { B | B ⊆ A , B finite } Finite Powerset A ∗ = { w 1 . . . w n | n ∈ N , w i ∈ A } Free Monoid (List) D ( A ) = { f | f probability distribution on A , Distributions and Supp ( f ) finite } 9/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 µ ) 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 ) 10/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) 11/22
S , T monads, EM ( T ) ≃ Alg ( Σ T , E T ) , EM ( S ) ≃ Alg ( Σ S , E S ) Definition A distributive law of T over S is a natural transformation λ : ST → TS (verifying structural properties) 12/22
� � S , T monads, EM ( T ) ≃ Alg ( Σ T , E T ) , EM ( S ) ≃ Alg ( Σ S , E S ) 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 η T η S µ S S µ T S λ TX � X � TX � STX TTX � STTX X � STX X TX STSTX SSTTX u m Operations in Σ S distribute over those of Σ T We call S the inner layer , T the outer layer . 12/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? 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 . 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. 13/22
The procedure: 1. Build ‘candidate’ λ : ST → TS 14/22
The procedure: 1. Build ‘candidate’ λ : ST → TS S always given by signature Σ and equations E 14/22
The procedure: 1. Build ‘candidate’ λ : ST → TS S always given by signature Σ and equations E Use monoidal ‘tensor’ ⊗ − , − : T (−) × T (−) → T (− × −) 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 , σ : A ar ( σ ) → A ) σ ∈ Σ → ( TA , T σ ◦ ⊗ ar ( σ ) : ( TA ) ar ( σ ) → TA ) σ ∈ Σ 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 , σ : A ar ( σ ) → A ) σ ∈ Σ → ( TA , T σ ◦ ⊗ ar ( σ ) : ( TA ) ar ( σ ) → TA ) σ ∈ Σ ; : ( P ( At ) ∗ ) 2 → P ( At ∗ ) , ( U , V ) �→ { u ; v | u ∈ U , v ∈ V } , � ˆ skip = { ǫ } 14/22
Recommend
More recommend