To my

>> ghc-4.02  treats  sum  strangely.
>> In the below program,  sum xs  needs small, constant size stack,
>> sm xs  needs stack proportional to  length xs.
>> And  sm  is the implementation of  sum  shown in  src/.../PrelList.lhs
>> I apply  ghc -c -O.
>> [..]


Simon Peyton-Jones <[EMAIL PROTECTED]>  replies


> foldl looks like this:
> 
> foldl k z [] = z
> foldl k z (x:xs) = foldl k (z `k` x) xs
> 
> To work in constant space, foldl needs to evaluate its second argument
> before the call, rather than building a thunk that is later forced
> (which in turn builds stack).  But in general, foldl is not strict in z.
> [..]
> Alternatively, you can write
> 
> foldl_strict k z [] = z
>
> [..]



Thank you. I see. 

But it remains a puzzle:  ghc-4.02-i386-unknown-linux

performs `sum' of Prelude as if it was NOT compiled from the sources
of distribution.
The source -  src/.../PrelList.lhs  says
                              sum = foldl (+) 0                  
                              {-# SPECIALISE sum :: [Int] -> Int #-}

Testing `sum' in  ghc-4.02-i386-unknown-linux  
shows that it needs *small stack* for  sum [1..n]  :: Int.

But when the user defines `sm' exactly as above `sum', then 
sm [1..n] :: Int   needs large stack!

I am sorry, if i confuse something, but could somebody repeat this
experiment, explain it?


------------------
Sergey Mechveliani
[EMAIL PROTECTED]

Reply via email to