<div dir="ltr">Wait, isn't call arity analysis meant to do this by itself now?</div><div class="gmail_extra"><br><div class="gmail_quote">On 7 October 2014 17:05, David Feuer <span dir="ltr"><<a href="mailto:david.feuer@gmail.com" target="_blank">david.feuer@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Just for the heck of it, I tried out an implementation of scanl using<br>
Joachim Breitner's magical oneShot primitive. Using the test<br>
<br>
scanlA :: (b -> a -> b) -> b -> [a] -> [b]<br>
scanlA f a bs = build $ \c n -><br>
    a `c`<br>
    foldr (\b g x -> let b' = f x b in (b' `c` g b'))<br>
          (const n)<br>
          bs<br>
          a<br>
<br>
scanlB :: (b -> a -> b) -> b -> [a] -> [b]<br>
scanlB f a bs = build $ \c n -><br>
    a `c`<br>
    foldr (\b g -> oneShot (\x -> let b' = f x b in (b' `c` g b')))<br>
          (const n)<br>
          bs<br>
          a<br>
<br>
f :: Int -> Bool<br>
f 0 = True<br>
f 1 = False<br>
{-# NOINLINE f #-}<br>
<br>
barA = scanlA (+) 0 . filter f<br>
barB = foldlB (+) 0 . filter f<br>
<br>
<br>
with -O2 (NOT disabling Call Arity) the Core from barB is really,<br>
really beautiful: it's small, there are no lets or local lambdas, and<br>
everything is completely unboxed. This is much better than the result<br>
of barA, which has a local let, and which doesn't seem to manage to<br>
unbox anything. It looks to me like this could be a pretty good tool<br>
to have around. It certainly has its limits—it doesn't do anything<br>
nice with reverse . reverse  or  reverse . scanl f b . reverse, but it<br>
doesn't need to be perfect to be useful. More evaluation, of course,<br>
is <a href="http://necessary.to" target="_blank">necessary.to</a> make sure it doesn't go wrong when used sanely.<br>
<br>
David<br>
_______________________________________________<br>
ghc-devs mailing list<br>
<a href="mailto:ghc-devs@haskell.org">ghc-devs@haskell.org</a><br>
<a href="http://www.haskell.org/mailman/listinfo/ghc-devs" target="_blank">http://www.haskell.org/mailman/listinfo/ghc-devs</a><br>
</blockquote></div><br></div>