From 90c9c8352b9c0762b8d820cd5e03fee7728dd66f Mon Sep 17 00:00:00 2001
From: Simon Peyton Jones
Date: Thu, 14 Feb 2013 08:22:08 +0000
Subject: [PATCH] Make 'length' into a good consumer, fixing Trac #876
Trac #876 is the oldest ticket I have fixed in a long time.
I finally figured out how to make foldr behave in a non
space-leaky way for length.
Thanks to Andy for re-opening.
---
libraries/base/GHC/List.lhs | 23 ++++++++++++++++++-----
1 file changed, 18 insertions(+), 5 deletions(-)
diff --git a/libraries/base/GHC/List.lhs b/libraries/base/GHC/List.lhs
index 02d69659016..049aa2aac33 100644
--- a/libraries/base/GHC/List.lhs
+++ b/libraries/base/GHC/List.lhs
@@ -114,12 +114,25 @@ null (_:_) = False
-- | /O(n)/. 'length' returns the length of a finite list as an 'Int'.
-- It is an instance of the more general 'Data.List.genericLength',
-- the result type of which may be any kind of number.
+{-# NOINLINE [1] length #-}
length :: [a] -> Int
-length l = len l 0#
- where
- len :: [a] -> Int# -> Int
- len [] a# = I# a#
- len (_:xs) a# = len xs (a# +# 1#)
+length l = lenAcc l 0#
+
+lenAcc :: [a] -> Int# -> Int
+lenAcc [] a# = I# a#
+lenAcc (_:xs) a# = lenAcc xs (a# +# 1#)
+
+incLen :: a -> (Int# -> Int) -> Int# -> Int
+incLen _ g x = g (x +# 1#)
+
+-- These rules make length into a good consumer
+-- Note that we use a higher-order-style use of foldr, so that
+-- the accumulating parameter can be evaluated strictly
+-- See Trac #876 for what goes wrong otherwise
+{-# RULES
+"length" [~1] forall xs. length xs = foldr incLen I# xs 0#
+"lengthList" [1] foldr incLen I# = lenAcc
+ #-}
-- | 'filter', applied to a predicate and a list, returns the list of
-- those elements that satisfy the predicate; i.e.,
--
GitLab