]> matita.cs.unibo.it Git - helm.git/blob - helm/matita/library/nat/compare.ma
More notation here and there.
[helm.git] / helm / matita / library / nat / compare.ma
1 (**************************************************************************)
2 (*       ___                                                              *)
3 (*      ||M||                                                             *)
4 (*      ||A||       A project by Andrea Asperti                           *)
5 (*      ||T||                                                             *)
6 (*      ||I||       Developers:                                           *)
7 (*      ||T||       A.Asperti, C.Sacerdoti Coen,                          *)
8 (*      ||A||       E.Tassi, S.Zacchiroli                                 *)
9 (*      \   /                                                             *)
10 (*       \ /        This file is distributed under the terms of the       *)
11 (*        v         GNU Lesser General Public License Version 2.1         *)
12 (*                                                                        *)
13 (**************************************************************************)
14
15 set "baseuri" "cic:/matita/nat/compare".
16
17 include "datatypes/bool.ma".
18 include "datatypes/compare.ma".
19 include "nat/orders.ma".
20
21 let rec eqb n m \def 
22 match n with 
23   [ O \Rightarrow 
24      match m with 
25      [ O \Rightarrow true
26            | (S q) \Rightarrow false] 
27   | (S p) \Rightarrow
28            match m with 
29      [ O \Rightarrow false
30            | (S q) \Rightarrow eqb p q]].
31            
32 theorem eqb_to_Prop: \forall n,m:nat. 
33 match (eqb n m) with
34 [ true  \Rightarrow n = m 
35 | false \Rightarrow n \neq m].
36 intros.
37 apply nat_elim2
38 (\lambda n,m:nat.match (eqb n m) with
39 [ true  \Rightarrow n = m 
40 | false \Rightarrow n \neq m]).
41 intro.elim n1.
42 simplify.reflexivity.
43 simplify.apply not_eq_O_S.
44 intro.
45 simplify.
46 intro. apply not_eq_O_S n1.apply sym_eq.assumption.
47 intros.simplify.
48 generalize in match H.
49 elim (eqb n1 m1).
50 simplify.apply eq_f.apply H1.
51 simplify.intro.apply H1.apply inj_S.assumption.
52 qed.
53
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)). 
56 intros.
57 cut 
58 match (eqb n m) with
59 [ true  \Rightarrow n = m
60 | false \Rightarrow n \neq m] \to (P (eqb n m)).
61 apply Hcut.apply eqb_to_Prop.
62 elim eqb n m.
63 apply (H H2).
64 apply (H1 H2).
65 qed.
66
67 let rec leb n m \def 
68 match n with 
69     [ O \Rightarrow true
70     | (S p) \Rightarrow
71         match m with 
72         [ O \Rightarrow false
73         | (S q) \Rightarrow leb p q]].
74         
75 theorem leb_to_Prop: \forall n,m:nat. 
76 match (leb n m) with
77 [ true  \Rightarrow n \leq m 
78 | false \Rightarrow n \nleq m].
79 intros.
80 apply nat_elim2
81 (\lambda n,m:nat.match (leb n m) with
82 [ true  \Rightarrow n \leq m 
83 | false \Rightarrow n \nleq 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.
89 qed.
90
91 theorem leb_elim: \forall n,m:nat. \forall P:bool \to Prop. 
92 (n \leq m \to (P true)) \to (n \nleq m \to (P false)) \to
93 P (leb n m).
94 intros.
95 cut 
96 match (leb n m) with
97 [ true  \Rightarrow n \leq m
98 | false \Rightarrow n \nleq m] \to (P (leb n m)).
99 apply Hcut.apply leb_to_Prop.
100 elim leb n m.
101 apply (H H2).
102 apply (H1 H2).
103 qed.
104
105 let rec nat_compare n m: compare \def
106 match n with
107 [ O \Rightarrow 
108     match m with 
109       [ O \Rightarrow EQ
110       | (S q) \Rightarrow LT ]
111 | (S p) \Rightarrow 
112     match m with 
113       [ O \Rightarrow GT
114       | (S q) \Rightarrow nat_compare p q]].
115
116 theorem nat_compare_n_n: \forall n:nat. nat_compare n n = EQ.
117 intro.elim n.
118 simplify.reflexivity.
119 simplify.assumption.
120 qed.
121
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.
125 qed.
126
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.
130 qed.
131
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)).
135 intros.
136 apply lt_O_n_elim n H.
137 apply lt_O_n_elim m H1.
138 intros.
139 simplify.reflexivity.
140 qed.
141
142 theorem nat_compare_to_Prop: \forall n,m:nat. 
143 match (nat_compare n m) with
144   [ LT \Rightarrow n < m
145   | EQ \Rightarrow n=m
146   | GT \Rightarrow m < n ].
147 intros.
148 apply nat_elim2 (\lambda n,m.match (nat_compare n m) with
149   [ LT \Rightarrow n < m
150   | EQ \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.
159 qed.
160
161 theorem nat_compare_n_m_m_n: \forall n,m:nat. 
162 nat_compare n m = compare_invert (nat_compare m n).
163 intros. 
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.
170 qed.
171      
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)).
175 intros.
176 cut match (nat_compare n m) with
177 [ LT \Rightarrow n < m
178 | EQ \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).
183 apply (H H3).
184 apply (H1 H3).
185 apply (H2 H3).
186 qed.