source: src/utilities/extralib.ma @ 709

Last change on this file since 709 was 709, checked in by sacerdot, 9 years ago

Notations should NOT be redefined. Just add a new interpretation.

File size: 21.5 KB
Line 
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
15include "basics/types.ma".
16include "basics/list.ma".
17include "basics/logic.ma".
18include "utilities/binary/Z.ma".
19include "utilities/binary/positive.ma".
20
21lemma eq_rect_Type0_r:
22 ∀A.∀a.∀P: ∀x:A. eq ? x a → Type[0]. P a (refl A a) → ∀x.∀p:eq ? x a.P x p.
23 #A #a #P #p #x0 #p0 @(eq_rect_r ??? p0) assumption.
24qed.
25
26lemma eq_rect_r2:
27 ∀A.∀a,x.∀p:eq ? x a.∀P: ∀x:A. eq ? x a → Type[2]. P a (refl A a) → P x p.
28 #A #a #x #p cases p; #P #H assumption.
29qed.
30
31lemma eq_rect_Type2_r:
32 ∀A.∀a.∀P: ∀x:A. eq ? x a → Type[2]. P a (refl A a) → ∀x.∀p:eq ? x a.P x p.
33 #A #a #P #p #x0 #p0 @(eq_rect_r2 ??? p0) assumption.
34qed.
35
36lemma eq_rect_CProp0_r:
37 ∀A.∀a.∀P: ∀x:A. eq ? x a → CProp[0]. P a (refl A a) → ∀x.∀p:eq ? x a.P x p.
38 #A #a #P #p #x0 #p0 @(eq_rect_r2 ??? p0) assumption.
39qed.
40
41lemma sym_neq : ∀A.∀x,y:A. x ≠ y → y ≠ x.
42#A #x #y *;#H @nmk #H' /2/;
43qed.
44
45interpretation "logical iff" 'iff x y = (iff x y).
46
47(* bool *)
48
49definition xorb : bool → bool → bool ≝
50  λx,y. match x with [ false ⇒ y | true ⇒ notb y ].
51 
52
53(* TODO: move to Z.ma *)
54
55lemma eqZb_z_z : ∀z.eqZb z z = true.
56#z cases z;normalize;//;
57qed.
58
59(* XXX: divides goes to arithmetics *)
60inductive dividesP (n,m:Pos) : Prop ≝
61| witness : ∀p:Pos.m = times n p → dividesP n m.
62interpretation "positive divides" 'divides n m = (dividesP n m).
63interpretation "positive not divides" 'ndivides n m = (Not (dividesP n m)).
64
65definition dividesZ : Z → Z → Prop ≝
66λx,y. match x with
67[ OZ ⇒ False
68| pos n ⇒ match y with [ OZ ⇒ True | pos m ⇒ dividesP n m | neg m ⇒ dividesP n m ]
69| neg n ⇒ match y with [ OZ ⇒ True | pos m ⇒ dividesP n m | neg m ⇒ dividesP n m ]
70].
71
72interpretation "integer divides" 'divides n m = (dividesZ n m).
73interpretation "integer not divides" 'ndivides n m = (Not (dividesZ n m)).
74
75(* should be proved in nat.ma, but it's not! *)
76lemma eqb_to_Prop : ∀n,m:nat.match eqb n m with [ true ⇒ n = m | false ⇒ n ≠ m ].
77#n elim n;
78[ #m cases m; //;
79| #n' #IH #m cases m; [ /2/; | #m' whd in match (eqb (S n') (S m')) in ⊢ %;
80  lapply (IH m'); cases (eqb n' m'); /2/; ]
81] qed.
82
83lemma pos_eqb_to_Prop : ∀n,m:Pos.match eqb n m with [ true ⇒ n = m | false ⇒ n ≠ m ].
84#n #m @eqb_elim //; qed.
85
86lemma injective_Z_of_nat : injective ? ? Z_of_nat.
87normalize;
88#n #m cases n;cases m;normalize;//;
89[ 1,2: #n' #H destruct
90| #n' #m' #H destruct; >(succ_pos_of_nat_inj … e0) //
91] qed.
92
93lemma reflexive_Zle : reflexive ? Zle.
94#x cases x; //; qed.
95
96lemma Zsucc_pos : ∀n. Z_of_nat (S n) = Zsucc (Z_of_nat n).
97#n cases n;normalize;//;qed.
98
99lemma Zsucc_le : ∀x:Z. x ≤ Zsucc x.
100#x cases x; //;
101#n cases n; //; qed.
102
103lemma Zplus_le_pos : ∀x,y:Z.∀n. x ≤ y → x ≤ y+pos n.
104#x #y
105@pos_elim
106 [ 2: #n' #IH ]
107>(Zplus_Zsucc_Zpred y ?)
108[ >(Zpred_Zsucc (pos n'))
109 #H @(transitive_Zle ??? (IH H)) >(Zplus_Zsucc ??)
110    @Zsucc_le
111| #H @(transitive_Zle ??? H) >(Zplus_z_OZ ?) @Zsucc_le
112] qed.
113
114(* XXX: Zmax must go to arithmetics *)
115definition Zmax : Z → Z → Z ≝
116  λx,y.match Z_compare x y with
117  [ LT ⇒ y
118  | _ ⇒ x ].
119
120lemma Zmax_l: ∀x,y. x ≤ Zmax x y.
121#x #y whd in ⊢ (??%); lapply (Z_compare_to_Prop x y); cases (Z_compare x y);
122/3/; cases x; /3/; qed.
123
124lemma Zmax_r: ∀x,y. y ≤ Zmax x y.
125#x #y whd in ⊢ (??%); lapply (Z_compare_to_Prop x y); cases (Z_compare x y);
126cases x; /3/; qed.
127
128theorem Zle_to_Zlt: ∀x,y:Z. x ≤ y → Zpred x < y.
129#x #y cases x;
130[ cases y;
131  [ 1,2: //
132  | #n @False_ind
133  ]
134| #n cases y;
135  [ normalize; @False_ind
136  | #m @(pos_cases … n) /2/;
137  | #m normalize; @False_ind
138  ]
139| #n cases y; /2/;
140] qed.
141   
142theorem Zlt_to_Zle_to_Zlt: ∀n,m,p:Z. n < m → m ≤ p → n < p.
143#n #m #p #Hlt #Hle <(Zpred_Zsucc n) @Zle_to_Zlt
144@(transitive_Zle … Hle) /2/;
145qed.
146
147definition decidable_eq_Z_Type : ∀z1,z2:Z.(z1 = z2) + (z1 ≠ z2).
148#z1 #z2 lapply (eqZb_to_Prop z1 z2);cases (eqZb z1 z2);normalize;#H
149[% //
150|%2 //]
151qed.
152
153lemma eqZb_false : ∀z1,z2. z1≠z2 → eqZb z1 z2 = false.
154#z1 #z2 lapply (eqZb_to_Prop z1 z2); cases (eqZb z1 z2); //;
155#H #H' @False_ind @(absurd ? H H')
156qed.
157
158(* Z.ma *)
159
160definition Zge: Z → Z → Prop ≝
161λn,m:Z.m ≤ n.
162
163interpretation "integer 'greater or equal to'" 'geq x y = (Zge x y).
164
165definition Zgt: Z → Z → Prop ≝
166λn,m:Z.m<n.
167
168interpretation "integer 'greater than'" 'gt x y = (Zgt x y).
169
170interpretation "integer 'not greater than'" 'ngtr x y = (Not (Zgt x y)).
171
172let rec Zleb (x:Z) (y:Z) : bool ≝
173  match x with
174  [ OZ ⇒
175    match y with
176    [ OZ ⇒ true
177    | pos m ⇒ true
178    | neg m ⇒ false ]
179  | pos n ⇒
180    match y with
181    [ OZ ⇒ false
182    | pos m ⇒ leb n m
183    | neg m ⇒ false ]
184  | neg n ⇒
185    match y with
186    [ OZ ⇒ true
187    | pos m ⇒ true
188    | neg m ⇒ leb m n ]].
189   
190let rec Zltb (x:Z) (y:Z) : bool ≝
191  match x with
192  [ OZ ⇒
193    match y with
194    [ OZ ⇒ false
195    | pos m ⇒ true
196    | neg m ⇒ false ]
197  | pos n ⇒
198    match y with
199    [ OZ ⇒ false
200    | pos m ⇒ leb (succ n) m
201    | neg m ⇒ false ]
202  | neg n ⇒
203    match y with
204    [ OZ ⇒ true
205    | pos m ⇒ true
206    | neg m ⇒ leb (succ m) n ]].
207
208
209
210theorem Zle_to_Zleb_true: ∀n,m. n ≤ m → Zleb n m = true.
211#n #m cases n;cases m; //;
212[ 1,2: #m' normalize; #H @(False_ind ? H)
213| 3,5: #n' #m' normalize; @le_to_leb_true
214| 4: #n' #m' normalize; #H @(False_ind ? H)
215] qed.
216
217theorem Zleb_true_to_Zle: ∀n,m.Zleb n m = true → n ≤ m.
218#n #m cases n;cases m; //;
219[ 1,2: #m' normalize; #H destruct
220| 3,5: #n' #m' normalize; @leb_true_to_le
221| 4: #n' #m' normalize; #H destruct
222] qed.
223
224theorem Zleb_false_to_not_Zle: ∀n,m.Zleb n m = false → n ≰ m.
225#n #m #H % #H' >(Zle_to_Zleb_true … H') in H #H destruct;
226qed.
227
228theorem Zlt_to_Zltb_true: ∀n,m. n < m → Zltb n m = true.
229#n #m cases n;cases m; //;
230[ normalize; #H @(False_ind ? H)
231| 2,3: #m' normalize; #H @(False_ind ? H)
232| 4,6: #n' #m' normalize; @le_to_leb_true
233| #n' #m' normalize; #H @(False_ind ? H)
234] qed.
235
236theorem Zltb_true_to_Zlt: ∀n,m. Zltb n m = true → n < m.
237#n #m cases n;cases m; //;
238[ normalize; #H destruct
239| 2,3: #m' normalize; #H destruct
240| 4,6: #n' #m' @leb_true_to_le
241| #n' #m' normalize; #H destruct
242] qed.
243
244theorem Zltb_false_to_not_Zlt: ∀n,m.Zltb n m = false → n ≮ m.
245#n #m #H % #H' >(Zlt_to_Zltb_true … H') in H #H destruct;
246qed.
247
248theorem Zleb_elim_Type0: ∀n,m:Z. ∀P:bool → Type[0].
249(n ≤ m → P true) → (n ≰ m → P false) → P (Zleb n m).
250#n #m #P #Hle #Hnle
251lapply (refl ? (Zleb n m));
252elim (Zleb n m) in ⊢ ((???%)→%);
253#Hb
254[ @Hle @(Zleb_true_to_Zle … Hb)
255| @Hnle @(Zleb_false_to_not_Zle … Hb)
256] qed.
257
258theorem Zltb_elim_Type0: ∀n,m:Z. ∀P:bool → Type[0].
259(n < m → P true) → (n ≮ m → P false) → P (Zltb n m).
260#n #m #P #Hlt #Hnlt
261lapply (refl ? (Zltb n m));
262elim (Zltb n m) in ⊢ ((???%)→%);
263#Hb
264[ @Hlt @(Zltb_true_to_Zlt … Hb)
265| @Hnlt @(Zltb_false_to_not_Zlt … Hb)
266] qed.
267
268lemma monotonic_Zle_Zsucc: monotonic Z Zle Zsucc.
269#x #y cases x; cases y; /2/;
270#n #m @(pos_cases … n) @(pos_cases … m)
271[ //;
272| #n' /2/;
273| #m' #H @False_ind normalize in H; @(absurd … (not_le_succ_one m')) /2/;
274| #n' #m' #H normalize in H;
275    >(Zsucc_neg_succ n') >(Zsucc_neg_succ m') /2/;
276] qed.
277
278lemma monotonic_Zle_Zpred: monotonic Z Zle Zpred.
279#x #y cases x; cases y;
280[ 1,2,7,8,9: /2/;
281| 3,4: #m normalize; *;
282| #m #n @(pos_cases … n) @(pos_cases … m)
283  [ 1,2: /2/;
284  | #n' normalize; #H @False_ind @(absurd … (not_le_succ_one n')) /2/;
285  | #n' #m' >(Zpred_pos_succ n') >(Zpred_pos_succ m') /2/;
286  ]
287| #m #n normalize; *;
288] qed.
289
290lemma monotonic_Zle_Zplus_r: ∀n.monotonic Z Zle (λm.n + m).
291#n cases n; //; #n' #a #b #H
292[ @(pos_elim … n')
293  [ <(Zsucc_Zplus_pos_O a) <(Zsucc_Zplus_pos_O b) /2/;
294  | #n'' #H >(?:pos (succ n'') = Zsucc (pos n'')) //;
295      >(Zplus_Zsucc …) >(Zplus_Zsucc …) /2/;
296  ]
297| @(pos_elim … n')
298  [ <(Zpred_Zplus_neg_O a) <(Zpred_Zplus_neg_O b) /2/;
299  | #n'' #H >(?:neg (succ n'') = Zpred (neg n'')) //;
300      >(Zplus_Zpred …) >(Zplus_Zpred …) /2/;
301  ]
302] qed.
303
304lemma monotonic_Zle_Zplus_l: ∀n.monotonic Z Zle (λm.m + n).
305/2/; qed.
306
307let rec Z_times (x:Z) (y:Z) : Z ≝
308match x with
309[ OZ ⇒ OZ
310| pos n ⇒
311  match y with
312  [ OZ ⇒ OZ
313  | pos m ⇒ pos (n*m)
314  | neg m ⇒ neg (n*m)
315  ]
316| neg n ⇒
317  match y with
318  [ OZ ⇒ OZ
319  | pos m ⇒ neg (n*m)
320  | neg m ⇒ pos (n*m)
321  ]
322].
323interpretation "integer multiplication" 'times x y = (Z_times x y).
324(* Now in cerco/Util.ma
325(* Borrowed from standard-library/didactic/exercises/duality.ma with precedences tweaked *)
326notation > "'if' term 19 e 'then' term 19 t 'else' term 48 f" non associative with precedence 19 for @{ 'if_then_else $e $t $f }.
327notation < "'if' \nbsp term 19 e \nbsp 'then' \nbsp term 19 t \nbsp 'else' \nbsp term 48 f \nbsp" non associative with precedence 19 for @{ 'if_then_else $e $t $f }.
328interpretation "Formula if_then_else" 'if_then_else e t f = (if_then_else ? e t f).
329*)
330(* datatypes/list.ma *)
331
332theorem nil_append_nil_both:
333  ∀A:Type[0]. ∀l1,l2:list A.
334    l1 @ l2 = [] → l1 = [] ∧ l2 = [].
335#A #l1 #l2 cases l1;
336[ cases l2;
337  [ /2/
338  | #h #t #H destruct;
339  ]
340| cases l2;
341  [ normalize; #h #t #H destruct;
342  | normalize; #h1 #t1 #h2 #h3 #H destruct;
343  ]
344] qed.
345
346(* division *)
347
348inductive natp : Type[0] ≝
349| pzero : natp
350| ppos  : Pos → natp.
351
352definition natp0 ≝
353λn. match n with [ pzero ⇒ pzero | ppos m ⇒ ppos (p0 m) ].
354
355definition natp1 ≝
356λn. match n with [ pzero ⇒ ppos one | ppos m ⇒ ppos (p1 m) ].
357
358let rec divide (m,n:Pos) on m ≝
359match m with
360[ one ⇒
361  match n with
362  [ one ⇒ 〈ppos one,pzero〉
363  | _ ⇒ 〈pzero,ppos one〉
364  ]
365| p0 m' ⇒
366  match divide m' n with
367  [ pair q r ⇒
368    match r with
369    [ pzero ⇒ 〈natp0 q,pzero〉
370    | ppos r' ⇒
371      match partial_minus (p0 r') n with
372      [ MinusNeg ⇒ 〈natp0 q, ppos (p0 r')〉
373      | MinusZero ⇒ 〈natp1 q, pzero〉
374      | MinusPos r'' ⇒ 〈natp1 q, ppos r''〉
375      ]
376    ]
377  ]
378| p1 m' ⇒
379  match divide m' n with
380  [ pair q r ⇒
381    match r with
382    [ pzero ⇒ match n with [ one ⇒ 〈natp1 q,pzero〉 | _ ⇒ 〈natp0 q,ppos one〉 ]
383    | ppos r' ⇒
384      match partial_minus (p1 r') n with
385      [ MinusNeg ⇒ 〈natp0 q, ppos (p1 r')〉
386      | MinusZero ⇒ 〈natp1 q, pzero〉
387      | MinusPos r'' ⇒ 〈natp1 q, ppos r''〉
388      ]
389    ]
390  ]
391].
392
393definition div ≝ λm,n. fst ?? (divide m n).
394definition mod ≝ λm,n. snd ?? (divide m n).
395
396definition pairdisc ≝
397λA,B.λx,y:Prod A B.
398match x with
399[(pair t0 t1) ⇒
400match y with
401[(pair u0 u1) ⇒
402  ∀P: Type[1].
403  (∀e0: (eq A (R0 ? t0) u0).
404   ∀e1: (eq (? u0 e0) (R1 ? t0 ? t1 u0 e0) u1).P) → P ] ].
405
406lemma pairdisc_elim : ∀A,B,x,y.x = y → pairdisc A B x y.
407#A #B #x #y #e >e cases y;
408#a #b normalize;#P #PH @PH %
409qed.
410
411lemma pred_minus: ∀n,m. pred n - m = pred (n-m).
412@pos_elim /3/;
413qed.
414
415lemma minus_plus_distrib: ∀n,m,p:Pos. m-(n+p) = m-n-p.
416@pos_elim
417[ //
418| #n #IH #m #p >(succ_plus_one …) >(IH m one) /2/;
419] qed.
420
421theorem plus_minus_r:
422∀m,n,p:Pos. m < n → p+(n-m) = (p+n)-m.
423#m #n #p #le >(commutative_plus …)
424>(commutative_plus p ?) @plus_minus //; qed.
425
426lemma plus_minus_le: ∀m,n,p:Pos. m≤n → m+p-n≤p.
427#m #n #p elim m;/2/; qed.
428(*
429lemma le_to_minus: ∀m,n. m≤n → m-n = 0.
430#m #n elim n;
431[ <(minus_n_O …) /2/;
432| #n' #IH #le inversion le; /2/; #n'' #A #B #C destruct;
433    >(eq_minus_S_pred …) >(IH A) /2/
434] qed.
435*)
436lemma minus_times_distrib_l: ∀n,m,p:Pos. n < m → p*m-p*n = p*(m-n).
437#n #m #p (*elim (decidable_lt n m);*)#lt
438(*[*) @(pos_elim … p) //;#p' #IH
439    <(times_succn_m …) <(times_succn_m …) <(times_succn_m …)
440    >(minus_plus_distrib …)
441    <(plus_minus … lt) <IH
442    >(plus_minus_r …) /2/;
443qed.
444(*|
445lapply (not_lt_to_le … lt); #le
446@(pos_elim … p) //; #p'
447 cut (m-n = one); [ /3/ ]
448  #mn >mn >(times_n_one …) >(times_n_one …)
449  #H <H in ⊢ (???%)
450  @sym_eq @le_n_one_to_eq <H
451  >(minus_plus_distrib …) @monotonic_le_minus_l
452  /2/;
453] qed.
454
455lemma S_pred_m_n: ∀m,n. m > n → S (pred (m-n)) = m-n.
456#m #n #H lapply (refl ? (m-n));elim (m-n) in ⊢ (???% → %);//;
457#H' lapply (minus_to_plus … H'); /2/;
458<(plus_n_O …) #H'' >H'' in H #H
459@False_ind @(absurd ? H ( not_le_Sn_n n))
460qed.
461*)
462
463let rec natp_plus (n,m:natp) ≝
464match n with
465[ pzero ⇒ m
466| ppos n' ⇒ match m with [ pzero ⇒ n | ppos m' ⇒ ppos (n'+m') ]
467].
468
469let rec natp_times (n,m:natp) ≝
470match n with
471[ pzero ⇒ pzero
472| ppos n' ⇒ match m with [ pzero ⇒ pzero | ppos m' ⇒ ppos (n'*m') ]
473].
474
475inductive natp_lt : natp → Pos → Prop ≝
476| plt_zero : ∀n. natp_lt pzero n
477| plt_pos : ∀n,m. n < m → natp_lt (ppos n) m.
478
479lemma lt_p0: ∀n:Pos. one < p0 n.
480#n normalize; /2/; qed.
481
482lemma lt_p1: ∀n:Pos. one < p1 n.
483#n' normalize; >(?:p1 n' = succ (p0 n')) //; qed.
484
485lemma divide_by_one: ∀m. divide m one = 〈ppos m,pzero〉.
486#m elim m;
487[ //;
488| 2,3: #m' #IH normalize; >IH //;
489] qed.
490
491lemma pos_nonzero2: ∀n. ∀P:Pos→Type[0]. ∀Q:Type[0]. match succ n with [ one ⇒ Q | p0 p ⇒ P (p0 p) | p1 p ⇒ P (p1 p) ] = P (succ n).
492#n #P #Q @succ_elim /2/; qed.
493
494lemma partial_minus_to_Prop: ∀n,m.
495  match partial_minus n m with
496  [ MinusNeg ⇒ n < m
497  | MinusZero ⇒ n = m
498  | MinusPos r ⇒ n = m+r
499  ].
500#n #m lapply (pos_compare_to_Prop n m); lapply (minus_to_plus n m);
501normalize; cases (partial_minus n m); /2/; qed.
502
503lemma double_lt1: ∀n,m:Pos. n<m → p1 n < p0 m.
504#n #m #lt elim lt; /2/;
505qed.
506
507lemma double_lt2: ∀n,m:Pos. n<m → p1 n < p1 m.
508#n #m #lt @(transitive_lt ? (p0 m) ?) /2/;
509qed.
510
511lemma double_lt3: ∀n,m:Pos. n<succ m → p0 n < p1 m.
512#n #m #lt inversion lt;
513[ #H >(succ_injective … H) //;
514| #p #H1 #H2 #H3 >(succ_injective … H3)
515    @(transitive_lt ? (p0 p) ?) /2/;
516]
517qed.
518
519lemma double_lt4: ∀n,m:Pos. n<m → p0 n < p0 m.
520#n #m #lt elim lt; /2/;
521qed.
522
523
524
525lemma plt_lt: ∀n,m:Pos. natp_lt (ppos n) m → n<m.
526#n #m #lt inversion lt;
527[ #p #H destruct;
528| #n' #m' #lt #e1 #e2 destruct @lt
529] qed.
530
531lemma lt_foo: ∀a,b:Pos. b+a < p0 b → a<b.
532#a #b /2/; qed.
533
534lemma lt_foo2: ∀a,b:Pos. b+a < p1 b → a<succ b.
535#a #b >(?:p1 b = succ (p0 b)) /2/; qed.
536
537lemma p0_plus: ∀n,m:Pos. p0 (n+m) = p0 n + p0 m.
538/2/; qed.
539
540lemma pair_eq1: ∀A,B. ∀a1,a2:A. ∀b1,b2:B. 〈a1,b1〉 = 〈a2,b2〉 → a1 = a2.
541#A #B #a1 #a2 #b1 #b2 #H destruct //
542qed.
543
544lemma pair_eq2: ∀A,B. ∀a1,a2:A. ∀b1,b2:B. 〈a1,b1〉 = 〈a2,b2〉 → b1 = b2.
545#A #B #a1 #a2 #b1 #b2 #H destruct //
546qed.
547
548theorem divide_ok : ∀m,n,dv,md.
549 divide m n = 〈dv,md〉 →
550 ppos m = natp_plus (natp_times dv (ppos n)) md ∧ natp_lt md n.
551#m #n @(pos_cases … n)
552[ >(divide_by_one m) #dv #md #H destruct /2/
553| #n' elim m;
554  [ #dv #md normalize; >(pos_nonzero …) #H destruct /3/
555  | #m' #IH #dv #md whd in ⊢ (??%? → ?(???%)?);
556      lapply (refl ? (divide m' (succ n')));
557      elim (divide m' (succ n')) in ⊢ (???% → % → ?);
558      #dv' #md' #DIVr elim (IH … DIVr);
559      whd in ⊢ (? → ? → ??%? → ?);
560      cases md';
561      [ cases dv'; normalize;
562        [ #H destruct
563        | #dv'' #Hr1 #Hr2 >(pos_nonzero …) #H destruct % /2/
564        ]
565      | cases dv'; [ 2: #dv'' ] @succ_elim
566          normalize; #n #md'' #Hr1 #Hr2
567          lapply (plt_lt … Hr2); #Hr2'
568          lapply (partial_minus_to_Prop md'' n);
569          cases (partial_minus md'' n); [ 3,6,9,12: #r'' ] normalize
570          #lt #e destruct % [ /3/ | *: /2/ ] @plt_pos
571          [ 1,3,5,7: @double_lt1 /2/;
572          | 2,4: @double_lt3 /2/;
573          | *: @double_lt2 /2/;
574          ]
575      ]
576  | #m' #IH #dv #md whd in ⊢ (??%? → ?);
577      lapply (refl ? (divide m' (succ n')));
578      elim (divide m' (succ n')) in ⊢ (???% → % → ?);
579      #dv' #md' #DIVr elim (IH … DIVr);
580      whd in ⊢ (? → ? → ??%? → ?); cases md';
581      [ cases dv'; normalize;
582        [ #H destruct;
583        | #dv'' #Hr1 #Hr2 #H destruct; /3/;
584        ]
585      | (*cases dv'; [ 2: #dv'' ] @succ_elim
586          normalize; #n #md'' #Hr1 #Hr2 *) #md'' #Hr1 #Hr2
587          lapply (plt_lt … Hr2); #Hr2'
588          whd in ⊢ (??%? → ?);
589          lapply (partial_minus_to_Prop (p0 md'') (succ n'));
590          cases (partial_minus (p0 md'') (succ n')); [ 3(*,6,9,12*): #r'' ]
591          cases dv' in Hr1 ⊢ %; [ 2,4,6: #dv'' ] normalize
592          #Hr1 destruct [ 1,2,3: >(p0_plus ? md'') ]
593          #lt #e [ 1,3,4,6: >lt ]
594          <(pair_eq1 ?????? e) <(pair_eq2 ?????? e)
595          normalize in ⊢ (?(???%)?); % /2/; @plt_pos
596          [ cut (succ n' + r'' < p0 (succ n')); /2/;
597          | cut (succ n' + r'' < p0 (succ n')); /2/;
598          | /2/;
599          | /2/;
600          ]
601      ]
602  ]
603] qed.
604
605lemma mod0_divides: ∀m,n,dv:Pos.
606  divide n m = 〈ppos dv,pzero〉 → m∣n.
607#m #n #dv #DIVIDE %{dv} lapply (divide_ok … DIVIDE) *; (* XXX: need ; or it eats normalize *)
608normalize #H destruct //
609qed.
610
611lemma divides_mod0: ∀dv,m,n:Pos.
612  n = dv*m → divide n m = 〈ppos dv,pzero〉.
613#dv #m #n #DIV lapply (refl ? (divide n m));
614elim (divide n m) in ⊢ (???% → ?); #dv' #md' #DIVIDE
615lapply (divide_ok … DIVIDE); *;
616cases dv' in DIVIDE ⊢ %; [ 2: #dv'' ]
617cases md'; [ 2,4: #md'' ] #DIVIDE  normalize;
618>DIV in ⊢ (% → ?) #H #lt destruct;
619[ lapply (plus_to_minus … e0);
620    >(commutative_times …) >(commutative_times dv'' …)
621    cut (dv'' < dv); [ cut (dv''*m < dv*m); /2/; ] #dvlt
622    >(minus_times_distrib_l …) //;
623
624 (*cut (0 < dv-dv'); [ lapply (not_le_to_lt … nle); /2/ ]
625    #Hdv *) #H' cut (md'' ≥ m); /2/; lapply (plt_lt … lt); #A #B @False_ind
626    @(absurd ? B (lt_to_not_le … A))
627
628| @False_ind @(absurd ? (plt_lt … lt) ?) /2/;
629
630| >DIVIDE cut (dv = dv''); /2/;
631]
632qed.
633
634lemma dec_divides: ∀m,n:Pos. (m∣n) + ¬(m∣n).
635#m #n lapply (refl ? (divide n m)); elim (divide n m) in ⊢ (???% → %);
636#dv #md cases md; cases dv;
637[ #DIVIDES lapply (divide_ok … DIVIDES); *; normalize; #H destruct
638| #dv' #H %1 @mod0_divides /2/;
639| #md' #DIVIDES %2 @nmk *; #dv'
640    >(commutative_times …) #H lapply (divides_mod0 … H);
641    >DIVIDES #H'
642    destruct;
643| #md' #dv' #DIVIDES %2 @nmk *; #dv'
644    >(commutative_times …) #H lapply (divides_mod0 … H);
645    >DIVIDES #H'
646    destruct;
647] qed.
648
649theorem dec_dividesZ: ∀p,q:Z. (p∣q) + ¬(p∣q).
650#p #q cases p;
651[ cases q; normalize; [ %2 /2/; | *: #m %2 /2/; ]
652| *: #n cases q; normalize; /2/;
653] qed.
654
655definition natp_to_Z ≝
656λn. match n with [ pzero ⇒ OZ | ppos p ⇒ pos p ].
657
658definition natp_to_negZ ≝
659λn. match n with [ pzero ⇒ OZ | ppos p ⇒ neg p ].
660
661(* TODO: check these definitions are right.  They are supposed to be the same
662   as Zdiv/Zmod in Coq. *)
663definition divZ ≝ λx,y.
664  match x with
665  [ OZ ⇒ OZ
666  | pos n ⇒
667    match y with
668    [ OZ ⇒ OZ
669    | pos m ⇒ natp_to_Z (fst ?? (divide n m))
670    | neg m ⇒ match divide n m with [ pair q r ⇒
671                match r with [ pzero ⇒ natp_to_negZ q | _ ⇒ Zpred (natp_to_negZ q) ] ]
672    ]
673  | neg n ⇒
674    match y with
675    [ OZ ⇒ OZ
676    | pos m ⇒ match divide n m with [ pair q r ⇒
677                match r with [ pzero ⇒ natp_to_negZ q | _ ⇒ Zpred (natp_to_negZ q) ] ]
678    | neg m ⇒ natp_to_Z (fst ?? (divide n m))
679    ]
680  ].
681
682definition modZ ≝ λx,y.
683  match x with
684  [ OZ ⇒ OZ
685  | pos n ⇒
686    match y with
687    [ OZ ⇒ OZ
688    | pos m ⇒ natp_to_Z (snd ?? (divide n m))
689    | neg m ⇒ match divide n m with [ pair q r ⇒
690                match r with [ pzero ⇒ OZ | _ ⇒ y+(natp_to_Z r) ] ]
691    ]
692  | neg n ⇒
693    match y with
694    [ OZ ⇒ OZ
695    | pos m ⇒ match divide n m with [ pair q r ⇒
696                match r with [ pzero ⇒ OZ | _ ⇒ y-(natp_to_Z r) ] ]
697    | neg m ⇒ natp_to_Z (snd ?? (divide n m))
698    ]
699  ].
700
701interpretation "natural division" 'divide m n = (div m n).
702interpretation "natural modulus" 'module m n = (mod m n).
703interpretation "integer division" 'divide m n = (divZ m n).
704interpretation "integer modulus" 'module m n = (modZ m n).
705
706lemma Zminus_Zlt: ∀x,y:Z. y>0 → x-y < x.
707#x #y cases y;
708[ #H @(False_ind … H)
709| #m #_ cases x; //; #n
710    whd in ⊢ (?%?);
711    lapply (pos_compare_to_Prop n m);
712    cases (pos_compare n m); /2/
713    #lt whd <(minus_Sn_m … lt) /2/;
714| #m #H @(False_ind … H)
715] qed.
716
717lemma pos_compare_lt: ∀n,m:Pos. n<m → pos_compare n m = LT.
718#n #m #lt lapply (pos_compare_to_Prop n m); cases (pos_compare n m);
719[ //;
720| 2,3: #H @False_ind @(absurd ? lt ?) /3/;
721] qed.
722
723theorem modZ_lt_mod: ∀x,y:Z. y>0 → 0 ≤ x \mod y ∧ x \mod y < y.
724#x #y cases y;
725[ #H @(False_ind … H)
726| #m #_ cases x;
727  [ % //;
728  | #n whd in ⊢ (?(??%)(?%?)); lapply (refl ? (divide n m));
729      cases (divide n m) in ⊢ (???% → %); #dv #md #H
730      elim (divide_ok … H); #e #l elim l; /2/;
731  | #n whd in ⊢ (?(??%)(?%?)); lapply (refl ? (divide n m));
732      cases (divide n m) in ⊢ (???% → %); #dv #md #H
733      elim (divide_ok … H); #e #l elim l;
734      [ /2/;
735      | #md' #m' #l' %
736        [ whd in ⊢ (??%); >(pos_compare_n_m_m_n …)
737            >(pos_compare_lt … l') //;
738        | @Zminus_Zlt //;
739        ]
740      ]
741  ]
742| #m #H @(False_ind … H)
743] qed.
Note: See TracBrowser for help on using the repository browser.