4

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.

Cactus
  • 27,075
  • 9
  • 69
  • 149
Kwang Yul Seo
  • 771
  • 2
  • 7
  • 15

1 Answers1

5

In general, you want to look at GHC Core. The primary option to see core output is to use the -ddump-simpl for GHC, described here. There are also many flags to modify that output (to make it simpler), such as -dsuppress-all.

Information on actually reading core can be found here.

Community
  • 1
  • 1
crockeea
  • 21,651
  • 10
  • 48
  • 101