Skip to main content

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

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.

Metadata

Release files for pyswip 0.3.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pyswip 0.3.3
File Size Uploaded
pyswip-0.3.3.tar.gz 36.0 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for pyswip 0.3.3
File Interpreter ABI Platform
pyswip-0.3.3-py3-none-any.whl Python 3 none any Details

Total release size: 72.1 kB

Release files / pyswip-0.3.3.tar.gz

Download URL pyswip-0.3.3.tar.gz
Size 36.0 kB
Tags Source
SHA-256 checksum
How to use checksums
745cc6d86046a4cf9ba775a76f00d97f7d9129f7517515228391d7399bc4fe3d
BLAKE2b-256 checksum
How to use checksums
ff7596b326387af00edd34c7cca121ce98c10062d7b8dc5ec73324727997f678
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.12.3

Release files / pyswip-0.3.3-py3-none-any.whl

Download URL pyswip-0.3.3-py3-none-any.whl
Size 36.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
abddc4729917e3631e4a516e16211cbee93d4b701700eb116d6c3e8228ec3052
BLAKE2b-256 checksum
How to use checksums
b52bc3bfc5a53714f7fb1b092aadb95277f133ce8c605f411f8888ca7ba340dd
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.12.3

Release history Release notifications | RSS feed

This release

0.3.3 This release

2 release files

0.3.2

2 release files

0.3.1

2 release files

0.3.0

2 release files

0.2.10

2 release files

0.2.9

2 release files

0.2.8

2 release files

0.2.7

2 release files

0.2.6

2 release files

0.2.5

2 release files

0.2.2

2 release files

0.2.1

0.2.0

0.1.3

0.1.2

0.1.1

0.1.0

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page