TRUE/FALSE/AND/OR/NOT/数値の定理をJSのlambda式で書いてみた
code:javascript
// Debug用
const FN = (x) => x + 1;
const TRUE = 1;
const FALSE = 0;
// 0 := λf x. x
const _0 = (f) => (x) => x
// SUCC := λn f x. f (n f x)
const SUCC = (n) => (f) => (x) => f(n(f)(x));
// 1 := λf x. f x
const _1 = SUCC(_0);
// 2 := λf x. f (f x)
const _2 = SUCC(_1);
const _3 = SUCC(_2);
const _4 = SUCC(_3);
// PLUS := λm n f x. m f (n f x)
const PLUS = (m) => (n) => (f) => (x) => m(f)(n(f)(x));
// T := λx y. x
const T = (x) => (y) => x;
// F := λx y. y
const F = (x) => (y) => y;
// NOT := λp. p F T
const NOT = (p) => p(F)(T);
// OR := λp q. p T q
const OR = (p) => (q) => p(T)(q);
// AND := λp q. p q F
const AND = (p) => (q) => p(q)(F);
console.log(NOT(T)(TRUE)(FALSE)); // -> 0
console.log(NOT(F)(TRUE)(FALSE)); // -> 1
console.log(OR(T)(T)(TRUE)(FALSE)); // -> 1
console.log(OR(T)(F)(TRUE)(FALSE)); // -> 1
console.log(OR(F)(T)(TRUE)(FALSE)); // -> 1
console.log(OR(F)(F)(TRUE)(FALSE)); // -> 0
console.log(AND(T)(T)(TRUE)(FALSE)); // -> 1
console.log(AND(F)(T)(TRUE)(FALSE)); // -> 0
console.log(AND(T)(F)(TRUE)(FALSE)); // -> 0
console.log(AND(F)(F)(TRUE)(FALSE)); // -> 0
console.log(_0(FN)(0)); // -> 0
console.log(_1(FN)(0)); // -> 1
console.log(_2(FN)(0)); // -> 1
console.log(_3(FN)(0)); // -> 1
console.log(_4(FN)(0)); // -> 4
console.log(SUCC(_1)(FN)(0)); // -> 2
console.log(PLUS(_1)(_2)(FN)(0)); // -> 3
省略版
code:javascript
// Debug用
const FN = (x) => x + '1';
const TRUE = 1;
const FALSE = 2;
// 0 := λf x. x
const _0 = (f, x) => x
// 1 := λf x. f x
const _1 = (f, x) => f(x);
// 2 := λf x. f (f x)
const _2 = (f, x) => f(f(x));
// SUCC := λn f x. f (n f x)
const SUCC = (n) => (f, x) => f(n(f, x));
// PLUS := λm n f x. m f (n f x)
const PLUS = (m, n) => (f, x) => m(f, n(f, x));
// PLUS := λm n. m SUCC n
const PLUS_VER2 = (m, n) => m(SUCC, n);
const T = (x, y) => x;
const F = (x, y) => y;
// NOT := λp. p FALSE TRUE
const NOT = (p) => p(F, T);
// OR := λp q. p TRUE q
const OR = (p, q) => p(T, q);
// AND := λp q. p q FALSE
const AND = (p, q) => p(q, F);
console.log(NOT(T)(TRUE, FALSE)); // -> 1
console.log(NOT(F)(TRUE, FALSE)); // -> 2
console.log(OR(T, T)(TRUE, FALSE)); // -> 1
console.log(OR(T, F)(TRUE, FALSE)); // -> 1
console.log(OR(F, T)(TRUE, FALSE)); // -> 1
console.log(OR(F, F)(TRUE, FALSE)); // -> 2
console.log(AND(T, T)(TRUE, FALSE)); // -> 1
console.log(AND(F, T)(TRUE, FALSE)); // -> 2
console.log(AND(T, F)(TRUE, FALSE)); // -> 2
console.log(AND(F, F)(TRUE, FALSE)); // -> 2
console.log(SUCC(_1)(FN, '')); // -> 11
console.log(PLUS(_1, _2)(FN, '')); // -> 111
console.log(PLUS_VER2(_1, _2)(FN, '')); // -> 111
code:ruby
FN = (-> (x) {x + 1})
T = (-> (x, y) { x }).curry
F = (-> (x, y) { y }).curry
NOT = (-> (p) { pFT }).curry AND = (-> (p, q) {pqF}).curry OR = (-> (p, q) {pTq}).curry _0 = (-> (f, x) {x}).curry
SUCC = (-> (n, f, x) {f[nfx]}).curry PLUS = (-> (m, n, f, x) {mf[nfx]}).curry # MULT := λm n. m (PLUS n) 0
MULT = (-> (m, n) {m[PLUSn]_0}).curry # PRED := λn f x. n (λg h. h (g f)) (λu. x) (λu. u)
PRED = (-> (n, f, x) {n})
# PRED := λn. n (λg k. (g 1) (λu. PLUS (g k) 1) k) (λv. 0) 0