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.1.tar.gz (28.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

yldprolog-1.3.1-py3-none-any.whl (27.6 kB view details)

Uploaded Python 3

File details

Details for the file yldprolog-1.3.1.tar.gz.

File metadata

  • Download URL: yldprolog-1.3.1.tar.gz
  • Upload date:
  • Size: 28.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.6

File hashes

Hashes for yldprolog-1.3.1.tar.gz
Algorithm Hash digest
SHA256 380d3e9c3429ffbb44497c70ae441694dd786ca498e78405bdb699db0e8dce4b
MD5 01744c6349a85591ce3201ad77a5af17
BLAKE2b-256 a3ab71fc90a98e90e2112424522c91d223c09aa63fa07f3a2357cac5ff271008

See more details on using hashes here.

File details

Details for the file yldprolog-1.3.1-py3-none-any.whl.

File metadata

  • Download URL: yldprolog-1.3.1-py3-none-any.whl
  • Upload date:
  • Size: 27.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.6

File hashes

Hashes for yldprolog-1.3.1-py3-none-any.whl
Algorithm Hash digest
SHA256 7e18884a197ebdce4155777fe5843363f6ae9bbe523f9d84fd2a3a74385026f5
MD5 259f64e411db4e726bf20d7646f8b6d9
BLAKE2b-256 82108b0be6850f99d44d217e766bf9993932bce9c80ef6e351499159b008af14

See more details on using hashes here.

Supported by

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