Fix #15453: bug in ForAllCo case in opt_trans_rule
authorNingning Xie <xnningxie@gmail.com>
Sun, 29 Jul 2018 11:15:33 +0000 (13:15 +0200)
committerBen Gamari <ben@smart-cactus.org>
Wed, 1 Aug 2018 00:24:39 +0000 (20:24 -0400)
commiteb2b71c55df329570361e78f2f0ecdfcf3fe5974
tree79a6d8ca6a61dda1be81ba514df7314c248dc2f6
parent79e136104922aa4dcb555084731a890294cda106
Fix #15453: bug in ForAllCo case in opt_trans_rule

Summary:
Given

```
co1 = \/ tv1 : eta1. r1
co2 = \/ tv2 : eta2. r2
```

We would like to optimize `co1; co2` so we push transitivity inside forall.
It should be

```
\/tv1 : (eta1;eta2).  (r1; r2[tv2 |-> tv1 |> eta1])
```

It is implemented in the ForAllCo case in opt_trans_rule in OptCoercion.
However current implementation is not right:

```
r2' = substCoWithUnchecked [tv2] [TyVarTy tv1] r2 -- ill-kinded!
```

This patch corrects it to be

```
r2' = substCoWithUnchecked [tv2] [mkCastTy (TyVarTy tv1) eta1] r2
```

Test Plan: validate

Reviewers: bgamari, goldfire, RyanGlScott

Reviewed By: RyanGlScott

Subscribers: rwbarton, thomie, carter

GHC Trac Issues: #15453

Differential Revision: https://phabricator.haskell.org/D5018

(cherry picked from commit 11de4380c2f16f374c6e8fbacf8dce00376e7efb)
compiler/types/OptCoercion.hs
testsuite/tests/simplCore/should_compile/T15453.hs [new file with mode: 0644]
testsuite/tests/simplCore/should_compile/all.T