0

I'm using pyDatalog (in Python 2.7). Using an arithmetic function like +, I can refer to an earlier bound variable:

>>> (X==1) & (Y==X+1)
[(1, 2)]

But I cannot use the boolean not operator the same way:

>>> not(False)
True
>>> (X==False) & (Y==not(X))
  File "<stdin>", line 1
    (X==False) & (Y==not(X))
                       ^
SyntaxError: invalid syntax
>>> 
Jeffrey Benjamin Brown
  • 3,427
  • 2
  • 28
  • 40

2 Answers2

1

You could use a custom resolver :

from pyDatalog import pyDatalog

@pyDatalog.predicate()
def not_2(X,Y):
    if X.is_const():
        yield (X.id, not(X.id))
    elif Y.is_const():
        yield (not(Y.id), Y.id)

@pyDatalog.program()
def _():
    print ((X==False) & (not_(X,Y)))
Pierre Carbonnelle
  • 2,305
  • 19
  • 25
  • What black magic is this? You define `not_2` but then you call `not`. The code runs, but then when I try to evaluate `(X==False) & (not_2(X,Y))` by itself in the REPL I get `name 'not' is not defined`. If I use `not_2` instead, I get an `AttributeError: 'generator' object has no attribute 'literals'`. Reading the code, it only looks like it should work if X or Y is a constant. To test that, I tried replacing the last line with `print ((X==False) & (not_(X,Y)) & (not_(Y,Z)))`. But that prints exactly the same output, as if it's ignoring Z. – Jeffrey Benjamin Brown Oct 25 '17 at 18:30
  • I'm using a custom predicate resolvers for not_. See Advanced Topics on pyDatalog site. – Pierre Carbonnelle Oct 26 '17 at 15:06
-1

it is operator precedence in Python

(Y == (not(X))

or

(Y == not X)
galaxyan
  • 5,944
  • 2
  • 19
  • 43