Search code examples
pythonprolog

output shows the type like Atom or Variable not the actual value of the variable(pyswip in python)


I am running the python program which is exectuing prolog queries(using pyswip library) the problem in this is that i am not getting the value of the variable in the output instead i am getting type of it like Atom/Variable with some address.

here is the code,

from pyswip import Prolog
swipl = Prolog()
swipl.retractall('car(_)')
swipl.assertz('(fun(X) :- car(X),red(X))')

#swipl.assertz('car(porcshe)')
swipl.assertz('car(Mercedez)')
swipl.assertz('car(Buggati)')
swipl.assertz('car(Audi)')

print (list(swipl.query('car(Which)')))

Output:-

[{'Which': Variable(100)}, {'Which': Variable(100)}, {'Which': Variable(70)}]

Why it is giving output like this?


Solution

  • Remember that Prolog variables start with an upper case letter and constants start with a lower case letter.

    So your program:

    car(Mercedes).
    car(Bugatti).
    car(Audi).
    

    Allows for any substitution:

    ?- car(beethoven).
    true ;    % Mercedes = beethoven
    true ;    % Bugatti = beethoven
    true.     % Audi = beethoven
    

    A shorter version of your program would be:

    car(_Anything).
    

    But you probably don't want that anything is a car. Try replacing the variables by constants (mercedes, bugatti, audi).

    Regarding Variable(100): when you use the underscore as a don't care notation for the contents of a variable or Prolog has to introduce variables for other reasons, they usually just get a number. I assume to distinguish the variable #100 from the nuber 100, they write Variable(100)- you can recognize it's not a Prolog term because Variable would be a variable but variables don't have arguments. So if you parse the output it would be clear what is meant.