[Haskell] What are possible causes of "C stack overflow" in Hugs?

Koji Nakahara yu- at div.club.ne.jp
Thu Feb 5 16:38:48 EST 2004

On Wed, 04 Feb 2004 23:00:50 +0000
Graham Klyne <gk at ninebynine.org> wrote:

> I'm getting a "C stack overflow" error from Hugs when processing a 
> moderately large dataset, but not doing anything that looks unreasonably 
> complex or involved.
> I think the function graphLabels1 is set to call itself recursively to a 
> depth of a little over 1000.  Can this kind of recursion blow the Hugs stack?

"foldl f x xs" generally requires #xs (the number of elements of xs) stacks to be evaluated.
Note that ls(the second argument of graphLabels1) is not evaluated until
> graphLabels1 [] ls     = ls

>                           foldl (flip addSetElem) ls (arcLabels t)
ls is also defined by a foldl and its ls is also ...

So, let gs = [g_1, g_2, ..., g_N],
you need at least \sum_{n = 1}^N #g_n stacks.  Is'n it large?

The simplest and safest solution is to change foldl to foldl', but it produces
the unnecessary evaluation of `elem`s in addSetElem's.

I think that
> import DeepSeq 
> {- or
> f $!! x = force x `seq` f x
> 		where force [] = ()
> 					force (x:xs) = x `seq` force xs
> -}
> ...
> graphLabels1 (t:gs) ls = graphLabels1 gs $!! foldl (flip addSetElem) ls (arcLabels t
is better in your case.

Sorry for my poor English and hope it helps,

Koji Nakahara

More information about the Haskell mailing list