The Monadic Way
From HaskellWiki
(Added output of the monadic evaluator and corrected some wikitext bugs) 
BrettGiles (Talk  contribs) (Links) 

(24 intermediate revisions by 4 users not shown)  
Line 1:  Line 1:  
−  ==An evaluation of Philip Wadler's "Monads for functional programming"== 
+  '''Note''' Since the size of the previous file was getting too big for a wiki, the tutorial has been divided into two parts: [[The Monadic Way Part I]] and [[The Monadic Way Part II]]. See below for some introductory remarks. 
−  This tutorial is a "translation" of Philip Welder's "Monads for 
+  '''Contents''' 
−  functional programming". 

−  (avail. from [http://homepages.inf.ed.ac.uk/wadler/topics/monads.html here]) 

−  I'm a Haskell newbie trying to grasp such a difficult concept as the 
+  ;[[Meet Bob The Monadic Lover]] 
−  ones of Monad and monadic computation. 
+  :A (supposedtobe) funny and short introduction to Monads, with code but without any reference to category theory: what monads look like and what they are useful for, from the perspective of a ... lover. It could be an introduction to "The Monadic Way" tutorial. 
−  While [http://www.cs.utah.edu/~hal/htut/ "Yet Another Haskell Tutorial"] 

−  gave me a good understanding of the type system when it 

−  comes to monads I find it almost unreadable. 

−  But I had also Welder's paper, and started reading it. Well, just 
+  ;[[The Monadic Way/Part I]] 
−  wonderful! It explains how to ''create'' a monad! 
+  :In the first part of the tutorial we will start from a very simple evaluator that will be transformed into a monadic evaluator with an increasing number of features: output, exceptions, and state: a very simple counter for tracking the number of recursions of the evaluation precess. 
−  So I decided to "translate it", in order to clarify to myself the 
+  ;[[The Monadic Way/Part II]] 
−  topic. And I'm now sharing this traslation (not completed yet) with 
+  :In the second part of the tutorial we will see how to take complexity out of our monadic evaluator with the use of monadic transformers, and specifically StateT. This part is just a skeleton, since, for the time being, it contains only the code. 
−  the hope it will be useful to someone else. 

−  Moreover, that's a wiki, so please improve it. And, specifically, 

−  correct my poor English. I'm Italian, afterall. 

−  ==A Simple Evaluator== 
+  ==Preliminary remarks== 
−  Let's start with something simple: suppose we want to implement a new 
+  When I started writing this tutorial I though that the only way to 
−  programming language. We just finished with 
+  explain monads to a newcomer was to show them from the inside, with 
−  [http://swiss.csail.mit.edu/classes/6.001/abelsonsussmanlectures/ Abelson and Sussman's Structure and Interpretation of ComputerPrograms] 
+  the use of lambda abstraction. Not only because this is the way Philip 
−  and we want to test what we have learned. 
+  Wedler's 
+  [http://homepages.inf.ed.ac.uk/wadler/papers/marktoberdorf/baastad.pdf paper] 

+  adopts, but also because I believed, and still believe, that the 

+  only way to understand what bind (>>=) does is to explain it as a 

+  function that takes a monad and an anonymous function. 

−  Our programming language will be very simple: it will just compute the 
+  I had this feeling because I am a newcomer, and this is the way I came to understand monads. 
−  sum operation. 

−  So we have just one primitive operation (Add) that takes two constants 
+  I did not received very much feedback for this tutorial, and I must 
−  and calculates their sum 
+  admit that I would like to. But one person, on the haskellcafe 
+  mailing list, [http://www.haskell.org/pipermail/haskellcafe/2006September/017740.html told me] 

+  that: 

+  <pre> 

+  imho your tutorial makes the error that is a very typical: when you 

+  write your tutorial you already know what are monads and what the 

+  program you will construct at the end. but your reader don't know all these! 

+  for such fresh reader this looks as you made some strange steps, write 

+  some ugly code and he doesn't have chances to understand that this ugly 

+  code is written just to show that this can be simplified by using monads. 

+  </pre> 

−  For instance, something like: 
+  I believe that Bulat is right. In this tutorial you go through some 
+  code that is '''really''' ugly and then, with some kind of magic, it 

+  turns out in the (redundant but) clean evaluator of the end of [[The Monadic Way/Part II]]. 

−  (Add (Con 5) (Con 6)) 
+  I took that mail as a challenge and 
+  [http://www.haskell.org/pipermail/haskellcafe/2006September/017778.html I responded] 

+  by writing [[Meet Bob The Monadic Lover]]. 

−  should yeld: 
+  In "Meet Bob" the code is clean, variable names are very descriptive, 
+  and you see that I can create a monad without any use of lambda 

+  abstraction. 

−  11 
+  Bind (in "Meet Bob" is askLover) now takes a monad and a partial 
+  application, not an anonymous function. 

−  We will implement our language with the help of a data type 
+  Obviously you can see an anonymous function as a partial application. 
−  constructor such as: 

−  <haskell> 
+  The problem, I think, is that, in "Meet Bob", you cannot understand 
+  the strict relation between what I did before and what I do when I 

+  start using the "donotation". You see that the same functions are 

+  being used ("tellMyself" and "newLove"), but "andrea < tellMyself 1" 

+  is not explained. It's just magic. 

−  module MyMonads where 
+  The fact is that you cannot understand "andrea < tellMyself 1" 
−  data Term = Con Int 
+  without the use of lambda abstraction. 
−   Add Term Term 

−  deriving (Show) 

−  
−  </haskell> 

−  
−  After that we build our interpreter: 

+  I should have written an intermediate step, something like this: 

<haskell> 
<haskell> 

−  +  drunk = do newLove "Paula " >> 

−  eval :: Term > Int 
+  (tellLover 1 10) >>= \paula > 
−  eval (Con a) = a 
+  (tellMyself 3) >>= \lorena > tellMyself (paula + lorena) 
−  eval (Add a b) = eval a + eval b 

</haskell> 
</haskell> 

−  That's it. Just an example: 
+  With this approach I think you can understand '''why and how''' you 
−  +  come to write something like this: 

−  *MyMonads> eval (Add (Con 5) (Con 6)) 

−  11 

−  *MyMonads> 

−  
−  Very very simple. The evaluator checks if its argument is a Cons: if 

−  it is it just returns it. 

−  
−  If it's not a Cons, but it is a Term, it evaluates the right one and 

−  sums the result with the result of the evaluation of the second term. 

−  
−  == Some Output, Please!== 

−  
−  Now, that's fine, but we'd like to add some features, like providing 

−  some output, to show how the computation was carried out. 

−  Well, but Haskell is a pure functional language, with no side effects, 

−  we were told. 

−  
−  Now we seem to be wanting to create a side effect of the computation, 

−  its output, and be able to stare at it... 

−  If we had some global variable to store the out that would be 

−  simple... 

−  
−  But we can create the output and carry it along the computation, 

−  concatenating it with the old one, and present it at the end of the 

−  evaluation together with the evaluation of the expression! 

−  
−  Simple and neat! 

−  
<haskell> 
<haskell> 

−  +  drunk = do newLove "Paula " 

−  type MOut a = (a, Output) 
+  paula < (tellLover 1 10) 
−  type Output = String 
+  lorena < tellMyself 3 
−  +  tellMyself (paula + lorena) 

−  formatLine :: Term > Int > Output 

−  formatLine t a = "eval (" ++ show t ++ ") <= " ++ show a ++ "  " 

−  
−  evalO :: Term > MOut Int 

−  evalO (Con a) = (a, formatLine (Con a) a) 

−  evalO (Add t u) = ((a + b),(x ++ y ++ formatLine (Add t u) (a + b))) 

−  where (a, x) = evalO t 

−  (b, y) = evalO u 

−  
</haskell> 
</haskell> 

−  Now we have what we want. But we had to change our evaluator quite a 
+  That is to say, in this way you can see a do block as a series of 
−  bit. First we added a function, that takes a Term (of the expression 
+  nested anonymous functions whose arguments are bound to some value by 
−  to be evaluated), an Int (the result of the evaluation) and gives back 
+  the application of >>=. Anonymous functions that bind to some value 
−  an output of type Output (that is a synonymous of String). 
+  the variables appearing after the ">" ("paula" and "lorena"). 
−  The evaluator changed quite a lot! Now it has a different type: it 
+  To summarize, I think that even if you can start using monads without 
−  takes a Term data type and produces a new type, we called MOut, that 
+  understanding that what happens inside a "do" block is strictly 
−  is actually a pair of a variable type a (an Int in our evaluator) and 
+  related with lambda calculus, I don't think you can claim you 
−  a type Output, a string. 
+  understand monads just because you are using them. 
−  So our evaluator, now, will take a Term (the type of the expressions 
+  And I'm quite sure that if a problem arises somewhere you can have a 
−  in our new programming language) and will produce a pair, composed of 
+  very difficult time in trying to find out what the problem is. This is 
−  the result of the evaluation (an Int) and the Output, a string. 
+  even more true when you start doing monadic transformations. 
−  So far so good. But what's happening inside the evaluator? 
+  ==How to explain monads to newcomers?== 
−  The first part will just return a pair with the number evaluated and 
+  Monads are not an impossibletounderstandunlessyouhaveaphd 
−  the output formatted by formatLine. 
+  topic. I don't know if I can claim I'm a living proof of this 
+  assumption, since I have a PhD. But please take into account that I 

+  have a PhD in Comparative Private Law! Nothing related to computer 

+  science. And I claim I understand monads. 

−  The second part does something more complicated: it returns a pair 
+  Moreover, since I have come to understand them, I think I can try to 
−  composed by 
+  explain them to newcomers like me. This is why I started writing this 
−  1. the result of the evaluation of the right Term summed to the result 
+  tutorial. 
−  of the evaluation of the second Term 

−  2. the output: the concatenation of the output produced by the 

−  evaluation of the right Term, the output produced by the evaluation of 

−  the left Term (each this evaluation returns a pair with the number and 

−  the output), and the formatted output of the evaluation. 

−  Let's try it: 
+  Still, in order to understand them I studied, and I studied hard. I 
−  *MyMonads> evalO (Add (Con 5) (Con 6)) 
+  wanted to understand, it was difficult, and I kept studying. Going 
−  (11,"eval (Con 5) <= 5  eval (Con 6) <= 6  eval (Add (Con 5) (Con 6)) <= 11  ") 
+  back to the foundation when needed. 
−  *MyMonads> 

−  It works! Let's put the output this way: 
+  So, I cannot explain monads to you unless you are willing to study. I can 
−  eval (Con 5) <= 5  
+  show you the way, but you must take your time and follow that way. 
−  eval (Con 6) <= 6  

−  eval (Add (Con 5) (Con 6)) <= 11  

−  Great! We are able to produce a side effect of our evaluation and 
+  ==What do I need to know to understand monads?== 
−  present it at the end of the computation, after all. 

−  Let's have a closer look at this expression: 
+  ===Functional programming=== 
−  <haskell> 

−  evalO (Add t u) = ((a + b),(x ++ y ++ formatLine (Add t u) (a + b))) 
+  First you need at least a basic understanding of functional 
−  where (a, x) = evalO t 
+  programming. 
−  (b, y) = evalO u 

−  </haskell> 
+  This is going to be the easiest step, because there is an invaluable 
+  resource available on line for free. 

−  Why all that? The problem is that we need "a" and "b" to calculate their 
+  This is what I did: I spent 20 hours of my life watching the Video 
−  sum, together with the output coming from their calculation (to be 
+  Lectures by Hal Abelson and Gerald Jay Sussman on 
−  concatenated by the expression x ++ y ++ formatLine ...). 
+  [http://swiss.csail.mit.edu/classes/6.001/abelsonsussmanlectures/ Structure and Interpretation of Computer Programs]. 
−  So we need to separate the pairs produced by "evalO t" and "eval u" 
+  This course, and the annexed text book (I did not read it entirely), 
−  (remember: eval now produces a value of type M Int, i.e. a pair of an 
+  are an amazing introduction to computer science: you start by learning 
−  Int and a String!). 
+  some basic Scheme and then Abelson and Sussman will bring you, step by 
+  step, to understand evaluation, interpretation and compilation of Lisp 

+  (Scheme) code. 

−  == Let's Go Monadic== 
+  The course is clear, interesting, funny and will provide you with a 
+  basic, but strong, understanding of functional programming. 

−  Is there a more general way of doing so? 
+  Believe me: if you like programming but don't have a computer science 
+  curriculum, you'll find out that spending 20 hours of your life to 

+  watch that course has been the most productive investment of your 

+  learning life. 

−  Let's analyze the evaluator from another perspective. From the type 
+  ===My problem with Haskell=== 
−  perspective. 

−  We solved our problem by creating a new type, a pair of an Int (the 
+  I find Haskell an amazingly expressive programming language. It makes 
−  result of the evaluation) and a String (the output of the process of 
+  it incredibly easy to perform very difficult tasks, just like monads, 
−  evaluation). 
+  for instance. 
−  The first part of the evaluator does nothing else but creating, from 
+  The problem is that, in doing so, it makes it difficult to understand 
−  a value of type Int, an object of type M Int (Int,Output). It does so 
+  Haskell to newcomers. 
−  by creating a pair with that Int and some text. 

−  The second part evaluates the two Term(s) and "stores" the values thus 
+  I must confess that tutorials and other learning material are quite 
−  produced in some variables to be use later to compute the output. 
+  dissatisfying on this regards too. 
−  Let's focus on the "stores" action. The correct term should be 
+  Since Haskell seems to make easy what is not easy, these tutorial take 
−  "binds". 
+  the "it's easy" approach. 
−  Take a function: 
+  I will give you an example. I think that the 
−  <haskell> 
+  [http://www.cs.utah.edu/~hal/htut/ "Yet Another Haskell Tutorial"] is 
−  f x = x + x 
+  a really good attempt to explain Haskell, and if you want to 
−  </haskell> 
+  understand this tutorial you need to read it at least for getting a 
−  "x" appears on both sides of the expression. We say that on the right 
+  good understanding of the Haskell type system. 
−  side "x" is bound to the value of x given on the left side. 

−  So 
+  The tutorial explains monads and explains the "do notation" in terms 
−  <haskell> 
+  of lambda abstraction (par. 9.1, p. 120). Still, to the lambda 
−  f 3 
+  operator ("\") the tutorial dedicates only 17 lines in par. 4.4.1. 
−  </haskell> 
+  Moreover "\" is explained just as another way of creating functions. 
−  binds x to 3 for the evaluation of the expression "x + x". 

−  Our evaluator binds "a" and "x" / "b" and "y" with the evaluation of 
+  This probably makes sense for a functional programmer who is studying 
−  "eval t" and "eval u" respectively. "a","b","x" and "y" will be then 
+  Haskell for the first time. But, on the other side it will make the 
−  used in the evaluation of ((a+)(x ++ ...). 
+  newcomer, who's not a functional programmer, believe that explaining 
+  monads in terms of "\" is just ugly, and definitely not Haskell. 

−  We know that there is an ad hoc operator for binding variables to a 
+  This is my problem with Haskell: it hides complexity with 
−  value: lambda, or \. 
+  constructions like "let...in", "where", "do". 
−  Indeed f x = x + x is syntactic sugar for: 
+  Still I believe that if you want to use those constructions you must 
−  <haskell> 
+  know what they do. And they do ugly stuff like the one you'll be 
−  f = \x > x + x 
+  looking at in this tutorial. 
−  </haskell> 

−  When we write f 3 we are actually binding "x" to 3 within what's next 

−  ">", that will be used (substituted) for evaluating f 3. 

−  So we can try to abstract this phenomenon. 
+  I am not saying that this is ''the'' way to learn Haskell. I'm just 
+  saying that this is the way I'm learning Haskell. And this is the way 

+  this tutorial has been written. 

−  What we need is a function that takes our composed type MOut Int and a 
+  ===Starting from the inside or the outside?=== 
−  function in order to produce a new MOut Int, concatenating the 

−  output of the computation of the first with the output of the 

−  computation of the second. 

−  This is what bindM does: 
+  In This tutorial I show monads from their inside. In "Meet Bob" I do 
+  the opposite. As I said I think (and I did it on purpose) that after 

+  reading "Meet Bob" you can have a general idea of what a monad is, but 

+  you need to go inside a monad to see how it works. 

−  <haskell> 
+  As a suggestion, I'd invite you to start with "Meet Bob", and then 
+  procede with the tutorial. 

−  bindM :: MOut a > (a > MOut b) > MOut b 
+  I hope the these two approaches will give you an overall image of a 
−  bindM m f = (b, x ++ y) 
+  monad. 
−  where (a, x) = m 

−  (b, y) = f a 

−  </haskell> 
+  ===Prerequisites=== 
−  It takes: 
+  As I said, in order to understand this tutorial you need: 
−  * "m": the compound type MOut Int carrying the result of an "eval 
+  * a basic understanding of functional programming (this is required to understand Haskell after all) 
−  Term", 
+  * a basic understanding of the type system: 
−  * a function "f". This function will take the Int ("a") extracted by the 
+  ** how to create new types (with "data" and "newtype") 
−  evaluation of "m" ((a,x)=m). This function will produce anew pair: a 
+  ** how to use type constructors to construct and to match types and their internal components 
−  new Int produced by a new evaluation; some new output. 
+  ** how to use a label field to retrieve a type internal component 
−  +  * a basic understanding of lambda abstraction 

−  bindM will return the new Int in pair with the concatenated outputs 

−  resulting from the evaluation of "m" and "f a". 

−  
−  So let's write the new version of the evaluator: 

−  
−  <haskell> 

−  
−  evalM_1 :: Term > MOut Int 

−  evalM_1 (Con a) = (a, formatLine (Con a) a) 

−  evalM_1 (Add t u) = bindM (evalM_1 t) (\a > 

−  bindM (evalM_1 u) (\b > 

−  ((a + b), formatLine (Add t u) (a + b)) 

−  ) 

−  ) 

−  
−  </haskell> 

−  
−  Ugly, isn't it? 

−  
−  Let's start from the outside: 

−  
−  </haskell> 

−  bindM (evalM_1 u) (\b > ((a + b), formatLine (Add t u) (a + b))) 

−  </haskell> 

−  
−  bindM takes the result of the evaluation "evalM_1 u", a type Mout Int, 

−  and a function. It will extract the Int from that type and use it to 

−  bind "b". 

−  
−  So in bindM (evalM_1 u)... "b" will be bound to a value. 

−  
−  Then the outer part (bindM (evalM_1 t) (\a...) will bind "a" to the 

−  value needed to evaluate "((a+b), formatLine...) and produce our final 

−  MOut Int. 

−  
−  We can write the evaluator in a more convinient way, now that we know 

−  what it does: 

−  
−  <haskell> 

−  
−  evalM_2 :: Term > MOut Int 

−  evalM_2 (Con a) = (a, formatLine (Con a) a) 

−  evalM_2 (Add t u) = evalM_2 t `bindM` \a > 

−  evalM_2 u `bindM` \b > 

−  ((a + b), (formatLine (Add t u) (a + b))) 

−  
−  </haskell> 

−  
−  Now, look at the first part: 

−  
−  <haskell> 

−  evalM_2 (Con a) = (a, formatLine (Con a) a) 

−  </haskell> 

−  
−  We could use a more general way of creating some output. 

−  
−  First we need a method for creating someting of type M a, starting from 

−  something of type a. This is what <hask>evalM_2 (Con a)</hask> is doing, after all. 

−  Very simply: 

−  
−  <haskell> 

−  
−  mkM :: a > MOut a 

−  mkM a = (a, "") 

−  
−  </haskell> 

−  
−  We then need to "insert" some text (Output) in our type M: 

−  
−  <haskell> 

−  
−  outPut :: Output > MOut () 

−  outPut x = ((), x) 

−  
−  </haskell> 

−  
−  Very simple: we have a string "x" (Output) and create a pair with a () 

−  instead of an Int, and the output. 

−  
−  This way we will be able to define also this firts part in terms of 

−  bindM, that will take care of concatenating outputs. 

−  
−  So we have now a new evaluator: 

−  
−  <haskell> 

−  
−  evalM_3 :: Term > MOut Int 

−  evalM_3 (Con a) = outPut (formatLine (Con a) a) `bindM` \_ > mkM a 

−  evalM_3 (Add t u) = evalM_3 t `bindM` \a > 

−  evalM_3 u `bindM` \b > 

−  outPut (formatLine (Add t u) (a + b)) `bindM` \_ > mkM (a + b) 

−  
−  </haskell> 

−  
−  Well, this is fine, definetly better then before, anyway. 

−  
−  Still we use `bindM` \_ > that binds something we do not use (_). We 

−  could write something for this case, when we concatenate computations 

−  without the need of binding variables. Let's call it `combineM`: 

−  
−  <haskell> 

−  
−  combineM :: MOut a > MOut b > MOut b 

−  combineM m f = m `bindM` \_ > f 

−  
−  </haskell> 

−  
−  So the new evaluator: 

−  
−  <haskell> 

−  
−  evalM :: Term > MOut Int 

−  evalM (Con a) = outPut (formatLine (Con a) a) `combineM` 

−  mkM a 

−  evalM (Add t u) = evalM t `bindM` \a > 

−  evalM u `bindM` \b > 

−  outPut (formatLine (Add t u) (a + b)) `combineM` 

−  mkM (a + b) 

−  
−  </haskell> 

−  
−  Let's put everything together (and change some names): 

−  
−  <haskell> 

−  
−  type MO a = (a, Out) 

−  type Out = String 

−  
−  mkMO :: a > MO a 

−  mkMO a = (a, "") 

−  
−  bindMO :: MO a > (a > MO b) > MO b 

−  bindMO m f = (b, x ++ y) 

−  where (a, x) = m 

−  (b, y) = f a 

−  
−  combineMO :: MO a > MO b > MO b 

−  combineMO m f = m `bindM` \_ > f 

−  
−  outMO :: Out > MO () 

−  outMO x = ((), x) 

−  
−  evalMO :: Term > MO Int 

−  evalMO (Con a) = outMO (formatLine (Con a) a) `combineMO` 

−  mkMO a 

−  evalMO (Add t u) = evalMO t `bindMO` \a > 

−  evalMO u `bindMO` \b > 

−  outMO (formatLine (Add t u) (a + b)) `combineMO` 

−  mkMO (a + b) 

−  
−  </haskell> 

−  
−  == Some Sugar, Please!== 

−  Now our evaluator has been completely transformed into a monadic 

−  evaluator. That's what it is: a monad. 

−  
−  We have a function that constructs an object of type MO Int, formed by 

−  a pair: the result of the evaluation and the accumulated 

−  (concatenated) output. 

−  
−  The process of accumulation and the act of parting the MO Int into its 

−  component is buried into bindM, that can also preserve some value for 

−  later uses. 

−  
−  So we have: 

−  * MO a type constructor for a type carrying a pair composed by an Int 

−  and a String; 

−  * bindMO, that gives a direction to the process of evaluation: it 

−  concatenates computations and captures some side effects we created. 

−  * mkOM lets us create an object of type MO Int starting from an Int. 

−  
−  As you see this is all we need to create a monad. In other words 

−  monads arise from the type system. Everything else is just syntactic 

−  sugar. 

−  
−  So, let's have a look to that sugar: the famous donotation! 

−  
−  We will now rewrite our basic evaluator to use it with the 

−  donotation. 

−  
−  Now we have to crate a new type: this is necessary in order to use 

−  specific monadic notation and have at our disposal the more practical 

−  donotation. 

−  
−  <haskell> 

−  
−  newtype Eval a = Eval a 

−  deriving (Show) 

−  
−  </haskell> 

−  
−  So, our type will be an instance of the monad class. We will have to 

−  define the methods of this class (>>= and return), but that will be 

−  easy since we already done that while defining bindMO and mkMO. 

−  
−  <haskell> 

−  
−  instance Monad Eval where 

−  return a = Eval a 

−  Eval m >>= f = f m 

−  
−  </haskell> 

−  
−  And then we will take the old version of our evaluator and substitute 

−  `bindMO` with >>= and `mkMO` with return: 

−  
−  <haskell> 

−  
−  evalM_4 :: Term > Eval Int 

−  evalM_4 (Con a) = return a 

−  evalM_4 (Add t u) = evalM_4 t >>= \a > 

−  evalM_4 u >>= \b > 

−  return (a + b) 

−  
−  </haskell> 

−  
−  which is, in the donotation: 

−  
−  <haskell> 

−  
−  evalM_5 :: Term > Eval Int 

−  evalM_5 (Con a) = return a 

−  evalM_5 (Add t u) = do a < evalM_5 t 

−  b < evalM_5 u 

−  return (a + b) 

−  
−  </haskell> 

−  
−  Simple: do binds the result of "eval_M5 t" to a, binds the result of 

−  "eval_M5 u" to b and then returns the sum. In a very imperative style. 

−  
−  We can now have an image of our monad: it is out type (Eval) that is 

−  made up of a pair: during evaluation the first member of the pair (the 

−  Int) will get the results of our computation (i.e.: the procedures to 

−  calculate the final result). The second part, the String called 

−  Output, will get filled up with the concatenated output of the 

−  computation. 

−  
−  The sequencing done by bindMO (now >>=) will take care of passing to 

−  the next evaluation the needed Int and will do some more side 

−  calculation to produce the output (concatenating outputs resulting 

−  from computation of the new Int, for instance). 

−  
−  So we can grasp the basic concept of a monad: it is like a label which 

−  we attach to each step of the evaluation (the String attached to the 

−  Int). 

−  This label is persistent within the process of computation and at each 

−  step bindMO can do some manipulation of it. 

−  We are creating sideeffects and propagating them within our monads. 

−  
−  Ok. Let's translate our outputproducing evaluator in monadic 

−  notation: 

−  
−  <haskell> 

−  
−  newtype Eval_IO a = Eval_IO (a, O) 

−  deriving (Show) 

−  type O = String 

−  
−  instance Monad Eval_IO where 

−  return a = Eval_IO (a, "") 

−  (>>=) m f = Eval_IO (b, x ++ y) 

−  where Eval_IO (a, x) = m 

−  Eval_IO (b, y) = f a 

−  print_IO :: O > Eval_IO () 

−  print_IO x = Eval_IO ((), x) 

−  
−  eval_IO :: Term > Eval_IO Int 

−  eval_IO (Con a) = do print_IO (formatLine (Con a) a) 

−  return a 

−  eval_IO (Add t u) = do a < eval_IO t 

−  b < eval_IO u 

−  print_IO (formatLine (Add t u) (a + b)) 

−  return (a + b) 

−  
−  </haskell> 

−  
−  Let's see the evaluator with output in action: 

−  *MyMonads> eval_IO (Add (Con 6) (Add (Con 16) (Add (Con 20) (Con 

−  12)))) 

−  Eval_IO (54,"eval (Con 6) <= 6  eval (Con 16) <= 16  eval (Con 20) <= 20  eval (Con 12) <= 12  eval (Add (Con 20) (Con 12)) <= 32  eval (Add (Con 16) (Add (Con 20) (Con 12))) <= 48  eval (Add (Con 6) (Add (Con 16) (Add (Con 20) (Con 12)))) <= 54  ") 

−  *MyMonads> 

−  Let's format the output part: 
+  I hope I'll be able to write a short introductory summary to these 
−  eval (Con 6) <= 6 
+  topics (I will put it below this introductory remarks). 
−  eval (Con 16) <= 16 

−  eval (Con 20) <= 20 

−  eval (Con 12) <= 12 

−  eval (Add (Con 20) (Con 12)) <= 32 

−  eval (Add (Con 16) (Add (Con 20) (Con 12))) <= 48 

−  eval (Add (Con 6) (Add (Con 16) (Add (Con 20) (Con 12)))) <= 54 

−  That's it. For today... 
+  Have fun with Haskell! 
−  (TO BE CONTINUED) 
+   [[User:AndreaRossatoAndrea Rossato]] 
−  Andrea Rossato 
+  [[Category:Tutorials]] 
−  arossato AT istitutocolli.org 
+  [[Category:Idioms]] 
+  [[Category:Monad]] 
Latest revision as of 22:35, 24 July 2007
Note Since the size of the previous file was getting too big for a wiki, the tutorial has been divided into two parts: The Monadic Way Part I and The Monadic Way Part II. See below for some introductory remarks.
Contents
 Meet Bob The Monadic Lover
 A (supposedtobe) funny and short introduction to Monads, with code but without any reference to category theory: what monads look like and what they are useful for, from the perspective of a ... lover. It could be an introduction to "The Monadic Way" tutorial.
 The Monadic Way/Part I
 In the first part of the tutorial we will start from a very simple evaluator that will be transformed into a monadic evaluator with an increasing number of features: output, exceptions, and state: a very simple counter for tracking the number of recursions of the evaluation precess.
 The Monadic Way/Part II
 In the second part of the tutorial we will see how to take complexity out of our monadic evaluator with the use of monadic transformers, and specifically StateT. This part is just a skeleton, since, for the time being, it contains only the code.
Contents 
[edit] 1 Preliminary remarks
When I started writing this tutorial I though that the only way to explain monads to a newcomer was to show them from the inside, with the use of lambda abstraction. Not only because this is the way Philip Wedler's paper adopts, but also because I believed, and still believe, that the only way to understand what bind (>>=) does is to explain it as a function that takes a monad and an anonymous function.
I had this feeling because I am a newcomer, and this is the way I came to understand monads.
I did not received very much feedback for this tutorial, and I must admit that I would like to. But one person, on the haskellcafe mailing list, told me that:
imho your tutorial makes the error that is a very typical: when you write your tutorial you already know what are monads and what the program you will construct at the end. but your reader don't know all these! for such fresh reader this looks as you made some strange steps, write some ugly code and he doesn't have chances to understand that this ugly code is written just to show that this can be simplified by using monads.
I believe that Bulat is right. In this tutorial you go through some code that is really ugly and then, with some kind of magic, it turns out in the (redundant but) clean evaluator of the end of The Monadic Way/Part II.
I took that mail as a challenge and I responded by writing Meet Bob The Monadic Lover.
In "Meet Bob" the code is clean, variable names are very descriptive, and you see that I can create a monad without any use of lambda abstraction.
Bind (in "Meet Bob" is askLover) now takes a monad and a partial application, not an anonymous function.
Obviously you can see an anonymous function as a partial application.
The problem, I think, is that, in "Meet Bob", you cannot understand the strict relation between what I did before and what I do when I start using the "donotation". You see that the same functions are being used ("tellMyself" and "newLove"), but "andrea < tellMyself 1" is not explained. It's just magic.
The fact is that you cannot understand "andrea < tellMyself 1" without the use of lambda abstraction.
I should have written an intermediate step, something like this:
drunk = do newLove "Paula " >> (tellLover 1 10) >>= \paula > (tellMyself 3) >>= \lorena > tellMyself (paula + lorena)
With this approach I think you can understand why and how you come to write something like this:
drunk = do newLove "Paula " paula < (tellLover 1 10) lorena < tellMyself 3 tellMyself (paula + lorena)
That is to say, in this way you can see a do block as a series of nested anonymous functions whose arguments are bound to some value by the application of >>=. Anonymous functions that bind to some value the variables appearing after the ">" ("paula" and "lorena").
To summarize, I think that even if you can start using monads without understanding that what happens inside a "do" block is strictly related with lambda calculus, I don't think you can claim you understand monads just because you are using them.
And I'm quite sure that if a problem arises somewhere you can have a very difficult time in trying to find out what the problem is. This is even more true when you start doing monadic transformations.
[edit] 2 How to explain monads to newcomers?
Monads are not an impossibletounderstandunlessyouhaveaphd topic. I don't know if I can claim I'm a living proof of this assumption, since I have a PhD. But please take into account that I have a PhD in Comparative Private Law! Nothing related to computer science. And I claim I understand monads.
Moreover, since I have come to understand them, I think I can try to explain them to newcomers like me. This is why I started writing this tutorial.
Still, in order to understand them I studied, and I studied hard. I wanted to understand, it was difficult, and I kept studying. Going back to the foundation when needed.
So, I cannot explain monads to you unless you are willing to study. I can show you the way, but you must take your time and follow that way.
[edit] 3 What do I need to know to understand monads?
[edit] 3.1 Functional programming
First you need at least a basic understanding of functional programming.
This is going to be the easiest step, because there is an invaluable resource available on line for free.
This is what I did: I spent 20 hours of my life watching the Video Lectures by Hal Abelson and Gerald Jay Sussman on Structure and Interpretation of Computer Programs.
This course, and the annexed text book (I did not read it entirely), are an amazing introduction to computer science: you start by learning some basic Scheme and then Abelson and Sussman will bring you, step by step, to understand evaluation, interpretation and compilation of Lisp (Scheme) code.
The course is clear, interesting, funny and will provide you with a basic, but strong, understanding of functional programming.
Believe me: if you like programming but don't have a computer science curriculum, you'll find out that spending 20 hours of your life to watch that course has been the most productive investment of your learning life.
[edit] 3.2 My problem with Haskell
I find Haskell an amazingly expressive programming language. It makes it incredibly easy to perform very difficult tasks, just like monads, for instance.
The problem is that, in doing so, it makes it difficult to understand Haskell to newcomers.
I must confess that tutorials and other learning material are quite dissatisfying on this regards too.
Since Haskell seems to make easy what is not easy, these tutorial take the "it's easy" approach.
I will give you an example. I think that the "Yet Another Haskell Tutorial" is a really good attempt to explain Haskell, and if you want to understand this tutorial you need to read it at least for getting a good understanding of the Haskell type system.
The tutorial explains monads and explains the "do notation" in terms of lambda abstraction (par. 9.1, p. 120). Still, to the lambda operator ("\") the tutorial dedicates only 17 lines in par. 4.4.1. Moreover "\" is explained just as another way of creating functions.
This probably makes sense for a functional programmer who is studying Haskell for the first time. But, on the other side it will make the newcomer, who's not a functional programmer, believe that explaining monads in terms of "\" is just ugly, and definitely not Haskell.
This is my problem with Haskell: it hides complexity with constructions like "let...in", "where", "do".
Still I believe that if you want to use those constructions you must know what they do. And they do ugly stuff like the one you'll be looking at in this tutorial.
I am not saying that this is the way to learn Haskell. I'm just saying that this is the way I'm learning Haskell. And this is the way this tutorial has been written.
[edit] 3.3 Starting from the inside or the outside?
In This tutorial I show monads from their inside. In "Meet Bob" I do the opposite. As I said I think (and I did it on purpose) that after reading "Meet Bob" you can have a general idea of what a monad is, but you need to go inside a monad to see how it works.
As a suggestion, I'd invite you to start with "Meet Bob", and then procede with the tutorial.
I hope the these two approaches will give you an overall image of a monad.
[edit] 3.4 Prerequisites
As I said, in order to understand this tutorial you need:
 a basic understanding of functional programming (this is required to understand Haskell after all)
 a basic understanding of the type system:
 how to create new types (with "data" and "newtype")
 how to use type constructors to construct and to match types and their internal components
 how to use a label field to retrieve a type internal component
 a basic understanding of lambda abstraction
I hope I'll be able to write a short introductory summary to these topics (I will put it below this introductory remarks).
Have fun with Haskell!