pydatalog

Using boolean operators in pyDatalog


I attempted to create a predicate in pyDatalog using the == operator, but the definition of flammable[X] appears to be invalid:

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.

I also attempted to include comparison operators in a pyDataLog predicate, but this didn't work either:

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

Is it possible to include these comparison and negation operators in pyDatalog predicates?


Solution

  • The issue here is that you want to assign predicates (flammable and notFlammable) but you are using the function assignment syntax. (Here's a useful link I found to understand the distinction between predicates and functions.) The way to assign predicates in pyDatalog is with arguments in parentheses and assignment with <= like so:

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

    Then for your second example, it's common in logic programming split disjunctions into multiple lines:

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

    This works.