Relating Idioms, Arrows and Monads from Monoidal Adjunctions @ SYCO - - PowerPoint PPT Presentation

relating idioms arrows and monads from monoidal
SMART_READER_LITE
LIVE PREVIEW

Relating Idioms, Arrows and Monads from Monoidal Adjunctions @ SYCO - - PowerPoint PPT Presentation

Relating Idioms, Arrows and Monads from Monoidal Adjunctions @ SYCO I Exequiel Rivas September 2018 Team r 2 , Inria 1 Semantics of effectful programming languages The basic idea behind the semantics of programs described below is that a


slide-1
SLIDE 1

Relating Idioms, Arrows and Monads from Monoidal Adjunctions @ SYCO I

Exequiel Rivas September 2018

Team πr2, Inria 1

slide-2
SLIDE 2

Semantics of effectful programming languages

The basic idea behind the semantics of programs described below is that a program denotes a morphism from A to TB.

  • E. Moggi 1989

2

slide-3
SLIDE 3

Semantics of effectful programming languages

The basic idea behind the semantics of programs described below is that a program denotes a morphism from A to TB.

  • E. Moggi 1989

Moggi used monads for an unified treatment of effects. Id

η

T

T ◦ T

µ

  • His usages follows:

◮ η lifts values to effectful computations, i.e. return. ◮ µ composes two effects sequentially, i.e. ;. 2

slide-4
SLIDE 4

Wadler: monads as an interface

Monads can be internalised as an interface. class Functor m ⇒ Monad m where return :: a → m a (> > =) :: m a → (a → m b) → m b The state monad State comes with operations get :: State Int , put :: Int → State () Computaions written using these operations and the interface. get > > = λi → if i ≡ 0 then return False else put 1 > > = \ → return True

3

slide-5
SLIDE 5

Arrows and applicative functors

Monads (as interfaces) has been generalised...

4

slide-6
SLIDE 6

Arrows and applicative functors

Monads (as interfaces) has been generalised... Providing more control over the computations. class Functor f ⇒ Idiom f where pure :: a → f a (⊛) :: f (a → b) → f a → f b

4

slide-7
SLIDE 7

Arrows and applicative functors

Monads (as interfaces) has been generalised... Providing more control over the computations. class Functor f ⇒ Idiom f where pure :: a → f a (⊛) :: f (a → b) → f a → f b class Arrow () where arr :: (x → y) → x y (≫) :: (x y) → (y z) → x z first :: (x y) → (x, z) (y, z)

4

slide-8
SLIDE 8

Idioms are oblivious, arrows are meticulous, monads are . . .

Lindley, Wadler and Yallop (2008), proved the equivalences Idiom = Arrow + (x y ∼ = 1 (x → y)), Monad = Arrow + (x y ∼ = x → (1 y))

5

slide-9
SLIDE 9

Idioms are oblivious, arrows are meticulous, monads are . . .

Lindley, Wadler and Yallop (2008), proved the equivalences Idiom = Arrow + (x y ∼ = 1 (x → y)), Monad = Arrow + (x y ∼ = x → (1 y)) Following a syntactic approach: calculi and translations.

5

slide-10
SLIDE 10

Idioms are oblivious, arrows are meticulous, monads are . . .

Lindley, Wadler and Yallop (2008), proved the equivalences Idiom = Arrow + (x y ∼ = 1 (x → y)), Monad = Arrow + (x y ∼ = x → (1 y)) Following a syntactic approach: calculi and translations. We aim for a semantic explanation, modelling:

◮ Arrows as profunctors Fop × F → S with monoid structure. ◮ Monads and idioms as functors F → S with monoid structure. 5

slide-11
SLIDE 11

Notions of computations as monoids

Monads, idioms and arrows have

◮ an operation embedding pure values: return, pure and arr. ◮ an operation sequencing computations: (>

> =), (⊛) and (≫).

6

slide-12
SLIDE 12

Notions of computations as monoids

Monads, idioms and arrows have

◮ an operation embedding pure values: return, pure and arr. ◮ an operation sequencing computations: (>

> =), (⊛) and (≫). Resemble monoids.

6

slide-13
SLIDE 13

Notions of computations as monoids

Monads, idioms and arrows have

◮ an operation embedding pure values: return, pure and arr. ◮ an operation sequencing computations: (>

> =), (⊛) and (≫). Resemble monoids. We model computational effects using monoidal categories. Monad ⇛ Monoid in ([F, S] , ◦) Idiom ⇛ Monoid in ([F, S] , ⋆) Arrow ⇛ Monoid in ([Fop× F, S]s, ⊗)

6

slide-14
SLIDE 14

Monoidal structures: ◦

The category of finitary endofunctors [F, S] has a substitution monoidal structure. (F ◦ G)X = Y FY × (Y → GX) The inclusion i : F → S acts as unit.

7

slide-15
SLIDE 15

Monoidal structures: ◦

The category of finitary endofunctors [F, S] has a substitution monoidal structure. (F ◦ G)X = Y FY × (Y → GX) The inclusion i : F → S acts as unit. A monoid i

return

M

M ◦ M

(> > =)

  • in ([F, S] , ◦, i) is a monad.

7

slide-16
SLIDE 16

Monoidal structures: ⋆

The category [F, S] also has a convolution monoidal structure. (F ⋆ G)X = Y FY × G(Y → X) The inclusion i : F → S also acts as the unit.

8

slide-17
SLIDE 17

Monoidal structures: ⋆

The category [F, S] also has a convolution monoidal structure. (F ⋆ G)X = Y FY × G(Y → X) The inclusion i : F → S also acts as the unit. A monoid i

pure

F

F ⋆ F

(⊛)

  • in ([F, S] , ⋆, i) is an idiom.

8

slide-18
SLIDE 18

Intermezzo: strong profunctors

Profunctors compatible with the underlying cartesian structure.

9

slide-19
SLIDE 19

Intermezzo: strong profunctors

Profunctors compatible with the underlying cartesian structure. Definition: strong profunctor A profunctor P : Fop × F → S is strong if it comes equipped with a family of morphisms strX,Y ,Z : P(X, Y ) → P(X × Z, Y × Z) natural in X, Y and dinatural in Z such that the following equations hold: P(id, π1) ◦ strX,Y ,1 = P(π1, id), strX,Y ,W ◦ strX,Y ,V = P(α−1, α) ◦ strX,Y ,V ×W

9

slide-20
SLIDE 20

Monoidal structures: ⊗

Strong profunctors Fop × F → S have composition of profunctors. (P ⊗ Q)(X, Y ) = W P(X, W ) × Q(W , Y ) The hom-set HomF : Fop × F → S as the unit.

10

slide-21
SLIDE 21

Monoidal structures: ⊗

Strong profunctors Fop × F → S have composition of profunctors. (P ⊗ Q)(X, Y ) = W P(X, W ) × Q(W , Y ) The hom-set HomF : Fop × F → S as the unit. A monoid HomF

arr

A

A ⊗ A

(≫)

  • in ([Fop × F, S]s , ⊗, HomF) is an arrow.

10

slide-22
SLIDE 22

The equations II

Idiom = Arrow + (x y ∼ = 1 (x → y)), Monad = Arrow + (x y ∼ = x → (1 y)) We have defined Idiom, Monad and Arrow in our model: Monad ⇛ Monoid in ([F, S] , ◦) Idiom ⇛ Monoid in ([F, S] , ⋆) Arrow ⇛ Monoid in ([Fop× F, S]s, ⊗)

11

slide-23
SLIDE 23

The equations II

Idiom = Arrow + (x y ∼ = 1 (x → y)), Monad = Arrow + (x y ∼ = x → (1 y)) We have defined Idiom, Monad and Arrow in our model: Monad ⇛ Monoid in ([F, S] , ◦) Idiom ⇛ Monoid in ([F, S] , ⋆) Arrow ⇛ Monoid in ([Fop× F, S]s, ⊗) Isomorphisms on the right still missing.

11

slide-24
SLIDE 24

Formalising the isomorphisms

As a first step, we model the isomorphisms for profunctors. If A is the strong profunctor underlying the arrow () x y ∼ = 1 (x → y) ⇛ A(x, y) ∼ = A(1, x → y), x y ∼ = x → (1 y) ⇛ A(x, y) ∼ = ix → A(1, y).

12

slide-25
SLIDE 25

Formalising the isomorphisms

As a first step, we model the isomorphisms for profunctors. If A is the strong profunctor underlying the arrow () x y ∼ = 1 (x → y) ⇛ A(x, y) ∼ = A(1, x → y), x y ∼ = x → (1 y) ⇛ A(x, y) ∼ = ix → A(1, y). We try to factorise A(1, x → y) and ix → A(1, y) as functors applied to A on x and y.

12

slide-26
SLIDE 26

Fixing one parameter

A strong profunctor in [Fop× F, S]s can be mapped to a functor F → S by evaluating its first parameter.

13

slide-27
SLIDE 27

Fixing one parameter

A strong profunctor in [Fop× F, S]s can be mapped to a functor F → S by evaluating its first parameter. In particular, evaluating with 1, we obtain

: [Fop× F, S]s − → [F, S] A∗ = Z → A(1, Z) τ ∗

Z

= τ1,Z

13

slide-28
SLIDE 28

From functors to strong profunctors

The functor -∗ has left and right adjoints:

14

slide-29
SLIDE 29

From functors to strong profunctors

The functor -∗ has left and right adjoints:

  • !

: [F, S] − → [Fop× F, S]s F! = (X, Y ) → F(X → Y )

14

slide-30
SLIDE 30

From functors to strong profunctors

The functor -∗ has left and right adjoints:

  • !

: [F, S] − → [Fop× F, S]s F! = (X, Y ) → F(X → Y )

: [F, S] − → [Fop× F, S]s F∗ = (X, Y ) → i X → F Y

14

slide-31
SLIDE 31

From functors to strong profunctors

The functor -∗ has left and right adjoints:

  • !

: [F, S] − → [Fop× F, S]s F! = (X, Y ) → F(X → Y )

: [F, S] − → [Fop× F, S]s F∗ = (X, Y ) → i X → F Y We end up with an adjoint triple

  • !

14

slide-32
SLIDE 32

The picture

We obtain the diagram [F, S]

  • !

  • [Fop× F, S]s
  • 15
slide-33
SLIDE 33

The picture

We obtain the diagram [F, S]

  • !

  • [Fop× F, S]s
  • and the isomorphisms become

A(x, y) ∼ = A(1, x → y) ⇛ A ∼ = (A∗)! A(x, y) ∼ = ix → A(1, y) ⇛ A ∼ = (A∗)∗

15

slide-34
SLIDE 34

What about the monoidal structures?

Idiom = Arrow + (x y ∼ = 1 (x → y)), Monad = Arrow + (x y ∼ = x → (1 y)).

16

slide-35
SLIDE 35

What about the monoidal structures?

Idiom = Arrow + (x y ∼ = 1 (x → y)), Monad = Arrow + (x y ∼ = x → (1 y)). On the isomorphisms we only dealt with the objects.

16

slide-36
SLIDE 36

What about the monoidal structures?

Idiom = Arrow + (x y ∼ = 1 (x → y)), Monad = Arrow + (x y ∼ = x → (1 y)). On the isomorphisms we only dealt with the objects. Theorem For an adjoint triple F ⊣ G ⊣ H, we have that the comonad FG and the monad HG are adjoint FG ⊣ HG.

16

slide-37
SLIDE 37

What about the monoidal structures?

Idiom = Arrow + (x y ∼ = 1 (x → y)), Monad = Arrow + (x y ∼ = x → (1 y)). On the isomorphisms we only dealt with the objects. Theorem For an adjoint triple F ⊣ G ⊣ H, we have that the comonad FG and the monad HG are adjoint FG ⊣ HG. From the adjoint triple

  • !

we obtain (-∗)! = ⊣ ♦ = (-∗)∗

16

slide-38
SLIDE 38

Idempotent monads and monoids

In our case, the comonad and the monad ♦ are idempotent.

17

slide-39
SLIDE 39

Idempotent monads and monoids

In our case, the comonad and the monad ♦ are idempotent. Definition: T-monoid If T : C → C is an idempotent (co)monad, then a T-monoid is quadruple (M, m, e, α) where

◮ (M, m : M ⊗ M → M, e : I → M) is a monoid; ◮ (M, α : TM → M) is a T-algebra.

T-monoids form a category Mon (T).

17

slide-40
SLIDE 40

Idempotent monads and monoids

In our case, the comonad and the monad ♦ are idempotent. Definition: T-monoid If T : C → C is an idempotent (co)monad, then a T-monoid is quadruple (M, m, e, α) where

◮ (M, m : M ⊗ M → M, e : I → M) is a monoid; ◮ (M, α : TM → M) is a T-algebra.

T-monoids form a category Mon (T). For idempotent (co)monads, (co)algebras are isos. A T-monoid (M, m, e, α) is a Monoid on C + (M ∼ = TM)

17

slide-41
SLIDE 41

The equivalences

Idiom = Arrow + (x y ∼ = 1 (x → y))

Mon ([F, S]) and Mon () are equivalent categories.

18

slide-42
SLIDE 42

The equivalences

Idiom = Arrow + (x y ∼ = 1 (x → y))

Mon ([F, S]) and Mon () are equivalent categories. Monad = Arrow + (x y ∼ = x → (1 y))

Mon ([F, S]) and Mon (♦) are equivalent categories.

18

slide-43
SLIDE 43

Proof sketch I

To prove Mon ([F, S]) and Mon () are equivalent categories

19

slide-44
SLIDE 44

Proof sketch I

To prove Mon ([F, S]) and Mon () are equivalent categories note that both functors are monoidal (monoidal adjunction) ([F, S] , ⋆)

  • !
  • ([Fop× F, S]s, ⊗)
  • 19
slide-45
SLIDE 45

Proof sketch I

To prove Mon ([F, S]) and Mon () are equivalent categories note that both functors are monoidal (monoidal adjunction) ([F, S] , ⋆)

  • !
  • ([Fop× F, S]s, ⊗)
  • Functors lift to categories of monoids.

19

slide-46
SLIDE 46

Proof sketch II

In the case Mon ([F, S]) and Mon (♦) are equivalent categories

20

slide-47
SLIDE 47

Proof sketch II

In the case Mon ([F, S]) and Mon (♦) are equivalent categories the adjunction ([Fop× F, S]s, ⊗)

  • ([F, S] , ◦)
  • is a monoidal conjunction. No guarantees that -∗ preserves

monoids.

20

slide-48
SLIDE 48

Proof sketch II

In the case Mon ([F, S]) and Mon (♦) are equivalent categories the adjunction ([Fop× F, S]s, ⊗)

  • ([F, S] , ◦)
  • is a monoidal conjunction. No guarantees that -∗ preserves

monoids. A result by Porst and Street gives conditions when an opmonoidal functor preserves monoids.

20

slide-49
SLIDE 49

Conclusions

We have extended the notions of computation as monoids view to show a semantic counterpart to Lindley et al.’s result.

21

slide-50
SLIDE 50

Conclusions

We have extended the notions of computation as monoids view to show a semantic counterpart to Lindley et al.’s result. Further work includes

◮ replacing F and S. ◮ relating to relative monads and other solutions that do not

suffer of size issues.

◮ seeing how comonads and other notions fit in the picture. 21