1 (**************************************************************************)
4 (* ||A|| A project by Andrea Asperti *)
6 (* ||I|| Developers: *)
7 (* ||T|| A.Asperti, C.Sacerdoti Coen, *)
8 (* ||A|| E.Tassi, S.Zacchiroli *)
10 (* \ / This file is distributed under the terms of the *)
11 (* v GNU Lesser General Public License Version 2.1 *)
13 (**************************************************************************)
15 set "baseuri" "cic:/matita/nat/compare".
17 include "datatypes/bool.ma".
18 include "datatypes/compare.ma".
19 include "nat/orders.ma".
26 | (S q) \Rightarrow false]
30 | (S q) \Rightarrow eqb p q]].
32 theorem eqb_to_Prop: \forall n,m:nat.
34 [ true \Rightarrow n = m
35 | false \Rightarrow n \neq m].
38 (\lambda n,m:nat.match (eqb n m) with
39 [ true \Rightarrow n = m
40 | false \Rightarrow n \neq m]).
43 simplify.apply not_eq_O_S.
46 intro. apply not_eq_O_S n1.apply sym_eq.assumption.
48 generalize in match H.
50 simplify.apply eq_f.apply H1.
51 simplify.intro.apply H1.apply inj_S.assumption.
54 theorem eqb_elim : \forall n,m:nat.\forall P:bool \to Prop.
55 (n=m \to (P true)) \to (n \neq m \to (P false)) \to (P (eqb n m)).
59 [ true \Rightarrow n = m
60 | false \Rightarrow n \neq m] \to (P (eqb n m)).
61 apply Hcut.apply eqb_to_Prop.
67 theorem eqb_n_n: \forall n. eqb n n = true.
68 intro.elim n.simplify.reflexivity.
72 theorem eq_to_eqb_true: \forall n,m:nat.
73 n = m \to eqb n m = true.
74 intros.apply eqb_elim n m.
76 intros.apply False_ind.apply H1 H.
79 theorem not_eq_to_eqb_false: \forall n,m:nat.
80 \lnot (n = m) \to eqb n m = false.
81 intros.apply eqb_elim n m.
82 intros. apply False_ind.apply H H1.
92 | (S q) \Rightarrow leb p q]].
94 theorem leb_to_Prop: \forall n,m:nat.
96 [ true \Rightarrow n \leq m
97 | false \Rightarrow n \nleq m].
100 (\lambda n,m:nat.match (leb n m) with
101 [ true \Rightarrow n \leq m
102 | false \Rightarrow n \nleq m]).
103 simplify.exact le_O_n.
104 simplify.exact not_le_Sn_O.
105 intros 2.simplify.elim (leb n1 m1).
106 simplify.apply le_S_S.apply H.
107 simplify.intros.apply H.apply le_S_S_to_le.assumption.
110 theorem leb_elim: \forall n,m:nat. \forall P:bool \to Prop.
111 (n \leq m \to (P true)) \to (n \nleq m \to (P false)) \to
116 [ true \Rightarrow n \leq m
117 | false \Rightarrow n \nleq m] \to (P (leb n m)).
118 apply Hcut.apply leb_to_Prop.
124 let rec nat_compare n m: compare \def
129 | (S q) \Rightarrow LT ]
133 | (S q) \Rightarrow nat_compare p q]].
135 theorem nat_compare_n_n: \forall n:nat. nat_compare n n = EQ.
137 simplify.reflexivity.
141 theorem nat_compare_S_S: \forall n,m:nat.
142 nat_compare n m = nat_compare (S n) (S m).
143 intros.simplify.reflexivity.
146 theorem S_pred: \forall n:nat.lt O n \to eq nat n (S (pred n)).
147 intro.elim n.apply False_ind.exact not_le_Sn_O O H.
148 apply eq_f.apply pred_Sn.
151 theorem nat_compare_pred_pred:
152 \forall n,m:nat.lt O n \to lt O m \to
153 eq compare (nat_compare n m) (nat_compare (pred n) (pred m)).
155 apply lt_O_n_elim n H.
156 apply lt_O_n_elim m H1.
158 simplify.reflexivity.
161 theorem nat_compare_to_Prop: \forall n,m:nat.
162 match (nat_compare n m) with
163 [ LT \Rightarrow n < m
165 | GT \Rightarrow m < n ].
167 apply nat_elim2 (\lambda n,m.match (nat_compare n m) with
168 [ LT \Rightarrow n < m
170 | GT \Rightarrow m < n ]).
171 intro.elim n1.simplify.reflexivity.
172 simplify.apply le_S_S.apply le_O_n.
173 intro.simplify.apply le_S_S. apply le_O_n.
174 intros 2.simplify.elim (nat_compare n1 m1).
175 simplify. apply le_S_S.apply H.
176 simplify. apply eq_f. apply H.
177 simplify. apply le_S_S.apply H.
180 theorem nat_compare_n_m_m_n: \forall n,m:nat.
181 nat_compare n m = compare_invert (nat_compare m n).
183 apply nat_elim2 (\lambda n,m. nat_compare n m = compare_invert (nat_compare m n)).
184 intros.elim n1.simplify.reflexivity.
185 simplify.reflexivity.
186 intro.elim n1.simplify.reflexivity.
187 simplify.reflexivity.
188 intros.simplify.elim H.reflexivity.
191 theorem nat_compare_elim : \forall n,m:nat. \forall P:compare \to Prop.
192 (n < m \to P LT) \to (n=m \to P EQ) \to (m < n \to P GT) \to
193 (P (nat_compare n m)).
195 cut match (nat_compare n m) with
196 [ LT \Rightarrow n < m
198 | GT \Rightarrow m < n] \to
199 (P (nat_compare n m)).
200 apply Hcut.apply nat_compare_to_Prop.
201 elim (nat_compare n m).