Implementation of a Monad with proof

The Probably Monad is same as the Maybe Monad, so this is how I had it defined:

data Probably x = Nope | Kinda x deriving (Show)

instance Monad Probably where
Kinda x >>= f = f x
Nope >>= f = Nope
return = Kinda

add x y = x >>= (\x -> y >>= (\y -> return (x + y)))
-- Example: add (add (Kinda 1) (Kinda 2)) (Kinda 4)

Proof:

1. Left identity:
return a >>= f ≡ f a
“return a” by definition is “Kinda a”, and so the bind operator is defined as “Kinda a >>= f ≡ f a”.
So with these two definitions, we have shown that “return a >>= f ≡ f a”.

2. Right identity:
m >>= return ≡ m
If we let m equal “Kinda n”, then we have “Kinda n >>= return ≡ Kinda n”, but by definition of the binding operator,
we have that “Kinda n >>= return = return n”, and so “return n”. So, by definition of return, we have “Kinda n”, which is m.

3. Associativity:
(m >>= f) >>= g ≡ m >>= (\x -> f x >>= g)
If we let m equal “Kinda n”, then we have:
LHS: (Kinda n >>= f) >>= g ≡ (f n) >>= g
RHS: Kinda n >>= (\x -> f x >>= g) ≡ (\x -> f x >>= g) n ≡ f n >>= g

fold on Monoids

I was looking at Wikipedia’s article regarding Monoids, and decided that it was interesting to make a post regarding the section “Monoids in Computer Science” http://en.wikipedia.org/wiki/Monoid#Monoids_in_computer_science.

So there they have posted this formula:

And let’s also take a look at Haskell’s definition for fold:

(>>>) = (++) 
fold []     = []
fold (m:l') = m >>> (fold l')

Notice the relation between the formulas? Just replace >>> with *, and there you have it!

Regarding the types, in Haskell we have:

*Main> :t fold
fold :: [[a]] -> [a]

And in Mathematics, we have:
fold: M* -> M

The star here represents Kleene’s star, and it is defined to be all concatenations on the elements of the set M, including the empty string (). Example, if M = {a, b} then M* = {(), (a), (b), (a,a), (a,b), (b,b), (b,a), (a,b,a), …}. So what this means is, that if we have M defined as list of integers, then M* would be list of list of integers (which is the result of list concatenation of the elements of M).

And what is fold? Well, fold basically takes a Monoid and applies its operation. Now that we have these two formulas, we can show how fold operates on a Monoid.

Let’s consider the Monoid M ([Int], ++, []). First, to prove that this triple makes a monoid, we must first take a look at the definition of ++:

[]     ++ ys = ys
(x:xs) ++ ys = x : (xs ++ ys) (*)

1. Closure
Namely, the (++) is the list concatenation operator, and when we concatenate two lists, we get list as an output, according to ++ definition.

2. Associativity
(xs ++ ys) ++ zs = xs ++ (ys ++ zs), order of evaluation will not change the resulting list
To prove this, we again need to look at ++ definition.

We can use Structural Induction in order to prove associativity.

Base case: If we let x = ys + zs, then [] ++ x = x by definition, LHS equals to x. and for the RHS, we get “” + ys = ys by definition, so ys ++ zs == x, and so LHS = RHS.

Inductive step: Assume that (xs ++ ys) ++ zs = xs ++ (ys ++ zs)

We need to prove that ((x:xs) ++ ys) ++ zs = (x:xs) ++ (ys ++ zs)

LHS: by (*) = (x : (xs ++ ys)) ++ zs => let xs ++ ys = p => x : p ++ zs = x : ((xs ++ ys) ++ zs)
RHS: by (*) = x : (xs ++ (ys ++ zs)) => by assumption => x : ((xs ++ ys) ++ zs)

So we get that LHS = RHS.

3. Identity
The empty list [] is the identity here, because [] ++ ys or ys ++ [] always equals to ys, according to ++ definition (already proved by base case in 2).

Now, we can start playing around with our fold:

*Main> fold [[1,2], [3]] -- per Monoid M
[1,2,3]
*Main> fold [['a'],['b'],['c']] -- same as Monoid M, but operates on set [Char] instead of [Int]
"abc"

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/