[prev in list] [next in list] [prev in thread] [next in thread] 

List:       haskell-beginners
Subject:    Re: [Haskell-beginners] <-
From:       Karl Voelker <ktvoelker () gmail ! com>
Date:       2012-08-31 3:54:43
Message-ID: CAFfow0z2a71hn6z1bO5=V2V0EH_wnb5z3nApiiVEaRfP-NFX-A () mail ! gmail ! com
[Download RAW message or body]

[Attachment #2 (multipart/alternative)]


On Thu, Aug 30, 2012 at 8:00 PM, Patrick Redmond <plredmond@gmail.com>wrote:

> 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.

[Attachment #5 (text/html)]

On Thu, Aug 30, 2012 at 8:00 PM, Patrick Redmond <span dir="ltr">&lt;<a \
href="mailto:plredmond@gmail.com" target="_blank">plredmond@gmail.com</a>&gt;</span> \
wrote:<br><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0 0 \
0 .8ex;border-left:1px #ccc solid;padding-left:1ex"> IO actions are given liberal \
coverage throughout the chapter, however<br> it is never mentioned whether the \
value-extractor syntax (&lt;-) has a<br> type or \
not.<br></blockquote><div><br></div><div>What sorts of things have types? Values have \
types, but x &lt;- getLine is not a value. I will elaborate.</div><div> \
</div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc \
solid;padding-left:1ex">

main = do<br>
    x &lt;- getLine<br>
    putStrLn $ reverse x<br>
<br></blockquote><div><br></div><div>As mentioned by others, this can be de-sugared \
into:</div><div><br></div><div>main = getLine &gt;&gt;= \x -&gt; putStrLn $ reverse \
x</div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 \
.8ex;border-left:1px #ccc solid;padding-left:1ex">

In this little program, getLine has type &quot;IO String&quot; and x has type<br>
&quot;String&quot;. This implies to me that (&lt;-) has type &quot;IO a -&gt; \
a&quot;. However,<br> GHCI chokes on &quot;:t (&lt;-)&quot; and Hoogle says it&#39;s \
just a syntactic element<br> &lt;<a \
href="http://www.haskell.org/haskellwiki/Keywords#.3C-" \
target="_blank">http://www.haskell.org/haskellwiki/Keywords#.3C-</a>&gt;.<br></blockquote><div><br></div><div>You \
are right about the types of getLine and x. But look at the part of the de-sugared \
code that corresponds to x &lt;- getLine:</div> <div><br></div><div>getLine &gt;&gt;= \
\x -&gt;</div><div><br></div><div>This isn&#39;t an expression. In fact, it&#39;s \
nothing valid on its own. Since you can&#39;t evaluate it to a value, don&#39;t \
expect it to have a type.</div> <div><br></div><blockquote class="gmail_quote" \
style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"> I guess I \
don&#39;t have a specific question, but I was kind of expecting<br> it to be a \
function with a type because everything seems to be a<br> function with a type in \
Haskell... Thanks for listening!<br></blockquote><div><br></div><div>There are other \
things in Haskell which don&#39;t have a type. Here&#39;s something very similar to \
your example:</div><div><br> </div><div>foo = let x = 3 in x + \
x</div><div><br></div><div>Does &quot;let x = 3&quot; have a type? Does the \
&quot;=&quot; in there have a type? (The answer is no, and the reasons are basically \
the same.)</div><div><br></div> <div>-Karl V. </div></div>



_______________________________________________
Beginners mailing list
Beginners@haskell.org
http://www.haskell.org/mailman/listinfo/beginners


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic