Last active
April 29, 2025 15:03
-
-
Save jonneale/3c25fcdccf79f8bc4dc0ebfc26537b75 to your computer and use it in GitHub Desktop.
Implementation of Boolean Logic and Church Numerals in the Lambda calculus
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function TRUE(x,y){ | |
return x; | |
} | |
function FALSE(x,y){ | |
return y; | |
} | |
function AND(p,q){ | |
return p(q,p); | |
} | |
// AND(TRUE,FALSE); | |
// AND(FALSE,TRUE); | |
// AND(TRUE,TRUE); | |
function NOT(p) { | |
return p(FALSE,TRUE) | |
} | |
////////////Currying | |
function TRUE(x){ | |
return function (y){ | |
return x; | |
} | |
} | |
function FALSE(x){ | |
return function (y){ | |
return y; | |
} | |
} | |
function AND(x){ | |
return function(y){ | |
return x(y)(x); | |
} | |
} | |
// OR is a bit mindblowing | |
function OR(x){ | |
return function(y){ | |
return x(TRUE)(y(TRUE)(FALSE)) | |
} | |
} | |
// as an exercise for the reader, have a go at XOR | |
// AND(TRUE)(FALSE); | |
// AND(FALSE)(TRUE); | |
// AND(TRUE)(TRUE); | |
////////////lambda syntax | |
// TRUE = x => y => x; | |
// FALSE = x => y => y; | |
// AND = x => y => x(y)(x); | |
// CHURCH NUMERALS | |
////////////; Numbers | |
function ZERO(f){ | |
return function(x){ | |
return x; | |
} | |
} | |
function ONE(f){ | |
return function(x){ | |
return f(x); | |
} | |
} | |
function TWO(f){ | |
return function(x){ | |
return f(f(x)); | |
} | |
} | |
// add one | |
// cheating with an intermediate variable!! | |
// function SUCC(n){ | |
// return function(f){ | |
// return function(x){ | |
// const intermediate = n(f)(x); | |
// return f(intermediate); | |
// } | |
// } | |
// } | |
// n is a Church numeral — a function that takes a function f and returns another function taking x. | |
// n(f)(x) means: apply f to x, n times. | |
//// do it n times | |
//// f(n(f)(x)) | |
//// do it one more | |
function SUCC(n){ | |
return function(f){ | |
return function(x){ | |
return f(n(f)(x)); | |
} | |
} | |
} | |
toNumber = n => n(x => x + 1)(0); | |
// // ONE = SUCC(ZERO); | |
// TWO = SUCC(ONE); | |
THREE = SUCC(TWO); | |
FOUR = SUCC(THREE); | |
FIVE = SUCC(FOUR); | |
SIX = SUCC(FIVE); | |
function IS_ZERO(n){ | |
return n(function(_){ | |
return FALSE; | |
})(TRUE); | |
} | |
// n is a Church numeral — a function that takes a function f and returns another function taking x. | |
// n(f)(x) means: apply f to x, n times. | |
// Take the number n | |
// Apply a function that always returns FALSE exactly n times, starting with TRUE. | |
function MULT(m) { | |
return function(n) { | |
return function(f) { | |
return m(n(f)); | |
}; | |
}; | |
} | |
// PRED - predecessor (n - 1) | |
function PRED(n) { | |
return function(f) { | |
return function(x) { | |
return n( | |
function(g) { | |
return function(h) { | |
return h(g(f)); | |
}; | |
} | |
)(function(u) { return x; })(function(u) { return u; }); | |
}; | |
}; | |
} | |
function FACT(n) { | |
return IS_ZERO(n)( | |
function(_) { return ONE; } // if n == 0 | |
)( | |
function(_) { return MULT(n)(FACT(PRED(n))); } | |
)(ZERO); // <<==== CALL the selected branch, argument is ignored, can be anything | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment