With Pierre Boutillier, we have been working on a new Coq tactic lately, called `invert`. From my point of view, it started as a quest to build a replacement to the `inversion` tactic. `inversion` is a pain to use, as it generates sub-goals with many (dependent) equalities that must be substituted, which force the use of `subst`, which in turns also has its quirks, making the mantra `inversion H; clear H; subst` quite fragile. Furthermore, `inversion` has efficiency problems, being quite slow and generating big proof terms. From Pierre’s point of view, this work was a good way to implement a better `destruct` tactic, based on what he did during an internship (report in French (PDF)).

In a nutshell, the idea behind a destruction and an inversion is quite similar: it boils down to a case analysis over a given hypothesis. And there are quite a few tactics that follow this scheme: `elim`, `case`, `destruct`, `inversion`, `dependent destruction`, `injection` and `discriminate` (it is true that the last two tactics are quite specialized, but fit the bill nevertheless). Why on Earth would we need to add a new element to this list?

Well, it turns out that building on ideas by Jean-Francois Monin to make so called “small inversions”, one can unify the inner-working of most of the aforementioned list: it suffices to build the right return clause for the case analysis.

Let’s take an example.

``````Variable A : Type.
Inductive vector: nat -> Type :=
| nil : vector 0
| cons: forall n (h:A) (v: vector n), vector (S n).

Inductive P : forall n, vector n -> Prop :=
| Pnil : P 0 nil
| Pcons: forall n h v, P n v -> P (S n) (cons n h v).

Lemma test n h v (H: P (S n) (cons n h v)) : P n v.
Proof.``````
``````H2 : P n v0
H0 : n0 = n
H1 : h0 = h
H3 : existT (fun n : nat => vector n) n v0 =
existT (fun n : nat => vector n) n v
============================
P n v``````

1, equivalent to Streicher’s axiom K. (Just to keep tabs, note that running the `Show Proof` command in Coq outputs a partial proof term that is already 73 lines long at this point.)

If we use `dependent destruction H` instead of inversion, we get the expected hypothesis `H: P n v` (which is far better from an usability point of view). Yet, there is no magic here: dependent destruction simply used a dependent equality axiom internally to get rid of the dependent equality, and generates a 64 lines long proof term that is not very pretty.

At this point, one may wonder: what should the proof term look like? and, is it necessary to use the K axiom here?

A black belt Coq user versed in dependent types could write the following one.

``````let diag :=
fun n0 : nat =>
match n0 as n' return (forall v0 : vector n', P n' v0 -> Prop) with
| 0 => fun (v0 : vector 0) (_ : P 0 v0) => True
| S m =>
fun v0 : vector (S m) =>
match v0 as v1 in (vector m0) return (P m0 v1 -> Prop) with
| nil => fun _ : P 0 nil => True
| cons p x v1 => fun _ : P (S p) (cons p x v1) => P p v1
end
end in
match H as H' in (P x y) return (diag x y H') with
| Pnil => I
| Pcons n0 h0 v0 Pv => Pv
end.``````

Then, what is `diag`? Well, it is a function that follows the structure of the arguments of `P` to single out impossible cases, and to refine the context in the other ones using dependent pattern matching, in order to reduce to the right type (the type of the initial conclusion of the goal). The idea behind such “small-scale inversions” was described by Monin in 2010 and is out of the scope of this blog post. What is new here is that we have mechanized the construction of the `diag` functions as a Coq tactic, making this whole approach practical.

All in all, using our new tactic, we can just use the following proof script:

``invert H; tauto. ``
``````let diag :=
fun n0 : nat =>
match n0 as n1 return (forall v0 : vector n1, P n1 v0 -> Prop) with
| 0 => fun (v0 : vector 0) (_ : P 0 v0) => False -> True
| S x =>
fun v0 : vector (S x) =>
match
v0 as v1 in (vector n1)
return
(match n1 as n2 return (vector n2 -> Type) with
| 0 => fun _ : vector 0 => False -> True
| S x0 => fun v2 : vector (S x0) => P (S x0) v2 -> Prop
end v1)
with
| nil => fun H0 : False => False_rect True H0
| cons n1 h0 v1 => fun _ : P (S n1) (cons n1 h0 v1) => P n1 v1
end
end in
(fun
invert_subgoal : forall (n0 : nat) (h0 : A) (v0 : vector n0)
(H0 : P n0 v0),
diag (S n0) (cons n0 h0 v0) (Pcons n0 h0 v0 H0) =>
match H as p in (P n0 v0) return (diag n0 v0 p) with
| Pnil => fun H0 : False => False_rect True H0
| Pcons x x0 x1 x2 => invert_subgoal x x0 x1 x2
end) (fun (n0 : nat) (_ : A) (v0 : vector n0) (H0 : P n0 v0) => H0))``````

We are still working on our prototype, but we are quite confident that we got the main thing right: mechanizing the construction of the return clause. We will come back to this blog when we need beta-testers!

1.

1. See the following FAQ question (Can I prove that the second components of equal dependent pairs are equal?). You may also be interested in this other question (What is Streicher’s axiom K?). The EqdepFacts standard library module, that has the equivalence proofs between all those subtle notions. Finally, if you want to finish this proof using these axioms, you can use `Require Import Eqdep.` then the `inj_pair2` lemma. Once you’re done, `Print Assumptions test.` will let you check that you relied on an additional axiom – or `Print Assumptions inj_pair2`.↩︎

2. Moreover, since our proof terms are less cluttered, it seems less likely than recursive definitions made in “proof mode” with `invert` will fail to pass the termination check once Coq’s guard condition deals properly with such commutative cuts, another part of Pierre’s thesis work.↩︎