HaskellWiki

Haskell | Wiki community | Recent changes
Random page | Special pages

 

Not logged in
Log in | Help

Monad (sans metaphors)

Categories: Glossary


1 Introduction

Many discussions of Haskell Monads seek to explain them through the use of a variety of metaphors. This page attempts to simply provide a more technical yet straightforward description of Monads in Haskell.

2 So what is a monad?

A Monad is a type constructor with two operations, implementing a standard interface and following a few simple rules.

The Monad type class tells you the interface (what operations you've got, and their types), the Monad laws tell you what all types implementing that interface should have in common.

The Monadic interface gives you two operations: one to throw things into a Monad thing (return), and one to chain two Monad things together (>>=). The chaining explicitly caters for information flowing from the first to the second parameter of (>>=).

The Monad laws tell you two useful facts about Monad things thrown together in that way: whatever it is the Monad does, anything just thrown into it will take no part in that action, and whichever way you use that chaining operation, the structure of chaining is irrelevant - only the ordering of chained Monad things matters.

There are usually other ways to create 'primitive' Monadic things, which can be combined into complex Monadic structures using the operations from the Monad interface.

There is usually a way to interpret Monadic structures built in this way - a 'run' operation of some kind. Examples include:


3 Source

Retrieved from "http://haskell.org/haskellwiki/Monad_%28sans_metaphors%29"

This page has been accessed 1,287 times. This page was last modified 23:07, 29 November 2007. Recent content is available under a simple permissive license.