Possible Duplicate:
Can GHC warn if class instance is a loop?
Consider a type class with two methods which are implementable in terms of each other:
class Num a => Foo a where
foo :: a
bar :: a -> a
bar x = baz x + 1
baz :: a -> a
baz x = bar x - 1
Depending on the type, it might be easier to implement bar or baz, or you might want to give implementations of both of them for efficiency reasons.
Now I go somewhere else and make an instance of this class
instance Foo Integer where
foo = 1
Oops, I forgot to implement either of bar or baz! Never mind, the type system will pick that up for me, won’t it?
C:\path\to\file> ghci Foo.hs
GHCi, version 7.4.1: http://www.haskell.org/ghc/ :? for help
Loading package ghc-prim ... linking ... done.
Loading package integer-gmp ... linking ... done.
Loading package base ... linking ... done.
[1 of 1] Compiling Main ( Foo.hs, interpreted )
Ok, modules loaded: Main.
Hmm, apparently not. Now if I try to use my class
*Main> bar 1
<interactive>: out of memory
Uh oh. Cue hours of painful debugging.
Is there a way to let GHC know that each instance needs to specify at least one of bar or baz?
Unfortunately not. Most libraries that define type classes with defaults will specify a “minimal complete definition”, but they don’t specify that to GHC in a checkable way right now. There’s been some vague talk of implementing a pragma for this, but nothing serious as far as I know.
Note that just checking for mutual recursion isn’t sufficient; mutually recursive default methods might be perfectly valid, e.g.
someandmanyinAlternative.