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)

Building the compiler

Installing ANTLR

You can use antlr4 to build the prolog compiler. You will need version 4.7.2 or higher.

Installing ANTLR manually (optional)

If your system's antlr4 is older, you can download the ANTLR jar file from the antlr web page:

wget https://www.antlr.org/download/antlr-4.7.2-complete.jar

Save this file somewhere, and create a wrapper script:

#!/bin/bash

java -cp "$HOME/antlr-4.7.2/antlr-4.7.2-complete.jar:$CLASSPATH" \
	org.antlr.v4.Tool "$@"

Installing the ANTLR Python runtime

You will also need to install the Python runtime, for example (Python3):

pip3 install antlr4-python3-runtime==4.7.2

Generating the compiler

You build the compiler by running make compiler.

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.1.1.tar.gz (25.3 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.1.1-py3-none-any.whl (41.0 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: yldprolog-1.1.1.tar.gz
  • Upload date:
  • Size: 25.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.0.1 pkginfo/1.7.0 requests/2.24.0 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.9.2

File hashes

Hashes for yldprolog-1.1.1.tar.gz
Algorithm Hash digest
SHA256 1e03a2c0213eb9dea47315b520fafcc76b2850b98224dbeaaaaf266a03cb0900
MD5 61605dc1a9c6cc71a948de0295e3f20a
BLAKE2b-256 793f92f0439c84161ddf173c12af8d4e47f91f6618fa0c98ec73cf62669bdad2

See more details on using hashes here.

File details

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

File metadata

  • Download URL: yldprolog-1.1.1-py3-none-any.whl
  • Upload date:
  • Size: 41.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.0.1 pkginfo/1.7.0 requests/2.24.0 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.9.2

File hashes

Hashes for yldprolog-1.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 48af123d3b88e5d5d48f5676f318c7a70c4a98d79d3435a7b96eef2f084de63e
MD5 627c376b500146f4218789af85d08741
BLAKE2b-256 b003f6b5b01f182c0b290aaff1a760db529dd1414141fcb07511a91e0e5e4134

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