Can anyone explain this error:
Syntax error in instance head (constructor expected)
class Nullable v where
default_val :: v
instance Num a => Nullable a where -- error reported here
default_val = 0::a
Thanks
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
First off, hackage has you covered.
Secondly, don’t write instances of the form
they don’t work like you want. (YMMV with compilers other than GHC, but I can’t see that it’s a good idea even then). Presumably your intent is to create several instances, something like:
You can think of type class constraints as an extra argument to a function, like this:
Then ghc actually sees these class instances like this:
Now, which instance should the compiler choose? There are two instances that both claim to be valid for all types. So there’s a compiler error.
This is a problem even if you one have one type class based instance. Suppose that you have these instances:
The first is still considered valid for all types, so ghc says that it needs the OverlappingInstances extension to use them all. That’s not entirely evil. But when you try to make any use of this, before long ghc will require another extension,
IncoherentInstances.Lots of people are afraid to use
UndecidableInstances, but that fear is misplaced. The worst that can happen withUndecidableInstancesis that compiling won’t terminate (but it usually does).IncoherentInstancesis the extension which should inspire fear, as it will bring doom upon your code. If GHC says that you have to enableIncoherentInstances, it means you need to change your code.tl;dr
Don’t write instances of the form
They don’t do what you want.