summaryrefslogtreecommitdiff
path: root/theories/Arith/EqNat.v
blob: 206fc0ab5d23fc61092fa6acd46acdb218177413 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
(************************************************************************)
(*  v      *   The Coq Proof Assistant  /  The Coq Development Team     *)
(* <O___,, *   INRIA - CNRS - LIX - LRI - PPS - Copyright 1999-2016     *)
(*   \VV/  **************************************************************)
(*    //   *      This file is distributed under the terms of the       *)
(*         *       GNU Lesser General Public License Version 2.1        *)
(************************************************************************)

Require Import PeanoNat.
Local Open Scope nat_scope.

(** Equality on natural numbers *)

(** * Propositional equality  *)

Fixpoint eq_nat n m : Prop :=
  match n, m with
    | O, O => True
    | O, S _ => False
    | S _, O => False
    | S n1, S m1 => eq_nat n1 m1
  end.

Theorem eq_nat_refl n : eq_nat n n.
Proof.
  induction n; simpl; auto.
Qed.
Hint Resolve eq_nat_refl: arith v62.

(** [eq] restricted to [nat] and [eq_nat] are equivalent *)

Theorem eq_nat_is_eq n m : eq_nat n m <-> n = m.
Proof.
  split.
  - revert m; induction n; destruct m; simpl; contradiction || auto.
  - intros <-; apply eq_nat_refl.
Qed.

Lemma eq_eq_nat n m : n = m -> eq_nat n m.
Proof.
 apply eq_nat_is_eq.
Qed.

Lemma eq_nat_eq n m : eq_nat n m -> n = m.
Proof.
 apply eq_nat_is_eq.
Qed.

Hint Immediate eq_eq_nat eq_nat_eq: arith v62.

Theorem eq_nat_elim :
  forall n (P:nat -> Prop), P n -> forall m, eq_nat n m -> P m.
Proof.
  intros; replace m with n; auto with arith.
Qed.

Theorem eq_nat_decide : forall n m, {eq_nat n m} + {~ eq_nat n m}.
Proof.
  induction n; destruct m; simpl.
  - left; trivial.
  - right; intro; trivial.
  - right; intro; trivial.
  - apply IHn.
Defined.


(** * Boolean equality on [nat].

   We reuse the one already defined in module [Nat].
   In scope [nat_scope], the notation "=?" can be used. *)

Notation beq_nat := Nat.eqb (compat "8.4").

Notation beq_nat_true_iff := Nat.eqb_eq (compat "8.4").
Notation beq_nat_false_iff := Nat.eqb_neq (compat "8.4").

Lemma beq_nat_refl n : true = (n =? n).
Proof.
 symmetry. apply Nat.eqb_refl.
Qed.

Lemma beq_nat_true n m : (n =? m) = true -> n=m.
Proof.
 apply Nat.eqb_eq.
Qed.

Lemma beq_nat_false n m : (n =? m) = false -> n<>m.
Proof.
 apply Nat.eqb_neq.
Qed.

(** TODO: is it really useful here to have a Defined ?
    Otherwise we could use Nat.eqb_eq *)

Definition beq_nat_eq : forall n m, true = (n =? m) -> n = m.
Proof.
  induction n; destruct m; simpl.
  - reflexivity.
  - discriminate.
  - discriminate.
  - intros H. case (IHn _ H). reflexivity.
Defined.