compilation of pattern-matching?

Claus Reinke claus.reinke at
Mon Mar 23 19:46:27 EDT 2009

> How could you match the first case with less than two case constructs?
> There are two (:) to check for, so I'm not sure what you are complaining about.
>  -- Lennart
The number of case constructs is needed, and since case in Core 
also specifies strict contexts, perhaps there would be no difference,
which is why I'm asking about the rationale/documentation.

My idea was that case branches correspond to conditional jumps
(though the exact correspondence and optimization has been the
subject of countless papers). If I loop through a very long list,
most of the time the test for (:) will succeed, requiring no jump,
while the test for [] will fail, requiring a jump to the alternative
branch. So, if GHC's pattern-match compilation is naive, the
reordering will introduce 2 jumps into the common case of the
loop where none would be needed, right?


> On Tue, Mar 24, 2009 at 12:16 AM, Claus Reinke <claus.reinke at> wrote:
>> I just noticed that GHC (6.11.20090320) seems to compile both
>> f (a:b:c) =
>> f (a:[]) = f [] =
>> and
>> f [] = f (a:[]) = f (a:b:c) =
>> to something like (looking at Core, but writing source)
>> f x = case x of { [] -> ..; (a:t) -> case t of { [] ->..; (b:c) ->..}}
>> That doesn't seem right to me: if I try to give the patterns in
>> the order from frequent to rare, in order to reduce jumps, I
>> don't expect GHC to rearrange things. What is the rationale
>> for this? And where can I read about GHC's pattern match
>> compilation approach in general?
>> Claus
>> _______________________________________________
>> Glasgow-haskell-users mailing list
>> Glasgow-haskell-users at

More information about the Glasgow-haskell-users mailing list