VictorTaelin/lambda-calculus
A simple, clean and fast implementation of the λ-calculus on JavaScript.
{ "createdAt": "2016-10-16T15:56:05Z", "defaultBranch": "master", "description": "A simple, clean and fast implementation of the λ-calculus on JavaScript. ", "fullName": "VictorTaelin/lambda-calculus", "homepage": "", "language": "JavaScript", "name": "lambda-calculus", "pushedAt": "2020-05-11T19:52:48Z", "stargazersCount": 45, "topics": [], "updatedAt": "2025-03-01T09:51:48Z", "url": "https://github.com/VictorTaelin/lambda-calculus"}Lambda Calculus
Section titled “Lambda Calculus”Note: there is a cleaner implementation on the issues, will merge eventually…
A simple, clean and fast implementation of the λ-calculus on JavaScript. It has zero dependencies and only 2.65KB gzipped. It evaluates terms very efficiently by using native JS functions, which is possible thanks to a clever compile/readback algorithm based on JS semantics. It includes the most relevant IO formats - bruijn-indices, binary, base64 and the usual “Wikipedia syntax” - so you can easily export to/from other tools. You can also convert native JS functions to terms and back.
Install
Section titled “Install”npm install lambda-calculusExample
Section titled “Example”const lam = require("lambda-calculus");
// Parses an input with the "Wikipedia syntax". The lambdas are optionalconst input = lam.fromString("(λa.λb.(a (a b)) λa.λb.(a (a b)))");
// Computes the result using native JS functionsconst output = lam.reduce(input);
// Print the resultconsole.log("Result: "+lam.toString(output));
// Print the result as bruijnconsole.log("Result (in bruijn): "+lam.toBruijn(output));
// Print the result as binaryconsole.log("Result (in binary): "+lam.toBLC(output));
// Print the result as base64console.log("Result (in base64): "+lam.toBLC64(output));
// There is a corresponding `lam.fromFormat(str)` for all functions aboveOutput:
Result: a.b.(a (a (a (a b))))Result (in bruijn): LL(1 (1 (1 (1 0))))Result (in binary): 00000111001110011100111010Result (in base64): EHOc6-- To/from native JS functionsfromFunction : Function -> TermtoFunction : Term -> Function
-- To/from JS numbers (using the church-encoding)fromNumber : Number -> TermtoNumber : Term -> Number
-- To/from the "Wikipedia syntax"fromString : String -> TermtoString : Term -> String
-- To/from bruijn-indicesfromBruijn : String -> TermtoBruijn : Term -> String
-- To/from binary lambda calculusfromBLC : String -> TermtoBLC : Term -> String
-- To/from base64-encoded binary lambda calculusfromBLC64 : String -> TermtoBLC64 : Term -> String
-- Reduces a term to normal formreduce : Term -> Termreduce = fromFunction . toFunction