[Haskell] Control.Monad.Writer as Python generator

ChrisK chrisk at MIT.EDU
Fri Apr 15 19:23:45 EDT 2005


On Apr 15, 2005, at 7:03 PM, Jan-Willem Maessen wrote:

>
> On Apr 15, 2005, at 6:07 PM, ChrisK wrote:
>
>>>> You are correct.  Moand.Cont yield even runs without -O optimizing,
>>>> just slower
>>>> ...
>>>> Anyone have an idea why ghci can't garbage collect it?
>>>> Is this an actual bug or an innate quirk of the REPL ?
>>>
>>> GHCi does not "compile" with optimizations, without -O the 
>>> strictness analyzer
>>> isn't run.
>>
>> The optimizer is irrelevant to whether it runs in constant space, as 
>> ghc without '-O' runs it just fine.  The optimizer is only useful for 
>> speed, which is not the issue.
>
> Not true!  The optimizer can change the asymptotic space consumption 
> of your program.  The example of sum is particularly germaine.  If we 
> write:
>
> x = foldl (+) 0 [1..n]
>
> this will, as it is evaluated, generate the suspended computation
> (((....((((0 + 1) + 2) + 3) + ...) + n
>
> This require O(n) space.
>

I was writing in the context of the yield with Monad.Cont example.  The 
'it' in 'whether it run in constant space' is the yield example of 
"length $ take (10^7) zerosInf".  Nothing more general.

So I agree with your statement, that asymptotic space consumption can 
change with optimization.  The odd thing is that ghc (without -O) 
compiles and run the example in question, but ghci will fail.  So I was 
wondering if there was an implementation of yield using continuations 
that would make yield space efficient in ghci.   Perhaps I should also 
check hugs, but I have not used hugs yet.

-- 
Chris



More information about the Haskell mailing list