[Haskell-cafe] Type of (>>= f) where f :: a -> m b
David Menendez
dave at zednenem.com
Mon May 10 06:18:43 EDT 2010
On Mon, May 10, 2010 at 5:51 AM, Milind Patil <milind_patil at hotmail.com> wrote:
> For a function
>
> f :: a -> m b
> f = undefined
>
> I am having trouble understanding how the type of
>
> (>>= f)
>
> is
>
> (>>= f) :: m a -> m b
>
> where, by definition, type of (>>=) is
>
> (>>=) :: (Monad m) => m a -> (a -> m b) -> m b
>
> I do not see how (>>= f) even unifies.
>
> I mean if I code a function with the same type as (>>=) ie.
>
> tt :: (Monad m) => m a -> (a -> m b) -> m b
> tt = undefined
>
> type of (tt f) does not infer to the same type as (>>= f), from ghc ...
>
> (tt f) :: (Monad ((->) b)) => (m a -> b -> b1) -> b -> b1
>
> There seems to something special about (>>=) apart from its type. And whats
> (Monad ((->) b))? I am new to Haskell and I may have gaps in my understanding of
> type inference in Haskell.
It's because >>= is a binary operator. When you partially apply a
binary operator, you get a "section" which applies one of the two
arguments.
Specifically, you have:
(>>=) = \m f -> m >>= f
(m >>=) = \f -> m >>= f
(>>= f) = \m -> m >>= f
There's more in the Haskell tutorial (section 3.2.1)
<http://www.haskell.org/tutorial/functions.html>
Or you can check the Haskell Report, section 3.5:
<http://www.haskell.org/onlinereport/exps.html#sections>
--
Dave Menendez <dave at zednenem.com>
<http://www.eyrie.org/~zednenem/>
More information about the Haskell-Cafe
mailing list