
I'm experimenting with type classes in Haskell and tried to implement a new class based on nullifying values of various types e.g. nullify 6 = 0 nullify 7.6 = 0 nullify [1,2,3] = [] nullify Tree = Empty etc. I set up my class as follows: class Null a where nullify :: a -> a instance Null Int where nullify x = 0 but get the following error when I invoke nullify 6 on the Hugs command line: ERROR - Unresolved overloading *** Type : (Num a, Null a) => a *** Expression : nullify 6 I then tried the following: instance Num a => Null a where nullify x = 0 but got the following error in Hugs ERROR "C:\zero.hs":5 - Syntax error in instance head (constructor expected) Can anybody explain what I'm doing wrong ? Thanks in advance, Tim.

nullify 6 on the Hugs command line:
ERROR - Unresolved overloading *** Type : (Num a, Null a) => a *** Expression : nullify 6
Try 'nullify (6::Int)'. Hugs (not due to any fault of its own) doesn't know that 6 is supposed to be of type Int, so if you also had an instance 'Null Double', what should it do?
I then tried the following:
instance Num a => Null a where nullify x = 0
but got the following error in Hugs
ERROR "C:\zero.hs":5 - Syntax error in instance head (constructor expected)
This is because instance declarations like this are illegal. Basically, there has to be a constructor on the RHS of the =>. You can't just have a type variable. -98 on hugs and -fglasgow-exts on ghc should lift this restrictuion, though I think you need -fallow-undecidable-instances in ghc and something else in Hugs... - Hal -- Hal Daume III | hdaume@isi.edu "Arrest this man, he talks in maths." | www.isi.edu/~hdaume

Hal Daume III
class Null a where nullify :: a -> a
Hmm...why not class Null a where null :: a (you are just throwing away the parameter anyway)?
I then tried the following:
instance Num a => Null a where nullify x = 0
but got the following error in Hugs
ERROR "C:\zero.hs":5 - Syntax error in instance head (constructor expected)
This is because instance declarations like this are illegal. Basically, there has to be a constructor on the RHS of the =>.
In this case, I'd expect a type (and not a class, like "Null a" is) I'm a bit puzzled by the terminology: What does "constructor" mean in this context? Normally, I'd use (unqualified) "constructor" to mean data constructor, as in Just, Nothing, Left, Right and so on. (From the obvious Prelude stuff: data Maybe a = Just a | Nothing data Either a b = Left a | Right b ) Unless I'm completely confused, perhaps Hugs should expect a "type constructor" instead of just a "constructor" in its error message? Or perhaps it could simply say "type"? -kzm -- If I haven't seen further, it is by standing in the footprints of giants

hi, it refers to type constructor. since value constructors are not allowed in the "instance head" the terminology is not ambiguous. on the other hand saying "type" would be wrong, as variables are (in a way) types, but they are not allowed there (in Haskell'98 that is). hope this helps iavor Ketil Malde wrote:
but got the following error in Hugs
ERROR "C:\zero.hs":5 - Syntax error in instance head (constructor expected)
I'm a bit puzzled by the terminology: What does "constructor" mean in this context? Normally, I'd use (unqualified) "constructor" to mean data constructor, as in Just, Nothing, Left, Right and so on.
(From the obvious Prelude stuff: data Maybe a = Just a | Nothing data Either a b = Left a | Right b )
Unless I'm completely confused, perhaps Hugs should expect a "type constructor" instead of just a "constructor" in its error message? Or perhaps it could simply say "type"?
-kzm
-- ================================================== | Iavor S. Diatchki, Ph.D. student | | Department of Computer Science and Engineering | | School of OGI at OHSU | | http://www.cse.ogi.edu/~diatchki | ==================================================

"Iavor S. Diatchki"
it refers to type constructor.
Okay, thanks for the clarification.
since value constructors are not allowed in the "instance head" the terminology is not ambiguous.
You know, I'm going to have to take issue with this. Yes, it's not ambigous if you knew the answer. I think error messages should be targetted at those who didn't know the answer, and doubly so for Hugs which is often used by beginners, and is arguably the easiest Haskell distribution to get up and running. (Just my $0.02 and all that)
on the other hand saying "type" would be wrong, as variables are (in a way) types
Huh? Do you mean "type variables"? :-) -kzm -- If I haven't seen further, it is by standing in the footprints of giants
participants (4)
-
Hal Daume III
-
Iavor S. Diatchki
-
Ketil Malde
-
Tim Stitt