为了避免CAF(资源共享),我尝试转换为功能
使用伪参数,但没有成功(noCafB
)。
我看过How to make a CAF not a CAF in Haskell?,因此尝试了noCafC
和noCafD
。编译时
-O0
,然后每次都会评估带有伪参数的函数。
但是,对于-O2
,似乎GHC将这些函数转换为
CAF。这是预期的行为(GHC的优化)吗?
module Main where
import Debug.Trace
cafA :: [Integer]
cafA = trace "hi" (map (+1) $ [1..])
noCafB :: a -> [Integer]
noCafB _ = trace "hi" (map (+1) $ [1..])
noCafC :: a -> [Integer]
noCafC _ = trace "hi" (map (+1) $ [1..])
{-# NOINLINE noCafC #-}
noCafD :: a -> [Integer]
noCafD _ = trace "hi" (map (+1) $ myEnumFrom 0 1)
{-# NOINLINE noCafD #-}
myEnumFrom :: a -> Integer -> [Integer]
myEnumFrom _ n = enumFrom n
{-# NOINLINE myEnumFrom #-}
main :: IO ()
main = do
putStrLn "cafA"
print $ (cafA !! 1 + cafA !! 2)
putStrLn "noCafB"
print $ (noCafB 0 !! 1 + noCafB 0 !! 2)
putStrLn "noCafC"
print $ (noCafC 0 !! 1 + noCafC 0 !! 2)
putStrLn "noCafD"
print $ (noCafD 0 !! 1 + noCafD 0 !! 2)
-O2
$ stack ghc -- --version
The Glorious Glasgow Haskell Compilation System, version 7.10.3
$ stack ghc -- -O2 cafTest.hs
[1 of 1] Compiling Main ( cafTest.hs, cafTest.o )
Linking cafTest ...
$ ./cafTest
cafA
hi
7
noCafB
7
noCafC
7
noCafD
hi
7
-O0
$ stack ghc -- -O0 cafTest.hs
[1 of 1] Compiling Main ( cafTest.hs, cafTest.o )
Linking cafTest ...
$ ./cafTest
cafA
hi
7
noCafB
hi
hi
7
noCafC
hi
hi
7
noCafD
hi
hi
7
我也试过没有trace
,但结果是一样的。在-O2
下,我发现incInt
函数的结果是通过检查分析输出来共享的。为什么会出现这种情况?
incIntOrg :: [Integer]
incInt = map (+1) [1..]
incInt :: a -> [Integer] -- results IS shared. should it be?
incInt _ = map (+1) $ myEnum 0 1
{-# NOINLINE incInt #-}
myEnum :: a -> Integer -> [Integer]
myEnum _ n = enumFrom n
{-# NOINLINE myEnum #-}
main :: IO ()
main = do
print (incInt 0 !! 9999999)
print (incInt 0 !! 9999999)
print (incInt 0 !! 9999999)
任何评论都将深深感激。感谢。