Skip to content

Data.ByteString.Lazy.elemIndices returns wrong results

elemIndices for Lazy ByteString returns wrong results occasionally for huge input.

-- lei.hs
import qualified Data.ByteString.Lazy as L
import Control.Monad

main = do
  n <- fmap (length . L.elemIndices 10) L.getContents
  putStrLn (show n)
$ ghc --make lei
[1 of 1] Compiling Main             ( lei.hs, lei.o )
Linking lei ...
$ yes | head -1000000 | ./lei
1000000
$ yes | head -1000000 | ./lei
999992
$ yes | head -1000000 | ./lei
999960
$ yes | head -1000000 | ./lei
1000000
$ yes | head -1000000 | ./lei
999976

Without getContents, elemIndices works fine.

import qualified Data.ByteString.Lazy as L
import System.Environment

main = do
  n <- fmap (read . head) getArgs
  let cs = L.pack . take (2*n) . cycle $ [48,10]
      ps = L.elemIndices 10 cs
  print (length ps)
$ ghc --make lei2.hs
[1 of 1] Compiling Main             ( lei2.hs, lei2.o )
Linking lei2 ...
$ ./lei2 1000000
1000000
$ ./lei2 1000000
1000000

So I think there is a problem about bytestring IO.

And I heard this isn't reproduced in Windows' command prompt.

This ticket is based on a thread of haskell-jp.

Trac metadata
Trac field Value
Version 6.10.4
Type Bug
TypeOfFailure OtherFailure
Priority normal
Resolution Unresolved
Component libraries/base
Test case
Differential revisions
BlockedBy
Related
Blocking
CC
Operating system
Architecture
To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information