class: title, center, middle LINE Haskell Boot Camp .title[ # The Monad Fear ] _The Shibuya Camp_
Finale --- - This slide deck was the visual aid for [XT](https://xtendo.org)'s presentation in the [LINE Haskell Boot Camp](https://engineering.linecorp.com/en/blog/detail/112) that took place in Shibuya, Tokyo, on October 28th, 2016. - The latest version of the deck is available at:
--- ## Fear, Uncertainty, and Doubt - Haskell is difficult to learn - Monad is difficult, and therefore Haskell is difficult - You need to understand monad in order to use Haskell - You need to understand monad in order to use _practical_ Haskell - Haskell is a mathematical language - Anyways you should learn monad -- **None** of the above is true. --- ## Not my original idea. - Dan Piponi, _[The IO Monad for People who Simply Don't Care](http://blog.sigfpe.com/2007/11/io-monad-for-people-who-simply-dont.html)_ (2007) - Brent Yorgey, _[Abstraction, intuition, and the “monad tutorial fallacy”](https://byorgey.wordpress.com/2009/01/12/abstraction-intuition-and-the-monad-tutorial-fallacy/)_ (2009) - tora, _[My how to give a 'monad tutorial' tutorial](http://www.doc.ic.ac.uk/~tora/monadsI.txt)_ (2010) - scottw, _[Why I won't be writing a monad tutorial](http://fsharpforfunandprofit.com/posts/why-i-wont-be-writing-a-monad-tutorial/)_ (2013) - Travis Hance, _[Write you a monad tutorial](https://tjhance.quora.com/Write-you-a-monad-tutorial)_ (2014) - ~kqr, _[The "What Are Monads?" Fallacy](http://two-wrongs.com/the-what-are-monads-fallacy)_ (2015) - Justin Le, _[IO Monad Considered Harmful](http://blog.jle.im/entry/io-monad-considered-harmful)_ (2015) The community began to recognize the problem: "Why do so many beginning Haskellers ask the same (wrong) question and go through the same (unnecessary) difficulties?" --- ## I've been organizing the South Korean Haskellers' group for nearly a year The two most frequently asked question from the newcomers: --- class: center, middle ## 1. If Haskell is so great, why's nobody using it? --- class: center, middle ## 2. What the hell is monad? --- ## Pedagogical catastrophe - Ask the Internet, "What is Haskell?" - Purely functional - Lazy - Uses monads - Type classes - etc. -- - In short, things that newcomers can't possibly have a clue of. -- - **Using things you don't know in order to teach you something you don't know** -- - ?! -- - It would be a miracle if there's no massive frustration. --- class: center, middle ## Why was Haskell created? --- ## Haskell 2010 Report ### Preface [...] #### Goals > 1. It should be suitable for teaching, research, and **applications, including building large systems**. -- - Explicitly stated goal of usefulness in **application development**. --- ## How the Haskell Committee happened -- - It is 1987. Non-strict purely functional languages are flooding. -- - Too many choices, redundant independent efforts. None is chosen in the market. -- - Purely functional seems to be a good idea, and it's a pity it's not used. Let's unite and create **a language that gets chosen**. -- - In other words, **Haskell was not meant to be academia-only**. -- - It was **designed to be used in real-life**. --- ## Haskell: A practical tool that solves real-world problems - A web server with higher performance than the ones written in C - Standard Chartered: "real time pricing, risk management, data analysis, regulatory systems, desktop and web GUIs, web services, algo pricing, end-user scripting" - Facebook's data access tools and antispam tools - Haskell in finances: ABN AMRO, Bank of America, Barclays, Credit Suisse, Deutsche Bank, Tsuru Capital --- class: center, middle FUD ## Haskell's I/O is
somehow special/weird/difficult --- ## Haskell ```no-highlight main = do putStrLn "Hello. What is your name?" x <- getLine putStrLn ("The input was " ++ x) ``` --- ## Python ```no-highlight if __name__ == '__main__': print ('Hello. What is your name?') x = input() print ('The input was ' + x) ``` --- ## Do the I/O in Haskell. It works. - There is ***NOTHING*** special about Haskell's I/O. - Do it as you did in other (imperative) languages. - **Deliberately imperative “look and feel”** - Simon Peyton Jones, _[Wearing the hair shirt: a retrospective on Haskell](http://research.microsoft.com/en-us/um/people/simonpj/papers/haskell-retrospective/)_ (2003) - We intentionally made it usable imperatively. - The misconception that Haskell's I/O is difficult/special/weird does NOT come from people who actually tried it. - One phrase caused all tragedy: --- class: center, middle, large ## IO monad --- ## IO Monad Considered Harmful Justin Le: > **The phrase “IO monad” considered harmful. Please do not use it**. > > ... > > I’m going to say that this is probably **the single most harmful and damaging thing** in Haskell ... -- ?! --- ## Unix commands ```no-highlight echo STRING cat FILENAME rm FILENAME mkdir DIRNAME ``` - `echo` and `cat` are **commands**, - `STRING` and `FILENAME` are **strings**, which are **argument for commands**. - **command** and **string** are different **types**. --- ## Unix pipe ```bash find . | grep "\.txt$" tail -f app.log | grep "^system:" > output.txt ``` - You want to feed the result output of one command as an input to another command. --- ## Haskell's command: action ```haskell putStrLn :: String -> IO () readFile :: FilePath -> IO String main = do x <- readFile "myname.txt" putStrLn ("Hello, " ++ x ++ "!") ``` - Just as `echo` is a command that takes one string as its argument, `putStrLn` is an action that takes one `String`. - Just as `cat` takes a file path as its argument and prints its content, `readFile` is an action that takes a `FilePath` and returns its content as a `String`. - Actions and `String`s are different types. - Actions and the `++` operator are also different types. --- ## Haskell's pipe - Haskell's pipe looks like this: `>>=` ```haskell main = readFile "myname.txt" >>= putStrLn ``` - Feed the output of one command as an input of another command. --- ## Haskell distinguishes pure functions and actions by their types ```haskell -- pure function not :: Bool -> Bool (++) :: [a] -> [a] -> [a] -- IO action getLine :: IO String putStrLn :: String -> IO () readFile :: FilePath -> IO String ``` --- ## Action is easy. Then why? - Just call them "`IO` action" or "`IO` type", and explain they are similar to Unix commands, and there will be nothing more to teach. - But we call them IO _monads_ and cause this chaos. - Bewildering number of beginners are asking "what is monad?" first. (Because when programmers see a strange jargon, they just have to understand it. It's their instinct) - "How do I deal with a sequence of multiple numbers in Haskell?" - "Use `List`." or "Use the `List` type." - No one replies with "Use the `List` monad" unless they're pure evil! - Therefore the answer to "How do I do the input/output in Haskell" should also be: - "Use the `IO` type." --- class: center, middle > Our biggest mistake: Using the scary term “monad” rather than “warm fuzzy thing” > > -- Simon Peyton Jones, _[Wearing the hair shirt: a retrospective on Haskell](http://research.microsoft.com/en-us/um/people/simonpj/papers/haskell-retrospective/)_ (2003) --- ## Historical background Since when did Haskell have monads? -- - 1987: The Committee is formed. They chose Miranda as the basis. -- No monads. -- - 1990: The Haskell 1.0 report is published. -- No monads. -- - 1991: The Haskell 1.1 report is published. -- No monads. -- - 1992: The Haskell 1.2 report is published. -- No monads. -- - 1996: The Haskell 1.3 report is published. -- **Monadic I/O is introduced**. -- - Monadic I/O has been in use in the community first. (?!) - Haskell 1.3, reflecting such community status, completely abolished the conventional `[Response] -> [Request]` I/O and introduced monadic I/O and the `do` syntax. --- The term "monadic I/O" or "IO monad" was originally made to distinguish the new concept from the old I/O. The term became a new jargon, and the tragedy began. --- ## Downfall - Haskell's I/O can be done without monads, and without knowing monads. - You can remove the type class `Monad` from Haskell and still do the I/O. - Because monad is no more than interface. You only need the `>>=` function (and optionally the `do` notation) redefined I/O only. - You can remove **the type class itself** and still do the I/O. Because type class is no more than interface. - But we call them "`IO` monad" and people immediately look up "monad" first. - If we called them `IO` type, this step could have been completely skipped. - The community is encouraging the yak shaving, or a "sidequest." --- class: center, middle ## "To use Haskell one must understand monad" --- ## JavaScript ```javascript >>> ['10', '10', '10'].map(parseInt) ``` -- ```javascript [10, NaN, 2] ``` -- Just as you don't need to know JavaScript to use JavaScript
You don't need to understand monad to use Haskell --- class: center ## All code of `do` notation is translated into monad.
Doesn't that mean monad is a required concept? -- Sounds convincing at a glance... --- class: center ## All C/C++/D/Rust/Go code is translated to Assembly.
Therefore, to learn C/C++/D/Rust/Go,
One must learn Assembly first. -- ... But now you see the problem. --- ## Counting (Source: [Why I won't be writing a monad tutorial](http://fsharpforfunandprofit.com/posts/why-i-wont-be-writing-a-monad-tutorial/) by scottw.) --- ## What is fruit? - When you're trying to explain "What is fruit?" to someone who doesn't know what fruit is: - Apple is fruit. Orange is fruit. Pear is fruit. - ... - Fruit is the seed-bearing structure in angiosperms formed from the ovary after flowering, normally as a means of botanical reproduction, often edible and juicy with the sweet or sour taste. - You don't do this. - It is a common intuitive pedagogy to derive abstract concepts from concrete examples. --- ## Even if you do understand... - Understanding the concept called monad, and actually using monadic types (`IO`, `ST`, `[]`, `Maybe`, ...) are a completely different matter. - Monad in category theory and monad in Haskell are, again, different. -- > Attempting to learn how to use monads by understanding what they are is like asking "What is a musical instrument?" and then assuming once you know the answer to that, you'll be able to play all of the musical instruments.
> ~kqr, _[The "What Are Monads?" Fallacy](http://two-wrongs.com/the-what-are-monads-fallacy)_ (2015) --- ## So this is what happens -- - Beginners believe they must understand monad. -- - They can't. -- (Most people drop out here) -- - They continue to struggle to understand monad while writing codes in Haskell -- - At some point they do understand what monad is -- - Then everything feels easy -- - They ought to spread this Awakening widely and redeem the people -- - Another monad tutorial emerges -- (there can never be enough) --- ## Monad tutorials -- > A 'newbie', in Haskell, is someone who hasn't yet implemented a compiler. They've only written a monad tutorial.
> [Pseudonym](http://sequence.complete.org/hwn/20061122) -- - Tutorial on how to write a monad tutorial - tora, _[My how to give a 'monad tutorial' tutorial](http://www.doc.ic.ac.uk/~tora/monadsI.txt)_ (2010) - Travis Hance, _[Write you a monad tutorial](https://tjhance.quora.com/Write-you-a-monad-tutorial)_ (2014) --- ## Monad is - Monad is a **type class**. - A type class is a set of **types**. - A type is a set of **values**. - In Haskell, monad is a shared interface to multiple types that share something in common. - Therefore, even when a type is monadic, they may look totally unrelated to other monadic types. --- ## Therefore, to learn monad -- - First use `IO` type. -- - and `Maybe` type. -- - and `List` type as well. -- - Write enough Haskell code. (\>1k lines?) -- - Then all of a sudden you realize what they have in common. (Aha moment) -- - It doesn't matter you still don't get it. Keep on. -- - **Never attempt to consciously learn what monad is.** -- It would only lead to frustration and confusion. --- class: center, middle ## "Well, I did try Haskell, but that monad thing..." --- class: center, middle ## No!
😄
Don't let monad stop your adventure into Haskell! --- ## Thank you. -
- GitHub: [@xtendo-org](https://github.com/xtendo-org) - Twitter: [@xtendo_org](https://twitter.com/xtendo_org)