在prolog中定义运算符

时间:2018-05-25 12:32:51

标签: prolog

我正在做很多prolog练习来提高我的逻辑技能。但是我坚持练习的要求。

我要做的是定义一个运算符 i ,其方式是:如果用户使用此语法输入复数,则通过提示(因此我使用read(X)操作者)

2+ i 4

我得到的结果

import sys

from PyQt5.Qt import *  # noqa


class GuiBuilder():
    def __init__(self, widget, name):
        self.widget = widget
        self.name = name
        self.init_ui()

    def init_ui(self):
        self.widget.setWindowTitle(self.name)


class MainWindow(QMainWindow):

    def __init__(self, name):
        super().__init__()

        self.gui_builder = GuiBuilder(self, name)
        self.init_ui()

    def init_ui(self):
        self.statusBar().showMessage('chk')
        self.setGeometry(300, 300, 250, 150)
        self.gui_builder.init_ui()


def main():
    app = QApplication(sys.argv)

    for title in ["window1", "window2", "window3"]:
        ex = MainWindow(title)
        ex.show()

    sys.exit(app.exec_())


if __name__ == "__main__":
    main()

我已经了解了如何在Prolog中定义运算符,我已经研究过 op 运算符,但我不知道如何使该减法运算真正发生

1 个答案:

答案 0 :(得分:2)

你的第一个问题是import pygame class base_sprite(pygame.sprite.Sprite): def __init__(self, color=(0,0,0), width=0, height=0, image=None,x=0,y=0, scale=None): pygame.sprite.Sprite.__init__(self) if "Surface" in type(image).__name__: self.image = image else: self.image = pygame.image.load(image) if scale != None: self.image = pygame.transform.scale(self.image, (scale[0], scale[1])) pygame.draw.rect(self.image, color, [5000000,5000000,width,height]) self.rect = self.image.get_rect() self.rect.x = x self.rect.y = y pygame.init() width = 320 height = 240 s = pygame.display.set_mode((width, height)) clock = pygame.time.Clock() gameGroup = pygame.sprite.Group() inventoryGroup = pygame.sprite.Group() back = base_sprite(width=320, height=240, image="images/back.png", x=0, y=0) headBorder = base_sprite(width=64, height=64, image="images/ItemBorder.png", x=180, y=10, scale=[50, 50]) handBorder = base_sprite(width=64, height=64, image="images/ItemBorder.png", x=250, y=94, scale=[50, 50]) bodyBorder = base_sprite(width=64, height=64, image="images/ItemBorder.png", x=180, y=94, scale=[50, 50]) feetBorder = base_sprite(width=64, height=64, image="images/ItemBorder.png", x=180, y=178, scale=[50, 50]) spellBorder = base_sprite(width=64, height=64, image="images/ItemBorder.png", x=110, y=10, scale=[50, 50]) symbol = base_sprite(width=100, height=100, image="images/mageSmall.png", x=5, y=(height/2)-50) gameGroup.add(back) inInventory = False running = True while running: events = pygame.event.get() for event in events: if event.type == pygame.KEYDOWN: if event.key == pygame.K_i: inInventory = not inInventory gameGroup.draw(s) if inInventory: inventoryGroup.add(back) inventoryGroup.add(symbol) inventoryGroup.add(headBorder) inventoryGroup.add(handBorder) inventoryGroup.add(bodyBorder) inventoryGroup.add(feetBorder) inventoryGroup.add(spellBorder) inventoryGroup.add(base_sprite(width=64, height=64, image="images/items/BasicHat.png", x=180, y=10, scale=[50, 50])) inventoryGroup.add(base_sprite(width=64, height=64, image="images/items/BasicThingToHitPeopleWith.png", x=250, y=94, scale=[50, 50])) inventoryGroup.add(base_sprite(width=64, height=64, image="images/items/BasicShirt.png", x=180, y=94, scale=[50, 50])) inventoryGroup.add(base_sprite(width=64, height=64, image="images/items/BasicShoes.png", x=180, y=178, scale=[50, 50])) inventoryGroup.add(base_sprite(width=64, height=64, image="images/items/BasicBook.png", x=110, y=10, scale=[50, 50])) inventoryGroup.draw(s) clock.tick(60) pygame.display.flip() 定义了一个二元运算符,你需要一元运算符,所以你需要这样的声明:

xfx

你的第二个问题是,在Prolog查询提示下输入算术表达式不会导致自动减少等任何情况。参见:

:- op(600, xf, i).

为了评估算术,你必须使用?- 3 + 4 * 7. ERROR: Undefined procedure: (+)/2 (DWIM could not correct goal) ?- X = 3 + 4 * 7. X = 3+4*7. 运算符:

is/2

尝试将?- X is 3 + 4 * 7. X = 31. 视为另一个谓词,将数值与表达式相关联。 ISO Prolog中无法修改is/2的行为,因此您必须制作自己的评估谓词并使用它:

is/2

一旦你拥有了它,你就可以通常的方式使用它:

eval((A + B i) + (C + D i), E + F i) :- 
    E is A + C, 
    F is B + D.

正如您所看到的,这可能会变得乏味,但它会起作用。如果你想手动获得更复杂数字的全面支持,你应该考虑做一个元解释器。