Skip to main content

A rewrite of Yield Prolog for Python

Project description

yldprolog

yldprolog is a rewrite of YieldProlog, which compiles Prolog into source code that you can embed in your code. Yield Prolog supports several programming languages, yldprolog only supports Python.

The rewrite grew out of the need to run multiple Prolog instances concurrently. In Yield Prolog, the active instance is a Python module, and therefore it is cumbersome to have multiple instances at the same time. This rewrite uses object instances instead, which make this much easier.

The interface for the engine changed and as a result, the code generated by the existing YieldProlog compiler is not compatible with yldprolog. You will need to use the yldprolog compiler.

The yldprolog compiler uses ANTLR and supports the most common Prolog constructs.

Usage

Compiling a Prolog program

Let's look at the following Prolog script:

%
% The following Prolog code is based on the example "monkey and banana"
% from: Ivan Bratko, Prolog Programming for Artificial Intelligence,
% third edition. ISBN 0-201-40375-7.
%

move(state(middle,onbox,middle,hasnot),
     grasp,
     state(middle,onbox,middle,has)).

move(state(P,onfloor,P,H),
     climb,
     state(P,onbox,P,H)).

move(state(P1,onfloor,P1,H),
     push(P1,P2),
     state(P2,onfloor,P2,H)).

move(state(P1,onfloor,B,H),
     walk(P1,P2),
     state(P2,onfloor,B,H)).

canget(state(_,_,_,has)).
canget(State1) :-
     move(State1,Move,State2),
     canget(State2).

You can compile this Prolog script with:

yldpc monkey.prolog > monkey.py

The result is a Python module that defines the predicates as functions. You can use these modules in your program, for example.

# 1. construct the yldproglog engine
yp = YP()
# 2. load a script from a path or filename
yp.load_script_from_file(pathlib.Path(_SCRIPT_DIR) / 'monkey.py')
# 3. execute the query:
#    canget(state(atdoor, onfloor, atwindow, hasnot))
q = yp.query('canget', [yp.functor('state',
	[yp.atom('atdoor'), yp.atom('onfloor'),
	yp.atom('atwindow'), yp.atom('hasnot')])])
# 4. q is a generator that will give all solutions. Since this
#    query has infinitely many solutions, we will just get the
#    first one.
self.assertEqual(next(q), False)

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

yldprolog-1.3.0.tar.gz (28.8 kB view hashes)

Uploaded Source

Built Distribution

yldprolog-1.3.0-py3-none-any.whl (27.6 kB view hashes)

Uploaded Python 3

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