Haskell:显示实例歧义

时间:2013-02-13 08:05:00

标签: haskell

我正在尝试编写一个show实例来显示格式良好的公式,但是在模拟整个语法之后,我仍然面临着与下面相同的错误。

Hugs> :load "C:\\Users\\Devil\\Desktop\\CASESTUDY1.hs"
ERROR file:.\CASESTUDY1.hs:15 - Ambiguous variable occurrence "show"
*** Could refer to: CASESTUDY1.show Hugs.Prelude.show 

以下是我的.hs文件的内容包括数据类型和相关的show instance。

module CASESTUDY1

where 

data Wff =   VAR String 
            | NEG Wff
            | AND Wff Wff 
            | OR Wff Wff
            | IMPL Wff Wff

instance Show Wff where
show (VAR x) = x
show (NEG x) = "~" ++ show(x)
show (AND x y) = "(" ++ show(x) ++ "^" ++ show(y) ++ ")"
show (OR x y) = "(" ++ show(x) ++ "v" ++ show(y) ++ ")"
show (IMPL x y) = "(" ++ show(x) ++ "-->" ++ show(y) ++ ")"

1 个答案:

答案 0 :(得分:4)

在haskell中,空白很重要。您需要缩进属于show实例的Show

instance Show Wff where
   show (VAR x)     = show x
   show (NEG x)     = "~" ++ show x
   show (AND x y)   = "(" ++ show x ++ "^" ++ show y ++ ")"
   show (OR x y)    = "(" ++ show x ++ "v" ++ show y ++ ")"
   show (IMPL x y)  = "(" ++ show x ++ "-->" ++ show y ++ ")"

此外,您不需要括号来传递要显示的参数。 show(x)应为show x


如果您正在学习haskell,我推荐这些特殊资源: