Re: [GHC] #7994: Make foldl into a good consumer

#7994: Make foldl into a good consumer
-------------------------------------+------------------------------------
Reporter: simonpj | Owner:
Type: bug | Status: new
Priority: normal | Milestone:
Component: Compiler | Version: 7.6.3
Resolution: | Keywords:
Operating System: Unknown/Multiple | Architecture: Unknown/Multiple
Type of failure: None/Unknown | Difficulty: Unknown
Test Case: | Blocked By:
Blocking: | Related Tickets:
-------------------------------------+------------------------------------
Comment (by nomeata):
Ok, here is a very different approach to solving the original problem of
this ticket, namely making `foldl` a good consumer.
Instead of trying hard to make the compiler sufficiently smart, why not
help him a little bit? So here is what I did:
I created a new magic function `oneShot` (similar to `lazy` etc.).
Semantically, `oneShot = λ f λ x. f x", but the unfolding will put a
`OneShot` on the binder for x. So essentially, it allows the programmer to
tell the compiler: I promise I will only call this function once (and if I
break my promise, I won’t blame you).
So now one would define
{{{
foldl k z xs = foldr (\v fn -> oneShot (\z -> fn (k z v))) id xs z
}}}
(because at this point, we know that the result of `foldr ... id xs` is
''not'' going to be used multiple times) and voilà – good code:
{{{
Foo.$wfoo
:: GHC.Prim.Int# -> (# GHC.Types.Double, GHC.Types.Double #)
[GblId,
Arity=1,
Str=DmdType
participants (1)
-
GHC