Home

Awesome

Latest Stable Version GitHub stars Total Downloads GitHub Workflow Status Scrutinizer code quality Type Coverage Code Coverage License Donate!

Combinator

Description

This package provides a list of well known Combinators.

A combinator is a higher-order function that uses only function application and earlier defined combinators to define a result from its arguments. It was introduced in 1920 by Moses Schönfinkel and Haskell Curry, and has more recently been used in computer science as a theoretical model of computation and also as a basis for the design of functional programming languages. Combinators which were introduced by Schönfinkel in 1920 with the idea of providing an analogous way to build up functions - and to remove any mention of variables - particularly in predicate logic.

Requirements

Installation

composer require loophp/combinator

Available combinators

NameAliasCompositionComposition using S and KHaskellLambda calculusTerm definition (JS like)Type# Arguments
AApplySK(SK)<details>(S(K))(S(K))</details>$λab.aba => b => a(b)(a -> b) -> a -> b2
BBluebirdS(KS)K<details>S(KS)K</details>.λabc.a(bc)a => b => c => a(b(c))(a -> b) -> (c -> a) -> c -> b3
BlackbirdBlackbirdBBB<details>(S(K(S(KS)K)))(S(KS)K)</details>...λabcd.a(bcd)a => b => c => => d => a(b(c)(d))(c -> d) -> (a -> b -> c) -> a -> b -> d4
CCardinalS(BBS)(KK)<details>((S((S(K(S(KS)K)))S))(KK))</details>flipλabc.acba => b => c => a(c)(b)(a -> b -> c) -> b -> a -> c3
DDoveBB<details>(S(K(S(KS)K)))</details>λabcd.ab(cd)a => b => c => d => a(b)(c(d))(a -> c -> d) -> a -> (b -> c) -> b -> d4
EEagleB(BBB)<details>(S(K((S(K(S(KS)K)))((S(KS))K))))</details>λabcde.ab(cde)a => b => c => d => e => a(b)(c(d)(e))(a -> d -> e) -> a -> (b -> c -> d) -> b -> c -> e5
FFinchETTET<details>((S(K((S((SK)K))(K((S(K(S((SK)K))))K)))))((S(K((S(K(S(KS)K)))((S(KS))K))))((S(K(S((SK)K))))K)))</details>λabc.cbaa => b => c => c(b)(a)a -> b -> (b -> a -> c) -> c3
GGoldfinchBBC<details>((S(K(S(KS)K)))((S((S(K(S(KS)K)))S))(KK)))</details>λabcd.ad(bc)a => b => c => d => a(d)(b(c))(a -> b -> c) -> (d -> b) -> d -> a -> c4
HHummingbirdBW(BC)<details>((S(K((S(K(S((S(K((S((SK)K))((SK)K))))((S(K(S(KS)K)))((S(K(S((SK)K))))K))))))K)))(S(K((S((S(K(S(KS)K)))S))(KK)))))</details>λabc.abcba => b => c => a(b)(c)(b)(a -> b -> a -> c) -> a -> b -> c3
IIdiotSKK<details>((SK)K)</details>idλa.aa => aa -> a1
JJayB(BC)(W(BC(E)))<details>((S(K(S(K((S((S(K(S(KS)K)))S))(KK))))))((S((S(K((S((SK)K))((SK)K))))((S(K(S(KS)K)))((S(K(S((SK)K))))K))))(K((S(K((S((S(K(S(KS)K)))S))(KK))))(S(K((S(K(S(KS)K)))((S(KS))K))))))))</details>λabcd.ab(adc)a => b => c => d => a(b)(a(d)(c))(a -> b -> b) -> a -> b -> a -> b4
KKestrelK<details>K</details>constλab.aa => b => aa -> b -> a2
KiKiteKI<details>(K((SK)K))</details>λab.ba => b => ba -> b -> b2
LLarkCBM<details>((S((S(KS))K))(K((S((SK)K))((SK)K))))</details>λab.a(bb)a => b => a(b(b))2
MMockingbirdSII<details>((S((SK)K))((SK)K))</details>λa.aaa => a(a)1
OOwlSI<details>(S((SK)K))</details>λab.b(ab)a => b => b(a(b))((a -> b) -> a) -> (a -> b) -> b2
OmegaΩMM<details>(((S((SK)K))((SK)K))((S((SK)K))((SK)K)))</details>λa.(aa)(aa)a => (a(a))(a(a))1
Phoenixλabcd.a(bd)(cd)a => b => c => d => a(b(d))(c(d))(a -> b -> c) -> (d -> a) -> (d -> b) -> d -> c4
Psionλabcd.a(bc)(bd)a => b => c => d => a(b(c))(b(d))(a -> a -> b) -> (c -> a) -> c -> c -> b4
QQueerCB<details>((S(K(S((S(KS))K))))K)</details>(##)λabc.b(ac)a => b => c => b(a(c))(a -> b) -> (b -> c) -> a -> c3
RRobinBBT<details>((S(K(S(KS)K)))((S(K(S((SK)K))))K))</details>λabc.bcaa => b => c => b(c)(a)a -> (b -> a -> c) -> b -> c3
SStarlingS<details>S</details><*>λabc.ac(bc)a => b => c => a(c)(b(c))(a -> b -> c) -> (a -> b) -> a -> c3
S_<*>λabc.a(bc)ca => b => c => a(b(c))(c)(a -> b -> c) -> (b -> a) -> b -> c3
S2<*>λabcd.a((bd)(cd))a => b => c => d => a(b(d))(c(d))(b -> c -> d) -> (a -> b) -> (a -> c) -> a -> d4
TThrushCI<details>((S(K(S((SK)K))))K)</details>(&)λab.baa => b => b(a)a -> (a -> b) -> b2
UTuringLO<details>((S(K(S((SK)K))))((S((SK)K))((SK)K)))</details>λab.b(aab)a => b => b(a(a)(b))2
VVireoBCT<details>((S(K((S((S(K(S(KS)K)))S))(KK))))((S(K(S((SK)K))))K))</details>λabc.caba => b => c => c(a)(b)a -> b -> (a -> b -> c) -> c3
WWarblerC(BMR)<details>((S(K(S((S(K((S((SK)K))((SK)K))))((S(K(S(KS)K)))((S(K(S((SK)K))))K))))))K)</details>λab.abba => b => a(b)(b)(a -> a -> b) -> a -> b2
YY-Fixed pointλa.(λb(a(bb))(λb(a(bb))))a => (b => b(b))(b => a(c => b(b)(c)))1
ZZ-Fixed pointλa.M(λb(a(Mb)))1

Usage

Simple combinators

<?php

declare(strict_types=1);

include 'vendor/autoload.php';

use loophp\combinator\Combinators;

// Lambda calculus: I combinator correspond to λa.a
Combinators::I()('a'); // a

// Lambda calculus: K combinator correspond to λa.λb.a (or λab.a)
Combinators::K()('a')('b'); // a

// Lambda calculus: C combinator correspond to λf(λa(λb(fba)))
// and thus: C K a b = b
Combinators::C()(Combinators::K())('a')('b'); // b

// Lambda calculus: Ki combinator correspond to λa.λb.b (or λab.b)
Combinators::Ki()('a')('b'); // b

Y combinator

<?php

declare(strict_types=1);

namespace Test;

include __DIR__ . '/vendor/autoload.php';

use Closure;
use loophp\combinator\Combinators;

// Example 1
$factorialGenerator = static fn (Closure $fact): Closure =>
static fn (int $n): int  => (0 === $n) ? 1 : ($n * $fact($n - 1));

$factorial = Combinators::Y()($factorialGenerator);

var_dump($factorial(6)); // 720

// Example 2
$fibonacciGenerator = static fn (Closure $fibo): Closure =>
static fn (int $number): int => (1 >= $number) ? $number : $fibo($number - 1) + $fibo($number - 2);

$fibonacci = Combinators::Y()($fibonacciGenerator);

var_dump($fibonacci(10)); // 55

More on the wikipedia page.

Suggested reading and resources

Contributing

Feel free to contribute by sending pull requests. We are a usually very responsive team and we will help you going through your pull request from the beginning to the end.

For some reasons, if you can't contribute to the code and willing to help, sponsoring is a good, sound and safe way to show us some gratitude for the hours we invested in this package.

Sponsor me on Github and/or any of the contributors.

Thanks

Authors

Changelog

See CHANGELOG.md for a changelog based on git commits.

For more detailed changelogs, please check the release changelogs.