我有一个文件OptionList.hs,有一个名为OptionList的新数据类型.我想在显示OptionList时隐藏EmptyOpt:
module OptionList ( OptionList, voidOption, (+:) ) where data OptionList a b = EmptyOpt | OptionList { optListHead :: a, optListTail :: b } deriving (Read) instance (Show a, Show b) => Show (OptionList a b) where show (OptionList voidOption a) = "{" ++ (show a) ++"}" show (OptionList a voidOption) = "{" ++ (show a) ++"}" show (OptionList a b) = "{"++ (show a) ++ ", " ++ (show b) ++"}" show voidOption = "" voidOption::(OptionList Int Int) voidOption = EmptyOpt (+:) :: a -> b -> (OptionList a b) infixr 5 +: t1 +: t2 = OptionList t1 t2
然后我有主文件 todo.hs
import OptionList main = do print ( 0 +: "test" +: voidOption )
但编译器告诉OptionList.hs中的模式匹配是重叠的:
OptionList.hs:12:9: Warning: Pattern match(es) are overlapped In an equation for ‘show’: show (OptionList a voidOption) = ... show (OptionList a b) = ...
当我执行它时,它确实是重叠的.它产生以下输出:
{{}}
(我希望它是{0, {"test"}}
)
但为什么这些模式重叠?
在线
show (OptionList voidOption a) = "{" ++ (show a) ++"}"
voidOption
是一个新的局部变量,就像a
是.它与voidOption
下面定义的变量没有关系.基本上,上面的行相当于
show (OptionList b a) = "{" ++ (show a) ++"}"
因此,它总是与以下行匹配并重叠.
如果我们将模式中的所有变量视为模式定义的变量,则很容易记住这一点.从某种意义上说,它们是价值"走出"的模式,而不是"在".
打开警告(-Wall
)应该警告你这个错误,因为voidOption
全局阴影的新本地绑定.
在模式中,您应该使用EmptyOpt
构造函数.例如
show (OptionList EmptyOpt a) = "{" ++ (show a) ++"}"