Haskell - Again!
I took interest in Functional Programming back in 2015, shortly after introduction to React, Redux, ImmutableJS and RxJS. Authors of these all claim to be in the realm of Functional Programming, which sounds somewhat not right to me because it is all JavaScript under the hood. I decided to dig in the headache giver of all - Haskell. But my angle of attack was not right and the concept of monad went in and out in the back of my head like X-Rays. I figured something was not right and decided to give up.
Six years later, it simply clicked. Hence, this post as a way to celebrate!
What is an application in Haskell
A Haskell application always has the return signature of IO ()
. That is, a monad of the unit
output. It does not mean the output is nothing but rather one same thing: the unit ()
. Meaning, the application shall terminate.
IO () <-> ()
-- Kleisli Category
Certain Haskell applications can be a transformation of environment into the IO ()
category or a state transition into the IO ()
category. Either way, respectively we should use:
runReaderT r m a
runStateT s m a
whereas m a
is IO ()
The main business or objective of main :: IO ()
is to convert all code paths to IO ()
. StateT
or ReaderT
or other Monad transformers should function likewise with respective runMonadT
. For example:
main :: IO ()
main =
runReaderT reader "tom.csv"
One thing to note is Haskell’s polymorphism. Where is runReaderT
defined? It is defined as runReaderT :: r -> ma
and due to polymorphism Haskell compiler knows m == IO
and a == ()
.
What’s up with Category Theory?
Six years ago, I should have learned…
One must accept that Haskell is a brainchild of a group of mathematicians, the expression of their programming worldview. Therefore, to understand their line of reasoning, one must walk their path from the beginning.
The [in]famous quote
A monad is just a monoid in the category of endofunctors, what's the problem?
makes sense to me immediately after I took about 2 weeks to finish of the first series on Category Theory by Dr.Bartosz Milewski [https://youtube.com/playlist?list=PLbgaMIhjbmEnaH_LTkxLI7FMa2HsnawM_]. Take your time! Clear your mind of any precepts you have had regarding programming. Sip some coffee.
To appreciate Haskell, one must have a minimal knowledge of Category Theory. To learn Haskell is a practical way to maintain your interest in learning Category Theory.