New approaches in 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 Basic Concepts:

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 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 , idA : 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 C1 → C2 in C to morphism FC1 → FC2 in D.

Viliam Slodičák, Pavol MackoNew approaches in functional programming using algebras and coalgebras 3/27 Recursion versus

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 Coalgebras and algebras

Coalgebras and algebras For the category C and polynomial endofunctors F,G : Set → Set: G-coalgebra is a pair (U, ϕ) and

ϕ = hdestr1, . . . , destrni = U → G(U)

is coalgebraic structure providing observable properties of a program system F -algebra is a pair (A, α) and

α = [cons1, . . . , consn] = 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 Category of Algebras

Algebra homomorphism

F (f) F (A) - F (B)

f ∗ :(A, α) → (B, β) α β ? ? A - B f

Category of algebras Alg(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

Initial algebra is the initial object of the category Alg(F )

(µF, inF )

algebra operation inF is defined:

inF : F (µF ) → F ;

The morphism from initial algebra into any algebra we call : for α : F (A) → A is

cata α : µF → A

Viliam Slodičák, Pavol MackoNew approaches in functional programming using algebras and coalgebras 7/27 Initial algebra

(µF, inF ) It holds for initial algebra:

inF F µF - µF

F (cata α) cata α ? ? FA - A α

inF ◦ cata α = F (cata α) ◦ α

Viliam Slodičák, Pavol MackoNew approaches in functional programming using algebras and coalgebras 8/27 Category of coalgebras

Coalgebra homomorphism

f U - V

ϕ ψ f∗ :(U, ϕ) → (V, ψ) ? ? F (U) - F (V ) F (f)

Category of coalgebras Coalg(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 Final coalgebra

Final coalgebra is the final object of the category Coalg(F )

(νF, outF )

coalgebra dynamics out is defined:

outF : 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 Final coalgebra

(νF, outF ) It holds for final coalgebra:

ϕ U - FU

ana ϕ F (ana ϕ) ? ? νF - F νF outF

ana ϕ ◦ outF = ϕ ◦ F f

Viliam Slodičák, Pavol MackoNew approaches in functional programming using algebras and coalgebras 11/27 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

F f f ? ? FA - A α It holds that f = ϕ ◦ F f ◦ α.

Viliam Slodičák, Pavol MackoNew approaches in functional programming using algebras and coalgebras 12/27 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

F f hylo(ϕ, α)F ? ? FA - A α It holds that hylo(ϕ, α)F = ϕ ◦ F f ◦ α. 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 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 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 + (I∗ × I) - 1 + (S × I)

∼ inF = [new, push] = } α = [new, π1] ? ? I∗ - S fill = cata α

Viliam Slodičák, Pavol MackoNew approaches in functional programming using algebras and coalgebras 15/27 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) = 0 0 κ2(s , i) if s = push(s , i)

Viliam Slodičák, Pavol MackoNew approaches in functional programming using algebras and coalgebras 16/27 Coalgebraic definition of constructors

new 1 - I∗

∼ κ1 } = next ? ? 1 + (1 × I) - 1 + (I∗ × I) id + (new × id)

ana ϕ = push I∗ × I - I∗

∼ ϕ = κ2 } = next = outF ? ? 1 + ((I∗ × I) × I) - 1 + (I∗ × I) id + (push × id)

Viliam Slodičák, Pavol MackoNew approaches in functional programming using algebras and coalgebras 17/27 Combining the algebra and coalgebra

[new, π1] 1 + (S × I) - S 6 6 id + (fill × id) fill

[new, push] 1 + (I∗ × I) - I∗ 6 id + (push × id) next ? 1 + ((I∗ × I) × I) - 1 + (I∗ × I) id + (push × id)

Viliam Slodičák, Pavol MackoNew approaches in functional programming using algebras and coalgebras 18/27 Recursive coalgebra for Stack

Recursive coalgebra for Stack

next I∗ - 1 + (I∗ × I)

fill } F (fill) ? ? S  1 + (S × I) [new, π1] 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 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 Implementation of the anamorphism

Anamorphism it represents the corecursive function

int → intList let 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 Implementation of the catamorphism

Catamorphism it represents the recursive function

intList → int let rec cata list = match list with | [] −> 1 | head : : t a i l −> head ∗ (cata tail);;

Viliam Slodičák, Pavol MackoNew approaches in functional programming using algebras and coalgebras 22/27 Implementation of the catamorphism Hylomorphism defined as the composition of anamorphism and catamorphism; it represents the function that corecursively generates the list and then it recursively treat with it int → int; the function ana generates the list of natural numbers from n to 1; the function cata eliminates the generated list of natural numbers; l e t f a c t x = cata (ana x);;

Execution of the function fact

# f a c t 4 ; ; − : i n t = 24

Viliam Slodičák, Pavol MackoNew approaches in functional programming using algebras and coalgebras 23/27 Implementation of the hylomorphism

Factorial by hylomorphism execution fact 4 = cata (ana 4) = 4 cata (ana 3) = 12 cata (ana 2) = 24 cata (ana 1) = 24 id = 24

Viliam Slodičák, Pavol MackoNew approaches in functional programming using algebras and coalgebras 24/27 Thank You for Your attention

Viliam Slodičák, Pavol MackoNew approaches in functional programming using algebras and coalgebras 25/27 Conferrence INFORMATICS’2011

Rožňava, 16th-18th Nov 2011

Topics

1 Computer Architectures 2 Computer Networks 3 Theoretical Informatics 4 Programming Paradigms, Programming Languages 5 Software Engineering 6 Distributed Systems 7 Computer Graphics and Virtual Reality 8 Artificial Intelligence 9 Knowledge Management 10 Information System Research 11 Applied Informatics and Simulation

Viliam Slodičák, Pavol MackoNew approaches in functional programming using algebras and coalgebras 26/27 Conferrence INFORMATICS’2011

Rožňava, 16th-18th Nov 2011

Topics Abstract submission: April 30th 2011 Abstract acceptance: May 15th 2011 Full paper submission: September 2th 2011 Notification of acceptance: September 30th 2011 Registration, payment, conference program: October 10th 2011 Conference: November 16th-18th 2011

Abstracts should be sent electronically to the address: [email protected]. Abstracts should be no more than 15 lines with name, affiliation of authors and topic.

Viliam Slodičák, Pavol MackoNew approaches in functional programming using algebras and coalgebras 27/27