Category Theory and Programming

Category theory is providing the abstract of the mathematics. Category theory formalize mathematics structure. Category theory is used to develop common language between mathematics.

In category theory, object and arrows (or morphisms) between objects. For each object there is a identity morphism and object arrows/functions/morphisms can be composed in associative fashion

For example: A — f() –> B — g() –> C then A — f().g() –> C here A, B, C are objects and strange arrays are morphims. This is showing that the object A when given morphism gives B.

In real world these can be set of Animal such as Cat, Dog, Elephants and when function is provided they gives there classes such as Carnivorous

Category theory is associative and identity. (h.f).g = h.(f.g) and f().1a = A

Category theory emphasis more on morphism. There are different types of morphisms. I had not deep dive into type of morphisms in category theory however when learning category theory with programming, I came across functors and monad most.

Functor is the structure preserving morphism often called as homophorism. These are the mapping between two categories.

In category theory, Monad and Monoid are same. They are also called endofunctor. Endofunctor are the category object morphism back to same category. The good real world example can be clock. Monad and Monoid in category theory are same however, they are different in programming because they operate at different level of abstraction.

Functor

Functor in programming implements a function called map.

Example of functor in Scala is List[A] => List[B] or more concretely List[Int] => List[String]

List(1, 2, 3).map((x: Int) => x.toString) 
trait Functor[F[_]] {
  def map[A, B](f: A => B): F[B]
}

This category in programming help programmer to write code in more abstract way.

Monad and Monoid in Programming

In scala flatMap is a monad. It is defined has M X M => M or 1 X M => M which means that the object maps to itself when compose with a function.

trait Monad[T]
{
    def flatMap(f: T => Monad[S]): Monad[S]
}

Monad are very useful in performing IO operation for Haskell programming where there are no mutable data types, these are called IO Monad. Monad are also used in Optional value for protecting from null exceptions, these are called Optional Monad.

This video is worth watching for Monad/Monoid By Brain Beckman Don’t fear the Monad

Reference

https://en.wikipedia.org/wiki/Functor
https://en.wikipedia.org/wiki/Category_theory
https://www.quora.com/What-is-the-difference-between-monoid-and-monad
https://math.stackexchange.com/questions/312605/what-is-category-theory-useful-for

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: