{ "info": { "author": "Matthew Rocklin", "author_email": "mrocklin@gmail.com", "bugtrack_url": null, "classifiers": [ "Development Status :: 5 - Production/Stable", "License :: OSI Approved :: BSD License", "Programming Language :: Python", "Programming Language :: Python :: 2.7", "Programming Language :: Python :: 3", "Programming Language :: Python :: 3.4", "Programming Language :: Python :: 3.5", "Programming Language :: Python :: Implementation :: CPython", "Programming Language :: Python :: Implementation :: PyPy" ], "description": "kanren\n=====\n\n[![](https://travis-ci.org/logpy/logpy.png)](https://travis-ci.org/logpy/logpy)\n\nLogic Programming in Python\n\nExamples\n--------\n\nkanren 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`\n\n~~~~~~~~~~~Python\n>>> from kanren import run, eq, membero, var, conde\n>>> x = var()\n>>> run(1, x, eq(x, 5))\n(5,)\n~~~~~~~~~~~\n\nMultiple variables and multiple goals can be used simultaneously. The\nfollowing code asks for a number x such that `x == z` and `z == 3`\n\n~~~~~~~~~~~Python\n>>> z = var()\n>>> run(1, x, eq(x, z),\n eq(z, 3))\n(3,)\n~~~~~~~~~~~\n\nkanren uses [unification](http://en.wikipedia.org/wiki/Unification_%28computer_science%29), an advanced form of pattern matching, to match within expression trees.\nThe following code asks for a number, x, such that `(1, 2) == (1, x)` holds.\n\n~~~~~~~~~~~Python\n>>> run(1, x, eq((1, 2), (1, x)))\n(2,)\n~~~~~~~~~~~\n\nThe above examples use `eq`, a *goal constructor* to state that two expressions\nare equal. Other goal constructors exist such as `membero(item, coll)` which\nstates that `item` is a member of `coll`, a collection.\n\nThe following example uses `membero` twice to ask for 2 values of x,\nsuch that x is a member of `(1, 2, 3)` and that x is a member of `(2, 3, 4)`.\n\n~~~~~~~~~~~Python\n>>> run(2, x, membero(x, (1, 2, 3)), # x is a member of (1, 2, 3)\n membero(x, (2, 3, 4))) # x is a member of (2, 3, 4)\n(2, 3)\n~~~~~~~~~~~\n\n### Logic Variables\n\nAs 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:\n\n~~~~~~~~~~~Python\n>>> z = var('test')\n>>> z\n~test\n~~~~~~~~~~~\n\nLastly, you may also use `vars()` with an integer parameter to create multiple logic variables at once:\n\n~~~~~~~~~~~Python\n>>> a, b, c = vars(3)\n>>> a\n~_1\n>>> b\n~_2\n>>> c\n~_3\n~~~~~~~~~~~\n\n### Representing Knowledge\n\nkanren stores data as facts that state relationships between terms.\n\nThe following code creates a parent relationship and uses it to state\nfacts about who is a parent of whom within the Simpsons family.\n\n~~~~~~~~~~~Python\n>>> from kanren import Relation, facts\n>>> parent = Relation()\n>>> facts(parent, (\"Homer\", \"Bart\"),\n... (\"Homer\", \"Lisa\"),\n... (\"Abe\", \"Homer\"))\n\n>>> run(1, x, parent(x, \"Bart\"))\n('Homer',)\n\n>>> run(2, x, parent(\"Homer\", x))\n('Lisa', 'Bart')\n~~~~~~~~~~~~\n\nWe can use intermediate variables for more complex queries. Who is Bart's grandfather?\n\n~~~~~~~~~~~Python\n>>> y = var()\n>>> run(1, x, parent(x, y),\n parent(y, 'Bart'))\n('Abe',)\n~~~~~~~~~~~~\n\nWe can express the grandfather relationship separately. In this example we use `conde`, a goal constructor for logical *and* and *or*.\n\n~~~~~~~~~~~Python\n>>> def grandparent(x, z):\n... y = var()\n... return conde((parent(x, y), parent(y, z)))\n\n>>> run(1, x, grandparent(x, 'Bart'))\n('Abe,')\n~~~~~~~~~~~~\n\nData Structures\n---------------\n\nkanren 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.\n\n\nExtending kanren to other Types\n------------------------------\n\nkanren uses [Multiple Dispatch](http://github.com/mrocklin/multipledispatch/) and the [unification library](https://github.com/mrocklin/unification) to support pattern matching on user defined types. Also see [unification (wikipedia)](http://en.wikipedia.org/wiki/Unification_%28computer_science%29).\nTypes which can be unified can be used for logic programming. See the [project examples](https://github.com/mrocklin/unification#examples) for how to extend the collection of unifiable types to your use case.\n\nInstall\n-------\n\nWith `pip` or `easy_install`\n\n pip install kanren\n\nFrom source\n\n git clone git@github.com:logpy/logpy.git\n cd logpy\n python setup.py install\n\nRun tests with tox\n\n tox\n\nDependencies\n------------\n\n``kanren`` supports Python 2.7+ and Python 3.3+ with a common codebase.\nIt is pure Python and requires no dependencies beyond the standard\nlibrary, [`toolz`](http://github.com/pytoolz/toolz/),\n[`multipledispatch`](http://github.com/mrocklin/multipledispatch/), and\n[`unification`](http://github.com/mrocklin/unification/).\n\nIt is, in short, a light weight dependency.\n\nAuthor\n------\n\n[Matthew Rocklin](http://matthewrocklin.com)\n\nLicense\n-------\n\nNew BSD license. See LICENSE.txt\n\nMotivation\n----------\n\nLogic 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.\n\nReferences\n----------\n\n* [Logic Programming on wikipedia](http://en.wikipedia.org/wiki/Logic_programming)\n* [miniKanren](http://minikanren.org/), a Scheme library for relational programming on which this library is based. More information can be found in the\n[thesis of William\nByrd](https://scholarworks.iu.edu/dspace/bitstream/handle/2022/8777/Byrd_indiana_0093A_10344.pdf).\n* [core.logic](https://github.com/clojure/core.logic) a popular implementation of miniKanren in Clojure.", "description_content_type": null, "docs_url": null, "download_url": "UNKNOWN", "downloads": { "last_day": -1, "last_month": -1, "last_week": -1 }, "home_page": "http://github.com/logpy/logpy", "keywords": null, "license": "BSD", "maintainer": null, "maintainer_email": null, "name": "kanren", "package_url": "https://pypi.org/project/kanren/", "platform": "UNKNOWN", "project_url": "https://pypi.org/project/kanren/", "project_urls": { "Download": "UNKNOWN", "Homepage": "http://github.com/logpy/logpy" }, "release_url": "https://pypi.org/project/kanren/0.2.3/", "requires_dist": null, "requires_python": null, "summary": "Logic Programming in python", "version": "0.2.3" }, "last_serial": 2498319, "releases": { "0.2.2": [ { "comment_text": "", "digests": { "md5": "99ddbde0e0744ccc234657093f2147b5", "sha256": "6241b04c15c3fcc40a32fb8f37ad0d76bb972cad63e726457e6c96f03620f24e" }, "downloads": -1, "filename": "kanren-0.2.2.tar.gz", "has_sig": false, "md5_digest": "99ddbde0e0744ccc234657093f2147b5", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 22863, "upload_time": "2016-09-06T05:01:12", "url": "https://files.pythonhosted.org/packages/ac/84/03800d76ece8ef5eb79f59d4bc91d147168a4eb3588c9243f1d1ccb614bd/kanren-0.2.2.tar.gz" } ], "0.2.3": [ { "comment_text": "", "digests": { "md5": "172da1e75989db4a7ebed41e1d187866", "sha256": "e11403a4f02fbbcb790ad7e1e01a22a9caa89fd5c79e38c0da6447a2de0084c4" }, "downloads": -1, "filename": "kanren-0.2.3.tar.gz", "has_sig": false, "md5_digest": "172da1e75989db4a7ebed41e1d187866", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 23256, "upload_time": "2016-12-04T06:30:17", "url": "https://files.pythonhosted.org/packages/f7/28/2c95935bed9ab8a62f6c2c05339754ef0b85f2bc8f186d7089ed91a1c0b0/kanren-0.2.3.tar.gz" } ] }, "urls": [ { "comment_text": "", "digests": { "md5": "172da1e75989db4a7ebed41e1d187866", "sha256": "e11403a4f02fbbcb790ad7e1e01a22a9caa89fd5c79e38c0da6447a2de0084c4" }, "downloads": -1, "filename": "kanren-0.2.3.tar.gz", "has_sig": false, "md5_digest": "172da1e75989db4a7ebed41e1d187866", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 23256, "upload_time": "2016-12-04T06:30:17", "url": "https://files.pythonhosted.org/packages/f7/28/2c95935bed9ab8a62f6c2c05339754ef0b85f2bc8f186d7089ed91a1c0b0/kanren-0.2.3.tar.gz" } ] }