Pylog - Pythonic Prolog Features

1.1 · active · verified Mon Apr 13

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

Install

Imports

Quickstart

This quickstart demonstrates how to define facts, create a simple rule using other predicates, set a goal, and retrieve solutions using the `run()` function. It showcases basic logical inference capabilities.

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)

view raw JSON →