... | ... | @@ -147,3 +147,53 @@ instance Traversable Square where |
|
|
to inhibit the `Functor` instance arising from `Monad` but retain that from `Traversable`. It is probably a good thing in any case to be clear about which instances should be generated and which not.
|
|
|
|
|
|
**Requirement 6** Except for managing the transition of legacy code, we should ensure that the meaning of an instance definition is clear only from its class declaration (and those of its superclasses) and not deduced from the presence or absence of other instances.
|
|
|
|
|
|
## Terminology and Notation
|
|
|
|
|
|
|
|
|
To nail down the technicalities of the proposal, we shall need names for things, and notation to present the things thus named.
|
|
|
|
|
|
|
|
|
Firstly, let us talk about the stuff which gets declared in classes, defined by default in classes, and defined in instances:
|
|
|
|
|
|
- The **immediate members** of a class C are the methods and associated type and data families which are declared in the class declaration for C.
|
|
|
- The **members** of a class C are the methods and associated type and data families which may be defined in instance definitions for C.
|
|
|
- A **defaulted member** of a class C is a member with a default definition which will be added to any C instance which does not contain an overriding definition.
|
|
|
- An **immediately defaulted member** of a class C is a member of class C which is given a default definition in the declaration of C.
|
|
|
|
|
|
|
|
|
In current Haskell, all members are immediate, but this proposal seeks to change that. Similarly, the only defaulted members we currently have are immediately defaulted immediate members. Data families cannot be defaulted (because that could lead to the duplication of data constructors), but methods and type families can be defaulted.
|
|
|
|
|
|
|
|
|
Next, let us fix terminology for talking about the superclasses of a class.
|
|
|
|
|
|
- An **immediate superclass** S of a class C is any class which heads a constraint in the declaration of C.
|
|
|
- A **superclass** of C is either C or a proper superclass of C.
|
|
|
- A **proper superclass** of C is a superclass of an immediate superclass of C.
|
|
|
|
|
|
|
|
|
Note that the use of "immediate" is consistent in that it applies to things which are introduced explicitly in class declarations.
|
|
|
|
|
|
|
|
|
Now, let us allow some superclass constraints in class declarations to be labelled with the `instance` keyword.
|
|
|
|
|
|
*decl* ::= ...
|
|
|
|
|
|
>
|
|
|
> \| `class` (*sups*`=>`)? *Name**name*+ `where`*members*
|
|
|
|
|
|
*sups* ::= *sup* \| `(`*sup*,\*`)`
|
|
|
|
|
|
*atom* ::= *Name**type*+
|
|
|
|
|
|
*sup* ::= *atom* \| `instance`*atom* (`-`*Name*+)
|
|
|
|
|
|
|
|
|
(Grammar grammar: postfix ? for 0 or 1, postfix + for 1 or more, postfix ,\* for 0 or more comma-separated)
|
|
|
|
|
|
|
|
|
We can say what are the "intrinsic" superclasses of a class, with "immediate" and "proper" used as above.
|
|
|
|
|
|
- An **immediate intrinsic superclass** S of a class C is any class *Name*d in an `instance`*sup* in the declaration of C.
|
|
|
- An **intrinsic superclass** of C is either C or a proper intrinsic superclass of C
|
|
|
- A **proper intrinsic superclass** of C is an intrinsic superclass of an immediate intrinsic superclass of C. |