Refactor TcDeriv to validity-check less in anyclass/via deriving (#13154)

Due to the way `DerivEnv` is currently structured, there is an
invariant that every derived instance must consist of a class applied
to a non-empty list of argument types, where the last argument *must*
be an application of a type constructor to some arguments. This works
for many cases, but there are also some design patterns in standalone
`anyclass`/`via` deriving that are made impossible due to enforcing
this invariant, as documented in #13154.

This fixes #13154 by refactoring `TcDeriv` and friends to perform
fewer validity checks when using the `anyclass` or `via` strategies.
The highlights are as followed:

* Five fields of `DerivEnv` have been factored out into a new
  `DerivInstTys` data type. These fields only make sense for
  instances that satisfy the invariant mentioned above, so
  `DerivInstTys` is now only used in `stock` and `newtype` deriving,
  but not in other deriving strategies.
* There is now a `Note [DerivEnv and DerivSpecMechanism]` describing
  the bullet point above in more detail, as well as explaining the
  exact requirements that each deriving strategy imposes.
* I've refactored `mkEqnHelp`'s call graph to be slightly less
  complicated. Instead of the previous `mkDataTypeEqn`/`mkNewTypeEqn`
  dichotomy, there is now a single entrypoint `mk_eqn`.
* Various bits of code were tweaked so as not to use fields that are
  specific to `DerivInstTys` so that they may be used by all deriving
  strategies, since not all deriving strategies use `DerivInstTys`.
22 jobs for master in 943 minutes and 18 seconds (queued for 9 seconds)
Status Job ID Name Coverage
  Lint
passed #187664
lint
lint-linters

00:00:14

passed #187667
lint
lint-submods-branch

00:00:15

passed #187665
lint
lint-testsuite

00:00:28

passed #187666
lint
typecheck-testsuite

00:00:21

 
  Quick Build
passed #187668
x86_64-linux
hadrian-ghc-in-ghci

00:12:43

 
  Build
passed #187671
x86_64-linux
release-x86_64-linux-deb9-dwarf

01:55:08

passed #187670
x86_64-linux
validate-x86_64-linux-deb9-debug

02:46:56

passed #187669
x86_64-linux
validate-x86_64-linux-deb9-hadrian

02:21:42

 
  Full Build
failed #187673
aarch64-linux allowed to fail
validate-aarch64-linux-deb9

02:25:18

failed #187674
armv7-linux allowed to fail
validate-armv7-linux-deb9

12:00:00

passed #187675
x86_64-linux
validate-i386-linux-deb9

01:53:17

passed #187672
x86_64-darwin
validate-x86_64-darwin

00:57:52

passed #187676
x86_64-linux
validate-x86_64-linux-deb9-integer-simple

01:19:57

passed #187677
x86_64-linux
validate-x86_64-linux-deb9-unreg

02:06:33

passed #187678
x86_64-linux
validate-x86_64-linux-fedora27

01:56:19

failed #187680
x86_64-windows allowed to fail
validate-x86_64-windows

00:10:47

failed #187679
x86_64-windows allowed to fail
validate-x86_64-windows-hadrian

00:52:07

 
  Cleanup
passed #187681
x86_64-darwin
cleanup-darwin

00:00:10

 
  Packaging
failed #187682
x86_64-linux allowed to fail
doc-tarball

00:01:02

 
  Testing
manual #187683
x86_64-linux allowed to fail manual
hackage
passed #187684
x86_64-linux
perf-nofib

00:38:24

 
  Deploy
failed #187685
x86_64-linux allowed to fail
pages

00:03:09

 
Name Stage Failure
failed
pages Deploy
$ git fetch https://gitlab.haskell.org/ghc/ghc-performance-notes.git refs/notes/perf:refs/notes/perf || true
warning: no common commits
From https://gitlab.haskell.org/ghc/ghc-performance-notes
* [new ref] refs/notes/perf -> refs/notes/perf
$ mkdir -p public/doc
$ tar -xf haddock.html.tar.xz -C public/doc
tar: haddock.html.tar.xz: Cannot open: No such file or directory
tar: Error is not recoverable: exiting now
ERROR: Job failed: exit code 1
failed
validate-x86_64-windows-hadrian Full Build
Runtime platform                                    arch=amd64 os=windows pid=200 revision=1f513601 version=11.10.1
WARNING: ghc.tar.xz: no matching files
WARNING: junit.xml: no matching files
ERROR: No files to upload
Uploading artifacts...
Runtime platform
arch=amd64 os=windows pid=2604 revision=1f513601 version=11.10.1
WARNING: junit.xml: no matching files
ERROR: No files to upload
ERROR: Job failed: exit status 1
failed
validate-armv7-linux-deb9 Full Build The script exceeded the maximum execution time set for the job
=====> T11430(normal) 1580 of 7270 [0, 24, 0]
cd "ghc-api/annotations/T11430.run" && $MAKE -s --no-print-directory T11430 <
=====> load-main(normal) 1581 of 7270 [0, 24, 0]
cd "ghc-api/annotations/load-main.run" && $MAKE -s --no-print-directory load-main <
=====> T12417(normal) 1582 of 7270 [0, 24, 0]
cd "ghc-api/annotations/T12417.run" && $MAKE -s --no-print-directory T12417 <
=====> T13163(normal) 1583 of 7270 [0, 24, 0]
cd "ghc-api/annotations/T13163.run" && $MAKE -s --no-print-directory T13163 <
Pulling docker image gitlab/gitlab-runner-helper:arm-7137fd54 ...
ERROR: Job failed: execution took longer than 12h0m0s seconds
failed
validate-x86_64-windows Full Build
Runtime platform                                    arch=amd64 os=windows pid=1784 revision=1f513601 version=11.10.1
WARNING: ghc-*.tar.xz: no matching files
WARNING: junit.xml: no matching files
ERROR: No files to upload
Uploading artifacts...
Runtime platform
arch=amd64 os=windows pid=4380 revision=1f513601 version=11.10.1
WARNING: junit.xml: no matching files
ERROR: No files to upload
ERROR: Job failed: exit status 2
failed
validate-aarch64-linux-deb9 Full Build
Uploading artifacts...
WARNING: ghc-*.tar.xz: no matching files
WARNING: junit.xml: no matching files
WARNING: performance-metrics.tsv: no matching files
ERROR: No files to upload
Uploading artifacts...
WARNING: junit.xml: no matching files
ERROR: No files to upload
ERROR: Job failed: exit code 1
failed
doc-tarball Packaging
Synchronizing submodule url for 'nofib'
Synchronizing submodule url for 'utils/haddock'
Synchronizing submodule url for 'utils/hsc2hs'
$ git submodule update --init --recursive
$ git checkout .gitmodules
$ git fetch https://gitlab.haskell.org/ghc/ghc-performance-notes.git refs/notes/perf:refs/notes/perf || true
$ if [ ! -f "$LINUX_BINDIST" ]; then # collapsed multi-line command
Error: ghc-x86_64-mingw32.tar.xz does not exist. Did the 64-bit Windows job fail?
ERROR: Job failed: exit code 1