Default declarations come in two distinct forms. They share the keyword default and they both affect instances for type classes, but otherwise they serve quite different purposes.
For a given type class T, several instances are typically defined. It is permitted to define instances at overlapping types, such as e.g. T [a] and T [Int], where the latter type is more specific than the former.
During type-checking, the compiler needs to insert instances as implicit arguments to functions that use the selectors of T. The compiler infers which instance type T t that is needed and chooses the most specific instance. However, it may happen that there is no most specific type. The prelude defines intInt :: IntLiteral Int and intFloat :: IntLiteral Float and similarly for Show. Neither of this is more specific than the other. Thus a top-level definition such as
s = show 1leads to problems; should implicit arguments for Int or Float be inserted? There is no reason to prefer one to the other so s is considered ill-typed. But the matter can be decided by inserting a default declaration; the prelude declares
default intInt < intFloatwhich means that the instance intInt is to be preferred to intFloat. Thus the Int instances are chosen and s is "1" rather than "1.0".
A declaration
default tD :: T Dwhere T is a type class and D is a data type produces automatically an instance following the ideas of Hinze's and Peyton Jones' derivable type classes. The mechanism applies only in some, rather restrictive, situations, but these include common cases like the Prelude's Eq and Ord, for which it is easy but tedious to define instances for a new data type.
The method applies only to one-parameter type classes, for which the types of all selectors are simple, according to the following inductive definition:
In addition to the above mechanism, default instances can be defined for type classes Show and Parse (defined in the Prelude), but as yet only for enumeration types (pending decisions on proper definitions of predefined type classes for conversion between values of data types and strings).