Lambda calculus interpreter in PHP.

Overview

lambda-php

reduce forever

Lambda calculus interpreter in PHP.

Lambda calculus

Lambda calculus is a very minimal programming language that was invented in 1936 by Alonzo Church. It is the functional equivalent of the Turing Machine.

Lambda calculus has only three concepts: Function definitions, lexically scoped variables, function application.

An example term would be the identity function:

λx.x

The first part λx defines a function that takes an x, the . signifies that the part that follows is the function body. The body just returns x.

In PHP, you would write the same thing as follows:

function ($x) {
    return $x;
}

You can nest function definitions. Here is a function returning a function:

λx.λy.x

And you can also apply a function to an argument, which just means calling the function.

λf.λg.f g

Which is the short hand (left-associative) form of writing

λf.λg.(f g)

Nested calls like:

λf.λg.λh.f g h

Are interpreted as:

λf.λg.λh.((f g) h)

If you want to change the grouping to be right-associative, you need to explicitly group them in parentheses:

λf.λg.λh.(f (g h))

Interestingly, lambda calculus is turing complete. Using just these three concepts you can represent any computation.

Check out the links at the bottom for more details on how to do stuff in lambda calculus.

Interpreter

This project consists of a lambda calculus expression parser using dissect, and an eval-apply interpreter based on Matt Might's implementation in scheme.

The interpreter is call-by-value which means that recursive calls need to be wrapped in a function to prevent them from being evaluated eagerly.

For examples of how to do numbers (church encoding), booleans, arithmetic, boolean logic, looping (recursion), etc. look at example.php.

REPL

This project ships with a read-eval-print-loop that you can use to evaluate lambda calculus expressions:

$ php repl.php

By default, it is in int-mode, expecting the result of the expression to be a church-encoded number. Example:

$ php repl.php
i> λf.λx.f (f (f x))
3

You can switch to bool-mode by sending the b command:

$ php repl.php
i> b
b> λx.λy.x
true

Or r for raw mode:

$ php repl.php
i> r
r> λx.x
λx.x

WIP

A few things are still a work in progress:

  • Krivine machine: This alternate interpreter would allow call-by-need and indexing into de-bruijn indices, which is needed by...

  • Binary lambda calculus: Allows encoding lambda calculus programs in binary form which produces extremely small programs. This also defines an I/O mechanism.

References

Thanks to

You might also like...
A sampling profiler for PHP written in PHP, which reads information about running PHP VM from outside of the process.

Reli Reli is a sampling profiler (or a VM state inspector) written in PHP. It can read information about running PHP script from outside of the proces

PHP Meminfo is a PHP extension that gives you insights on the PHP memory content

MEMINFO PHP Meminfo is a PHP extension that gives you insights on the PHP memory content. Its main goal is to help you understand memory leaks: by loo

A sampling profiler for PHP written in PHP, which reads information about running PHP VM from outside of the process.

Reli Reli is a sampling profiler (or a VM state inspector) written in PHP. It can read information about running PHP script from outside of the proces

A multithreaded application server for PHP, written in PHP.

appserver.io, a PHP application server This is the main repository for the appserver.io project. What is appserver.io appserver.io is a multithreaded

Easy to use utility functions for everyday PHP projects. This is a port of the Lodash JS library to PHP

Lodash-PHP Lodash-PHP is a port of the Lodash JS library to PHP. It is a set of easy to use utility functions for everyday PHP projects. Lodash-PHP tr

A PHP 5.3+ and PHP 7.3 framework for OpenGraph Protocol

Opengraph Test with Atoum cd Opengraph/ curl -s https://getcomposer.org/installer | php php composer.phar install --dev ./vendor/atoum/atoum/bin/atoum

A status monitor for Elite Dangerous, written in PHP. Designed for 1080p screens in the four-panel-view in panel.php, and for 7 inch screens with a resolution of 1024x600 connected to a Raspberry Pi.

EDStatusPanel A status monitor for Elite Dangerous, written in PHP. Designed for 1080p screens in the four-panel-view in panel.php, and for 7 inch scr

🐘 A probe program for PHP environment (一款精美的 PHP 探針, 又名X探針、劉海探針)

Simplified Chinese | 简体中文 Traditional Chinese(Taiwan) | 正體中文(臺灣) Traditional Chinese(Hong Kong) | 正體中文(香港) Japanese | 日本語 😎 X Prober This is a probe

PHP Text Analysis is a library for performing Information Retrieval (IR) and Natural Language Processing (NLP) tasks using the PHP language

php-text-analysis PHP Text Analysis is a library for performing Information Retrieval (IR) and Natural Language Processing (NLP) tasks using the PHP l

Owner
Igor
Igor
The PHP Interpreter

The PHP Interpreter PHP is a popular general-purpose scripting language that is especially suited to web development. Fast, flexible and pragmatic, PH

php.net 34.8k Dec 28, 2022
Analyzer of PHP code to search issues with deprecated functionality in newer interpreter versions.

PhpDeprecationDetector PhpDeprecationDetector - analyzer of PHP code to search usages of deprecated functionality in newer interpreter versions - depr

Sergey 312 Dec 26, 2022
Analyzer of PHP code to search issues with deprecated functionality in newer interpreter versions.

PhpDeprecationDetector PhpDeprecationDetector - analyzer of PHP code to search usages of deprecated functionality in newer interpreter versions - depr

Sergey 312 Dec 26, 2022
Sey is a powerful math interpreter with infinite-precision.

Sey, pronounce say, is a powerful math interpreter with infinite-precision.

Félix Dorn 13 Oct 1, 2022
λ Run PHP Coroutines & Fibers as-a-Service on the AWS Lambda.

λ Swoole Runtime for AWS Lambda Run PHP Coroutines & Fibers as-a-Service on the AWS Lambda. Getting started Create your Lambda function index.php <?ph

Leo Cavalcante 32 Dec 29, 2022
PHP Runtime Layer for AWS Lambda

PHP Layer For AWS Lambda Ever wanted to run PHP websites in AWS Lambda? It's your lucky day! This Lambda Runtime Layer runs the PHP 7.3/7.1 webserver

Stackery 319 Nov 30, 2022
A tool to create php lambda's in AWS via custom runtime api

Getting Started This composer library assists in the creation, configuration, and testing of an AWS Lambda function. It utilizes the AWS Lambda custom

Mike McGrath 0 Jul 13, 2022
Because every Wedding RSVP website needs to follow DDD, CQRS, Hexagonal Architecture, Event Sourcing, and be deployed on Lambda.

Our Wedding Website Because every Wedding RSVP website needs to follow DDD, CQRS, Hexagonal Architecture, Event Sourcing, and be deployed on Lambda. ?

Edd Mann 3 Aug 21, 2022
Bridge to use Symfony Messenger on AWS Lambda with Bref

Bridge to use Symfony Messenger on AWS Lambda with Bref. This bridge allows messages to be dispatched to SQS, SNS or EventBridge, while workers handle

bref 58 Nov 15, 2022
The Current US Version of PHP-Nuke Evolution Xtreme v3.0.1b-beta often known as Nuke-Evolution Xtreme. This is a hardened version of PHP-Nuke and is secure and safe. We are currently porting Xtreme over to PHP 8.0.3

2021 Nightly Builds Repository PHP-Nuke Evolution Xtreme Developers TheGhost - Ernest Allen Buffington (Lead Developer) SeaBeast08 - Sebastian Scott B

Ernest Buffington 7 Aug 28, 2022