Ticket #2622 (closed bug: invalid)

Opened 5 years ago

Last modified 5 years ago

sum and product thunkpile

Reported by: Bart Massey Owned by:
Priority: normal Milestone:
Component: libraries/base Version: 6.9
Keywords: Cc:
Operating System: Unknown/Multiple Architecture: Unknown/Multiple
Type of failure: Difficulty: Unknown
Test Case: Blocked By:
Blocking: Related Tickets:

Description

Do "ulimit -v 524288" at the shell so your machine won't hang. Run ghci, bring in Data.List, and execute "sum [1..10**8]". Notice that you run out of memory. Now try again with "foldl1' (+) [1..10**8]". Note that you get an answer.

The definition of sum and product in the Report use foldl. However, the versions currently being used are open-coded for some reason. They should just use foldl1' (after handling the special case of the empty list appropriately). As it is, the functions thunkpile; this seems pointless, as they are ultimately completely strict in their argument.

This kind of bug is grim for new users, which is why the severity is marked "major".

Patch attached. This is just a context unidiff against current Darcs head; a Darcs-format patch with full context (at 350KB) is at  http://po8.org/bart/sumprod.patch

Attachments

sumprod.diff Download (0.6 KB) - added by Bart Massey 5 years ago.

Change History

Changed 5 years ago by Bart Massey

Changed 5 years ago by Bart Massey

Oh, BTW, maybe product needs to be open-coded (but not the current way!) so that it can be properly lazy on encountering a 0 in the list. I have mixed feelings about this; on the one hand, it's in the spirit of "maximal laziness", on the other hand, it's a bit strange.

Should I file another bug report or proposal for this?

Changed 5 years ago by simonmar

  • status changed from new to closed
  • difficulty set to Unknown
  • resolution set to invalid

We can't apply this patch, because it changes the semantics of sum & product. Note that sum is overloaded, and there's no requirement that (+) is strict for any given instance of Num.

When used at Int or Integer, we do indeed have strict specialised versions of sum, but these don't get used inside GHCi, because GHCi doesn't do optimisation. Perhaps there's a case that we should be doing this kind of optimisation in GHCi, but that's another story (and should be another ticket).

Changed 5 years ago by simonmar

  • architecture changed from Multiple to Unknown/Multiple

Changed 5 years ago by simonmar

  • os changed from Multiple to Unknown/Multiple
Note: See TracTickets for help on using tickets.