Skip to main content

PySwip enables querying SWI-Prolog in your Python programs.

Project description

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.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pyswip-0.2.8.tar.gz (21.4 kB view details)

Uploaded Source

Built Distribution

pyswip-0.2.8-py2.py3-none-any.whl (24.2 kB view details)

Uploaded Python 2Python 3

File details

Details for the file pyswip-0.2.8.tar.gz.

File metadata

  • Download URL: pyswip-0.2.8.tar.gz
  • Upload date:
  • Size: 21.4 kB
  • Tags: Source
  • Uploaded using 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

File hashes

Hashes for pyswip-0.2.8.tar.gz
Algorithm Hash digest
SHA256 f07b3dc1c7fa610acb83f2fd49ab596d601d953739702dae2a98285abf97e1f0
MD5 139dc1eba8cc5f9b448f677044aa3dda
BLAKE2b-256 a06f2c82b8afd2c844802ad4fbf0fa9092909bdad87dd35772b251cfa697332e

See more details on using hashes here.

File details

Details for the file pyswip-0.2.8-py2.py3-none-any.whl.

File metadata

  • Download URL: pyswip-0.2.8-py2.py3-none-any.whl
  • Upload date:
  • Size: 24.2 kB
  • Tags: Python 2, Python 3
  • Uploaded using 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

File hashes

Hashes for pyswip-0.2.8-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 ecb44bb4f88cdccfc228e747660d268496c9931a55207206cd8bf48849d4fee5
MD5 a3b258b078ea14cf2156c15cb75f8800
BLAKE2b-256 60cd7defc05e52763f14286545331b8e865ff829304befcaf22c4001f5706b77

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page