7.16.1. Using generics

Import the module Generics from the
lang package. This import brings into
scope the data types Unit,
:*:, and :+:. (You
don't need this import if you don't mention these types
explicitly; for example, if you are simply giving instance
declarations.)

7.16.2. Changes wrt the paper

Note that the type constructors :+: and :*:
can be written infix (indeed, you can now use
any operator starting in a colon as an infix type constructor). Also note that
the type constructors are not exactly as in the paper (Unit instead of 1, etc).
Finally, note that the syntax of the type patterns in the class declaration
uses "{|" and "|}" brackets; curly braces
alone would ambiguous when they appear on right hand sides (an extension we
anticipate wanting).

7.16.3. Terminology and restrictions

Terminology. A "generic default method" in a class declaration
is one that is defined using type patterns as above.
A "polymorphic default method" is a default method defined as in Haskell 98.
A "generic class declaration" is a class declaration with at least one
generic default method.

Restrictions:

Alas, we do not yet implement the stuff about constructor names and
field labels.

A generic class can have only one parameter; you can't have a generic
multi-parameter class.

A default method must be defined entirely using type patterns, or entirely
without. So this is illegal:

However it is perfectly OK for some methods of a generic class to have
generic default methods and others to have polymorphic default methods.

The type variable(s) in the type pattern for a generic method declaration
scope over the right hand side. So this is legal (note the use of the type variable ``p'' in a type signature on the right hand side:

Here, op1, op2, op3 are OK, but op4 is rejected, because it has a type variable
inside a list.

This restriction is an implementation restriction: we just haven't got around to
implementing the necessary bidirectional maps over arbitrary type constructors.
It would be relatively easy to add specific type constructors, such as Maybe and list,
to the ones that are allowed.

In an instance declaration for a generic class, the idea is that the compiler
will fill in the methods for you, based on the generic templates. However it can only
do so if

The instance type is simple (a type constructor applied to type variables, as in Haskell 98).

No constructor of the instance type has unboxed fields.

(Of course, these things can only arise if you are already using GHC extensions.)
However, you can still give an instance declarations for types which break these rules,
provided you give explicit code to override any generic default methods.

The option -ddump-deriv dumps incomprehensible stuff giving details of
what the compiler does with generic declarations.