Haskell:显示实例歧义 [英] Haskell : Show instance Ambiguity

查看:92
本文介绍了Haskell:显示实例歧义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

 拥抱> :加载C:\\ Users \\Devil\\Desktop\\\CASESTUDY1.hs
错误文件:.\CASESTUDY1.hs:15 - 模糊变量发生显示
***可以参考:CASESTUDY1.show Hugs.Prelude.show

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

 模块CASESTUDY1 

其中

数据Wff = VAR字符串
| NEG Wff
| AND Wff Wff
|或Wff Wff
| IMPL Wff Wff

实例显示Wff其中
显示(VAR x)= x
显示(NEG x)=〜++显示(x)
显示(AND xy)=(++ show(x)++^++ show(y)++)
show(OR xy)=(++ show(x)+ +v++ show(y)++)
show(IMPL xy)=(++ show(x)++ - >++ show(y)++ )


解决方案

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

  instance显示Wff其中
显示(VAR x)=显示x
显示(NEG x)=〜++显示x
show(AND xy)=(++ show x ++^++ show y ++)
show(OR xy)=(++ show x ++ v++ show y ++)
show(IMPL xy)=(++ show x ++ - >++ show y ++)

另外,您不需要括号来传递参数来显示。 show(x)应该是 show x






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


I am trying to write a show instance to display well formed formula but after miming whole syntax I am still facing the same error as below.

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 

Below is the content of my .hs file include data type and related 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) ++ ")"

解决方案

In haskell, whitespace is important. You need to indent the show's that belong to your instance of 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 ++ ")"

Also, you do not need parenthesis to pass the parameters to show. show(x) should be show x.


If you are learning haskell I recommend these exceptional resources:

这篇关于Haskell:显示实例歧义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆