]> matita.cs.unibo.it Git - helm.git/blob - matita/matita/contribs/lambdadelta/ground_2/lib/arith.ma
update in ground_2 and basic_2
[helm.git] / matita / matita / contribs / lambdadelta / ground_2 / lib / arith.ma
1 (**************************************************************************)
2 (*       ___                                                              *)
3 (*      ||M||                                                             *)
4 (*      ||A||       A project by Andrea Asperti                           *)
5 (*      ||T||                                                             *)
6 (*      ||I||       Developers:                                           *)
7 (*      ||T||         The HELM team.                                      *)
8 (*      ||A||         http://helm.cs.unibo.it                             *)
9 (*      \   /                                                             *)
10 (*       \ /        This file is distributed under the terms of the       *)
11 (*        v         GNU General Public License Version 2                  *)
12 (*                                                                        *)
13 (**************************************************************************)
14
15 include "ground_2/notation/functions/uparrow_1.ma".
16 include "ground_2/notation/functions/downarrow_1.ma".
17 include "arithmetics/nat.ma".
18 include "ground_2/lib/relations.ma".
19
20 (* ARITHMETICAL PROPERTIES **************************************************)
21
22 interpretation "nat successor" 'UpArrow m = (S m).
23
24 interpretation "nat predecessor" 'DownArrow m = (pred m).
25
26 interpretation "nat min" 'and x y = (min x y).
27
28 interpretation "nat max" 'or x y = (max x y).
29
30 (* Iota equations ***********************************************************)
31
32 lemma pred_O: pred 0 = 0.
33 normalize // qed.
34
35 lemma pred_S: ∀m. pred (S m) = m.
36 // qed.
37
38 lemma plus_S1: ∀x,y. ↑(x+y) = (↑x) + y.
39 // qed.
40
41 lemma max_O1: ∀n. n = (0 ∨ n).
42 // qed.
43
44 lemma max_O2: ∀n. n = (n ∨ 0).
45 // qed.
46
47 lemma max_SS: ∀n1,n2. ↑(n1∨n2) = (↑n1 ∨ ↑n2).
48 #n1 #n2 elim (decidable_le n1 n2) #H normalize
49 [ >(le_to_leb_true … H) | >(not_le_to_leb_false … H) ] -H //
50 qed.
51
52 (* Equations ****************************************************************)
53
54 lemma plus_SO: ∀n. n + 1 = ↑n.
55 // qed.
56
57 lemma minus_plus_m_m_commutative: ∀n,m:nat. n = m + n - m.
58 // qed-.
59
60 lemma plus_n_2: ∀n. n + 2 = n + 1 + 1.
61 // qed.
62
63 lemma arith_l (n1) (n2): ↑n2-n1 = 1-n1+(n2-(n1-1)).
64 * // qed.
65
66 lemma arith_l_eq: ∀x. 1 = 1-x+(x-(x-1)).
67 // qed.
68
69 (* Note: uses minus_minus_comm, minus_plus_m_m, commutative_plus, plus_minus *)
70 lemma plus_minus_minus_be: ∀x,y,z. y ≤ z → z ≤ x → (x - z) + (z - y) = x - y.
71 #x #z #y #Hzy #Hyx >plus_minus // >commutative_plus >plus_minus //
72 qed-.
73
74 lemma lt_succ_pred: ∀m,n. n < m → m = ↑↓m.
75 #m #n #Hm >S_pred /2 width=2 by ltn_to_ltO/
76 qed-.
77
78 fact plus_minus_minus_be_aux: ∀i,x,y,z. y ≤ z → z ≤ x → i = z - y → x - z + i = x - y.
79 /2 width=1 by plus_minus_minus_be/ qed-.
80
81 lemma le_plus_minus: ∀m,n,p. p ≤ n → m + n - p = m + (n - p).
82 /2 by plus_minus/ qed-.
83
84 lemma le_plus_minus_comm: ∀n,m,p. p ≤ m → m + n - p = m - p + n.
85 /2 by plus_minus/ qed-.
86
87 lemma minus_minus_comm3: ∀n,x,y,z. n-x-y-z = n-y-z-x.
88 // qed.
89
90 lemma arith_b1: ∀a,b,c1. c1 ≤ b → a - c1 - (b - c1) = a - b.
91 #a #b #c1 #H >minus_minus_comm >minus_le_minus_minus_comm //
92 qed-.
93
94 lemma arith_b2: ∀a,b,c1,c2. c1 + c2 ≤ b → a - c1 - c2 - (b - c1 - c2) = a - b.
95 #a #b #c1 #c2 #H >minus_plus >minus_plus >minus_plus /2 width=1 by arith_b1/
96 qed-.
97
98 lemma arith_c1x: ∀x,a,b,c1. x + c1 + a - (b + c1) = x + a - b.
99 /3 by monotonic_le_minus_l, le_to_le_to_eq, le_n/ qed.
100
101 lemma arith_h1: ∀a1,a2,b,c1. c1 ≤ a1 → c1 ≤ b →
102                 a1 - c1 + a2 - (b - c1) = a1 + a2 - b.
103 #a1 #a2 #b #c1 #H1 #H2 >plus_minus /2 width=1 by arith_b2/
104 qed-.
105
106 lemma arith_i: ∀x,y,z. y < x → x+z-y-1 = x-y-1+z.
107 /2 width=1 by plus_minus/ qed-.
108
109 lemma plus_to_minus_2: ∀m1,m2,n1,n2. n1 ≤ m1 → n2 ≤ m2 →
110                        m1+n2 = m2+n1 → m1-n1 = m2-n2.
111 /2 width=1 by arith_b1/ qed-.
112
113 lemma idempotent_max: ∀n:nat. n = (n ∨ n).
114 #n normalize >le_to_leb_true //
115 qed.
116
117 lemma associative_max: associative … max.
118 #x #y #z normalize
119 @(leb_elim x y) normalize #Hxy
120 @(leb_elim y z) normalize #Hyz //
121 [1,2: >le_to_leb_true /2 width=3 by transitive_le/
122 | >not_le_to_leb_false /4 width=3 by lt_to_not_le, not_le_to_lt, transitive_lt/
123   >not_le_to_leb_false //
124 ]
125 qed.
126
127 (* Properties ***************************************************************)
128
129 lemma eq_nat_dec: ∀n1,n2:nat. Decidable (n1 = n2).
130 #n1 elim n1 -n1 [| #n1 #IHn1 ] * [2,4: #n2 ]
131 [1,4: @or_intror #H destruct
132 | elim (IHn1 n2) -IHn1 /3 width=1 by or_intror, or_introl/
133 | /2 width=1 by or_introl/
134 ]
135 qed-.
136
137 lemma lt_or_eq_or_gt: ∀m,n. ∨∨ m < n | n = m | n < m.
138 #m #n elim (lt_or_ge m n) /2 width=1 by or3_intro0/
139 #H elim H -m /2 width=1 by or3_intro1/
140 #m #Hm * /3 width=1 by not_le_to_lt, le_S_S, or3_intro2/
141 qed-.
142
143 fact le_repl_sn_conf_aux: ∀x,y,z:nat. x ≤ z → x = y → y ≤ z.
144 // qed-.
145
146 fact le_repl_sn_trans_aux: ∀x,y,z:nat. x ≤ z → y = x → y ≤ z.
147 // qed-.
148
149 lemma monotonic_le_minus_l2: ∀x1,x2,y,z. x1 ≤ x2 → x1 - y - z ≤ x2 - y - z.
150 /3 width=1 by monotonic_le_minus_l/ qed.
151
152 lemma minus_le_trans_sn: ∀x1,x2. x1 ≤ x2 → ∀x. x1-x ≤ x2.
153 /2 width=3 by transitive_le/ qed.
154
155 (* Note: this might interfere with nat.ma *)
156 lemma monotonic_lt_pred: ∀m,n. m < n → 0 < m → pred m < pred n.
157 #m #n #Hmn #Hm whd >(S_pred … Hm)
158 @le_S_S_to_le >S_pred /2 width=3 by transitive_lt/
159 qed.
160
161 lemma lt_S_S: ∀x,y. x < y → ↑x < ↑y.
162 /2 width=1 by le_S_S/ qed.
163
164 lemma lt_S: ∀n,m. n < m → n < ↑m.
165 /2 width=1 by le_S/ qed.
166
167 lemma max_S1_le_S: ∀n1,n2,n. (n1 ∨ n2) ≤ n → (↑n1 ∨ n2) ≤ ↑n.
168 /4 width=2 by to_max, le_maxr, le_S_S, le_S/ qed-.
169
170 lemma max_S2_le_S: ∀n1,n2,n. (n1 ∨ n2) ≤ n → (n1 ∨ ↑n2) ≤ ↑n.
171 /2 width=1 by max_S1_le_S/ qed-.
172
173 lemma arith_j: ∀x,y,z. x-y-1 ≤ x-(y-z)-1.
174 /3 width=1 by monotonic_le_minus_l, monotonic_le_minus_r/ qed.
175
176 lemma arith_k_sn: ∀z,x,y,n. z < x → x+n ≤ y → x-z-1+n ≤ y-z-1.
177 #z #x #y #n #Hzx #Hxny
178 >plus_minus [2: /2 width=1 by monotonic_le_minus_r/ ]
179 >plus_minus [2: /2 width=1 by lt_to_le/ ]
180 /2 width=1 by monotonic_le_minus_l2/
181 qed.
182
183 lemma arith_k_dx: ∀z,x,y,n. z < x → y ≤ x+n → y-z-1 ≤ x-z-1+n.
184 #z #x #y #n #Hzx #Hyxn
185 >plus_minus [2: /2 width=1 by monotonic_le_minus_r/ ]
186 >plus_minus [2: /2 width=1 by lt_to_le/ ]
187 /2 width=1 by monotonic_le_minus_l2/
188 qed.
189
190 (* Inversion & forward lemmas ***********************************************)
191
192 lemma lt_refl_false: ∀n. n < n → ⊥.
193 #n #H elim (lt_to_not_eq … H) -H /2 width=1 by/
194 qed-.
195
196 lemma lt_zero_false: ∀n. n < 0 → ⊥.
197 #n #H elim (lt_to_not_le … H) -H /2 width=1 by/
198 qed-.
199
200 lemma lt_le_false: ∀x,y. x < y → y ≤ x → ⊥.
201 /3 width=4 by lt_refl_false, lt_to_le_to_lt/ qed-.
202
203 lemma succ_inv_refl_sn: ∀x. ↑x = x → ⊥.
204 #x #H @(lt_le_false x (↑x)) //
205 qed-.
206
207 lemma le_plus_xSy_O_false: ∀x,y. x + S y ≤ 0 → ⊥.
208 #x #y #H lapply (le_n_O_to_eq … H) -H <plus_n_Sm #H destruct
209 qed-.
210
211 lemma le_plus_xySz_x_false: ∀y,z,x. x + y + S z ≤ x → ⊥.
212 #y #z #x elim x -x /3 width=1 by le_S_S_to_le/
213 #H elim (le_plus_xSy_O_false … H)
214 qed-.
215
216 lemma plus_xySz_x_false: ∀z,x,y. x + y + S z = x → ⊥.
217 /2 width=4 by le_plus_xySz_x_false/ qed-.
218
219 lemma plus_xSy_x_false: ∀y,x. x + S y = x → ⊥.
220 /2 width=4 by plus_xySz_x_false/ qed-.
221
222 lemma pred_inv_refl: ∀m. pred m = m → m = 0.
223 * // normalize #m #H elim (lt_refl_false m) //
224 qed-.
225
226 lemma discr_plus_xy_y: ∀x,y. x + y = y → x = 0.
227 // qed-.
228
229 lemma discr_plus_x_xy: ∀x,y. x = x + y → y = 0.
230 /2 width=2 by le_plus_minus_comm/ qed-.
231
232 lemma lt_plus_SO_to_le: ∀x,y. x < y + 1 → x ≤ y.
233 /2 width=1 by monotonic_pred/ qed-.
234
235 lemma plus2_inv_le_sn: ∀m1,m2,n1,n2. m1 + n1 = m2 + n2 → m1 ≤ m2 → n2 ≤ n1.
236 #m1 #m2 #n1 #n2 #H #Hm
237 lapply (monotonic_le_plus_l n1 … Hm) -Hm >H -H
238 /2 width=2 by le_plus_to_le/
239 qed-.
240
241 lemma lt_S_S_to_lt: ∀x,y. ↑x < ↑y → x < y.
242 /2 width=1 by le_S_S_to_le/ qed-.
243
244 (* Note this should go in nat.ma *)
245 lemma discr_x_minus_xy: ∀x,y. x = x - y → x = 0 ∨ y = 0.
246 #x @(nat_ind_plus … x) -x /2 width=1 by or_introl/
247 #x #_ #y @(nat_ind_plus … y) -y /2 width=1 by or_intror/
248 #y #_ >minus_plus_plus_l
249 #H lapply (discr_plus_xy_minus_xz … H) -H
250 #H destruct
251 qed-.
252
253 lemma lt_inv_O1: ∀n. 0 < n → ∃m. ↑m = n.
254 * /2 width=2 by ex_intro/
255 #H cases (lt_le_false … H) -H //
256 qed-.
257
258 lemma lt_inv_S1: ∀m,n. ↑m < n → ∃∃p. m < p & ↑p = n.
259 #m * /3 width=3 by lt_S_S_to_lt, ex2_intro/
260 #H cases (lt_le_false … H) -H //
261 qed-.
262
263 lemma lt_inv_gen: ∀y,x. x < y → ∃∃z. x ≤ z & ↑z = y.
264 * /3 width=3 by le_S_S_to_le, ex2_intro/
265 #x #H elim (lt_le_false … H) -H //
266 qed-.
267
268 lemma plus_inv_O3: ∀x,y. x + y = 0 → x = 0 ∧ y = 0.
269 /2 width=1 by plus_le_0/ qed-.
270
271 lemma plus_inv_S3_sn: ∀x1,x2,x3. x1+x2 = ↑x3 →
272                       ∨∨ ∧∧ x1 = 0 & x2 = ↑x3
273                        | ∃∃y1. x1 = ↑y1 & y1 + x2 = x3.
274 * /3 width=1 by or_introl, conj/
275 #x1 #x2 #x3 <plus_S1 #H destruct
276 /3 width=3 by ex2_intro, or_intror/
277 qed-.
278
279 lemma plus_inv_S3_dx: ∀x2,x1,x3. x1+x2 = ↑x3 →
280                       ∨∨ ∧∧ x2 = 0 & x1 = ↑x3
281                        | ∃∃y2. x2 = ↑y2 & x1 + y2 = x3.
282 * /3 width=1 by or_introl, conj/
283 #x2 #x1 #x3 <plus_n_Sm #H destruct
284 /3 width=3 by ex2_intro, or_intror/
285 qed-.
286
287 lemma max_inv_O3: ∀x,y. (x ∨ y) = 0 → 0 = x ∧ 0 = y.
288 /4 width=2 by le_maxr, le_maxl, le_n_O_to_eq, conj/
289 qed-.
290
291 lemma zero_eq_plus: ∀x,y. 0 = x + y → 0 = x ∧ 0 = y.
292 * /2 width=1 by conj/ #x #y normalize #H destruct
293 qed-.
294
295 lemma nat_split: ∀x. x = 0 ∨ ∃y. ↑y = x.
296 * /3 width=2 by ex_intro, or_introl, or_intror/
297 qed-.
298
299 lemma lt_elim: ∀R:relation nat.
300                (∀n2. R O (↑n2)) →
301                (∀n1,n2. R n1 n2 → R (↑n1) (↑n2)) →
302                ∀n2,n1. n1 < n2 → R n1 n2.
303 #R #IH1 #IH2 #n2 elim n2 -n2
304 [ #n1 #H elim (lt_le_false … H) -H //
305 | #n2 #IH * /4 width=1 by lt_S_S_to_lt/
306 ]
307 qed-.
308
309 lemma le_elim: ∀R:relation nat.
310                (∀n2. R O (n2)) →
311                (∀n1,n2. R n1 n2 → R (↑n1) (↑n2)) →
312                ∀n1,n2. n1 ≤ n2 → R n1 n2.
313 #R #IH1 #IH2 #n1 #n2 @(nat_elim2 … n1 n2) -n1 -n2
314 /4 width=1 by monotonic_pred/ -IH1 -IH2
315 #n1 #H elim (lt_le_false … H) -H //
316 qed-.
317
318 (* Iterators ****************************************************************)
319
320 (* Note: see also: lib/arithemetics/bigops.ma *)
321 rec definition iter (n:nat) (B:Type[0]) (op: B → B) (nil: B) ≝
322   match n with
323    [ O   ⇒ nil
324    | S k ⇒ op (iter k B op nil)
325    ].
326
327 interpretation "iterated function" 'exp op n = (iter n ? op).
328
329 lemma iter_O: ∀B:Type[0]. ∀f:B→B.∀b. f^0 b = b.
330 // qed.
331
332 lemma iter_S: ∀B:Type[0]. ∀f:B→B.∀b,l. f^(S l) b = f (f^l b).
333 // qed.
334
335 lemma iter_SO: ∀B:Type[0]. ∀f:B→B. ∀b,l. f^(l+1) b = f (f^l b).
336 #B #f #b #l >commutative_plus //
337 qed.
338
339 lemma iter_n_Sm: ∀B:Type[0]. ∀f:B→B. ∀b,l. f^l (f b) = f (f^l b).
340 #B #f #b #l elim l -l normalize //
341 qed.
342
343 lemma iter_plus: ∀B:Type[0]. ∀f:B→B. ∀b,l1,l2. f^(l1+l2) b = f^l1 (f^l2 b).
344 #B #f #b #l1 elim l1 -l1 normalize //
345 qed.
346
347 (* Trichotomy operator ******************************************************)
348
349 (* Note: this is "if eqb n1 n2 then a2 else if leb n1 n2 then a1 else a3" *)
350 rec definition tri (A:Type[0]) n1 n2 a1 a2 a3 on n1 : A ≝
351   match n1 with
352   [ O    ⇒ match n2 with [ O ⇒ a2 | S n2 ⇒ a1 ]
353   | S n1 ⇒ match n2 with [ O ⇒ a3 | S n2 ⇒ tri A n1 n2 a1 a2 a3 ]
354   ].
355
356 lemma tri_lt: ∀A,a1,a2,a3,n2,n1. n1 < n2 → tri A n1 n2 a1 a2 a3 = a1.
357 #A #a1 #a2 #a3 #n2 elim n2 -n2
358 [ #n1 #H elim (lt_zero_false … H)
359 | #n2 #IH #n1 elim n1 -n1 /3 width=1 by monotonic_lt_pred/
360 ]
361 qed.
362
363 lemma tri_eq: ∀A,a1,a2,a3,n. tri A n n a1 a2 a3 = a2.
364 #A #a1 #a2 #a3 #n elim n -n normalize //
365 qed.
366
367 lemma tri_gt: ∀A,a1,a2,a3,n1,n2. n2 < n1 → tri A n1 n2 a1 a2 a3 = a3.
368 #A #a1 #a2 #a3 #n1 elim n1 -n1
369 [ #n2 #H elim (lt_zero_false … H)
370 | #n1 #IH #n2 elim n2 -n2 /3 width=1 by monotonic_lt_pred/
371 ]
372 qed.