Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>This probably isn't the answer you're looking for, but here you go anyways:</p> <h1>A really crooked way of looking at monads &amp; co.</h1> <p>One way of looking at abstract concepts like these is to link them with basic concepts, such as ordinary list processing operations. Then, you could say that,</p> <ul> <li>A category generalizes the <code>(.)</code> operation. </li> <li>A monoid generalizes the <code>(++)</code> operation.</li> <li>A functor generalises the <code>map</code> operation.</li> <li>An applicative functor generalizes the <code>zip</code> (or <code>zipWith</code>) operation.</li> <li>A monad generalizes the <code>concat</code> operation.</li> </ul> <h1>A Category</h1> <p>A category consists of a set (or a class) of objects and bunch of arrows that each connect two of the objects. In addition, for each object, there should be an identity arrow connecting this object to itself. Further, if there is one arrow (<code>f</code>) that ends on an object, and another (<code>g</code>) that starts from the same object, there should then also be a composite arrow called <code>g . f</code>.</p> <p>In Haskell this is modelled as a typeclass that represents the category of Haskell types as objects. </p> <pre><code> class Category cat where id :: cat a a (.) :: cat b c -&gt; cat a b -&gt; cat a c </code></pre> <p>Basic examples of a category are functions. Each function connects two types, for all types, there is the function <code>id :: a -&gt; a</code> that connects the type (and the value) to itself. The composition of functions is the ordinary function composition.</p> <p>In short, categories in Haskell base are things that <em>behave like functions</em>, i.e. you can put one after another with a generalized version of <code>(.)</code>. </p> <h1>A Monoid</h1> <p>A monoid is a set with an unit element and an associative operation. This is modelled in Haskell as:</p> <pre><code>class Monoid a where mempty :: a mappend :: a -&gt; a -&gt; a </code></pre> <p>Common examples of monoids include:</p> <ul> <li>set of integers, the element 0, and the operation <code>(+)</code>. </li> <li>set of positive integers, the element 1, and the operation <code>(*)</code>. </li> <li>set of all lists, the empty list <code>[]</code>, and the operation <code>(++)</code>.</li> </ul> <p>These are modelled in Haskell as</p> <pre><code>newtype Sum a = Sum {getSum :: a} instance (Num a) =&gt; Monoid (Sum a) where mempty = Sum 0 mappend (Sum a) (Sum b) = Sum (a + b) instance Monoid [a] where mempty = [] mappend = (++) </code></pre> <p>Monoids are used to 'combine' and accumulate things. For example, the function <code>mconcat :: Monoid a =&gt; [a] -&gt; a</code>, can be used to reduce a list of sums to single sum, or a nested list into a flat list. Consider this as a kind of generalization of <code>(++)</code> or <code>(+)</code> operations that in a way 'merge' two things. </p> <h1>A Functor</h1> <p>A functor in Haskell is a thing that quite directly generalizes the operation <code>map :: (a-&gt;b) -&gt; [a] -&gt; [b]</code>. Instead of mapping over a list, it maps over some <em>structure</em>, such as a list, binary tree, or even an IO operation. Functors are modelled like this:</p> <pre><code>class Functor f where fmap :: (a-&gt;b) -&gt; f a -&gt; f b </code></pre> <p>Contrast this to the definition of the normal <code>map</code> function. </p> <h1>An Applicative Functor</h1> <p>Applicative functors can be seen as things with a generalized <code>zipWith</code> operation. Functors map over general structures one at the time, but with an Applicative functor you can zip together two or more structures. For the simplest example, you can use applicatives to zip together two integers inside the <code>Maybe</code> type: </p> <pre><code>pure (+) &lt;*&gt; Just 1 &lt;*&gt; Just 2 -- gives Just 3 </code></pre> <p>Notice that the structure can affect the result, for example:</p> <pre><code>pure (+) &lt;*&gt; Nothing &lt;*&gt; Just 2 -- gives Nothing </code></pre> <p>Contrast this to the usual <code>zipWith</code> function:</p> <pre><code>zipWith (+) [1] [2] </code></pre> <p>Instead of of just lists, the applicative works for all kinds of structures. Additionally, the clever trickery with <code>pure</code> and <code>(&lt;*&gt;)</code> generalizes the zipping to work with any number of arguments. To see how this works, inspect the following types while keeping the concept of partially applied functions at hand:</p> <pre><code>instance (Functor f) =&gt; Applicative f where pure :: a -&gt; f a (&lt;*&gt;) :: f (a -&gt; b) -&gt; f a -&gt; f b </code></pre> <p>Notice also the similarity between <code>fmap</code> and <code>(&lt;*&gt;)</code>.</p> <h1>A Monad</h1> <p>Monads are often used to model different computational contexts, such as non-deterministic, or side-effectful computations. Since there are already far too many monad tutorials, I will just recommend <a href="http://blog.sigfpe.com/2006/08/you-could-have-invented-monads-and.html">The best one</a>, instead of writing yet another.</p> <p>Relating to the ordinary list processing functions, monads generalize the function <code>concat :: [[a]] -&gt; [a]</code> to work with many other sorts of structures besides lists. As a simple example, the monadic operation <code>join</code> can be used to flatten nested <code>Maybe</code> values:</p> <pre><code>join (Just (Just 42)) -- gives Just 42 join (Just (Nothing)) -- gives Nothing </code></pre> <p>How is this related to the use of Monads as a means of structuring computations? Consider a toy example where you do two consecutive queries from some database. The first query returns you some key value, with which you wish to do another lookup. The problem here is that the first value is wrapped inside <code>Maybe</code>, so you can't query with that directly. Instead, as maybe is a <code>Functor</code>, you could instead <code>fmap</code> the return value with the new query. This would give you two nested <code>Maybe</code> values like above. Another query would result in three layers of <code>Maybe</code>s. This would be quite difficult to program with, but a monadic <code>join</code> gives you a way to flatten this structure, and work with just a single level of <code>Maybe</code>s.</p> <p>(I think I'll be editing this post a lot before it makes any sense..)</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload