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