Category: The Essence of Composition
A category consists of objects and arrows that go between them. If you have an arrow from object π΄ to object π΅, and another arrow from object π΅ to object πΆ, then there must be an arrow β their composition β that goes from π΄ to πΆ.
->
= morphism
val f: A => B
val g: B => C
g compose f
::
in Haskell means βhas the type ofβ
Composition
Composition is associative
val f : A =>B
val g: B =>C
val h: C =>D
h compose (g compose f) === (h compose g) compose f === h compose g compose f
Identity:
π β idπ΄ = π
idπ΅ β π = π
def identity[A](a: A): A = a
f compose identity[A] == f
identity[B] _ compose f == f
A category consists of objects and arrows (morphisms). Arrows can be composed, and the composition is associative. Every object has an identity arrow that serves as a unit under composition. An object in category theory is an abstract nebulous entity. All you can ever know about it is how it relates to other objects β how it connects with them using arrows.
Types
val x: BigInt
BigInt
is an infinite set, of which x is a part of.
There is a type called bottom ( |, or Unicode β₯), which signifies that a function may not terminate. Functions that may return bottom, are called partial (as opposed to total functions).
val fact = (n: Int) => (1 to n).toList.product
vs C
int fact(int n) {
int i;
int result = 1;
for (i = 2; i <= n; ++i)
result *= i;
return result;
}
Much closer to the math definition!
A mathematical function is just a mapping of values to values. functions that always produce the same result given the same input and have no side effects are called pure functions.
You can even build functions that take an empty set (Set as discussed before):
def absurd[A]: Nothing => A
This will never be able to be called though, as you would need to provide something with the type Nothing as an argument.
This different to taking no arguments or ()
/ Unit
val f44: Unit => BigInt = _ => 44
val fInt: BigInt => Unit = _ => ()
Notice that this function doesnβt depend on the input, not even itβs type. Functions that can be implemented with the same formula for any type are called parametrically polymorphic.
sealed trait Bool
final case object True extends Bool
final case object False extends Bool
This is an ADT, Bool can be either True or False. Functions to Bool are called predicates
Categories Great and Small
Detour - what is a graph?
A graph is the set of values input->output of a function.
y = x * 2
Graph: |Input|Output| |-|-| |0|0| |1|2| |2|4| This can also be plotted to a graph, therefore the name.