Are you happy with your logging solution? Would you help us out by taking a 30-second survey? Click here


Logic Programming in Python

Subscribe to updates I use logpy

Statistics on logpy

Number of watchers on Github 399
Number of open issues 12
Average time to close an issue 6 months
Main language Python
Average time to merge a PR 2 days
Open pull requests 2+
Closed pull requests 1+
Last commit about 3 years ago
Repo Created almost 7 years ago
Repo Last Updated over 1 year ago
Size 541 KB
Organization / Authorlogpy
Page Updated
Do you use logpy? Leave a review!
View open issues (12)
View logpy activity
View on github
Fresh, new opensource launches 🚀🚀🚀
Trendy new open source projects in your inbox! View examples

Subscribe to our mailing list

Evaluating logpy for your project? Score Explanation
Commits Score (?)
Issues & PR Score (?)


Logic Programming in Python


kanren enables the expression of relations and the search for values which satisfy them. The following code is the Hello, world! of logic programming. It asks for 1 number, x, such that x == 5

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

Multiple variables and multiple goals can be used simultaneously. The following code asks for a number x such that x == z and z == 3

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

kanren uses unification, an advanced form of pattern matching, to match within expression trees. The following code asks for a number, x, such that (1, 2) == (1, x) holds.

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

The above examples use eq, a goal constructor to state that two expressions are equal. Other goal constructors exist such as membero(item, coll) which states that item is a member of coll, a collection.

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

>>> run(2, 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)

Logic Variables

As in the above examples, z = var() creates a logic variable. You may also, optionally, pass a token name for a variable to aid in debugging:

>>> z = var('test')
>>> z

Lastly, you may also use vars() with an integer parameter to create multiple logic variables at once:

>>> a, b, c = vars(3)
>>> a
>>> b
>>> c

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"))

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

We can use intermediate variables for more complex queries. Who is Bart's grandfather?

>>> y = var()
>>> run(1, x, parent(x, y),
              parent(y, 'Bart'))

We can express the grandfather relationship separately. In this example we use conde, a goal constructor for logical and and or.

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

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

Data Structures

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

Extending kanren to other Types

kanren uses Multiple Dispatch and the unification library to support pattern matching on user defined types. Also see unification (wikipedia). Types which can be unified can be used for logic programming. See the project examples for how to extend the collection of unifiable types to your use case.


With pip or easy_install

pip install kanren

From source

git clone
cd logpy
python install

Run tests with tox



kanren supports Python 2.7+ and Python 3.3+ with a common codebase. It is pure Python and requires no dependencies beyond the standard library, toolz, multipledispatch, and unification.

It is, in short, a light weight dependency.


Matthew Rocklin


New BSD license. See LICENSE.txt


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.


logpy open issues Ask a question     (View All Issues)
  • over 3 years FYI: some more examples for you!
  • over 3 years Improve conso and membero
  • over 4 years CLP based on LP
  • almost 5 years Readme updates
  • about 6 years Matching one expression against a lot (>1000) possible patterns?
  • over 6 years Arithmetic Goals and Var
  • almost 7 years Matche - (LISPer needed)
  • almost 7 years Associative/Commutative unification
  • almost 7 years Strategies for graph traversal
  • almost 7 years `appendo`
logpy open pull requests (View All Pulls)
  • Term
  • Test examples
logpy list of languages used
Other projects in Python