PySWIP enables querying SWI-Prolog in your Python programs.
Project description
PySWIP 0.2.2
============
PySWIP is a GPL'd 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.
Note that this version of PySWIP is slightly incompatible with 0.1.x versions.
Requirements:
-------------
* Python 2.3 and higher.
* ctypes 1.0 and higher.
* SWI-Prolog 5.6.x and higher (most probably other versions will also work).
* libpl as a shared library.
* Works on Linux and Win32, should work for all POSIX.
News
----
* Importing ``pyswip`` automatically initializes SWI-Prolog.
* Fixed a bug with querying lists with the new interface.
Example (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
Since version 0.1.3 of PySWIP, it is possible to register a Python function as
a Prolog predicate through SWI-Prolog's foreign language interface.
Example (Foreign Functions):
----------------------------
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)")
list(prolog.query("father(michael,X), hello(X)"))
Outputs:
Hello, john
Hello, gina
Since version 0.2, PySWIP contains a 'Pythonic' interface which allows writing
predicates in pure Python (*Note that interface is experimental.*)
Example (Pythonic interface):
-----------------------------
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
Outputs:
Hello, john
Hello, gina
============
PySWIP is a GPL'd 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.
Note that this version of PySWIP is slightly incompatible with 0.1.x versions.
Requirements:
-------------
* Python 2.3 and higher.
* ctypes 1.0 and higher.
* SWI-Prolog 5.6.x and higher (most probably other versions will also work).
* libpl as a shared library.
* Works on Linux and Win32, should work for all POSIX.
News
----
* Importing ``pyswip`` automatically initializes SWI-Prolog.
* Fixed a bug with querying lists with the new interface.
Example (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
Since version 0.1.3 of PySWIP, it is possible to register a Python function as
a Prolog predicate through SWI-Prolog's foreign language interface.
Example (Foreign Functions):
----------------------------
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)")
list(prolog.query("father(michael,X), hello(X)"))
Outputs:
Hello, john
Hello, gina
Since version 0.2, PySWIP contains a 'Pythonic' interface which allows writing
predicates in pure Python (*Note that interface is experimental.*)
Example (Pythonic interface):
-----------------------------
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
Outputs:
Hello, john
Hello, gina
Project details
Release history Release notifications | RSS feed
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.2.tar.gz
(25.3 kB
view details)
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
pyswip-0.2.2.win32.exe
(76.1 kB
view details)
File details
Details for the file pyswip-0.2.2.tar.gz.
File metadata
- Download URL: pyswip-0.2.2.tar.gz
- Upload date:
- Size: 25.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
7633c206a2fa6f6fae5a1d76b7e2b2ad09aa14413f95e4fc96c91856a5fa6bc2
|
|
| MD5 |
2ff192e10e1762297eba597cb2a0a9da
|
|
| BLAKE2b-256 |
c9126806b29703d7423b9c0b7033a01f85f1d40bb7a553ccff51cc46eac4e486
|
File details
Details for the file pyswip-0.2.2.win32.exe.
File metadata
- Download URL: pyswip-0.2.2.win32.exe
- Upload date:
- Size: 76.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
7b85b579787d5c70eac42206157f746fe98e1aa092fd96f1f81e3db2803d1c00
|
|
| MD5 |
02a9e78332ed541d83bf13fd7142ed94
|
|
| BLAKE2b-256 |
410fd87bee97005bdbbbb6cea34b26f3c142d8d079c3114f8a6eff7eb5ea2ad6
|