{"id":20842813,"url":"https://github.com/mudge/php-microkanren","last_synced_at":"2025-05-08T22:41:58.412Z","repository":{"id":13563260,"uuid":"16255543","full_name":"mudge/php-microkanren","owner":"mudge","description":"A PHP implementation of μKanren.","archived":false,"fork":false,"pushed_at":"2014-02-12T22:58:16.000Z","size":339,"stargazers_count":8,"open_issues_count":1,"forks_count":2,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-05-08T22:41:56.491Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"https://packagist.org/packages/mudge/php-microkanren","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"bsd-3-clause","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/mudge.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2014-01-26T15:34:16.000Z","updated_at":"2018-03-14T21:46:10.000Z","dependencies_parsed_at":"2022-07-12T15:11:28.673Z","dependency_job_id":null,"html_url":"https://github.com/mudge/php-microkanren","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mudge%2Fphp-microkanren","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mudge%2Fphp-microkanren/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mudge%2Fphp-microkanren/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mudge%2Fphp-microkanren/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/mudge","download_url":"https://codeload.github.com/mudge/php-microkanren/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":253160727,"owners_count":21863624,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-18T01:25:35.972Z","updated_at":"2025-05-08T22:41:58.391Z","avatar_url":"https://github.com/mudge.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# PHPµKanren [![Build Status](https://travis-ci.org/mudge/php-microkanren.png?branch=master)](https://travis-ci.org/mudge/php-microkanren)\n\nA PHP implementation of [Jason Hemann and Daniel P. Friedman's\nµKanren](http://webyrd.net/scheme-2013/papers/HemannMuKanren2013.pdf).\n\n## Installation\n\nAdd the following to your `composer.json`:\n\n```javascript\n{\n    \"require\": {\n        \"mudge/php-microkanren\": \"v0.1.0\"\n    }\n}\n```\n\n## Usage\n\n```php\nrequire_once 'vendor/autoload.php';\n\nuse MicroKanren\\Core as U;\n\n$f = U\\callFresh(function ($q) {\n  return U\\eq($q, 5);\n});\n\necho $f(U\\emptyState());\n/* =\u003e (((#(0) . 5)) . 1) */\n```\n\nInside the `MicroKanren\\Core` namespace, there are implementations of the core\nµKanren functions as described in the original paper as well as common Lisp\nprimitives needed for their execution. As the reference implementation is in\n[Chez Scheme](http://www.scheme.com), this implementation attempts to mimic\nthat particular Lisp as closely as possible.\n\n## Lisp Primitives\n\n### `cons($car, $cdr)`\n\n```php\n$c = cons(1, cons(2, cons(3, nil())));\n```\n\nReturn a new [cons cell](http://en.wikipedia.org/wiki/Cons) with `$car` and\n`$cdr` (this is the most basic primitive for creating lists).\n\n### `car($alist)`\n\n```php\n$c = cons(1, cons(2, nil()));\ncar($c);\n/* =\u003e 1 */\n```\n\nReturn the first element of `$alist`.\n\n### `cdr($alist)`\n\n```php\n$c = cons(1, cons(2, nil()));\ncdr($c);\n/* =\u003e cons(2, nil()) */\n```\n\nReturn the rest of the `$alist`.\n\n### `nil()`\n\n```php\n$n = nil();\n$n === nil();\n/* =\u003e true */\n```\n\nReturn the empty list. Note that all instances of nil are identical.\n\n### `isPair($obj)`\n\n```php\nisPair(cons(1, nil())); /* =\u003e true  */\nisPair(4);              /* =\u003e false */\nisPair(nil());          /* =\u003e false */\n```\n\nReturn true if `$obj` is a valid pair (viz. a cons cell that is not the empty\nlist, equivalent to Petite Scheme's `pair?`).\n\n### `isNull($obj)`\n\n```php\nisNull(nil());          /* =\u003e true  */\nisNull(cons(1, nil())); /* =\u003e false */\n```\n\nReturn true is `$obj` is the empty list (equivalent to Petite Scheme's\n`null?`).\n\n### `assp($proc, $alist)`\n\n```php\n$list = alist(cons(1, 'a'), cons(2, 'b'));\n$isEven = function ($x) { return $x % 2 === 0; };\n\nassp($isEven, $list);\n/* =\u003e cons(2, 'b') */\n```\n\n\"Return the first element of `$alist` for whose car `$proc` returns true, or\nfalse.\" \u0026mdash; [Petite Scheme's `assp`](http://www.scheme.com/csug7/objects.html#./objects:s15)\n\n### `alist(...)`\n\n```php\nalist(1, 2, 3);\n/* =\u003e cons(1, cons(2, cons(3, nil()))) */\n```\n\nA convenience function for constructing `cons` cells, equivalent to Petite\nScheme's `list`.\n\n### `length($alist)`\n\n```php\nlength(alist(1, 2, 3));\n/* =\u003e 3 */\n```\n\nReturn the length of `$alist`.\n\n### `map($proc, $alist)`\n\n```php\n$list = alist(1, 2, 3);\nmap(function ($x) { return $x + 1; }, $list);\n/* =\u003e alist(2, 3, 4) */\n```\n\nReturn a list resulting in applying `$proc` to each value in `$alist`.\n\n## µKanren functions\n\n### `variable($c)`\n\nReturn a new variable containing an index `$c` (equivalent to `var`).\n\n### `isVariable($x)`\n\nReturns true if `$x` is a variable (equivalent to `var?`).\n\n### `isVariableEquals($x1, $x2)`\n\nReturns true if `$x1` and `$x2` refer to the same variable (equivalent to\n`var=?`).\n\n### `mzero()`\n\nReturn the empty stream (equivalent to `mzero`).\n\n### `walk($u, $s)`\n\nSearches for a variable's value in the substitution. If a non-variable term is\nwalked, return that term.\n\n### `extS($x, $v, $s)`\n\nExtends the substitution with a new binding  (equivalent to `ext-s`).\n\n### `unit($sC)`\n\nLifts the state into a stream whose only element is that state.\n\n### `unify($u, $v, $s)`\n\nUnifies two terms in a substitution.\n\n### `eq($u, $v)`\n\nReturns a goal that succeeds if two terms unify in the received state\n(equivalent to `≡` from the paper and `==` from the reference implementation).\n\n### `callFresh($f)`\n\nReturns a goal given a unary function whose body is a goal (equivalent to `call/fresh`).\n\n### `mplus($d1, $d2)`\n\nMerges streams.\n\n### `bind($d, $g)`\n\nInvokes a goal on each element of a stream.\n\n### `disj($g1, $g2)`\n\nReturns a goal that succeeds if either of the given goals succeed.\n\n### `conj($g1, $g2)`\n\nReturns a goal that succeeds if both given goals succeed.\n\n### `emptyState()`\n\nReturns a state with an empty substitution and variable index set to 0 (equivalent to `empty-state`).\n\n### `pull($d)`\n\nAdvances a stream until it matures.\n\n### `takeAll($d)`\n\nReturns all results from a stream (equivalent to `take-all`).\n\n### `take($n, $d)`\n\nReturns a `$n` results from a stream.\n\n### `reifyName($n)`\n\nReturns a string name for a given number (equivalent to `reify-name`).\n\n### `reifyS($v, $s)`\n\nReifies a state's substitution with respect to a variable (equivalent to `reify-s`).\n\n### `walkStar($v, $s)`\n\nEquivalent to `walk*`.\n\n### `reifyFirst($sC)`\n\nEquivalent to `reify-1st`.\n\nSee [the test\nsuite](https://github.com/mudge/php-microkanren/blob/master/tests/MicroKanren/CoreTest.php)\nfor more examples of usage.\n\n## References\n\n* [Justin S. Leitgeb's microKanren in Ruby](https://github.com/jsl/ruby_ukanren);\n* [Scott Vokes' Lua port of microKanren](https://github.com/silentbicycle/lua-ukanren);\n* [Jason Hemann and Daniel P. Friedman's reference Scheme implementation](https://github.com/jasonhemann/microKanren).\n\n## License\n\nCopyright © 2014 Paul Mucur.\n\nDistributed under the MIT License.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmudge%2Fphp-microkanren","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmudge%2Fphp-microkanren","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmudge%2Fphp-microkanren/lists"}