Skip to content
forked from yuce/pyswip

PySwip is a Python-Prolog interface that enables querying SWI-Prolog in your Python programs.

License

Notifications You must be signed in to change notification settings

FForzano/pyswip

 
 

Repository files navigation

PySwip

PySwip logo

What's New?

See the Change Log.

Install

If you have SWI-Prolog installed, it's just:

pip install -U pyswip

See Get Started for detailed instructions.

Introduction

PySwip is a Python-Prolog interface that enables querying SWI-Prolog in your Python programs. It features an SWI-Prolog foreign language interface, a utility class that makes it easy querying with Prolog and also a Pythonic interface.

Since PySwip uses SWI-Prolog as a shared library and ctypes to access it, it doesn't require compilation to be installed.

PySwip was brought to you by the PySwip community. Thanks to all contributors.

Documentation

SWI-Prolog Flag Configuration

PySwip allows you to configure SWI-Prolog flags via a TOML file (swipl.toml) or environment variables. Only flags supported by the SWI-Prolog command-line interface are accepted and validated according to the official documentation.

By default, PySwip looks for a file named swipl.toml in the current working directory. You can specify a different configuration file by setting the SWIPL_CONF_FILE environment variable:

export SWIPL_CONF_FILE="/path/to/custom_swipl.toml"

Example TOML configuration

[swipl]
debug = true
stack-limit = "1000000K"

Example environment variable Set an environment variable for a flag:

export SWIPL_STACK_LIMIT="512M"

Notes

  • Integer flags that accept units (K, M, G, B) must be passed as strings in TOML (with quotes).
  • Flags not supported by the SWI-Prolog CLI are ignored.
  • Validation is automatic and follows SWI-Prolog documentation.

Examples

Using Prolog

from pyswip import Prolog
Prolog.assertz("father(michael,john)")
Prolog.assertz("father(michael,gina)")
list(Prolog.query("father(michael,X)")) == [{'X': 'john'}, {'X': 'gina'}]
for soln in Prolog.query("father(X,Y)"):
    print(soln["X"], "is the father of", soln["Y"])
# michael is the father of john
# michael is the father of gina

An existing knowledge base stored in a Prolog file can also be consulted, and queried. Assuming the filename "knowledge_base.pl" and the Python is being run in the same working directory, it is consulted like so:

from pyswip import Prolog
Prolog.consult("knowledge_base.pl")

Foreign Functions

from pyswip import Prolog, registerForeign

def hello(t):
    print("Hello,", t)
hello.arity = 1

registerForeign(hello)

Prolog.assertz("father(michael,john)")
Prolog.assertz("father(michael,gina)")
print(list(Prolog.query("father(michael,X), hello(X)")))

Pythonic interface (Experimental)

from pyswip import Functor, Variable, Query, call

assertz = Functor("assertz", 1)
father = Functor("father", 2)
call(assertz(father("michael","john")))
call(assertz(father("michael","gina")))
X = Variable()

q = Query(father("michael",X))
while q.nextSolution():
    print("Hello,", X.value)
q.closeQuery()

# Outputs:
#    Hello, john
#    Hello, gina

The core functionality of Prolog.query is based on Nathan Denny's public domain prolog.py.

Help!

PySwip Community Home

PySwip was used in scientific articles, dissertations, and student projects over the years. Head out to PySwip Community for more information and community links.

Do you have a project, video or publication that uses/mentions PySwip? file an issue or send a pull request.

If you would like to reference PySwip in a LaTeX document, you can use the provided BibTeX file. You can also use the following information to refer to PySwip:

  • Author: Yüce Tekol and PySwip contributors
  • Title: PySwip VERSION
  • URL: https://pyswip.org

License

PySwip is licensed under the MIT license.

About

PySwip is a Python-Prolog interface that enables querying SWI-Prolog in your Python programs.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Python 97.9%
  • Prolog 1.8%
  • Makefile 0.3%