Herbert Valerio Riedel | 19 Jul 2012 12:50
Picon
Gravatar

Why GHC doesn't warn about LHS nullary-constructor pattern bindings?

Hello,

Recently, I was a bit suprised that GHC didn't warn about useless
`where` definitions such as the following when using `-Wall` (and I
couldn't find a respective warning GHC CLI flag which would have enabled
reporting a warning in this case -- unless I missed it)

  module Foo where

  foo :: Int -> Int
  foo n = n + 1
    where
      Nothing = Just n

...wouldn't it be a sensible thing for GHC to warn in such cases
(i.e. when the LHS of a pattern binding is a nullary constructor), or is
there a useful application for this construct?

(In my original case, I ended up with such a "dead" construct after some
"unsound" code refactoring, and it would have helped me catch my error
earlier, if GHC would have pointed out that somethings fishy with my
code)
Henning Thielemann | 19 Jul 2012 12:56
Picon

Re: Why GHC doesn't warn about LHS nullary-constructor pattern bindings?


On Thu, 19 Jul 2012, Herbert Valerio Riedel wrote:

> Recently, I was a bit suprised that GHC didn't warn about useless
> `where` definitions such as the following when using `-Wall` (and I
> couldn't find a respective warning GHC CLI flag which would have enabled
> reporting a warning in this case -- unless I missed it)
>
>  module Foo where
>
>  foo :: Int -> Int
>  foo n = n + 1
>    where
>      Nothing = Just n

I think that

   where
     x <at> Nothing = Just n

could be useful, if 'x' is evaluated somewhere.
Christopher Done | 19 Jul 2012 13:07
Picon
Gravatar

Re: Why GHC doesn't warn about LHS nullary-constructor pattern bindings?

In your case the Nothing is unused so will never be a problem.

Perhaps more worrying:

foo :: Int -> Int
foo n = x + 1
    where
      Just x = Nothing

This gives no warnings.
Christian Maeder | 19 Jul 2012 14:20
Picon
Favicon

Re: Why GHC doesn't warn about LHS nullary-constructor pattern bindings?

You're right. That's a good case for a feature request.

In fact any binding that binds no variables should be warned about:

   let Just _ = ...

Cheers Christian

Am 19.07.2012 12:50, schrieb Herbert Valerio Riedel:
> Hello,
>
> Recently, I was a bit suprised that GHC didn't warn about useless
> `where` definitions such as the following when using `-Wall` (and I
> couldn't find a respective warning GHC CLI flag which would have enabled
> reporting a warning in this case -- unless I missed it)
>
>    module Foo where
>
>    foo :: Int -> Int
>    foo n = n + 1
>      where
>        Nothing = Just n
>
> ...wouldn't it be a sensible thing for GHC to warn in such cases
> (i.e. when the LHS of a pattern binding is a nullary constructor), or is
> there a useful application for this construct?
>
> (In my original case, I ended up with such a "dead" construct after some
> "unsound" code refactoring, and it would have helped me catch my error
> earlier, if GHC would have pointed out that somethings fishy with my
(Continue reading)


Gmane