|
|
|
# Class Method Types
|
|
|
|
|
|
|
|
## Brief Explanation
|
|
|
|
|
|
|
|
|
|
|
|
In [ Haskell 98, s4.3.1](http://www.haskell.org/onlinereport/decls.html#class-decls), the signatures of methods in a class may contain constraints, but these constraints must not mention the argument of the type class. The following is illegal:
|
|
|
|
|
|
|
|
|
|
|
|
```wiki
|
|
|
|
class Foo a where
|
|
|
|
op :: Num a => a -> a -> a
|
| ... | ... | @@ -14,34 +14,8 @@ class Foo a where |
|
|
|
The motivation was perhaps that without such constraints, class dictionaries could be represented as records with [PolymorphicComponents](polymorphic-components).
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
However this restriction is not implemented by Hugs, following a suggestion of Mark Jones in
|
|
|
|
[ Typing Haskell in Haskell](http://www.cse.ogi.edu/~mpj/thih/), and can be turned off in GHC with `-fglasgow-exts`.
|
|
|
|
|
|
|
|
## interaction with existentials
|
|
|
|
|
|
|
|
|
|
|
|
If this were allowed than existential types of the form
|
|
|
|
|
|
|
|
```wiki
|
|
|
|
data SomeFoo = exists a . Foo a => SomeFoo a
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
would have to carry around a dictionary for Num as well as Foo in dictionary passing implementation of type classes if it wished to use methods of this form.
|
|
|
|
|
|
|
|
|
|
|
|
Typecase based implementations of classes such as jhcs do not have a problem with classes or existentials of this form since all classes are determined from the single type parameter no matter how many classes are actually needed.
|
|
|
|
|
|
|
|
## Examples
|
|
|
|
|
|
|
|
|
|
|
|
Can someone submit some real world examples of where this would be useful?
|
|
|
|
|
|
|
|
## Cons
|
|
|
|
|
|
|
|
- There are alternative type-preserving translations, so this doesn't really matter much anymore: In the absence of this restriction, a type-preserving translation of a type system including either [AssociatedTypes](associated-types) or [FunctionalDependencies](functional-dependencies) becomes much more difficult (see Section 4.3.4 of [ Associated Types with Class](http://www.cse.unsw.edu.au/~chak/papers/CKPM05.html), POPL'05, and [ClassMethodTypes/TypedTrans](class-method-types/typed-trans)). Even if we adopt neither of these two type system features for Haskell', we will impact future directions of Haskell.
|
|
|
|
|
|
|
|
## Tickets
|
|
|
|
|
|
|
|
<table><tr><th>[\#17](https://gitlab.haskell.org//haskell/prime/issues/17)</th>
|
|
|
|
<td>relax restriction on signatures of class methods</td></tr></table> |
|
|
\ No newline at end of file |