[cvs-nhc98] patch applied (nhc98): Prelude sum and product should really use foldl' not foldl.

Ian Lynagh igloo at earth.li
Fri Mar 2 09:51:09 EST 2007


On Fri, Mar 02, 2007 at 05:58:56AM -0800, Malcolm Wallace wrote:
> Thu Mar  1 08:54:37 PST 2007  Malcolm.Wallace at cs.york.ac.uk
>   * Prelude sum and product should really use foldl' not foldl.

Not according to the Haskell 98 report:

http://haskell.org/onlinereport/standard-prelude.html#$vsum

The difference is shown up by:

-------------------------------------------------
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 (6.6 and HEAD) agrees with the report:

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

while hugs (September 2006) disagrees:

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

I guess a decision should be made for Haskell', probably either going
with foldl' or introducing sum' and product'.


Thanks
Ian



More information about the Cvs-nhc98 mailing list