Superclass defaults
From HaskellWiki
(Difference between revisions)
m (more typos) |
m |
||
| Line 43: | Line 43: | ||
<li> | <li> | ||
A class alias is declared with the syntax | A class alias is declared with the syntax | ||
| - | <haskell>class alias | + | <haskell>class alias Ctx => Alias a = (Class1 a, Class2 a, ..) where ...</haskell> |
The body can contain default implementations of methods from <hask>Class1</hask>, <hask>Class2</hask> and their superclasses. | The body can contain default implementations of methods from <hask>Class1</hask>, <hask>Class2</hask> and their superclasses. | ||
</li><li> | </li><li> | ||
Current revision
John Meacham's class alias proposal conflates two different issues:
- Class aliases: A single name for multiple classes.
- Method defaults in aliases: Allow defaults across classes.
I think it would be better to separate the two.
In particular, we would likeMonad
Functor
fmap
Monad_
1 Superclass defaults
- A class declaration can contain method defaults for methods in that class and in its (indirect) superclasses.
-
An instance declaration can specify multiple classes
instance (Class1 a, Class2 a, ...) where ...
Subject to the constraint that:
- No class appears more than once in the list.
- The arguments to each class are the same.
- For each pair of classes, andClass1in the list:Class2
- is a (indirect) superclass ofClass1, orClass2
- is a (indirect) superclass ofClass2, orClass1
- andClass1have a common subclassClass2in the list.Class3
In other words, the superclass relation gives a connected acyclic graph with a single source, the most specific class in the heirarchy.
-
If no implementation of a method is given in such an instance declaration, a default is used. Multiple classes can give a default form. If bothmandClass1have a default implementation, andClass2is a (indirect) superclass ofClass1, then the default fromClass2is ignored.Class1
It is an error if more than one default remains after this process.
2 Class aliases
The above can be extended with class aliasses, this part of the propsal is exactly the same as John's.
-
A class alias is declared with the syntax
The body can contain default implementations of methods from
class alias Ctx => Alias a = (Class1 a, Class2 a, ..) where ...
,Class1and their superclasses.Class2 -
In a context, is treated the same asAlias a.(Head, Class1 a, Class2 a, ..)
-
In an instance head, is treated the same asAlias a,(_Alias a, Class1 a, Class2 a, ..)
whereis considered a subclass of_AliasandClass1that contains the default methods from the class alias body.Class2The nameclass (Head, Class1 a, Class2 a, ..) => _Alias a where ...
is a fresh name. I.e. it can not be refered to, it is used only for the purpose of this specification._Alias
