Monad Law Explanation in F #

Here is an explanation of the laws of the Monad in Haskell.

How to explain Monad laws in F #?

  • bind (M, return) is equivalent to M.

  • bind ((return x), f) is equivalent to f x.

  • bind (bind (m, f), g) is equivalent to bind (m, (fun x → bind (fx, g))).

+8
monads f #
source share
1 answer

I think a good way to understand them in F # is to look at what they mean using the syntax of the calculation expressions. I will write m for some compiler, but you can imagine that it is async or any other type of computation.

Left person

 m { let! x' = m { return x } = m { let x' = x return! fx' } return! fx' } 

Correct identity

 m { let! x = comp = m { return! comp } return x } 

Associativity

 m { let! x = comp = m { let! y = m { let! x = comp let! y = fx return! fx } return! gy } return! gy } 

Laws, in fact, tell you that you should be able to refactor one version of a program to another without changing the meaning - just like you can refactor regular programs in F #.

+11
source share

All Articles