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