[Haskell-cafe] Unnecessarily strict implementations

Daniel Fischer daniel.is.fischer at web.de
Fri Sep 3 08:38:42 EDT 2010


On Friday 03 September 2010 12:28:43, Jan Christiansen wrote:
> Hi,
>
> On 03.09.2010, at 01:32, Daniel Fischer wrote:
> > No surprise, there aren't many 'ä's in Shakespeare's works, are there?
>
> nope
>
> > On the other hand, the current implementation of lines does not seem
> > to
> > suffer from Wadler's tuple space leak (according to one test I
> > made), so
> > I'd stick with the current implementation for the time being.
>
> Well, I think it does.

Indeed. I botched my test, allowing it to drop the reference to the first 
line too early by not using enough of it, so although lines shows the leak,

        let {
          ds1_s1ts :: ([Char], [Char])
          LclId

          ds1_s1ts =
            case $wbreak @ Char lvl_r1uj wild_B1
            of _ { (# ww1_anp, ww2_anq #) ->
            (ww1_anp, ww2_anq)
            } } in
        :
          @ String
          (case ds1_s1ts of _ { (l_aij, _) -> l_aij })
          (case ds1_s1ts of _ { (_, s'_ail) ->
           case s'_ail of _ {

, I managed to conceal it.


> But obviously one can argue that this is a rare application.

Yes. Ordinarily, lines in text files aren't longer than a few hundred 
characters, leaking those, who cares?
But. Occasionally, long lines occur, and avoiding the space leak seems more 
important to me than having
lines (_|_ : _|_) = _|_ : _|_
instead of
lines (_|_ : _|_) = _|_

> Hopefully I haven't made a mistake here?

No.

>
> By accident I stumbled across an odd behaviour. If I use the following
> definition and replace all occurrences of break by break' in the
> program above the memory behaviour is bad if I compile it without
> profiling. But if I compile the program with -prof -auto-all the
> program runs in constant space. Is this a known behaviour?

I can't reproduce that. For me, it leaks also with profiling.

>
> break' :: (a -> Bool) -> [a] -> ([a],[a])
> break' p xs = (ys,zs)
>   where
>    (ys,zs) = break p xs
>
> Cheers, Jan



More information about the Haskell-Cafe mailing list