# Roundcrisis

## More Computation expressions

06 Dec 2015

I guess I am one of those people that really enjoy this time of the year, so I had to add another post. This is a sneaky(?) second post for #FsAdvent. Ready for some more computation expressions?

In the previous post I introduced a definition and parts of a computation expression, as well as one of the simplest example.

### The beauty of reality

I started looking at computation expressions because I was trying to deal with the “C#-F# design mismatch”(™) (yes I just made discovered that up) meaning that I wanted to deal in some nice ways with the fact that when I am using a C# object it will occasionally be null and other fun things. And when I started reading about solutions to that, I found that there are some well known ways to deal with that.

Sometimes the world surprises you with something amazing, could be a moon rising in the night sky, or maybe someone having a nice gesture towards another fellow human just because. Perhaps there is something to this natural ordered chaos. And with ordered chaos in mind, a great example of usage of computation expressions is error handling.

So, lets see some code

``````
type MaybeBuilder() =
member __.Bind(maybeValue, func) =
match maybeValue with
| Some value -> func value
| None -> None
member __.Return value = Some value

let divide a b =
match b with
| 0 -> None
| _ -> Some(a / b)

let maybe = MaybeBuilder()

let divisionM a b c d =
maybe { let! x = divide a b
let! y = divide x c
let! z = divide y d
return z }

``````

Similar to the example in the previous post, we have a builder, in this case, `MaybeBuilder` and the signature for this function is :

Bind:: ‘a option * (‘a -> ‘b option) -> ‘b option

Bind takes a wrapped value (‘a option) and a function that takes an `'a` and returns a wrapped `'b` (‘b option) and returns a wrapped `'b`(‘b option). In F#, `'a option` is a generic option where ‘a can be any type. You can use most generic types as wrappers.

A great explanation on why `Bind` signature is relevant comes from this post. Go ahead and read it. I’ll just pretend you didn’t read it try to explain it and then you will think that it might be good to go and read it :D

What happens in `Bind` if `maybeValue` has a value

• Unwrap ‘a , execute the function
• Return a wrapped value (of type ‘b option)

What happens in `Bind` if `maybeValue` is nothing

• There is `None` so we do nothing.
• Return a None (which is still of type ‘b option )

The good thing is, because we return with signature `'b option` that means we can feed the result back into Bind if that is necessary.

This triple of a wrapped type, Bind and Return is called a monad, and that is cool because when you realise something is a monad, then that means you get some other stuff for free. To show you how, I’ll start talking about Monoids.

### Monoids

I am sure you have heard that

A monad is a monoid in the category of endofunctors.

When I read that first I have to admit that was not terribly useful. So I went to check out Monoid, and it turns out they are pretty cool :D

A monoid needs a type and an operation and it needs to follow the following rules:

1. Closure. The operation must have the following signature ‘a -> ‘a -> ‘a
2. identity. There must exist an instance I of ‘a such that ‘a . I = ‘a
3. Associativity. Given the same order, no matter how we associate the operations so (a . b). c = a .( b. c ) where a, b and c are instances of ‘a

examples For The Win!!

lets say we have a type `Colour` and an operation `addColour`

Does this constitute a monoid?

1. Closure: the function `addColour` has the signature: `val addColour : c1:Colour -> c2:Colour -> Colour` It looks like we do have closure.

2. Identity. There exist a colour, black, that if you add to any other colour, the result is the same colour.

It looks like we have an identity

1. Associativity. We can add three different colours and no matter which two we add in which order, the result is the same colour.

When we run that the result is:

` Ok, passed 100 tests. val it : unit = ()`

What happened here is that we randomly generated a, b and c to be 3 colours and we ran that test a hundred times. Verifying that in fact this pair is a monoid.

Knowing that something is a monoid can be really useful because you have certain properties that are true of monoids, for example:

• You can convert pair-wise operations to operations that apply to collections
• You can parallelise operations
• Incrementalism

The obvious next step now is to create monoids with computation expressions, and this is very cool because to implement them we need some of the other functions that we can use in a builder.

In here we are using some new members: Zero, Yield, Combine and For. Below are all the functions that can be used on builders,

(source)

### Custom Operations

and you can call this

this translates to:

I guess by now we are all a bit tired like these otters:

See you next time

Batmandrea

#### References

Categories:   programming   fsharp   monoids

Want to discuss this post? the best place right now for me is mastodon, please message me @roundcrisis@types.pl with your comment or question.