Haskell Wiki的Numeric Haskell页面的第2.3.1.1节"融合注释" 通过显示优化代码来解释循环融合,如下所示:
优化前:
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))
Run Code Online (Sandbox Code Playgroud)
优化后:
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)))
}
Run Code Online (Sandbox Code Playgroud)
我很好奇我怎么能看到像这样的优化代码.文章提到了ghc-core工具,但没有显示具体的命令.