Skip to main content

Logical unification in Python

Project description

Logical Unification

Build Status Coverage Status PyPI

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 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'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.

About

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.

Source Distribution

logical-unification-0.4.1.tar.gz (24.9 kB view details)

Uploaded Source

Built Distribution

logical_unification-0.4.1-py3-none-any.whl (11.8 kB view details)

Uploaded Python 3

File details

Details for the file logical-unification-0.4.1.tar.gz.

File metadata

  • Download URL: logical-unification-0.4.1.tar.gz
  • Upload date:
  • Size: 24.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/45.2.0.post20200210 requests-toolbelt/0.9.1 tqdm/4.42.1 CPython/3.7.6

File hashes

Hashes for logical-unification-0.4.1.tar.gz
Algorithm Hash digest
SHA256 c2a318cb8f3c1a464e2fd58d8602dcda5996f9cc507bb560c64d7d9ea8c3da17
MD5 83260b122b94e5de8064e398adf4aeb2
BLAKE2b-256 9b60256102b3f47b212f5cec85191872ccddd314d84154df94b12ea5b1ea63ac

See more details on using hashes here.

File details

Details for the file logical_unification-0.4.1-py3-none-any.whl.

File metadata

  • Download URL: logical_unification-0.4.1-py3-none-any.whl
  • Upload date:
  • Size: 11.8 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/45.2.0.post20200210 requests-toolbelt/0.9.1 tqdm/4.42.1 CPython/3.7.6

File hashes

Hashes for logical_unification-0.4.1-py3-none-any.whl
Algorithm Hash digest
SHA256 a7dbca3d37cd11f5d11533d58e759f6a59ea5291c027a73cf2a6df4b47e027b9
MD5 11de386395e9ed04e425c79aefea27c8
BLAKE2b-256 79632deb026244a297adcf01e10c7421aa4174fdf4dfa01aa1d2f988dc3710f2

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page