Skip to main content

PySwip

What's New?

See the CHANGELOG.

This library is being cleaned up and refactored. Expect API breakage and incompatibility with previous versions.

Thanks to all contributors. If you have contributed to PySwip in the past and your name does not appear on that list, please let me know so I can add your name.

Introduction

PySwip is a Python - SWI-Prolog bridge enabling to query SWI-Prolog in your Python programs. It features an (incomplete) 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.

Requirements:

  • Python 2.7 or 3.4 and higher.
    • PyPy is currently not supported.
  • SWI-Prolog 7.2.x and higher.
  • libswipl as a shared library. This is the default on most platforms.
  • Works on Linux, Windows, MacOS and FreeBSD. Should work on other POSIX.

Install

See INSTALL for instructions.

Examples

Using Prolog

from pyswip import Prolog
prolog = 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 = Prolog()
>>> prolog.consult("knowledge_base.pl")

Foreign Functions

from __future__ import print_function
from pyswip import Prolog, registerForeign

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

registerForeign(hello)

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

Pythonic interface (Experimental)

from __future__ import print_function
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!

Contact

Projects/Publications that Use or Reference PySwip

Do you have a project, video or publication that uses/mentions PySwip? Please let me know or send a pull request.

Books

Publications

Videos

Projects

Blog Posts

License

Copyright (c) 2007-2018 Yüce Tekol

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Release files for pyswip 0.2.8

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.2.8
File Size Uploaded
pyswip-0.2.8.tar.gz 21.4 kB Details

Built distribution (wheel)

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

Total release size: 45.6 kB

Release files / pyswip-0.2.8.tar.gz

Download URL pyswip-0.2.8.tar.gz
Size 21.4 kB
Tags Source
SHA-256 checksum
How to use checksums
f07b3dc1c7fa610acb83f2fd49ab596d601d953739702dae2a98285abf97e1f0
BLAKE2b-256 checksum
How to use checksums
a06f2c82b8afd2c844802ad4fbf0fa9092909bdad87dd35772b251cfa697332e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.13.0 pkginfo/1.4.2 requests/2.18.4 setuptools/40.8.0 requests-toolbelt/0.8.0 tqdm/4.23.4 CPython/3.6.3

Release files / pyswip-0.2.8-py2.py3-none-any.whl

Download URL pyswip-0.2.8-py2.py3-none-any.whl
Size 24.2 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
ecb44bb4f88cdccfc228e747660d268496c9931a55207206cd8bf48849d4fee5
BLAKE2b-256 checksum
How to use checksums
60cd7defc05e52763f14286545331b8e865ff829304befcaf22c4001f5706b77
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.13.0 pkginfo/1.4.2 requests/2.18.4 setuptools/40.8.0 requests-toolbelt/0.8.0 tqdm/4.23.4 CPython/3.6.3

Release history Release notifications | RSS feed

0.3.3

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

This release

0.2.8 This release

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