[GHC] #9585: Note about semantics of (!!) is not specific enough, if it's correct
GHC
ghc-devs at haskell.org
Fri Sep 12 20:52:57 UTC 2014
#9585: Note about semantics of (!!) is not specific enough, if it's correct
-------------------------------------+-------------------------------------
Reporter: dfeuer | Owner:
Type: bug | Status: new
Priority: normal | Milestone:
Component: libraries/base | Version: 7.9
Keywords: | Operating System:
Architecture: Unknown/Multiple | Unknown/Multiple
Difficulty: Unknown | Type of failure:
Blocked By: | Documentation bug
Related Tickets: | Test Case:
| Blocking:
| Differential Revisions:
-------------------------------------+-------------------------------------
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.
{{{#!hs
-- | 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
#ifdef USE_REPORT_PRELUDE
xs !! n | n < 0 = error "Prelude.!!: negative index"
[] !! _ = error "Prelude.!!: index too large"
(x:_) !! 0 = x
(_:xs) !! n = xs !! (n-1)
#else
-- 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
where
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#)
#endif
}}}
--
Ticket URL: <http://ghc.haskell.org/trac/ghc/ticket/9585>
GHC <http://www.haskell.org/ghc/>
The Glasgow Haskell Compiler
More information about the ghc-tickets
mailing list