Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Minimal Complete Definition Annotations for Haskell

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?

like image 525
crockeea Avatar asked Jul 17 '13 01:07

crockeea


2 Answers

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.

like image 131
crockeea Avatar answered Nov 16 '22 01:11

crockeea


The two approaches I've seen are basically:

  1. 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.

  2. 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.

like image 31
Ben Avatar answered Nov 16 '22 02:11

Ben