[Haskell] simple function: stack overflow in hugs vs none in ghc

Tom Pledger tom at pledger.gen.nz
Sun Sep 23 16:11:32 EDT 2007

John Lask wrote:
  | The following code causes a "C stack overflow" in hugs (version 20051031)
  | but not in ghc (version 6.6)
  | The point of the exercise is to process a very large file lazily,
  | returning the consumed and unconsumed parts (i.e. basic parser  
  | >     sqnc p ts = let ( r, ts' ) = p ts in case r of
  | >                          Nothing -> ([],ts')
  | >                          Just x -> let (r',ts'') = (sqnc p ts')   
in (x:r', ts'' )

I don't know how ghc is avoiding the stack overflow, but it looks like  
it's caused by strict pattern matching.  The first call to sqnc wants  
reassurance that the second call is returning a pair (as opposed to  
_|_) before deciding that it's OK to return a pair.

Try putting a tilde in the recursive call, to make the pattern lazy.

     let ~(r',ts'') = ...

There's a recent thread on lazy patterns in the haskell-cafe list, too.



More information about the Haskell mailing list