Skip to main content

kanren

Build Status Coverage Status PyPI

Logic/relational programming in Python with miniKanren.

Motivation

Logic programming is a general programming paradigm. This implementation however came about specifically to serve as an algorithmic core for Computer Algebra Systems in Python and for the automated generation and optimization of numeric software. Domain specific languages, code generation, and compilers have recently been a hot topic in the Scientific Python community. kanren aims to be a low-level core for these projects.

Examples

kanren enables one to express sophisticated relations—in the form of goals—and generate values that satisfy the relations. The following code is the "Hello, world!" of logic programming; it asks for values of the logic variable x such that x == 5:

>>> from kanren import run, eq, membero, var, lall
>>> x = var()
>>> run(1, x, eq(x, 5))
(5,)

Multiple logic variables and goals can be used simultaneously. The following code asks for one list containing the values of x and z such that x == z and z == 3:

>>> z = var()
>>> run(1, [x, z], eq(x, z),
                   eq(z, 3))
([3, 4],)

kanren uses unification to match forms within expression trees. The following code asks for values of x such that (1, 2) == (1, x):

>>> run(1, x, eq((1, 2), (1, x)))
(2,)

The above examples use eq: a goal constructor that creates a goal for unification between two objects. Other goal constructors, such as membero(item, coll), express more sophisticated relations and are often constructed from simpler ones like eq. More specifically, membero states that item is a member of the collection coll.

The following example uses membero to ask for all values of x, such that x is a member of (1, 2, 3) and x is a member of (2, 3, 4).

>>> run(0, x, membero(x, (1, 2, 3)),  # x is a member of (1, 2, 3)
              membero(x, (2, 3, 4)))  # x is a member of (2, 3, 4)
(2, 3)

The examples above made implicit use of the goal constructors lall and lany, which represent goal conjunction and disjunction, respectively. Many useful relations can be expressed with lall, lany, and eq alone, but in kanren it's also easy to leverage the host language and explicitly create any relation expressible in Python.

Representing Knowledge

kanren stores data as facts that state relationships between terms. The following code creates a parent relationship and uses it to state facts about who is a parent of whom within the Simpsons family:

>>> from kanren import Relation, facts
>>> parent = Relation()
>>> facts(parent, ("Homer", "Bart"),
...               ("Homer", "Lisa"),
...               ("Abe",  "Homer"))

>>> run(1, x, parent(x, "Bart"))
('Homer',)

>>> run(2, x, parent("Homer", x))
('Lisa', 'Bart')

We can use intermediate variables for more complex queries. For instance, who is Bart's grandfather?

>>> grandparent_lv, parent_lv = var(), var()
>>> run(1, grandparent_lv, parent(grandparent_lv, parent_lv),
                           parent(parent_lv, 'Bart'))
('Abe',)

We can express the grandfather relationship as a distinct relation by creating a goal constructor:

>>> def grandparent(x, z):
...     y = var()
...     return lall(parent(x, y), parent(y, z))

>>> run(1, x, grandparent(x, 'Bart'))
('Abe,')

Data Structures

kanren depends on functions, tuples, dicts, and generators. There are almost no new data structures/classes in kanren so it is simple to integrate into preexisting code.

Extending kanren

kanren uses multipledispatch and the logical-unification library to support pattern matching on user defined types. Essentially, types that can be unified can be used with most kanren goals. See the project examples for demonstrations of how the collection of unifiable types can be extended to your use case.

Installation

Using pip:

pip install miniKanren

To install from source:

git clone git@github.com:pythological/kanren.git
cd kanren
pip install -r requirements.txt

Tests can be run with the provided Makefile:

make check

About

This project is a fork of logpy.

References

Metadata

Release files for miniKanren 0.3.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for miniKanren 0.3.1
File Size Uploaded Provenance Actions
miniKanren-0.3.1.tar.gz 35.0 kB Without provenance Details for miniKanren-0.3.1.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for miniKanren 0.3.1
File Interpreter ABI Platform Actions
miniKanren-0.3.1-py3-none-any.whl Python 3 none any Details for miniKanren-0.3.1-py3-none-any.whl

Total release size: 81.2 kB

Release files / miniKanren-0.3.1.tar.gz

Download URL miniKanren-0.3.1.tar.gz
Size 35.0 kB
Tags Source
SHA-256 checksum
How to use checksums
f0f3ea1eb8745e2e1c36eac0d10ac42b523c9e9ff7ba31f3b1185116906801ff
BLAKE2b-256 checksum
How to use checksums
26ea9e043f17d2976987889f3c5d36f2ca4e770dd8ac3fdb78c00dcb0b1dd9fa
Upload date
Uploaded using Trusted Publishing?
What is 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

Release files / miniKanren-0.3.1-py3-none-any.whl

Download URL miniKanren-0.3.1-py3-none-any.whl
Size 46.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
d5a57ab86673f23620830a1f6a1cf2bac42fd2a16e3c15ccb1a7dae446111007
BLAKE2b-256 checksum
How to use checksums
208b67dc86a4a34ff886375fae0e5a22f5e32562268025578d7dc2ba8bd21db3
Upload date
Uploaded using Trusted Publishing?
What is 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

Security for miniKanren 0.3.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page