
I'm reading "Learn You a Haskell for Great Good!", chapter 9, "Input and Output" http://learnyouahaskell.com/input-and-output. IO actions are given liberal coverage throughout the chapter, however it is never mentioned whether the value-extractor syntax (<-) has a type or not. main = do x <- getLine putStrLn $ reverse x In this little program, getLine has type "IO String" and x has type "String". This implies to me that (<-) has type "IO a -> a". However, GHCI chokes on ":t (<-)" and Hoogle says it's just a syntactic element http://www.haskell.org/haskellwiki/Keywords#.3C-. I guess I don't have a specific question, but I was kind of expecting it to be a function with a type because everything seems to be a function with a type in Haskell... Thanks for listening!

It is a syntatic sugar that is expanded to getLine >>= \x -> putStrLn $ reverse x
= is defined in the typeclass for Monad.
In general, if something is using <- notation, it's type is Monad m => m a,
where m could be any of many monads, IO, Maybe, [] (lists), Parser or even
some type of yours that you made an instance of Monad, which you can do if
you would like to use that syntax.
On Thu, Aug 30, 2012 at 11:00 PM, Patrick Redmond
I'm reading "Learn You a Haskell for Great Good!", chapter 9, "Input and Output" http://learnyouahaskell.com/input-and-output.
IO actions are given liberal coverage throughout the chapter, however it is never mentioned whether the value-extractor syntax (<-) has a type or not.
main = do x <- getLine putStrLn $ reverse x
In this little program, getLine has type "IO String" and x has type "String". This implies to me that (<-) has type "IO a -> a". However, GHCI chokes on ":t (<-)" and Hoogle says it's just a syntactic element http://www.haskell.org/haskellwiki/Keywords#.3C-.
I guess I don't have a specific question, but I was kind of expecting it to be a function with a type because everything seems to be a function with a type in Haskell... Thanks for listening!
_______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners

The (<-) symbol is syntax, so doesn't really have a type and probably shouldn't be thought of as having one. It's more like, given the expression that appears to its right of the type (m a) implies that the value to its left is of the type a. Unlike, (->) which has a kind (not a type), since it takes two type variables to produce a type e.g. (->) Int Int is a function taking Int to Int. Hope that helps. On 31/08/12 13:00, Patrick Redmond wrote:
I'm reading "Learn You a Haskell for Great Good!", chapter 9, "Input and Output" http://learnyouahaskell.com/input-and-output.
IO actions are given liberal coverage throughout the chapter, however it is never mentioned whether the value-extractor syntax (<-) has a type or not.
main = do x <- getLine putStrLn $ reverse x
In this little program, getLine has type "IO String" and x has type "String". This implies to me that (<-) has type "IO a -> a". However, GHCI chokes on ":t (<-)" and Hoogle says it's just a syntactic element http://www.haskell.org/haskellwiki/Keywords#.3C-.
I guess I don't have a specific question, but I was kind of expecting it to be a function with a type because everything seems to be a function with a type in Haskell... Thanks for listening!
_______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners
-- Tony Morris http://tmorris.net/

On Thu, Aug 30, 2012 at 8:00 PM, Patrick Redmond
IO actions are given liberal coverage throughout the chapter, however it is never mentioned whether the value-extractor syntax (<-) has a type or not.
What sorts of things have types? Values have types, but x <- getLine is not a value. I will elaborate.
main = do x <- getLine putStrLn $ reverse x
As mentioned by others, this can be de-sugared into: main = getLine >>= \x -> putStrLn $ reverse x
In this little program, getLine has type "IO String" and x has type "String". This implies to me that (<-) has type "IO a -> a". However, GHCI chokes on ":t (<-)" and Hoogle says it's just a syntactic element http://www.haskell.org/haskellwiki/Keywords#.3C-.
You are right about the types of getLine and x. But look at the part of the de-sugared code that corresponds to x <- getLine: getLine >>= \x -> This isn't an expression. In fact, it's nothing valid on its own. Since you can't evaluate it to a value, don't expect it to have a type. I guess I don't have a specific question, but I was kind of expecting
it to be a function with a type because everything seems to be a function with a type in Haskell... Thanks for listening!
There are other things in Haskell which don't have a type. Here's something very similar to your example: foo = let x = 3 in x + x Does "let x = 3" have a type? Does the "=" in there have a type? (The answer is no, and the reasons are basically the same.) -Karl V.

On Thu, Aug 30, 2012 at 11:09 PM, David McBride
It is a syntatic sugar that is expanded to
getLine >>= \x -> putStrLn $ reverse x
= is defined in the typeclass for Monad.
Interesting. Does that mean the lines following a "x <- getLine" are simply balled up into a function? What if there are multiple lines? main = do fn <- getLine ln <- getLine putStr $ reverse ln putStr " " putStr $ reverse fn
In general, if something is using <- notation, it's type is Monad m => m a, where m could be any of many monads, IO, Maybe, [] (lists), Parser or even some type of yours that you made an instance of Monad, which you can do if you would like to use that syntax.
I thought it might start to get at monads..
On Thu, Aug 30, 2012 at 11:10 PM, Tony Morris
The (<-) symbol is syntax, so doesn't really have a type and probably shouldn't be thought of as having one.
It's more like, given the expression that appears to its right of the type (m a) implies that the value to its left is of the type a.
And then that implication is realized through the syntactic
transformation which occurs when (<-) is desugared..? I'll keep
reading.
On Thu, Aug 30, 2012 at 11:54 PM, Karl Voelker
There are other things in Haskell which don't have a type. Here's something very similar to your example:
foo = let x = 3 in x + x
Does "let x = 3" have a type? Does the "=" in there have a type? (The answer is no, and the reasons are basically the same.)
I've taken a pl class in which we learned that "let <name> = <expr1> in <expr2>" is implemented by expanding to something like "(\<name> -> <expr2>) <expr1>", so I'm familiar with this, but I appreciate your making the parallel to (<-). Thanks!

On Sat, Sep 1, 2012 at 10:42 AM, Patrick Redmond
Interesting. Does that mean the lines following a "x <- getLine" are simply balled up into a function? What if there are multiple lines?
I'll insert the translation:
main = do main = fn <- getLine getLine >>= \fn -> ln <- getLine getLine >>= \ln -> putStr $ reverse ln putStr (reverse ln) >> putStr " " putStr " " >> putStr $ reverse fn putStr (reverse fn)
Which is just a single large expression. "do" syntax is not magic, nor is it some fundamentally different language. It's just a convenient way to write a certain repetitious pattern. -- brandon s allbery allbery.b@gmail.com wandering unix systems administrator (available) (412) 475-9364 vm/sms
participants (5)
-
Brandon Allbery
-
David McBride
-
Karl Voelker
-
Patrick Redmond
-
Tony Morris