[prev in list] [next in list] [prev in thread] [next in thread]
List: haskell-beginners
Subject: Re: [Haskell-beginners] binding vs. parameter passing
From: Ramnath R Iyer <rri () silentyak ! com>
Date: 2019-07-23 3:44:47
Message-ID: CACL-FmxSh6zF8-qO9o8GcMZ9x1M2Kcbf_+Es57r=8Xh6_zVErw () mail ! gmail ! com
[Download RAW message or body]
[Attachment #2 (multipart/alternative)]
This answer is likely incomplete at best and wrong at worst, but I'll give
it a shot.
When you have this type declaration:
i :: a
It means that you are declaring the symbol i that can be slotted into any
location that requires an a. It is a task for the compiler to process the
overall program specification and infer that a has a particular concrete
type. When the program is specified though, the value i has a generic type
with no bounds, and the only way to *construct* such a value is to supply a
value of type a from somewhere else.
The problem here is not that you can't *bind* 1 to i, but that you cannot
*construct* any value of type a.
A direct analog of this would be - not the application of the id function
as you called out - but that you cannot define a function with
the following signature, not including bottom:
f :: a -> b
The reason is that you can't supply a value of type b.
I have some experience with Java, and it has a similar constraint that
generic types cannot be instantiated.
--
RRI
On Mon, Jul 22, 2019 at 7:30 PM James Jones <jejones3141@gmail.com> wrote:
> One problem in *Programming Haskell from First Principles* confuses me.
> (So far.)
>
> It's one where you start with the declaration and binding
>
> i :: Num a => a
> i = 1
>
> (which of course works because Num a => a is the type of 1) and then
> change the declaration to
>
> i :: a
>
> and first try to predict and then see what happens. It fails, with ghci
> suggesting that you put the Num a => back. That seemed reasonable at
> first, but then I considered this:
>
> - id has type a -> a, and if you try to evaluate id 1 it works without
> complaint.
> - In all the work I've done on compilers, parameter passing has
> effectively been assignment of actual parameters to the corresponding
> formal parameters. In Haskell, that might mean passing along the
> appropriate thunk, but the principle is the same, isn't it?
>
> So, if I can't bind 1 to i which is declared to have type a, why can I
> successfully pass 1 to id?
> _______________________________________________
> Beginners mailing list
> Beginners@haskell.org
> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
>
--
Ramnath R Iyer
[Attachment #5 (text/html)]
<div dir="ltr">This answer is likely incomplete at best and wrong at worst, but \
I'll give it a shot.<div><br></div><div>When you have this type \
declaration:</div><div><br></div><div><font face="courier new, monospace">i :: \
a</font></div><div><font face="courier new, monospace"><br></font></div><div><font \
face="arial, sans-serif">It means that you are declaring the symbol </font><font \
face="courier new, monospace">i</font><font face="arial, sans-serif"> that can be \
slotted into any location that requires an </font><font face="courier new, \
monospace">a</font><font face="arial, sans-serif">. It is a task for the compiler to \
process the overall program specification and infer that </font><font face="courier \
new, monospace">a</font><font face="arial, sans-serif"> has a particular concrete \
type. When the program is specified though, the value </font><font face="courier new, \
monospace">i</font><font face="arial, sans-serif"> has a generic type with no bounds, \
and the only way to <i>construct</i> such a value is to supply a value of type \
</font><font face="courier new, monospace">a</font><font face="arial, sans-serif"> \
from somewhere else.</font></div><div><font face="arial, \
sans-serif"><br></font></div><div><font face="arial, sans-serif">The problem here is \
not that you can't <i>bind</i> 1 to </font><font face="courier new, \
monospace">i</font><font face="arial, sans-serif">, but that you cannot \
<i>construct</i> any value of type </font><font face="courier new, \
monospace">a</font><font face="arial, sans-serif">.</font></div><div><font \
face="arial, sans-serif"><br></font></div><div><font face="arial, sans-serif">A \
direct analog of this would be - not the application of the </font><font \
face="courier new, monospace">id</font><font face="arial, sans-serif"> function as \
you called out - but that you cannot define a function with the following signature, \
not including bottom:</font></div><div><font face="arial, \
sans-serif"><br></font></div><div><font face="courier new, monospace">f :: a -> \
b</font></div><div><font face="arial, sans-serif"><br></font></div><div><font \
face="arial, sans-serif">The reason is that you can't supply a value of type \
</font><font face="courier new, monospace">b</font><font face="arial, \
sans-serif">.</font></div><div><font face="arial, \
sans-serif"><br></font></div><div><font face="arial, sans-serif">I have some \
experience with Java, and it has a similar constraint that generic types cannot be \
instantiated.</font></div><div><font face="arial, \
sans-serif"><br></font></div><div><font face="arial, sans-serif">-- \
</font></div><div><font face="arial, sans-serif">RRI</font></div></div><br><div \
class="gmail_quote"><div dir="ltr" class="gmail_attr">On Mon, Jul 22, 2019 at 7:30 PM \
James Jones <<a href="mailto:jejones3141@gmail.com" \
target="_blank">jejones3141@gmail.com</a>> wrote:<br></div><blockquote \
class="gmail_quote" style="margin:0px 0px 0px \
0.8ex;border-left-width:1px;border-left-style:solid;border-left-color:rgb(204,204,204);padding-left:1ex"><div \
dir="ltr">One problem in <i>Programming Haskell from First Principles</i> confuses \
me. (So far.)<div><br></div><div>It's one where you start with the declaration \
and binding</div><div><br></div><div><font face="courier new, monospace">i :: Num a \
=> a</font></div><div><font face="courier new, monospace">i = \
1</font></div><div><br></div><div>(which of course works because <font face="courier \
new, monospace">Num a => a</font> is the type of 1) and then change the \
declaration to</div><div><br></div><div><font face="courier new, monospace">i :: \
a</font></div><div><br></div><div>and first try to predict and then see what happens. \
It fails, with ghci suggesting that you put the <font face="courier new, \
monospace">Num a =></font> back. That seemed reasonable at first, but then I \
considered this:</div><div><ul><li><font face="courier new, monospace">id</font> has \
type <font face="courier new, monospace">a -> a</font>, and if you try to evaluate \
<font face="courier new, monospace">id 1</font> it works without \
complaint.</li><li>In all the work I've done on compilers, parameter passing has \
effectively been assignment of actual parameters to the corresponding formal \
parameters. In Haskell, that might mean passing along the appropriate thunk, but the \
principle is the same, isn't it?</li></ul><div>So, if I can't bind 1 to i \
which is declared to have type a, why can I successfully pass 1 to \
id?</div></div></div> _______________________________________________<br>
Beginners mailing list<br>
<a href="mailto:Beginners@haskell.org" target="_blank">Beginners@haskell.org</a><br>
<a href="http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners" rel="noreferrer" \
target="_blank">http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners</a><br> \
</blockquote></div><br clear="all"><div><br></div>-- <br><div dir="ltr" \
class="gmail-m_7287239113464015438gmail_signature"><div dir="ltr"><div>Ramnath R \
Iyer</div></div></div>
[Attachment #6 (text/plain)]
_______________________________________________
Beginners mailing list
Beginners@haskell.org
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
[prev in list] [next in list] [prev in thread] [next in thread]
Configure |
About |
News |
Add a list |
Sponsored by KoreLogic