1

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

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 文件的内容,包括数据类型和相关的显示实例。

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) ++ ")"
4

1 回答 1

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,我推荐这些特殊资源:

于 2013-02-13T08:13:07.513 回答