I have the following code:
{-
Running GHC with the -ddump-simple flag gives:
[Arity 1 NoCafRefs Str: DmdType U(L)] Main.i2i = GHC.Real.toInteger1
Converting from Int to Integer seems to be lazy. Why is this so - is there a case where I can have
(toInteger _|_ ::Int) /= _|_
?
Edit: The question is more about the GHC rigor analyzer than about laziness per se. This code was obtained from studying the standard mean function:
--mean :: Integer -> Integer -> [Integer] -> Double mean :: Integer -> Int -> [Integer] -> Double mean acc n [] = fromIntegral acc / fromIntegral n mean acc n (x:xs) = mean (acc + x) (n + 1) xs main = print $ mean 0 0 [1..1000000]
This code works in O (N) space. When I uncomment the first line, the space consumption changes to O (1). It seems like it comes down to calling Integral, which in turn comes down to toInteger. Strictness analyst somehow cannot conclude that the transformation is strict, which seems strange to me.
source share