relating idioms arrows and monads from monoidal
play

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


  1. Relating Idioms, Arrows and Monads from Monoidal Adjunctions @ SYCO I Exequiel Rivas September 2018 Team π r 2 , Inria 1

  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

  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. η µ � T Id T ◦ T His usages follows: ◮ η lifts values to effectful computations, i.e. return . ◮ µ composes two effects sequentially, i.e. ; . 2

  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. = λ i → if i ≡ 0 then return False get > > else put 1 > = \ → return True > 3

  5. Arrows and applicative functors Monads (as interfaces) has been generalised... 4

  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

  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

  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

  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

  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 F op × F → S with monoid structure. ◮ Monads and idioms as functors F → S with monoid structure. 5

  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

  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

  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 ([ F op × F , S ] s , ⊗ ) 6

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

  15. � Monoidal structures: ◦ The category of finitary endofunctors [ F , S ] has a substitution monoidal structure. � Y ( F ◦ G ) X = FY × ( Y → GX ) The inclusion i : F → S acts as unit. A monoid ( > =) > return � M M ◦ M i in ([ F , S ] , ◦ , i ) is a monad. 7

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

  17. � Monoidal structures: ⋆ The category [ F , S ] also has a convolution monoidal structure. � Y ( F ⋆ G ) X = FY × G ( Y → X ) The inclusion i : F → S also acts as the unit. A monoid ( ⊛ ) pure � F i F ⋆ F in ([ F , S ] , ⋆, i ) is an idiom. 8

  18. Intermezzo: strong profunctors Profunctors compatible with the underlying cartesian structure. 9

  19. Intermezzo: strong profunctors Profunctors compatible with the underlying cartesian structure. Definition: strong profunctor A profunctor P : F op × F → S is strong if it comes equipped with a family of morphisms str X , 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 ) ◦ str X , Y , 1 = P ( π 1 , id ) , str X , Y , W ◦ str X , Y , V = P ( α − 1 , α ) ◦ str X , Y , V × W 9

  20. Monoidal structures: ⊗ Strong profunctors F op × F → S have composition of profunctors. � W ( P ⊗ Q )( X , Y ) = P ( X , W ) × Q ( W , Y ) The hom-set Hom F : F op × F → S as the unit. 10

  21. � Monoidal structures: ⊗ Strong profunctors F op × F → S have composition of profunctors. � W ( P ⊗ Q )( X , Y ) = P ( X , W ) × Q ( W , Y ) The hom-set Hom F : F op × F → S as the unit. A monoid ( ≫ ) arr � A Hom F A ⊗ A in ([ F op × F , S ] s , ⊗ , Hom F ) is an arrow. 10

  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 ([ F op × F , S ] s , ⊗ ) 11

  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 ([ F op × F , S ] s , ⊗ ) Isomorphisms on the right still missing. 11

  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 ∼ A ( x , y ) ∼ = 1 � ( x → y ) ⇛ = A (1 , x → y ) , x � y ∼ A ( x , y ) ∼ = x → (1 � y ) = ix → A (1 , y ) . ⇛ 12

  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 ∼ A ( x , y ) ∼ = 1 � ( x → y ) ⇛ = A (1 , x → y ) , x � y ∼ A ( x , y ) ∼ = x → (1 � 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

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

  27. Fixing one parameter A strong profunctor in [ F op × F , S ] s can be mapped to a functor F → S by evaluating its first parameter. In particular, evaluating with 1, we obtain - ∗ : [ F op × F , S ] s − → [ F , S ] A ∗ = Z �→ A (1 , Z ) τ ∗ = τ 1 , Z Z 13

  28. From functors to strong profunctors The functor - ∗ has left and right adjoints: 14

  29. From functors to strong profunctors The functor - ∗ has left and right adjoints: - ! : [ F , S ] − → [ F op × F , S ] s F ! = ( X , Y ) �→ F ( X → Y ) 14

  30. From functors to strong profunctors The functor - ∗ has left and right adjoints: - ! : [ F , S ] − → [ F op × F , S ] s F ! = ( X , Y ) �→ F ( X → Y ) - ∗ : [ F , S ] − → [ F op × F , S ] s F ∗ = ( X , Y ) �→ i X → F Y 14

  31. From functors to strong profunctors The functor - ∗ has left and right adjoints: - ! : [ F , S ] − → [ F op × F , S ] s F ! = ( X , Y ) �→ F ( X → Y ) - ∗ : [ F , S ] − → [ F op × F , S ] s F ∗ = ( X , Y ) �→ i X → F Y We end up with an adjoint triple ∗ ⊣ ⊣ - ! - - ∗ 14

  32. � � � The picture We obtain the diagram - ! ⊥ [ F , S ] - ∗ [ F op × F , S ] s ⊥ - ∗ 15

  33. � � � The picture We obtain the diagram - ! ⊥ [ F , S ] - ∗ [ F op × F , S ] s ⊥ - ∗ and the isomorphisms become A ( x , y ) ∼ A ∼ = ( A ∗ ) ! = A (1 , x → y ) ⇛ A ( x , y ) ∼ A ∼ = ( A ∗ ) ∗ = ix → A (1 , y ) ⇛ 15

  34. What about the monoidal structures? Idiom = Arrow + ( x � y ∼ = 1 � ( x → y )) , Monad = Arrow + ( x � y ∼ = x → (1 � y )) . 16

  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

  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

Recommend


More recommend