]> matita.cs.unibo.it Git - helm.git/blob - helm/matita/library/Z/compare.ma
"bool.ma" is now always included before "logic.ma", in order to speed up
[helm.git] / helm / matita / library / Z / 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/Z/compare".
16
17 include "datatypes/bool.ma".
18 include "datatypes/compare.ma".
19 include "Z/orders.ma".
20 include "nat/compare.ma".
21
22 (* boolean equality *)
23 definition eqZb : Z \to Z \to bool \def
24 \lambda x,y:Z.
25   match x with
26   [ OZ \Rightarrow 
27       match y with
28         [ OZ \Rightarrow true
29         | (pos q) \Rightarrow false
30         | (neg q) \Rightarrow false]
31   | (pos p) \Rightarrow 
32       match y with
33         [ OZ \Rightarrow false
34         | (pos q) \Rightarrow eqb p q
35         | (neg q) \Rightarrow false]     
36   | (neg p) \Rightarrow 
37       match y with
38         [ OZ \Rightarrow false
39         | (pos q) \Rightarrow false
40         | (neg q) \Rightarrow eqb p q]].
41
42 theorem eqZb_to_Prop: 
43 \forall x,y:Z. 
44 match eqZb x y with
45 [ true \Rightarrow x=y
46 | false \Rightarrow \lnot x=y].
47 intros.elim x.
48 elim y.
49 simplify.reflexivity.
50 simplify.apply not_eq_OZ_neg.
51 simplify.apply not_eq_OZ_pos.
52 elim y.
53 simplify.intro.apply not_eq_OZ_neg n ?.apply sym_eq.assumption.
54 simplify.apply eqb_elim.intro.simplify.apply eq_f.assumption.
55 intro.simplify.intro.apply H.apply inj_neg.assumption.
56 simplify.intro.apply not_eq_pos_neg n1 n ?.apply sym_eq.assumption.
57 elim y.
58 simplify.intro.apply not_eq_OZ_pos n ?.apply sym_eq.assumption.
59 simplify.apply not_eq_pos_neg.
60 simplify.apply eqb_elim.intro.simplify.apply eq_f.assumption.
61 intro.simplify.intro.apply H.apply inj_pos.assumption.
62 qed.
63
64 theorem eqZb_elim: \forall x,y:Z.\forall P:bool \to Prop.
65 (x=y \to (P true)) \to (\lnot x=y \to (P false)) \to P (eqZb x y).
66 intros.
67 cut 
68 match (eqZb x y) with
69 [ true \Rightarrow x=y
70 | false \Rightarrow \lnot x=y] \to P (eqZb x y).
71 apply Hcut.
72 apply eqZb_to_Prop.
73 elim (eqZb).
74 apply H H2.
75 apply H1 H2.
76 qed.
77
78 definition Z_compare : Z \to Z \to compare \def
79 \lambda x,y:Z.
80   match x with
81   [ OZ \Rightarrow 
82     match y with 
83     [ OZ \Rightarrow EQ
84     | (pos m) \Rightarrow LT
85     | (neg m) \Rightarrow GT ]
86   | (pos n) \Rightarrow 
87     match y with 
88     [ OZ \Rightarrow GT
89     | (pos m) \Rightarrow (nat_compare n m)
90     | (neg m) \Rightarrow GT]
91   | (neg n) \Rightarrow 
92     match y with 
93     [ OZ \Rightarrow LT
94     | (pos m) \Rightarrow LT
95     | (neg m) \Rightarrow nat_compare m n ]].
96
97 theorem Z_compare_to_Prop : 
98 \forall x,y:Z. match (Z_compare x y) with
99 [ LT \Rightarrow x < y
100 | EQ \Rightarrow x=y
101 | GT \Rightarrow y < x]. 
102 intros.
103 elim x. elim y.
104 simplify.apply refl_eq.
105 simplify.exact I.
106 simplify.exact I.
107 elim y. simplify.exact I.
108 simplify. 
109 cut match (nat_compare n1 n) with
110 [ LT \Rightarrow n1<n
111 | EQ \Rightarrow n1=n
112 | GT \Rightarrow n<n1] \to 
113 match (nat_compare n1 n) with
114 [ LT \Rightarrow (S n1) \leq n
115 | EQ \Rightarrow neg n = neg n1
116 | GT \Rightarrow (S n) \leq n1]. 
117 apply Hcut. apply nat_compare_to_Prop. 
118 elim (nat_compare n1 n).
119 simplify.exact H.
120 simplify.exact H.
121 simplify.apply eq_f.apply sym_eq.exact H.
122 simplify.exact I.
123 elim y.simplify.exact I.
124 simplify.exact I.
125 simplify.
126 cut match (nat_compare n n1) with
127 [ LT \Rightarrow n<n1
128 | EQ \Rightarrow n=n1
129 | GT \Rightarrow n1<n] \to 
130 match (nat_compare n n1) with
131 [ LT \Rightarrow (S n) \leq n1
132 | EQ \Rightarrow pos n = pos n1
133 | GT \Rightarrow (S n1) \leq n]. 
134 apply Hcut. apply nat_compare_to_Prop. 
135 elim (nat_compare n n1).
136 simplify.exact H.
137 simplify.exact H.
138 simplify.apply eq_f.exact H.
139 qed.