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

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. Prelude> sum[1..100000] 5000050000 Prelude> sum[1..1000000] *** Exception: stack overflow Prelude> ________________________ Hugs> sum[1..10000] 50005000 Hugs> sum[1..100000] 5000050000 Hugs> Thanks in anticipation. Best regards, Vishal Belsare ps: please excuse if the question is too uncool for the list. I come from an economics background; the markets are bad, I am depressed and to add to my pain, GHC refuses to let me know sum[1..100000] -- Firstly, I propose for myself the rule that the obviousness of a proposition does not free me from the obligation to continue searching for a proof of it, at least until I clearly realize that absolutely no proof could ever be required, and why. ~ Bernhard Bolzano

On Sun, Nov 23, 2008 at 6:07 AM, Vishal Belsare
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).
You might also learn you a haskell for great good with: http://learnyouahaskell.com
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.
You might try telling ghci to use optimizations. I but that will fix this problem. I can't reproduce your stack overflow locally (I even tried with bigger numbers). But, this might help: ghci -O then try your code. Jason

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

shoot.spam:
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.
Prelude> sum[1..100000] 5000050000 Prelude> sum[1..1000000] *** Exception: stack overflow Prelude>
________________________
Hugs> sum[1..10000] 50005000 Hugs> sum[1..100000] 5000050000 Hugs>
Thanks in anticipation.
Best regards, Vishal Belsare
Probably the simplest thing to do is compile the code, $ cat A.hs main = print (sum [1..1000000]) $ ghc -O2 A.hs $ time ./a.out 500000500000 ./a.out 0.09s user 0.00s system 96% cpu 0.100 total As Duncan pointed out, Hugs has a non-standard strict implementation of sum, which is nice, but breaks the spec if you do it at every type. GHC with optimisations determines that indeed this should be strict, replacing sum with a specialised version, but those optimisations are enabled in the compiler, not ghci. -- Don

On Sun, Nov 23, 2008 at 10:50 AM, Don Stewart
shoot.spam:
I started off with Hugs and recently used GHC (to use the 'let a = .." syntax interactively, which Hugs doesn't allow perhaps).
Probably the simplest thing to do is compile the code,
I rather doubt that's the simplest way to use the 'let...' syntax interactively, actually. /g -- I am in here
participants (5)
-
Don Stewart
-
Duncan Coutts
-
J. Garrett Morris
-
Jason Dagit
-
Vishal Belsare