A simple python library for generating prolog terms
Project description
prologterms - a python library for constructing prolog terms
Example:
from prologterms import TermGenerator, PrologRenderer, Var X = Var('X') P = TermGenerator() term = P.member(X, [1, 2, 3]) r = PrologRenderer() print(r.render(term))
writes:
member(X, [1, 2, 3])
All prolog escaping conventions are handled automatically.
See the Jupyter notebook
Rules
The “<=” operator in python is overloaded to mean the same as prolog “:-”. This means a rule object can be created as follows:
P.ancestor(X,Y) <= (P.parent(X,Z), P.ancestor(Z,Y))
This can be done more explicitly using a Rule constructor:
from prologterms import TermGenerator, PrologRenderer, Var, Rule X = Var('X') Y = Var('Y') Z = Var('Z') P = TermGenerator() rule = Rule(P.ancestor(X,Y), (P.parent(X,Z), P.ancestor(Z,Y))
Usage
This module is of little use by itself. It is intended to be used to generate prolog programs that can be fed into a prolog execution engine.
write prolog programs and queries to a file, and load these using an engine like swi-prolog
through web services, e.g. via pengines
Pengines
[Note: requires latest pengines which may not be on pypi]
One of the intended applications is pengines
from pengines.Builder import PengineBuilder from pengines.Pengine import Pengine from prologterms import TermGenerator, PrologRenderer, Program, Var P = TermGenerator() X = Var('X') Y = Var('Y') Z = Var('Z') R = PrologRenderer() p = Program( P.ancestor(X,Y) <= (P.parent(X,Z), P.ancestor(Z,Y)), P.ancestor(X,Y) <= P.parent(X,Y), P.parent('a','b'), P.parent('b','c'), P.parent('c','d') ) q = P.ancestor(X,Y) factory = PengineBuilder(urlserver="http://localhost:4242", srctext=R.render(p), ask=R.render(q)) pengine = Pengine(builder=factory, debug=True) while pengine.currentQuery.hasMore: pengine.doNext(pengine.currentQuery) for p in pengine.currentQuery.availProofs: print('{} <- {}'.format(p[X.name], p[Y.name]))
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
Built Distribution
File details
Details for the file prologterms-0.0.4.tar.gz
.
File metadata
- Download URL: prologterms-0.0.4.tar.gz
- Upload date:
- Size: 3.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | d23886eddce2ea094a92aa8df3eac39c951a6249062bc57db7126fb6edfe5eed |
|
MD5 | 5bb2224d8a894c3ee67de51bd0f47233 |
|
BLAKE2b-256 | cdc5ff73570f12c2d14fd3558c79b3f836d2b198862d7e1364aebf5880bfbfdd |
Provenance
File details
Details for the file prologterms-0.0.4-py3-none-any.whl
.
File metadata
- Download URL: prologterms-0.0.4-py3-none-any.whl
- Upload date:
- Size: 3.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 45dca3b49e4be8e22b87d3b40a7c9a570cb7579bf7980d814dfc0c417e4fc819 |
|
MD5 | 840a13337e107168ea639c13ba8e9f01 |
|
BLAKE2b-256 | 32f635193158a17a791e1db36ddf775fcf6e284facdaec74ba8e6da357e26935 |