Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can monads make my job easier? Show me some cool piece of code

Tags:

haskell

monads

I like reading snippets of code about concepts that I don't understand. Are there any snippets that show off monads in all their glory? More importantly how can I apply monads to make my job easier.

I use jQuery heavily. That's one cool application of monads I know of.

like image 864
Saurabh Sawant Avatar asked Oct 06 '10 20:10

Saurabh Sawant


People also ask

What are monads used for?

A monad is an algebraic structure in category theory, and in Haskell it is used to describe computations as sequences of steps, and to handle side effects such as state and IO. Monads are abstract, and they have many useful concrete instances. Monads provide a way to structure a program.

What is monad code?

In functional programming, a monad is a software design pattern with a structure that combines program fragments (functions) and wraps their return values in a type with additional computation.

What problem do monads solve?

Monad is a simple and powerful design pattern for function composition that helps us to solve very common IT problems such as input/output, exception handling, parsing, concurrency and other. Application becomes less error prone.

What is a monad for dummies?

So in simple words, a monad is a rule to pass from any type X to another type T(X) , and a rule to pass from two functions f:X->T(Y) and g:Y->T(Z) (that you would like to compose but can't) to a new function h:X->T(Z) . Which, however, is not the composition in strict mathematical sense.


2 Answers

Like others, I think the question is far too general. I think most answers (like mine) will give examples of something neat making use of one specific monad. The real power of monads is that, once you understand them as an abstraction, you can apply that knowledge to any new monads you come across (and in Haskell there are a lot). This in turn means you can easily figure out what new code does and how to use it because you already know the interface and some rules that govern its behavior.

Anyway, here's an example using the List monad from a test-running script I wrote:

runAll :: IO ()
runAll = do
  curdir <- getCurrentDirectory
  sequence $ runTest <$> srcSets <*> optExeFlags <*> optLibFlags
  setCurrentDirectory curdir

Technically I'm using the Applicative interface, but you can just change the <*>'s to ap from Control.Monad if that bothers you.

The cool thing about this is that it calls runTest for every combination of arguments from the lists "srcSets", "optExeFlags", and "optLibFlags" in order to generate profiling data for each of those sets. I think this is much nicer than what I would have done in C (3 nested loops).

like image 108
John L Avatar answered Sep 28 '22 03:09

John L


Your question is really vague -- it's like asking, "show an example of code that uses variables". It's so intrinsic to programming that any code is going to be an example. So, I'll just give you the most-recently-visited Haskell function that's still open in my editor, and explain why I used monadic control flow.

It's a code snippet from my xmonad config file. It is part of the implementation for a layout that behaves in a certain way when there is one window to manage, and in another way for more than one window. This function takes a message and generates a new layout. If we decide that there is no change to be made, however, we return Nothing:

handleMessage' :: AlmostFull a -> SomeMessage -> Int -> Maybe (AlmostFull a)
handleMessage' l@(AlmostFull ratio delta t) m winCount =
  case winCount of
    -- keep existing Tall layout, maybe update ratio
    0 -> finalize (maybeUpdateRatio $ fromMessage m) (Just t)
    1 -> finalize (maybeUpdateRatio $ fromMessage m) (Just t)

    -- keep existing ratio, maybe update Tall layout
    _ -> finalize (Just ratio) (pureMessage t m)
  where
    finalize :: Maybe Rational -> Maybe (Tall a) -> Maybe (AlmostFull a)
    finalize ratio t = ratio >>= \ratio -> t >>= \t ->
      return $ AlmostFull ratio delta t

    maybeUpdateRatio :: Message -> Maybe Rational
    maybeUpdateRatio (Just Shrink) = Just (max 0 $ ratio-delta)
    maybeUpdateRatio (Just Expand) = Just (min 1 $ ratio+delta)
    maybeUpdateRatio _             = Nothing

We decide what to return based on the current window manager state (which is determined by a computation in the X monad, whose result we pass to this function to keep the actual logic pure) -- if there are 0 or 1 windows, we pass the message to the AlmostFull layout and let it decide what to do. That's the f function. It returns Just the new ratio if the message changes the ratio, otherwise it returns Nothing. The other half is similar; it passes the message onto Tall's handler if there are 2 or more windows. That returns Just a new Tall layout if that's what the user asked for, otherwise it returns Nothing.

The finalize function is the interesting part; it extracts both ratio (the desired new ratio) and t (the desired new Tall layout) from its Maybe wrapper. This means that both have to be not Nothing, otherwise we automatically return Nothing from our function.

The reason we used the Maybe monad here was so that we could write a function contingent on all results being available, without having to write any code to handle the cases where a Nothing appeared.

like image 25
jrockway Avatar answered Sep 28 '22 03:09

jrockway