Skip to content

Instantly share code, notes, and snippets.

@SimonMeskens
Created November 23, 2021 21:24
Show Gist options
  • Save SimonMeskens/52560729c7c9225be037be0eb2818499 to your computer and use it in GitHub Desktop.
Save SimonMeskens/52560729c7c9225be037be0eb2818499 to your computer and use it in GitHub Desktop.
Lambda calculus in the TS type system
type Test1 = λ<Not, [True]>; // False
type Test2 = λ<And, [True, False]>; // False
type Test3 = λ<And, [True, True]>; // True
// Boolean
interface True extends Func { expression: Var<this, 0>; }
interface False extends Func { expression: Var<this, 1>; }
interface Not extends Func { expression: λ<Var<this, 0>, [False, True]> }
interface And extends Func { expression: λ<Var<this, 0>, [Var<this, 1>, Var<this, 0>]> }
// Plumbing
type Func = {
variables: Func[];
expression: unknown;
}
type Var<F extends Func, X extends number> = F["variables"][X];
type λ<Exp extends Func, Vars extends unknown[]> = (Exp & {
variables: Vars;
})["expression"];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment