I defined a NumericPrelude Ring
instance for my own data type, but failed to define one
or fromInteger
. When I compiled the program, I got no warnings because the Ring
class has default mutually recursive implementations for one
and fromInteger
. The result: a stack overflow that was very difficult to find. (Indeed, when using -XRebindableSyntax, the fromInteger
on numeric constants need not be explicit, so it was quite difficult to figure out fromInteger
was the culprit of the stack overflow.)
Is there a way for developers to annotate classes to indicate a minimal complete definition? It would be very helpful if GHC could throw a warning for instances which do not meet this definition, while allowing a complete set of default implementations. If not, what is the accepted practice here? Should developers leave the (a?) minimal set of methods without defaults so that the appropriate warnings are thrown, or do we rely on users to RTFM?
It looks like this may be in progress.
http://ghc.haskell.org/trac/ghc/ticket/7633 (and related: http://ghc.haskell.org/trac/ghc/ticket/6028)
It looks like it is set to be integrated in GHC 7.8.1.
UPDATE
Here's the minimal pragma in GHC 7.8.
The two approaches I've seen are basically:
Provide the defaults. Specify the minimal complete definition in the documentation (often you have options; with mutually recursive defaults you only need to implement enough of the methods to break the recursion, but you can choose any ones you like). Expect instance writers to read the documentation.
Don't specify the defaults, but provide functions with names like defaultImplementationOfFoo
. That basically forces the instance writer to explicitly ask for the defaults, without requiring them to actually provide the implementation of the default. But they still have to read the documentation to know that those functions exist.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With