Library coq_nat
Natural Numbers in the Coq Standard Library
Print nat.
Print plus.
Print plus.
Moreover Coq introduces notations for natural numbers allowing
to write 0 for zero (instead of the letter O), + for
plus, with the infix notation, and to use decimal numbers
instead of the unary notation with S.
In the previous exercise we proved that 0 + n = n and a very
simple proof it was. Now let us try to prove n + 0 = n. This
is slightly more difficult. Can you see why? How would you prove
that?
You can also do
elim n
which is is the primitive tactic, but then you have to do additional intros. You can also do
destruct n
but that doesn't give you an induction hypothesis, only case distinction
elim n
which is is the primitive tactic, but then you have to do additional intros. You can also do
destruct n
but that doesn't give you an induction hypothesis, only case distinction
- simpl.
reflexivity.
- simpl.
rewrite -> IHn.
reflexivity.
Qed.
reflexivity.
- simpl.
rewrite -> IHn.
reflexivity.
Qed.
Let us prove a simple property concerning addition and successor
Lemma plus_succ : forall m n, S (m + n) = m + S n.
Proof.
intros.
induction m.
- simpl.
reflexivity.
- simpl.
rewrite IHm.
reflexivity.
Qed.
Let us prove that addition is commutative.
For this we may need the facts that 0 is a neutral element for
addition and that S (n + m) = n + S m - the facts that we proved
before. However, those results are also available in the standard
library. First we need to make them available with the
Require Import command.
Prove the following Lemma using plus_n_O, plus_O_n and plus_n_Sm
Let us prove that addition is associative
Which argument should we do induction over?
Which argument should we do induction over?
So far we have been dealing with addition on natural numbers. How
about multiplication? Not surprisingly it is defined in the
standard library of Coq, but before seeing the definition
try to think of how would you define that (using addition).
Print mult.
Here is this definition for reference (and in the way you would
type it in, which slightly differs from the way Coq presents it)
Fixpoint mult (n m:nat) {struct n} : nat :=
match n with
| O => 0
| S p => m + mult p m
end
Let us prove the 0 properties of multiplication
Lemma mult_0_l : forall n, 0 * n = 0.
Proof.
Admitted.
Lemma mult_0_r : forall n, n * 0 = 0.
Proof.
Admitted.
Now, let us prove that '1' is neutral for multiplication
Remember the properties of addition we were using. They may
come in handy in this exercise. To get access to many results
concerning arithmetic of natural number (including properties
of addition and multiplication) we have already loaded the library
Arith
Lemma mult_1_l : forall n, 1 * n = n.
Proof.
Admitted.
Lemma mult_1_r : forall n, n * 1 = n.
Proof.
Admitted.
Now, let us prove that multiplication is commutative
For this we may need to use some properties of addition and multiplication
that we proved before.
... and a following auxiliary lemma
Lemma mult_succ : forall m n, n + n * m = n * S m.
Proof.
Admitted.
Lemma mult_comm : forall m n, m * n = n * m.
Proof.
Admitted.
Multiplication distributes over addition