Skip to content

Instantly share code, notes, and snippets.

@pingbird
Created April 19, 2018 20:24
Show Gist options
  • Save pingbird/230f4fa6828f2273dcb456abd07bba79 to your computer and use it in GitHub Desktop.
Save pingbird/230f4fa6828f2273dcb456abd07bba79 to your computer and use it in GitHub Desktop.
local null = function(null) return null end
local _return = function(ret) return ret end
local _if = function(cond) return function(ontrue) return function(onfalse) return cond(ontrue)(onfalse) end end end
local _while = function(iv) return function(lb) return function(l) return (function(body) return lb(iv)(body(body)(iv))(iv) end)(function(body) return function(st) return (function(nst) return _if(lb(nst))(body(body)(nst))(nst) end)(l(st)) end end) end end end
local _until = function(iv) return function(lb) return function(l) return (function(body) return body(body)(iv) end)(function(body) return function(st) return (function(nst) return _if(lb(nst))(nst)(body(body)(nst)) end)(l(st)) end end) end end end
local match = function(cmp) return function(val) return function(res) return function(d) return res(function(e) return function(nx) return cmp(val)(e(tFirst))(e(tSecond))(nx) end end)(d) end end end end
local _true = function(t) return function(f) return t end end
local _false = function(t) return function(f) return f end end
local bNot = function(b) return b(_false)(_true) end
local bOr = function(a) return function(b) return a(_true)(b(_true)(_false)) end end
local bAnd = function(a) return function(b) return a(b(_true)(_false))(_false) end end
local bXor = function(a) return function(b) return a(b(_false)(_true))(b(_true)(_false)) end end
local uSucc = function(n) return function(f) return function(x) return f(n(f)(x)) end end end
local uPred = function(n) return function(f) return function(x) return n(function(g) return function(h) return h(g(f)) end end)(function(u) return x end)(function(u) return u end) end end end
local uAdd = function(m) return function(n) return function(f) return function(x) return m(f)(n(f)(x)) end end end end
local uSub = function(m) return function(n) return n(uPred)(m) end end
local uMul = function(m) return function(n) return function(f) return m(n(f)) end end end
local uPow = function(m) return function(n) return function(f) return function(x) return n(m)(f)(x) end end end end
local uIsZero = function(n) return n(function(x) return _false end)(_true) end
local uMod = function(n) return function(m) return (function(x) return function(n) return x(n)(uPred)(n) end end)(n(function(nx) return uPred(uIsZero(nx)(m)(nx)) end))(uPred(m)) end end
local uDiv = function(m) return function(n) return n end end
local uEQ = function(m) return function(n) return bAnd(uIsZero(uSub(m)(n)))(uIsZero(uSub(n)(m))) end end
local uNEQ = function(m) return function(n) return bNot(bAnd(uIsZero(uSub(m)(n)))(uIsZero(uSub(n)(m)))) end end
local uGT = function(m) return function(n) return bAnd(uIsZero(uSub(n)(m)))(bNot(uIsZero(uSub(m)(n)))) end end
local uLT = function(m) return function(n) return bAnd(uIsZero(uSub(m)(n)))(bNot(uIsZero(uSub(n)(m)))) end end
local uGE = function(m) return function(n) return bNot(bAnd(uIsZero(uSub(m)(n)))(bNot(uIsZero(uSub(n)(m))))) end end
local uLE = function(m) return function(n) return bNot(bAnd(uIsZero(uSub(n)(m)))(bNot(uIsZero(uSub(m)(n))))) end end
local uSigned = function(n) return function(sgn) return sgn(n)(function(f) return function(x) return x end end) end end
local uMin = function(x) return function(y) return uGT(x)(y)(y)(x) end end
local uMax = function(x) return function(y) return uGT(x)(y)(x)(y) end end
local iSucc = function(n) return n(function(a) return function(b) return function(sgn) return sgn(uSucc(a))(b) end end end) end
local iPred = function(n) return n(function(a) return function(b) return function(sgn) return sgn(a)(uSucc(b)) end end end) end
local iAdd = function(m) return function(n) return m(function(ma) return function(mb) return n(function(na) return function(nb) return function(sgn) return sgn(uAdd(ma)(na))(uAdd(mb)(nb)) end end end) end end) end end
local iNeg = function(n) return n(function(a) return function(b) return function(sgn) return sgn(b)(a) end end end) end
local iNormalize = function(n) return n(function(a) return function(b) return function(sgn) return sgn(uSub(a)(b))(uSub(b)(a)) end end end) end
local iSub = function(m) return function(n) return iAdd(m)(iNeg(n)) end end
local iMul = function(m) return function(n) return m(function(ma) return function(mb) return n(function(na) return function(nb) return uIsZero(ma)(function(sgn) return sgn(uMul(mb)(nb))(uMul(mb)(na)) end)(function(sgn) return sgn(uMul(ma)(na))(uMul(ma)(nb)) end) end end) end end) end end
local iIsZero = function(n) return iNormalize(n)(function(a) return function(b) return bAnd(uIsZero(a))(uIsZero(b)) end end) end
local iIsNeg = function(n) return n(function(a) return function(b) return uGT(b)(a) end end) end
local iIsPos = function(n) return n(function(a) return function(b) return uGT(a)(b) end end) end
local iGT = function(m) return function(n) return iIsNeg(iSub(n)(m)) end end
local iLT = function(m) return function(n) return iIsNeg(iSub(m)(n)) end end
local iGE = function(m) return function(n) return bNot(iLT(m)(n)) end end
local iLE = function(m) return function(n) return bNot(iGT(m)(n)) end end
local iEQ = function(m) return function(n) return iNormalize(m)(function(ma) return function(mb) return iNormalize(n)(function(na) return function(nb) return bAnd(uEQ(ma)(na))(uEQ(mb)(nb)) end end) end end) end end
local iNEQ = function(m) return function(n) return bNot(iEQ(m)(n)) end end
local iUnsigned = function(n) return iNormalize(n)(function(a) return function(b) return a end end) end
local iMin = function(x) return function(y) return iGT(x)(y)(y)(x) end end
local iMax = function(x) return function(y) return iGT(x)(y)(x)(y) end end
local tPair = function(a) return function(b) return function(tpl) return tpl(a)(b) end end end
local tFirst = function(a) return function(b) return a end end
local tSecond = function(a) return function(b) return b end end
local tSetFirst = function(t) return function(v) return tPair(v)(t(tSecond)) end end
local tSetSecond = function(t) return function(v) return tPair(t(tFirst))(v) end end
local tVararg = function(i) return function(fn) return i(function(nx) return function(st) return function(e) return nx(function(f) return function(l) return st(f)(f(e)(l)) end end) end end end)(fn)(function(f) return function(l) return l end end) end end
local tTuple = function(i) return tVararg(i)(function(v) return function(tpl) return v(function(e) return function(nx) return function(st) return nx(st(e)) end end end)(function(st) return st end)(tpl) end end) end
local tGet = function(i) return function(p) return tVararg(i)(function(v) return v(function(e) return function(nx) return function(idx) return uIsZero(idx)(e)(nx(uPred(idx))) end end end)(function(idx) return function(x) return x end end)(p) end) end end
local tSet = function(i) return function(t) return function(n) return function(sv) return t(tVararg(i)(function(v) return function(tpl) return v(function(e) return function(nx) return function(idx) return function(st) return nx(uSucc(idx))(st(uEQ(idx)(n)(sv)(e))) end end end end)(function(idx) return function(st) return st end end)(function(f) return function(x) return x end end)(tpl) end end)) end end end end
local tVector = function(i) return function(t) return t(tVararg(i)(function(v) return v end)) end end
local vMap = function(s) return function(fn) return function(f) return function(l) return s(function(nm) return function(nx) return f(fn(nm))(nx) end end)(l) end end end end
local vFold = function(v) return function(iv) return function(fn) return v(function(e) return function(nx) return function(st) return nx(fn(st)(e)) end end end)(function(st) return st end)(iv) end end end
local vFoldMap = function(v) return function(iv) return function(fn) return function(f) return function(l) return v(function(e) return function(nx) return function(st) return fn(st)(e)(function(nst) return function(ne) return f(ne)(nx(nst)) end end) end end end)(function(st) return l end)(iv) end end end end end
local vConcat = function(a) return function(b) return function(f) return function(l) return a(f)(b(f)(l)) end end end end
local vExpand = function(v) return function(l) return vFold(v)(function(f) return function(l) return l end end)(function(st) return function(e) return vConcat(st)(l(e)) end end) end end
local vInsertEnd = function(v) return function(e) return function(f) return function(l) return v(f)(f(e)(l)) end end end end
local vInsertStart = function(v) return function(e) return function(f) return function(l) return f(e)(v(f)(l)) end end end end
local vIndexify = function(v) return vFoldMap(v)(function(f) return function(x) return x end end)(function(st) return function(e) return tPair(uSucc(st))(tPair(st)(e)) end end) end
local vReverse = function(v) return v(function(e) return function(nx) return vInsertEnd(nx)(e) end end)(function(f) return function(l) return l end end) end
local vLength = function(v) return function(f) return function(x) return v(function(e) return function(nx) return f(nx) end end)(x) end end end
local vFirstWhere = function(v) return function(l) return function(d) return function(fn) return v(function(e) return function(nx) return l(e)(fn(e))(nx) end end)(d) end end end end
local vTake = function(v) return function(i) return v(function(e) return function(nx) return function(idx) return uIsZero(idx)(function(f) return function(l) return l end end)(vInsertStart(nx(uPred(idx)))(e)) end end end)(function(idx) return function(f) return function(l) return l end end end)(i) end end
local vSkip = function(v) return function(i) return v(function(e) return function(nx) return function(idx) return uIsZero(idx)(vInsertStart(nx(uPred(idx)))(e))(nx(uPred(idx))) end end end)(function(idx) return function(f) return function(l) return l end end end)(i) end end
local vGet = function(v) return function(i) return function(d) return function(fn) return v(function(e) return function(nx) return function(idx) return uIsZero(idx)(fn(e))(nx(uPred(idx))) end end end)(function(idx) return d end)(i) end end end end
local vWhere = function(v) return function(fn) return function(f) return function(l) return v(function(e) return function(nx) return fn(e)(f(e)(nx))(nx) end end)(l) end end end end
local vSet = function(v) return function(i) return function(d) return vMap(vIndexify(v))(function(e) return uEQ(e(tFirst))(i)(d)(e(tSecond)) end) end end end
local vGenerate = function(j) return function(e) return function(f) return function(l) return j(function(nx) return f(e)(nx) end)(l) end end end end
local vGenerateRange = function(i) return function(j) return function(f) return function(l) return uSub(j)(i)(function(nx) return function(st) return f(uAdd(st)(i))(nx(uSucc(st))) end end)(function(st) return l end)(function(f) return function(x) return x end end) end end end end
local vIsEmpty = function(v) return v(function(e) return function(nx) return _false end end)(_true) end
local vRemoveLast = function(v) return function(f) return function(l) return v(function(e) return function(nx) return function(tpl) return tpl(nx(tSecond)(function(e) return e end)(f(e))(nx(tFirst)))(_false) end end end)(function(tpl) return tpl(l)(_true) end)(tFirst) end end end
local vRemoveFirst = function(v) return vSkip(v)(function(f) return function(x) return f(x) end end) end
local vRemoveWhere = function(v) return function(fn) return function(f) return function(l) return v(function(e) return function(nx) return fn(e)(nx)(f(e)(nx)) end end)(l) end end end end
local vSlice = function(v) return function(start) return function(_end) return vTake(vSkip(v)(start))(uSub(_end)(start)) end end end
local vFirst = function(v) return function(d) return function(fn) return v(function(e) return function(nx) return fn(e) end end)(d) end end end
local vLast = function(v) return function(d) return function(fn) return v(function(e) return function(nx) return nx(tFirst)(function(tpl) return tpl(_false)(e) end)(nx) end end)(function(tpl) return tpl(_true)(d) end)(tSecond) end end end
local vEQ = function(cmp) return function(a) return function(b) return bAnd(uEQ(vLength(a))(vLength(b)))(vFold(a)(function(tpl) return tpl(_true)(b) end)(function(st) return function(e) return function(tpl) return tpl(bAnd(st(tFirst))(cmp(e)(vFirst(st(tSecond))(function(f) return function(x) return x end end)(null))))(vSkip(st(tSecond))(function(f) return function(x) return f(x) end end)) end end end)(tFirst)) end end end
local vTakeWhile = function(v) return function(lm) return function(f) return function(l) return v(function(e) return function(nx) return lm(e)(f(e)(nx))(l) end end)(l) end end end end
local vSkipWhile = function(v) return function(lm) return function(f) return function(l) return v(function(e) return function(nx) return function(cn) return bOr(cn)(bNot(lm(e)))(f(e)(nx(_true)))(nx(_false)) end end end)(function(cn) return l end)(_false) end end end end
local vInsertAt = function(v) return function(i) return function(e) return vConcat(vInsertEnd(vTake(v)(i))(e))(vSkip(v)(i)) end end end
local vReduce = function(v) return function(l) return function(d) return function(fn) return vFold(vSkip(v)(function(f) return function(x) return f(x) end end))(vFirst(v)(d)(null))(l) end end end end
local vRemoveAt = function(v) return function(i) return vExpand(vIndexify(v))(function(t) return uEQ(t(tFirst))(i)(function(f) return function(l) return l end end)(function(f) return function(l) return f(t(tSecond))(l) end end) end) end end
local vAny = function(v) return function(l) return v(function(e) return function(nx) return l(e)(_true)(nx) end end)(_false) end end
local sUnsigned = function(s) return vFold(s)(function(f) return function(x) return x end end)(function(st) return function(e) return uAdd(uMul(st)(function(f) return function(x) return f(f(f(f(f(f(f(f(f(f(x)))))))))) end end))(uSub(e)(function(f) return function(x) return f(f(f(f(f(f(f(f(f(f(f(f(f(f(f(f(f(f(f(f(f(f(f(f(f(f(f(f(f(f(f(f(f(f(f(f(f(f(f(f(f(f(f(f(f(f(f(f(x)))))))))))))))))))))))))))))))))))))))))))))))) end end)) end end) end
local sSigned = function(s) return (function(o) return vSkip(s)(function(f) return function(x) return f(x) end end)(function(f) return function(x) return x end end)(function(st) return function(e) return uAdd(uMul(st)(function(f) return function(x) return f(f(f(f(f(f(f(f(f(f(x)))))))))) end end))(uSub(e)(function(f) return function(x) return f(f(f(f(f(f(f(f(f(f(f(f(f(f(f(f(f(f(f(f(f(f(f(f(f(f(f(f(f(f(f(f(f(f(f(f(f(f(f(f(f(f(f(f(f(f(f(f(x)))))))))))))))))))))))))))))))))))))))))))))))) end end)(uEQ)(vFirst(s))(function(f) return function(x) return f(f(f(f(f(f(f(f(f(f(f(f(f(f(f(f(f(f(f(f(f(f(f(f(f(f(f(f(f(f(f(f(f(f(f(f(f(f(f(f(f(f(f(x))))))))))))))))))))))))))))))))))))))))))) end end)(function(sgn) return sgn(o)(function(f) return function(x) return x end end) end)(function(sgn) return sgn(function(f) return function(x) return x end end)(o) end)) end end) end)(vFold) end
local mSet = function(cmp) return function(m) return function(k) return function(ne) return function(f) return function(l) return m(function(e) return function(nx) return function(st) return cmp(e(tFirst))(k)(f(function(tpl) return tpl(k)(ne) end)(nx(_true)))(f(e)(nx(st))) end end end)(function(st) return st(l)(f(function(tpl) return tpl(k)(ne) end)(l)) end)(_false) end end end end end end
local mAddFromVec = function(cmp) return function(ma) return function(vec) return function(k) return function(v) return vFold(vec)(ma)(function(st) return function(e) return mSet(cmp)(st)(k(e))(v(e)) end end) end end end end end
local mFromVec = function(cmp) return function(vec) return function(k) return function(v) return mAddFromVec(cmp)(function(f) return function(l) return l end end)(vec)(k)(v) end end end end
local mKeys = function(m) return vMap(m)(function(e) return e(tFirst) end) end
local mValues = function(m) return vMap(m)(function(e) return e(tSecond) end) end
local mGet = function(cmp) return function(m) return function(k) return function(d) return function(fn) return vFirstWhere(m)(function(e) return cmp(k)(e(tFirst)) end)(d)(function(e) return fn(e(tSecond)) end) end end end end end
local mExists = function(cmp) return function(m) return function(k) return vAny(m)(function(e) return cmp(k)(e(tFirst)) end) end end end
local mContains = function(cmp) return function(m) return function(k) return vAny(m)(function(e) return cmp(k)(e(tSecond)) end) end end end
local mAddAll = function(cmp) return function(ma) return function(mb) return vFold(mb)(ma)(function(st) return function(e) return mSet(cmp)(st)(e(tFirst))(e(tSecond)) end end) end end end
local mRemove = function(cmp) return function(m) return function(k) return vRemoveWhere(m)(function(e) return cmp(k)(e(tFirst)) end) end end end
local mPutIfAbsent = function(cmp) return function(m) return function(k) return function(e) return function(f) return function(l) return m(function(e) return function(nx) return function(st) return cmp(e(tFirst))(k)(f(e)(nx(_true)))(f)(e)(nx(st)) end end end)(function(st) return st(l)(f(function(tpl) return tpl(k)(e) end)) end)(_false) end end end end end end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment