All Projects → pythological → kanren

pythological / kanren

Licence: other
An extensible, lightweight relational/logic programming DSL written in pure Python

Programming Languages

python
139335 projects - #7 most used programming language
Makefile
30231 projects

Projects that are alternatives of or similar to kanren

symbolic-pymc
Tools for the symbolic manipulation of PyMC models, Theano, and TensorFlow graphs.
Stars: ✭ 58 (-47.75%)
Mutual labels:  minikanren, symbolic-computation, relational-programming
simsttab
Simple timetabling engine for schools
Stars: ✭ 21 (-81.08%)
Mutual labels:  clp
Symbolic-computation-Python
Symbolic computation using SymPy and various applications
Stars: ✭ 18 (-83.78%)
Mutual labels:  symbolic-computation
clpsmt-miniKanren
CLP(SMT) on top of miniKanren
Stars: ✭ 31 (-72.07%)
Mutual labels:  minikanren
Bracmat
Programming language for symbolic computation with unusual combination of pattern matching features: Tree patterns, associative patterns and expressions embedded in patterns.
Stars: ✭ 42 (-62.16%)
Mutual labels:  symbolic-computation
ukanren-rs
Rust implementation of µKanren, a featherweight relational programming language.
Stars: ✭ 98 (-11.71%)
Mutual labels:  minikanren
SymbolicTensors.jl
Manipulate tensors symbolically in Julia! Currently needs a SymPy dependency, but work is ongoing to change the backend to SymbolicUtils.jl
Stars: ✭ 26 (-76.58%)
Mutual labels:  symbolic-computation
Synthetic-data-gen
Various methods for generating synthetic data for data science and ML
Stars: ✭ 57 (-48.65%)
Mutual labels:  symbolic-computation
Hedgehog Lab
Run, compile and execute JavaScript for Scientific Computing and Data Visualization TOTALLY TOTALLY TOTALLY in your BROWSER! An open source scientific computing environment for JavaScript TOTALLY in your browser, matrix operations with GPU acceleration, TeX support, data visualization and symbolic computation.
Stars: ✭ 1,797 (+1518.92%)
Mutual labels:  symbolic-computation
gominikanren
a Go implementation of miniKanren, an embedded Domain Specific Language for logic programming.
Stars: ✭ 28 (-74.77%)
Mutual labels:  minikanren
SymbolicControlSystems.jl
An interface between ControlSystems.jl and SymPy.jl
Stars: ✭ 20 (-81.98%)
Mutual labels:  symbolic-computation
mathiu.cpp
mathiu : a simple computer algebra system in C++.
Stars: ✭ 58 (-47.75%)
Mutual labels:  symbolic-computation
leanTAP
A Declarative Theorem Prover for First-Order Classical Logic
Stars: ✭ 24 (-78.38%)
Mutual labels:  minikanren
HashedExpression
Type-safe modelling DSL, symbolic transformation, and code generation for solving optimization problems.
Stars: ✭ 40 (-63.96%)
Mutual labels:  symbolic-computation
Abacus
Advanced Combinatorics and Algebraic Number Theory Symbolic Computation library for JavaScript, Python
Stars: ✭ 16 (-85.59%)
Mutual labels:  symbolic-computation
CC33Z
Curso de Ciência da Computação
Stars: ✭ 50 (-54.95%)
Mutual labels:  symbolic-computation
shen-minikanren
An embedding of miniKanren in Shen.
Stars: ✭ 24 (-78.38%)
Mutual labels:  minikanren
QuantumLattices.jl
Julia package for the construction of quantum lattice systems.
Stars: ✭ 79 (-28.83%)
Mutual labels:  symbolic-computation
ciao
Ciao is a modern Prolog implementation that builds up from a logic-based simple kernel designed to be portable, extensible, and modular.
Stars: ✭ 190 (+71.17%)
Mutual labels:  clp
Kelvin
A powerful language for symbolic computation written in Swift.
Stars: ✭ 23 (-79.28%)
Mutual labels:  symbolic-computation

kanren

Build Status Coverage Status PyPI Join the chat at https://gitter.im/pythological/kanren

Logic/relational programming in Python with miniKanren.

Installation

Using pip:

pip install miniKanren

Using conda:

conda install -c conda-forge miniKanren

Development

First obtain the project source:

git clone [email protected]:pythological/kanren.git
cd kanren

Install the development dependencies:

$ pip install -r requirements.txt

Set up pre-commit hooks:

$ pre-commit install --install-hooks

Tests can be run with the provided Makefile:

make check

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.

These points—along with kanren examples—are covered in the paper "miniKanren as a Tool for Symbolic Computation in Python".

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, 3],)

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,')

Constraints

kanren provides a fully functional constraint system that allows one to restrict unification and object types:

>>> from kanren.constraints import neq, isinstanceo

>>> run(0, x,
...     neq(x, 1),  # Not "equal" to 1
...     neq(x, 3),  # Not "equal" to 3
...     membero(x, (1, 2, 3)))
(2,)

>>> from numbers import Integral
>>> run(0, x,
...     isinstanceo(x, Integral),  # `x` must be of type `Integral`
...     membero(x, (1.1, 2, 3.2, 4)))
(2, 4)

Graph Relations

kanren comes with support for relational graph operations suitable for basic symbolic algebra operations. See the examples in doc/graphs.md.

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 logical-unification project's examples for demonstrations of how arbitrary types can be made unifiable.

About

This project is a fork of logpy.

References

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].