Motivation for refineDefaultAlt

Matthew Pickering matthewtpickering at gmail.com
Fri May 11 15:54:45 UTC 2018


To round off Simon's email with a concrete example and explanation.

```
{-# LANGUAGE BangPatterns #-}
module Test where

mid x = x
{-# NOINLINE mid #-}

data Foo = Foo1 ()

test :: Foo -> ()
test x =
  case x of
    !_ -> mid (case x of
                Foo1 x1 -> x1)

```

refineDefaultAlt fills in the DEFAULT here with `Foo ip1` and then x
becomes bound to `Foo ip1` so is inlined into the other case which
causes the KnownBranch optimisation to kick in.

Simon J's point also seems plausible. Especially as it's called just
before combineIdenticalAlts.

Thanks everyone!



On Fri, May 11, 2018 at 4:17 PM, Simon Jakobi
<simon.jakobi at googlemail.com> wrote:
> Hi!
>
> I thought refineDefaultAlt was about scenarios like this:
>
> data D = C0 | C1 | C2
>
> case e of
>    DEFAULT -> e0
>    C0 -> e1
>    C1 -> e1
>
> When we apply combineIdenticalAlts to this expression, it can't
> combine the alts for C0 and C1, as we already have a default case.
>
> If we apply refineDefaultAlt first, we get
>
> case e of
>   C0 -> e1
>   C1 -> e1
>   C2 -> e0
>
> and combineIdenticalAlts can turn that into
>
> case e of
>   DEFAULT -> e1
>   C2 -> e0
>
> But that's just my own interpretation and possibly not the original motivation.
>
> Cheers,
> Simon
>
>
>
> 2018-05-11 17:03 GMT+02:00 Simon Peyton Jones via ghc-devs
> <ghc-devs at haskell.org>:
>> Because if e contains
>>
>>             …(case x of Foo p q -> e2)…
>>
>> as a sub-expression, we’d like to simplify it.
>>
>>
>>
>> Sorry that is not documented; please do add that to the comments in the
>> source code.
>>
>>
>>
>> Simon
>>
>>
>>
>> From: ghc-devs <ghc-devs-bounces at haskell.org> On Behalf Of Matthew Pickering
>> Sent: 11 May 2018 15:54
>> To: GHC developers <ghc-devs at haskell.org>
>> Subject: Motivation for refineDefaultAlt
>>
>>
>>
>> Hi all,
>>
>>
>>
>> Does anyone know the motivation for refineDefaultAlt?
>>
>> The comment states
>>
>> - -- | Refine the default alternative to a 'DataAlt', if there is a unique
>> way to do so.
>>
>> OK - so the code transforms something like
>>
>> case x of { DEFAULT -> e }
>> ===>
>>
>> case x of { Foo a1 a2 a3 -> e }
>>
>>
>> but why is this necessary or desirable?
>>
>> Perhaps you know Simon (Jakobi)?
>>
>> Cheers,
>>
>>
>>
>> Matt
>>
>>
>>
>>
>> _______________________________________________
>> ghc-devs mailing list
>> ghc-devs at haskell.org
>> http://mail.haskell.org/cgi-bin/mailman/listinfo/ghc-devs
>>


More information about the ghc-devs mailing list