Opened 5 years ago

Closed 5 years ago

#9510 closed bug (fixed)

Prelude.!! is not a good consumer

Reported by: dfeuer Owned by: ekmett
Priority: normal Milestone: 7.10.1
Component: Core Libraries Version: 7.8.3
Keywords: fusion Cc: hvr, ekmett, core-libraries-committee@…
Operating System: Unknown/Multiple Architecture: Unknown/Multiple
Type of failure: Runtime performance bug Test Case:
Blocked By: Blocking:
Related Tickets: Differential Rev(s): Phab:D380
Wiki Page:


I think we can probably do something like this, but I haven't checked the Core yet:

xs !! n
  | n < 0     = error "Prelude.!!: Negative index"
  | otherwise = foldr go (error "Prelude.!!: index too large or list empty") xs n
    go x _ 0 = x
    go _ f k = f (k - 1)

Change History (5)

comment:1 Changed 5 years ago by dfeuer

Note that, like the new last-as-foldl, this almost certainly won't be able to unbox the list elements, for the same reason.

comment:2 Changed 5 years ago by thoughtpolice

Component: libraries/baseCore Libraries
Owner: set to ekmett

Moving over to new owning component 'Core Libraries'.

comment:3 Changed 5 years ago by dfeuer

Cc: core-libraries-committee@… added
Differential Rev(s): Phab:D380
Status: newpatch

comment:4 Changed 5 years ago by Herbert Valerio Riedel <hvr@…>

In 75979f3661ff16ec44528a23005ac1be2b9683fe/ghc:

base: Refactor/clean-up *List modules

This gets rid of all hand-unboxing in `GHC.List` and moves `Foldable`
requirements from `Data.OldList` into `GHC.List` (preparatory work for
addressing #9716).  Specifically, this moves the definition of
`maximum`, `minimum`, `foldl'`, `foldl1`, `foldl1'`, `sum`, and
`product` into `GHC.List` (which now needs to import `GHC.Num`)

Make `take`, `drop`, `length`, and `!!` generally saner (see also #9510)

Performance overall seems minimally affected. Some things go up; some
things go down; nothing moves horribly much. The code is much easier to

Differential Revision:

comment:5 Changed 5 years ago by dfeuer

Milestone: 7.10.1
Resolution: fixed
Status: patchclosed
Note: See TracTickets for help on using tickets.