Commissioned for this museum · after Peano's axioms, by way of variadic tuple types

Arithmetic with No Numbers

TypeScript·2020·34 lines·1536 bytes

Curator’s note

Nothing in this file runs. Every sum in it is worked out before the program exists at all, by the type checker, as a side effect of deciding whether the file is well typed. Compile it and the answers are already known; run it and nothing happens.

It works because TypeScript will tell you the length of a tuple type, and will let you build a tuple by spreading other tuples into it. That is enough to count. Tally<3> is a tuple with three members in it, built by adding one at a time until the length matches — recursion in the type system, with the base case written as a condition. Once you can count, addition is laying two tallies end to end and asking how long the result is. Subtraction is pattern matching: take A apart and see what is left once B has been removed from the end. Multiplication is addition done B times.

None of this was designed. Conditional types were added so a library could say "this function returns whatever you passed it"; variadic tuples were added so that wrapping a function would not lose the shape of its arguments. Put together they turn out to be a small, slow, purely functional programming language that nobody can run on purpose.

The last line is a lie the checker refuses — 3 + 4 is not 8 — which is there because a proof that cannot fail is not a proof.

See also what a type system can and cannot prove.