The module PreludeLib contains generic operations to derive members of the standard prelude classess: Eq, Bounded, Compare, Show (TODO: add Enum and Read)
Although these classes may already be automatically derived via the deriving mechanism, this module is included for two reasons:
- Deriving only works when datatypes are defined. This library allows instances of these classes to be generated anywhere. For example, suppose some other module contains the definition of the datatype T and exposes all of its constructors, but, frustratingly, does not derive an instance of the Show class.
You could define a Show instance of
T in your own module with the
import RepLib (repr1 ''T) -- make the Rep1 instance of T available instance Show T where showsPrec = showsPrecR1 rep1 -- showsPrecR1 is defined in this module
- This library also serves as a model for generic functions that are slight modifications to these prelude operations. For example, if you wanted to define reverse lexicographic ordering or an XML pretty printer for datatypes, you might start here. This library is also a good place to start learning how to define your own generic operations, because the behavior of these operations should match the deriving mechanism specified by Haskell 98.