Logical unification in Python
Project description
Logical Unification
Logical unification
in Python, extensible via dispatch.
Examples
unification
has built-in support for most Python data types:
>>> from unification import *
>>> unify(1, 1)
{}
>>> unify(1, 2)
False
>>> x = var('x')
>>> unify((1, x), (1, 2))
{~x: 2}
>>> unify((x, x), (1, 2))
False
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]
unification
also supports function dispatch through pattern matching:
>> from unification.match import *
>>> n = var('n')
@match(0)
def fib(n):
return 0
@match(1)
def fib(n):
return 1
@match(n)
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 stresses extensibility over performance, preliminary benchmarks show that this is 2-5x slower than straight tuple-based unification.
unification
's approach is reliable; although one caveat is set unification, which is challenging to do in general. It should work well in moderately complex cases, but it may break down under very complex ones.
Installation
Using pip
:
pip install python-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
About
This project is a fork of unification
.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file logical-unification-0.3.3.tar.gz
.
File metadata
- Download URL: logical-unification-0.3.3.tar.gz
- Upload date:
- Size: 23.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/42.0.2.post20191203 requests-toolbelt/0.9.1 tqdm/4.40.2 CPython/3.7.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 027688d457a391c9af4ca1d3e681fa7c3ec6056ceefece5c2b08097639cc031b |
|
MD5 | 388643ab33e9ec47df0cf9845d14e895 |
|
BLAKE2b-256 | 59ef1bef8a8a549d34fa8919d40413e9e5a9f139d329f35ab7b806da91909e81 |
Provenance
File details
Details for the file logical_unification-0.3.3-py3-none-any.whl
.
File metadata
- Download URL: logical_unification-0.3.3-py3-none-any.whl
- Upload date:
- Size: 10.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/42.0.2.post20191203 requests-toolbelt/0.9.1 tqdm/4.40.2 CPython/3.7.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1247cfa90a083db208d800f107ffb7ca3630806e52285eb6f8887ce06f9af92d |
|
MD5 | d2cb2f839790d0bf7148231871a4d672 |
|
BLAKE2b-256 | a283a759061aa1f15907c7761f557d47c4831ed8c698711207df16312a1b1ead |