Skip to content

Instantly share code, notes, and snippets.

@maxcai314
Last active April 18, 2025 16:21
Show Gist options
  • Save maxcai314/be791a389d70ce8cf76d08a7d64ce91a to your computer and use it in GitHub Desktop.
Save maxcai314/be791a389d70ce8cf76d08a7d64ce91a to your computer and use it in GitHub Desktop.
Pure Lambda Calculus in Google Sheets
=LET(
BEGIN_LIB_BOOL, "standard boolean library",
identity_l, LAMBDA(x, x),
true_l, LAMBDA(a, LAMBDA(b, a)),
false_l, LAMBDA(a, LAMBDA(b, b)),
not_l, LAMBDA(a, a(false_l)(true_l)),
and_l, LAMBDA(a, LAMBDA(b, a(b)(false_l))),
bool_to_literal, LAMBDA(b, b(TRUE)(FALSE)),
literal_to_bool, LAMBDA(a, IF(a, true_l, false_l)),
BEGIN_LIB_NUMERAL, "church encoding for natural numbers",
zero_l, LAMBDA(f, LAMBDA(x, x)),
one_l, LAMBDA(f, LAMBDA(x, f(x))),
two_l, LAMBDA(f, LAMBDA(x, f(f(x)))),
three_l, LAMBDA(f, LAMBDA(x, f(f(f(x))))),
succ_l, LAMBDA(n, LAMBDA(f, LAMBDA(x, f((n)(f)(x))))),
plus_l, LAMBDA(m, LAMBDA(n, n(succ_l)(m))),
times_l, LAMBDA(m, LAMBDA(n, LAMBDA(f, m(n(f))))),
pred_l, LAMBDA(n, LAMBDA(f, LAMBDA(x, n(LAMBDA(g, LAMBDA(h, h(g(f)))))(LAMBDA(u, x))(LAMBDA(u, u))))),
minus_l, LAMBDA(m, LAMBDA(n, n(pred_l)(m))),
exp_l, LAMBDA(b, LAMBDA(n, n(b))),
is_zero_l, LAMBDA(n, n(LAMBDA(any, false_l))(true_l)),
num_to_literal, LAMBDA(n, n(LAMBDA(l, l+1))(0)),
literal_to_num, LAMBDA(l, pred_l(REDUCE(zero_l, SEQUENCE(l+1), LAMBDA(num, i, succ_l(num))))),
BEGIN_USER_CODE, "user code for this program cell",
input, literal_to_num(6),
SUPPLIER_THROWAWAY_KEY, "input for lazy supplier lambda",
base_case_supplier, LAMBDA(_, one_l),
factorial_bootstrap, LAMBDA(f, LAMBDA(x, (is_zero_l(x) (base_case_supplier) (LAMBDA(_, times_l(x)((f)(f) (minus_l(x)(one_l))))) )(SUPPLIER_THROWAWAY_KEY))),
factorial, (factorial_bootstrap) (factorial_bootstrap),
RETURN_VALUE, "the output of this program cell",
CONCATENATE(num_to_literal(input), "! = ", num_to_literal(factorial(input)))
)
=LET(
BEGIN_LIB_BOOL, "standard boolean library",
identity_l, LAMBDA(x, x),
true_l, LAMBDA(a, LAMBDA(b, a)),
false_l, LAMBDA(a, LAMBDA(b, b)),
not_l, LAMBDA(a, a(false_l)(true_l)),
and_l, LAMBDA(a, LAMBDA(b, a(b)(false_l))),
bool_to_literal, LAMBDA(b, b(TRUE)(FALSE)),
literal_to_bool, LAMBDA(a, IF(a, true_l, false_l)),
BEGIN_LIB_NUMERAL, "church encoding for natural numbers",
zero_l, LAMBDA(f, LAMBDA(x, x)),
one_l, LAMBDA(f, LAMBDA(x, f(x))),
two_l, LAMBDA(f, LAMBDA(x, f(f(x)))),
three_l, LAMBDA(f, LAMBDA(x, f(f(f(x))))),
succ_l, LAMBDA(n, LAMBDA(f, LAMBDA(x, f((n)(f)(x))))),
plus_l, LAMBDA(m, LAMBDA(n, n(succ_l)(m))),
times_l, LAMBDA(m, LAMBDA(n, LAMBDA(f, m(n(f))))),
pred_l, LAMBDA(n, LAMBDA(f, LAMBDA(x, n(LAMBDA(g, LAMBDA(h, h(g(f)))))(LAMBDA(u, x))(LAMBDA(u, u))))),
minus_l, LAMBDA(m, LAMBDA(n, n(pred_l)(m))),
exp_l, LAMBDA(b, LAMBDA(n, n(b))),
is_zero_l, LAMBDA(n, n(LAMBDA(any, false_l))(true_l)),
num_to_literal, LAMBDA(n, n(LAMBDA(l, l+1))(0)),
literal_to_num, LAMBDA(l, pred_l(REDUCE(zero_l, SEQUENCE(l+1), LAMBDA(num, i, succ_l(num))))),
BEGIN_LIB_TUPLE, "functions for pairs and tuples",
make_pair, LAMBDA(a, LAMBDA(b, LAMBDA(f, f(a)(b)))),
pair_first, LAMBDA(a, LAMBDA(b, a)),
pair_second, LAMBDA(a, LAMBDA(b, b)),
BEGIN_USER_CODE, "user code for this program cell",
input, literal_to_num(8),
base_case, make_pair(zero_l)(one_l),
factorial_next, LAMBDA(state, make_pair(succ_l(state(pair_first)))(times_l(succ_l(state(pair_first)))(state(pair_second)))),
factorial, LAMBDA(n, n(factorial_next)(base_case)(pair_second)),
RETURN_VALUE, "the output of this program cell",
CONCATENATE(num_to_literal(input), "! = ", num_to_literal(factorial(input)))
)
@maxcai314
Copy link
Author

Every variable is closed-form and can be inlined, and this code uses no other excel/google sheets features other than for parsing inputs and decoding outputs. Each lambda has exactly one input value, and more complex functions use currying.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment