Opened 8 years ago

Last modified 8 years ago

#120 new modification

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

Reported by: igloo@… Owned by: none
Milestone: Keywords:
Cc: Section:
State:

Description

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
C
*Main> f'
*** Exception: Adding A
*Main> s
C
*Main>

while hugs and this proposal say:

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

Change History (1)

comment:1 Changed 8 years ago by igloo@…

  • component changed from HaskellPrime to Proposal
  • Owner changed from ijones to none
Note: See TracTickets for help on using tickets.