2013-02-13 61 views
1

我想寫一個顯示實例來顯示格式良好的公式,但在模仿完整的語法後,我仍然面臨如下的錯誤。Haskell:顯示實例歧義

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實例的內容。

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

在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


如果你正在學習哈斯克爾我建議這些特殊資源:

+0

非常感謝Dacto,這是這麼久以來我感動哈斯克爾..謝謝你非常非常... – 2013-02-13 08:58:42

+0

不用擔心,玩得開心! – Dacto 2013-02-13 09:00:13