在 Haskell 中定义逻辑运算符

发布于 2024-12-19 07:46:56 字数 937 浏览 2 评论 0原文

在我的作业中,我必须定义逻辑运算符,如下所示:
使用这个数据结构:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

绳情 2024-12-26 07:46:56

你的教授强迫你为布尔运算符创建一个抽象语法树(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 type Formula -> Formula. In looking at the definition, I also believe you have left a line out of data Formula that should be something like VarLiteral MyBool. It looks like you AST is a way of writing programs which operate on MyBool'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

清晰传感 2024-12-26 07:46:56

问题在于函数前面的 : 表示数据构造函数;就像以大写字母开头一样。您应该将 :&: 重命名为 |&|.

编辑:没关系,我刚刚意识到您实际上想要完成的任务。

:&: 应该采用两个 MyBool 并创建一个 Formula 而不是另一个 MyBool。您尝试将 :&: 作为函数实现;它是一个数据构造函数。您已在 data Forumla = ... 表达式中声明了它。

您根本不需要函数声明。完全删除以下代码块:

infixr 3 :&:
(:&:) :: MyBool -> MyBool -> MyBool
Cierto :&: x = x
Falso :&: x = Falso

然后您应该能够使用 :&: 获取两个 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 two MyBools and create a Formula rather than another MyBool. You tried to implement the :&: as a function; it is a data constructor. You've already declared it in the data Forumla = ... expression.

You do not need the function declaration at all. Delete the following block of code completely:

infixr 3 :&:
(:&:) :: MyBool -> MyBool -> MyBool
Cierto :&: x = x
Falso :&: x = Falso

You should then be able to use :&: to take two MyBools and create a Formula without adding any other code.

However, having :&: actually act on MyBools is not general enough. We want to be able to and together both expressions and booleans. Thus, you actually want :&: to combine Formulas. This is what your code already does. What you are missing is a constructor like Literal that takes a MyBool and returns a Formula representing that boolean.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文