]> matita.cs.unibo.it Git - helm.git/blob - helm/matita/library/nat/compare.ma
Notation for equality used everywhere.
[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 (le n m) 
32 | false \Rightarrow (Not (le n m))].
33 intros.
34 apply nat_elim2
35 (\lambda n,m:nat.match (leb n m) with
36 [ true  \Rightarrow (le n m) 
37 | false \Rightarrow (Not (le n 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 ((le n m) \to (P true)) \to ((Not (le n m)) \to (P false)) \to
47 P (leb n m).
48 intros.
49 cut 
50 match (leb n m) with
51 [ true  \Rightarrow (le n m) 
52 | false \Rightarrow (Not (le n 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 nat_compare_to_Prop: \forall n,m:nat. 
82 match (nat_compare n m) with
83   [ LT \Rightarrow (lt n m)
84   | EQ \Rightarrow n=m
85   | GT \Rightarrow (lt m n) ].
86 intros.
87 apply nat_elim2 (\lambda n,m.match (nat_compare n m) with
88   [ LT \Rightarrow (lt n m)
89   | EQ \Rightarrow n=m
90   | GT \Rightarrow (lt m n) ]).
91 intro.elim n1.simplify.reflexivity.
92 simplify.apply le_S_S.apply le_O_n.
93 intro.simplify.apply le_S_S. apply le_O_n.
94 intros 2.simplify.elim (nat_compare n1 m1).
95 simplify. apply le_S_S.apply H.
96 simplify. apply le_S_S.apply H.
97 simplify. apply eq_f. apply H.
98 qed.
99
100 theorem nat_compare_n_m_m_n: \forall n,m:nat. 
101 nat_compare n m = compare_invert (nat_compare m n).
102 intros. 
103 apply nat_elim2 (\lambda n,m. nat_compare n m = compare_invert (nat_compare m n)).
104 intros.elim n1.simplify.reflexivity.
105 simplify.reflexivity.
106 intro.elim n1.simplify.reflexivity.
107 simplify.reflexivity.
108 intros.simplify.elim H.reflexivity.
109 qed.
110      
111 theorem nat_compare_elim : \forall n,m:nat. \forall P:compare \to Prop.
112 ((lt n m) \to (P LT)) \to (n=m \to (P EQ)) \to ((lt m n) \to (P GT)) \to 
113 (P (nat_compare n m)).
114 intros.
115 cut match (nat_compare n m) with
116 [ LT \Rightarrow (lt n m)
117 | EQ \Rightarrow n=m
118 | GT \Rightarrow (lt m n)] \to
119 (P (nat_compare n m)).
120 apply Hcut.apply nat_compare_to_Prop.
121 elim (nat_compare n m).
122 apply (H H3).
123 apply (H2 H3).
124 apply (H1 H3).
125 qed.