The difference between function application and bind in Haskell

If you have read my blog posts so far, you will have understood the difference between normal function application in Haskell and the monadic bind (>>=).

What I essentially talk about, is the difference between

f x

and

x >>= f

You cannot use one in place of the other.

The first, f x, means that we have a function f of type a -> b and an object x of type a. We apply the function f to object x and we get an object f x of type b.

The second, x >>= f, means  that we have a type constructor turned into a Monad. Let us call our type constructor/Monad m. We have an object x of type m a and a function f of type a -> m b. x >>= f means that we “apply” the function f to object x. Notice that I wrote the word “apply” in quotes. I did that because this is not normal function application. We “force” the object of type m a as input to a function f that takes not an object of type m a but an object of type a. This is why we need to use >>=, instead of normal function application. >>= has been programmed to take an object of type m a and a function of type a -> m b and return an object of type m b.

Thus, when we see something like f x, we should think of a normal object x of type a and a normal function f :: a -> b. And we know that f x is of (normal) type b.

But when we see something like x >>= f, we should think of a type constructor/Monad m, of an elevated object x of type m a and of a function f :: a -> m b that takes a normal object of type a and returns an elevated object of type m b. And we know that x >>= f is of (elevated) type m b.

Advertisements

About Dimitrios Kalemis

I am a systems engineer specializing in Microsoft products and technologies. I am also an author. Please visit my blog to see the blog posts I have written, the books I have written and the applications I have created. I definitely recommend my blog posts under the category "Management", all my books and all my applications. I believe that you will find them interesting and useful. I am in the process of writing more blog posts and books, so please visit my blog from time to time to see what I come up with next. I am also active on other sites; links to those you can find in the "About me" page of my blog.
This entry was posted in Development. Bookmark the permalink.