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

Isaac Dupree isaacdupree at charter.net
Sun Sep 23 16:52:11 EDT 2007


(redirecting to haskell-cafe)

Tom Pledger wrote:
>  | >     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'') = ...

"let" already is lazy -- putting a tilde there has no effect.  In fact 
that tuple is produced from (x:r', ts'' ) even if (sqnc p ts') is _|_. 
(The "let" at the beginning of the function can be discovered to be 
strict by strictness analysis (which GHC does), because "r" is analyzed 
by the "case", and the pair needs to be deconstructed to find the value 
of "r".)

Isaac



More information about the Haskell-Cafe mailing list