[GHC] #7881: Warning for pointless ranges like [5..2]

#7881: Warning for pointless ranges like [5..2] -----------------------------+---------------------------------------------- Reporter: mpe | Owner: Type: feature request | Status: new Priority: normal | Component: Compiler Version: 7.6.3 | Keywords: beginner,warning Os: Unknown/Multiple | Architecture: Unknown/Multiple Failure: None/Unknown | Blockedby: Blocking: | Related: -----------------------------+---------------------------------------------- Haskell beginners sometimes tend to write ranges like [3..1] or [5..2] and assume that the result will be [3,2,1] or [5,4,3,2]. This is not the case of course. I suggest that literals like [5..2] generate a warning at compile time, analogous to: "Foo.hs:32: Warning: Literal list [5..2] evaluates to [] because 5 > 2 and the default step size is +1. Replace the literal with the empty list or with [5,4..2] to suppress this warning." In my opinion this should only be a compile time warning, not a runtime warning. If a > 3 and b is < 3 and you write [a..b] the result is still [], but that mistake can only be caught at runtime, and requires an additional run-time check. In some cases it may even be the desired behaviour to let [a..b] evaluate to [] if a>b. On the other hand, a literal expression like [5..2] is most likely an error, because if the user would expect [5..2] to evaluate to [], he would simply write []. -- Ticket URL: http://hackage.haskell.org/trac/ghc/ticket/7881 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

#7881: Warning for pointless ranges like [5..2] ---------------------------------+------------------------------------------ Reporter: mpe | Owner: Type: feature request | Status: new Priority: normal | Milestone: Component: Compiler | Version: 7.6.3 Keywords: beginner,warning | Os: Unknown/Multiple Architecture: Unknown/Multiple | Failure: None/Unknown Difficulty: Unknown | Testcase: Blockedby: | Blocking: Related: | ---------------------------------+------------------------------------------ Changes (by simonpj): * difficulty: => Unknown Comment: I think this is a good idea. The gotcha is this: {{{ newtype RInt = RI Int deriving( Eq, Show ) instance Ord RInt where compare (RI a) (RI b) = compare b a instance Enum RInt where enumFromTo (RI a) (RI b) = map (RI . negate) [negate a .. negate b] instance Num RInt where fromInteger n = RI (fromInteger n) main = print ([3..1] :: [RInt]) }}} Here `RInt` has an `eunuFromTo` implementation that doesn't produce an empty list! So you need to wait till after the type checker, and bleat only for empty ranges on known types like `Int`, `Integer`, `Char` and so on. Fortunately the desugarer, which runs afer the type checker, has the right plumbing to produce warnings. If someone wants to do this, I'm open to a patch. Simon -- Ticket URL: http://hackage.haskell.org/trac/ghc/ticket/7881#comment:1 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler
participants (1)
-
GHC