Skip to content

Align simplifyDeriv with other inference-related validity checks

Ryan Scott requested to merge wip/T25621 into master

Previously, the validity checks that simplifyDeriv employed to check inferred contexts in derived instances were stricter than the validity checks used for inferred contexts in let-bindings. One undesirable consequence of this is that a derived instance would reject an equality constraint that a let-binding would happily infer (see #25621).

This commit fixes this by making the validity checks used for type inference in derived instances and for let-bindings more uniform. Although these are implemented in different parts of the code, we now make an effort to share as much code as possible in the two places in order to keep their validity checks in sync. We also relax many of the conservative validity checks in validDerivPred (which was previously responsible for rejecting equality constraints that a let-binding would happily accept) in favor of the checks in approximateWC. See the newly revamped Note [Valid 'deriving' predicate] in GHC.Tc.Validity for the full details.

In principle, this commit should make deriving accept strictly more instances than it did before. This can be seen in the GHC test suite: several test cases' stderr contents now become shorter due to not complaining about inferred constraints that were previously rejected. In two particular cases, T8984 and T22696b, test cases that GHC previously rejected outright are now accepted.

The T8984 test case now being accepted is somewhat interesting, as the inferred constraint (Coercible (cat a (N cat a Int)) (cat a (cat a Int))) is particularly exotic-looking, but nevertheless, approximateWC accepts it. It is a bit unclear if we want approximateWC to accept these exotic-looking Coercible constraints long-term, but we will defer that discussion for a later time.

Fixes #25621.

Merge request reports

Loading