compilation of pattern-matching?

Max Bolingbroke batterseapower at hotmail.com
Tue Mar 24 04:09:06 EDT 2009


2009/3/23 Claus Reinke <claus.reinke at talk21.com>:
> 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) ->..}}

In Core, case alternatives are stored in an order determined by the
"data constructor tag" of the thing being matched on - this is
independent of the order you wrote them in the source code. I believe
the reason for this is just to reduce the work done by the code
generator a tiny bit (and it's sometimes handy to know that the
default case, if any, is always the first alternative).

I don't know if preserving the order of the alts as written by the
user would be a significant gain for the code generator. Maybe codegen
should just output those tests for data alternatives that contain a
recursive use of the data type first - e.g. the cons constructor for
lists or the branch constructor for trees?

Cheers,
Max


More information about the Glasgow-haskell-users mailing list