I am learning the Haskell programming language. From what I am reading, Input/Ouput (IO) raises challenges for Haskell's purity, since by definition we are interacting with the outside world. From Wikipedia:
In a purely functional language, such as Haskell, functions cannot have any externally visible side effects as part of the function semantics. Although a function cannot directly cause a side effect, it can construct a value describing a desired side effect, that the caller should apply at a convenient time.
In the Haskell notation, a value of type IO a represents an action that, when performed, produces a value of type a.
Soon, I learned that IO is an example of a Haskell monad. Although we don't get a lot of explanation of what monads are. From Functors, Applicatives, And Monads In Pictures
How to learn about Monads:
- Get a PhD in computer science.
- Throw it away because you don’t need it for this section!
By now, I have read various definitions of - that they add context or create smaller programming languages inside a big one around a specific concept. Still trying to get a sense of what monads and how those ideas apply.
In Haskell, monad
is another typeclass, with basically only one rule defining it. And IO
is an instance of that.
class Monad m where
(>>=) :: m a -> (a -> m b) -> m b
https://stackoverflow.com/questions/44965/what-is-a-monad
Is there something algebraic going on here? What is so algebraic about IO?
I apologize for being language specific. My hope is that most of this discussion applies to all functional programming languages. Any errors in my discussion represent my own limited understanding of this area.
There is another definition of monad I found in nLab that is not even specific to programming languages.
In a separate question, I would like to understand how the category theory notion of monad matches with the CS definition in the case of Haskell.