Upload
vlad-patryshev
View
697
Download
1
Embed Size (px)
DESCRIPTION
My course of Formal Methods at Santa Clara University, Winter 2014.
Citation preview
Formal Methods in Software
Lecture 8. Category Theory
Vlad PatryshevSCU2014
In This Lecture● functors and examples (diagrams; product; exponentiations); ● currying/yoneda lemma; ● example with integers/rationals; ● monad
Example: Parametric Classtrait List[A] extends Iterable[A] {
def map[A](f:A=>B):List[B]
}
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)
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.
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
+1 is a FunctorIn Sets, take X ↦ X+1. f: X→Y ↦ (f+1): X+1 → Y+1
This functor is called Option[T].
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.
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(); }};
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)
Currying: Yoneda Lemma
Fun(A×B, C) ≡ Fun(A, CB)
(Caveat: what is Fun? A set? It’s not always available as a set)
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 */
Example with ListsCategory ScalaTake functor List.We see two properties:
● singleton: X → List[X] ● flatten: List[List[X]] → List[X]
Do you see similarity?
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*
Referenceshttp://www.amazon.com/Category-Computer-Scientists-Foundations-Computing/dp/0262660717http://fundeps.com/tables/FromSemigroupToMonads.pdfWikipedia