When we talk about a compiler that optimizes intermediate lists, we usually talk about "fusion" implemented in the GHC RULESpragma; You can talk about how this works, and which list features are “good consumers” and “producers” here .
Unfortunately, this does not seem to be spana "good producer." You can see for yourself by asking to see the output of the GHC core and get a list of the rules that were launched usingghc -O2 -ddump-simpl -dsuppress-module-prefixes -dsuppress-uniques -ddump-core-stats -ddump-inlinings -ddump-rule-firings test.hs
Here is the cleared output:
Rule fired: Class op >=
Rule fired: SPEC Data.List.sum
Inlining done: geInt{v r3n} [gid]
Inlining done: sum_sum1{v rkV} [gid]
Inlining done: span{v r1Q} [gid]
Inlining done: sum_sum'1{v rl6} [gid]
==================== Tidy Core ====================
Result size of Tidy Core = {terms: 24, types: 27, coercions: 0}
addPos1 :: Int -> Bool
addPos1 = \ (ds :: Int) -> case ds of _ { I# x -> >=# x 0 }
addPos [InlPrag=INLINE[0]] :: [Int] -> (Int, [Int])
addPos =
\ (w :: [Int]) ->
case $wspan @ Int addPos1 w of _ { (# ww1, ww2 #) ->
case $wsum' ww1 0 of ww3 { __DEFAULT -> (I
}
, - / span, sum.
, vector , , .