from pyDatalog import pyDatalog
from pyDatalog.pyDatalog import create_terms as terms
from pyDatalog.pyDatalog import ask
pyDatalog.create_terms('scale') # the long way of doing it
terms('A, B, C, V')
scale['meter', 'inch'] = 39.3700787
scale['mile', 'inch'] = 63360.0
scale['feet', 'inch'] = 12.0
scale[A, B] = 1/scale[B, A]
scale[A,B] = scale[A,C] * scale[C, B]#loops indefinitely after this point
print(scale['inch', 'meter'] == V)
print(scale['mile', 'meter'] == V)
terms('conv')
conv[V, A, B] = V * scale[A, B]
print(conv[3, 'mile', 'meter'] == V)
print(conv[1, 'meter', 'feet'] == V)With version 0.16.0, it is necessary to re-order the definition of scale as follows, in order to avoid an infinite loop :
scale[A,B] = scale[A,C] * scale[C, B]
scale[A, B] = 1/scale[B, A]
As the tutorial says : “The most general definition of the function is given first. When searching for possible answers, pyDatalog begins with the last rule defined, i.e. the more specific, and stops as soon as a valid answer is found for the function. ”
In this case, the engine should start to resolve a query with scale[A, B] = 1/scale[B, A], not with scale[A,B] = scale[A,C] * scale[C, B]