Your data structures are made of maths!

6 min read Original article ↗
  • 1.

    Category Theory forBeginners Your data structures are made of maths! Melbourne Scala User Group Mar 2015 @KenScambler

  • 2.

    Data structures More andmore “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.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.

    Integers as types? Wecan actually use integers to represent our types! The integers correspond to the size of the type

  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.

    sealed trait Holiday caseobject Christmas extends Holiday case object Easter extends Holiday case object AnzacDay extends Holiday Sums in code

  • 31.

    sealed trait Holiday caseobject Christmas extends Holiday case object Easter extends Holiday case object AnzacDay extends Holiday 1

  • 32.

    sealed trait Holiday caseobject Christmas extends Holiday case object Easter extends Holiday case object AnzacDay extends Holiday 11

  • 33.

    sealed trait Holiday caseobject Christmas extends Holiday case object Easter extends Holiday case object AnzacDay extends Holiday 11 1

  • 34.
  • 35.

    sealed trait Holiday caseobject Christmas extends Holiday case object Easter extends Holiday case object AnzacDay extends Holiday 3

  • 36.
  • 37.
  • 38.
  • 39.
  • 40.

    sealed trait Opt[A] caseclass Some[A](a: A) extends Opt[A] case class None[A] extends Opt[A]A + 1

  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.

    Functions “with noarguments” are tacitly from a singleton type such as Unit Singleton types carry no information.

  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
  • 76.

    sealed trait List[+A] caseclass Cons[A](h: A, t: List[A]) extends List[A] case object Nil extends List[Nothing] A

  • 77.

    sealed trait List[+A] caseclass Cons[A](h: A, t: List[A]) extends List[A] case object Nil extends List[Nothing] A L(A)

  • 78.

    sealed trait List[+A] caseclass Cons[A](h: A, t: List[A]) extends List[A] case object Nil extends List[Nothing] A × L(A)

  • 79.

    sealed trait List[+A] caseclass Cons[A](h: A, t: List[A]) extends List[A] case object Nil extends List[Nothing] A × L(A) 1

  • 80.

    sealed trait List[+A] caseclass Cons[A](h: A, t: List[A]) extends List[A] case object Nil extends List[Nothing] 1 + A × L(A)

  • 81.
  • 82.
  • 83.
  • 84.
  • 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.

    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.

    What does itmean for two types to have the same number?

  • 88.
  • 89.
  • 90.
  • 91.
  • 92.
  • 93.
  • 94.
  • 95.
  • 96.
  • 97.
  • 98.

    There can belots of isos between two objects! If there’s at least one, we can say they are isomorphic or A ≅ B

  • 99.
  • 100.
  • 101.
  • 102.
  • 103.
  • 104.
  • 105.
  • 106.
  • 107.
  • 108.
  • 109.
  • 110.

    Products in CT A× BA B first seco nd trait Product[A,B] { def first: A def second: B }

  • 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.
  • 113.
  • 114.

    A A×B B A product inC is a sum in Cop A sum in C is a product in Cop A+B B A C Cop

  • 115.
  • 116.
  • 117.
  • 118.
  • 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.

    A × BAB first seco nd × × Does that still count as A × B?

  • 121.

    A × BAB first seco nd × × No way!

  • 122.

    A × BAB first seco nd × × Umpire theA someB trait Umpire { def theA: A def someB: B }

  • 123.

    A × BAB first seco nd × × Umpire theA someB trait Umpire { def theA: A def someB: B } unique∃

  • 124.
  • 125.
  • 126.

    (a, b,a b Umpire traitUmpire { def theA: A = a def someB: B = b } , ) not actually unique  Instances

  • 127.

    Requiring a uniquearrow from a 3rd object that independently knows A and B proves that there’s no extra gunk.

  • 128.
  • 129.

    What if Umpirehas special knowledge about other products?

  • 130.

    A × BAB first seco nd Umpire theA someB trait Umpire { def theA: A def someB: B def specialOtherProd: (A,B) }

  • 131.
  • 132.

    We need toknow nothing about the object other than the two arrows!

  • 133.

    PA B ??? ? ? unique∃ Forall objects that 1) have an arrow to A and B 2) there exists a unique arrow to P

  • 134.
  • 135.
  • 136.

    SA B ??? ? ? unique∃ Forall objects that 1) have an arrow from A and B 2) there exists a unique arrow from S

  • 137.
  • 138.
  • 139.
  • 140.
  • 141.

    Compare to programming: traitMonoid[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.

    Like UMPs, typeparameters “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.

    Proving what yourcode isn’t prevents bloat and error, and promotes reuse. Proving what your code is allows you to use it.

  • 144.
  • 145.
  • 146.
  • 147.
  • 148.
  • 149.
  • 150.

    Another way oflooking at it… C A Bfg h If f ∘ g = f ∘ h, then g = h

  • 151.
  • 152.
  • 153.

    Injections in code User( firstName= "Bob", lastName = "Smith", age = 73) User JSON { “firstName”: "Bob", “lastName”: ”Smith”, “age”: 73 }

  • 154.
  • 155.
  • 156.
  • 157.
  • 158.
  • 159.

    A B C Ifg ∘ f = h ∘ f, then g = h f g h

  • 160.

    A B C fg h If g ∘ f = h ∘ f, then g = h

  • 161.

    A B C fg h If g ∘ f = h ∘ f, then g = h

  • 162.
  • 163.
  • 164.
  • 165.
  • 166.
  • 167.
  • 168.
  • 169.
  • 170.
  • 171.
  • 172.
  • 173.
  • 174.
  • 175.
  • 176.
  • 177.
  • 178.
  • 179.
  • 180.
  • 181.
  • 182.
  • 183.
  • 184.
  • 185.

    It is essentialto understand how information is preserved in flows like this

  • 186.
  • 187.
  • 188.
  • 189.
  • 190.
  • 191.
  • 192.

    Isomorphism is more interestingthan equality! Isomorphic types can be rewritten, optimised without error. Isomorphic mappings allow us to preserve information

  • 193.
  • 194.
  • 195.

    Further reading Awodey, “CategoryTheory” 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.

    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/