2

I had written a program in Prolog and I want to reproduce the results in python using pyDatalog. Below is a snippet of my program.

To give an overview of my problem, I have the nodes('a','b','c' and 'd') connected by arrows ('w1,'w2','w3', and 'w4').

I want to generate a list of the arrows connecting any two nodes I specify. The methodology I used in this is almost identical to the one I used in Prolog but it never finishes executing when I run it so I wonder if I am invoking infinite recursions. I would greatly appreciate any help and I have included my working Prolog program at the end.

from pyDatalog import pyDatalog
pyDatalog.create_terms('A, B, C, O, X')
pyDatalog.create_terms('con, this_c')

+con('w1','a','c')
+con('w2','c','b')
+con('w3','b','d')
+con('w4','d','a')

this_c(O, A, B) <= con(O, A, B) 
this_c(O, A, B) <= con(X, A, C) & this_c(O, C, B)
print(this_c(O, 'a', 'b'))

My code in prolog is as follows.

con('w1',a,c).
con('w2',c,b).
con('w3',b,d).
con('w4',d,a).

up('w1').
up('w3').
down('w2').
down('w4').

check(X,Y):-up(X),atom_concat('+', X, Y).

check(X,Y):-down(X),atom_concat('-', X, Y).

comb(Output,Input,A,B):- con(X,A,B), check(X,Y), append([Y],Input,Output).

comb(Output,Input,A,B):- comb(Out,Input,A,C),con(X,C,B), check(X,Y), append([Y],Out,Output).

If I query comb(O,[],a,b) the output is O = ['-w2', '+w1']

false
  • 10,264
  • 13
  • 101
  • 209
njemal
  • 87
  • 9
  • Are you certain it's an infinite loop, and not just an extremely long computation? How long did you let it run? – InfiniteHigh Jul 18 '19 at 16:58
  • Where did the `X` come from in `this_c(O, A, B) <= con(X, A, C) & this_c(O, C, B)`? – Daniel Lyons Jul 18 '19 at 17:07
  • @InfiniteHigh I left it running for over an hour now so I'm pretty sure something is of. – njemal Jul 18 '19 at 17:50
  • @DanielLyons I made a mistake when I copied over the code in my question (fixed it), X is a term I created and I intended it to just be a placeholder/sigleton. – njemal Jul 18 '19 at 17:53

0 Answers0