• Stars
    star
    447
  • Rank 97,700 (Top 2 %)
  • Language
    Python
  • License
    MIT License
  • Created about 11 years ago
  • Updated 6 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

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.

PySwip


Installing the Latest Version

The latest SWI-Prolog supported by Ubuntu 18.04 and 20.04 are 7.6.4. We generally want to support LTS releases of Ubuntu. The compatibility of PySwip with 7.6.4 on master is broken, so we are not able to release a new version until this is fixed. In the meantime, you can use the following to install PySwip from the master branch:

pip install git+https://github.com/yuce/pyswip@master#egg=pyswip

The End of Python 2 Support

Python 2 has reached end of life on January 1st, 2020 as documented here. So, PySwip 0.2.10 will be the last version which officially supports Python 2.

Do you still require Python 2 support? Let us know at: #94


What's New?

See the CHANGELOG.

WARNING! PySwip has no Windows installers! If you are a Windows user, see INSTALL. There are some "free download" sites that claim to be hosting PySwip installers. DO NOT TRUST THEM!

Thanks to all contributors.

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 3.6 and higher.
    • PyPy is currently not supported.
  • SWI-Prolog 8.2 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

IMPORTANT: Make sure the SWI-Prolog architecture is the same as the Python architecture. If you are using a 64bit build of Python, use a 64bit build of SWI-Prolog, etc.

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!

Projects/Publications that Use or Reference PySwip

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.

Books

Publications

Videos

Projects

Blog Posts

Companies using PySwip

  • Magazino GmbH Magazino develops and builds intelligent, mobile robots for intralogistics.

License

Copyright (c) 2007-2020 Yüce Tekol and PySwip contributors

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.

More Repositories

1

pot

POT is an Erlang library for generating Google Authenticator compatible one time passwords
Erlang
237
star
2

png

A pure Erlang library for creating PNG images. It can currently create 8 and 16 bit RGB, RGB with alpha, indexed, grayscale and grayscale with alpha images.
Erlang
53
star
3

erlang-vscode

Erlang/OTP support for Visual Studio Code
TypeScript
19
star
4

teacup_nats

Teacup based NATS client for Erlang
Erlang
16
star
5

random

Elixir port of Python 3 random module.
Elixir
13
star
6

droaring

Roaring Bitmaps for D
D
11
star
7

picon

Pilosa Console
Go
11
star
8

jacl

Specification for just another configuration language.
ANTLR
9
star
9

bert.erl

BERT Encoder/Decoder
Erlang
8
star
10

nats-client-benchmarks

Performance of NATS clients in different languages/frameworks
Elixir
6
star
11

teacup

Simple TCP client library for Erlang.
Erlang
5
star
12

go-jacl

A module which implements Jacl, a straightforward, easy to write and easy to understand configuration language.
Go
5
star
13

pyopenbsd

Python wrapper for some OpenBSD-specific APIs
Python
4
star
14

js-pilosa

Javascript/Typescript client for Pilosa distributed bitmap index
JavaScript
4
star
15

nats_msg

NATS Protocol Message Encoder/Decoder
Erlang
3
star
16

i3gobar

External bar for i3 written in Golang.
Go
2
star
17

vscode-erlang-next

TypeScript
2
star
18

erwatch

Watch files and directories for changes.
Erlang
2
star
19

twitter_kit

Erlang Twitter Kit
Erlang
1
star
20

catnats

cat for NATS gnatsd server
Python
1
star
21

escript-performance

Testing escript vs compiled module performance
Erlang
1
star
22

dotfiles

Vim Script
1
star
23

whatels

Erlang
1
star