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

Graham Klyne gk at ninebynine.org
Wed Feb 4 23:00:50 EST 2004

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.

As far as I can tell, the code causing the problem is about here:
--  Return list of distinct labels used in a graph

graphLabels :: (Label lb) => [Arc lb] -> [lb]
graphLabels gs = graphLabels1 gs []

graphLabels1 (t:gs) ls = graphLabels1 gs $
                          foldl (flip addSetElem) ls (arcLabels t)
graphLabels1 [] ls     = ls

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?

This code has been working quite happily for some time.  The called 
function arcLabels is quite trivial, and addSetElem is pretty simple too:

addSetElem :: (Eq a) => a -> [a] -> [a]
addSetElem e es = if e `elem` es then es else e:es


I changed the function 'graphLabels' thus:
graphLabels :: (Label lb) => [Arc lb] -> [lb]
graphLabels gs = nub $ concat $ map arcLabels gs
and the C stack overflow goes away.  But I've been burned in the past with 
serious performance problems using nub, so I'm wary of this.


Graham Klyne
For email:

More information about the Haskell mailing list