home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #30 / NN_1992_30.iso / spool / comp / lang / function / 1468 < prev    next >
Encoding:
Text File  |  1992-12-16  |  3.3 KB  |  77 lines

  1. Newsgroups: comp.lang.functional
  2. Path: sparky!uunet!gumby!yale!cs.yale.edu!news
  3. From: jones-mark@CS.YALE.EDU (Mark P. Jones)
  4. Subject: Re: What is a monad?
  5. Message-ID: <1992Dec16.150303.211@cs.yale.edu>
  6. Keywords: monads categories Haskell
  7. Sender: news@cs.yale.edu (Usenet News)
  8. Nntp-Posting-Host: chickadee.systemsz.cs.yale.edu
  9. Organization: Yale University, Department of Computer Science, New Haven, CT
  10. References: <1992Dec15.173324@aifh.ed.ac.uk>
  11. Date: Wed, 16 Dec 1992 15:03:03 GMT
  12. Lines: 63
  13.  
  14. In article <1992Dec15.173324@aifh.ed.ac.uk> williamc@aifh.ed.ac.uk
  15. (William Chesters) asks:
  16.  
  17. | What is a monad?
  18.  
  19. Well, since I'm responsible for starting the conversations about monads in
  20. recent posts, I guess I'd better give you a reply!
  21.  
  22. The concept of a monad comes from category theory; I'll spare you the
  23. full details since you can find these in standard text books on the
  24. subject.  Much of the recent interest in monads in functional programming
  25. is the result of a couple of papers written by Philip Wadler which show
  26. how monads can be used to describe all kinds of different programming
  27. language features -- for example, I/O, manipulation of state, continuations
  28. and exceptions -- in a purely functional language like Haskell.
  29.  
  30. Here's a brief summary of how this looks in Haskell.  A monad is given by
  31. a unary type constructor M and two operations:
  32.     unitM :: a -> M a
  33.     bindM :: M a -> (a -> M b) -> M b
  34. One simple way to understand what is going on here is to think of M a as
  35. a type of computations that return values of type a.  With this in mind,
  36. an expression of the form  unitM e  behaves like a trivial computation which
  37. just returns e.  An expression  m `bindM` f  represents a computation which
  38. runs the computation m, takes the result and passes that as an argument
  39. to f.  Every monad has unit and bind operators.  The thing that makes
  40. individual monads more interesting is the additional structure that they
  41. provide.  For example, the ability to update a state or to generate an
  42. exception.
  43.  
  44. You'll find all of this is better explained in the papers that I mentioned
  45. above:
  46.  
  47.  o Phil Wadler, Comprehending Monads (from the ACM conference on LISP &
  48.    functional programming, Nice, France, 1990),
  49.  
  50.  o Phil Wadler, The Essence of Functional Programming (from ACM POPL 1992).
  51.  
  52. To try and explain how this relates to my earlier message, I'm currently
  53. working with an extended form of overloading.  This doesn't really have
  54. anything to do with monads, except that this approach to overloading turns
  55. out to be particularly convenient for programming with monads.
  56.  
  57. The following definitions are a simplified version of the way that monads
  58. (and functors) are defined in the prelude for my system.  If you are familiar
  59. with the use of type classes in Haskell, you should be able to follow what
  60. is going on here, except that whereas a type class corresponds to a set of
  61. types, the following declarations correspond more closely to sets of type
  62. constructors:
  63.  
  64.     class Functor f where
  65.         map :: (a -> b) -> (f a -> f b)
  66.  
  67.     class Functor m => Monad m where
  68.         return    :: a -> m a
  69.         bind      :: m a -> (a -> m b) -> m b
  70.  
  71. (I happened to be using the name `unit' for something else, so I use the
  72. name `return' here instead of `unit' as I did above (and as Phil does in
  73. his papers)).
  74.  
  75. Hope this helps,
  76. Mark
  77.