• Places
    • Home
    • Graphs
    • Prefixes
  • Admin
    • Users
    • Settings
    • Plugins
    • Statistics
  • CPACK
    • Home
    • List packs
    • Submit pack
  • Repository
    • Load local file
    • Load from HTTP
    • Load from library
    • Remove triples
    • Clear repository
  • Query
    • YASGUI SPARQL Editor
    • Simple Form
    • SWISH Prolog shell
  • Help
    • Documentation
    • Tutorial
    • Roadmap
    • HTTP Services
  • Login

1 Machine Query Interface Overview
All Application Manual Name SummaryHelp

  • Documentation
    • Reference manual
    • Packages
      • mqi -- Python and Other Programming Languge Integration for SWI Prolog
        • Machine Query Interface Overview
          • Installation Steps for Python
          • Installation Steps for Other Languages
          • Prolog Language Differences from the Top Level
          • Embedded Mode: Integrating the Machine Query Interface Into a New Programming Language
          • Standalone Mode: Debugging Prolog Code Used in an Application
          • Machine Query Interface Messages

1.3 Prolog Language Differences from the Top Level

The Machine Query Interface is designed to act like using the "top level" prompt of SWI Prolog itself (i.e. the "?-" prompt). If you've built the Prolog part of your application by loading code, running it and debugging it using the normal SWI Prolog top level, integrating it with your native language should be straightforward: simply run the commands you'd normally run on the top level, but now run them using the query APIs provided by the library built for your target language. Those APIs will allow you to send the exact same text to Prolog and they should execute the same way. Here's an example using the Python swiplserver library:

% Prolog Top Level
?- member(X, [first, second, third]).
X = first ;
X = second ;
X = third.
# Python using the swiplserver library
from swiplserver import PrologMQI, PrologThread

with PrologMQI() as mqi:
    with mqi.create_thread() as prolog_thread:
        result = prolog_thread.query("member(X, [first, second, third]).")
        print(result)

first
second
third

While the query functionality of the MQI does run on a thread, it will always be the same thread, and, if you use a single connection, it will only allow queries to be run one at a time, just like the top level. Of course, the queries you send can launch threads, just like the top level, so you are not limited to a single threaded application. There are a few differences from the top level, however:

  • Normally, the SWI Prolog top level runs all user code in the context of a built-in module called "user", as does the MQI. However, the top level allows this to be changed using the module/1 predicate. This predicate has no effect when sent to the MQI.
  • Predefined streams like user_input are initially bound to the standard operating system I/O streams (like STDIN) and, since the Prolog process is running invisibly, will obviously not work as expected. Those streams can be changed, however, by issuing commands using system predicates as defined in the SWI Prolog documentation.
  • Every connection to the MQI runs in its own thread, so opening two connections from an application means you are running multithreaded code.
  • The SWI Prolog top level does special handling to make residual attributes on variables print out in a human-friendly way. MQI instead returns any residuals in a special variable called $residuals that is added to the results.
  • Some Prolog extensions do not provide the full answer to a query by means of the variable bindings. The top level does extra work to find where the answers are stored and print them out. All of these extensions have an interface to get to this data as a Prolog term and users of MQI need to do this work themselves. Examples include: Constraint Handling Rules (CHR) stores constraints in global variables, Well Founded Semantics has the notion of delayed, which is translated into a program that carries the inconsistency if there are conflicting negations, s(CASP) has a model and justification.

A basic rule to remember is: any predicates designed to interact with or change the default behavior of the top level itself probably won't have any effect.

ClioPatria (version V3.1.1-51-ga0b30a5)