The inverse of a bijection.
In my “introduction to proof” course I teach the undergraduates about equivalence relations. What the students do not know, is that earlier on in the course I have secretly inserted examples of equivalence relations without ever letting on. For example, in the number theory section I prove that that if
is a fixed positive integer, and
are integers, then


and 
And when we do equivalence relations I can then say “oh look! Congruence mod
is an equivalence relation!”
I do the same trick with bijections. I prove that the identity function is a bijection, the inverse of a bijection is a bijection, and that the composite of two bijections is a bijection. Then later on I can say “oh look! ‘I biject with you’ is an equivalence relation!”
Coding up congruences mod
in Lean is a joy to do, and one of my example files on this stuff even made it into mathlib!
But coding up the bijection equivalence relation into mathlib, one runs into an issue which is hard to explain to mathematicians. The problem is with symmetry — proving that the inverse of a bijection is a bijection. Say
is a bijection, and
. We’re trying to define the inverse function to
and we want to figure out its value on
. Lean has no trouble proving that there exists a unique element of
such that applying
to it gives
. But when you try to access this element of
while defining the inverse of
, you get an intimidating error:
induction tactic failed, recursor 'Exists.dcases_on'
can only eliminate into PropNow Lean 3 does not have particularly friendly error messages, but as a Lean beginner I remember finding this one completely baffling. As far as type theory is concerned, there is a universe issue; the user just tried to define a function to Type but the only function Lean could find was one into Prop. A mathematician using Lean might know about Lean’s two kinds of universes — Prop is the universe of true-false statements, where things like the statement of Fermat’s Last Theorem lives, and Type is the universe of data, where things like the real numbers live. So what is going on? The problem is that
is in Prop, but the actual
which exists is in Type, and Lean is complaining that it does not have an algorithm to get hold of
, and hence to move from the weak Prop universe to the stronger Type one.
Now of course to a mathematician this is just all ridiculous — the algorithm is “there is a unique
— take that one”. The Lean beginner will learn that instead of applying the cases tactic to ∃ (x : X), f x = y to get
, they can apply the choose tactic, and then one gets a new error
definition 'g' is noncomputable, it depends on 'classical.some'which one fixes by writing noncomputable theory at the top of the file. One is left with a feeling that Lean is kind of bewildering, and why was there so much fuss about applying the obvious algorithm “take the unique
, duh”?
Recently on the Lean Zulip chat there has been talk amongst computationally-minded people about fancy things such as domain theory, topologies, the difference between option and roption, and difference lists. All of this has been slowly sinking into my head in some sort of way, and it culminated this morning in my actually seeing an explicit example (needs Zulip login) of a bijection between two sets where even a mathematician can see that there is some sort of issue with defining the inverse bijection. Thanks to Mario Carneiro for coming up with the example. The rest of this post explains it.
The example.
The sets in question are as follows. Let
. That’s easy enough.
Let
be the set of infinite sequences
where each 
T,F
(true or false), and with the property that for all
we have
. In words — if at some point in the sequence we see
, then everything after that point is also
.
Examples of elements of
:
FFFFTTTTTTTTTTTTTTT...
FFFFFFFFFTTTTTTTTTT...
FFFFFFFFFFFFFFFFFFF...
TTTTTTTTTTTTTTTTTTT...It is not hard to convince yourself that sequences in
come in two flavours: either the sequence has some finite number
of Fs and then everything afterwards is T, or the sequence can be all Fs.
The bijection
sends
to the “all F” sequence, and
to the sequence which contains
Fs and then reverts to T forevermore. This is easily checked to be a bijection, and one even feels that it’s possible to write down the inverse. But how easy is it in practice?
Here’s the issue. Say I now give you a “black box” sequence
in
. It’s a function, I can guarantee that it’s in
(I even have a proof!), and if you give me any
I will return
. We know that there is a unique
with
and it’s now your job to find it. What is your algorithm?
Initially you can start off by evaluating the function at some values. It turns out that
is F, that
is F and then that
is F. You realise that the
you’re looking for is either a very large integer, or
. But which one? You have what looks like complete access to
, but you cannot work out which
it corresponds to — indeed you cannot even say whether
or not!
After a while, you get sick of this and say that actually you haven’t got complete access to
and I am suppressing information. You complain that you don’t just want to evaluate
at lots of numbers, you want to see the actual definition. So I reveal the definition:
is defined to be T if there is a counterexample to the Goldbach conjecture which is at most
, and F otherwise. To put it another way — if the Goldbach conjecture is true then the sequence is all Fs and
, but if it fails at some point then
is the positive integer where it fails. I even show you the one-line proof that
is an element of
: if there’s a counterexample which is at most
then clearly there’s a counterexample which is at most
. Even with the definition, you are none the wiser!
Conclusion.
When a function is marked as noncomputable in Lean it means that Lean has not been told a method for working it out for every input. The innocuous-looking statement “there exists a unique
with
, so choose that
” is clearly something which can be done abstractly, but there is no method for finding
, even if we can prove that it must exist. There can’t be a method for finding it in general — any such method would translate into a method for proving Goldbach’s conjecture, the 3n+1 problem, Fermat’s Last Theorem, the twin primes conjecture and so on; these are hard problems. Indeed, one can prove using a diagonalisation argument that no algorithm can exist to invert this bijection. The inverse of our computable function
certainly exists, but it is a noncomputable function. Lean can handle such functions, no problem, but it has to be told explicitly that it should be ignoring computability issues (e.g. with the line noncomputable theory at the top of a file).