Pylog - Pythonic Prolog Features
Pylog is a lightweight Python library that implements core features of Prolog, enabling logic programming paradigms within Python. It allows defining facts and rules to perform simple logical deductions and goal-oriented queries. The current stable version is 1.1, with an infrequent release cadence focusing on stability and minor enhancements.
Warnings
- breaking The project underwent significant restructuring around versions `1.0`/`1.1` (including the 'test1' release), which changed the import paths for core functions.
- gotcha Pylog heavily relies on global state for its `facts` and `rules` knowledge base. Calls to `facts()` and `rules()` modify a shared global state.
- gotcha Pylog implements a subset of Prolog's features. It may not support advanced Prolog constructs like complex data structures, explicit cut/fail predicates, or certain types of recursion with optimal performance.
Install
-
pip install pylog
Imports
- facts, rules, goal, run
from pylog.pylog import facts, rules, goal, run
Quickstart
from pylog.pylog import facts, rules, goal, run
# Define facts
facts("human(socrates)")
facts("human(plato)")
# Define a rule: All humans are mortal
# The body is a list of predicates that must be true for the head to be true.
rules("mortal(X)", ["human(X)"])
# Define a goal to find all mortal beings
goal("mortal(X)")
# Run the inference engine to find solutions
solutions = run()
print("Solutions for mortal(X):")
for s in solutions:
print(s)
# Example for a specific query
goal("mortal(socrates)")
solutions_socrates = run()
print("\nSolutions for mortal(socrates):")
for s in solutions_socrates:
print(s)