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 \lnot (n = m)].
38 (\lambda n,m:nat.match (eqb n m) with
39 [ true \Rightarrow n = m
40 | false \Rightarrow \lnot (n = 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 (\lnot n=m \to (P false)) \to (P (eqb n m)).
59 [ true \Rightarrow n = m
60 | false \Rightarrow \lnot (n = m)] \to (P (eqb n m)).
61 apply Hcut.apply eqb_to_Prop.
73 | (S q) \Rightarrow leb p q]].
75 theorem leb_to_Prop: \forall n,m:nat.
77 [ true \Rightarrow n \leq m
78 | false \Rightarrow \lnot (n \leq m)].
81 (\lambda n,m:nat.match (leb n m) with
82 [ true \Rightarrow n \leq m
83 | false \Rightarrow \lnot (n \leq m)]).
84 simplify.exact le_O_n.
85 simplify.exact not_le_Sn_O.
86 intros 2.simplify.elim (leb n1 m1).
87 simplify.apply le_S_S.apply H.
88 simplify.intros.apply H.apply le_S_S_to_le.assumption.
91 theorem leb_elim: \forall n,m:nat. \forall P:bool \to Prop.
92 (n \leq m \to (P true)) \to (\not (n \leq m) \to (P false)) \to
97 [ true \Rightarrow n \leq m
98 | false \Rightarrow \lnot (n \leq m)] \to (P (leb n m)).
99 apply Hcut.apply leb_to_Prop.
105 let rec nat_compare n m: compare \def
110 | (S q) \Rightarrow LT ]
114 | (S q) \Rightarrow nat_compare p q]].
116 theorem nat_compare_n_n: \forall n:nat. nat_compare n n = EQ.
118 simplify.reflexivity.
122 theorem nat_compare_S_S: \forall n,m:nat.
123 nat_compare n m = nat_compare (S n) (S m).
124 intros.simplify.reflexivity.
127 theorem S_pred: \forall n:nat.lt O n \to eq nat n (S (pred n)).
128 intro.elim n.apply False_ind.exact not_le_Sn_O O H.
129 apply eq_f.apply pred_Sn.
132 theorem nat_compare_pred_pred:
133 \forall n,m:nat.lt O n \to lt O m \to
134 eq compare (nat_compare n m) (nat_compare (pred n) (pred m)).
136 apply lt_O_n_elim n H.
137 apply lt_O_n_elim m H1.
139 simplify.reflexivity.
142 theorem nat_compare_to_Prop: \forall n,m:nat.
143 match (nat_compare n m) with
144 [ LT \Rightarrow n < m
146 | GT \Rightarrow m < n ].
148 apply nat_elim2 (\lambda n,m.match (nat_compare n m) with
149 [ LT \Rightarrow n < m
151 | GT \Rightarrow m < n ]).
152 intro.elim n1.simplify.reflexivity.
153 simplify.apply le_S_S.apply le_O_n.
154 intro.simplify.apply le_S_S. apply le_O_n.
155 intros 2.simplify.elim (nat_compare n1 m1).
156 simplify. apply le_S_S.apply H.
157 simplify. apply eq_f. apply H.
158 simplify. apply le_S_S.apply H.
161 theorem nat_compare_n_m_m_n: \forall n,m:nat.
162 nat_compare n m = compare_invert (nat_compare m n).
164 apply nat_elim2 (\lambda n,m. nat_compare n m = compare_invert (nat_compare m n)).
165 intros.elim n1.simplify.reflexivity.
166 simplify.reflexivity.
167 intro.elim n1.simplify.reflexivity.
168 simplify.reflexivity.
169 intros.simplify.elim H.reflexivity.
172 theorem nat_compare_elim : \forall n,m:nat. \forall P:compare \to Prop.
173 (n < m \to P LT) \to (n=m \to P EQ) \to (m < n \to P GT) \to
174 (P (nat_compare n m)).
176 cut match (nat_compare n m) with
177 [ LT \Rightarrow n < m
179 | GT \Rightarrow m < n] \to
180 (P (nat_compare n m)).
181 apply Hcut.apply nat_compare_to_Prop.
182 elim (nat_compare n m).