Type of (>>= f) where f :: a -> m b

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. regards, Milind Patil

(>>= f) is equivalent to (flip (>>=) f), not to ((>>=) f). You can try this with your own function this way: (&$^) :: (Monad m) => m a -> (a -> m b) -> m b (&$^) = undefined :t (&$^ f) Milind Patil 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.
regards, Milind Patil
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe

On Mon, May 10, 2010 at 5:51 AM, Milind Patil
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

David Menendez
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.
Understood :-)! Thanks for the responses -- both of them. Milind Patil

On May 10, 2010, at 05:51 , Milind Patil wrote:
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.
Everyone else having answered the first question, I'll take this one: ((->) r) is the monad of functions with a single argument (and therefore all functions, as Haskell curries all functions: that is, "f a b c" is a function which takes an a and returns a function that takes a b, which in turn produces a function that takes a c and produces the final result). It's also known as the Reader monad. The syntax is the function form of the section (r ->) (which I think is otherwise illegal, since -> is actually syntax rather than an operator); understand this as a partial application of (r -> x), a function which takes an r and produces an x. -- brandon s. allbery [solaris,freebsd,perl,pugs,haskell] allbery@kf8nh.com system administrator [openafs,heimdal,too many hats] allbery@ece.cmu.edu electrical and computer engineering, carnegie mellon university KF8NH
participants (4)
-
Brandon S. Allbery KF8NH
-
David Menendez
-
Miguel Mitrofanov
-
Milind Patil