Send Beginners mailing list submissions to
[email protected]
To subscribe or unsubscribe via the World Wide Web, visit
http://www.haskell.org/mailman/listinfo/beginners
or, via email, send a message with subject or body 'help' to
[email protected]
You can reach the person managing the list at
[email protected]
When replying, please edit your Subject line so it is more specific
than "Re: Contents of Beginners digest..."
Today's Topics:
1. A few really short beginners questions (Klaus Gy)
2. Re: A few really short beginners questions (Brent Yorgey)
3. Re: A few really short beginners questions
(Andrew Sackville-West)
4. Re: A few really short beginners questions (Klaus Gy)
5. Re: A few really short beginners questions (Brent Yorgey)
6. Re: A few really short beginners questions (Isaac Dupree)
7. Re: A few really short beginners questions (Klaus Gy)
8. Re: A few really short beginners questions (Klaus Gy)
----------------------------------------------------------------------
Message: 1
Date: Sun, 3 Oct 2010 20:00:19 +0200
From: Klaus Gy <[email protected]>
Subject: [Haskell-beginners] A few really short beginners questions
To: beginners <[email protected]>
Message-ID:
<[email protected]>
Content-Type: text/plain; charset=ISO-8859-1
Hi! I have a few questions to improve my knowledge of Haskell. I
didn't stumble over these problems while working on specific tasks, I
more or less constructed them explicitly to get an adequate
understanding of the basic Haskell semantics.
1
Why can't I apply a class directly to all instances of another,
existing class? For example why is it possible to write
class Test a
instance Num a => Test [a]
but not with
instance Num a => Test a
in the last row?
2
Why is the following example not valid?
f :: a -> a
f '0' = 0
f x = 1
I think the reason lies in the type system but I can't locate it exactly.
3
Why is the following legal
[] :: Num a => [a]
but not with a self declared class instad of Num (unresolved overloading)?
4
Why does in the declaration
f undefined = '0'
the expression undefined work apparently in the same way as a wildcard?
Thanks, fweth
------------------------------
Message: 2
Date: Sun, 3 Oct 2010 14:10:24 -0400
From: Brent Yorgey <[email protected]>
Subject: Re: [Haskell-beginners] A few really short beginners
questions
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=us-ascii
On Sun, Oct 03, 2010 at 08:00:19PM +0200, Klaus Gy wrote:
> Hi! I have a few questions to improve my knowledge of Haskell. I
> didn't stumble over these problems while working on specific tasks, I
> more or less constructed them explicitly to get an adequate
> understanding of the basic Haskell semantics.
>
> 1
>
> Why can't I apply a class directly to all instances of another,
> existing class? For example why is it possible to write
>
> class Test a
>
> instance Num a => Test [a]
>
> but not with
>
> instance Num a => Test a
>
> in the last row?
Because these two instances are overlapping: if an instance for Test
[Int] was wanted, which instance should be chosen? Both 'Test [a]' and
'Test a' match 'Test [Int]' so it is ambiguous. Now, it is possible
to turn on the OverlappingInstances flag, in which case the 'more
specific' instance (in this case Test [a]) would be chosen, but this
is generally considered bad for your health unless you Know What You
Are Doing (tm).
>
> 2
>
> Why is the following example not valid?
>
> f :: a -> a
> f '0' = 0
> f x = 1
>
> I think the reason lies in the type system but I can't locate it
> exactly.
There is no type information around at runtime, so if f is supposed to
work for all types it must work *uniformly* for all types: it is not
possible to say "if the argument is a Char, do this; otherwise, do
that".
>
> 3
>
> Why is the following legal
>
> [] :: Num a => [a]
>
> but not with a self declared class instad of Num (unresolved
> overloading)?
It should be possible with a self declared class. I'd have to see
more context to see why you are getting this error.
>
> 4
>
> Why does in the declaration
>
> f undefined = '0'
>
> the expression undefined work apparently in the same way as a
> wildcard?
In a pattern, names simply match anything and bind that name to the
value. The fact that you have used the name "undefined" is not
relevant; it simply shadows any existing binding for the name
"undefined". So
f blergh = '0'
is precisely the same as
f undefined = '0'
Also, consider this example:
x = 6
f x = 3
f _ = 9
At first glance you might think that f yields 3 when passed 6 as an
argument and 9 for everything else; but in fact f always returns 3;
the two x's have nothing to do with one another.
-Brent
------------------------------
Message: 3
Date: Sun, 3 Oct 2010 11:11:47 -0700
From: Andrew Sackville-West <[email protected]>
Subject: Re: [Haskell-beginners] A few really short beginners
questions
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset="us-ascii"
On Sun, Oct 03, 2010 at 08:00:19PM +0200, Klaus Gy wrote:
> Hi! I have a few questions to improve my knowledge of Haskell. I
> didn't stumble over these problems while working on specific tasks, I
> more or less constructed them explicitly to get an adequate
> understanding of the basic Haskell semantics.
I'm fairly noobish at haskell, but I can answer some of them I think.
[...]
> 2
>
> Why is the following example not valid?
>
> f :: a -> a
> f '0' = 0
> f x = 1
>
> I think the reason lies in the type system but I can't locate it exactly.
>
in the type declaration, you have claimed that f takes an object of
one type and returns another object of that same type. That is, every
instance of 'a' in the type declaration has to refer to the *same*
type. 'a' gets bound to a specific type and must match that type
throughout.
now, f '0' = 0 has type f::Num t => Char -> t. This means that f takes
a char and returns some numeric type. But char is not an instance of
Num, and so this definition of f does not match its type signature.
> 3
>
> Why is the following legal
>
> [] :: Num a => [a]
>
> but not with a self declared class instad of Num (unresolved overloading)?
there was a discussion here over the last few days about the
polymorphism of []. It might help to read over that.
>
> 4
>
> Why does in the declaration
>
> f undefined = '0'
>
> the expression undefined work apparently in the same way as a wildcard?
I believe that what you are doing here is binding the argument of f to
a local name "undefined". You are subsequently not using the argument
to f, so it is ignored giving a wildcard type of behavior. If this was
included in a list of pattern-matching definitions of f, then it will
match when all others fail. But this is not a property of "undefined"
in this context. You've just given it a name that happens to coincide
with a reserved word. You could just as easily use "x" or "foo" or
whatever in the same way. Note that commonly, if you are not going to
use the argument to a function, you use "_" to prevent it from being
bound at all.
for example, in:
f:: Int -> Char
f 1 = '1'
f 2 = '2'
f undefined = '0' -- using your word here, but I'd typically not since
its a reserved word
you could replace "undefined" with "_"
f _ = '0'
to get the same result. The first usage will generate a compiler
warning for the unused binding. The second will not.
At least that's how I understand it.
A
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 198 bytes
Desc: Digital signature
Url :
http://www.haskell.org/pipermail/beginners/attachments/20101003/baf2a09f/attachment-0001.bin
------------------------------
Message: 4
Date: Sun, 3 Oct 2010 20:39:37 +0200
From: Klaus Gy <[email protected]>
Subject: Re: [Haskell-beginners] A few really short beginners
questions
To: beginners <[email protected]>
Message-ID:
<[email protected]>
Content-Type: text/plain; charset=ISO-8859-1
Thank You very much for the fast replies! I think I expressed myself
badly in the first question. What does not work is the following:
class Test a
instance Num a => Test a
Thanks, fweth
2010/10/3, Brent Yorgey <[email protected]>:
- Zitierten Text ausblenden -
> On Sun, Oct 03, 2010 at 08:00:19PM +0200, Klaus Gy wrote:
>> Hi! I have a few questions to improve my knowledge of Haskell. I
>> didn't stumble over these problems while working on specific tasks, I
>> more or less constructed them explicitly to get an adequate
>> understanding of the basic Haskell semantics.
>>
>> 1
>>
>> Why can't I apply a class directly to all instances of another,
>> existing class? For example why is it possible to write
>>
>> class Test a
>>
>> instance Num a => Test [a]
>>
>> but not with
>>
>> instance Num a => Test a
>>
>> in the last row?
>
> Because these two instances are overlapping: if an instance for Test
> [Int] was wanted, which instance should be chosen? Both 'Test [a]' and
> 'Test a' match 'Test [Int]' so it is ambiguous. Now, it is possible
> to turn on the OverlappingInstances flag, in which case the 'more
> specific' instance (in this case Test [a]) would be chosen, but this
> is generally considered bad for your health unless you Know What You
> Are Doing (tm).
>
>>
>> 2
>>
>> Why is the following example not valid?
>>
>> f :: a -> a
>> f '0' = 0
>> f x = 1
>>
>> I think the reason lies in the type system but I can't locate it
>> exactly.
>
> There is no type information around at runtime, so if f is supposed to
> work for all types it must work *uniformly* for all types: it is not
> possible to say "if the argument is a Char, do this; otherwise, do
> that".
>
>>
>> 3
>>
>> Why is the following legal
>>
>> [] :: Num a => [a]
>>
>> but not with a self declared class instad of Num (unresolved
>> overloading)?
>
> It should be possible with a self declared class. I'd have to see
> more context to see why you are getting this error.
>
>>
>> 4
>>
>> Why does in the declaration
>>
>> f undefined = '0'
>>
>> the expression undefined work apparently in the same way as a
>> wildcard?
>
> In a pattern, names simply match anything and bind that name to the
> value. The fact that you have used the name "undefined" is not
> relevant; it simply shadows any existing binding for the name
> "undefined". So
>
> f blergh = '0'
>
> is precisely the same as
>
> f undefined = '0'
>
> Also, consider this example:
>
> x = 6
>
> f x = 3
> f _ = 9
>
> At first glance you might think that f yields 3 when passed 6 as an
> argument and 9 for everything else; but in fact f always returns 3;
> the two x's have nothing to do with one another.
>
> -Brent
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners
>
------------------------------
Message: 5
Date: Sun, 3 Oct 2010 15:06:29 -0400
From: Brent Yorgey <[email protected]>
Subject: Re: [Haskell-beginners] A few really short beginners
questions
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=us-ascii
On Sun, Oct 03, 2010 at 11:11:47AM -0700, Andrew Sackville-West wrote:
> >
> > Why is the following example not valid?
> >
> > f :: a -> a
> > f '0' = 0
> > f x = 1
> >
> > I think the reason lies in the type system but I can't locate it exactly.
> >
>
> in the type declaration, you have claimed that f takes an object of
> one type and returns another object of that same type. That is, every
> instance of 'a' in the type declaration has to refer to the *same*
> type. 'a' gets bound to a specific type and must match that type
> throughout.
>
> now, f '0' = 0 has type f::Num t => Char -> t. This means that f takes
> a char and returns some numeric type. But char is not an instance of
> Num, and so this definition of f does not match its type signature.
This is a good point, I hadn't even noticed the type mismatch!
However, note that this isn't the whole story:
f :: a -> a
f '0' = 'a'
f x = x
does not work either, for the reasons I mentioned in my other email.
-Brent
------------------------------
Message: 6
Date: Sun, 03 Oct 2010 15:30:32 -0400
From: Isaac Dupree <[email protected]>
Subject: Re: [Haskell-beginners] A few really short beginners
questions
To: beginners <[email protected]>
Message-ID: <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
On 10/03/10 14:39, Klaus Gy wrote:
> Thank You very much for the fast replies! I think I expressed myself
> badly in the first question. What does not work is the following:
>
> class Test a
>
> instance Num a => Test a
For a reason that seems pretty odd until you get used to it.
In the instance, "Test a", or the "a" thereof, is called the "instance
head". "Num a" is called the "context". When the compiler looks for an
instance, it works by looking just at the "instance head": and "a"
refers to all types. "instance Num a => Test a" says that the one and
only instance that the class has is the one described in that instance.
(And it only works when "a" is a Num type; for other attempted uses
you'd get a compile error.) For various reasons, such instances aren't
allowed by default.
Note that instance Num a => Test [a] might also mean something
different than you intended, even though it's allowed: it defines the
one and only instance of Test on lists.
-Isaac
------------------------------
Message: 7
Date: Sun, 3 Oct 2010 21:44:28 +0200
From: Klaus Gy <[email protected]>
Subject: Re: [Haskell-beginners] A few really short beginners
questions
To: beginners <[email protected]>
Message-ID:
<[email protected]>
Content-Type: text/plain; charset=ISO-8859-1
Yea, Hugs tells me:
ERROR (filepath):3 - Syntax error in instance head (constructor expected)
But since I'm trying to understand Haskell and have nothing particular
in mind with this program, I'm not stressed finding a solution for
this. I'm happy if the code is principally correct.
fweth
2010/10/3, Brent Yorgey <[email protected]>:
> On Sun, Oct 03, 2010 at 08:32:31PM +0200, Klaus Gy wrote:
>> Thank You very much for the fast reply! I think I expressed myself
>> badly in the first question. What does not work is the following:
>>
>> class Test a
>>
>> instance Num a => Test a
>
> What do you mean "doesn't work"? Do you get an error? Or it doesn't
> have the behavior you expect? This ought to work fine.
>
> -Brent
>
------------------------------
Message: 8
Date: Mon, 4 Oct 2010 00:05:55 +0200
From: Klaus Gy <[email protected]>
Subject: Re: [Haskell-beginners] A few really short beginners
questions
To: beginners <[email protected]>
Message-ID:
<[email protected]>
Content-Type: text/plain; charset=ISO-8859-1
2010/10/3, Isaac Dupree <[email protected]>:
> On 10/03/10 14:39, Klaus Gy wrote:
>> Thank You very much for the fast replies! I think I expressed myself
>> badly in the first question. What does not work is the following:
>>
>> class Test a
>>
>> instance Num a => Test a
>
> For a reason that seems pretty odd until you get used to it.
> In the instance, "Test a", or the "a" thereof, is called the "instance
> head". "Num a" is called the "context". When the compiler looks for an
> instance, it works by looking just at the "instance head": and "a"
> refers to all types. "instance Num a => Test a" says that the one and
> only instance that the class has is the one described in that instance.
> (And it only works when "a" is a Num type; for other attempted uses
> you'd get a compile error.) For various reasons, such instances aren't
> allowed by default.
>
> Note that instance Num a => Test [a] might also mean something
> different than you intended, even though it's allowed: it defines the
> one and only instance of Test on lists.
>
> -Isaac
Thanks! I could have avoided this question because I've just found out
that the details of instance declarations are all described in the
Haskell 98 report along with other restrictions I was not aware of.
So, can I assume that there is no need in praxis to declare all
instances of an existing class to instances of a new class?
fweth
------------------------------
_______________________________________________
Beginners mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/beginners
End of Beginners Digest, Vol 28, Issue 12
*****************************************