Section 2.3.1.1 "A note on fusion" of Haskell Wiki's Numeric Haskell page explains loop fusion by showing the optimised code as in the following:
Before optimization:
import qualified Data.Vector as V
test :: V.Vector Int -> Double
test = V.foldl (\ a b -> a * sqrt (fromIntegral b)) 0
create :: Int -> V.Vector Int
create n = (V.enumFromTo 1 n)
main = print (test (create 1000000))
After optimization:
main_$s$wfoldlM_loop :: Int# -> Double# -> Double#
main_$s$wfoldlM_loop =
\ (sc_sWA :: Int#) (sc1_sWB :: Double#) ->
case <=# sc_sWA 1000000 of _ {
False -> sc1_sWB;
True ->
main_$s$wfoldlM_loop
(+# sc_sWA 1)
(*##
sc1_sWB (sqrtDouble# (int2Double# sc_sWA)))
}
I am curious about how I can see the optimised code like this. The article mentioned ghc-core tool, but didn't show the specific command.