Admin message

Due to a large amount of spam we do not allow new users to create repositories, they are "external" users. If you are a new user and want to create a repository, for example for forking GHC, open a new issue on ghc/ghc using the "get-verified" issue template

Eta-expansion of data constructors messes with RULEs
Consider the following example: ```hs expensive :: Int -> Int# expensive 0 = 2# expensive i = expensive (i-1) data D = MkD Int# Int f :: a -> Bool f _ = False {-# INLINE[0] f #-} {-# RULES "f/MkD" forall x. f (MkD x) = True #-} bar = f (MkD (expensive 10)) mkD :: Int# -> Int -> D mkD x y = MkD x y {-# OPAQUE mkD #-} {-# RULES "f/mkD" forall x. f (mkD x) = True #-} baz = f (mkD (expensive 10)) ``` The `f/mkD` rule fires as expected, but the `f/MkD` rule does not fire. That's the bug I'm reporting in this ticket. To analyse this, here are the RULES that GHC currently generates after typechecking: ```hs "f/MkD" forall (m :: Multiplicity). forall (x :: Int#). f @(Int %m -> D) (\ (y :: Int) -> MkD x y) = True "f/mkD" forall (x :: Int#). f @(Int -> D) (mkD x) = True ``` Let's work backwards, starting with `baz`. We want to apply the rule `forall x. f (mkD x) = True`. The expression we are given is `mkD (expensive 10)`, so we match on the rule, and because `expensive 10` is unlifted we case bind it, so we get: ```hs baz = case expensive 10 of { _ -> True } ``` This is confirmed by looking at the `-ddump-simpl` output. We should expect the same to happen for the data constructor `MkD`. The issue is that, as per Note [Typechecking of data constructors], we aggressively eta-expand all data constructors to make multiplicities line up. So we end up in the following situation: - The RULE is of the form `forall {m :: Multiplicity}. forall (x :: Int#). f @(Int %m -> D) (\ (y :: Int) -> MkD x y) = True`. - The expression we are matching on is of the form `f @(Int -> D) (case expensive 10 of { e -> \ (y :: Int) -> MkD e y })`. Hence the RULE unexpectedly fails to match. I was expecting the following to happen when typechecking the RULE `f/MkD`: 1. Start with `f (MkD x)`. 2. Typechecker: eta-expand `MkD` to get `f ((\ u v -> MkD u v) x)`. 3. Simple optimiser: beta-reduce the lambda to get `f (case x of { e -> \ v -> MkD e v})`. Then we would get a RULE template which would match in this example. However, in the simple optimiser, we decide (call to `exprOkForSpeculation` in `needsCaseBinding`) that it is OK to **not** introduce a case binding, so we instead end up with the RULE LHS `f (\ v -> MkD x v})` instead. This template doesn't apply on the Core `f (case expensive 10 of { e -> \ v -> MkD e v })`, so the RULE fails to match.
issue