structuring computations
play

Structuring Computations Structuring Computations Contents Jacobs - PowerPoint PPT Presentation

FACULTY OF SCIENCE Bart Jacobs Structuring Computations Structuring Computations Contents Jacobs Types06, 18/4/06 p.1/52 Structuring Computations Contents I. Sneak preview VII. Hoare logic for JML II. Comonads VIII. Conclusions


  1. Structuring Computations coKleisli category of computations • coKleisli maps X N × N − → Y form a category • Identity via coreturn; composition via delta/cobind • Gives output in Y for completely given input stream of X ’s • Basis for dataflow calculus by Uustalu & Vene (like in Lustre, Lucid) Jacobs – Types’06, 18/4/’06 – p.12/52

  2. Structuring Computations Discrete time signals Jacobs – Types’06, 18/4/’06 – p.13/52

  3. Structuring Computations Discrete time signals Three basic comonads: Jacobs – Types’06, 18/4/’06 – p.13/52

  4. � � � � Structuring Computations Discrete time signals Three basic comonads: causality anti-causality X ⋆ × X X N × N � X N no past no future � λm. α ( n + m ) ( � α (0) ,...,α ( n − 1) � ,α ( n )) ( α,n ) Jacobs – Types’06, 18/4/’06 – p.13/52

  5. � � � � Structuring Computations Discrete time signals Three basic comonads: causality anti-causality X ⋆ × X X N × N � X N no past no future � λm. α ( n + m ) ( � α (0) ,...,α ( n − 1) � ,α ( n )) ( α,n ) with “comonad homomorphisms” between them Jacobs – Types’06, 18/4/’06 – p.13/52

  6. Structuring Computations Continuous time signals Jacobs – Types’06, 18/4/’06 – p.14/52

  7. Structuring Computations Continuous time signals Analogues fundamental diagram of comonads: Jacobs – Types’06, 18/4/’06 – p.14/52

  8. � Structuring Computations Continuous time signals Analogues fundamental diagram of comonads: X [0 ,t ) × X � X [0 , ∞ ) × [0 , ∞ ) � X [0 , ∞ ) t ∈ [0 , ∞ ) Jacobs – Types’06, 18/4/’06 – p.14/52

  9. � Structuring Computations Continuous time signals Analogues fundamental diagram of comonads: X [0 ,t ) × X � X [0 , ∞ ) × [0 , ∞ ) � X [0 , ∞ ) t ∈ [0 , ∞ ) where: X [0 ,t ) × X ∼ X [0 ,t ] ∼ = X [0 , 1] × [0 , ∞ ) � � = t ∈ [0 , ∞ ) t ∈ [0 , ∞ ) Jacobs – Types’06, 18/4/’06 – p.14/52

  10. III. Arrows Jacobs – Types’06, 18/4/’06 – p.15/52

  11. Structuring Computations Arrow overview Jacobs – Types’06, 18/4/’06 – p.16/52

  12. Structuring Computations Arrow overview • Introduced in Haskell by Hughes in 2000, as common interface extending monads (parser as main example) Jacobs – Types’06, 18/4/’06 – p.16/52

  13. Structuring Computations Arrow overview • Introduced in Haskell by Hughes in 2000, as common interface extending monads (parser as main example) • Binary type operation A ( − , +) with three operations: arr, > > > , first. Jacobs – Types’06, 18/4/’06 – p.16/52

  14. Structuring Computations Arrow overview • Introduced in Haskell by Hughes in 2000, as common interface extending monads (parser as main example) • Binary type operation A ( − , +) with three operations: arr, > > > , first. • Folklore claim: Arrows are Freyd categories (Power & Robinson’99) Jacobs – Types’06, 18/4/’06 – p.16/52

  15. Structuring Computations Arrow overview • Introduced in Haskell by Hughes in 2000, as common interface extending monads (parser as main example) • Binary type operation A ( − , +) with three operations: arr, > > > , first. • Folklore claim: Arrows are Freyd categories (Power & Robinson’99) • Recently substantiated by first describing arrows as monoids in a category of bifunctors C op × C → Sets Jacobs – Types’06, 18/4/’06 – p.16/52

  16. Structuring Computations Arrow in Haskell Jacobs – Types’06, 18/4/’06 – p.17/52

  17. Structuring Computations Arrow in Haskell Introduced as type class: Jacobs – Types’06, 18/4/’06 – p.17/52

  18. Structuring Computations Arrow in Haskell Introduced as type class: class Arrow A where arr :: ( X → Y ) → A X Y ( > > ) :: A X Y → A Y Z → A X Z > first :: A X Y → A ( X, Z ) ( Y, Z ) Jacobs – Types’06, 18/4/’06 – p.17/52

  19. Structuring Computations Arrow in Haskell Introduced as type class: class Arrow A where arr :: ( X → Y ) → A X Y ( > > ) :: A X Y → A Y Z → A X Z > first :: A X Y → A ( X, Z ) ( Y, Z ) Which should satisfy 8 equations, such as: ( a > > b ) > > c = a > > ( b > > c ) > > > > > arr (1) = a a > > first ( arr ( f )) = arr ( f × 1) , etc Jacobs – Types’06, 18/4/’06 – p.17/52

  20. Structuring Computations Arrow examples Jacobs – Types’06, 18/4/’06 – p.18/52

  21. Structuring Computations Arrow examples • ( X, Y ) �− → ( X → T ( Y )) , for T monad ( X, Y ) �− → ( G ( X ) → Y ) , for G comonad Jacobs – Types’06, 18/4/’06 – p.18/52

  22. Structuring Computations Arrow examples • ( X, Y ) �− → ( X → T ( Y )) , for T monad ( X, Y ) �− → ( G ( X ) → Y ) , for G comonad → ( X × X → [0 , 1] ( Y × Y ) ) for • ( X, Y ) �− quantum computation Jacobs – Types’06, 18/4/’06 – p.18/52

  23. Structuring Computations Arrow examples • ( X, Y ) �− → ( X → T ( Y )) , for T monad ( X, Y ) �− → ( G ( X ) → Y ) , for G comonad → ( X × X → [0 , 1] ( Y × Y ) ) for • ( X, Y ) �− quantum computation → ( X N → P ( Y N )) for • ( X, Y ) �− “non-deterministic dataflow” Jacobs – Types’06, 18/4/’06 – p.18/52

  24. Structuring Computations Arrow examples • ( X, Y ) �− → ( X → T ( Y )) , for T monad ( X, Y ) �− → ( G ( X ) → Y ) , for G comonad → ( X × X → [0 , 1] ( Y × Y ) ) for • ( X, Y ) �− quantum computation → ( X N → P ( Y N )) for • ( X, Y ) �− “non-deterministic dataflow” → (2 × S ⋆ ) × • ( X, Y ) �− (( S ⋆ × X ) → (1 + ( S ⋆ × Y ))) for Swierstra-Duponcheel parser that motivated Hughes Jacobs – Types’06, 18/4/’06 – p.18/52

  25. Structuring Computations Arrows, categorically Jacobs – Types’06, 18/4/’06 – p.19/52

  26. Structuring Computations Arrows, categorically • A is functorial: for f : X ′ → X and g : Y → Y ′ , A ( f, g ) � A ( X ′ , Y ′ ) A ( X, Y ) � arr ( f ) > a � > arr ( g ) > > a > > Jacobs – Types’06, 18/4/’06 – p.19/52

  27. Structuring Computations Arrows, categorically • A is functorial: for f : X ′ → X and g : Y → Y ′ , A ( f, g ) � A ( X ′ , Y ′ ) A ( X, Y ) � arr ( f ) > a � > arr ( g ) > > a > > • arr : (+) ( − ) → A ( − , +) is natural transformation (natro, for short) Jacobs – Types’06, 18/4/’06 – p.19/52

  28. Structuring Computations Arrows, categorically • A is functorial: for f : X ′ → X and g : Y → Y ′ , A ( f, g ) � A ( X ′ , Y ′ ) A ( X, Y ) � arr ( f ) > a � > arr ( g ) > > a > > • arr : (+) ( − ) → A ( − , +) is natural transformation (natro, for short) • > > is natro A ⊗ A → A , for tensor product of > distributors / profunctors Jacobs – Types’06, 18/4/’06 – p.19/52

  29. Structuring Computations Arrows, categorically • A is functorial: for f : X ′ → X and g : Y → Y ′ , A ( f, g ) � A ( X ′ , Y ′ ) A ( X, Y ) � arr ( f ) > a � > arr ( g ) > > a > > • arr : (+) ( − ) → A ( − , +) is natural transformation (natro, for short) • > > is natro A ⊗ A → A , for tensor product of > distributors / profunctors • first corresponds to “internal strength” Jacobs – Types’06, 18/4/’06 – p.19/52

  30. Structuring Computations Excurs: monoid in a category Jacobs – Types’06, 18/4/’06 – p.20/52

  31. Structuring Computations Excurs: monoid in a category • Standardly, a monoid is a set M with associative m : M × M → M and two-sided unit e : 1 → M Jacobs – Types’06, 18/4/’06 – p.20/52

  32. Structuring Computations Excurs: monoid in a category • Standardly, a monoid is a set M with associative m : M × M → M and two-sided unit e : 1 → M • Can be formulated in category with finite products (1 , × ) : equations become diagrams Jacobs – Types’06, 18/4/’06 – p.20/52

  33. � � � Structuring Computations Excurs: monoid in a category • Standardly, a monoid is a set M with associative m : M × M → M and two-sided unit e : 1 → M • Can be formulated in category with finite products (1 , × ) : equations become diagrams • No projections/diagonals needed: also in monoidal category with ( I, ⊗ ) . Eg. ∼ ∼ 1 ⊗ e = � I ⊗ Me ⊗ 1 = � M ⊗ M M ⊗ M M ⊗ I M � ����������������������� � � � ����������������������� � � � � � � � � � � � m � � m � � � � � � � � � � � � � � � � � � � � � � � � � � � � � M � M Jacobs – Types’06, 18/4/’06 – p.20/52

  34. Structuring Computations Excurs: monads are monoids Jacobs – Types’06, 18/4/’06 – p.21/52

  35. Structuring Computations Excurs: monads are monoids • The functor category C C is monoidal: F ⊗ G = F ◦ G I = Id Jacobs – Types’06, 18/4/’06 – p.21/52

  36. � Structuring Computations Excurs: monads are monoids • The functor category C C is monoidal: F ⊗ G = F ◦ G I = Id • A monoid in C C is a functor M : C → C with natros: µ η � M M ⊗ M Id M ◦ M satisfying the monoid equations Jacobs – Types’06, 18/4/’06 – p.21/52

  37. � Structuring Computations Excurs: monads are monoids • The functor category C C is monoidal: F ⊗ G = F ◦ G I = Id • A monoid in C C is a functor M : C → C with natros: µ η � M M ⊗ M Id M ◦ M satisfying the monoid equations • A monoid in C C is precisely a monad! Jacobs – Types’06, 18/4/’06 – p.21/52

  38. Structuring Computations Arrows are also monoids Jacobs – Types’06, 18/4/’06 – p.22/52

  39. Structuring Computations Arrows are also monoids • Arrows are monoids in category of bifunctors C op × C → Sets Jacobs – Types’06, 18/4/’06 – p.22/52

  40. Structuring Computations Arrows are also monoids • Arrows are monoids in category of bifunctors C op × C → Sets • Tensor ⊗ more complicated, with exponentiation/hom as unit Jacobs – Types’06, 18/4/’06 – p.22/52

  41. Structuring Computations Arrows are also monoids • Arrows are monoids in category of bifunctors C op × C → Sets • Tensor ⊗ more complicated, with exponentiation/hom as unit • Allows for precise comparison with Freyd categories (bijective correspondence) Jacobs – Types’06, 18/4/’06 – p.22/52

  42. Structuring Computations Arrows are also monoids • Arrows are monoids in category of bifunctors C op × C → Sets • Tensor ⊗ more complicated, with exponentiation/hom as unit • Allows for precise comparison with Freyd categories (bijective correspondence) • Details in Heunen & Jacobs, MFPS’06. Jacobs – Types’06, 18/4/’06 – p.22/52

  43. Structuring Computations Arrows, intuitively Jacobs – Types’06, 18/4/’06 – p.23/52

  44. Structuring Computations Arrows, intuitively • Most fundamental mathematical structure in computing? Jacobs – Types’06, 18/4/’06 – p.23/52

  45. Structuring Computations Arrows, intuitively • Most fundamental mathematical structure in computing? • Monoid ( A, ; , skip ) of programs/actions A ∈ Sets with sequential composition Jacobs – Types’06, 18/4/’06 – p.23/52

  46. Structuring Computations Arrows, intuitively • Most fundamental mathematical structure in computing? • Monoid ( A, ; , skip ) of programs/actions A ∈ Sets with sequential composition • Adding input and output makes A ( − , +) binary operator Jacobs – Types’06, 18/4/’06 – p.23/52

  47. Structuring Computations Arrows, intuitively • Most fundamental mathematical structure in computing? • Monoid ( A, ; , skip ) of programs/actions A ∈ Sets with sequential composition • Adding input and output makes A ( − , +) binary operator • Hence carrier A becomes bifunctor C op × C → Sets Jacobs – Types’06, 18/4/’06 – p.23/52

  48. Structuring Computations Arrows, intuitively • Most fundamental mathematical structure in computing? • Monoid ( A, ; , skip ) of programs/actions A ∈ Sets with sequential composition • Adding input and output makes A ( − , +) binary operator • Hence carrier A becomes bifunctor C op × C → Sets • Keeping the monoid structure leads to Hughes’ Arrow Jacobs – Types’06, 18/4/’06 – p.23/52

  49. IV. Monads Jacobs – Types’06, 18/4/’06 – p.24/52

  50. Structuring Computations Monad overview Jacobs – Types’06, 18/4/’06 – p.25/52

  51. Structuring Computations Monad overview • Introduced by Moggi (1991), popularised in functional programming by Wadler Jacobs – Types’06, 18/4/’06 – p.25/52

  52. Structuring Computations Monad overview • Introduced by Moggi (1991), popularised in functional programming by Wadler • for structuring outputs / computational effects Jacobs – Types’06, 18/4/’06 – p.25/52

  53. Structuring Computations Monad overview • Introduced by Moggi (1991), popularised in functional programming by Wadler • for structuring outputs / computational effects • Standard examples: • lift / maybe 1 + ( − ) • exception E + ( − ) • list ( − ) ⋆ • state ( − × S ) S • non-determinism P (powerset) • probability D (distribution) Jacobs – Types’06, 18/4/’06 – p.25/52

  54. Structuring Computations Java monad Jacobs – Types’06, 18/4/’06 – p.26/52

  55. Structuring Computations Java monad • Definition [Jacobs & Poll’03]: J ( X ) = (1 + S × X + S × E ) S Jacobs – Types’06, 18/4/’06 – p.26/52

Recommend


More recommend