background preloader

Monads

Facebook Twitter

Revisiting 'Monadic Parsing in Haskell' - Vaibhav Sagar. Posted on 4 February 2018 ‘Monadic Parsing in Haskell’ is a short paper that laid the groundwork for libraries like Parsec and Attoparsec.

Revisiting 'Monadic Parsing in Haskell' - Vaibhav Sagar

Although it was published in 1998 (almost 20 years ago!) It has aged gracefully and the code samples will run with almost no changes. However, the state of the art has advanced since then and I think the use of modern Haskell can make this material simpler to follow and implement. Monadic parsing in Haskell is what sold me on all three. MonadGuide. Monads for the Working Haskell Programmer. Theodore Norvell Memorial University of Newfoundland New: Updated to Haskell '98.

Monads for the Working Haskell Programmer

This short tutorial introduces monads to Haskell programmers. It applies to Haskell '98. Gofer programmers (are there any left?) Can read it too; there is a section at the end detailing the differences between Haskell and Gofer around monads and another about the differences between Haskell 1.3, Haskell 1.4, and Haskell 98. Haskell/Monad transformers. We have seen how monads can help handling IO actions, Maybe, lists, and state.

Haskell/Monad transformers

With monads providing a common way to use such useful general-purpose tools, a natural thing we might want to do is using the capabilities of several monads at once. For instance, a function could use both I/O and Maybe exception handling. Tutorials. 1 Introductions to Haskell These are the recommended places to start learning, short of buying a textbook. 1.1 Best places to start.

Tutorials

All About Monads. All About Monads is a tutorial on monads and monad transformers and a walk-through of common monad instances.

All About Monads

You can download a PDF version here. And here is a version of the article which includes source code. 1.1 What is a monad? A monad is a way to structure computations in terms of values and sequences of computations using those values. Monads allow the programmer to build up computations using sequential building blocks, which can themselves be sequences of computations. How to play with Control.Monad.Writer in haskell? Haskell Monad Tutorial. All About Monads. Grok Haskell Monad Transformers. Another literate Haskell post: I've tried a few times to read various documents on the web aboutMonad Transformers in Haskell.

Grok Haskell Monad Transformers

I think that in almost every case theauthors are trying to show how clever they are rather than explainingtheir use. If I had just seen the simplest possible examples of MonadTransformers at work I would have been able to figure out what was goingon and that would have given me enough information to bootstrap myselfinto Monad Transforming enlightenment. Haskell/Monad transformers. Reader Monad Confusion. I've been using haskell for about a year now, and thought I was really beginning to get a handle on this monad thing.

Reader Monad Confusion

Other than a strange name, and confusing C programmers with the keyword return that doesn't return, they looked like they were just a way of connecting things together, kinda like function composition. In fact, I found myself frequently writing code like: > goldbach => let primes = sieve [2..] > where sieve (p:xs) = p : sieve [x | x<-xs, x `mod` p /= 0]> p100 = takeWhile (<100) primes > pairs = [x+y | x<-p100, y<-p100]> evens = [4,6..100] :: [Int]> isPair x = x `elem` pairs> checkEvens = map isPair evens> conjecture = all id checkEvens> in conjecture Here function composition, (not) cleverly disguised as a bunch of lets that accumulate, acts as the bind operator, providing sequencing. The Monadic Way. 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.

The Monadic Way

See below for some introductory remarks. Contents Meet Bob The Monadic Lover A (supposed-to-be) 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.

Baastad. Haskell/Monoids. In earlier parts of the book, we have made a few passing allusions to monoids and the Monoid type class (most notably when discussing MonadPlus).

Haskell/Monoids

Here we'll give them a more detailed look and show what makes them useful. What is a monoid? [edit] The operation of adding numbers has a handful of properties which are so elementary we don't even think about them when summing numbers up. One of them is associativity: when adding three or more numbers it doesn't matter how we group the terms. Haskell/Understanding monads. Monads are very useful in Haskell, but the concept is often difficult at first.

Haskell/Understanding monads

Since they have so many applications, people often explain them from a particular point of view, and that can confuse your understanding of monads in their full glory. Historically, monads were introduced into Haskell to perform input/output. A predetermined execution order is crucial for things like reading and writing files, and monadic operations follow an inherent sequence. Introductions to advanced Haskell topics. Many people bemoan the sharp divide between experts and beginning Haskell programmers. One thing I've noticed is that "advanced" Haskell topics all have one thing in common: there exists only one good tutorial on that topic and only the experts have found it.

This post is a collection of links to what I consider to be the definitive tutorials on these topics. Monads Monads are technically not an advanced Haskell topic, but I include this topic in the list because the vast majority of monad tutorials are terrible and/or misleading. In my opinion, the best monad tutorial (Titled: "You could have invented monads") was written 8 years ago by Dan Piponi and nobody has ever improved on it since. You Could Have Invented Monads! (And Maybe You Already Have.) If you hadn't guessed, this is about monads as they appear in pure functional programming languages like Haskell.

They are closely related to the monads of category theory, but are not exactly the same because Haskell doesn't enforce the identities satisfied by categorical monads. Writing introductions to monads seems to have developed into an industry. There's a gentle Introduction, a Haskell Programmer's introduction with the advice "Don't Panic", an introduction for the "Working Haskell Programmer" and countless others that introduce monads as everything from a type of functor to a type of space suit. But all of these introduce monads as something esoteric in need of explanation. The State Monad: A Tutorial for the Confused? - Brandon.Si(mmons) I’ve written this brief tutorial on haskell’s State monad to help bridge some of the elusive gaps that I encountered in other explanations I’ve read, and to try to cut through all the sticky abstraction.

This is written for someone who has a good understanding of the Maybe and List monads, but has gotten stuck trying to understand State. I hope it’s helpful! The Data Declaration: To understand a monad you look at it’s datatype and then at the definition for bind (>>=). Most monad tutorials start by showing you the data declaration of a State s a in passing, as if it needed no explanation: 11. State Monad. State Monad. The State Monad by Example This is a short tutorial on the state monad. Emphasis is placed on intuition. A quick tutorial on the state monad. The state monad is a built in monad in Haskell that allows for chaining of a state variable (which may be arbitrarily complex) through a series of function calls, to simulate stateful code. It is defined as: which, honestly, can be pretty bewildering at first.

Three Useful Monads. Confusion over the State Monad code on "Learn you a Haskell" Monads made difficult. Monads as containers. All About Monads. The Monad.Reader/Issue3/Functional Programming vs Object Oriented Programming.