为什么我从GHCi收到此警告?

时间:2010-09-30 17:49:41

标签: haskell warnings ghc case-statement overloaded-strings

我在模式匹配时收到一个好奇的警告,但只有当OverloadedStrings被启用时...

$ ghci -Wall
GHCi, version 6.12.1: http://www.haskell.org/ghc/  :? for help
Loading package ghc-prim ... linking ... done.
Loading package integer-gmp ... linking ... done.
Loading package base ... linking ... done.
Prelude> let f x = case (x :: [String]) of {[""] -> "root"; ["product", _] -> "product"; _ -> "unknown"}
Prelude> :q
Leaving GHCi.
$ ghci -Wall -XOverloadedStrings
GHCi, version 6.12.1: http://www.haskell.org/ghc/  :? for help
Loading package ghc-prim ... linking ... done.
Loading package integer-gmp ... linking ... done.
Loading package base ... linking ... done.
Prelude> let f x = case (x :: [String]) of {[""] -> "root"; ["product", _] -> "product"; _ -> "unknown"}

<interactive>:1:10:
    Warning: Pattern match(es) are overlapped
             In a case alternative: [""] -> ...
Prelude> let g x = case (x :: [String]) of {[] -> "root"; ["product", _] -> "product"; _ -> "unknown"}
Prelude> let h x = case (x :: [String]) of {["oops"] -> "root"; ["product", _] -> "product"; _ -> "unknown"}
Prelude> :q
Leaving GHCi.

我不明白为什么我会收到带有OverloadedStrings的f警告,特别是因为没有OverloadedStrings我没有得到f的警告,也没有收到g的警告{1}}或h,仅在第一种模式中与f不同(在所有情况下仅匹配一个特定值)。

假设这不是GHC中的错误,我错过了什么?

2 个答案:

答案 0 :(得分:4)

这是一个稍微简单的例子,它在GHC 6.12.3中显示了同样的问题:

f :: String -> Bool
f "" = True
f "a" = False

g :: String -> Bool
g "" = True
g "aa" = False

只有g获得与-XOverloadedStrings的重叠警告。我认为这必须是一个错误。

答案 1 :(得分:2)

编辑:基本上你想要这个(在匹配从(IsString b) => b转换回[Char]之后,但匹配是在一致的类型中完成的):

f :: [String] -> String
f = matchf

matchf :: (Show b, IsString a, Eq a, IsString b) => [a] -> b
matchf x = case x of [""] -> "root"; ["product", _] -> "product"; _ -> "unknown"

否则GHC会警告将"" :: String"" :: (Data.String.IsString t) => t(字面值)匹配。如果文字""正确默认为String,那么找出原因(可能是错误?)会很有趣:

Prelude> show ("" :: (Data.String.IsString t) => t)

<interactive>:1:0:
    Warning: Defaulting the following constraint(s) to type `String'

您的字符串必须派生Eq进行模式匹配才能使用-XOverloadedStrings。 String仍然只是带有-XOverloadedStrings的[Char],但字符串文字不是。

在不触发警告的情况下执行此操作的另一种方法:

test.hs:

import GHC.Exts(IsString(..))

newtype OString = OString String deriving (Eq, Show)
instance IsString OString where fromString = OString

f :: [OString] -> OString
f x = case (x :: [OString]) of {[""] -> "root"; ["product", _] -> "product"; _ -> "unknown"}

运行它:

$ ghci -Wall -XOverloadedStrings
GHCi, version 6.12.1: http://www.haskell.org/ghc/  :? for help
Loading package ghc-prim ... linking ... done.
Loading package integer-gmp ... linking ... done.
Loading package base ... linking ... done.
Prelude> :l test.hs
[1 of 1] Compiling Main             ( test.hs, interpreted )
Ok, modules loaded: Main.
*Main> f []
OString "unknown"
*Main> f [""]
OString "root"
*Main> f ["product"]
OString "unknown"
*Main> f ["product", "x"]
OString "product"

来源:http://www.haskell.org/ghc/docs/6.12.2/html/users_guide/type-class-extensions.html#overloaded-strings