Logical unification in Python
Low maintenance activity. Last release 13 months ago, commits in 0 of the last 26 weeks. Weaker on commit activity and recent releases.
pip install logical-unificationLogical Unification
Logical unification in Python, extensible via dispatch.
Installation
Using pip:
pip install logical-unification
To install from source:
git clone git@github.com:pythological/unification.git
cd unification
pip install -r requirements.txt
Tests can be run with the provided Makefile:
make check
Examples
unification has built-in support for unifying most Python data types via the function unify:
>>> from unification import *
>>> unify(1, 1)
{}
>>> unify(1, 2)
False
>>> x = var()
>>> unify((1, x), (1, 2))
{~x: 2}
>>> unify((x, x), (1, 2))
False
Unifiable objects containing logic variables can also be reified using reify:
>>> reify((1, x), {x: 2})
(1, 2)
And most Python data structures:
>>> unify({"a": 1, "b": 2}, {"a": x, "b": 2})
{~x: 1}
>>> unify({"a": 1, "b": 2}, {"a": x, "b": 2, "c": 3})
False
>>> from collections import namedtuples
>>> ntuple = namedtuple("ntuple", ("a", "b"))
>>> unify(ntuple(1, 2), ntuple(x, 2))
{~x: 1}
Custom classes can be made "unifiable" with the unifiable decorator:
@unifiable
class Account(object):
def __init__(self, id, name, balance):
self.id = id
self.name = 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]
[False,
{~name: 'Bob', ~id: 2},
{~name: 'Charlie', ~id: 2},
False,
False]