]> matita.cs.unibo.it Git - helm.git/blob - helm/matita/library/nat/compare.ma
Committing all the recent development of Andrea after the merge between his
[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 "nat/orders.ma".
18 include "datatypes/bool.ma".
19 include "datatypes/compare.ma".
20
21 let rec leb n m \def 
22 match n with 
23     [ O \Rightarrow true
24     | (S p) \Rightarrow
25         match m with 
26         [ O \Rightarrow false
27         | (S q) \Rightarrow leb p q]].
28         
29 theorem leb_to_Prop: \forall n,m:nat. 
30 match (leb n m) with
31 [ true  \Rightarrow n \leq m 
32 | false \Rightarrow \lnot (n \leq m)].
33 intros.
34 apply nat_elim2
35 (\lambda n,m:nat.match (leb n m) with
36 [ true  \Rightarrow n \leq m 
37 | false \Rightarrow \lnot (n \leq m)]).
38 simplify.exact le_O_n.
39 simplify.exact not_le_Sn_O.
40 intros 2.simplify.elim (leb n1 m1).
41 simplify.apply le_S_S.apply H.
42 simplify.intros.apply H.apply le_S_S_to_le.assumption.
43 qed.
44
45 theorem leb_elim: \forall n,m:nat. \forall P:bool \to Prop. 
46 (n \leq m \to (P true)) \to (\not (n \leq m) \to (P false)) \to
47 P (leb n m).
48 intros.
49 cut 
50 match (leb n m) with
51 [ true  \Rightarrow n \leq m
52 | false \Rightarrow \lnot (n \leq m)] \to (P (leb n m)).
53 apply Hcut.apply leb_to_Prop.
54 elim leb n m.
55 apply (H H2).
56 apply (H1 H2).
57 qed.
58
59 let rec nat_compare n m: compare \def
60 match n with
61 [ O \Rightarrow 
62     match m with 
63       [ O \Rightarrow EQ
64       | (S q) \Rightarrow LT ]
65 | (S p) \Rightarrow 
66     match m with 
67       [ O \Rightarrow GT
68       | (S q) \Rightarrow nat_compare p q]].
69
70 theorem nat_compare_n_n: \forall n:nat. nat_compare n n = EQ.
71 intro.elim n.
72 simplify.reflexivity.
73 simplify.assumption.
74 qed.
75
76 theorem nat_compare_S_S: \forall n,m:nat. 
77 nat_compare n m = nat_compare (S n) (S m).
78 intros.simplify.reflexivity.
79 qed.
80
81 theorem S_pred: \forall n:nat.lt O n \to eq nat n (S (pred n)).
82 intro.elim n.apply False_ind.exact not_le_Sn_O O H.
83 apply eq_f.apply pred_Sn.
84 qed.
85
86 theorem nat_compare_pred_pred: 
87 \forall n,m:nat.lt O n \to lt O m \to 
88 eq compare (nat_compare n m) (nat_compare (pred n) (pred m)).
89 intros.
90 apply lt_O_n_elim n H.
91 apply lt_O_n_elim m H1.
92 intros.
93 simplify.reflexivity.
94 qed.
95
96 theorem nat_compare_to_Prop: \forall n,m:nat. 
97 match (nat_compare n m) with
98   [ LT \Rightarrow n < m
99   | EQ \Rightarrow n=m
100   | GT \Rightarrow m < n ].
101 intros.
102 apply nat_elim2 (\lambda n,m.match (nat_compare n m) with
103   [ LT \Rightarrow n < m
104   | EQ \Rightarrow n=m
105   | GT \Rightarrow m < n ]).
106 intro.elim n1.simplify.reflexivity.
107 simplify.apply le_S_S.apply le_O_n.
108 intro.simplify.apply le_S_S. apply le_O_n.
109 intros 2.simplify.elim (nat_compare n1 m1).
110 simplify. apply le_S_S.apply H.
111 simplify. apply le_S_S.apply H.
112 simplify. apply eq_f. apply H.
113 qed.
114
115 theorem nat_compare_n_m_m_n: \forall n,m:nat. 
116 nat_compare n m = compare_invert (nat_compare m n).
117 intros. 
118 apply nat_elim2 (\lambda n,m. nat_compare n m = compare_invert (nat_compare m n)).
119 intros.elim n1.simplify.reflexivity.
120 simplify.reflexivity.
121 intro.elim n1.simplify.reflexivity.
122 simplify.reflexivity.
123 intros.simplify.elim H.reflexivity.
124 qed.
125      
126 theorem nat_compare_elim : \forall n,m:nat. \forall P:compare \to Prop.
127 (n < m \to P LT) \to (n=m \to P EQ) \to (m < n \to P GT) \to 
128 (P (nat_compare n m)).
129 intros.
130 cut match (nat_compare n m) with
131 [ LT \Rightarrow n < m
132 | EQ \Rightarrow n=m
133 | GT \Rightarrow m < n] \to
134 (P (nat_compare n m)).
135 apply Hcut.apply nat_compare_to_Prop.
136 elim (nat_compare n m).
137 apply (H H3).
138 apply (H2 H3).
139 apply (H1 H3).
140 qed.