[Haskell-cafe] out-commented code vs. case with redundant pattern matches

Sergiu Ivanov sivanov at colimite.fr
Wed Nov 29 10:43:20 UTC 2017


Hi Henning,

Thus quoth  Henning Thielemann  on Wed Nov 29 2017 at 11:04 (+0100):
> Occasionally I have multiple implementations of the same task and want to
> choose one quickly but statically. I do not want to out-comment unused
> branches because they shall still be type checked. So far I used this
> scheme:
>
>     case 0::Int of
>        0 -> putStrLn "A"
>        1 -> putStrLn "B"
>        _ -> putStrLn "C"

Maybe you can factor out the implementations of different branches into
separate functions, like in:

  case 0 :: Int of
    0 -> putStrLn "A"
    1 -> handleOne1
  where
    handleOne1 :: Int -> IO ()
    handleOne1 = putStrLn "B"

    handleOne1 :: Int -> IO ()
    handleOne2 = putStrLn "C"

This should keep the typechecking.

> With ghc-8.0.2 and ghc-8.2.2 I get these warnings:
>
> RedundantCase.hs:4:7: warning: [-Woverlapping-patterns]
>      Pattern match is redundant
>      In a case alternative: 0 -> ...
>
> RedundantCase.hs:5:7: warning: [-Woverlapping-patterns]
>      Pattern match is redundant
>      In a case alternative: 1 -> ...
>
> I thought that "redundant" means that the first two cases overlap with
> '_'. But if I replace '_' by '2' I get not only the non-exhaustive
> patterns warning but an additional redundancy warning on pattern '2'.
>
> Is there a nice way to tell GHC that the unused branches are intended,
> without generally disabling overlapping patterns warning?

To me, you are creating overlapping patterns in the case statements, so
having unused branches and _not_ having warnings about them kind of
breaks the point of this type of warning (personal opinion).

Now, depending on the localisation of alternative branches, you may want
to use per-file preprocessor directives to disable the warning.

--
Sergiu


> I mean, this one does not provoke any warnings:
>
>     if True
>       then putStrLn "X"
>       else putStrLn "Y"
>
> but is limited to two branches.
> _______________________________________________
> Haskell-Cafe mailing list
> To (un)subscribe, modify options or view archives go to:
> http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
> Only members subscribed via the mailman list are allowed to post.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 487 bytes
Desc: not available
URL: <http://mail.haskell.org/pipermail/haskell-cafe/attachments/20171129/f4bcf665/attachment.sig>


More information about the Haskell-Cafe mailing list