Skip to main content
Python Software Foundation 20th Year Anniversary Fundraiser  Donate today!

Logical unification in Python

Project description

Logical Unification

Build Status Coverage Status PyPI

Logical unification in Python, extensible via dispatch.


Using pip:

pip install logical-unification

To install from source:

git clone
cd unification
pip install -r requirements.txt

Tests can be run with the provided Makefile:

make check


unification has built-in support for most Python data types:

>>> from unification import *
>>> unify(1, 1)
>>> unify(1, 2)
>>> x = var('x')
>>> unify((1, x), (1, 2))
{~x: 2}
>>> unify((x, x), (1, 2))

Custom classes can be made "unifiable" with the unifiable decorator:

class Account(object):
    def __init__(self, id, name, balance): = id = name
        self.balance = balance

>>> data = [Account(1, 'Alice', 100),
            Account(2, 'Bob', 0),
            Account(2, 'Charlie', 0),
            Account(2, 'Denis', 400),
            Account(2, 'Edith', 500)]
>>> id, name, balance = var('id'), var('name'), var('balance')
>>> [unify(Account(id, name, balance), acct) for acct in data]
[{~name: 'Alice', ~balance: 100, ~id: 1},
{~name: 'Bob', ~balance: 0, ~id: 2},
{~name: 'Charlie', ~balance: 0, ~id: 2},
{~name: 'Denis', ~balance: 400, ~id: 2},
{~name: 'Edith', ~balance: 500, ~id: 2}]
>>> [unify(Account(id, name, 0), acct) for acct in data]
{~name: 'Bob', ~id: 2},
{~name: 'Charlie', ~id: 2},

unification also supports function dispatch through pattern matching:

>> from unification.match import *
>>> n = var('n')

def fib(n):
    return 0

def fib(n):
    return 1

def fib(n):
    return fib(n - 1) + fib(n - 2)

>>> map(fib, [0, 1, 2, 3, 4, 5, 6, 7, 8, 0])
[0, 1, 1, 2, 3, 5, 8, 13, 21, 34]

The pattern matching can be fairly complex:

>> name, amount = var('name'), var('amount')

@match({'status': 200, 'data': {'name': name, 'credit': amount}})
def respond(name, amount):
    balance[name] +=  amount

@match({'status': 200, 'data': {'name': name, 'debit': amount}})
def respond(name, amount):
    balance[name] -= amount

@match({'status': 404})
def respond():
    print("Bad Request")

See the full example in the examples directory.

Performance and Reliability

unification's current design allows for unification and reification of nested structures that break the Python stack recursion limit. This scalability incurs an overhead cost compared to simple stack-based recursive unification/reificiation.


This project is a fork of unification.

Project details

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Files for logical-unification, version 0.4.3
Filename, size File type Python version Upload date Hashes
Filename, size logical_unification-0.4.3-py3-none-any.whl (11.8 kB) File type Wheel Python version py3 Upload date Hashes View
Filename, size logical-unification-0.4.3.tar.gz (25.0 kB) File type Source Python version None Upload date Hashes View

Supported by

AWS AWS Cloud computing Datadog Datadog Monitoring DigiCert DigiCert EV certificate Facebook / Instagram Facebook / Instagram PSF Sponsor Fastly Fastly CDN Google Google Object Storage and Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Salesforce Salesforce PSF Sponsor Sentry Sentry Error logging StatusPage StatusPage Status page