no non-typevariable in instance declarations
Hello. I found that Hugs differs from GHC 4.08.1 and from NHC98 1.00 in instance declarations where the instance head has only type variables: Hugs accepts them while the other two rejects. Attached is a small program that demonstrates it. Hugs happily runs the program and outputs the list ["NUM","Integer","NUM"] NHC98 spits the message In file ./t.hs: 6:23 Found a but expected one of [ ( <conid> GHC is more verbose in its message: t.hs:6: Illegal instance declaration for `C a' (There must be at least one non-type-variable in the instance head) Compilation had errors Why GHC and NHC98 are more restrictive than Hugs? This style of instantiation would be very helpful when dealing with type extensions in Haskell (based on classes to provide the interface for common operations on the extendable type). Regards, Romildo -- Prof. José Romildo Malaquias <romildo@iceb.ufop.br> Departamento de Computação Universidade Federal de Ouro Preto Brasil
class C a where ty :: a -> String instance (Num a) => C a where ty _ = "NUM" instance C Integer where ty _ = "Integer"
Why GHC and NHC98 are more restrictive than Hugs?
The instances for (Num a=> a) and Integer overlap, and are therefore forbidden by Haskell'98. Hugs allows overlapping instances. So does GHC with a special flag (-foverlapping-instances I think). nhc98 does not provide type-system extensions to Haskell'98. Regards, Malcolm
On Tue, Nov 14, 2000 at 05:02:30PM +0000, Malcolm Wallace wrote:
class C a where ty :: a -> String instance (Num a) => C a where ty _ = "NUM" instance C Integer where ty _ = "Integer"
Why GHC and NHC98 are more restrictive than Hugs?
The instances for (Num a=> a) and Integer overlap, and are therefore forbidden by Haskell'98.
But this is not relevant to my question. Removing the instance declaration instance C Integer where ty _ = "Integer" from the program (so that there is no instance overlapping now) does not help. Both GHC and NHC98 still complains with the same diagnostics as before. They are not accepting the instance declaration instance (Num a) => C a where ty _ = "NUM" because there is no non-type-variable component in the instantiated type "a" above. Again, why they have this restrictions while Hugs has not?
Hugs allows overlapping instances.
When given the option +o
So does GHC with a special flag (-foverlapping-instances I think).
Yes
nhc98 does not provide type-system extensions to Haskell'98.
Not exactly. NHC98 provides at least the following extension to Haskell 98: * existentialy quantified type variables (By the way, is there any plans to implement multiparameter type classes and instance overlapping in NHC98?) Thanks, Romildo -- Prof. José Romildo Malaquias <romildo@iceb.ufop.br> Departamento de Computação Universidade Federal de Ouro Preto Brasil
Tue, 14 Nov 2000 16:17:48 -0200, José Romildo Malaquias <romildo@urano.iceb.ufop.br> pisze:
But this is not relevant to my question. Removing the instance declaration
instance C Integer where ty _ = "Integer"
from the program (so that there is no instance overlapping now) does not help.
In this case your instance would be the only one possible (any other would overlap) and it could be equally well written as a plain function. Actually Haskell 98 has more severe restriction than non-overlapping instances. The instance head must be a type constructor applied to as many distinct type variables as needed to let the kinds match. "instance Foo [Int]" is as non-standard as "instance Foo a". -- __("< Marcin Kowalczyk * qrczak@knm.org.pl http://qrczak.ids.net.pl/ \__/ ^^ SYGNATURA ZASTÊPCZA QRCZAK
On Tue, Nov 14, 2000 at 07:41:21PM +0000, Marcin 'Qrczak' Kowalczyk wrote:
Tue, 14 Nov 2000 16:17:48 -0200, José Romildo Malaquias <romildo@urano.iceb.ufop.br> pisze:
But this is not relevant to my question. Removing the instance declaration
instance C Integer where ty _ = "Integer"
from the program (so that there is no instance overlapping now) does not help.
In this case your instance would be the only one possible (any other would overlap) and it could be equally well written as a plain function.
Any instance for a no-Num type will not overlap: instance (Num a) => C a where ty _ = "NUM" instance C Char where ty _ = "CHAR" and the overloaded function cannot be written as a plain function.
Actually Haskell 98 has more severe restriction than non-overlapping instances. The instance head must be a type constructor applied to as many distinct type variables as needed to let the kinds match. "instance Foo [Int]" is as non-standard as "instance Foo a".
I am not understand the restriction well. I verified that both Hugs and GHC, when using extensions, accepts "instance C [Int]", but only Hugs accepts "instance (Num a) => C a". Where is this issue presented in a simple way easily understandable by regular Haskell programmers? Any pointers? Romildo -- Prof. José Romildo Malaquias <romildo@iceb.ufop.br> Departamento de Computação Universidade Federal de Ouro Preto Brasil
José Romildo Malaquias wrote:
On Tue, Nov 14, 2000 at 05:02:30PM +0000, Malcolm Wallace wrote:
class C a where ty :: a -> String instance (Num a) => C a where ty _ = "NUM" instance C Integer where ty _ = "Integer"
Why GHC and NHC98 are more restrictive than Hugs?
The instances for (Num a=> a) and Integer overlap, and are therefore forbidden by Haskell'98.
But this is not relevant to my question. Removing the instance declaration
instance C Integer where ty _ = "Integer"
from the program (so that there is no instance overlapping now) does not help. Both GHC and NHC98 still complains with the same diagnostics as before. They are not accepting the instance declaration
instance (Num a) => C a where ty _ = "NUM"
because there is no non-type-variable component in the instantiated type "a" above.
Again, why they have this restrictions while Hugs has not?
GHC doesn't have this restriction either, but since it's not Haskell 98, you don't get it without some effort ;-). The following combination of flags will convince GHC to like your program: -fallow-overlapping-instances -fallow-undecidable-instances --Jeff
On Tue, Nov 14, 2000 at 02:18:47PM -0800, Jeffrey R. Lewis wrote:
José Romildo Malaquias wrote:
On Tue, Nov 14, 2000 at 05:02:30PM +0000, Malcolm Wallace wrote:
class C a where ty :: a -> String instance (Num a) => C a where ty _ = "NUM" instance C Integer where ty _ = "Integer"
Why GHC and NHC98 are more restrictive than Hugs?
The instances for (Num a=> a) and Integer overlap, and are therefore forbidden by Haskell'98.
But this is not relevant to my question. Removing the instance declaration
instance C Integer where ty _ = "Integer"
from the program (so that there is no instance overlapping now) does not help. Both GHC and NHC98 still complains with the same diagnostics as before. They are not accepting the instance declaration
instance (Num a) => C a where ty _ = "NUM"
because there is no non-type-variable component in the instantiated type "a" above.
Again, why they have this restrictions while Hugs has not?
GHC doesn't have this restriction either, but since it's not Haskell 98, you don't get it without some effort ;-). The following combination of flags will convince GHC to like your program:
-fallow-overlapping-instances -fallow-undecidable-instances
Thanks, Jeff. I did not know about the -fallow-undecidable-instances option to GHC. As I am already using some extensions from GHC (existentialy quantified type variables, multiparameter type classes, implicit parameters, overlapping instances) I think I could use undecidable instances too. I hope new versions of the Haskell language will include them. Romildo -- Prof. José Romildo Malaquias <romildo@iceb.ufop.br> Departamento de Computação Universidade Federal de Ouro Preto Brasil
(By the way, is there any plans to implement multiparameter type classes and instance overlapping in NHC98?)
No-one at York has any current plans to implement MPTC or instance overlapping in nhc98. However, other people would be most welcome to do so if they wished. Regards, Malcolm
On Wed, Nov 15, 2000 at 10:21:28AM +0000, Malcolm Wallace wrote:
(By the way, is there any plans to implement multiparameter type classes and instance overlapping in NHC98?)
No-one at York has any current plans to implement MPTC or instance overlapping in nhc98. However, other people would be most welcome to do so if they wished.
I should be finishing my current project this year and next year I should start looking at functional language implementation. I will be starting a graduate course which may culminate in the implementation of a new functional language with a better system for overloading. As an exercise I may try implementing some extensions to NHC98. Regards, Romildo -- Prof. José Romildo Malaquias <romildo@iceb.ufop.br> Departamento de Computação Universidade Federal de Ouro Preto Brasil
participants (4)
-
Jeffrey R. Lewis -
José Romildo Malaquias -
Malcolm Wallace -
qrczak@knm.org.pl