在 Haskell 中定义逻辑运算符
在我的作业中,我必须定义逻辑运算符,如下所示:
使用这个数据结构:
data MyBool = Cierto|Falso deriving (Show,Eq) -- Cierto = True and Falso = False
data PQR = A|B|C|D|E|F|G|H|I|J|K|L|M|N|O|P|Q|R|S|T|U|V|W|X|Y|Z deriving (Show,Eq)
data Formula = VarProp PQR
|Neg Formula -- logic not
|Formula :|: Formula -- logic or
|Formula :&: Formula -- logic and... etc
|Formula :->: Formula
|Formula :<->: Formula deriving (Show,Eq)
我必须定义函数来告诉我给定的公式是 True 还是 False,所以例如,如果我写 (Cierto :&: Falso)
答案必须是: Falso
。
根据我的老师的说法,在这种情况下必须调用该函数 :&:
并且必须接收 MyBool
类型,所以我尝试像这样实现:
infixr 3 :&:
(:&:) :: MyBool -> MyBool -> MyBool
Cierto :&: x = x
Falso :&: x = Falso
但是当我尝试加载它说:
Invalid type signature
我不知道我在这里做错了什么。
In my homework I have to define the logic operators as follows:
Using this data structure:
data MyBool = Cierto|Falso deriving (Show,Eq) -- Cierto = True and Falso = False
data PQR = A|B|C|D|E|F|G|H|I|J|K|L|M|N|O|P|Q|R|S|T|U|V|W|X|Y|Z deriving (Show,Eq)
data Formula = VarProp PQR
|Neg Formula -- logic not
|Formula :|: Formula -- logic or
|Formula :&: Formula -- logic and... etc
|Formula :->: Formula
|Formula :<->: Formula deriving (Show,Eq)
And I have to define functions that tell me if a given formula is True or False, so for example if I write (Cierto :&: Falso)
the answer has to be: Falso
.
According to my teacher the function has to be called in this case :&:
and has to receive MyBool
types so I tried to implemented like this:
infixr 3 :&:
(:&:) :: MyBool -> MyBool -> MyBool
Cierto :&: x = x
Falso :&: x = Falso
but when I try to load it it says:
Invalid type signature
I don't know what I doing wrong here.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你的教授强迫你为布尔运算符创建一个抽象语法树(AST)。如果您在此之前没有听说过“抽象语法树”这个词,那么是时候问问别人到底是怎么回事了。
实际上,您想要做的是编写一个类型为
Formula ->; 的函数(称为
。在查看定义时,我还相信您在eval
)。公式data Formula
中留下了一行,该行应该类似于VarLiteral MyBool
。看起来你的 AST 是一种编写在 MyBool 上运行的程序的方法,并且支持典型的布尔运算以及:->:
分配(?)。我用 Haskell 写了一些 AST 评估器(虽然没有这么老土:)),感觉你的问题中缺少了一些内容。考虑到我所面临的情况,我对你最好的建议是,这项作业比你想象的更抽象。
祝你好运
Your professor is forcing you to create an Abstract Syntax Tree(AST) for boolean operators. if you have not heard the words "Abstract Syntax Tree" prior to this, it is time to ask someone what the heck is going on.
What you in fact want to do is write a function (called say
eval
) with the typeFormula -> Formula
. In looking at the definition, I also believe you have left a line out ofdata Formula
that should be something likeVarLiteral MyBool
. It looks like you AST is a way of writing programs which operate onMyBool
's and supports the typical boolean operations along with:->:
assign (?).I've written a few AST evaluators in Haskell (though nothing this corny :) ) and it feels like there are a few pieces missing in your question. My best advice for you, given what I have in front of me, is to say that this assignment is one level more abstract than you think it is.
Best of Luck
问题在于函数前面的:
表示数据构造函数;就像以大写字母开头一样。您应该将:&:
重命名为|&|
.编辑:没关系,我刚刚意识到您实际上想要完成的任务。
:&:
应该采用两个MyBool
并创建一个Formula
而不是另一个MyBool
。您尝试将:&:
作为函数实现;它是一个数据构造函数。您已在data Forumla = ...
表达式中声明了它。您根本不需要函数声明。完全删除以下代码块:
然后您应该能够使用
:&:
获取两个MyBool
并创建一个Formula
,而无需添加任何其他代码。然而,让
:&:
实际上作用于MyBool
还不够通用。我们希望能够将表达式和布尔值组合在一起。因此,您实际上希望:&:
组合Formula
。这就是您的代码已经执行的操作。您缺少的是像Literal
这样的构造函数,它接受MyBool
并返回表示该布尔值的Formula
。The issue is that a:
at the front of a function denotes a data constructor; it's like starting it with a capital letter. You should rename:&:
to something like|&|
.Edit: Never mind, I just realized what you're actually trying to accomplish.
The
:&:
is supposed to take twoMyBool
s and create aFormula
rather than anotherMyBool
. You tried to implement the:&:
as a function; it is a data constructor. You've already declared it in thedata Forumla = ...
expression.You do not need the function declaration at all. Delete the following block of code completely:
You should then be able to use
:&:
to take twoMyBool
s and create aFormula
without adding any other code.However, having
:&:
actually act onMyBool
s is not general enough. We want to be able to and together both expressions and booleans. Thus, you actually want:&:
to combineFormula
s. This is what your code already does. What you are missing is a constructor likeLiteral
that takes aMyBool
and returns aFormula
representing that boolean.