0

I am trying to prove the following simple theorem over natural numbers:

((i + j) = (i + k)) -> (j = k)

Here is what I have in Coq:

Theorem cancel : forall (i j k : nat),
  ((add i j) = (add i k)) -> (j = k).
Proof.
intros i j k.
induction i.
simpl.
apply A_IMPLIES_A.
simpl.

And after that I have the sub-goal:

S (add i j) = S (add i k) -> j = k

So I thought I'd apply eq_add_S which states that S m = S n -> m = n. However, when I try to do so with apply eq_add_S I get the following error:

Error:
In environment
i, j, k : nat
IHi : add i j = add i k -> j = k
Unable to unify "k" with "add i k".

So I guess it can't understand that I want is m = (add i j) and n = (add i k). How come Coq can't read my mind? or more seriously, how can I help him do so? thanks!

OrenIshShalom
  • 5,974
  • 9
  • 37
  • 87

2 Answers2

2

The problem is not that Coq can't guess what value to use for m and n, but that your goal does not have the right shape for you to instantiate that theorem. When you write apply eq_add_S, Coq tries to unify S n = S m -> n = m with S (add i j) = S (add i k) -> j = k, which cannot be done.

What you need is to apply eq_add_S to the goal's premise, by introducing it into the context.

Proof.
intros i j k H. (* H : add i j = add i k *)
induction i as [|i IH].
- apply H.
- apply eq_add_S in H.
  (* ...  *)
Arthur Azevedo De Amorim
  • 23,012
  • 3
  • 33
  • 39
  • This is probably a trivial question, but where is H defined? I mean I see it in the comment, but how can I actually define it? – OrenIshShalom Jan 21 '19 at 13:22
  • In Coq jargon, the `intros ... H` tactic _introduces_ the `add i j = add i k` premise from the goal into the context, giving it the name `H`. – Arthur Azevedo De Amorim Jan 21 '19 at 16:38
  • I'm a bit confused, I thought H should be: `S (add i j) = S (add i k) -> (add i j) = (add i k)` no? any chance you can fill in the missing details? It seems that simple Coq examples are pretty hard to find online ... thanks! – OrenIshShalom Jan 21 '19 at 16:53
  • No, what you wrote is the particular instance of the `eq_add_S` lemma that you need to complete the proof. It is what Coq produces after executing `apply eq_add_S in H.`. I think the easiest thing for you would be to go over the Software Foundations book (https://softwarefoundations.cis.upenn.edu/). It is long, but filled with examples, including simple and complex. – Arthur Azevedo De Amorim Jan 21 '19 at 17:03
0

I'm posting the solution as a separate answer hoping other users can benefit from it. Here it is:

Theorem cancel : forall (i j k : nat),
  ((add i j) = (add i k)) -> (j = k).
Proof.
intros i j k H.
induction i.
apply H.
simpl in H.
apply eq_add_S in H.
apply IHi in H.
assumption.
Qed.
OrenIshShalom
  • 5,974
  • 9
  • 37
  • 87