Motivation for refineDefaultAlt

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Motivation for refineDefaultAlt

Matthew Pickering
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
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/ghc-devs
Reply | Threaded
Open this post in threaded view
|

RE: Motivation for refineDefaultAlt

GHC - devs mailing list

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 <[hidden email]> On Behalf Of Matthew Pickering
Sent: 11 May 2018 15:54
To: GHC developers <[hidden email]>
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
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/ghc-devs
Reply | Threaded
Open this post in threaded view
|

Re: Motivation for refineDefaultAlt

GHC - devs mailing list
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
<[hidden email]>:

> 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 <[hidden email]> On Behalf Of Matthew Pickering
> Sent: 11 May 2018 15:54
> To: GHC developers <[hidden email]>
> 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
> [hidden email]
> http://mail.haskell.org/cgi-bin/mailman/listinfo/ghc-devs
>
_______________________________________________
ghc-devs mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/ghc-devs
Reply | Threaded
Open this post in threaded view
|

Re: Motivation for refineDefaultAlt

Matthew Pickering
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
<[hidden email]> 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
> <[hidden email]>:
>> 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 <[hidden email]> On Behalf Of Matthew Pickering
>> Sent: 11 May 2018 15:54
>> To: GHC developers <[hidden email]>
>> 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
>> [hidden email]
>> http://mail.haskell.org/cgi-bin/mailman/listinfo/ghc-devs
>>
_______________________________________________
ghc-devs mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/ghc-devs