Evaluation order, ghc versus hugs, lazy vs. strict

Jan Kybic kybic@ieee.org
19 Aug 2002 23:19:41 +0200


Hello,
        I am wrestling with having Haskell (using ghc(i) version 5.02.2)
evaluate things in good order and to garbage collect what he no longer
needs. I could simplify the problem to this simple example:

main = print $ sum [0..1000000]

When you use a sufficiently large number (perhaps 10000000 instead of
1000000), both ghci and ghc terminate with "Stack space overflow".
On the other hand, Hugs (dated December 2001) runs the example OK
and produces the result of 500000500000.

I have the following questions:

1. Why does this happen? Why does not ghc Haskell generate the list
lazily, summing as he goes and forgetting what he no longer needs?

2. How can I find out what happens? The normal profiling 
( -prof -auto-all ) does not seem to be very helpful. Could not we
get some kind of tracing, printing out what is being evaluated?

3. How can I tell Haskell what I want ?
   a) that the (+) in sum is strict. It is in this case but in other more 
      complex, like  "foldl1 f [0..1000000]" I end up with closures
      f ( f ( f ( f ( ....)))) which are not evaluated until the end.

   b) that the list [0..1000000] should be lazy, e.g. evaluated 
      only as needed


I will be very grateful for any help. I am trying to use Haskell for
numerical calculations but so far I am not very satisfied:
the code is very elegant but runs of the order of many magnitudes 
slower than the corresponding C implementation. Please help me avoid
going to C again.

Thanks,
        Jan

-- 
-------------------------------------------------------------------------
Jan Kybic <kybic@ieee.org>      Odyssee, INRIA, Sophia-Antipolis, France
       or <Jan.Kybic@sophia.inria.fr>,tel. work +33 492 38 7589, fax 7845
                    http://www-sop.inria.fr/robotvis/personnel/Jan.Kybic/