Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"<-" and associated values

Tags:

haskell

monads

Say I've written the following amazin piece of code:

func = do
  a <- Just 5
  return a

It's pretty pointless, I know. Here, a is 5, and func returns Just 5.

Now I rewrite my awesome (yet pointless) function:

func' = do
  a <- Nothing
  return a

This function returns Nothing, but what the heck is a? There's nothing to extract from a Nothing value, yet the program doesn't whine when I do something like this:

func'' = do
  a <- Nothing
  b <- Just 5
  return $ a+b

I just have a hard time seeing what actually happens. What is a? In other words: What does <- actually do? Saying it "extracts the value from right-side and binds it to the left-side" is obviously over-simplifying it. What is it I'm not getting?

Thanks :)

like image 912
Undreren Avatar asked Apr 18 '12 07:04

Undreren


People also ask

What are associated values?

The raw value for a particular enumeration case is always the same. Associated values are set when you create a new constant or variable based on one of the enumeration's cases, and can be different each time you do so.

What are associated values in enum?

In Swift enum, we learned how to define a data type that has a fixed set of related values. However, sometimes we may want to attach additional information to enum values. These additional information attached to enum values are called associated values.

Can you give useful examples of enum associated values?

For instance, you might describe a weather enum that lists sunny, windy, and rainy as cases, but has an associated value for cloudy so that you can store the cloud coverage. Or you might describe types of houses, with the number of bedrooms being an associated integer.

What is raw value in enum?

Enum raw values To set a value to your enum, you need to assign a data type to it. In our case above, we are using a type of String . Each raw value for our enum case must be a unique string, character, or value of any integer or floating-point type. This means the value for the two case statements cannot be the same.


2 Answers

Let's try and desugar the do-notation of that last example.

func'' = Nothing >>= (\a -> Just 5 >>= (\b -> return $ a+b))

Now, let's see how >>= is defined for Maybe. It's in the Prelude:

instance  Monad Maybe  where
    (Just x) >>= k   =  k x
    Nothing  >>= k   =  Nothing
    return           =  Just
    fail s           =  Nothing

So Nothing >>= foo is simply Nothing

like image 189
Sarah Avatar answered Nov 23 '22 18:11

Sarah


The answer lies in the definition of the Monad instance of Maybe:

instance Monad Maybe where
   (Just x) >>= k      = k x
   Nothing  >>= _      = Nothing
   (Just _) >>  k      = k
   Nothing  >>  _      = Nothing
   return              = Just

Your func'' translates to:

Nothing >>= (\a -> (Just 5 >>= (\b -> return (a+b))))

From the definition of (>>=) you can see that the first Nothing is just threaded through to the result.

like image 33
Peter Avatar answered Nov 23 '22 17:11

Peter