minikanren
minikanren is a Python library that provides a Domain Specific Language (DSL) for relational (logic) programming. It enables users to express sophisticated relations—in the form of goals—and generate values that satisfy these relations. The library is often used as an algorithmic core for Computer Algebra Systems and for the automated generation and optimization of numeric software. The current version is 1.0.5, with ongoing development and participation in miniKanren workshops across various language implementations. [2, 3]
Warnings
- gotcha The PyPI package is `minikanren`, but the Python module to import is `kanren`. Importing from `minikanren` directly will result in an `ImportError`. [2, 3]
- gotcha Like other logic programming languages, `minikanren` queries may enter an infinite loop if no solution exists within an infinite search tree. The interleaved search strategy guarantees finding solutions if they exist, but cannot guarantee termination if no solution is found and the search space is unbounded. [16]
- gotcha While `minikanren`'s interleaving search provides completeness, the order of goals can still impact performance, especially in complex scenarios. Inefficient goal ordering might lead to longer execution times or seemingly non-terminating searches if favorable branches are explored later. [18, 20]
- breaking The `kanren` project is a fork of `logpy` and while aiming for syntactic parity, it deviates significantly in 'core mechanics and offerings'. Users migrating from `logpy` should be aware that internal behaviors and potential extensions might differ substantially. [2]
Install
-
pip install minikanren
Imports
- run, eq, membero, var, lall
from kanren import run, eq, membero, var, lall
Quickstart
from kanren import run, eq, var x = var() results = run(1, x, eq(x, 5)) print(results) y, z = var(), var() results_multi = run(1, (y, z), eq(y, z), eq(z, 3)) print(results_multi) # Example with unification of structures results_struct = run(1, x, eq((1, 2), (1, x))) print(results_struct)