【问题标题】:Using boolean operators in pyDatalog在 pyDatalog 中使用布尔运算符
【发布时间】:2014-11-25 22:26:35
【问题描述】:

我尝试使用 == 运算符在 pyDatalog 中创建谓词,但 fluent[X] 的定义似乎无效:

from pyDatalog import pyDatalog
pyDatalog.create_terms('flammable, notFlammable, X, Y')

flammable[X] = ((X == 'wood') or (X == 'coal'))`
#TypeError: unhashable type: 'Query'

notFlammable[X] = ~flammable[X]
#This doesn't work either.

我还尝试在 pyDataLog 谓词中包含比较运算符,但这也不起作用:

threeOrFour[X] = ((X < 3) or (X > 4))
#pyDatalog.util.DatalogError: Error: left hand side of comparison must be bound: </2 in line None of None

是否可以在 pyDatalog 谓词中包含这些比较和否定运算符?

【问题讨论】:

    标签: pydatalog


    【解决方案1】:

    这里的问题是您想要分配谓词(易燃和不可燃),但您使用的是函数分配语法。 (Here's a useful link I found to understand the distinction between predicates and functions.) 在 pyDatalog 中分配谓词的方法是用括号中的参数和&lt;= 赋值,如下所示:

    from pyDatalog import pyDatalog
    pyDatalog.create_terms('flammable, notFlammable, X, Y')
    
    flammable(X) <= ((X == 'wood') or (X == 'coal'))
    
    
    notFlammable(X) <= ~flammable(X)
    

    那么对于您的第二个示例,在逻辑编程中将析取拆分为多行是很常见的:

    from pyDatalog import pyDatalog
    
    pyDatalog.create_terms('threeOrFour,X')
    
    threeOrFour(X) <= (X < 3)
    threeOrFour(X) <= (X > 4)
    

    这行得通。

    【讨论】:

    • 是否也可以在一个语句而不是两个语句中定义析取?
    • 另外,PyDataLog 有没有类似于 Prolog 的 member 谓词的东西?
    猜你喜欢
    • 2021-07-14
    • 1970-01-01
    • 1970-01-01
    • 2011-04-20
    • 2011-09-27
    • 2011-03-27
    相关资源
    最近更新 更多