src/HOL/Old_Number_Theory/Chinese.thy
 author blanchet Wed Sep 24 15:45:55 2014 +0200 (2014-09-24) changeset 58425 246985c6b20b parent 57514 bdc2c6b40bf2 child 58889 5b7a9633cfa8 permissions -rw-r--r--
simpler proof
```     1 (*  Title:      HOL/Old_Number_Theory/Chinese.thy
```
```     2     Author:     Thomas M. Rasmussen
```
```     3     Copyright   2000  University of Cambridge
```
```     4 *)
```
```     5
```
```     6 header {* The Chinese Remainder Theorem *}
```
```     7
```
```     8 theory Chinese
```
```     9 imports IntPrimes
```
```    10 begin
```
```    11
```
```    12 text {*
```
```    13   The Chinese Remainder Theorem for an arbitrary finite number of
```
```    14   equations.  (The one-equation case is included in theory @{text
```
```    15   IntPrimes}.  Uses functions for indexing.\footnote{Maybe @{term
```
```    16   funprod} and @{term funsum} should be based on general @{term fold}
```
```    17   on indices?}
```
```    18 *}
```
```    19
```
```    20
```
```    21 subsection {* Definitions *}
```
```    22
```
```    23 primrec funprod :: "(nat => int) => nat => nat => int"
```
```    24 where
```
```    25   "funprod f i 0 = f i"
```
```    26 | "funprod f i (Suc n) = f (Suc (i + n)) * funprod f i n"
```
```    27
```
```    28 primrec funsum :: "(nat => int) => nat => nat => int"
```
```    29 where
```
```    30   "funsum f i 0 = f i"
```
```    31 | "funsum f i (Suc n) = f (Suc (i + n)) + funsum f i n"
```
```    32
```
```    33 definition
```
```    34   m_cond :: "nat => (nat => int) => bool" where
```
```    35   "m_cond n mf =
```
```    36     ((\<forall>i. i \<le> n --> 0 < mf i) \<and>
```
```    37       (\<forall>i j. i \<le> n \<and> j \<le> n \<and> i \<noteq> j --> zgcd (mf i) (mf j) = 1))"
```
```    38
```
```    39 definition
```
```    40   km_cond :: "nat => (nat => int) => (nat => int) => bool" where
```
```    41   "km_cond n kf mf = (\<forall>i. i \<le> n --> zgcd (kf i) (mf i) = 1)"
```
```    42
```
```    43 definition
```
```    44   lincong_sol ::
```
```    45     "nat => (nat => int) => (nat => int) => (nat => int) => int => bool" where
```
```    46   "lincong_sol n kf bf mf x = (\<forall>i. i \<le> n --> zcong (kf i * x) (bf i) (mf i))"
```
```    47
```
```    48 definition
```
```    49   mhf :: "(nat => int) => nat => nat => int" where
```
```    50   "mhf mf n i =
```
```    51     (if i = 0 then funprod mf (Suc 0) (n - Suc 0)
```
```    52      else if i = n then funprod mf 0 (n - Suc 0)
```
```    53      else funprod mf 0 (i - Suc 0) * funprod mf (Suc i) (n - Suc 0 - i))"
```
```    54
```
```    55 definition
```
```    56   xilin_sol ::
```
```    57     "nat => nat => (nat => int) => (nat => int) => (nat => int) => int" where
```
```    58   "xilin_sol i n kf bf mf =
```
```    59     (if 0 < n \<and> i \<le> n \<and> m_cond n mf \<and> km_cond n kf mf then
```
```    60         (SOME x. 0 \<le> x \<and> x < mf i \<and> zcong (kf i * mhf mf n i * x) (bf i) (mf i))
```
```    61      else 0)"
```
```    62
```
```    63 definition
```
```    64   x_sol :: "nat => (nat => int) => (nat => int) => (nat => int) => int" where
```
```    65   "x_sol n kf bf mf = funsum (\<lambda>i. xilin_sol i n kf bf mf * mhf mf n i) 0 n"
```
```    66
```
```    67
```
```    68 text {* \medskip @{term funprod} and @{term funsum} *}
```
```    69
```
```    70 lemma funprod_pos: "(\<forall>i. i \<le> n --> 0 < mf i) ==> 0 < funprod mf 0 n"
```
```    71 by (induct n) auto
```
```    72
```
```    73 lemma funprod_zgcd [rule_format (no_asm)]:
```
```    74   "(\<forall>i. k \<le> i \<and> i \<le> k + l --> zgcd (mf i) (mf m) = 1) -->
```
```    75     zgcd (funprod mf k l) (mf m) = 1"
```
```    76   apply (induct l)
```
```    77    apply simp_all
```
```    78   apply (rule impI)+
```
```    79   apply (subst zgcd_zmult_cancel)
```
```    80   apply auto
```
```    81   done
```
```    82
```
```    83 lemma funprod_zdvd [rule_format]:
```
```    84     "k \<le> i --> i \<le> k + l --> mf i dvd funprod mf k l"
```
```    85   apply (induct l)
```
```    86    apply auto
```
```    87   apply (subgoal_tac "i = Suc (k + l)")
```
```    88    apply (simp_all (no_asm_simp))
```
```    89   done
```
```    90
```
```    91 lemma funsum_mod:
```
```    92     "funsum f k l mod m = funsum (\<lambda>i. (f i) mod m) k l mod m"
```
```    93   apply (induct l)
```
```    94    apply auto
```
```    95   apply (rule trans)
```
```    96    apply (rule mod_add_eq)
```
```    97   apply simp
```
```    98   apply (rule mod_add_right_eq [symmetric])
```
```    99   done
```
```   100
```
```   101 lemma funsum_zero [rule_format (no_asm)]:
```
```   102     "(\<forall>i. k \<le> i \<and> i \<le> k + l --> f i = 0) --> (funsum f k l) = 0"
```
```   103   apply (induct l)
```
```   104    apply auto
```
```   105   done
```
```   106
```
```   107 lemma funsum_oneelem [rule_format (no_asm)]:
```
```   108   "k \<le> j --> j \<le> k + l -->
```
```   109     (\<forall>i. k \<le> i \<and> i \<le> k + l \<and> i \<noteq> j --> f i = 0) -->
```
```   110     funsum f k l = f j"
```
```   111   apply (induct l)
```
```   112    prefer 2
```
```   113    apply clarify
```
```   114    defer
```
```   115    apply clarify
```
```   116    apply (subgoal_tac "k = j")
```
```   117     apply (simp_all (no_asm_simp))
```
```   118   apply (case_tac "Suc (k + l) = j")
```
```   119    apply (subgoal_tac "funsum f k l = 0")
```
```   120     apply (rule_tac [2] funsum_zero)
```
```   121     apply (subgoal_tac [3] "f (Suc (k + l)) = 0")
```
```   122      apply (subgoal_tac [3] "j \<le> k + l")
```
```   123       prefer 4
```
```   124       apply arith
```
```   125      apply auto
```
```   126   done
```
```   127
```
```   128
```
```   129 subsection {* Chinese: uniqueness *}
```
```   130
```
```   131 lemma zcong_funprod_aux:
```
```   132   "m_cond n mf ==> km_cond n kf mf
```
```   133     ==> lincong_sol n kf bf mf x ==> lincong_sol n kf bf mf y
```
```   134     ==> [x = y] (mod mf n)"
```
```   135   apply (unfold m_cond_def km_cond_def lincong_sol_def)
```
```   136   apply (rule iffD1)
```
```   137    apply (rule_tac k = "kf n" in zcong_cancel2)
```
```   138     apply (rule_tac [3] b = "bf n" in zcong_trans)
```
```   139      prefer 4
```
```   140      apply (subst zcong_sym)
```
```   141      defer
```
```   142      apply (rule order_less_imp_le)
```
```   143      apply simp_all
```
```   144   done
```
```   145
```
```   146 lemma zcong_funprod [rule_format]:
```
```   147   "m_cond n mf --> km_cond n kf mf -->
```
```   148     lincong_sol n kf bf mf x --> lincong_sol n kf bf mf y -->
```
```   149     [x = y] (mod funprod mf 0 n)"
```
```   150   apply (induct n)
```
```   151    apply (simp_all (no_asm))
```
```   152    apply (blast intro: zcong_funprod_aux)
```
```   153   apply (rule impI)+
```
```   154   apply (rule zcong_zgcd_zmult_zmod)
```
```   155     apply (blast intro: zcong_funprod_aux)
```
```   156     prefer 2
```
```   157     apply (subst zgcd_commute)
```
```   158     apply (rule funprod_zgcd)
```
```   159    apply (auto simp add: m_cond_def km_cond_def lincong_sol_def)
```
```   160   done
```
```   161
```
```   162
```
```   163 subsection {* Chinese: existence *}
```
```   164
```
```   165 lemma unique_xi_sol:
```
```   166   "0 < n ==> i \<le> n ==> m_cond n mf ==> km_cond n kf mf
```
```   167     ==> \<exists>!x. 0 \<le> x \<and> x < mf i \<and> [kf i * mhf mf n i * x = bf i] (mod mf i)"
```
```   168   apply (rule zcong_lineq_unique)
```
```   169    apply (tactic {* stac @{thm zgcd_zmult_cancel} 2 *})
```
```   170     apply (unfold m_cond_def km_cond_def mhf_def)
```
```   171     apply (simp_all (no_asm_simp))
```
```   172   apply safe
```
```   173     apply (tactic {* stac @{thm zgcd_zmult_cancel} 3 *})
```
```   174      apply (rule_tac [!] funprod_zgcd)
```
```   175      apply safe
```
```   176      apply simp_all
```
```   177    apply (subgoal_tac "ia<n")
```
```   178     prefer 2
```
```   179     apply arith
```
```   180    apply (case_tac [2] i)
```
```   181     apply simp_all
```
```   182   done
```
```   183
```
```   184 lemma x_sol_lin_aux:
```
```   185     "0 < n ==> i \<le> n ==> j \<le> n ==> j \<noteq> i ==> mf j dvd mhf mf n i"
```
```   186   apply (unfold mhf_def)
```
```   187   apply (case_tac "i = 0")
```
```   188    apply (case_tac [2] "i = n")
```
```   189     apply (simp_all (no_asm_simp))
```
```   190     apply (case_tac [3] "j < i")
```
```   191      apply (rule_tac [3] dvd_mult2)
```
```   192      apply (rule_tac [4] dvd_mult)
```
```   193      apply (rule_tac [!] funprod_zdvd)
```
```   194      apply arith
```
```   195      apply arith
```
```   196      apply arith
```
```   197      apply arith
```
```   198      apply arith
```
```   199      apply arith
```
```   200      apply arith
```
```   201      apply arith
```
```   202   done
```
```   203
```
```   204 lemma x_sol_lin:
```
```   205   "0 < n ==> i \<le> n
```
```   206     ==> x_sol n kf bf mf mod mf i =
```
```   207       xilin_sol i n kf bf mf * mhf mf n i mod mf i"
```
```   208   apply (unfold x_sol_def)
```
```   209   apply (subst funsum_mod)
```
```   210   apply (subst funsum_oneelem)
```
```   211      apply auto
```
```   212   apply (subst dvd_eq_mod_eq_0 [symmetric])
```
```   213   apply (rule dvd_mult)
```
```   214   apply (rule x_sol_lin_aux)
```
```   215   apply auto
```
```   216   done
```
```   217
```
```   218
```
```   219 subsection {* Chinese *}
```
```   220
```
```   221 lemma chinese_remainder:
```
```   222   "0 < n ==> m_cond n mf ==> km_cond n kf mf
```
```   223     ==> \<exists>!x. 0 \<le> x \<and> x < funprod mf 0 n \<and> lincong_sol n kf bf mf x"
```
```   224   apply safe
```
```   225    apply (rule_tac [2] m = "funprod mf 0 n" in zcong_zless_imp_eq)
```
```   226        apply (rule_tac [6] zcong_funprod)
```
```   227           apply auto
```
```   228   apply (rule_tac x = "x_sol n kf bf mf mod funprod mf 0 n" in exI)
```
```   229   apply (unfold lincong_sol_def)
```
```   230   apply safe
```
```   231     apply (tactic {* stac @{thm zcong_zmod} 3 *})
```
```   232     apply (tactic {* stac @{thm mod_mult_eq} 3 *})
```
```   233     apply (tactic {* stac @{thm mod_mod_cancel} 3 *})
```
```   234       apply (tactic {* stac @{thm x_sol_lin} 4 *})
```
```   235         apply (tactic {* stac (@{thm mod_mult_eq} RS sym) 6 *})
```
```   236         apply (tactic {* stac (@{thm zcong_zmod} RS sym) 6 *})
```
```   237         apply (subgoal_tac [6]
```
```   238           "0 \<le> xilin_sol i n kf bf mf \<and> xilin_sol i n kf bf mf < mf i
```
```   239           \<and> [kf i * mhf mf n i * xilin_sol i n kf bf mf = bf i] (mod mf i)")
```
```   240          prefer 6
```
```   241          apply (simp add: ac_simps)
```
```   242         apply (unfold xilin_sol_def)
```
```   243         apply (tactic {* asm_simp_tac @{context} 6 *})
```
```   244         apply (rule_tac [6] ex1_implies_ex [THEN someI_ex])
```
```   245         apply (rule_tac [6] unique_xi_sol)
```
```   246            apply (rule_tac [3] funprod_zdvd)
```
```   247             apply (unfold m_cond_def)
```
```   248             apply (rule funprod_pos [THEN pos_mod_sign])
```
```   249             apply (rule_tac [2] funprod_pos [THEN pos_mod_bound])
```
```   250             apply auto
```
```   251   done
```
```   252
```
```   253 end
```