Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does GHC only warn on partial implemented classes, and not error?

I think the title is already self explanatory, but here's an example anyway to show my point:

class Foo a where
    someFunction :: a -> a -> Bool

instance Foo Bool

When I compile this the compiler gives a warning:

Warning:
    No explicit method or default declaration for `someFunction'
    in the instance declaration for `Foo Bool'

Calling the function will now result in a runtime error. Why is this a warning, and not a compile-time error? And is there any way to make this a compile-time error instead?

like image 735
Tiddo Avatar asked Mar 25 '15 10:03

Tiddo


1 Answers

The GHC documentation provides an example where a warning is sufficient:

-fwarn-missing-methods:

This option is on by default, and warns you whenever an instance declaration is missing one or more methods, and the corresponding class declaration has no default declaration for them.

The warning is suppressed if the method name begins with an underscore. Here's an example where this is useful:

class C a where
  _simpleFn :: a -> String
  complexFn :: a -> a -> String
  complexFn x y = ... _simpleFn ...

The idea is that: (a) users of the class will only call complexFn; never _simpleFn; and (b) instance declarations can define either complexFn or _simpleFn.

The MINIMAL pragma can be used to change which combination of methods will be required for instances of a particular class. See Section 7.20.5, “MINIMAL pragma”.

That's the reason missing methods don't result in an error, but a warning. If you want to make warnings fatal, use -Werror. Since there is no -ferr-missing-methods, -Werror is the only way to make -fwarn-missing-methods a compiler error.

like image 55
Zeta Avatar answered Oct 04 '22 03:10

Zeta