PHPackages                             mudge/php-microkanren - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. mudge/php-microkanren

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

mudge/php-microkanren
=====================

An implementation of microKanren in PHP.

v0.1.0(12y ago)9292[1 PRs](https://github.com/mudge/php-microkanren/pulls)MITPHPPHP &gt;= 5.3.0

Since Feb 1Pushed 12y ago1 watchersCompare

[ Source](https://github.com/mudge/php-microkanren)[ Packagist](https://packagist.org/packages/mudge/php-microkanren)[ Docs](https://github.com/mudge/php-microkanren)[ RSS](/packages/mudge-php-microkanren/feed)WikiDiscussions master Synced 3d ago

READMEChangelogDependencies (1)Versions (2)Used By (0)

PHPµKanren [![Build Status](https://camo.githubusercontent.com/d64421cc5a2b4c2e96d5ad3bb2ae0fe2a9c7d46098730ab44df610de9f95e5c7/68747470733a2f2f7472617669732d63692e6f72672f6d756467652f7068702d6d6963726f6b616e72656e2e706e673f6272616e63683d6d6173746572)](https://travis-ci.org/mudge/php-microkanren)
=========================================================================================================================================================================================================================================================================================================

[](#phpµkanren-)

A PHP implementation of [Jason Hemann and Daniel P. Friedman's µKanren](http://webyrd.net/scheme-2013/papers/HemannMuKanren2013.pdf).

Installation
------------

[](#installation)

Add the following to your `composer.json`:

```
{
    "require": {
        "mudge/php-microkanren": "v0.1.0"
    }
}
```

Usage
-----

[](#usage)

```
require_once 'vendor/autoload.php';

use MicroKanren\Core as U;

$f = U\callFresh(function ($q) {
  return U\eq($q, 5);
});

echo $f(U\emptyState());
/* => (((#(0) . 5)) . 1) */
```

Inside the `MicroKanren\Core` namespace, there are implementations of the core µKanren functions as described in the original paper as well as common Lisp primitives needed for their execution. As the reference implementation is in [Chez Scheme](http://www.scheme.com), this implementation attempts to mimic that particular Lisp as closely as possible.

Lisp Primitives
---------------

[](#lisp-primitives)

### `cons($car, $cdr)`

[](#conscar-cdr)

```
$c = cons(1, cons(2, cons(3, nil())));
```

Return a new [cons cell](http://en.wikipedia.org/wiki/Cons) with `$car` and `$cdr` (this is the most basic primitive for creating lists).

### `car($alist)`

[](#caralist)

```
$c = cons(1, cons(2, nil()));
car($c);
/* => 1 */
```

Return the first element of `$alist`.

### `cdr($alist)`

[](#cdralist)

```
$c = cons(1, cons(2, nil()));
cdr($c);
/* => cons(2, nil()) */
```

Return the rest of the `$alist`.

### `nil()`

[](#nil)

```
$n = nil();
$n === nil();
/* => true */
```

Return the empty list. Note that all instances of nil are identical.

### `isPair($obj)`

[](#ispairobj)

```
isPair(cons(1, nil())); /* => true  */
isPair(4);              /* => false */
isPair(nil());          /* => false */
```

Return true if `$obj` is a valid pair (viz. a cons cell that is not the empty list, equivalent to Petite Scheme's `pair?`).

### `isNull($obj)`

[](#isnullobj)

```
isNull(nil());          /* => true  */
isNull(cons(1, nil())); /* => false */
```

Return true is `$obj` is the empty list (equivalent to Petite Scheme's `null?`).

### `assp($proc, $alist)`

[](#asspproc-alist)

```
$list = alist(cons(1, 'a'), cons(2, 'b'));
$isEven = function ($x) { return $x % 2 === 0; };

assp($isEven, $list);
/* => cons(2, 'b') */
```

"Return the first element of `$alist` for whose car `$proc` returns true, or false." — [Petite Scheme's `assp`](http://www.scheme.com/csug7/objects.html#./objects:s15)

### `alist(...)`

[](#alist)

```
alist(1, 2, 3);
/* => cons(1, cons(2, cons(3, nil()))) */
```

A convenience function for constructing `cons` cells, equivalent to Petite Scheme's `list`.

### `length($alist)`

[](#lengthalist)

```
length(alist(1, 2, 3));
/* => 3 */
```

Return the length of `$alist`.

### `map($proc, $alist)`

[](#mapproc-alist)

```
$list = alist(1, 2, 3);
map(function ($x) { return $x + 1; }, $list);
/* => alist(2, 3, 4) */
```

Return a list resulting in applying `$proc` to each value in `$alist`.

µKanren functions
-----------------

[](#µkanren-functions)

### `variable($c)`

[](#variablec)

Return a new variable containing an index `$c` (equivalent to `var`).

### `isVariable($x)`

[](#isvariablex)

Returns true if `$x` is a variable (equivalent to `var?`).

### `isVariableEquals($x1, $x2)`

[](#isvariableequalsx1-x2)

Returns true if `$x1` and `$x2` refer to the same variable (equivalent to `var=?`).

### `mzero()`

[](#mzero)

Return the empty stream (equivalent to `mzero`).

### `walk($u, $s)`

[](#walku-s)

Searches for a variable's value in the substitution. If a non-variable term is walked, return that term.

### `extS($x, $v, $s)`

[](#extsx-v-s)

Extends the substitution with a new binding (equivalent to `ext-s`).

### `unit($sC)`

[](#unitsc)

Lifts the state into a stream whose only element is that state.

### `unify($u, $v, $s)`

[](#unifyu-v-s)

Unifies two terms in a substitution.

### `eq($u, $v)`

[](#equ-v)

Returns a goal that succeeds if two terms unify in the received state (equivalent to `≡` from the paper and `==` from the reference implementation).

### `callFresh($f)`

[](#callfreshf)

Returns a goal given a unary function whose body is a goal (equivalent to `call/fresh`).

### `mplus($d1, $d2)`

[](#mplusd1-d2)

Merges streams.

### `bind($d, $g)`

[](#bindd-g)

Invokes a goal on each element of a stream.

### `disj($g1, $g2)`

[](#disjg1-g2)

Returns a goal that succeeds if either of the given goals succeed.

### `conj($g1, $g2)`

[](#conjg1-g2)

Returns a goal that succeeds if both given goals succeed.

### `emptyState()`

[](#emptystate)

Returns a state with an empty substitution and variable index set to 0 (equivalent to `empty-state`).

### `pull($d)`

[](#pulld)

Advances a stream until it matures.

### `takeAll($d)`

[](#takealld)

Returns all results from a stream (equivalent to `take-all`).

### `take($n, $d)`

[](#taken-d)

Returns a `$n` results from a stream.

### `reifyName($n)`

[](#reifynamen)

Returns a string name for a given number (equivalent to `reify-name`).

### `reifyS($v, $s)`

[](#reifysv-s)

Reifies a state's substitution with respect to a variable (equivalent to `reify-s`).

### `walkStar($v, $s)`

[](#walkstarv-s)

Equivalent to `walk*`.

### `reifyFirst($sC)`

[](#reifyfirstsc)

Equivalent to `reify-1st`.

See [the test suite](https://github.com/mudge/php-microkanren/blob/master/tests/MicroKanren/CoreTest.php)for more examples of usage.

References
----------

[](#references)

- [Justin S. Leitgeb's microKanren in Ruby](https://github.com/jsl/ruby_ukanren);
- [Scott Vokes' Lua port of microKanren](https://github.com/silentbicycle/lua-ukanren);
- [Jason Hemann and Daniel P. Friedman's reference Scheme implementation](https://github.com/jasonhemann/microKanren).

License
-------

[](#license)

Copyright © 2014 Paul Mucur.

Distributed under the MIT License.

###  Health Score

25

—

LowBetter than 37% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity14

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity48

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Unknown

Total

1

Last Release

4485d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/f71f3185f57184403546c1e81f6cd6ceb2f4a76062a7b327c4c567de9c93e573?d=identicon)[mudge](/maintainers/mudge)

---

Top Contributors

[![mudge](https://avatars.githubusercontent.com/u/287?v=4)](https://github.com/mudge "mudge (26 commits)")

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/mudge-php-microkanren/health.svg)

```
[![Health](https://phpackages.com/badges/mudge-php-microkanren/health.svg)](https://phpackages.com/packages/mudge-php-microkanren)
```

###  Alternatives

[jacopo/bootstrap-3-table-generator

Dynamic table generator for bootstrap 3

1411.5k1](/packages/jacopo-bootstrap-3-table-generator)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
