import Data.List
a = foldl' (+) 0 [1..99999999]
main = putStrLn $ show $ a
This program takes a while to run. But a
does not depend on anything and thus is constant. It could be perfectly calculated at compile time. Why is not GHC optimizing for this? Is there a flag for it to do so, or should I just replace that kind of constant calculation by the values themselves?
This is not a perfect solution, but as kqr already remarked you can of course achieve your goal with Template Haskell:
{-# LANGUAGE TemplateHaskell #-}
import Language.Haskell.TH
import Data.List
a :: Integer
a = $( return . LitE . IntegerL $ foldl' (+) 0 [1..99999999] )
main = print a
This generates the integer literal 4999999950000000
from the fold expression, before actually starting to compile the program.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With