Created
October 16, 2019 16:32
-
-
Save lucaslugao/2865200ad8de5402ad4e1b2e5ad6aa3c to your computer and use it in GitHub Desktop.
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
const assert = require("assert"); | |
c0 = (s, z) => z; | |
succ = n => (s, z) => s(n(s, z)); | |
add = (x, y) => (s, z) => x(s, y(s, z)); | |
mul = (x, y) => (s, z) => x(z => y(s, z), z); | |
T = (x, y) => x; | |
F = (x, y) => y; | |
and = (a, b) => a(b, F); | |
or = (a, b) => a(T, b); | |
not = a => a(F, T); | |
phi = p => z => z(succ(p(T)), p(T)); | |
pred = n => n(phi, createPair(c0, c0))(F); | |
createPair = (a, b) => z => z(a, b); | |
churchToNum = n => n(x => x + 1, 0); | |
churchToPair = p => [churchToNum(p(T)), churchToNum(p(F))]; | |
churchToBool = b => b(true, false); | |
for (let i = 1; i < 100; i++) global["c" + i] = succ(global["c" + (i - 1)]); | |
assert.strictEqual(churchToNum(c15), 15); | |
assert.strictEqual(churchToNum(succ(c15)), 16); | |
assert.strictEqual(churchToNum(pred(c10)), 9); | |
assert.strictEqual(churchToNum(add(c15, c8)), 23); | |
assert.strictEqual(churchToNum(mul(c13, c2)), 26); | |
assert.deepEqual(churchToPair(createPair(c2, c4)), [2, 4]); | |
assert.strictEqual(churchToBool(T), true); | |
assert.strictEqual(churchToBool(F), false); | |
assert.strictEqual(churchToBool(not(F)), true); | |
assert.strictEqual(churchToBool(and(T, F)), false); | |
assert.strictEqual(churchToBool(or(T, F)), true); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment