new approaches in functional programming using algebras
play

New approaches in functional programming using algebras and - PowerPoint PPT Presentation

New approaches in functional programming using algebras and coalgebras Viliam Slodik, Pavol Macko Department of Computers and Informatics Faculty of Electrical Engineering and Informatics Technical University of Ko sice ETAPS - Workshop


  1. New approaches in functional programming using algebras and coalgebras Viliam Slodičák, Pavol Macko Department of Computers and Informatics Faculty of Electrical Engineering and Informatics Technical University of Koˇ sice ETAPS - Workshop on generative technologies Saarbrücken, 27.03.2011 Viliam Slodičák, Pavol MackoNew approaches in functional programming using algebras and coalgebras 1/27

  2. Basic Concepts: Category theory Program is defined as data structures and algorithms. In developing large scale programs we always have to apply several mathematical theories. The goal of programming is then to formulate a solution over these theories. Mathematical machines (i.e. computers) are able to make logical reasoning over representations of types. Categories are useful in computer science, where we often use more complex structures not expressible by sets. The relations between objects are expressed by morphisms. Viliam Slodičák, Pavol MackoNew approaches in functional programming using algebras and coalgebras 2/27

  3. Basic Concepts: Category theory Category Ob ( C ), objects of category C , e.g. A, B, . . . ; Morph ( C ), morphisms of category C , e.g. f : A → B ; identity morphism for each object of C , id A : A → A ; composition of morphisms: for f : A → B and g : B → C there is f ◦ g : A → C . Functor is a morphism between categories, F : C → D ; maps objects of C to objects of D ; maps morphism C 1 → C 2 in C to morphism FC 1 → FC 2 in D . Viliam Slodičák, Pavol MackoNew approaches in functional programming using algebras and coalgebras 3/27

  4. Recursion versus corecursion Recursion in computer programming is exemplified when a function is defined in terms of simpler, often smaller versions of itself. Recursion in computer programming is exemplified when a function is defined in terms of simpler, often smaller versions of itself. Dual notion to recursion is corecursion. Corecursion can produce both finite and infinite data structures as result, and may employ self-referential data structures. Viliam Slodičák, Pavol MackoNew approaches in functional programming using algebras and coalgebras 4/27

  5. Coalgebras and algebras Coalgebras and algebras For the category C and polynomial endofunctors F, G : S et → S et : G -coalgebra is a pair ( U, ϕ ) and ϕ = � destr 1 , . . . , destr n � = U → G ( U ) is coalgebraic structure providing observable properties of a program system F -algebra is a pair ( A, α ) and α = [ cons 1 , . . . , cons n ] = F ( A ) → A is algebraic structure describing internal structure of a program system. Viliam Slodičák, Pavol MackoNew approaches in functional programming using algebras and coalgebras 5/27

  6. Category of Algebras Algebra homomorphism F ( A ) F ( f ) ✲ F ( B ) f ∗ : ( A, α ) → ( B, β ) α β ❄ ❄ ✲ B A f Category of algebras A lg ( F ) objects - algebras: ( A, a ) , ( B, b ) , . . . ; morphisms - algebra homomorphisms: f ∗ : ( A, a ) → ( B, b ); identity - for each algebra: id ( A,a ) : ( A, a ) → ( A, a ); morphisms are composable. Viliam Slodičák, Pavol MackoNew approaches in functional programming using algebras and coalgebras 6/27

  7. Initial algebra Initial algebra is the initial object of the category A lg ( F ) ( µF, in F ) algebra operation in F is defined: in F : F ( µF ) → F ; The morphism from initial algebra into any algebra we call catamorphism: for α : F ( A ) → A is cata α : µF → A Viliam Slodičák, Pavol MackoNew approaches in functional programming using algebras and coalgebras 7/27

  8. Initial algebra ( µF, in F ) It holds for initial algebra: in F ✲ µF FµF F ( cata α ) cata α ❄ ❄ ✲ A FA α in F ◦ cata α = F ( cata α ) ◦ α Viliam Slodičák, Pavol MackoNew approaches in functional programming using algebras and coalgebras 8/27

  9. Category of coalgebras Coalgebra homomorphism f ✲ V U ϕ ψ f ∗ : ( U, ϕ ) → ( V, ψ ) ❄ ❄ ✲ F ( V ) F ( U ) F ( f ) Category of coalgebras C oalg ( F ) objects - coalgebras: ( U, ϕ ) , ( V, ψ ) , . . . ; morphisms - coalgebra homomorphisms: f ∗ : ( U, ϕ ) → ( V, ψ ); identity - for each coalgebra: id ( U,ϕ ) : ( U, ϕ ) → ( U, ϕ ); morphisms are composable. Viliam Slodičák, Pavol MackoNew approaches in functional programming using algebras and coalgebras 9/27

  10. Final coalgebra Final coalgebra is the final object of the category C oalg ( F ) ( νF, out F ) coalgebra dynamics out is defined: out F : F → F ( νF ); The morphism from any coalgebra into the final coalgebra we call anamorphism: for ϕ : U → F ( U ) is ana α : U → νF Viliam Slodičák, Pavol MackoNew approaches in functional programming using algebras and coalgebras 10/27

  11. Final coalgebra ( νF, out F ) It holds for final coalgebra: ϕ ✲ FU U ana ϕ F ( ana ϕ ) ❄ ❄ ✲ FνF νF out F ana ϕ ◦ out F = ϕ ◦ Ff Viliam Slodičák, Pavol MackoNew approaches in functional programming using algebras and coalgebras 11/27

  12. Recursive Coalgebra Definition A coalgebra ( U, ϕ ) is called recursive if for every algebra ( A, α ) there exists a unique coalgebra-to-algebra morphism f : U → A FU ✛ ϕ U Ff f ❄ ❄ ✲ A FA α It holds that f = ϕ ◦ Ff ◦ α. Viliam Slodičák, Pavol MackoNew approaches in functional programming using algebras and coalgebras 12/27

  13. Hylomorphism Definition A coalgebra ( U, ϕ ) is called recursive if for every algebra ( A, α ) there exists a unique coalgebra-to-algebra morphism f : U → A FU ✛ ϕ U hylo ( ϕ, α ) F Ff ❄ ❄ ✲ A FA α It holds that hylo ( ϕ, α ) F = ϕ ◦ Ff ◦ α. Moreover, the hylomorphism is a composition of anamorphism and catamorphism hylo ( ϕ, α ) F = ( cata α ) F ◦ ( ana ϕ ) F Viliam Slodičák, Pavol MackoNew approaches in functional programming using algebras and coalgebras 13/27

  14. Data Structure: Stack Stack ( σ ): Stack ( σ ) : new : → Stack ( σ ) push : Stack ( σ ) , σ → Stack ( σ ) top : Stack ( σ ) → σ is empty : Stack ( σ ) → bool pop : Stack ( σ ) → Stack ( σ ) Polynomial endofunctor F ( S ) = 1 + ( S × I ) The F -algebra ( S, a ), where a = [ new, push ] is defined by  new, if w = (1 , ( s, ε )) [ new, push ]( w ) = push ( s, i ) if w = (2 , ( s, i )) and w ∈ 1 + ( S × I ). Viliam Slodičák, Pavol MackoNew approaches in functional programming using algebras and coalgebras 14/27

  15. Constructor Operations on Stack Constructor operations on Stack: new : → S push : S × I → S where I is the representation of the type σ ; I ∗ , the Kleene closure over I contains the sequences of stack values; S is the representation of the type Stack ( σ ). The initial algebra ( I ∗ , [ new, push ]) id + ( fill × id ) ✲ 1 + ( S × I ) 1 + ( I ∗ × I ) in F = [ new, push ] ∼ α = [ new, π 1 ] = ⊚ ❄ ❄ ✲ S I ∗ fill = cata α Viliam Slodičák, Pavol MackoNew approaches in functional programming using algebras and coalgebras 15/27

  16. Coalgebraic definition of constructors Combinig the operations pop a top we construct the operation next : S → 1 + ( S × I ) where I is the representation of the type σ ; I ∗ , the Kleene closure over I contains the sequences of stack values; S is the state space. The final coalgebra ( I ∗ , next ) where next : I ∗ → 1 + ( I ∗ × I )  κ 1 ( ∗ ) if s is empty next ( s ) = κ 2 ( s ′ , i ) if s = push ( s ′ , i ) Viliam Slodičák, Pavol MackoNew approaches in functional programming using algebras and coalgebras 16/27

  17. Coalgebraic definition of constructors new ✲ I ∗ 1 ∼ κ 1 = next ⊚ ❄ ❄ ✲ 1 + ( I ∗ × I ) 1 + (1 × I ) id + ( new × id ) ana ϕ = push ✲ I ∗ I ∗ × I ∼ ϕ = κ 2 = next = out F ⊚ ❄ ❄ ✲ 1 + ( I ∗ × I ) 1 + (( I ∗ × I ) × I ) id + ( push × id ) Viliam Slodičák, Pavol MackoNew approaches in functional programming using algebras and coalgebras 17/27

  18. Combining the algebra and coalgebra [ new, π 1 ] ✲ S 1 + ( S × I ) ✻ ✻ id + ( fill × id ) fill [ new, push ] ✲ I ∗ 1 + ( I ∗ × I ) ✻ id + ( push × id ) next ❄ ✲ 1 + ( I ∗ × I ) 1 + (( I ∗ × I ) × I ) id + ( push × id ) Viliam Slodičák, Pavol MackoNew approaches in functional programming using algebras and coalgebras 18/27

  19. Recursive coalgebra for Stack Recursive coalgebra for Stack next ✲ 1 + ( I ∗ × I ) I ∗ fill F ( fill ) ⊚ ❄ ❄ ✛ [ new, π 1 ] 1 + ( S × I ) S It holds fill = next ◦ F ( fill ) ◦ [ new, π 1 ] where fill : I ∗ → S Viliam Slodičák, Pavol MackoNew approaches in functional programming using algebras and coalgebras 19/27

  20. Recursive coalgebra for Stack Recursive coalgebra for Stack The coalgebra-to-algebra morphism fill : I ∗ → S is defined:  s if card ( i ) = length ( s ) fill ( i ) = ⊥ otherwise for i ∈ I ∗ , s ∈ S . ( I ∗ , next ) → ( S, [ push, new ]) Viliam Slodičák, Pavol MackoNew approaches in functional programming using algebras and coalgebras 20/27

  21. Implementation of the anamorphism Anamorphism it represents the corecursive function int → intList l e t rec ana n = match n with | 0 − > [ ] | 1 − > [ 1 ] | n − > n : : ana (n − 1);; Viliam Slodičák, Pavol MackoNew approaches in functional programming using algebras and coalgebras 21/27

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend