Checkable "minimal complete definitions"
#6028 (closed) suggested warning on cyclic unimplemented defaults. This doesn't work for the reasons mentioned there, among others (also e.g. `Alternative` has mutually recursive some
and many
methods, which shouldn't be warned about). Figuring out when to warn automatically seems hard.
But Haskell already has an ad-hoc mechanism for specifying which methods need to be implemented: A "minimal complete definition" specified in the comments of almost every class definition that has optional methods. Unfortunately comments are aren't compiler-checked. It seems that the simplest solution would be to specify these in a way that the compiler can understand.
The obvious approach is to add a pragma for it in the class definition. In particular, one could write a pragma for each "minimal set" of definitions, and the compiler could warn if none of them are implemented (and suggest which methods to implement). This lets us keep the convenience of default method implementations without losing safety. Without any pragmas, the compiler could fall back to the set "all methods without defaults", which is what it uses now.
It might look something like this:
class Functor m => Monad m where
return :: a -> m a
(>>=) :: m a -> (a -> m b) -> m b
m >>= f = join (fmap f m)
join :: m (m a) -> m a
join m = m >>= id
{-# MINIMAL return, join #-}
{-# MINIMAL return, (>>=) #-}
class Eq a where
(==), (/=) :: a -> a -> Bool
x == y = not (x /= y)
x /= y = not (x == y)
{-# MINIMAL (==) #-}
{-# MINIMAL (/=) #-}
Trac metadata
Trac field | Value |
---|---|
Version | 7.6.1 |
Type | FeatureRequest |
TypeOfFailure | OtherFailure |
Priority | normal |
Resolution | Unresolved |
Component | Compiler |
Test case | |
Differential revisions | |
BlockedBy | |
Related | #6028 (closed) |
Blocking | |
CC | |
Operating system | |
Architecture |