Logical Unification

0.4.7 · active · verified Sat Apr 11

Logical Unification is a Python library that provides capabilities for logical unification, a core concept in logic programming and automated reasoning. It enables solving equations between symbolic expressions by finding substitutions for variables. The library is a fork of the original 'unification' project, designed with a generator-based approach to handle deeply nested structures efficiently and avoid Python's recursion limits. It is currently at version 0.4.7 and receives periodic updates focusing on features and maintenance.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates the core `unify` and `reify` functions, along with creating logic variables using `var()`. It shows how to unify basic Python types like integers and tuples, and how to apply the resulting substitutions to reify terms. Dictionaries are also supported for unification.

from unification import unify, var, reify

x = var()
y = var()

# Unify constants
print(f"Unify(1, 1): {unify(1, 1)}")
print(f"Unify(1, 2): {unify(1, 2)}")

# Unify with a variable
print(f"Unify((1, x), (1, 2)): {unify((1, x), (1, 2))}")

# Unify multiple variables where they must be the same
print(f"Unify((x, x), (1, 2)): {unify((x, x), (1, 2))}")

# Reify a term with a substitution
substitution = unify((1, x), (1, 2))
if substitution:
    print(f"Reify((1, x), {substitution}): {reify((1, x), substitution)}")

# Unify dictionaries
print(f"Unify({{\"a\": 1, \"b\": 2}}, {{\"a\": x, \"b\": 2}}): {unify({"a": 1, "b": 2}, {"a": x, "b": 2})}")

view raw JSON →