1 (* Title: Chinese.ML |
|
2 ID: $Id$ |
|
3 Author: Thomas M. Rasmussen |
|
4 Copyright 2000 University of Cambridge |
|
5 |
|
6 The Chinese Remainder Theorem for an arbitrary finite number of equations. |
|
7 (The one-equation case is included in 'IntPrimes') |
|
8 |
|
9 Uses functions for indexing. Maybe 'funprod' and 'funsum' |
|
10 should be based on general 'fold' on indices? |
|
11 *) |
|
12 |
|
13 |
|
14 (*** funprod and funsum ***) |
|
15 |
|
16 Goal "(ALL i. i <= n --> #0 < mf i) --> #0 < funprod mf 0 n"; |
|
17 by (induct_tac "n" 1); |
|
18 by Auto_tac; |
|
19 by (asm_full_simp_tac (simpset() addsimps [int_0_less_mult_iff]) 1); |
|
20 qed_spec_mp "funprod_pos"; |
|
21 |
|
22 Goal "(ALL i. k<=i & i<=(k+l) --> zgcd (mf i, mf m) = #1) --> \ |
|
23 \ zgcd (funprod mf k l, mf m) = #1"; |
|
24 by (induct_tac "l" 1); |
|
25 by (ALLGOALS Simp_tac); |
|
26 by (REPEAT (rtac impI 1)); |
|
27 by (stac zgcd_zmult_cancel 1); |
|
28 by Auto_tac; |
|
29 qed_spec_mp "funprod_zgcd"; |
|
30 |
|
31 Goal "k<=i --> i<=(k+l) --> (mf i) dvd (funprod mf k l)"; |
|
32 by (induct_tac "l" 1); |
|
33 by Auto_tac; |
|
34 by (rtac zdvd_zmult2 2); |
|
35 by (rtac zdvd_zmult 3); |
|
36 by (subgoal_tac "i=k" 1); |
|
37 by (subgoal_tac "i=Suc (k + n)" 3); |
|
38 by (ALLGOALS Asm_simp_tac); |
|
39 qed_spec_mp "funprod_zdvd"; |
|
40 |
|
41 Goal "(funsum f k l) mod m = (funsum (%i. (f i) mod m) k l) mod m"; |
|
42 by (induct_tac "l" 1); |
|
43 by Auto_tac; |
|
44 by (rtac trans 1); |
|
45 by (rtac zmod_zadd1_eq 1); |
|
46 by (Asm_simp_tac 1); |
|
47 by (rtac (zmod_zadd_right_eq RS sym) 1); |
|
48 qed "funsum_mod"; |
|
49 |
|
50 Goal "(ALL i. k<=i & i<=(k+l) --> (f i) = #0) --> (funsum f k l) = #0"; |
|
51 by (induct_tac "l" 1); |
|
52 by Auto_tac; |
|
53 qed_spec_mp "funsum_zero"; |
|
54 |
|
55 Goal "k<=j --> j<=(k+l) --> \ |
|
56 \ (ALL i. k<=i & i<=(k+l) & i~=j --> (f i) = #0) --> \ |
|
57 \ (funsum f k l) = (f j)"; |
|
58 by (induct_tac "l" 1); |
|
59 by (ALLGOALS Clarify_tac); |
|
60 by (subgoal_tac "k=j" 1 THEN ALLGOALS Asm_simp_tac); |
|
61 by (case_tac "Suc (k+n) = j" 1); |
|
62 by (subgoal_tac "funsum f k n = #0" 1); |
|
63 by (rtac funsum_zero 2); |
|
64 by (subgoal_tac "f (Suc (k+n)) = #0" 3); |
|
65 by (subgoal_tac "j<=k+n" 3); |
|
66 by (arith_tac 4); |
|
67 by Auto_tac; |
|
68 qed_spec_mp "funsum_oneelem"; |
|
69 |
|
70 |
|
71 (*** Chinese: Uniqueness ***) |
|
72 |
|
73 Goalw [m_cond_def,km_cond_def,lincong_sol_def] |
|
74 "[| m_cond n mf; km_cond n kf mf; \ |
|
75 \ lincong_sol n kf bf mf x; lincong_sol n kf bf mf y |] \ |
|
76 \ ==> [x=y] (mod mf n)"; |
|
77 by (rtac iffD1 1); |
|
78 by (res_inst_tac [("k","kf n")] zcong_cancel2 1); |
|
79 by (res_inst_tac [("b","bf n")] zcong_trans 3); |
|
80 by (stac zcong_sym 4); |
|
81 by (rtac order_less_imp_le 1); |
|
82 by (ALLGOALS Asm_simp_tac); |
|
83 val lemma = result(); |
|
84 |
|
85 Goal "m_cond n mf --> km_cond n kf mf --> \ |
|
86 \ lincong_sol n kf bf mf x --> lincong_sol n kf bf mf y --> \ |
|
87 \ [x=y] (mod funprod mf 0 n)"; |
|
88 by (induct_tac "n" 1); |
|
89 by (ALLGOALS Simp_tac); |
|
90 by (blast_tac (claset() addIs [lemma]) 1); |
|
91 by (REPEAT (rtac impI 1)); |
|
92 by (rtac zcong_zgcd_zmult_zmod 1); |
|
93 by (blast_tac (claset() addIs [lemma]) 1); |
|
94 by (stac zgcd_commute 2); |
|
95 by (rtac funprod_zgcd 2); |
|
96 by (auto_tac (claset(), |
|
97 simpset() addsimps [m_cond_def,km_cond_def,lincong_sol_def])); |
|
98 qed_spec_mp "zcong_funprod"; |
|
99 |
|
100 |
|
101 (* Chinese: Existence *) |
|
102 |
|
103 Goal "[| 0<n; i<=n; m_cond n mf; km_cond n kf mf |] \ |
|
104 \ ==> EX! x. #0<=x & x<(mf i) & \ |
|
105 \ [(kf i)*(mhf mf n i)*x = bf i] (mod mf i)"; |
|
106 by (rtac zcong_lineq_unique 1); |
|
107 by (stac zgcd_zmult_cancel 2); |
|
108 by (rewrite_goals_tac [m_cond_def,km_cond_def,mhf_def]); |
|
109 by (ALLGOALS Asm_simp_tac); |
|
110 by Safe_tac; |
|
111 by (stac zgcd_zmult_cancel 3); |
|
112 by (ALLGOALS (rtac funprod_zgcd)); |
|
113 by Safe_tac; |
|
114 by (ALLGOALS Asm_full_simp_tac); |
|
115 by (subgoal_tac "ia<=n" 3); |
|
116 by (arith_tac 4); |
|
117 by (subgoal_tac "i<n" 1); |
|
118 by (arith_tac 2); |
|
119 by (case_tac "i" 2); |
|
120 by (ALLGOALS Asm_full_simp_tac); |
|
121 qed "unique_xi_sol"; |
|
122 |
|
123 Goalw [mhf_def] "[| 0<n; i<=n; j<=n; j~=i |] ==> (mf j) dvd (mhf mf n i)"; |
|
124 by (case_tac "i=0" 1); |
|
125 by (case_tac "i=n" 2); |
|
126 by (ALLGOALS Asm_simp_tac); |
|
127 by (case_tac "j<i" 3); |
|
128 by (rtac zdvd_zmult2 3); |
|
129 by (rtac zdvd_zmult 4); |
|
130 by (ALLGOALS (rtac funprod_zdvd)); |
|
131 by (ALLGOALS arith_tac); |
|
132 val lemma = result(); |
|
133 |
|
134 Goalw [x_sol_def] |
|
135 "[| 0<n; i<=n |] \ |
|
136 \ ==> (x_sol n kf bf mf) mod (mf i) = \ |
|
137 \ (xilin_sol i n kf bf mf)*(mhf mf n i) mod (mf i)"; |
|
138 by (stac funsum_mod 1); |
|
139 by (stac funsum_oneelem 1); |
|
140 by Auto_tac; |
|
141 by (stac (zdvd_iff_zmod_eq_0 RS sym) 1); |
|
142 by (rtac zdvd_zmult 1); |
|
143 by (rtac lemma 1); |
|
144 by Auto_tac; |
|
145 qed "x_sol_lin"; |
|
146 |
|
147 |
|
148 (* Chinese *) |
|
149 |
|
150 Goal "[| 0<n; m_cond n mf; km_cond n kf mf |] \ |
|
151 \ ==> (EX! x. #0 <= x & x < (funprod mf 0 n) & \ |
|
152 \ (lincong_sol n kf bf mf x))"; |
|
153 by Safe_tac; |
|
154 by (res_inst_tac [("m","funprod mf 0 n")] zcong_zless_imp_eq 2); |
|
155 by (rtac zcong_funprod 6); |
|
156 by Auto_tac; |
|
157 by (res_inst_tac [("x","(x_sol n kf bf mf) mod (funprod mf 0 n)")] exI 1); |
|
158 by (rewtac lincong_sol_def); |
|
159 by Safe_tac; |
|
160 by (stac zcong_zmod 3); |
|
161 by (stac zmod_zmult_distrib 3); |
|
162 by (stac zmod_zdvd_zmod 3); |
|
163 by (stac x_sol_lin 5); |
|
164 by (stac (zmod_zmult_distrib RS sym) 7); |
|
165 by (stac (zcong_zmod RS sym) 7); |
|
166 by (subgoal_tac "#0<=(xilin_sol i n kf bf mf) & \ |
|
167 \ (xilin_sol i n kf bf mf)<(mf i) & \ |
|
168 \ [(kf i)*(mhf mf n i)*(xilin_sol i n kf bf mf) = bf i] \ |
|
169 \ (mod mf i)" 7); |
|
170 by (asm_full_simp_tac (simpset() addsimps zmult_ac) 7); |
|
171 by (rewtac xilin_sol_def); |
|
172 by (Asm_simp_tac 7); |
|
173 by (rtac (ex1_implies_ex RS someI_ex) 7); |
|
174 by (rtac unique_xi_sol 7); |
|
175 by (rtac funprod_zdvd 4); |
|
176 by (rewtac m_cond_def); |
|
177 by (rtac (funprod_pos RS pos_mod_sign) 1); |
|
178 by (rtac (funprod_pos RS pos_mod_bound) 2); |
|
179 by Auto_tac; |
|
180 qed "chinese_remainder"; |
|