{-# LANGUAGE MagicHash #-}
{-# LANGUAGE CPP #-}
import Prelude hiding (foldl)
import GHC.Prim

-- ~0.4s real DOPEEL+DOUNROLL
-- ~0.4s real DOPEEL
-- stackoverflow without
main = print $ foldl (+) 0 [1..10000000::Int]

#ifdef DOPEEL
#ifdef DOUNROLL
-- simulate INLINE foldl PEEL 1 UNROLL1 (+SAT)
{-# INLINE foldl #-}
foldl op n l = foldlW n l
  where
  foldlW n (a:b:t) = foldlW ((n`op`a)`op`b) t
  foldlW n (a:t)   = foldlW (n`op`a) t
  foldlW n []      = n
#else
-- simulate INLINE foldl PEEL 1 (+SAT)
{-# INLINE foldl #-}
foldl op n l = foldlW n l
  where
  foldlW n (h:t) = foldlW (n`op`h) t
  foldlW n []    = n
#endif
#else
foldl op n (h:t) = foldl op (h`op`n) t
foldl op n []    = n
#endif

