Explains the basic concepts of Category Theory, useful terminology to help understand the literature, and why it's so relevant to software engineering.
2. Abstract maths… for us?
Dizzyingly abstract branch of maths
“Abstract nonsense”?
Programming = maths
Programming = abstraction
Really useful to programming!
3. The plan
Basic Category Theory concepts
New vocabulary (helpful for further reading)
How it relates to programming
Category Theory as seen by maths versus FP
4. A bit of background
1940s Eilenberg, Mac Lane invent Category Theory
1958 Monads discovered by Godement
In programming:
1990 Moggi, Wadler apply monads to programming
2006 “Applicative Programming with Effects” McBride &
Paterson
2006 “Essence of the Iterator Pattern” Gibbons & Oliveira
43. Class hierarchy
Objects = classes
Arrows = “extends”
Composition = “extends” is transitive
Identity = trivial
44. Class hierarchy
Partially ordered sets (posets)
Objects = elements in the set
Arrows = ordering relation ≤
Composition = ≤ is transitive
Identity = trivial
48. “Free Category” from
graphs!
Objects = nodes
Arrows = paths (0 to many edges)
Composition = aligning paths end to
end
Identity = you’re already there
50. Category of Types & Functions
object FnCat
extends Category[Function1] {
def compose[A,B,C](
c: B => C,
d: A => B): A => C = {
a => c(d(a))
}
def id[A]: A => A = (a => a)
}
107. Functors
Fundamental concept in Category Theory
Super useful
Everywhere
Staple of functional programming
Write code that’s ignorant of unnecessary context
117. Monoids are categories
Objects = placeholder singleton
Arrows = elements of the monoid
Composition = •
Identity = id
Only one object
Each arrow is an element
in the monoid
181. Using composable
abstractions means your
code can grow without
getting more complex
Categories and Monoids
capture the essence of
composition in software!
182. Look for Monoids and
Categories in your domain
where you can
You can even bludgeon non-
composable things into free
monoids and free categories
199. def getRosterNames(users: Roster[User]):
Roster[Name] = {
LogFactory.getLogger.info(s"When you
party with ${users.rosterTitle}, you must
party hard!")
if (users.isFull) EmptyRoster("(everyone)
")
else users.map(_.name)
}
200. When code knows too much,
soon new things will appear
that actually require the other
stuff.
208. There is no better
way to understand
the patterns
underlying software
than studying
Category Theory.
209. Further reading
Awodey, “Category Theory”
Lawvere & Schanuel, “Conceptual Mathematics: an
introduction to categories”
Jeremy Kun, “Math ∩ Programming” at
http://jeremykun.com/
Gabriel Gonzalez “Haskell for all”
http://www.haskellforall.com/2012/08/the-category-design-
pattern.html
http://www.haskellforall.com/2014/04/scalable-program-
architectures.html