Skip to content

Repository files navigation

lambda

lambda is a really tiny library that brings lambda calculus in Java.

Build StatusCodacy BadgeLanguage grade: JavaTotal alertsCoverage Status

Introduction

One of the goals of the project is to make Java look like lambda calculus as much as possible. The identity function I, for example, is pretty close.

λI = (λx) -> x;

β-reductions are introduced by the word β; for example, the AND function becomes

λAND = (λa) -> (λb) -> β(a, b, a);

Performances

Long story short, it sucks, but it has never been the goal.

Function Library

The library contains a good number of λ-expressions. For example, three different fixed-point combinators (X, Y, and Θ).

λX = (λf) -> β((λx) -> β(x, x), (λx) -> β(f, β(x, x)));
λY = (λf) -> β((λx) -> β(f, β(x, x)), (λx) -> β(f, β(x, x)));
λΘ = β((λx) -> (λy) -> β(y, β(x, x, y)), (λx) -> (λy) -> β(y, β(x, x, y)));

Church encoding for numbers through 16 and powers of two through 1024 have been implemented.

λZERO = (λf) -> (λx) -> x;
λONE = β(SUCC, ZERO);
λTWO = β(SUCC, ONE);
// ...λONETHOUSANDTWENTYFOUR = β(EXP, TWO, TEN);

Standard high-order functions such as FILTER, MAP, FOLDL and FOLDR.

Examples

λFACT = β(Y, (λf) -> (λn) -> β(IF, β(LEQ, n, ONE),
ONE,
β(MUL, n, β(f, β(PRED, n)))));
λFIB = β(Y, (λf) -> (λn) -> β(IF, β(LEQ, n, TWO),
ONE,
β(ADD, β(f, β(PRED, n)), β(f, β(SUB, n, TWO)))));
λCOLLATZ = β(Y, (λf) -> (λn) -> β(IF, β(EQ, n, ONE),
β(CONS, ONE, NIL),
β(CONS, n, β(f, β(IF, β(ISEVEN, n),
β(DIV, n, TWO),
β(SUCC, β(MUL, n, THREE)))))));
λSORT = β(Y, (λf) -> (λl) -> β(IF, β(ISNIL, l),
NIL,
β(CAT, β(CAT, β(f, β(FILTER, β(FLIP, LEQ, β(HEAD, l)), β(TAIL, l))),
β(CONS, β(HEAD, l), NIL)),
β(f, β(FILTER, β(FLIP, GREAT, β(HEAD, l)), β(TAIL, l))))));

About

Lambda Calculus Standard Functions in Java

Topics

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages