the theory behind functional programming functional: based on lambda calculus / combinators,...

11
The theory behind Functional Programming • Functional: based on lambda calculus / combinators, recursion theory, category theory. • By contrast with other paradigms: Functions ONLY! • Defining functions • Defining data types

Upload: kristin-mckinney

Post on 18-Jan-2016

222 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: The theory behind Functional Programming Functional: based on lambda calculus / combinators, recursion theory, category theory. By contrast with other

The theory behind Functional Programming

• Functional: based on lambda calculus / combinators, recursion theory, category theory.

• By contrast with other paradigms: Functions ONLY!

• Defining functions• Defining data types

Page 2: The theory behind Functional Programming Functional: based on lambda calculus / combinators, recursion theory, category theory. By contrast with other

Category Theory

• Abstract view of functions as “arrows” with domains and codomains as their “source” and “target” objects

• Things to watch:• How we compose arrows• Which arrows can be composed• How objects can be seen as identity arrows• How we connect categories with functors

Page 3: The theory behind Functional Programming Functional: based on lambda calculus / combinators, recursion theory, category theory. By contrast with other

Resources on Category Theory

• http://en.wikipedia.org/wiki/Category_theory• https://www.google.com/search?q=Awodey+

+Category+Theory+&ie=utf-8&oe=utf-8&aq=t&rls=org.mozilla:en-US:official&client=firefox-a

Page 4: The theory behind Functional Programming Functional: based on lambda calculus / combinators, recursion theory, category theory. By contrast with other

Definition of a Category

• Tw classes: Objects A,B,C,… and Arrows f,g,h,…• For each arrow f : A->B, two objects A=dom(f)

B=cod(f)• If f:A->B and g:B->C, their composition, denoted

g . f : A->C exists• For each object A the arrow 1_A : A->A exists• For arrows f:A->B,g:B->C,h:C->D composition is

associative: h . (g . f) = (h . g).f• For f:A->B, 1_A is a unit arrow: f . 1_A=f=1_B . f

Page 5: The theory behind Functional Programming Functional: based on lambda calculus / combinators, recursion theory, category theory. By contrast with other

Examples of Categories

• Finite sets with functions• Groups with group homomorphisms• Vector spaces with linear mappings• Graphs with graph homomorphisms• Topological spaces with continuous functions• Posets and monotone functions

Page 6: The theory behind Functional Programming Functional: based on lambda calculus / combinators, recursion theory, category theory. By contrast with other

Functional programming seen as a category

• Objects: monomorphic data types: Integer, Boolean, records of employees

• Arrows: functions between data types• Id:: a -> a provides 1_a for each type a• “.” provides composition – and it is defined if

types match• Function composition is associative

Page 7: The theory behind Functional Programming Functional: based on lambda calculus / combinators, recursion theory, category theory. By contrast with other

Functors

• a (covariant) functor F : C → D:– for each object x in C, an object F(x) in D– for each arrow f : x → y in C, an arrow F(f) : F(x) →

F(y),• such that the following two properties hold:– For every object x in C, F(1x) = 1F(x);– For all morphisms f : x → y and g : y → z, F(g ∘ f) =

F(g) ∘ F(f).• Of special interest: endofunctors F:C->C

Page 8: The theory behind Functional Programming Functional: based on lambda calculus / combinators, recursion theory, category theory. By contrast with other

The Initial Algebra Semantics for polymorphic data types (1)

• F-algebra: can be used to represent data structures used in programming, such as lists and trees. Defined such that the following diagram commutes:

Page 9: The theory behind Functional Programming Functional: based on lambda calculus / combinators, recursion theory, category theory. By contrast with other

The Initial Algebra Semantics for polymorphic data types (2)

• the functor F: Set -> Set that sends a set X to 1+X• + denotes the usual coproduct (also called

“sum”) given by disjoint union, and 1 is a terminal object (i.e. any singleton set)

• the set N of natural numbers together with the function succ : 1+N->N, is the + of the functions zero : 1->N (whose image is 0) and succ : 1+N->N (which sends an integer n to n+1) is an initial F-algebra

Page 10: The theory behind Functional Programming Functional: based on lambda calculus / combinators, recursion theory, category theory. By contrast with other

The Initial Algebra Semantics for polymorphic data types (3)

• Types defined by using least fixed point construct with functor F can be regarded as an initial F-algebra, provided that parametricity holds for the type

• Examples:data N = Zero | Succ Ndata List a = Nil | Cons a (List a)map provides the functor definition for lists: it preserves the structure while changing its payload