SWI-Prolog / issues

Dummy repository for issue tracking
7 stars 3 forks source link

Query timeout when using the MQI while the GUI returns an almost instant result #131

Closed DavidJilg closed 7 months ago

DavidJilg commented 7 months ago

There appears to be a problem with the Machine Query Interface (at least when called from Python using the swiplserver library) that results in queries reaching a timeout, while the same queries produce an almost instant result in the GUI. The following example illustrates the problem:

Example

Consider the following Prolog implementation of the TAK-function (I am aware that it is not a correct implementation of the function).

tak(X,Y,Z,Y).
tak(X,Y,Z,R) :-
    X < Y,
    X1 is X - 1,
    Y1 is Y - 1,
    Z1 is Z - 1,
    tak(X1, Y, Z, R1),
    tak(Y1, Z, X, R2),
    tak(Z1, X, Y, R3),
    tak(R1, R2, R3, R).

When executing the query tak(1, 2, 3, R). , the GUI version of SWI-Prolog returns an almost instant result:

GUI-Screenshot\

However, when executing the same query through the MQI interface using the python library swiplserver, and the following implementation, the query results in a timeout even when setting a high timeout time (maximum that I tried is 1000 seconds).

from swiplserver import PrologMQI

# Todo Replace with absolute path on your system
prolog_file_path = "C:/Users/daji01/Documents/tak.pl"

mqi = PrologMQI()
with mqi.create_thread() as prolog_thread:
    prolog_thread.query(f'consult("{prolog_file_path}").')
    result = prolog_thread.query("tak(1, 2, 3, R).", query_timeout_seconds=5)
    print(result)
Traceback (most recent call last):
  File "C:\Users\daji01\Documents\mqi_timeout_bug.py", line 9, in <module>
    result = prolog_thread.query("tak(1, 2, 3, R).", query_timeout_seconds=5)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\daji01\AppData\Local\Programs\Python\Python312\Lib\site-packages\swiplserver\prologmqi.py", line 682, in query
    return self._return_prolog_response()
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\daji01\AppData\Local\Programs\Python\Python312\Lib\site-packages\swiplserver\prologmqi.py", line 831, in _return_prolog_response
    raise {
swiplserver.prologmqi.PrologQueryTimeoutError: time_limit_exceeded

Other Prolog programs work fine with this implementation. I am using SWI-Prolog version 9.2.1 for x64-win64 on Windows 11 Pro Version 10.0.22631 Build 22631.

JanWielemaker commented 7 months ago

You'd probably better ask on Discourse as more people look there. Didn't MQI do a findall/3? You implementation produces an infinite number of results, so a findall/3 will not complete (well, eventually runs out of memory).

DavidJilg commented 7 months ago

Thank you for the hint! However, I wonder why the GUI version returns a single result if an infinite number of results are produced and the MQI does not. Is the difference in behavior intended?

JanWielemaker commented 7 months ago

Well, I don't know what you mean by the GUI version, ut if I run the query in the commandline I can keep hitting ";" and get more and more answers.

DavidJilg commented 7 months ago

Sorry, I was not aware that you could enter ";" and get more results. I was pressing Enter, and it would stop after one result. It seems there was no Problem after all, and I misjudged the issue because I am not that familiar with SWI-Prolog. Sorry for the confusion.