Skip to main content

declarative programming on json-like objects in Python

Project description

Inference Logic

https://img.shields.io/pypi/v/inference_logic.svg https://img.shields.io/travis/gecBurton/inference_logic.svg Documentation Status Updates

The goal of this project is to explore how to write a minimal set of features that allows a programmer to code declaratively in native Python.

The code is loosely based on Prolog, but rather than use the Prolog Term/functor structure it uses JSON-like dicts and tuples.

The success of this project is measured by the number of the 99 problems solved to keep the code focussed on delivering features and not bike-shedding

This code is experimental and incomplete. Do not use it in your work or school! If you wish to use a serious, well tested declarative tool in Python use the excellent pyDatalog.

tldr

from inference_logic import Variable, Rule, search

X, Y, Z, C, P = Variable.factory("X", "Y", "Z", "C", "P")

db = [
    dict(parent="Abe", child="Homer"),
    dict(parent="Homer", child="Lisa"),
    dict(parent="Homer", child="Bart"),
    dict(parent="Homer", child="Maggie"),
    Rule(dict(ancestor=X, descendant=Z), dict(parent=X, child=Z)),
    Rule(
        dict(ancestor=X, descendant=Z),
        dict(parent=X, child=Y),
        dict(ancestor=Y, descendant=Z),
    ),
]

query = dict(ancestor=P, descendant=C)
results = search(db, query)


assert next(results) == {C: "Lisa", P: "Abe"}
assert next(results) == {C: "Bart", P: "Abe"}
assert next(results) == {C: "Maggie", P: "Abe"}
assert next(results) == {C: "Homer", P: "Abe"}
assert next(results) == {C: "Lisa", P: "Homer"}
assert next(results) == {C: "Bart", P: "Homer"}
assert next(results) == {C: "Maggie", P: "Homer"}

Credits

This package was created with Cookiecutter and the audreyr/cookiecutter-pypackage project template.

This was originally inspired by py4fun some of this code here comes directly from this project.

Thank you to kclaurelie for helping to solve the fundamental problem that had been bugging me for months!

History

0.1.0 (2020-10-05)

  • First release on PyPI.

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

inference_logic-0.1.0.tar.gz (26.6 kB view hashes)

Uploaded Source

Built Distribution

inference_logic-0.1.0-py2.py3-none-any.whl (9.4 kB view hashes)

Uploaded Python 2 Python 3

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