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
Structuring Computations Discrete time signals Jacobs – Types’06, 18/4/’06 – p.13/52
Structuring Computations Discrete time signals Three basic comonads: Jacobs – Types’06, 18/4/’06 – p.13/52
� � � � 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
� � � � 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
Structuring Computations Continuous time signals Jacobs – Types’06, 18/4/’06 – p.14/52
Structuring Computations Continuous time signals Analogues fundamental diagram of comonads: Jacobs – Types’06, 18/4/’06 – p.14/52
� 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
� 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
III. Arrows Jacobs – Types’06, 18/4/’06 – p.15/52
Structuring Computations Arrow overview Jacobs – Types’06, 18/4/’06 – p.16/52
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
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
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
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
Structuring Computations Arrow in Haskell Jacobs – Types’06, 18/4/’06 – p.17/52
Structuring Computations Arrow in Haskell Introduced as type class: Jacobs – Types’06, 18/4/’06 – p.17/52
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
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
Structuring Computations Arrow examples Jacobs – Types’06, 18/4/’06 – p.18/52
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
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
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
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
Structuring Computations Arrows, categorically Jacobs – Types’06, 18/4/’06 – p.19/52
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
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
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
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
Structuring Computations Excurs: monoid in a category Jacobs – Types’06, 18/4/’06 – p.20/52
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
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
� � � 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
Structuring Computations Excurs: monads are monoids Jacobs – Types’06, 18/4/’06 – p.21/52
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
� 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
� 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
Structuring Computations Arrows are also monoids Jacobs – Types’06, 18/4/’06 – p.22/52
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
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
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
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
Structuring Computations Arrows, intuitively Jacobs – Types’06, 18/4/’06 – p.23/52
Structuring Computations Arrows, intuitively • Most fundamental mathematical structure in computing? Jacobs – Types’06, 18/4/’06 – p.23/52
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
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
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
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
IV. Monads Jacobs – Types’06, 18/4/’06 – p.24/52
Structuring Computations Monad overview Jacobs – Types’06, 18/4/’06 – p.25/52
Structuring Computations Monad overview • Introduced by Moggi (1991), popularised in functional programming by Wadler Jacobs – Types’06, 18/4/’06 – p.25/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
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
Structuring Computations Java monad Jacobs – Types’06, 18/4/’06 – p.26/52
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