Skip to main content

A simple python library for generating prolog terms

Project description

prologterms - a python library for constructing prolog terms


from prologterms import TermGenerator, PrologRenderer, Var

X = Var('X')
P = TermGenerator()
term = P.member(X, [1, 2, 3])
r = PrologRenderer()


member(X, [1, 2, 3])

All prolog escaping conventions are handled automatically.

See the Jupyter notebook


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))


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


[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),

q = P.ancestor(X,Y)

factory = PengineBuilder(urlserver="http://localhost:4242",
pengine = Pengine(builder=factory, debug=True)
while pengine.currentQuery.hasMore:
for p in pengine.currentQuery.availProofs:
    print('{} <- {}'.format(p[], p[]))

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

prologterms-0.0.5.tar.gz (3.8 kB view hashes)

Uploaded source

Built Distribution

prologterms-0.0.5-py3-none-any.whl (3.8 kB view hashes)

Uploaded py3

Supported by

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