17
Formal Methods in Software Lecture 8. Category Theory Vlad Patryshev SCU 2014

Formal methods 8 - category theory (last one)

Embed Size (px)

DESCRIPTION

My course of Formal Methods at Santa Clara University, Winter 2014.

Citation preview

Page 1: Formal methods   8 - category theory (last one)

Formal Methods in Software

Lecture 8. Category Theory

Vlad PatryshevSCU2014

Page 2: Formal methods   8 - category theory (last one)

In This Lecture● functors and examples (diagrams; product; exponentiations); ● currying/yoneda lemma; ● example with integers/rationals; ● monad

Page 3: Formal methods   8 - category theory (last one)

Example: Parametric Classtrait List[A] extends Iterable[A] {

def map[A](f:A=>B):List[B]

}

Page 4: Formal methods   8 - category theory (last one)

Functor

Given categories A and B, a functor F: A → B consists of mappings F: Obj(A) → Obj(B) and F: Fun(A) → Fun(B) ,

so that F(idX)=idF(X)

and F(f∘g)=F(f)∘F(g)

Page 5: Formal methods   8 - category theory (last one)

More Examples● A= 3 , B=Sets; what is a functor 3 → Sets?

Have to define: sets F(0), F(1), F(2); functions F(f), F(g), F(gf)

● Any f:X→Y in category C is a functor from 2 to C.

Page 6: Formal methods   8 - category theory (last one)

Cartesian Product is a FunctorFix an object A. X ↦ X×A is a functor. How does it work on functions?f:X→Y ↦ f×idA: X×A → Y×A

Page 7: Formal methods   8 - category theory (last one)

+1 is a FunctorIn Sets, take X ↦ X+1. f: X→Y ↦ (f+1): X+1 → Y+1

This functor is called Option[T].

Page 8: Formal methods   8 - category theory (last one)

Category of CategoriesTake functors F:A→B, G:B→C; their composition H=G∘F is also a functor.H(idX) = G(F(idX)) = G(idF(X)) = idG(F(X))

H(p∘q) = G(F(p∘q)) = G(F(p)∘F(q)) = G(F(p)) ∘G(F(q)) = H(p) ∘H(q)

Identity functor IdA:A→A Id(f) = f

Cat - a category of all categories. No barber problems, nobody shaves nobody.

Page 9: Formal methods   8 - category theory (last one)

Exponential FunctorA, B - objects in category C. BA - an object of functions from A to B, if such exists.E.g. in Set, {f:A→B}. In Scala In Java (Guava) (https://code.google.com/p/guava-

libraries/wiki/FunctionalExplained)

trait Function[X,Y] { def apply(x:X): Y}

interface Function<X,Y> { Y apply(X x);}

val len:Function[String, Int] = _.length Function<String, Integer> lengthFunction = new Function<String, Integer>() { public Integer apply(String string) { return string.length(); }};

Page 10: Formal methods   8 - category theory (last one)

Define Exponential via CurryingIn Set, f:A→CB ≡ f’:A×B→Cf(x)(b) = f’(x,b)

Very similar to P⊢(Q→R) ≡ P∧Q ⊢ R

Actually… it’s the same thing.Take posets, for example, Boolean lattice, where a×b ≡ min(a,b) /*see lecture 7*/ ≡ a∧b

a∧b ≤ c ≡ a ≤ (b→c)

Page 11: Formal methods   8 - category theory (last one)

Currying: Yoneda Lemma

Fun(A×B, C) ≡ Fun(A, CB)

(Caveat: what is Fun? A set? It’s not always available as a set)

Page 12: Formal methods   8 - category theory (last one)

Example with NumbersCategory Z = (Z,≤); category Q = (Q,≤)Inclusion iz: Z ↣ Q - preserves order, so is a functor.How about Q → Z? Upb: q ↦ Upb(q)Upb(q) ≤ n /* this is in Z */ ≡ q ≤ n /* this is in Q */

Take composition q ↦ iZ(Upb(q)), call it Mint.We see two properties:

● q ≤ Mint(q)● Mint(Mint(q)) ≤ Mint(q) /* actually equal */

Page 13: Formal methods   8 - category theory (last one)

Example with ListsCategory ScalaTake functor List.We see two properties:

● singleton: X → List[X] ● flatten: List[List[X]] → List[X]

Do you see similarity?

Page 14: Formal methods   8 - category theory (last one)

Monad

* f:F(X)->G(X) is natural if for all p:X→ Y

Given a category C, an endofunctor M: C → C is called a Monad if it has two features:

● unitX: X → M(X)● flattenX: M(M(X)) → M(X)

These two functions should be natural*

Page 15: Formal methods   8 - category theory (last one)
Page 17: Formal methods   8 - category theory (last one)