Skip to content

Instantly share code, notes, and snippets.

@lf94
Created April 12, 2019 20:54
Show Gist options
  • Save lf94/4bcde7ef22dafd3d296868a695024624 to your computer and use it in GitHub Desktop.
Save lf94/4bcde7ef22dafd3d296868a695024624 to your computer and use it in GitHub Desktop.
// Propositional logic implemenation
const True = a => b => a;
const False = a => b => b;
const If = x => x;
const Not = a => a(False)(True);
const And = a => b => a(True)(False)(b(True)(False))(False);
const Or = a => b => a(True)(b(True)(False));
const Xor = a => b => And(Or(a)(b))(Not(And(a)(b)));
// SKI calculus implementation
const S = x => y => z => x(z)(y(z));
const K = x => y => x;
const I = x => x;
// Iota combinator calculus
const i = x => x(S)(K);
// Lists
const First = True;
const Second = False;
const Nil = False;
const Pair = P = fst => snd => func => func(fst)(snd);
const Map = M = func => fst => snd => Pair(func(fst))(snd(Map(func)));
const Push = p => el => P(el)(P(p(First))(p(Second)));
// Arithmetic
const Add = m => n => f => x => m(f)(n(f)(x));
const Mul = m => n => f => m(n(f));
const _0 = False;
const _1 = f => x => f(x);
const _2 = Add(_1)(_1);
const _3 = Add(_1)(_2);
const _4 = Mul(_2)(_2);
const _5 = Add(_2)(_3);
const _6 = Add(_2)(_4);
const _7 = Add(_1)(_6);
const _8 = Mul(_2)(_4);
const _9 = Add(_1)(_8);
const _10 = Mul(_2)(_5);
// Binary
const Byte = _8(P(_0))(Nil);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment