Successfully reported this slideshow.   ×

## Check these out next

1 of 196
1 of 196

Discusses the algebraic properties of types, different kinds of functions and the information that is preserved or lost, and Category Theory concepts that underpin and unify them.

Discusses the algebraic properties of types, different kinds of functions and the information that is preserved or lost, and Category Theory concepts that underpin and unify them.

1. 1. Category Theory for Beginners Your data structures are made of maths! Melbourne Scala User Group Mar 2015 @KenScambler
2. 2. Data structures More and more “logical” Less low-level memory/hardware connection More and more support for immutability We can use maths to reason about them! Category Theory can reveal even deeper symmetries
3. 3. Algebraic Data Types Constants, sums, products, exponents We can directly manipulate them with algebra!
4. 4. Products A × B
5. 5. NinjaTurtles
6. 6. NinjaTurtles × BluesBrothers×
7. 7. NinjaTurtles BluesBrothers = NTs and BBs× = ×
8. 8. =4 × 2 8
9. 9. Integers as types? We can actually use integers to represent our types! The integers correspond to the size of the type
10. 10. Products in code (NinjaTurtle, BluesBrother)
11. 11. (NinjaTurtle, BluesBrother) 4
12. 12. (NinjaTurtle, BluesBrother) 4 2
13. 13. NinjaTurtle (,) BluesBrother 4 2×
14. 14. (NinjaTurtle, BluesBrother) 8
15. 15. case class TurtleAndBrother( foo1: NinjaTurtle, foo2: BluesBrother)
16. 16. case class TurtleAndBrother( foo1: NinjaTurtle, foo2: BluesBrother) 4
17. 17. case class TurtleAndBrother( foo1: NinjaTurtle, foo2: BluesBrother) 4 2
18. 18. case class TurtleAndBrother(…) NinjaTurtle BluesBrother 4 2 ×
19. 19. case class TurtleAndBrother( foo1: NinjaTurtle, foo2: BluesBrother) 8
20. 20. trait Confluster { def defrabulate(): BluesBrother def empontigle(): NinjaTurtle }
21. 21. trait Confluster { def defrabulate(): BluesBrother def empontigle(): NinjaTurtle } 2
22. 22. trait Confluster { def defrabulate(): BluesBrother def empontigle(): NinjaTurtle } 4 2
23. 23. trait Confluster { def; def; } BluesBrother NinjaTurtle 2 4 ×
24. 24. trait Confluster { def defrabulate(): BluesBrother def empontigle(): NinjaTurtle } 8
25. 25. Sums A + B
26. 26. true false Boolean
27. 27. true false + Boolean Shapes+
28. 28. true false + = Boolean Shapes Boolean or Shapes true false + =
29. 29. true false true false =2 + 4 6
30. 30. sealed trait Holiday case object Christmas extends Holiday case object Easter extends Holiday case object AnzacDay extends Holiday Sums in code
31. 31. sealed trait Holiday case object Christmas extends Holiday case object Easter extends Holiday case object AnzacDay extends Holiday 1
32. 32. sealed trait Holiday case object Christmas extends Holiday case object Easter extends Holiday case object AnzacDay extends Holiday 11
33. 33. sealed trait Holiday case object Christmas extends Holiday case object Easter extends Holiday case object AnzacDay extends Holiday 11 1
34. 34. sealed trait Holiday, case, extends Christmas Easter AnzacDay 11 1 +
35. 35. sealed trait Holiday case object Christmas extends Holiday case object Easter extends Holiday case object AnzacDay extends Holiday 3
36. 36. sealed trait Opt[A] case class Some[A](a: A) extends Opt[A] case class None[A] extends Opt[A]
37. 37. sealed trait Opt[A] case class Some[A](a: A) extends Opt[A] case class None[A] extends Opt[A] A
38. 38. sealed trait Opt[A] case class Some[A](a: A) extends Opt[A] case class None[A] extends Opt[A] A 1
39. 39. sealed trait Opt[A], case class, extends Some[A](a: A) None[A] A 1 +
40. 40. sealed trait Opt[A] case class Some[A](a: A) extends Opt[A] case class None[A] extends Opt[A]A + 1
41. 41. Either[Holiday, Opt[Boolean]]
42. 42. Either[Holiday, Opt[Boolean]] 3
43. 43. Either[Holiday, Opt[Boolean]] 3 2
44. 44. Either[Holiday, Opt[Boolean]] 3 2 + 1
45. 45. Either[Holiday, Opt[Boolean]] 3 3
46. 46. Either[Holiday, Opt[Boolean]] 3 3+
47. 47. Either[Holiday, Opt[Boolean]] 6
48. 48. Exponents BA
49. 49. Exponents BA A type to the power of another type?? Huh?
50. 50. true false Boolean
51. 51. true false Boolean Shapes
52. 52. true false Boolean Shapes = Shapes  Boolean true false true false true false true false true false true false true false true false
53. 53. true false 23 = true false true false true false true false true false true false true false true false 8
54. 54. Function types = exponents! A  B = BA
55. 55. Exponents in code def getTurtle(): NinjaTurtles
56. 56. def getTurtle: () => NinjaTurtles
57. 57. def getTurtle: () => NinjaTurtles 1
58. 58. def getTurtle: () => NinjaTurtles 1 4
59. 59. def getTurtle: () => NinjaTurtles 1  4
60. 60. def getTurtle: () => NinjaTurtles 41
61. 61. def getTurtle: () => NinjaTurtles 4
62. 62. Functions “with no arguments” are tacitly from a singleton type such as Unit Singleton types carry no information.
63. 63. trait State[S, A] { def run(state: S): (A, S) }
64. 64. trait State[S, A] { def run(state: S): (A, S) } A×S
65. 65. trait State[S, A] { def run(state: S): (A, S) } S  A×S
66. 66. trait State[S, A] { def run(state: S): (A, S) } (A×S)S
67. 67. {•} Zero On e Product s Sum sExponent s Sets Scala Algebra {} A×B A∪B AB (A, B) 0 1 AB A+B A -> B A / B Nothing Unit BA
68. 68. Currying def tupled[A, B, C](a: A, b: B): C def curried[A, B, C]: A => (B => C)
69. 69. Currying def tupled[A, B, C](a: A, b: B): C def curried[A, B, C]: A => (B => C) A×B
70. 70. Currying def tupled[A, B, C](a: A, b: B): C def curried[A, B, C]: A => (B => C) A×B  C
71. 71. Currying def tupled[A, B, C](a: A, b: B): C def curried[A, B, C]: A => (B => C) CAB
72. 72. Currying def tupled[A, B, C](a: A, b: B): C def curried[A, B, C]: A => (B => C) CAB A(B  C)
73. 73. Currying def tupled[A, B, C](a: A, b: B): C def curried[A, B, C]: A => (B => C) CAB ACB
74. 74. Currying def tupled[A, B, C](a: A, b: B): C def curried[A, B, C]: A => (B => C) CAB CAB
75. 75. Recursion sealed trait List[+A] case class Cons[A](h: A, t: List[A]) extends List[A] case object Nil extends List[Nothing]
76. 76. sealed trait List[+A] case class Cons[A](h: A, t: List[A]) extends List[A] case object Nil extends List[Nothing] A
77. 77. sealed trait List[+A] case class Cons[A](h: A, t: List[A]) extends List[A] case object Nil extends List[Nothing] A L(A)
78. 78. sealed trait List[+A] case class Cons[A](h: A, t: List[A]) extends List[A] case object Nil extends List[Nothing] A × L(A)
79. 79. sealed trait List[+A] case class Cons[A](h: A, t: List[A]) extends List[A] case object Nil extends List[Nothing] A × L(A) 1
80. 80. sealed trait List[+A] case class Cons[A](h: A, t: List[A]) extends List[A] case object Nil extends List[Nothing] 1 + A × L(A)
81. 81. Expanding a list… L(a) = 1 + a × L(a)
82. 82. Expanding a list… L(a) = 1 + a L(a)
83. 83. Expanding a list… L(a) = 1 + a L(a) = 1 + a (1 + a L(a))
84. 84. Expanding a list… L(a) = 1 + a L(a) = 1 + a (1 + a L(a)) = 1 + a + a2 (1 + a L(a))
85. 85. Expanding a list… L(a) = 1 + a L(a) = 1 + a (1 + a L(a)) = 1 + a + a2 (1 + a L(a)) … = 1 + a + a2 + a3 + a4 + a5…
86. 86. Expanding a list… L(a) = 1 + a L(a) = 1 + a (1 + a L(a)) = 1 + a + a2 (1 + a L(a)) … = 1 + a + a2 + a3 + a4 + a5… Nil or 1-length or 2-length or 3-length or 4-length etc
87. 87. What does it mean for two types to have the same number?
88. 88. Not identical… but isomorphic
89. 89. Terminology isomorphism
90. 90. Terminology isomorphism Equal
91. 91. Terminology isomorphism Equal-shape-ism
92. 92. Terminology isomorphism “Sorta kinda the same-ish” but I want to sound really smart - Programmers
93. 93. Terminology isomorphism “Sorta kinda the same-ish” but I want to sound really smart - Programmers
94. 94. Terminology isomorphism One-to-one mapping between two objects so you can go back-and-forth without losing information
95. 95. These 4 Shapes Wiggles Set functions Set 4 = 4
96. 96. These 4 Shapes Wiggles
97. 97. These 4 Shapes Wiggles
98. 98. There can be lots of isos between two objects! If there’s at least one, we can say they are isomorphic or A ≅ B
99. 99. Programming language syntaxProgramming language FEATURES!!! NAMES NAMES NAMES actual structure Programming language syntax Programming language FEATURES!!! NAMES NAMES NAMES actual structure class Furpular { class Diggleton { } }
100. 100. Programming language syntaxProgramming language FEATURES!!! NAMES NAMES NAMES actual structure Programming language syntax Programming language FEATURES!!! NAMES NAMES NAMES actual structure class Furpular { class Diggleton { } }
101. 101. Programming language syntaxProgramming language FEATURES!!! NAMES NAMES NAMES actual structure Programming language syntax Programming language FEATURES!!! NAMES NAMES NAMES actual structure class Furpular { class Diggleton { } } Looking at data structures algebraically lets us compare the true structure
102. 102. actual structure but faster actual structure Knowing an isomorphism, we can rewrite for • Performance • Memory usage • Elegance with proven correctness!
103. 103. actual structure but faster actual structure Knowing an isomorphism, we can rewrite for • Performance • Memory usage • Elegance with proven correctness!* *mumble mumble non-termination
104. 104. Category Theory object A B object arrow Arrows join objects. They can represent absolutely anything.
105. 105. Categories generalise functions over sets A B f
106. 106. Arrows compose like functions A B C f g
107. 107. Arrows compose like functions A C g ∘ f
108. 108. A Every object has an identity arrow, just like the identity function
109. 109. That’s all a category is!
110. 110. Products in CT A × BA B first seco nd trait Product[A,B] { def first: A def second: B }
111. 111. Sums in CT A + BA B Left Right sealed trait Sum[A,B] case class Left[A,B](a: A) extends Sum[A,B] case class Right[A,B](b: B) extends Sum[A,B]
112. 112. Opposite categories C Cop A B C g ∘ f f g A B C f ∘ g f g Isomorphic!
113. 113. A B C g ∘ f f g A B C f ∘ g f g Just flip the arrows, and reverse composition!
114. 114. A A×B B A product in C is a sum in Cop A sum in C is a product in Cop A+B B A C Cop
115. 115. Sums are isomorphic to Products!
116. 116. Terminology dual An object and its equivalent in the opposite category are to each other.
117. 117. Terminology Co-(thing) Often we call something’s dual a
118. 118. Terminology Coproducts Sums are also called
119. 119. Tightening the definitions A × BA B first seco nd × × trait ProductPlusPlus[A,B] { def first: A def second: B def banana: Banana def brother: BluesBrother }
120. 120. A × BA B first seco nd × × Does that still count as A × B?
121. 121. A × BA B first seco nd × × No way!
122. 122. A × BA B first seco nd × × Umpire theA someB trait Umpire { def theA: A def someB: B }
123. 123. A × BA B first seco nd × × Umpire theA someB trait Umpire { def theA: A def someB: B } unique∃
124. 124. (a, b,a b Umpire trait Umpire { def theA: A = a def someB: B = b } , )Instances
125. 125. (a, b,a b Umpire trait Umpire { def theA: A = a def someB: B = b } , )Instances
126. 126. (a, b,a b Umpire trait Umpire { def theA: A = a def someB: B = b } , ) not actually unique  Instances
127. 127. Requiring a unique arrow from a 3rd object that independently knows A and B proves that there’s no extra gunk.
128. 128. But wait!
129. 129. What if Umpire has special knowledge about other products?
130. 130. A × BA B first seco nd Umpire theA someB trait Umpire { def theA: A def someB: B def specialOtherProd: (A,B) }
131. 131. It could introduce strange new things with its special knowledge!
132. 132. We need to know nothing about the object other than the two arrows!
133. 133. PA B ??? ? ? unique∃ For all objects that 1) have an arrow to A and B 2) there exists a unique arrow to P
134. 134. PA B ??? ? ? unique∃ Then P is “the” product of A and B!
135. 135. Same with sums!
136. 136. SA B ??? ? ? unique∃ For all objects that 1) have an arrow from A and B 2) there exists a unique arrow from S
137. 137. SA B ??? ? ? unique∃ Then S is “the” sum of A and B!
138. 138. Terminology universal property universal mapping property UMP
139. 139. Terminology universal property “The most efficient solution to a problem”
140. 140. Terminology universal property Proves that - We generate only what we need - We depend on only what we need
141. 141. Compare to programming: trait Monoid[M] { def id: M def compose(a: M, b: M): M } trait Foldable[F[_]] { def foldMap[M: Monoid, A]( fa: F[A], f: A => M): M }
142. 142. Like UMPs, type parameters “for all F” “for all A and M where M is a Monoid” don’t just prove what your code is, but what it isn’t.
143. 143. Proving what your code isn’t prevents bloat and error, and promotes reuse. Proving what your code is allows you to use it.
144. 144. Zero On eProduct s Sum s Exponent s Scala Algebra (A, B) 0 1 AB A+B A -> B A / B Nothing Unit BA Types vs Algebra vs CT CT ? ? ? ? ? ?
145. 145. Intermission
146. 146. Injections A B A function is injective if it maps 1-to-1 onto a subset of the codomain
147. 147. A B All the information in A is preserved…
148. 148. A B All the information in A is preserved… A
149. 149. B But everything else in B is lost. A B
150. 150. Another way of looking at it… C A Bfg h If f ∘ g = f ∘ h, then g = h
151. 151. C A Bfg h If f ∘ g = f ∘ h, then g = h
152. 152. C A Bfg h If f ∘ g = f ∘ h, then g = h
153. 153. Injections in code User( firstName = "Bob", lastName = "Smith", age = 73) User JSON { “firstName”: "Bob", “lastName”: ”Smith”, “age”: 73 }
154. 154. User JSON We don’t lose information converting to JSON.
155. 155. But from JSON? We lose structure… User JSON
156. 156. Surjections A B A function is surjective if it maps onto the whole of the codomain
157. 157. A All the information in B is preserved… AB
158. 158. A But everything else in A is lost. AB
159. 159. A B C If g ∘ f = h ∘ f, then g = h f g h
160. 160. A B C f g h If g ∘ f = h ∘ f, then g = h
161. 161. A B C f g h If g ∘ f = h ∘ f, then g = h
162. 162. Injections generalised C A Bf g h If f ∘ g = f ∘ h, then g = h
163. 163. Monomorphisms C A Bf g h f is monic If f ∘ g = f ∘ h, then g = h
164. 164. Monomorphisms C A Bf g h If f ∘ g = f ∘ h, then g = h
165. 165. Surjections generalised CA Bf g h If g ∘ f = h ∘ f, then g = h
166. 166. Epimorphisms CA Bf g h f is epic If g ∘ f = h ∘ f, then g = h
167. 167. C A B A mono in C is an epi in Cop A B C Cop C
168. 168. Monos are dual to epis.
169. 169. Bijections A B A function is bijective if it maps 1-to-1 onto the whole codomain
170. 170. Bijections A B We don’t lose any information A  B  A… A
171. 171. Bijections B Nor do we lose information B  A  B… A B
172. 172. The CT equivalent is of course…
173. 173. The CT equivalent is of course… Isomorphisms!
174. 174. Injection Surjection Bijection Mono Epi Iso
175. 175. Motivation So much software is just mapping between things!
176. 176. form
177. 177. form {…}
178. 178. form {…} Order(a,b,c)
179. 179. form {…} Order(a,b,c) INSERT INTO…
180. 180. form {…} Order(a,b,c) INSERT INTO…
181. 181. form {…} Order(a,b,c) INSERT INTO… Receipt(…)
182. 182. form {…} Order(a,b,c) INSERT INTO… Receipt(…) Logs on disk
183. 183. form {…} Order(a,b,c) INSERT INTO… Receipt(…) Logs on disk <xml>
184. 184. form {…} Order(a,b,c) INSERT INTO… Receipt(…)<xml> receipt Logs on disk
185. 185. It is essential to understand how information is preserved in flows like this
186. 186. Chinese whispers! Bugs proliferate where data is lost!
187. 187. Injections and surjections tell us what is preserved and what is lost
188. 188. Bijections are especially valuable
189. 189. Conclusion Data structures are made out of maths!
190. 190. How we map between them is maths too.
191. 191. Understanding the underlying shape of data and functions is enormously helpful for more robust, error-free software
192. 192. Isomorphism is more interesting than equality! Isomorphic types can be rewritten, optimised without error. Isomorphic mappings allow us to preserve information
193. 193. Universal properties exemplify • Depending on the minimum • Producing the minimum ?
194. 194. Again, Category Theory shows us deeper, simpler patterns, unifying concepts that otherwise look different ? ? × +
195. 195. Further reading Awodey, “Category Theory” Lawvere & Schanuel, “Conceptual Mathematics: an introduction to categories” Jeremy Kun, “Math ∩ Programming” at http://jeremykun.com/ Chris Taylor, “The algebra of algebraic datatypes” http://chris-taylor.github.io/blog/2013/02/10/the-algebra-of- algebraic-data-types/ http://chris-taylor.github.io/blog/2013/02/11/the-algebra-of- algebraic-data-types-part-ii/ http://chris-taylor.github.io/blog/2013/02/13/the-algebra-of- algebraic-data-types-part-iii/
196. 196. Further reading Bartosz Milewski “Categories for Programmers” http://bartoszmilewski.com/2014/10/28/category-theory-for- programmers-the-preface/ http://bartoszmilewski.com/2015/03/13/function-types/