Skip to content

Instantly share code, notes, and snippets.

@keller
Created February 11, 2021 04:40
Show Gist options
  • Save keller/86f0bf2e370235fcb0145178af36f929 to your computer and use it in GitHub Desktop.
Save keller/86f0bf2e370235fcb0145178af36f929 to your computer and use it in GitHub Desktop.
//Lambda Calc JS
const TRUE = x => y => x;
const FALSE = x => y => y;
const ZERO = x => y => y;
const ONE = x => y => x(y);
const SUCC = n => x => y => x(n(x)(y));
const MULT = x => y => z => x(y(z));
const PAIR = x => y => f => f(x)(y);
const CAR = p => p(x => y => x);
const CDR = p => p(x => y => y);
const PHI = p => PAIR(CDR(p))(SUCC(CDR(p)));
const PRED = n => CAR(n(PHI)(PAIR(ZERO)(ZERO)));
const SUB = x => y => y(PRED)(x);
const IS_ZERO = n => n(x => FALSE)(TRUE);
const LEQ = x => y => IS_ZERO(SUB(x)(y));
const Z = f => (x => f(y => x(x)(y)))(x => f(y => x(x)(y)));
const FAC = Z(f => n => LEQ(ONE)(n)(x => MULT(n)(f(PRED(n)))(x))(ONE));
// 5! = 120
print(FAC(to_cn(5)));
// --- JS helpers. not strict lambda calc below
function print(n) {
console.log(n(x => x + 1)(0));
}
function to_cn(x) {
return Z(f => x => (x === 0 ? ZERO : y => SUCC(f(x - 1))(y)))(x);
}
console.log(
(x =>
(n => n(x => x + 1)(0))(
(x => (y => x(z => y(y)(z)))(y => x(z => y(y)(z))))(x => y =>
(x => y =>
(x => x(x => x => y => y)(x => y => x))(
(x => y =>
y(x =>
(x => x(x => y => x))(
x(x =>
(x => y => z => z(x)(y))((x => x(x => y => y))(x))(
(x => y => z => y(x(y)(z)))((x => x(x => y => y))(x))
)
)((x => y => z => z(x)(y))(x => y => y)(x => y => y))
)
)(x))(x)(y)
))(x => y => x(y))(y)(z =>
(x => y => z => x(y(z)))(y)(
x(
(x =>
(x => x(x => y => x))(
x(x =>
(x => y => z => z(x)(y))((x => x(x => y => y))(x))(
(x => y => z => y(x(y)(z)))((x => x(x => y => y))(x))
)
)((x => y => z => z(x)(y))(x => y => y)(x => y => y))
))(y)
)
)(z)
)(x => y => x(y))
)(
(x =>
(x => (y => x(z => y(y)(z)))(y => x(z => y(y)(z))))(x => y =>
y === 0
? x => y => y
: z => (x => y => z => y(x(y)(z)))(x(y - 1))(z)
)(x))(x)
)
))(5)
);
/* minified:
FAC = x=>(n=>n(x=>x+1)(0))((x=>(y=>x(z=>y(y)(z)))(y=>x(z=>y(y)(z))))(x=>y=>(x=>y=>(x=>x(x=>x=>y=>y)(x=>y=>x))((x=>y=>y(x=>(x=>x(x=>y=>x))(x(x=>(x=>y=>z=>z(x)(y))((x=>x(x=>y=>y))(x))((x=>y=>z=>y(x(y)(z)))((x=>x(x=>y=>y))(x))))((x=>y=>z=>z(x)(y))(x=>y=>y)(x=>y=>y))))(x))(x)(y)))(x=>y=>x(y))(y)(z=>(x=>y=>z=>x(y(z)))(y)(x((x=>(x=>x(x=>y=>x))(x(x=>(x=>y=>z=>z(x)(y))((x=>x(x=>y=>y))(x))((x=>y=>z=>y(x(y)(z)))((x=>x(x=>y=>y))(x))))((x=>y=>z=>z(x)(y))(x=>y=>y)(x=>y=>y))))(y)))(z))(x=>y=>x(y)))((x=>(x=>(y=>x(z=>y(y)(z)))(y=>x(z=>y(y)(z))))(x=>y=>y===0?x=>y=>y:z=>(x=>y=>z=>y(x(y)(z)))(x(y-1))(z))(x))(x)))
*/
//Lambda Calc JS
const I = x => x;
const TRUE = x => y => x;
const FALSE = x => y => y;
const ZERO = x => y => y;
const ONE = x => y => x(y);
const TWO = x => y => x(x(y));
const SUCC = n => x => y => x(n(x)(y));
const THREE = SUCC(TWO);
const FOUR = SUCC(SUCC(TWO));
const ADD = x => y => x(SUCC)(y);
const FIVE = ADD(TWO)(THREE);
const TEN = ADD(FIVE)(FIVE);
const MULT = x => y => z => x(y(z));
const ONE_HUNDRED = MULT(TEN)(TEN);
const IS_ZERO = n => n(x => FALSE)(TRUE);
const PAIR = x => y => f => f(x)(y);
const CAR = p => p(x => y => x);
const CDR = p => p(x => y => y);
const PHI = p => PAIR(CDR(p))(SUCC(CDR(p)));
const PRED = n => CAR(n(PHI)(PAIR(ZERO)(ZERO)));
const NINE = PRED(TEN);
const SUB = x => y => y(PRED)(x);
const SIX = SUB(TEN)(FOUR);
const NOT = x => x(FALSE)(TRUE);
const AND = x => y => x(y)(FALSE);
const OR = x => y => x(TRUE)(y);
const LEQ = x => y => IS_ZERO(SUB(x)(y));
const GEQ = x => y => IS_ZERO(SUB(y)(x));
const GT = x => y => NOT(LEQ(x)(y));
const LT = x => y => NOT(GEQ(x)(y));
const EQ = x => y => AND(LEQ(x)(y))(LEQ(y)(x));
const Z = f => (x => f(y => x(x)(y)))(x => f(y => x(x)(y)));
const FAC = Z(f => n => GT(n)(ONE)(x => MULT(n)(f(SUB(n)(ONE)))(x))(ONE));
printNum(FAC(FIVE));
// js helpers
function print(f) {
console.log(f);
}
function printNum(n) {
print(n(x => x + 1)(0));
}
module.exports = {
TRUE,
FALSE,
ZERO,
ONE,
SUCC,
ADD,
MULT,
PAIR,
CAR,
CDR,
PHI,
PRED,
SUB,
NOT,
AND,
OR,
LE,
GE,
GT,
LT,
EQ,
Z,
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment