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 include "datatypes/bool.ma".
16 include "datatypes/compare.ma".
17 include "nat/orders.ma".
24 | (S q) \Rightarrow false]
28 | (S q) \Rightarrow eqb p q]].
30 theorem eqb_to_Prop: \forall n,m:nat.
32 [ true \Rightarrow n = m
33 | false \Rightarrow n \neq m].
36 (\lambda n,m:nat.match (eqb n m) with
37 [ true \Rightarrow n = m
38 | false \Rightarrow n \neq m])).
41 simplify.apply not_eq_O_S.
44 intro. apply (not_eq_O_S n1).apply sym_eq.assumption.
46 generalize in match H.
48 simplify.apply eq_f.apply H1.
49 simplify.unfold Not.intro.apply H1.apply inj_S.assumption.
52 theorem eqb_elim : \forall n,m:nat.\forall P:bool \to Prop.
53 (n=m \to (P true)) \to (n \neq m \to (P false)) \to (P (eqb n m)).
57 [ true \Rightarrow n = m
58 | false \Rightarrow n \neq m] \to (P (eqb n m))).
59 apply Hcut.apply eqb_to_Prop.
65 theorem eqb_n_n: \forall n. eqb n n = true.
66 intro.elim n.simplify.reflexivity.
70 theorem eqb_true_to_eq: \forall n,m:nat.
71 eqb n m = true \to n = m.
75 [ true \Rightarrow n = m
76 | false \Rightarrow n \neq m].
81 theorem eqb_false_to_not_eq: \forall n,m:nat.
82 eqb n m = false \to n \neq m.
86 [ true \Rightarrow n = m
87 | false \Rightarrow n \neq m].
92 theorem eq_to_eqb_true: \forall n,m:nat.
93 n = m \to eqb n m = true.
94 intros.apply (eqb_elim n m).
96 intros.apply False_ind.apply (H1 H).
99 theorem not_eq_to_eqb_false: \forall n,m:nat.
100 \lnot (n = m) \to eqb n m = false.
101 intros.apply (eqb_elim n m).
102 intros. apply False_ind.apply (H H1).
111 [ O \Rightarrow false
112 | (S q) \Rightarrow leb p q]].
114 theorem leb_elim: \forall n,m:nat. \forall P:bool \to Prop.
115 (n \leq m \to (P true)) \to (n \nleq m \to (P false)) \to
117 apply nat_elim2; intros; simplify
118 [apply H.apply le_O_n
119 |apply H1.apply not_le_Sn_O.
121 [apply H1.apply le_S_S.assumption.
122 |apply H2.unfold Not.intros.apply H3.apply le_S_S_to_le.assumption
127 theorem leb_true_to_le:\forall n,m.
128 leb n m = true \to (n \le m).
136 theorem leb_false_to_not_le:\forall n,m.
137 leb n m = false \to \lnot (n \le m).
145 theorem decidable_le: \forall n,m. n \leq m \lor n \nleq m.
148 [intro.left.assumption
149 |intro.right.assumption
154 theorem le_to_leb_true: \forall n,m. n \leq m \to leb n m = true.
155 intros.apply leb_elim;intros
157 |apply False_ind.apply H1.apply H.
161 theorem lt_to_leb_false: \forall n,m. m < n \to leb n m = false.
162 intros.apply leb_elim;intros
163 [apply False_ind.apply (le_to_not_lt ? ? H1). assumption
168 theorem leb_to_Prop: \forall n,m:nat.
170 [ true \Rightarrow n \leq m
171 | false \Rightarrow n \nleq m].
172 apply nat_elim2;simplify
176 elim ((leb n m));simplify
177 [apply le_S_S.apply H
178 |unfold Not.intros.apply H.apply le_S_S_to_le.assumption
184 theorem leb_elim: \forall n,m:nat. \forall P:bool \to Prop.
185 (n \leq m \to (P true)) \to (n \nleq m \to (P false)) \to
189 (match (leb n m) with
190 [ true \Rightarrow n \leq m
191 | false \Rightarrow n \nleq m] \to (P (leb n m))).
192 apply Hcut.apply leb_to_Prop.
199 definition ltb ≝λn,m. leb n m ∧ notb (eqb n m).
201 theorem ltb_to_Prop :
216 | apply (not_eq_to_le_to_lt ? ? H H1)
220 | apply le_to_not_lt;
221 generalize in match (not_le_to_lt ? ? H1);
229 theorem ltb_elim: ∀n,m:nat. ∀P:bool → Prop.
230 (n < m → (P true)) → (n ≮ m → (P false)) →
234 (match (ltb n m) with
236 | false ⇒ n ≮ m] → (P (ltb n m))).
237 apply Hcut.apply ltb_to_Prop.
243 let rec nat_compare n m: compare \def
248 | (S q) \Rightarrow LT ]
252 | (S q) \Rightarrow nat_compare p q]].
254 theorem nat_compare_n_n: \forall n:nat. nat_compare n n = EQ.
256 simplify.reflexivity.
260 theorem nat_compare_S_S: \forall n,m:nat.
261 nat_compare n m = nat_compare (S n) (S m).
262 intros.simplify.reflexivity.
265 theorem nat_compare_pred_pred:
266 \forall n,m:nat.lt O n \to lt O m \to
267 eq compare (nat_compare n m) (nat_compare (pred n) (pred m)).
269 apply (lt_O_n_elim n H).
270 apply (lt_O_n_elim m H1).
272 simplify.reflexivity.
275 theorem nat_compare_to_Prop: \forall n,m:nat.
276 match (nat_compare n m) with
277 [ LT \Rightarrow n < m
279 | GT \Rightarrow m < n ].
281 apply (nat_elim2 (\lambda n,m.match (nat_compare n m) with
282 [ LT \Rightarrow n < m
284 | GT \Rightarrow m < n ])).
285 intro.elim n1.simplify.reflexivity.
286 simplify.unfold lt.apply le_S_S.apply le_O_n.
287 intro.simplify.unfold lt.apply le_S_S. apply le_O_n.
288 intros 2.simplify.elim ((nat_compare n1 m1)).
289 simplify. unfold lt. apply le_S_S.apply H.
290 simplify. apply eq_f. apply H.
291 simplify. unfold lt.apply le_S_S.apply H.
294 theorem nat_compare_n_m_m_n: \forall n,m:nat.
295 nat_compare n m = compare_invert (nat_compare m n).
297 apply (nat_elim2 (\lambda n,m. nat_compare n m = compare_invert (nat_compare m n))).
298 intros.elim n1.simplify.reflexivity.
299 simplify.reflexivity.
300 intro.elim n1.simplify.reflexivity.
301 simplify.reflexivity.
302 intros.simplify.elim H.reflexivity.
305 theorem nat_compare_elim : \forall n,m:nat. \forall P:compare \to Prop.
306 (n < m \to P LT) \to (n=m \to P EQ) \to (m < n \to P GT) \to
307 (P (nat_compare n m)).
309 cut (match (nat_compare n m) with
310 [ LT \Rightarrow n < m
312 | GT \Rightarrow m < n] \to
313 (P (nat_compare n m))).
314 apply Hcut.apply nat_compare_to_Prop.
315 elim ((nat_compare n m)).
321 inductive cmp_cases (n,m:nat) : CProp ≝
322 | cmp_le : n ≤ m → cmp_cases n m
323 | cmp_gt : m < n → cmp_cases n m.
325 lemma cmp_nat: ∀n,m.cmp_cases n m.
326 intros; generalize in match (nat_compare_to_Prop n m);
327 cases (nat_compare n m); intros;
328 [constructor 1;apply lt_to_le|constructor 1;rewrite > H|constructor 2]
329 try assumption; apply le_n;