Deriving for phantom and empty types
authorDavid Feuer <david.feuer@gmail.com>
Thu, 30 Mar 2017 17:30:52 +0000 (13:30 -0400)
committerDavid Feuer <David.Feuer@gmail.com>
Thu, 30 Mar 2017 17:30:54 +0000 (13:30 -0400)
commit69f070d8e4d6043937e3405675ac911448bfcb44
tree022823fcccf914836dfd804e2facfd977492a8fa
parentff7094e5a80435ff68490c725029e762913a72d3
Deriving for phantom and empty types

Make `Functor`, `Foldable`, and `Traversable` take advantage
of the case where the type parameter is phantom. In this case,

* `fmap _ = coerce`
* `foldMap _ _ = mempty`
* `traverse _ x = pure (coerce x)`

For the sake of consistency and especially simplicity, make other types
with no data constructors behave the same:

* `fmap _ x = case x of`
* `foldMap _ _ = mempty`
* `traverse _ x = pure (case x of)`

Similarly, for `Generic`,

* `to x = case x of`
* `from x = case x of`

Give all derived methods for types without constructors appropriate
arities. For example,

```
    compare _ _ = error ...
```

rather than

```
    compare = error ...
```

Fixes #13117 and #13328

Reviewers: austin, bgamari, RyanGlScott

Reviewed By: RyanGlScott

Subscribers: ekmett, RyanGlScott, rwbarton, thomie

Differential Revision: https://phabricator.haskell.org/D3374
13 files changed:
compiler/typecheck/TcGenDeriv.hs
compiler/typecheck/TcGenFunctor.hs
compiler/typecheck/TcGenGenerics.hs
docs/users_guide/8.4.1-notes.rst [new file with mode: 0644]
docs/users_guide/glasgow_exts.rst
docs/users_guide/index.rst
testsuite/tests/deriving/should_compile/all.T
testsuite/tests/deriving/should_compile/drv-empty-data.hs [new file with mode: 0644]
testsuite/tests/deriving/should_compile/drv-empty-data.stderr [new file with mode: 0644]
testsuite/tests/deriving/should_compile/drv-phantom.hs [new file with mode: 0644]
testsuite/tests/deriving/should_compile/drv-phantom.stderr [new file with mode: 0644]
testsuite/tests/generics/T10604/T10604_deriving.stderr
testsuite/tests/perf/compiler/T13056.hs