Skip to content

Instantly share code, notes, and snippets.

@md2perpe
Created June 13, 2018 18:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save md2perpe/03a9c3494a0ed41d53f60a4159ff2a60 to your computer and use it in GitHub Desktop.
Save md2perpe/03a9c3494a0ed41d53f60a4159ff2a60 to your computer and use it in GitHub Desktop.
Lambda calculus in Typescript
type _bool<X, Y> = (X, Y) => X | Y;
let _true = <X, Y>(x: X, y: Y) => x;
let _false = <X, Y>(x: X, y: Y) => y;
let _not = <X, Y>(b: _bool<Y, X>) => (x: X, y: Y) => b(y, x);
let _and = <X, Y>(b1: _bool<X | Y, Y>, b2: _bool<X, Y>) => (x: X, y: Y) => b1(b2(x, y), _false(x, y));
let _or = <X, Y>(b1: _bool<X, X | Y>, b2: _bool<X, Y>) => (x: X, y: Y) => b1(_true(x, y), b2(x, y));
// Using
// let __compose = <X, Y, U, V, W>(f: (U, V) => W, g: (X, Y) => U, h: (X, Y) => V) => (x: X, y: Y) => f(g(x, y), h(x, y));
// we can write
// let _and = <X, Y>(b1: _bool<X | Y, Y>, b2: _bool<X, Y>) => __compose(b1, b2, _false);
// let _or = <X, Y>(b1: _bool<X | Y, Y>, b2: _bool<X, Y>) => __compose(b1, _true, b2);
type _nat<X> = (f: (X) => X, x: X) => X;
let _zero = <X>(f: (X) => X, x: X) => x;
let _succ = <X>(n: _nat<X>) => (f: (X) => X, x: X) => n(f, f(x));
let _one = <X>(f: (X) => X, x: X) => _succ(_zero);
let __curry = <X, Y, Z>(f: (X, Y) => Z) => (x: X) => (y: Y) => f(x, y);
let _plus = <X>(n1: _nat<X>, n2: _nat<X>) => (f: (X) => X, x: X) => n1(f, n2(f, x));
let _times = <X>(n1: _nat<X>, n2: _nat<X>) => (f: (X) => X, x: X) => n1(__curry(n2)(f), x);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment