No confusion over no_confusion
I’ve always been a bit confused about no_confusion. Functions with names like this are occasionally applied in Theorem Proving In Lean but with little explanation (for example in section 10.1 of TPIL they use it twice to prove 2 ≠ 1) [note added Oct 2018 : this is no longer true; it was the version of TPIL for Lean 3.3.0 which did this]. Of course you can always try and find out your own explanations, but in the past I’ve struggled trying to get to the bottom of no_confusion_type with no docs and no hints as to what its purpose is. But Chris Hughes and I sat down last night and tried to figure out what was going on, and I think we cracked it. So here is a hopefully unconfused explanation of at least some of the aspects of no_confusion. Before I start on it though, here are some facts about inductive types.
Free gifts with every inductive type.
I found the explanation of inductive types in TPIL very helpful. But let me flag a few things which didn’t sink in the first time.
Let’s create a really simple inductive type — an “enumerated type with two constructors” — Lean’s way of creating what a mathematician would call a set with two elements.
inductive mytype
| AA : mytype
| ZZ : mytype [try it here — lean web editor]
What actually happens when this code is processed? Lean adds a new constant mytype to the environment, which is a fancy way of saying that it now makes sense to talk about things of type mytype. It also adds some other stuff. It adds the constructors mytype.AA and mytype.ZZ — these are the two ways of creating new things of type mytype. And it adds the eliminator mytype.rec. Let’s take a closer look at this eliminator. The output of #check @mytype.rec is
mytype.rec : Π {C : mytype → Sort u_1}, C mytype.AA → C mytype.ZZ → Π (n : mytype), C nwhich reads as follows: “Say we have a function C from mytype to somewhere. If we know C AA and we know C ZZ, then for all n of type mytype, we know C n.”
The point that I want to emphasize here is that these things — the new type, its constructors and eliminator — are added into the environment as axioms, or constants or whatever you want to call them. You can’t “look at the proof” of mytype.rec in Lean — it just is. If you try #print mytype.rec you’re just informed that mytype.rec is an eliminator. Take a look at the output, as well as the output of #print mytype and #print mytype.AA to see more examples of new things which have just appeared in your Lean environment.
However there is a whole bunch of other stuff which has also just appeared in the environment. If you type #print prefix mytype you will see, as well as the constructors and the eliminator, about 14 other new functions which have magically appeared, including the dreaded (or formerly dreaded, in my case) mytype.no_confusion, which appears to be a function whose output is of type mytype.no_confusion_type. One thing which perhaps needs to be stressed here though, is that these 14 other functions are not just magic new axioms like the constructors and eliminator — they have actually been proved or constructed using our new axioms. You can take a look at the proof of mytype.no_confusion with
#print mytype.no_confusion. Doing this might well leave you confused, and it’s the purpose of this post to try and unconfuse you.
What does no_confusion do?
The idea behind inductive types is that two instances of an inductive type should be equal if and only if they were ultimately constructed in the same way. What does this actually mean? The constructors and eliminator do not seem to say this explicitly, so what is going on? Certainly (at least in constructive logic) every expression whose type is mytype will, at the end of the day, either equal AA or ZZ. But can we prove that AA ≠ ZZ? This sounds like the sort of thing which should somehow be an axiom, or part of the system in some way. But we’ve just seen how the system has changed when we created our new type — we have mytype and AA and ZZ and the eliminator, and nothing else. If we want to prove AA ≠ ZZ then we are going to have to do it from these axioms — we are going to have to prove it by induction somehow.
Here is quite a crazy proof that AA ≠ ZZ. I do know a simpler one, but the simpler proof doesn’t generalise, and this slightly crazy proof I present here will turn out to be an instructive one (or, at least, I found it instructive).
First let’s define a function which takes as input two expressions s and t of type mytype, and outputs a Proposition. The Proposition will be true if s and t are equal, and false otherwise. We can easily do this, using the cases tactic, which is in this setting just the tactic version of mytype.rec. Here’s the function.
def mytype_equal : mytype → mytype → Prop :=
begin
intros s t,
cases s,
{ -- s = AA
cases t,
{ -- s = AA, t = AA
exact true },
{ -- s = AA, t = ZZ
exact false }
},
{ -- s = ZZ
cases t,
{ -- s = ZZ, t = AA
exact false },
{ -- s = ZZ, t = ZZ
exact true }
}
endOne could instead use the equation compiler:
open mytype -- so I can just type AA instead of mytype.AA
definition mytype_equal' : mytype → mytype → Prop
| AA AA := true
| AA ZZ := false
| ZZ AA := false
| ZZ ZZ := trueor one could even write the same code as a function, without using tactic mode, and check that it equals the tactic version:
definition mytype_equal'' : mytype → mytype → Prop :=
λ s t, mytype.rec (mytype.rec true false t) (mytype.rec false true t) s
example : mytype_equal = mytype_equal'' := rfl Now here’s the trick! Let’s write a function which takes two terms s and t of type mytype, plus a proof that s = t, and returns a proof of mytype_equal s t. This sounds feasible — after all, if s = t then we can just rewrite mytype_equal s t into mytype_equal t t which will always evaluate to true, and recall that trivial is a proof of true. So the code looks something like this:
definition mytype_no_confusion (s : mytype) (t : mytype) : s = t → mytype_equal s t :=
begin
intro H, -- H : s = t
rw H, -- goal now mytype_equal t t
cases t, -- goal 1 now my_type_equal AA AA
{ trivial },
-- goal 2 now my_type_equal ZZ ZZ
{ trivial }
end If we run this code with s and t different elements of mytype, then we have proved s = t → false, which is exactly s ≠ t!
open mytype -- if you didn't already
theorem unconfused : AA ≠ ZZ := mytype_no_confusion AA ZZ That’s how no_confusion works in this simplest case.
The other thing no_confusion does
This is not quite the end of the story, however. For enumerated types this is all we need, but what about types like the natural numbers? Let’s roll our own and see what else we need to do to give some rigour to the idea that “two natural numbers are the same if and only if they are constructed in the same way”.
inductive xnat
| zero : xnat
| succ : xnat → xnat The reason we think of these as the natural numbers is that it’s not hard to convince oneself that the only ways to build instances of this type are zero, succ zero, succ (succ zero), succ (succ (succ zero))) and so on. Again, for these to be a good model of the natural numbers it’s crucial that all of these things are distinct.
Exercise. Mimic the mytype proof and write code for xnat_equal, xnat_no_confusion and then prove ∀ n : xnat, zero ≠ succ n.
But there’s something else. We also need to prove ∀ m n : xnat, succ m = succ n → m = n and our current set-up doesn’t let us do this. The problem is that xnat_equal (succ m) (succ n) is true and we’ve lost any information about m and n by this point. You may have noticed when writing xnat_equal that we threw away s and t when defining xnat_equal (succ s) (succ t). This was our error — we need instead to return a Prop which depends on s and t. So how about this?
def xnat_no_confusion_prop (s t : xnat) : Prop :=
begin
cases s with m,
cases t with n,
exact true, -- s = t = zero
exact false, -- s = zero, t = succ n
cases t with n,
exact false, -- s = succ m, t = zero
exact (m = n), -- new idea when s = succ m, t = succ n!
end Now we can still prove
def xnat_no_confusion' (s t : xnat) (H : s = t) : xnat_no_confusion_prop s t :=
begin
rw H,
cases t with n,
exact trivial, -- t = 0
show (n=n), -- t = succ n
refl,
end but this time we have enough left in the tank to get the injectivity of succ:
example (m n : xnat) : succ m = succ n → m = n := λ H, xnat_no_confusion' _ _ H (this is just term mode for intro H, exact xnat_no_confusion' _ _ H.)
The whole truth
We used Props like m = n to pull off this trick. As you can imagine, for more complicated situations we might need some more general types. In fact the actual no_confusion_type and no_confusion functions built by Lean when the type is initialised look like the below. They take as input an arbitrary Type P and return Types instead of the Props I used above:
variables m n : xnat
variable P : Sort*
#reduce xnat.no_confusion_type P zero zero -- P → P
#reduce xnat.no_confusion_type P (succ m) zero -- P
#reduce xnat.no_confusion_type P zero (succ n) -- P
#reduce xnat.no_confusion_type P (succ m) (succ n) -- ((m = n) → P) → PI would imagine that this gives more flexibility in some cases; at this point I do not have a clear idea of why this greater flexibility is needed.
[Finally let me comment that an easier proof of injectivity of nat.succ is just to write a one-sided inverse pred. This was the idea behind the simpler proof I was alluding to earlier. But this idea doesn’t work in general, and it’s an interesting exercise to figure out why!]