Do What I Mean RULES for foldr2 look shady
There's a comment in the source:
The foldr2/right rule isn't exactly right, because it changes
the strictness of foldr2 (and thereby zip)
E.g. main = print (null (zip nonobviousNil (build undefined)))
where nonobviousNil = f 3
f n = if n == 0 then [] else f (n-1)
I'm going to leave it though.
This rule is intended to allow foldr2
to fuse with either argument list. There are thus two problems, one already documented and the other not:
- The rule can turn working code into non-working code, although this seems to be relatively unlikely. (The problem the above example is showing is that if the left list ends at the same time the right list bottoms out, the world goes boom. So
foldr2 f [1,2,3,4] (1:2:3:4:undefined)
appears to be a problem. You could argue this is not a big deal, I suppose. - The
foldr2/left
andfoldr2/right
rules are not confluent. They are both active in all phases, but if both list arguments are good producers, they will each want to rewrite the expression differently. So if you actually care about which one fuses, you need to explicitly block fusion with one argument usingNOINLINE
, which of course could easily muck up some other optimization.
My uninformed opinion: nix the foldr2/right
rule, and change the documentation to indicate that foldr2
fuses with its left argument.
Trac metadata
Trac field | Value |
---|---|
Version | 7.8.3 |
Type | Bug |
TypeOfFailure | OtherFailure |
Priority | normal |
Resolution | Unresolved |
Component | libraries/base |
Test case | |
Differential revisions | |
BlockedBy | |
Related | |
Blocking | |
CC | ekmett, hvr |
Operating system | |
Architecture |