Simple Prolog Interpreter in Python
Set up environment
python -m venv .venv
.\.venv\Scripts\activate
Install dependencies
pip install -r requirements.txt
Run REPL
python -m prolog.prolog [options] path
For example,
python -m prolog.prolog tests/data/puzzle1.prolog
Sample REPL session output:
python -m prolog.prolog tests/data/myadven.prolog
Welcome to Simple Prolog
ctrl-c to quit
> location(desk, office).
yes
> location(desk, office1).
no
> location(X, Y).
X = desk Y = office
X = apple Y = kitchen
X = flashlight Y = desk
X = 'washing machine' Y = cellar
X = nani Y = 'washing machine'
X = broccoli Y = kitchen
X = crackers Y = kitchen
X = computer Y = office
no
> door(kitchen, R), location(T, R).
R = office T = desk
R = office T = computer
R = cellar T = 'washing machine'
no
Simple Prolog supports following built-ins: write, tab, nl and fail.
You can use them to display values of variables or text:
Welcome to Simple Prolog
ctrl-c to quit
> room(X), tab, write(X), nl.
kitchen
X = kitchen
office
X = office
hall
X = hall
'dinning room'
X = 'dinning room'
cellar
X = cellar
no
>
or if you do not want to see the solutions just print out:
> write('This is the list of rooms:'), nl, room(X), tab, write(X), nl, fail.
'This is the list of rooms:'
kitchen
office
hall
'dinning room'
cellar
no
>
You can also perform simple arithmetic operations. For example given following rule:
c_to_f(C, F) :- F is C * 9 / 5 + 32.
You can ask Prolog to convert Celsius to Fahrenheit:
Welcome to Simple Prolog
ctrl-c to quit
> c_to_f(0, F).
F = 32.0
yes
> c_to_f(100, F).
F = 212.0
yes
>
You can also ask Prolog REPL to do calculation directly:
Welcome to Simple Prolog
ctrl-c to quit
> Z is 4 * 10 - 2 * 4.
Z = 32.0
yes
> Z is 4 * (10 - 2) * 4.
Z = 128.0
yes
>
Following logical operators are supported:
==
=/
<
=<
>
>=
Here is an example of rule that uses logical operator:
freezing(X) :- X =< 32.
Welcome to Simple Prolog
ctrl-c to quit
> freezing(30).
yes
>
Or you can use it directly from REPL:
Welcome to Simple Prolog
ctrl-c to quit
> X is 2+2, X > 1.
yes
> X is 2+2, X > 5.
no
>
Simple Prolog also has support for lists. Here are some examples:
Welcome to Simple Prolog
ctrl-c to quit
> rgb([red, green, blue]).
yes
> rgb([R, G, B]).
R = red G = green B = blue
yes
> rgb([_, G, _]).
G = green
yes
> rgb([R, green, B]).
R = red B = blue
yes
> rgb([red, green | H]).
H = [blue]
yes
> rgb([H | T]).
H = red T = [green, blue]
yes
> rgb([H | [X, Y]]).
H = red X = green Y = blue
yes
>
Test
Linter:
python -m flake8
Tests:
poetry run pytest --cov=prolog tests
How to Use PyProlog as a Library
Install pyprolog:
pip install pyprolog
Here is an example how to use PyProlog as a library:
from prolog import Scanner, Parser, Runtime
def main():
source = '''
location(computer, office).
location(knife, kitchen).
location(chair, office).
location(shoe, hall).
isoffice(X) :- location(computer, X), location(chair, X).
'''
tokens = Scanner(source).tokenize()
rules = Parser(tokens).parse_rules()
runtime = Runtime(rules)
goal_text = 'location(X, office).'
goal = Parser(Scanner(goal_text).tokenize()).parse_terms()
x = goal.args[0]
has_solution = False
for index, item in enumerate(runtime.execute(goal)):
has_solution = True
print(str(item))
print(str(goal.match(item).get(x)))
if has_solution:
print('Query has solution')
else:
print('Query has no solution')
if __name__ == "__main__":
main()
Acknoledgments
This was inspired and based on this article
Metadata
Release files for pieprolog 1.0.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| pieprolog-1.0.0.tar.gz | 19.0 kB | Without provenance | Details for pieprolog-1.0.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| pieprolog-1.0.0-py3-none-any.whl | Python 3 | none | any | Details for pieprolog-1.0.0-py3-none-any.whl |
Total release size: 42.9 kB
Release files / pieprolog-1.0.0.tar.gz
| Download URL | pieprolog-1.0.0.tar.gz |
|---|---|
| Size | 19.0 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
ce5f2cab30b191d505f0e9efc2de2eb50283aca866706b0dc57ac8a2f47b226d
|
|
BLAKE2b-256 checksum How to use checksums |
864ed9618046738dc3a7f19a0b1535ab896e6ec9b3ccc69c32f6db7f08787bf2
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/5.1.1 CPython/3.12.3
|
Release files / pieprolog-1.0.0-py3-none-any.whl
| Download URL | pieprolog-1.0.0-py3-none-any.whl |
|---|---|
| Size | 23.9 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
a42217ef9a535095e3de5b2d65292b99db8aac2a0f422d5ceeedaa3e7724756d
|
|
BLAKE2b-256 checksum How to use checksums |
21afa1f6dae894d216aafaad4dd5a6de5dbd2a4fb1bbcf5949f65a8b22426034
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/5.1.1 CPython/3.12.3
|
Security for pieprolog 1.0.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.