Opened 10 years ago

Last modified 16 months ago

#120 new modification

Should sum and product be strict? Or have strict variants?

Reported by: igloo@… Owned by: none
Priority: normal Milestone:
Version: Keywords:
Cc: Meta Owner:
State: Section:
Related Tickets:


Currently sum and product are lazy according to the report. GHC matches the report, while hugs makes them strict. nhc98 recently made them strict, but reverted to lazy upon the report's definition being pointed out.

For most cases, such as summing a list of Ints or Integers, strict behaviour is desirable. Thus we propose that either sum and product are made strict (i.e. use foldl') or strict variants sum' and product' are made available as well.

Here is a program to demonstrate the difference:

import Data.List

data Foo = Zero | A | B | C
    deriving (Eq, Show)

instance Num Foo where
    _ + Zero = error "Adding Zero"
    _ + A = error "Adding A"
    _ + B = error "Adding B"
    _ + C = C
    fromInteger 0 = Zero

xs = [A, B, C]
f  = foldl  (+) 0 xs
f' = foldl' (+) 0 xs
s = sum xs

GHC and Haskell 98 say:

*Main> f
*Main> f'
*** Exception: Adding A
*Main> s

while hugs and this proposal say:

Main> f
Main> f'
Program error: Adding A
Main> s 
Program error: Adding A

Change History (3)

comment:1 Changed 10 years ago by igloo@…

  • component changed from HaskellPrime to Proposal
  • Owner changed from ijones to none

comment:2 Changed 16 months ago by hvr

  • Milestone set to

moving non-milestoned many year old legacy tickets out of the way

comment:3 Changed 16 months ago by hvr

  • Priority changed from minor to normal

Set default priority (as this confuses Trac otherwise)

Note: See TracTickets for help on using tickets.