|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Type Synonym Instances
|
|
|
|
|
|
|
|
|
|
|
|
## Brief Explanation
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Haskell 98 permits only type constructors defined using `data` or `newtype` in instance heads.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
The proposal is to allow type synonyms (fully applied, as ever) in instance heads. These would be fully expanded before any other restrictions on instance heads were checked (see [FlexibleInstances](flexible-instances) and [OverlappingInstances](overlapping-instances)).
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Not very useful without either [FlexibleInstances](flexible-instances) or [UndecidableInstances](undecidable-instances).
|
|
|
|
|
|
|
|
|
|
|
|
## References
|
|
|
|
|
|
|
|
|
|
|
|
- [ Instance declarations](http://www.haskell.org/onlinereport/decls.html#instance-decls) in the Haskell 98 Report
|
|
|
|
- [ Instance declarations](http://www.haskell.org/ghc/docs/latest/html/users_guide/type-extensions.html#instance-decls) in the GHC's User's Guide.
|
|
|
|
|
|
|
|
## Tickets
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<table><tr><th>[\#70](https://gitlab.haskell.org//haskell/prime/issues/70)</th>
|
|
|
|
<td>allow TypeSynonymInstances</td></tr></table>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
## Pros
|
|
|
|
|
|
|
|
|
|
|
|
- Offered by GHC and Hugs for several years.
|
|
|
|
- Synonyms can cut large instance heads to a manageable size, e.g. when building a monad using monad transformers.
|
|
|
|
- Allows a library author to generalize types while retaining source compatibility with old client instances. e.g. replacing
|
|
|
|
|
|
|
|
```wiki
|
|
|
|
data T a = ...
|
|
|
|
```
|
|
|
|
|
|
|
|
with
|
|
|
|
|
|
|
|
```wiki
|
|
|
|
data GenT param a = ...
|
|
|
|
type T = GenT Default
|
|
|
|
```
|
|
|
|
|
|
|
|
## Cons
|
|
|
|
|
|
|
|
|
|
|
|
- Since constraints on the instance head refer to the expanded type, errors can be more obscure.
|
|
|
|
|
|
|
|
## Comment
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
This change would make type synonyms a bit more like newtypes. [NewtypeDeriving](newtype-deriving) makes newtypes a bit more like
|
|
|
|
type synonyms since they can be made to inherit properties of the underlying type, albeit (usefully) selectively.
|
|
|
|
Having both type synonyms and newtype is a bit confusing, and the more alike they are, the more confusing it is.
|
|
|
|
I guess looking for a single replacement is not an option for Haskell', but at least we should consider if
|
|
|
|
automatic derivation for newtypes does not supersede this one as it would make it significantly less tiresome to
|
|
|
|
introduce a newtype for purposes of abbreviation.
|
|
|
|
|
|
|
|
|