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

new approaches in functional programming using algebras
SMART_READER_LITE
LIVE PREVIEW

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


slide-1
SLIDE 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

slide-2
SLIDE 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

  • ver 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

slide-3
SLIDE 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 , 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

slide-4
SLIDE 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

slide-5
SLIDE 5

Coalgebras and algebras

Coalgebras and algebras

For the category C and polynomial endofunctors F, G : Set → Set: G-coalgebra is a pair (U, ϕ) and ϕ = destr1, . . . , destrn = 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

slide-6
SLIDE 6

Category of Algebras

Algebra homomorphism f∗ : (A, α) → (B, β)

F(A) F(f)

✲ F(B)

A α

f

✲ B

β

Category of algebras

Alg(F)

  • bjects - 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

slide-7
SLIDE 7

Initial algebra

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 catamorphism: for α : F(A) → A is cata α : µF → A

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

slide-8
SLIDE 8

Initial algebra

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

FµF inF✲ µF FA F(cata α)

α

✲ A

cata α

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

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

slide-9
SLIDE 9

Category of coalgebras

Coalgebra homomorphism f∗ : (U, ϕ) → (V, ψ)

U f

✲ V

F(U) ϕ

F(f)

✲ F(V )

ψ

Category of coalgebras

Coalg(F)

  • bjects - 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

slide-10
SLIDE 10

Final coalgebra

Final coalgebra is the final object of the category Coalg(F) (νF, outF ) coalgebra dynamics out is defined:

  • utF : 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

slide-11
SLIDE 11

Final coalgebra

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

U ϕ

✲ FU

νF ana ϕ

  • utF

✲ FνF

F(ana ϕ)

ana ϕ ◦ outF = ϕ ◦ Ff

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

slide-12
SLIDE 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 FA Ff

α

✲ A

f

It holds that f = ϕ ◦ Ff ◦ α.

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

slide-13
SLIDE 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 FA Ff

α

✲ A

hylo(ϕ, α)F

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

slide-14
SLIDE 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, push](w) =  new, if w = (1, (s, ε)) 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

slide-15
SLIDE 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]) 1 + (I∗ × I) id + (fill × id)

✲ 1 + (S × I)

⊚ I∗ inF = [new, push] ∼ =

fill = cata α

✲ S

α = [new, π1]

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

slide-16
SLIDE 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) next(s) =  κ1(∗) if s is empty κ2(s′, i) if s = push(s′, i)

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

slide-17
SLIDE 17

Coalgebraic definition of constructors

1 new

✲ I∗

⊚ 1 + (1 × I) κ1

id + (new × id)

✲ 1 + (I∗ × I)

∼ = next

I∗ × I ana ϕ = push

✲ I∗

⊚ 1 + ((I∗ × I) × I) ϕ = κ2

id + (push × id)

✲ 1 + (I∗ × I)

∼ = next = outF

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

slide-18
SLIDE 18

Combining the algebra and coalgebra

1 + (S × I) [new, π1]

✲ S

1 + (I∗ × I) id + (fill × id)

[new, push]

✲ I∗

fill

1 + ((I∗ × I) × I) id + (push × id)

id + (push × id)

✲ 1 + (I∗ × I)

next

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

slide-19
SLIDE 19

Recursive coalgebra for Stack

Recursive coalgebra for Stack

I∗ next✲ 1 + (I∗ × I) ⊚ S fill

❄ ✛

[new, π1] 1 + (S × I) F(fill)

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

slide-20
SLIDE 20

Recursive coalgebra for Stack

Recursive coalgebra for Stack

The coalgebra-to-algebra morphism fill : I∗ → S is defined: fill(i) =  s if card(i) = length(s) ⊥

  • therwise

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

slide-21
SLIDE 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

slide-22
SLIDE 22

Implementation of the catamorphism

Catamorphism

it represents the recursive function intList → int l e t rec cata l i s t = match l i s t with | [ ] −> 1 | head : : t a i l −> head ∗ ( cata t a i l ) ; ;

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

slide-23
SLIDE 23

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 ; ; − : int = 24

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

slide-24
SLIDE 24

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

slide-25
SLIDE 25

Thank You for Your attention

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

slide-26
SLIDE 26

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

slide-27
SLIDE 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: submission@kpi.fei.tuke.sk. 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