summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSimon Jakobi <simon.jakobi@gmail.com>2021-03-19 01:23:30 +0100
committerSimon Jakobi <simon.jakobi@gmail.com>2021-08-01 15:43:59 +0200
commit4e91276a0850f5377ba217f94991768bf6a9de4b (patch)
treedd42fd76a88f78f3b1ed147306cbe2e1996d0d36
parent54d6b20192fe6fc244248c7766533a768c591bae (diff)
downloadhaskell-wip/sjakobi/genericLength-overflow-doc.tar.gz
base: Document overflow behaviour of genericLengthwip/sjakobi/genericLength-overflow-doc
-rw-r--r--libraries/base/Data/List.hs9
1 files changed, 9 insertions, 0 deletions
diff --git a/libraries/base/Data/List.hs b/libraries/base/Data/List.hs
index 2e2b1efde4..d2813bfa35 100644
--- a/libraries/base/Data/List.hs
+++ b/libraries/base/Data/List.hs
@@ -752,6 +752,15 @@ minimumBy cmp = fromMaybe (errorWithoutStackTrace "minimumBy: empty structure")
-- 3
-- >>> genericLength [1, 2, 3] :: Float
-- 3.0
+--
+-- Users should take care to pick a return type that is wide enough to contain
+-- the full length of the list. If the width is insufficient, the overflow
+-- behaviour will depend on the @(+)@ implementation in the selected 'Num'
+-- instance. The following example overflows because the actual list length
+-- of 200 lies outside of the 'Int8' range of @-128..127@.
+--
+-- >>> genericLength [1..200] :: Int8
+-- -56
genericLength :: (Num i) => [a] -> i
{-# NOINLINE [1] genericLength #-}
genericLength [] = 0