Category

  1. Collection of objects (e.g. A, B, C, …)
  1. Collection of arrows (morphisms) between the objects (f : A → B, g : B → C)
  1. Composition of arrows (e.g. h = g o f : A → C)
Under the following rules:
  1. f o (g o h) = (f o g) o h
    (The composition of morphisms needs to be associative)
  1. f o idA = f = idB o f
    (The identity morphism/Unit)
Where idX is the identity function (idX : X → X) so that idX(x) = x, where x ∈ X.

Here is a list of other group-like structures and their properties:

Monoids

A monoid is an (abstract) algebraic structure. Monoids (and other algebraic structures in general) are useful because they share some set of properties, which then programmers can take for granted.

Let’s give a mathematical definition of what monoids represent, and then we will show some examples.

So, a monoid is basically consisted of a set S, along with a binary operation • (note: this doesn’t have to mean multiplication specifically, it can be any operation), so that the following properties are fulfilled:

1. Closure
(∀a,b ∈ S): a•b ∈ S
This basically means that the operation • belongs in the same set as the arguments a and b.

2. Associativity
(∀a,b,c ∈ S): (a•b)•c = a•(b•c)
This means that the order of evaluating expressions will not matter.

3. Identity
(∃e∀a): a•e = e•a = a

For example, the set of natural numbers ℕ together with the binary operation + (standard addition) make up a monoid. Let’s see how:

1. Closure – Every addition of two natural numbers is also a natural number.
2. Associativity – Order of addition will not change the result, i.e. (a+b)+c = a+(b+c)
3. Identity – The identity element of this specific monoid is e=0, because we have that any natural number added to 0, or 0 added to any natural number will be the natural number itself.

Articles worth reading

Lockhart’s Lament (General):
http://www.maa.org/sites/default/files/pdf/devlin/LockhartsLament.pdf

Why Haskell (Haskell):
http://scienceblogs.com/goodmath/2006/11/26/why-haskell/

Category Theory and Haskell (Haskell):
http://www.alpheccar.org/content/74.html (Part I)
http://www.alpheccar.org/content/76.html (Part II)
http://www.alpheccar.org/content/77.html (Part III)

Brian Beckman: Don’t fear the Monad (Haskell):
http://www.youtube.com/watch?v=ZhuHCtR3xq8

Monad Laws (Haskell):
http://www.haskell.org/haskellwiki/Monad_laws

Understanding F-Algebras (Haskell/Mathematics):
https://www.fpcomplete.com/user/bartosz/understanding-algebras

Functors, Applicatives, And Monads In Pictures (Haskell):
http://adit.io/posts/2013-04-17-functors,_applicatives,_and_monads_in_pictures.html

Oh my laziness! (Haskell):
http://alpmestan.com/2013/10/02/oh-my-laziness/

Haskell/Syntatic sugar (Haskell):
http://en.wikibooks.org/wiki/Haskell/Syntactic_sugar

What is Lambda Calculus and should you care? (Mathematics):
http://zeroturnaround.com/rebellabs/what-is-lambda-calculus-and-why-should-you-care/

Structural induction

Structural induction is a generalization of mathematical induction.

To see how, consider the definition of naturals (which mathematical induction is based on):

Inductive nat : Set :=
  | O => nat
  | S => nat -> nat.

So, generally what we do is prove the base case P(O), and then prove the recursive step P(S (S n)) by assuming P(n).

Structural induction can be applied more generally, say lists.

Inductive natlist : Set :=
  | nil  => natlist
  | cons => nat -> natlist -> natlist.

In this case we prove the base case P(nil), and then prove the recursive step P(l') by assuming P(l), where l' = cons(x, l).

So as an example, let’s try to prove that map(id) = id.

First, let’s look at the definitions for map and id:

map _ []     = []
map f (x:xs) = f x : map f xs
id x         = x

We use induction on x.

Base case: map(id, []) = [] = id([])

Inductive step: Assume that map(id, xs) = id(xs), and prove that map(id, (x:xs)) = id(x:xs).

map(id, (x:xs)) = id(x) : map(id, xs) = I.H. = id(x) : id(xs) = x : xs = id(x : xs).

Eta-reduction: map(id) = id.

Q.E.D.

Prime check

Here is a tiny proof, when doing primality test, why it is enough to check divisors only up to sqrt(n) instead of n:

Let n divide q.

If n < sqrt(q) < q
then n < sqrt(q) < q/n < q

q/n < q is trivial

sqrt(q) < q/n
1/sqrt(q) < 1/n

sqrt(q) > n

Alternatively

If sqrt(q) < n < q
then q/n < sqrt(q) < n < q

sqrt(q) < n
sqrt(q) > q/n

Implementation of the algorithm in Haskell:

primetest :: Int -> Bool
primetest x = primetest' x $ (ceiling.sqrt.fromIntegral) x
where
primetest' 1 _ = False
primetest' x 1 = True
primetest' x y = if x `mod` y == 0 then False else primetest' x (y - 1)

The part where I do (ceiling.sqrt.fromIntegral) may look magical, but what it does is:
1. Convert x from integer to something that sqrt accepts
2. Calculate the sqrt of it
3. Convert it back to integer