[Haskell-cafe] Laziness through boxing

Chris Kuklewicz haskell at list.mightyreason.com
Tue Jan 16 16:19:14 EST 2007

C Rodrigues wrote:
> I had a problem with strictness in the Parsec library, and I'd like to
> know if there's a good way to solve it.  The following illustrates the
> problem.  This raises an error when run:
> main = parseTest (return undefined >> return 0) ""
> Whereas this does not:
> main = parseTest (return (Just undefined) >> return 0) ""
> I have a parser that does parsing and name resolution at the same time,
> by passing the completed symbol table in as a part of the parser state. 
> Lookups into the completed symbol table have to be lazy since that
> symbol table is not ready until parsing is complete.  My parser kept
> producing <<loop>> when it ran, which turned out to be an effect of
> strictness in Parsec.
> My solution was to box all the lazy values with Just.  The result feels
> awkward to me.  It involves fromJust, and moreover, it's easy to miss a
> place where boxing is required, and hard to track down the cause of a
> <<loop>>.  Is there a better way to deal with this issue?  And why is
> Parsec strict in return values?
> -Chris

To get better unboxing you can make your own box:

data Box a = Box {unBox :: a}

and then replace fromJust with unBox

More information about the Haskell-Cafe mailing list