Another major constraint-solver refactoring
authorSimon Peyton Jones <simonpj@microsoft.com>
Tue, 25 Oct 2016 16:41:45 +0000 (17:41 +0100)
committerSimon Peyton Jones <simonpj@microsoft.com>
Fri, 25 Nov 2016 11:18:52 +0000 (11:18 +0000)
commit1eec1f21268af907f59b5d5c071a9a25de7369c7
tree818ea9214d94e0a3896ba675b52b737018a74a98
parent0123efde8090fc60a6bfef5943ba35440cec0c69
Another major constraint-solver refactoring

This patch takes further my refactoring of the constraint
solver, which I've been doing over the last couple of months
in consultation with Richard.

It fixes a number of tricky bugs that made the constraint
solver actually go into a loop, including

  Trac #12526
  Trac #12444
  Trac #12538

The main changes are these

* Flatten unification variables (fmvs/fuvs) appear on the LHS
  of a tvar/tyvar equality; thus
           fmv ~ alpha
  and not
           alpha ~ fmv

  See Note [Put flatten unification variables on the left]
  in TcUnify.  This is implemented by TcUnify.swapOverTyVars.

* Don't reduce a "loopy" CFunEqCan where the fsk appears on
  the LHS:
      F t1 .. tn ~ fsk
  where 'fsk' is free in t1..tn.
  See Note [FunEq occurs-check principle] in TcInteract

  This neatly stops some infinite loops that people reported;
  and it allows us to delete some crufty code in reduce_top_fun_eq.
  And it appears to be no loss whatsoever.

  As well as fixing loops, ContextStack2 and T5837 both terminate
  when they didn't before.

* Previously we generated "derived shadow" constraints from
  Wanteds, but we could (and sometimes did; Trac #xxxx) repeatedly
  generate a derived shadow from the same Wanted.

  A big change in this patch is to have two kinds of Wanteds:
     [WD] behaves like a pair of a Wanted and a Derived
     [W]  behaves like a Wanted only
  See CtFlavour and ShadowInfo in TcRnTypes, and the ctev_nosh
  field of a Wanted.

  This turned out to be a lot simpler.  A [WD] gets split into a
  [W] and a [D] in TcSMonad.maybeEmitShaodow.

  See TcSMonad Note [The improvement story and derived shadows]

* Rather than have a separate inert_model in the InertCans, I've
  put the derived equalities back into inert_eqs.  We weren't
  gaining anything from a separate field.

* Previously we had a mode for the constraint solver in which it
  would more aggressively solve Derived constraints; it was used
  for simplifying the context of a 'deriving' clause, or a 'default'
  delcaration, for example.

  But the complexity wasn't worth it; now I just make proper Wanted
  constraints.  See TcMType.cloneWC

* Don't generate injectivity improvement for Givens; see
  Note [No FunEq improvement for Givens] in TcInteract

* solveSimpleWanteds leaves the insolubles in-place rather than
  returning them.  Simpler.

I also did lots of work on comments, including fixing Trac #12821.
48 files changed:
compiler/iface/ToIface.hs
compiler/typecheck/TcCanonical.hs
compiler/typecheck/TcErrors.hs
compiler/typecheck/TcExpr.hs
compiler/typecheck/TcFlatten.hs
compiler/typecheck/TcInteract.hs
compiler/typecheck/TcMType.hs
compiler/typecheck/TcRnTypes.hs
compiler/typecheck/TcRules.hs
compiler/typecheck/TcSMonad.hs
compiler/typecheck/TcSimplify.hs
compiler/typecheck/TcType.hs
compiler/typecheck/TcUnify.hs
testsuite/tests/indexed-types/should_compile/T10226.hs
testsuite/tests/indexed-types/should_compile/T10634.hs
testsuite/tests/indexed-types/should_compile/T12526.hs [new file with mode: 0644]
testsuite/tests/indexed-types/should_compile/T12538.hs [new file with mode: 0644]
testsuite/tests/indexed-types/should_compile/T12538.stderr [new file with mode: 0644]
testsuite/tests/indexed-types/should_compile/T3017.stderr
testsuite/tests/indexed-types/should_compile/T4338.hs
testsuite/tests/indexed-types/should_compile/all.T
testsuite/tests/indexed-types/should_fail/T2544.stderr
testsuite/tests/indexed-types/should_fail/T2627b.stderr
testsuite/tests/indexed-types/should_fail/T3330c.stderr
testsuite/tests/indexed-types/should_fail/T4179.stderr
testsuite/tests/indexed-types/should_fail/T6123.stderr
testsuite/tests/indexed-types/should_fail/T7786.hs
testsuite/tests/indexed-types/should_fail/T7786.stderr
testsuite/tests/indexed-types/should_fail/T8227.stderr
testsuite/tests/partial-sigs/should_compile/T10403.stderr
testsuite/tests/perf/compiler/T5837.hs
testsuite/tests/perf/compiler/T5837.stderr [deleted file]
testsuite/tests/perf/compiler/all.T
testsuite/tests/polykinds/T12444.hs [new file with mode: 0644]
testsuite/tests/polykinds/T12444.stderr [new file with mode: 0644]
testsuite/tests/polykinds/T9222.stderr
testsuite/tests/polykinds/all.T
testsuite/tests/typecheck/should_compile/Improvement.hs
testsuite/tests/typecheck/should_compile/T6018.hs
testsuite/tests/typecheck/should_compile/T6018.stderr
testsuite/tests/typecheck/should_fail/ContextStack2.hs
testsuite/tests/typecheck/should_fail/ContextStack2.stderr [deleted file]
testsuite/tests/typecheck/should_fail/Makefile
testsuite/tests/typecheck/should_fail/T5691.stderr
testsuite/tests/typecheck/should_fail/T5853.stderr
testsuite/tests/typecheck/should_fail/T8450.stderr
testsuite/tests/typecheck/should_fail/T9260.stderr
testsuite/tests/typecheck/should_fail/all.T