1 (**************************************************************************)
4 (* ||A|| A project by Andrea Asperti *)
6 (* ||I|| Developers: *)
7 (* ||T|| The HELM team. *)
8 (* ||A|| http://helm.cs.unibo.it *)
10 (* \ / This file is distributed under the terms of the *)
11 (* v GNU General Public License Version 2 *)
13 (**************************************************************************)
15 include "ground_2/notation/functions/successor_1.ma".
16 include "ground_2/notation/functions/predecessor_1.ma".
17 include "arithmetics/nat.ma".
18 include "ground_2/lib/star.ma".
20 (* ARITHMETICAL PROPERTIES **************************************************)
22 interpretation "nat successor" 'Successor m = (S m).
24 interpretation "nat predecessor" 'Predecessor m = (pred m).
26 interpretation "nat min" 'and x y = (min x y).
28 interpretation "nat max" 'or x y = (max x y).
30 (* Iota equations ***********************************************************)
32 lemma pred_O: pred 0 = 0.
35 lemma pred_S: ∀m. pred (S m) = m.
38 lemma max_O1: ∀n. n = (0 ∨ n).
41 lemma max_O2: ∀n. n = (n ∨ 0).
44 lemma max_SS: ∀n1,n2. ⫯(n1∨n2) = (⫯n1 ∨ ⫯n2).
45 #n1 #n2 elim (decidable_le n1 n2) #H normalize
46 [ >(le_to_leb_true … H) | >(not_le_to_leb_false … H) ] -H //
49 (* Equations ****************************************************************)
51 lemma minus_plus_m_m_commutative: ∀n,m:nat. n = m + n - m.
54 (* Note: uses minus_minus_comm, minus_plus_m_m, commutative_plus, plus_minus *)
55 lemma plus_minus_minus_be: ∀x,y,z. y ≤ z → z ≤ x → (x - z) + (z - y) = x - y.
56 #x #z #y #Hzy #Hyx >plus_minus // >commutative_plus >plus_minus //
59 fact plus_minus_minus_be_aux: ∀i,x,y,z. y ≤ z → z ≤ x → i = z - y → x - z + i = x - y.
60 /2 width=1 by plus_minus_minus_be/ qed-.
62 lemma plus_n_2: ∀n. n + 2 = n + 1 + 1.
65 lemma le_plus_minus: ∀m,n,p. p ≤ n → m + n - p = m + (n - p).
66 /2 by plus_minus/ qed.
68 lemma le_plus_minus_comm: ∀n,m,p. p ≤ m → m + n - p = m - p + n.
69 /2 by plus_minus/ qed.
71 lemma minus_minus_comm3: ∀n,x,y,z. n-x-y-z = n-y-z-x.
74 lemma arith_b1: ∀a,b,c1. c1 ≤ b → a - c1 - (b - c1) = a - b.
75 #a #b #c1 #H >minus_minus_comm >minus_le_minus_minus_comm //
78 lemma arith_b2: ∀a,b,c1,c2. c1 + c2 ≤ b → a - c1 - c2 - (b - c1 - c2) = a - b.
79 #a #b #c1 #c2 #H >minus_plus >minus_plus >minus_plus /2 width=1 by arith_b1/
82 lemma arith_c1x: ∀x,a,b,c1. x + c1 + a - (b + c1) = x + a - b.
83 /3 by monotonic_le_minus_l, le_to_le_to_eq, le_n/ qed.
85 lemma arith_h1: ∀a1,a2,b,c1. c1 ≤ a1 → c1 ≤ b →
86 a1 - c1 + a2 - (b - c1) = a1 + a2 - b.
87 #a1 #a2 #b #c1 #H1 #H2 >plus_minus /2 width=1 by arith_b2/
90 lemma arith_i: ∀x,y,z. y < x → x+z-y-1 = x-y-1+z.
91 /2 width=1 by plus_minus/ qed-.
93 (* Properties ***************************************************************)
95 lemma eq_nat_dec: ∀n1,n2:nat. Decidable (n1 = n2).
96 #n1 elim n1 -n1 [| #n1 #IHn1 ] * [2,4: #n2 ]
97 [1,4: @or_intror #H destruct
98 | elim (IHn1 n2) -IHn1 /3 width=1 by or_intror, or_introl/
99 | /2 width=1 by or_introl/
103 lemma lt_or_eq_or_gt: ∀m,n. ∨∨ m < n | n = m | n < m.
104 #m #n elim (lt_or_ge m n) /2 width=1 by or3_intro0/
105 #H elim H -m /2 width=1 by or3_intro1/
106 #m #Hm * /3 width=1 by not_le_to_lt, le_S_S, or3_intro2/
109 fact le_repl_sn_conf_aux: ∀x,y,z:nat. x ≤ z → x = y → y ≤ z.
112 fact le_repl_sn_trans_aux: ∀x,y,z:nat. x ≤ z → y = x → y ≤ z.
115 lemma monotonic_le_minus_l2: ∀x1,x2,y,z. x1 ≤ x2 → x1 - y - z ≤ x2 - y - z.
116 /3 width=1 by monotonic_le_minus_l/ qed.
118 (* Note: this might interfere with nat.ma *)
119 lemma monotonic_lt_pred: ∀m,n. m < n → O < m → pred m < pred n.
120 #m #n #Hmn #Hm whd >(S_pred … Hm)
121 @le_S_S_to_le >S_pred /2 width=3 by transitive_lt/
124 lemma lt_S_S: ∀x,y. x < y → ⫯x < ⫯y.
125 /2 width=1 by le_S_S/ qed.
127 lemma lt_S: ∀n,m. n < m → n < ⫯m.
128 /2 width=1 by le_S/ qed.
130 lemma max_S1_le_S: ∀n1,n2,n. (n1 ∨ n2) ≤ n → (⫯n1 ∨ n2) ≤ ⫯n.
131 /4 width=2 by to_max, le_maxr, le_S_S, le_S/ qed-.
133 lemma max_S2_le_S: ∀n1,n2,n. (n1 ∨ n2) ≤ n → (n1 ∨ ⫯n2) ≤ ⫯n.
134 /2 width=1 by max_S1_le_S/ qed-.
136 lemma arith_j: ∀x,y,z. x-y-1 ≤ x-(y-z)-1.
137 /3 width=1 by monotonic_le_minus_l, monotonic_le_minus_r/ qed.
139 lemma arith_k_sn: ∀z,x,y,n. z < x → x+n ≤ y → x-z-1+n ≤ y-z-1.
140 #z #x #y #n #Hzx #Hxny
141 >plus_minus [2: /2 width=1 by monotonic_le_minus_r/ ]
142 >plus_minus [2: /2 width=1 by lt_to_le/ ]
143 /2 width=1 by monotonic_le_minus_l2/
146 lemma arith_k_dx: ∀z,x,y,n. z < x → y ≤ x+n → y-z-1 ≤ x-z-1+n.
147 #z #x #y #n #Hzx #Hyxn
148 >plus_minus [2: /2 width=1 by monotonic_le_minus_r/ ]
149 >plus_minus [2: /2 width=1 by lt_to_le/ ]
150 /2 width=1 by monotonic_le_minus_l2/
153 (* Inversion & forward lemmas ***********************************************)
155 lemma discr_plus_xy_y: ∀x,y. x + y = y → x = 0.
158 lemma discr_plus_x_xy: ∀x,y. x = x + y → y = 0.
159 /2 width=2 by le_plus_minus_comm/ qed-.
161 lemma lt_plus_SO_to_le: ∀x,y. x < y + 1 → x ≤ y.
162 /2 width=1 by monotonic_pred/ qed-.
164 lemma lt_refl_false: ∀n. n < n → ⊥.
165 #n #H elim (lt_to_not_eq … H) -H /2 width=1 by/
168 lemma lt_zero_false: ∀n. n < 0 → ⊥.
169 #n #H elim (lt_to_not_le … H) -H /2 width=1 by/
172 lemma lt_le_false: ∀x,y. x < y → y ≤ x → ⊥.
173 /3 width=4 by lt_refl_false, lt_to_le_to_lt/ qed-.
175 lemma succ_inv_refl_sn: ∀x. ⫯x = x → ⊥.
176 #x #H @(lt_le_false x (⫯x)) //
179 lemma lt_inv_O1: ∀n. 0 < n → ∃m. ⫯m = n.
180 * /2 width=2 by ex_intro/
181 #H cases (lt_le_false … H) -H //
184 lemma lt_inv_S1: ∀m,n. ⫯m < n → ∃∃p. m < p & ⫯p = n.
185 #m * /3 width=3 by lt_S_S_to_lt, ex2_intro/
186 #H cases (lt_le_false … H) -H //
189 lemma lt_inv_gen: ∀y,x. x < y → ∃∃z. x ≤ z & ⫯z = y.
190 * /3 width=3 by le_S_S_to_le, ex2_intro/
191 #x #H elim (lt_le_false … H) -H //
194 lemma pred_inv_refl: ∀m. pred m = m → m = 0.
195 * // normalize #m #H elim (lt_refl_false m) //
198 lemma le_plus_xSy_O_false: ∀x,y. x + S y ≤ 0 → ⊥.
199 #x #y #H lapply (le_n_O_to_eq … H) -H <plus_n_Sm #H destruct
202 lemma le_plus_xySz_x_false: ∀y,z,x. x + y + S z ≤ x → ⊥.
203 #y #z #x elim x -x /3 width=1 by le_S_S_to_le/
204 #H elim (le_plus_xSy_O_false … H)
207 lemma plus_xySz_x_false: ∀z,x,y. x + y + S z = x → ⊥.
208 /2 width=4 by le_plus_xySz_x_false/ qed-.
210 lemma plus_xSy_x_false: ∀y,x. x + S y = x → ⊥.
211 /2 width=4 by plus_xySz_x_false/ qed-.
213 (* Note this should go in nat.ma *)
214 lemma discr_x_minus_xy: ∀x,y. x = x - y → x = 0 ∨ y = 0.
215 #x @(nat_ind_plus … x) -x /2 width=1 by or_introl/
216 #x #_ #y @(nat_ind_plus … y) -y /2 width=1 by or_intror/
217 #y #_ >minus_plus_plus_l
218 #H lapply (discr_plus_xy_minus_xz … H) -H
222 lemma zero_eq_plus: ∀x,y. 0 = x + y → 0 = x ∧ 0 = y.
223 * /2 width=1 by conj/ #x #y normalize #H destruct
226 lemma lt_S_S_to_lt: ∀x,y. ⫯x < ⫯y → x < y.
227 /2 width=1 by le_S_S_to_le/ qed-.
229 lemma lt_elim: ∀R:relation nat.
231 (∀n1,n2. R n1 n2 → R (⫯n1) (⫯n2)) →
232 ∀n2,n1. n1 < n2 → R n1 n2.
233 #R #IH1 #IH2 #n2 elim n2 -n2
234 [ #n1 #H elim (lt_le_false … H) -H //
235 | #n2 #IH * /4 width=1 by lt_S_S_to_lt/
239 lemma le_elim: ∀R:relation nat.
241 (∀n1,n2. R n1 n2 → R (⫯n1) (⫯n2)) →
242 ∀n1,n2. n1 ≤ n2 → R n1 n2.
243 #R #IH1 #IH2 #n1 #n2 @(nat_elim2 … n1 n2) -n1 -n2
244 /4 width=1 by monotonic_pred/ -IH1 -IH2
245 #n1 #H elim (lt_le_false … H) -H //
248 (* Iterators ****************************************************************)
250 (* Note: see also: lib/arithemetics/bigops.ma *)
251 rec definition iter (n:nat) (B:Type[0]) (op: B → B) (nil: B) ≝
254 | S k ⇒ op (iter k B op nil)
257 interpretation "iterated function" 'exp op n = (iter n ? op).
259 lemma iter_O: ∀B:Type[0]. ∀f:B→B.∀b. f^0 b = b.
262 lemma iter_S: ∀B:Type[0]. ∀f:B→B.∀b,l. f^(S l) b = f (f^l b).
265 lemma iter_SO: ∀B:Type[0]. ∀f:B→B. ∀b,l. f^(l+1) b = f (f^l b).
266 #B #f #b #l >commutative_plus //
269 lemma iter_n_Sm: ∀B:Type[0]. ∀f:B→B. ∀b,l. f^l (f b) = f (f^l b).
270 #B #f #b #l elim l -l normalize //
273 lemma iter_plus: ∀B:Type[0]. ∀f:B→B. ∀b,l1,l2. f^(l1+l2) b = f^l1 (f^l2 b).
274 #B #f #b #l1 elim l1 -l1 normalize //
277 (* Trichotomy operator ******************************************************)
279 (* Note: this is "if eqb n1 n2 then a2 else if leb n1 n2 then a1 else a3" *)
280 rec definition tri (A:Type[0]) n1 n2 a1 a2 a3 on n1 : A ≝
282 [ O ⇒ match n2 with [ O ⇒ a2 | S n2 ⇒ a1 ]
283 | S n1 ⇒ match n2 with [ O ⇒ a3 | S n2 ⇒ tri A n1 n2 a1 a2 a3 ]
286 lemma tri_lt: ∀A,a1,a2,a3,n2,n1. n1 < n2 → tri A n1 n2 a1 a2 a3 = a1.
287 #A #a1 #a2 #a3 #n2 elim n2 -n2
288 [ #n1 #H elim (lt_zero_false … H)
289 | #n2 #IH #n1 elim n1 -n1 /3 width=1 by monotonic_lt_pred/
293 lemma tri_eq: ∀A,a1,a2,a3,n. tri A n n a1 a2 a3 = a2.
294 #A #a1 #a2 #a3 #n elim n -n normalize //
297 lemma tri_gt: ∀A,a1,a2,a3,n1,n2. n2 < n1 → tri A n1 n2 a1 a2 a3 = a3.
298 #A #a1 #a2 #a3 #n1 elim n1 -n1
299 [ #n2 #H elim (lt_zero_false … H)
300 | #n1 #IH #n2 elim n2 -n2 /3 width=1 by monotonic_lt_pred/