... | ... | @@ -72,6 +72,106 @@ dataT a whereMkT:: forall b a. b ->T a |
|
|
|
|
|
In earlier GHCs, reifying `MkT` would have yielded a type headed by `ForallC [PlainTV a, PlainTV b]`. Now, reifying `MkT` will give a type headed by `ForallC [PlainTV b, PlainTV a]`, as one would expect.
|
|
|
|
|
|
### Deriving for empty data types
|
|
|
|
|
|
|
|
|
As part of [ this proposal](https://github.com/ghc-proposals/ghc-proposals/pull/63) was implemented, GHC now derives instances for empty data types (with no constructors) much differently than it did before. Here are the highlights:
|
|
|
|
|
|
- There is now an `EmptyDataDeriving` pragma which allows deriving `Eq`, `Ord`, `Read`, `Show` instances for empty data types. (Previously, this could only be done via `StandaloneDeriving`, but with `EmptyDataDeriving`, one can use `deriving` clauses to accomplish the same thing now.)
|
|
|
|
|
|
- Previously, the generated code for derived instances for empty data types was quite awful, involving lots of uses of `error`. What follows a list of what code GHC used to derive for empty data types, contrasted with what GHC now derives after this proposal was implemented (using a running example of `data Empty a`, with no constructors):
|
|
|
|
|
|
- Derived `Eq` and `Ord` instances would previously emit code that used
|
|
|
`error`:
|
|
|
|
|
|
```
|
|
|
instanceEq(Empty a)where(==)=error"Void =="instanceOrd(Empty a)where
|
|
|
compare =error"Void compare"
|
|
|
```
|
|
|
|
|
|
>
|
|
|
> Now, they emit code that uses maximally defined, lazier semantics:
|
|
|
|
|
|
```
|
|
|
instanceEq(Empty a)where_==_=TrueinstanceOrd(Empty a)where
|
|
|
compare __=EQ
|
|
|
```
|
|
|
|
|
|
- Derived `Read` instances would previous emit code that used
|
|
|
`parens`:
|
|
|
|
|
|
```
|
|
|
instanceRead(Empty a)where
|
|
|
readPrec = parens pfail
|
|
|
```
|
|
|
|
|
|
>
|
|
|
> But `parens` forces parts of the parsed string that it doesn't need to.
|
|
|
> Now, the derived instance will not use `parens` (that it, parsing
|
|
|
> `Empty` will always fail, without reading \*any\* input):
|
|
|
|
|
|
```
|
|
|
instanceRead(Empty a)where
|
|
|
readPrec = pfail
|
|
|
```
|
|
|
|
|
|
- Derived `Show` instances would previously emit code that used
|
|
|
`error`:
|
|
|
|
|
|
```
|
|
|
instanceShow(Empty a)where
|
|
|
showsPrec ="Void showsPrec"
|
|
|
```
|
|
|
|
|
|
>
|
|
|
> Now, they emit code that inspects the argument. That is, if the argument
|
|
|
> diverges, then showing it will also diverge:
|
|
|
|
|
|
```
|
|
|
instanceShow(Empty a)where
|
|
|
showsPrec _ x =case x of{}
|
|
|
```
|
|
|
|
|
|
- Derived `Functor`, `Foldable`, `Traversable`, `Generic`,
|
|
|
`Generic1`, `Lift`, and `Data` instances previously emitted code that
|
|
|
used `error`:
|
|
|
|
|
|
```
|
|
|
instanceFunctorEmptywhere
|
|
|
fmap =error"Void fmap"instanceFoldableEmptywhere
|
|
|
foldMap =error"Void foldMap"instanceTraversableEmptywhere
|
|
|
traverse =error"Void traverse"instanceGeneric(Empty a)where
|
|
|
from =M1(error"No generic representation for empty datatype Empty")
|
|
|
to (M1_)=error"No values for empty datatype Empty"-- Similarly for Generic1instanceLift(Empty a)where
|
|
|
lift _=error"Can't lift value of empty datatype Empty"instanceData a =>Data(Empty a)where
|
|
|
gfoldl ___=error"Void gfoldl"
|
|
|
toConstr _=error"Void toConstr"...
|
|
|
```
|
|
|
|
|
|
>
|
|
|
> Now, derived `Functor`, `Traversable, `Generic`, `Generic1\`,
|
|
|
> `Lift`, and `Data` instances emit code which inspects their
|
|
|
> arguments:
|
|
|
|
|
|
```
|
|
|
instanceFunctorEmptywhere
|
|
|
fmap _ x =case x of{}instanceTraversableEmptywhere
|
|
|
traverse _ x = pure (case x of{})instanceGeneric(Empty a)where
|
|
|
from x =M1(case x of{})
|
|
|
to (M1 x)=case x of{}-- Similarly for Generic1instanceLift(Empty a)where
|
|
|
lift x = pure (case x of{})instanceData a =>Data(Empty a)where
|
|
|
gfoldl _ x =case x of{}
|
|
|
toConstr x =case x of{}...
|
|
|
```
|
|
|
|
|
|
>
|
|
|
> Derived `Foldable` instances now are maximally lazy:
|
|
|
|
|
|
```
|
|
|
instanceFoldableEmptywhere
|
|
|
foldMap __= mempty
|
|
|
```
|
|
|
|
|
|
---
|
|
|
|
|
|
## Library changes
|
... | ... | |