PHPackages                             loophp/church-encoding - 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. loophp/church-encoding

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

loophp/church-encoding
======================

Church encoding in PHP

40[1 issues](https://github.com/loophp/church-encoding/issues)PHPCI passing

Since Nov 10Pushed 3mo ago1 watchersCompare

[ Source](https://github.com/loophp/church-encoding)[ Packagist](https://packagist.org/packages/loophp/church-encoding)[ RSS](/packages/loophp-church-encoding/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependenciesVersions (3)Used By (0)

[![GitHub stars](https://camo.githubusercontent.com/53f95b59ac3558ac9bb8e71cd6d3a1d0bdee8fcdb97953d997692b911bdc6471/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f73746172732f6c6f6f7068702f6368757263682d656e636f64696e672e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/loophp/church-encoding)[![Sponsor on GitHub](https://camo.githubusercontent.com/f43e1f9ed3dc36b01e8cae156cf5e3fe01245deb7c49aed7ee2dfca00b446166/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f53706f6e736f722d4769744875622d627269676874677265656e2e7376673f7374796c653d666c61742d737175617265)](https://github.com/sponsors/drupol)

Church Encoding
===============

[](#church-encoding)

> A functional programming exercise in PHP demonstrating how data and logic can be represented purely using functions.

In mathematics, **Church encoding** is a means of representing data and operators in the Lambda calculus. Church numerals are a representation of natural numbers using Lambda notation. The method is named after [Alonzo Church](https://en.wikipedia.org/wiki/Alonzo_Church), who first encoded data in the Lambda calculus this way.

Church encoding is not intended as a practical implementation of primitive data types. Its main purpose is to show that primitive data types are not *necessary*to represent any computation. More information on [Wikipedia](https://en.wikipedia.org/wiki/Church_encoding).

History
-------

[](#history)

This library was created for **personal educational purposes** and made public. It was inspired by the work of [Marcelo Camargo](https://github.com/haskellcamargo).

Although available via Composer and [Packagist](https://packagist.org/packages/loophp/church-encoding), this library is primarily useful for **learning and experimentation** rather than production use.

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

[](#installation)

```
composer require loophp/church-encoding
```

Quick Example
-------------

[](#quick-example)

```
use Loophp\ChurchEncoding\Church;

// Church numerals
$zero = Church::zero();
$one = Church::succ($zero);
$two = Church::succ($one);

// Church booleans
$true = Church::true();
$false = Church::false();

echo $two->toInt(); // 2
```

Documentation
-------------

[](#documentation)

### Church numerals

[](#church-numerals)

Imagine a programming language that doesn’t support numbers or booleans, only **lambdas**. Could we still represent counting, addition, and multiplication? Yes: that’s the idea behind *Church numerals*.

A Church numeral is a function with two parameters: `λf . λx . something`

- The first parameter `f` is the *successor function*.
- The second parameter `x` represents *zero*.

Examples:

- `C0 = λf . λx . x`
- `C1 = λf . λx . f x`
- `C2 = λf . λx . f (f x)`

Each numeral applies the successor function `f` as many times as its numeric value. We can count, add, or multiply using these forms — though to “see” the result, we must count the applications of `f` manually.

### Church booleans

[](#church-booleans)

Booleans can also be represented using functions:

- `true = λx . λy . x`
- `false = λx . λy . y`

These can be used to define logic operators:

- `and = λM . λN . M (N true false) false`
- `or = λM . λN . M true (N true false)`
- `not = λM . M false true`

References
----------

[](#references)

- *Types and Programming Languages* ([TAPL](https://www.cis.upenn.edu/~bcpierce/tapl/))
- *Structure and Interpretation of Computer Programs* ([SICP](https://en.wikipedia.org/wiki/Structure_and_Interpretation_of_Computer_Programs))
- Lectures by [Robert “Corky” Cartwright](https://www.cs.rice.edu/~cork/)
- Gabriel Lebec: [Part 1](https://www.youtube.com/watch?v=3VQ382QG-y4) and [Part 2](https://www.youtube.com/watch?v=pAnLQ9jwN-E)
- Package [loophp/combinator](https://github.com/loophp/combinator)
- [Lambda calculus on Wikipedia](https://en.wikipedia.org/wiki/Lambda_calculus)
- [Church encoding on Wikipedia](https://en.wikipedia.org/wiki/Church_encoding)
- [Programming with Less Than Nothing](https://joshmoody.org/blog/programming-with-less-than-nothing/)

Code Quality, Tests and Benchmarks
----------------------------------

[](#code-quality-tests-and-benchmarks)

Every change triggers automated tests via [GitHub Actions](https://github.com/loophp/church-encoding/actions).

- Tests are written with [PHPSpec](http://www.phpspec.net/) (`composer phpspec`)
- Static analysis via [PHPStan](https://github.com/phpstan/phpstan) and [Psalm](https://github.com/vimeo/psalm)
- Mutation testing with [Infection](https://github.com/infection/infection) (`composer infection`)
- Pre-commit checks with [GrumPHP](https://github.com/phpro/grumphp) (`composer grumphp`)

Contributing
------------

[](#contributing)

Contributions are welcome! Send a pull request on GitHub.

If you prefer to support my open-source work financially, you can sponsor me on [GitHub](https://github.com/sponsors/drupol) or [PayPal](https://www.paypal.me/drupol).

Project Status
--------------

[](#project-status)

This project is **feature-complete** and stable. It is maintained for educational purposes and may receive occasional compatibility updates.

Changelog
---------

[](#changelog)

See [CHANGELOG.md](https://github.com/loophp/church-encoding/blob/master/CHANGELOG.md) for the commit-based changelog. For detailed release notes, visit [the release page](https://github.com/loophp/church-encoding/releases).

###  Health Score

20

—

LowBetter than 14% of packages

Maintenance48

Moderate activity, may be stable

Popularity4

Limited adoption so far

Community10

Small or concentrated contributor base

Maturity16

Early-stage or recently created project

 Bus Factor1

Top contributor holds 54.6% 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.

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/252042?v=4)[Pol Dellaiera](/maintainers/drupol)[@drupol](https://github.com/drupol)

---

Top Contributors

[![drupol](https://avatars.githubusercontent.com/u/252042?v=4)](https://github.com/drupol "drupol (53 commits)")[![dependabot[bot]](https://avatars.githubusercontent.com/in/29110?v=4)](https://github.com/dependabot[bot] "dependabot[bot] (32 commits)")[![renovate[bot]](https://avatars.githubusercontent.com/in/2740?v=4)](https://github.com/renovate[bot] "renovate[bot] (12 commits)")

---

Tags

church-encodingeducationalfunctional-programminglearning

### Embed Badge

![Health badge](/badges/loophp-church-encoding/health.svg)

```
[![Health](https://phpackages.com/badges/loophp-church-encoding/health.svg)](https://phpackages.com/packages/loophp-church-encoding)
```

###  Alternatives

[joaopaulolndev/filament-edit-env

Package to edit env file

322.5k](/packages/joaopaulolndev-filament-edit-env)

PHPackages © 2026

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