sum[1..100000] --> stack overflow in GHC, but not in Hugs?

Duncan Coutts duncan.coutts at worc.ox.ac.uk
Sun Nov 23 10:22:06 EST 2008


On Sun, 2008-11-23 at 19:37 +0530, Vishal Belsare wrote:
> Hi,
> 
> Please bear with a very basic question. I am trying to 'learn me a
> Haskell for great good' using Hutton's book and some online tutorials.
> I started off with Hugs and recently used GHC (to use the 'let a  =
> .." syntax interactively, which Hugs doesn't allow perhaps).
> 
> There's a simple issue I am having with GHC. sum[1..100000] works fine
> on Hugs, however, the same in GHCi generates a stack overflow
> exception. I am trying to figure out what's going on.

Note:

Hugs is using the wrong definition of sum, that's why it has different
behaviour.

The Haskell98 report defines them as:

sum, product     :: (Num a) => [a] -> a
sum              =  foldl (+) 0  
product          =  foldl (*) 1

While hugs uses:

sum, product     :: Num a => [a] -> a
sum               = foldl' (+) 0
product           = foldl' (*) 1

Obviously these have different semantics (eg on lazy Num instances) as
well as different operational behaviour. Either hugs or the Prelude
should be fixed.

It is a bit odd that they're defined with foldl, since if they were
really meant to be lazy then why were they not defined with foldr?

It is being considered for Haskell':
http://hackage.haskell.org/trac/haskell-prime/ticket/120


Duncan



More information about the Glasgow-haskell-users mailing list