Default method implementation
It's always a design question, whether to provide default implementions of methods of Type classes and how to design them.
Here are some suggestions:
- Make only trivial default implementations.
- Check that default implementations do not introduce type class dependencies that are not necessary otherwise
- E.g. if you have a
Monadconstraint, but no
Functorconstraint, then use
- If you have a choice of whether to implement the method default by custom code or by calling other methods of the same class, call methods of the same class. This reduces the amount of implementation work for class instances. It also makes it more probable, that no extra super-classes are needed.
- E.g. the default methods of the class providing
modshould implement the defaults, where
modand vice versa.
- Do not try to implement
modby repeated subtraction or so.
- Document which methods must be implemented at least.
- E.g. "instances must implement
- You may use a MINIMAL pragma so GHC can warn about incomplete instances.
- Instance implementations should not call other methods of the same class with respect to instantiated type.