js-to-lambda
raw JSON → 0.4.0 verified Fri May 01 auth: no javascript abandoned
A transpiler that converts JavaScript code into lambda calculus expressions. Current version 0.4.0 is a WIP with limited functionality, supporting only function declarations, arguments, currying, and parsing functions as tokens. It converts JS functions to Church-encoded lambda terms but does not handle most JavaScript features (conditionals, loops, objects, etc.). Release cadence is irregular; the project is experimental and has not been updated since 2018. Differentiators: it's one of the few tools attempting JS-to-lambda conversion, but remains highly incomplete and is more a proof-of-concept than a production tool.
Common errors
error Variable 'a' at position X is already bound to a parent scope. ↓
cause Inner function parameter name shadows an outer function parameter.
fix
Rename the inner parameter to a unique name.
error SyntaxError: Unexpected token ↓
cause Using unsupported JavaScript syntax (e.g., arrow functions, if statements, object literals).
fix
Rewrite the code using only supported constructs: function declarations,
function keyword, basic arguments, and return statements. error TypeError: compile is not a function ↓
cause Incorrect import (CommonJS require instead of ESM import).
fix
Use
import { compile } from 'js-to-lambda' or const { compile } = require('js-to-lambda') if using Node with CJS interop. Warnings
gotcha The package is a work-in-progress and severely incomplete. It only handles function declarations, basic currying, and function application. Most JavaScript constructs (if/else, loops, objects, arrays, arithmetic operations) are not supported and will cause errors or incorrect output. ↓
fix Check documentation for supported syntax; avoid using any constructs not explicitly listed as supported.
breaking Shadowing of variable names in nested functions throws an error. For example, `function f(a) { return function(a) { return a; } }` will throw: "Variable 'a' at position X is already bound to a parent scope." ↓
fix Rename inner parameter to avoid shadowing the outer scope.
deprecated The package has not been updated since 2018 and is effectively abandoned. No further development or bug fixes are expected. ↓
fix Consider forking the project or using an alternative approach if needed.
Install
npm install js-to-lambda yarn add js-to-lambda pnpm add js-to-lambda Imports
- compile wrong
const compile = require('js-to-lambda')correctimport { compile } from 'js-to-lambda' - default wrong
const { default: jsToLambda } = require('js-to-lambda')correctimport jsToLambda from 'js-to-lambda' - compile
const { compile } = require('js-to-lambda')
Quickstart
import { compile } from 'js-to-lambda';
// Convert a simple function to lambda calculus
const code = `function f(a, b) { return a; }`;
const lambda = compile(code);
console.log(lambda); // λa.λb.a
// Curried function with multiple arguments
const code2 = `function add(x) { return function(y) { return x + y; } }`;
const lambda2 = compile(code2);
console.log(lambda2); // λx.λy. (x + y)
// Church encoding: boolean true
const trueCode = `function T(a, b) { return a; }`;
const trueLambda = compile(trueCode);
console.log(trueLambda); // λa.λb.a
// Church encoding: boolean false
const falseCode = `function F(a, b) { return b; }`;
const falseLambda = compile(falseCode);
console.log(falseLambda); // λa.λb.b