pyDatalog loop in Mark Carter example

67 views
Skip to first unread message

Peter Etchells

unread,
Jul 27, 2015, 1:13:53 AM7/27/15
to pyDatalog
I have tried running the example in /https://mcturra2000.wordpress.com/2014/09/14/logic-programming-example-unit-conversion-using-datalog/,
(I am running version 0.16.0 with Python 2.7)

but it loops indefinitely:

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)

tximiss S

unread,
Aug 20, 2015, 1:49:59 PM8/20/15
to pyDatalog
Same problem here. Python 3.4

tximiss S

unread,
Aug 20, 2015, 3:42:33 PM8/20/15
to pyDatalog
Sorry, but there is a comment:

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]


works for me

thank you!

jcdouet

unread,
Oct 3, 2015, 3:57:31 PM10/3/15
to pyDatalog
Very interesting,

I had the case too, and I did not notice the lines you quote from the tutorial...Thus, my facts and rules were not ordered that way, I had an infinite loop...

Thank you very much !
Reply all
Reply to author
Forward
0 new messages