PHPackages                             d3lph1/boollet - 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. d3lph1/boollet

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

d3lph1/boollet
==============

Boolean algebra toolkit for PHP

0.1.0(4y ago)410.0k↓32.7%MITPHPPHP ^8.1

Since Mar 12Pushed 4y ago2 watchersCompare

[ Source](https://github.com/D3lph1/Boollet)[ Packagist](https://packagist.org/packages/d3lph1/boollet)[ RSS](/packages/d3lph1-boollet/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (1)Dependencies (3)Versions (2)Used By (0)

 [![](logo.png)](logo.png)

Boollet
=======

[](#boollet)

Boollet is a boolean algebra toolkit for PHP.

[![PHP Version Require](https://camo.githubusercontent.com/39075c7fed260a07bb579c3e40aba7a7f4784d54551d6d9957b2067c11a7468b/687474703a2f2f706f7365722e707567782e6f72672f64336c7068312f626f6f6c6c65742f726571756972652f706870)](https://packagist.org/packages/d3lph1/boollet)[![License](https://camo.githubusercontent.com/f4e80b7d1472db458261d76d675f03cfef7d05d29683815b64059ec6aadf65ff/687474703a2f2f706f7365722e707567782e6f72672f64336c7068312f626f6f6c6c65742f6c6963656e7365)](https://packagist.org/packages/d3lph1/boollet)

Features
--------

[](#features)

- [Expression object API](#value-binding)
- [Expression parser](#expression-parser)
- [Building truth tables](#building-truth-table)
- [Complete conjunctive/disjunctive normal form calculation](#complete-conjunctivedisjunctive-normal-form-calculation)
- [Zhegalkin Polynomial calculation](#zhegalkin-polynomial-calculation)
- [SAT and UNSAT solvers](#sat-and-unsat-solvers)

Requirements
------------

[](#requirements)

- PHP &gt;= 8.1

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

[](#installation)

```
composer require d3lph1/boollet
```

Usage
-----

[](#usage)

### Expression object API

[](#expression-object-api)

You can create either `UnaryExpression` or `BinaryExpression` with one or two operands respectively:

```
use D3lph1\Boollet\Structure\Expression\{Variable, UnaryExpression, BinaryExpression};
use D3lph1\Boollet\Structure\Operator\{UnaryOperators, BinaryOperators};

$expr = new BinaryExpression(
    new UnaryExpression(UnaryOperators::NOT, new Variable(false)),
    BinaryOperators::AND,
    new BinaryExpression(new Variable(true), BinaryOperators::OR, new Variable(false, label: 'Z'))
);

echo $expr; // (!A ⋀ (B ⋁ Z))
```

> If there is no label for variable specified, it will be assigned with sequentially autogenerated symbols.

Evaluate the expression with initial variable values:

```
$val = $expr->evaluate(); // true
```

Evaluate the expression with overwritten variable values (It can be partially overwritten):

```
$val = $expr->evaluate(['A' => true, 'B' => true, 'Z' => true]) // false
```

### Value binding

[](#value-binding)

In the example above there are only static variable values which could not be changed dynamically without expression reconstructing.

To change value of variable at runtime you should use `Variable::set()` method. For convenient batch value setup there is `ValueBinder` class:

```
use D3lph1\Boollet\ValueBinder;

$a = new Variable(false);
$b = new Variable(true);
$z = new Variable(false, label: 'Z');

$expr = new BinaryExpression(
    new UnaryExpression(UnaryOperators::NOT, $a),
    BinaryOperators::AND,
    new BinaryExpression($b, BinaryOperators::OR, $z)
);

$binder = new ValueBinder();
$binder->bind($a);
$binder->bindAll([$b, $z]);

$binder->set([
    'A' => true,
    'B' => true,
    'Z' => true
])

$expr->evaluate(); // true
```

### Expression parser

[](#expression-parser)

For parsing stringed expressions uses `ShuntingYardParser` parser implementation. Under the hood it uses Dijkstra's algorithm of the same name.

```
use D3lph1\Boollet\Parser\{Lexer, Reader\StringInputReader, ShuntingYardParser};

$lexer = Lexer::default();
$input = new StringInputReader('X ⊕ Y → (X ⋀ Z)');
$parser = new ShuntingYardParser($lexer);

$expr = $parser->parse($input);

echo $expr; // ((X ⊕ Y) → (X ⋀ Z))
```

### Building truth table

[](#building-truth-table)

```
use D3lph1\Boollet\TruthTable;

$table = TruthTable::tabulate($expr);
$table->setLabel('f(X ⊕ Y → (X ⋀ Z))');

echo $table;
```

```
+---+---+---+--------------------+
| X | Y | Z | f(X ⊕ Y → (X ⋀ Z)) |
+---+---+---+--------------------+
| 0 | 0 | 0 |                  1 |
| 0 | 0 | 0 |                  1 |
| 0 | 1 | 0 |                  0 |
| 0 | 1 | 0 |                  0 |
| 1 | 0 | 0 |                  0 |
| 1 | 0 | 0 |                  1 |
| 1 | 1 | 0 |                  1 |
| 1 | 1 | 0 |                  1 |
+---+---+---+--------------------+

```

### Complete conjunctive/disjunctive normal form calculation

[](#complete-conjunctivedisjunctive-normal-form-calculation)

Class `NormalForms` provides utility methods to find complete conjunctive (or disjunctive ) normal form representations.

```
use D3lph1\Boollet\NormalForm\NormalForms;

// $expr ~ ((X ⊕ Y) → (X ⋀ Z))

$ccnf = NormalForms::calculateCompleteConjunctive($expr); // ((X ⋁ (!Y ⋁ Z)) ⋀ ((X ⋁ (!Y ⋁ !Z)) ⋀ (!X ⋁ (Y ⋁ Z))))
$cdnf = NormalForms::calculateCompleteDisjunctive($expr); // ((!X ⋀ (!Y ⋀ !Z)) ⋁ ((!X ⋀ (!Y ⋀ Z)) ⋁ ((X ⋀ (!Y ⋀ Z)) ⋁ ((X ⋀ (Y ⋀ !Z)) ⋁ (X ⋀ (Y ⋀ Z))))))
```

### Zhegalkin Polynomial calculation

[](#zhegalkin-polynomial-calculation)

For such needs you can use `ZhegalkinPolynomial` utility class:

```
use \D3lph1\Boollet\ZhegalkinPolynomial;

// $expr ~ (!X → ((!Y ⊕ X) ⋀ !Z))

$polynomial = ZhegalkinPolynomial::calculate($expr);

echo $polynomial; // ((Z ⋀ (Y ⋀ X)) ⊕ ((Y ⋀ X) ⊕ ((Z ⋀ X) ⊕ ((Z ⋀ Y) ⊕ (Y ⊕ (Z ⊕ 1))))))
```

### SAT and UNSAT solvers

[](#sat-and-unsat-solvers)

Boollet provides naive algorithm implementations to solve [boolean (un)satisfiability problem](https://en.wikipedia.org/wiki/Boolean_satisfiability_problem).

> SAT is the problem of determining if there exists an interpretation that satisfies a given boolean formula (formula becomes `true`).

> UNSAT is the problem of determining if there exists an interpretation that not satisfies a given boolean formula (formula becomes `false`).

`CompleteDisjunctiveNormalFormSATSolver` works only with expressions in complete disjunctive normal form. Whereas `CompleteConjunctiveNormalFormUNSATSolver` uses only expressions in complete conjunctive normal form.

The second argument of the method `findAllPossibleSolutions()` takes an array of variables with respect to which it is required to solve the problem. Other variables whose labels are not passed to this argument must have values (in the example below `y` is such variable).

```
use \D3lph1\Boollet\SAT\CompleteDisjunctiveNormalFormSATSolver;
// $expr ~ X ⋁ (Y ⋀ Z)

$y->set(false);

$cdnf = NormalForms::calculateCompleteDisjunctive($expr);

$sat = new CompleteDisjunctiveNormalFormSATSolver();
$solutions = $sat->findAllPossibleSolutions($cdnf, ['X', 'Z']);
$solutions = $sat->findAllPossibleSolutions($cdnf, ['X', 'Z']);
```

`$solutions` will look like this:

```
^ array:2 [▼
  0 => array:2 [▼
    "X" => true
    "Z" => false
  ]
  1 => array:2 [▼
    "X" => true
    "Z" => true
  ]
]
```

To conveniently define results constraints, you can use `findAllPossibleSolutionsWithConstraints()`:

```
use D3lph1\Boollet\Constraints\Constraints;

$solutions = $sat->findAllPossibleSolutionsWithConstraints($cdnf, ['X', 'Z'], new class() implements Constraints {
    public function isSatisfy(array $values): bool
    {
        return $values['X'];
    }
});
```

`$solutions` will look like this:

```
^ array:1 [▼
  0 => array:2 [▼
    "X" => true
    "Z" => false
  ]
]
```

License
-------

[](#license)

This code is published under the [MIT license](https://opensource.org/licenses/MIT). This means you can do almost anything with it, as long as the copyright notice and the accompanying license file is left intact.

###  Health Score

29

—

LowBetter than 59% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity29

Limited adoption so far

Community8

Small or concentrated contributor base

Maturity47

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

1528d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/80c741a8b692fa29d07a11f30a2e1a09cecd607cc52e888827b4faf3b2c74b7a?d=identicon)[D3lph1](/maintainers/D3lph1)

---

Top Contributors

[![D3lph1](https://avatars.githubusercontent.com/u/19612725?v=4)](https://github.com/D3lph1 "D3lph1 (6 commits)")

---

Tags

boolean-algebranormal-formssatsat-solvertruth-tablezhegalkin-polynomial

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/d3lph1-boollet/health.svg)

```
[![Health](https://phpackages.com/badges/d3lph1-boollet/health.svg)](https://phpackages.com/packages/d3lph1-boollet)
```

###  Alternatives

[mtdowling/supervisor-event

Wires callback functions to Supervisor events

123174.8k1](/packages/mtdowling-supervisor-event)[classiebit/addchat-laravel

Live chat widget for Laravel that also includes multi-user chat, group permissions, customer support chat &amp; more.

13912.0k](/packages/classiebit-addchat-laravel)

PHPackages © 2026

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