Opened 4 years ago

Closed 4 years ago

Last modified 4 years ago

#9585 closed bug (fixed)

Note about semantics of (!!) is not specific enough, if it's correct

Reported by: dfeuer Owned by:
Priority: normal Milestone:
Component: libraries/base Version: 7.9
Keywords: Cc: hvr, ekmett
Operating System: Unknown/Multiple Architecture: Unknown/Multiple
Type of failure: Documentation bug Test Case:
Blocked By: Blocking:
Related Tickets: Differential Rev(s):
Wiki Page:


The definition of (!!) in GHC.List, copied below, has a vague note about semantics for the Prelude and HBC versions being different. I can't see the difference myself. If there really is a difference, the details should be in the comment.

-- | List index (subscript) operator, starting from 0.
-- It is an instance of the more general 'Data.List.genericIndex',
-- which takes an index of any integral type.
(!!)                    :: [a] -> Int -> a
xs     !! n | n < 0 =  error "Prelude.!!: negative index"
[]     !! _         =  error "Prelude.!!: index too large"
(x:_)  !! 0         =  x
(_:xs) !! n         =  xs !! (n-1)
-- HBC version (stolen), then unboxified
-- The semantics is not quite the same for error conditions
-- in the more efficient version.
xs !! (I# n0) | isTrue# (n0 <# 0#) =  error "Prelude.(!!): negative index\n"
              | otherwise          =  sub xs n0
                            sub :: [a] -> Int# -> a
                            sub []     _ = error "Prelude.(!!): index too large\n"
                            sub (y:ys) n = if isTrue# (n ==# 0#)
                                           then y
                                           else sub ys (n -# 1#)

Change History (2)

comment:1 Changed 4 years ago by nomeata

Resolution: fixed
Status: newclosed

After staring at the code for a few minutes, I also don’t see a difference – both evaluate n first, and then go down the list in the obvious manner.

Some archaeology reveals: ceb68b9118fa883e88abfaa532fc78f6640cf17f changed the prelude definition, and before the change, !! would evaluate the list before the number.

So you are right, the comment is wrong. I just removed it.

comment:2 Changed 4 years ago by Joachim Breitner <mail@…>

In 9d713150d87de07e132c1211eec956e0ae69aa7f/ghc:

Remove obsolete comment about (!!)

as spotted by David Feuer and reported as #9585.
The comment was right until ceb68b9118fa883e88abfaa532fc78f6640cf17f,
which is only 13 years ago :-).
Note: See TracTickets for help on using tickets.