Search code examples
haskellmonadstype-signature

Monad type signature example


The type signature of (+) is:

    (+) :: Num a => a -> a -> a

I can see this in:

    + 4 5

Resulting in 9. + takes 4 and returns a function of roughly:

    (4 + a) -> a

...which then takes 5 and evaluates to 9. Basically, two things in, one thing out. I don't see how this works using the bind operator's type signature. To me, in practice, it always looks like one in, one out.

Could someone please walk me through a simple example using a Maybe monad the way I've done above for (+) ? Hopefully, this will be generally useful to Haskell newbie's like me!


Solution

  • Here is the type signature of bind:

    (>>=)
        :: Monad m
        => m a         -- Left argument
        -> (a -> m b)  -- Right argument
        -> m b
    

    Here is an example of a bind with two arguments:

    Just 1 >>= (\n -> Just (n + 1))
    ^^^^^^     ^^^^^^^^^^^^^^^^^^^^
    Arg #1     Arg #2
    

    .. and it evaluates to Just 2