A Small Proof that Fin is Injective

Posted on November 15, 2019

Here’s a puzzle: can you prove that Fin is injective? That’s the type constructor, by the way, not the type itself. Here’s the type of the proof we want:

I’m going to present a proof of this lemma that has a couple interesting features. You should try it yourself before reading on, though: it’s difficult, but great practice for understanding Agda’s type system.

First off, I should say that it’s not really a “new” proof: it’s basically Andras Kovac’s proof, with one key change. That proof, as well as this one, goes --without-K : because I actually use this proof in some work I’m doing in Cubical Agda at the moment, this was non optional. It does make things significantly harder, and disallows nice tricks like the ones used by effectfully.

Computational Inequalities

The trick we’re going to use comes courtesy of James Wood. The central idea is the following type:

This proof of inequality of Fin s is different from the usual definition, which might be something like:

Our definition is based on the decidable equality of two Fin s. It also uses the standard library’s new Dec type. Basically, we get better computation behaviour from our definition. It behaves as if it were defined like so:

The benefit of this, in contrast to _≢ᶠ′_ , is that each case becomes a definitional equality we don’t have to prove. Compare the two following proofs of congruence under suc :

The Proof

First, we will describe an “injection” for functions from Fin s to Fin s.

We’re using the negated from of injectivity here, which is usually avoided in constructive settings. It actually works a little better for us here, though. Since we’re working in the domain of Fin s, and since our proof is prop-valued, it’s almost like we’re working in classical logic.

Next, we have the workhorse of the proof, the shrink lemma:

This will give us the inductive step for the overall proof. Notice the absence of any cong s or the like: the computation behaviour of ≢ᶠ saves us on that particular front. Also we don’t have to use ⊥-elim at any point: again, because of the computation behaviour of ≢ᶠ , Agda knows that certain cases are unreachable, so we don’t even have to define them.

Next, we derive the proof that a Fin cannot inject into a smaller Fin .

That’s actually the bulk of the proof done: the rest is Lego, joining up the pieces and types. First, we give the normal definition of injectivity:

Then we convert from one to the other:

And finally we have our proof:

All in all, the proof is about 36 lines, which is pretty short for what it does.