Consider the following development, an isolated part of Adam Chlipala's simplHyp
:
(** Fail if H is in context *)
Ltac notInCtx H := assert H; [ assumption | fail 1 ] || idtac.
Ltac injectionInCtx :=
match goal with
(* Is matching on G strictly necessary? *)
| [ H : ?F ?X = ?F ?Y |- ?G ] =>
(* fail early if it wouldn't progress *)
notInCtx (X = Y);
injection H;
match goal with
(* G is used here *)
| [ |- X = Y -> G ] =>
try clear H; intros; try subst
end
end.
Goal forall (x y : nat), S x = S y -> x = y.
intros x y H.
injectionInCtx.
exact eq_refl.
Qed.
See the comments inline - G
is matched at the outset, and eventually used to verify that the end goal remains the same. Is this to preclude the possibility that injection H
might modify the goal or add extraneous assumptions?