src/HOL/Number_Theory/UniqueFactorization.thy
author haftmann
Mon Mar 08 09:38:58 2010 +0100 (2010-03-08)
changeset 35644 d20cf282342e
parent 35416 d8d7d1b785af
child 36903 489c1fbbb028
permissions -rw-r--r--
transfer: avoid camel case
nipkow@31719
     1
(*  Title:      UniqueFactorization.thy
nipkow@31719
     2
    Author:     Jeremy Avigad
nipkow@31719
     3
nipkow@31719
     4
    
nipkow@31719
     5
    Unique factorization for the natural numbers and the integers.
nipkow@31719
     6
nipkow@31719
     7
    Note: there were previous Isabelle formalizations of unique
nipkow@31719
     8
    factorization due to Thomas Marthedal Rasmussen, and, building on
nipkow@31719
     9
    that, by Jeremy Avigad and David Gray.  
nipkow@31719
    10
*)
nipkow@31719
    11
nipkow@31719
    12
header {* UniqueFactorization *}
nipkow@31719
    13
nipkow@31719
    14
theory UniqueFactorization
nipkow@31719
    15
imports Cong Multiset
nipkow@31719
    16
begin
nipkow@31719
    17
nipkow@31719
    18
(* inherited from Multiset *)
nipkow@31719
    19
declare One_nat_def [simp del] 
nipkow@31719
    20
nipkow@31719
    21
(* As a simp or intro rule,
nipkow@31719
    22
nipkow@31719
    23
     prime p \<Longrightarrow> p > 0
nipkow@31719
    24
nipkow@31719
    25
   wreaks havoc here. When the premise includes ALL x :# M. prime x, it 
nipkow@31719
    26
   leads to the backchaining
nipkow@31719
    27
nipkow@31719
    28
     x > 0  
nipkow@31719
    29
     prime x 
nipkow@31719
    30
     x :# M   which is, unfortunately,
nipkow@31719
    31
     count M x > 0
nipkow@31719
    32
*)
nipkow@31719
    33
nipkow@31719
    34
nipkow@31719
    35
(* useful facts *)
nipkow@31719
    36
nipkow@31719
    37
lemma setsum_Un2: "finite (A Un B) \<Longrightarrow> 
nipkow@31719
    38
    setsum f (A Un B) = setsum f (A - B) + setsum f (B - A) + 
nipkow@31719
    39
      setsum f (A Int B)"
nipkow@31719
    40
  apply (subgoal_tac "A Un B = (A - B) Un (B - A) Un (A Int B)")
nipkow@31719
    41
  apply (erule ssubst)
nipkow@31719
    42
  apply (subst setsum_Un_disjoint)
nipkow@31719
    43
  apply auto
nipkow@31719
    44
  apply (subst setsum_Un_disjoint)
nipkow@31719
    45
  apply auto
nipkow@31719
    46
done
nipkow@31719
    47
nipkow@31719
    48
lemma setprod_Un2: "finite (A Un B) \<Longrightarrow> 
nipkow@31719
    49
    setprod f (A Un B) = setprod f (A - B) * setprod f (B - A) * 
nipkow@31719
    50
      setprod f (A Int B)"
nipkow@31719
    51
  apply (subgoal_tac "A Un B = (A - B) Un (B - A) Un (A Int B)")
nipkow@31719
    52
  apply (erule ssubst)
nipkow@31719
    53
  apply (subst setprod_Un_disjoint)
nipkow@31719
    54
  apply auto
nipkow@31719
    55
  apply (subst setprod_Un_disjoint)
nipkow@31719
    56
  apply auto
nipkow@31719
    57
done
nipkow@31719
    58
 
nipkow@31719
    59
(* Should this go in Multiset.thy? *)
nipkow@31719
    60
(* TN: No longer an intro-rule; needed only once and might get in the way *)
nipkow@31719
    61
lemma multiset_eqI: "[| !!x. count M x = count N x |] ==> M = N"
nipkow@31719
    62
  by (subst multiset_eq_conv_count_eq, blast)
nipkow@31719
    63
nipkow@31719
    64
(* Here is a version of set product for multisets. Is it worth moving
nipkow@31719
    65
   to multiset.thy? If so, one should similarly define msetsum for abelian 
nipkow@31719
    66
   semirings, using of_nat. Also, is it worth developing bounded quantifiers 
nipkow@31719
    67
   "ALL i :# M. P i"? 
nipkow@31719
    68
*)
nipkow@31719
    69
haftmann@35416
    70
definition msetprod :: "('a => ('b::{power,comm_monoid_mult})) => 'a multiset => 'b" where
nipkow@31719
    71
  "msetprod f M == setprod (%x. (f x)^(count M x)) (set_of M)"
nipkow@31719
    72
nipkow@31719
    73
syntax
nipkow@31719
    74
  "_msetprod" :: "pttrn => 'a set => 'b => 'b::comm_monoid_mult" 
nipkow@31719
    75
      ("(3PROD _:#_. _)" [0, 51, 10] 10)
nipkow@31719
    76
nipkow@31719
    77
translations
wenzelm@35054
    78
  "PROD i :# A. b" == "CONST msetprod (%i. b) A"
nipkow@31719
    79
nipkow@31719
    80
lemma msetprod_Un: "msetprod f (A+B) = msetprod f A * msetprod f B" 
nipkow@31719
    81
  apply (simp add: msetprod_def power_add)
nipkow@31719
    82
  apply (subst setprod_Un2)
nipkow@31719
    83
  apply auto
nipkow@31719
    84
  apply (subgoal_tac 
nipkow@31719
    85
      "(PROD x:set_of A - set_of B. f x ^ count A x * f x ^ count B x) =
nipkow@31719
    86
       (PROD x:set_of A - set_of B. f x ^ count A x)")
nipkow@31719
    87
  apply (erule ssubst)
nipkow@31719
    88
  apply (subgoal_tac 
nipkow@31719
    89
      "(PROD x:set_of B - set_of A. f x ^ count A x * f x ^ count B x) =
nipkow@31719
    90
       (PROD x:set_of B - set_of A. f x ^ count B x)")
nipkow@31719
    91
  apply (erule ssubst)
nipkow@31719
    92
  apply (subgoal_tac "(PROD x:set_of A. f x ^ count A x) = 
nipkow@31719
    93
    (PROD x:set_of A - set_of B. f x ^ count A x) *
nipkow@31719
    94
    (PROD x:set_of A Int set_of B. f x ^ count A x)")
nipkow@31719
    95
  apply (erule ssubst)
nipkow@31719
    96
  apply (subgoal_tac "(PROD x:set_of B. f x ^ count B x) = 
nipkow@31719
    97
    (PROD x:set_of B - set_of A. f x ^ count B x) *
nipkow@31719
    98
    (PROD x:set_of A Int set_of B. f x ^ count B x)")
nipkow@31719
    99
  apply (erule ssubst)
nipkow@31719
   100
  apply (subst setprod_timesf)
nipkow@31719
   101
  apply (force simp add: mult_ac)
nipkow@31719
   102
  apply (subst setprod_Un_disjoint [symmetric])
nipkow@31719
   103
  apply (auto intro: setprod_cong)
nipkow@31719
   104
  apply (subst setprod_Un_disjoint [symmetric])
nipkow@31719
   105
  apply (auto intro: setprod_cong)
nipkow@31719
   106
done
nipkow@31719
   107
nipkow@31719
   108
nipkow@31719
   109
subsection {* unique factorization: multiset version *}
nipkow@31719
   110
nipkow@31719
   111
lemma multiset_prime_factorization_exists [rule_format]: "n > 0 --> 
nipkow@31719
   112
    (EX M. (ALL (p::nat) : set_of M. prime p) & n = (PROD i :# M. i))"
nipkow@31719
   113
proof (rule nat_less_induct, clarify)
nipkow@31719
   114
  fix n :: nat
nipkow@31719
   115
  assume ih: "ALL m < n. 0 < m --> (EX M. (ALL p : set_of M. prime p) & m = 
nipkow@31719
   116
      (PROD i :# M. i))"
nipkow@31719
   117
  assume "(n::nat) > 0"
nipkow@31719
   118
  then have "n = 1 | (n > 1 & prime n) | (n > 1 & ~ prime n)"
nipkow@31719
   119
    by arith
nipkow@31719
   120
  moreover 
nipkow@31719
   121
  {
nipkow@31719
   122
    assume "n = 1"
nipkow@31719
   123
    then have "(ALL p : set_of {#}. prime p) & n = (PROD i :# {#}. i)"
nipkow@31719
   124
        by (auto simp add: msetprod_def)
nipkow@31719
   125
  } 
nipkow@31719
   126
  moreover 
nipkow@31719
   127
  {
nipkow@31719
   128
    assume "n > 1" and "prime n"
nipkow@31719
   129
    then have "(ALL p : set_of {# n #}. prime p) & n = (PROD i :# {# n #}. i)"
nipkow@31719
   130
      by (auto simp add: msetprod_def)
nipkow@31719
   131
  } 
nipkow@31719
   132
  moreover 
nipkow@31719
   133
  {
nipkow@31719
   134
    assume "n > 1" and "~ prime n"
nipkow@31952
   135
    from prems not_prime_eq_prod_nat
nipkow@31719
   136
      obtain m k where "n = m * k & 1 < m & m < n & 1 < k & k < n"
nipkow@31719
   137
        by blast
nipkow@31719
   138
    with ih obtain Q R where "(ALL p : set_of Q. prime p) & m = (PROD i:#Q. i)"
nipkow@31719
   139
        and "(ALL p: set_of R. prime p) & k = (PROD i:#R. i)"
nipkow@31719
   140
      by blast
nipkow@31719
   141
    hence "(ALL p: set_of (Q + R). prime p) & n = (PROD i :# Q + R. i)"
nipkow@31719
   142
      by (auto simp add: prems msetprod_Un set_of_union)
nipkow@31719
   143
    then have "EX M. (ALL p : set_of M. prime p) & n = (PROD i :# M. i)"..
nipkow@31719
   144
  }
nipkow@31719
   145
  ultimately show "EX M. (ALL p : set_of M. prime p) & n = (PROD i::nat:#M. i)"
nipkow@31719
   146
    by blast
nipkow@31719
   147
qed
nipkow@31719
   148
nipkow@31719
   149
lemma multiset_prime_factorization_unique_aux:
nipkow@31719
   150
  fixes a :: nat
nipkow@31719
   151
  assumes "(ALL p : set_of M. prime p)" and
nipkow@31719
   152
    "(ALL p : set_of N. prime p)" and
nipkow@31719
   153
    "(PROD i :# M. i) dvd (PROD i:# N. i)"
nipkow@31719
   154
  shows
nipkow@31719
   155
    "count M a <= count N a"
nipkow@31719
   156
proof cases
nipkow@31719
   157
  assume "a : set_of M"
nipkow@31719
   158
  with prems have a: "prime a"
nipkow@31719
   159
    by auto
nipkow@31719
   160
  with prems have "a ^ count M a dvd (PROD i :# M. i)"
nipkow@31719
   161
    by (auto intro: dvd_setprod simp add: msetprod_def)
nipkow@31719
   162
  also have "... dvd (PROD i :# N. i)"
nipkow@31719
   163
    by (rule prems)
nipkow@31719
   164
  also have "... = (PROD i : (set_of N). i ^ (count N i))"
nipkow@31719
   165
    by (simp add: msetprod_def)
nipkow@31719
   166
  also have "... = 
nipkow@31719
   167
      a^(count N a) * (PROD i : (set_of N - {a}). i ^ (count N i))"
nipkow@31719
   168
    proof (cases)
nipkow@31719
   169
      assume "a : set_of N"
nipkow@31719
   170
      hence b: "set_of N = {a} Un (set_of N - {a})"
nipkow@31719
   171
        by auto
nipkow@31719
   172
      thus ?thesis
nipkow@31719
   173
        by (subst (1) b, subst setprod_Un_disjoint, auto)
nipkow@31719
   174
    next
nipkow@31719
   175
      assume "a ~: set_of N" 
nipkow@31719
   176
      thus ?thesis
nipkow@31719
   177
        by auto
nipkow@31719
   178
    qed
nipkow@31719
   179
  finally have "a ^ count M a dvd 
nipkow@31719
   180
      a^(count N a) * (PROD i : (set_of N - {a}). i ^ (count N i))".
nipkow@31719
   181
  moreover have "coprime (a ^ count M a)
nipkow@31719
   182
      (PROD i : (set_of N - {a}). i ^ (count N i))"
nipkow@31952
   183
    apply (subst gcd_commute_nat)
nipkow@31952
   184
    apply (rule setprod_coprime_nat)
nipkow@31952
   185
    apply (rule primes_imp_powers_coprime_nat)
nipkow@31719
   186
    apply (insert prems, auto) 
nipkow@31719
   187
    done
nipkow@31719
   188
  ultimately have "a ^ count M a dvd a^(count N a)"
nipkow@31952
   189
    by (elim coprime_dvd_mult_nat)
nipkow@31719
   190
  with a show ?thesis 
nipkow@31719
   191
    by (intro power_dvd_imp_le, auto)
nipkow@31719
   192
next
nipkow@31719
   193
  assume "a ~: set_of M"
nipkow@31719
   194
  thus ?thesis by auto
nipkow@31719
   195
qed
nipkow@31719
   196
nipkow@31719
   197
lemma multiset_prime_factorization_unique:
nipkow@31719
   198
  assumes "(ALL (p::nat) : set_of M. prime p)" and
nipkow@31719
   199
    "(ALL p : set_of N. prime p)" and
nipkow@31719
   200
    "(PROD i :# M. i) = (PROD i:# N. i)"
nipkow@31719
   201
  shows
nipkow@31719
   202
    "M = N"
nipkow@31719
   203
proof -
nipkow@31719
   204
  {
nipkow@31719
   205
    fix a
nipkow@31719
   206
    from prems have "count M a <= count N a"
nipkow@31719
   207
      by (intro multiset_prime_factorization_unique_aux, auto) 
nipkow@31719
   208
    moreover from prems have "count N a <= count M a"
nipkow@31719
   209
      by (intro multiset_prime_factorization_unique_aux, auto) 
nipkow@31719
   210
    ultimately have "count M a = count N a"
nipkow@31719
   211
      by auto
nipkow@31719
   212
  }
nipkow@31719
   213
  thus ?thesis by (simp add:multiset_eq_conv_count_eq)
nipkow@31719
   214
qed
nipkow@31719
   215
haftmann@35416
   216
definition multiset_prime_factorization :: "nat => nat multiset" where
nipkow@31719
   217
  "multiset_prime_factorization n ==
nipkow@31719
   218
     if n > 0 then (THE M. ((ALL p : set_of M. prime p) & 
nipkow@31719
   219
       n = (PROD i :# M. i)))
nipkow@31719
   220
     else {#}"
nipkow@31719
   221
nipkow@31719
   222
lemma multiset_prime_factorization: "n > 0 ==>
nipkow@31719
   223
    (ALL p : set_of (multiset_prime_factorization n). prime p) &
nipkow@31719
   224
       n = (PROD i :# (multiset_prime_factorization n). i)"
nipkow@31719
   225
  apply (unfold multiset_prime_factorization_def)
nipkow@31719
   226
  apply clarsimp
nipkow@31719
   227
  apply (frule multiset_prime_factorization_exists)
nipkow@31719
   228
  apply clarify
nipkow@31719
   229
  apply (rule theI)
nipkow@31719
   230
  apply (insert multiset_prime_factorization_unique, blast)+
nipkow@31719
   231
done
nipkow@31719
   232
nipkow@31719
   233
nipkow@31719
   234
subsection {* Prime factors and multiplicity for nats and ints *}
nipkow@31719
   235
nipkow@31719
   236
class unique_factorization =
nipkow@31719
   237
nipkow@31719
   238
fixes
nipkow@31719
   239
  multiplicity :: "'a \<Rightarrow> 'a \<Rightarrow> nat" and
nipkow@31719
   240
  prime_factors :: "'a \<Rightarrow> 'a set"
nipkow@31719
   241
nipkow@31719
   242
(* definitions for the natural numbers *)
nipkow@31719
   243
nipkow@31719
   244
instantiation nat :: unique_factorization
nipkow@31719
   245
nipkow@31719
   246
begin
nipkow@31719
   247
nipkow@31719
   248
definition
nipkow@31719
   249
  multiplicity_nat :: "nat \<Rightarrow> nat \<Rightarrow> nat"
nipkow@31719
   250
where
nipkow@31719
   251
  "multiplicity_nat p n = count (multiset_prime_factorization n) p"
nipkow@31719
   252
nipkow@31719
   253
definition
nipkow@31719
   254
  prime_factors_nat :: "nat \<Rightarrow> nat set"
nipkow@31719
   255
where
nipkow@31719
   256
  "prime_factors_nat n = set_of (multiset_prime_factorization n)"
nipkow@31719
   257
nipkow@31719
   258
instance proof qed
nipkow@31719
   259
nipkow@31719
   260
end
nipkow@31719
   261
nipkow@31719
   262
(* definitions for the integers *)
nipkow@31719
   263
nipkow@31719
   264
instantiation int :: unique_factorization
nipkow@31719
   265
nipkow@31719
   266
begin
nipkow@31719
   267
nipkow@31719
   268
definition
nipkow@31719
   269
  multiplicity_int :: "int \<Rightarrow> int \<Rightarrow> nat"
nipkow@31719
   270
where
nipkow@31719
   271
  "multiplicity_int p n = multiplicity (nat p) (nat n)"
nipkow@31719
   272
nipkow@31719
   273
definition
nipkow@31719
   274
  prime_factors_int :: "int \<Rightarrow> int set"
nipkow@31719
   275
where
nipkow@31719
   276
  "prime_factors_int n = int ` (prime_factors (nat n))"
nipkow@31719
   277
nipkow@31719
   278
instance proof qed
nipkow@31719
   279
nipkow@31719
   280
end
nipkow@31719
   281
nipkow@31719
   282
nipkow@31719
   283
subsection {* Set up transfer *}
nipkow@31719
   284
nipkow@31719
   285
lemma transfer_nat_int_prime_factors: 
nipkow@31719
   286
  "prime_factors (nat n) = nat ` prime_factors n"
nipkow@31719
   287
  unfolding prime_factors_int_def apply auto
nipkow@31719
   288
  by (subst transfer_int_nat_set_return_embed, assumption)
nipkow@31719
   289
nipkow@31719
   290
lemma transfer_nat_int_prime_factors_closure: "n >= 0 \<Longrightarrow> 
nipkow@31719
   291
    nat_set (prime_factors n)"
nipkow@31719
   292
  by (auto simp add: nat_set_def prime_factors_int_def)
nipkow@31719
   293
nipkow@31719
   294
lemma transfer_nat_int_multiplicity: "p >= 0 \<Longrightarrow> n >= 0 \<Longrightarrow>
nipkow@31719
   295
  multiplicity (nat p) (nat n) = multiplicity p n"
nipkow@31719
   296
  by (auto simp add: multiplicity_int_def)
nipkow@31719
   297
haftmann@35644
   298
declare transfer_morphism_nat_int[transfer add return: 
nipkow@31719
   299
  transfer_nat_int_prime_factors transfer_nat_int_prime_factors_closure
nipkow@31719
   300
  transfer_nat_int_multiplicity]
nipkow@31719
   301
nipkow@31719
   302
nipkow@31719
   303
lemma transfer_int_nat_prime_factors:
nipkow@31719
   304
    "prime_factors (int n) = int ` prime_factors n"
nipkow@31719
   305
  unfolding prime_factors_int_def by auto
nipkow@31719
   306
nipkow@31719
   307
lemma transfer_int_nat_prime_factors_closure: "is_nat n \<Longrightarrow> 
nipkow@31719
   308
    nat_set (prime_factors n)"
nipkow@31719
   309
  by (simp only: transfer_nat_int_prime_factors_closure is_nat_def)
nipkow@31719
   310
nipkow@31719
   311
lemma transfer_int_nat_multiplicity: 
nipkow@31719
   312
    "multiplicity (int p) (int n) = multiplicity p n"
nipkow@31719
   313
  by (auto simp add: multiplicity_int_def)
nipkow@31719
   314
haftmann@35644
   315
declare transfer_morphism_int_nat[transfer add return: 
nipkow@31719
   316
  transfer_int_nat_prime_factors transfer_int_nat_prime_factors_closure
nipkow@31719
   317
  transfer_int_nat_multiplicity]
nipkow@31719
   318
nipkow@31719
   319
nipkow@31719
   320
subsection {* Properties of prime factors and multiplicity for nats and ints *}
nipkow@31719
   321
nipkow@31952
   322
lemma prime_factors_ge_0_int [elim]: "p : prime_factors (n::int) \<Longrightarrow> p >= 0"
nipkow@31719
   323
  by (unfold prime_factors_int_def, auto)
nipkow@31719
   324
nipkow@31952
   325
lemma prime_factors_prime_nat [intro]: "p : prime_factors (n::nat) \<Longrightarrow> prime p"
nipkow@31719
   326
  apply (case_tac "n = 0")
nipkow@31719
   327
  apply (simp add: prime_factors_nat_def multiset_prime_factorization_def)
nipkow@31719
   328
  apply (auto simp add: prime_factors_nat_def multiset_prime_factorization)
nipkow@31719
   329
done
nipkow@31719
   330
nipkow@31952
   331
lemma prime_factors_prime_int [intro]:
nipkow@31719
   332
  assumes "n >= 0" and "p : prime_factors (n::int)"
nipkow@31719
   333
  shows "prime p"
nipkow@31719
   334
nipkow@31952
   335
  apply (rule prime_factors_prime_nat [transferred, of n p])
nipkow@31719
   336
  using prems apply auto
nipkow@31719
   337
done
nipkow@31719
   338
nipkow@31952
   339
lemma prime_factors_gt_0_nat [elim]: "p : prime_factors x \<Longrightarrow> p > (0::nat)"
nipkow@31952
   340
  by (frule prime_factors_prime_nat, auto)
nipkow@31719
   341
nipkow@31952
   342
lemma prime_factors_gt_0_int [elim]: "x >= 0 \<Longrightarrow> p : prime_factors x \<Longrightarrow> 
nipkow@31719
   343
    p > (0::int)"
nipkow@31952
   344
  by (frule (1) prime_factors_prime_int, auto)
nipkow@31719
   345
nipkow@31952
   346
lemma prime_factors_finite_nat [iff]: "finite (prime_factors (n::nat))"
nipkow@31719
   347
  by (unfold prime_factors_nat_def, auto)
nipkow@31719
   348
nipkow@31952
   349
lemma prime_factors_finite_int [iff]: "finite (prime_factors (n::int))"
nipkow@31719
   350
  by (unfold prime_factors_int_def, auto)
nipkow@31719
   351
nipkow@31952
   352
lemma prime_factors_altdef_nat: "prime_factors (n::nat) = 
nipkow@31719
   353
    {p. multiplicity p n > 0}"
nipkow@31719
   354
  by (force simp add: prime_factors_nat_def multiplicity_nat_def)
nipkow@31719
   355
nipkow@31952
   356
lemma prime_factors_altdef_int: "prime_factors (n::int) = 
nipkow@31719
   357
    {p. p >= 0 & multiplicity p n > 0}"
nipkow@31719
   358
  apply (unfold prime_factors_int_def multiplicity_int_def)
nipkow@31952
   359
  apply (subst prime_factors_altdef_nat)
nipkow@31719
   360
  apply (auto simp add: image_def)
nipkow@31719
   361
done
nipkow@31719
   362
nipkow@31952
   363
lemma prime_factorization_nat: "(n::nat) > 0 \<Longrightarrow> 
nipkow@31719
   364
    n = (PROD p : prime_factors n. p^(multiplicity p n))"
nipkow@31719
   365
  by (frule multiset_prime_factorization, 
nipkow@31719
   366
    simp add: prime_factors_nat_def multiplicity_nat_def msetprod_def)
nipkow@31719
   367
nipkow@31952
   368
thm prime_factorization_nat [transferred] 
nipkow@31719
   369
nipkow@31952
   370
lemma prime_factorization_int: 
nipkow@31719
   371
  assumes "(n::int) > 0"
nipkow@31719
   372
  shows "n = (PROD p : prime_factors n. p^(multiplicity p n))"
nipkow@31719
   373
nipkow@31952
   374
  apply (rule prime_factorization_nat [transferred, of n])
nipkow@31719
   375
  using prems apply auto
nipkow@31719
   376
done
nipkow@31719
   377
nipkow@31952
   378
lemma neq_zero_eq_gt_zero_nat: "((x::nat) ~= 0) = (x > 0)"
nipkow@31719
   379
  by auto
nipkow@31719
   380
nipkow@31952
   381
lemma prime_factorization_unique_nat: 
nipkow@31719
   382
    "S = { (p::nat) . f p > 0} \<Longrightarrow> finite S \<Longrightarrow> (ALL p : S. prime p) \<Longrightarrow>
nipkow@31719
   383
      n = (PROD p : S. p^(f p)) \<Longrightarrow>
nipkow@31719
   384
        S = prime_factors n & (ALL p. f p = multiplicity p n)"
nipkow@31719
   385
  apply (subgoal_tac "multiset_prime_factorization n = Abs_multiset
nipkow@31719
   386
      f")
nipkow@31719
   387
  apply (unfold prime_factors_nat_def multiplicity_nat_def)
haftmann@34947
   388
  apply (simp add: set_of_def Abs_multiset_inverse multiset_def)
nipkow@31719
   389
  apply (unfold multiset_prime_factorization_def)
nipkow@31719
   390
  apply (subgoal_tac "n > 0")
nipkow@31719
   391
  prefer 2
nipkow@31719
   392
  apply force
nipkow@31719
   393
  apply (subst if_P, assumption)
nipkow@31719
   394
  apply (rule the1_equality)
nipkow@31719
   395
  apply (rule ex_ex1I)
nipkow@31719
   396
  apply (rule multiset_prime_factorization_exists, assumption)
nipkow@31719
   397
  apply (rule multiset_prime_factorization_unique)
nipkow@31719
   398
  apply force
nipkow@31719
   399
  apply force
nipkow@31719
   400
  apply force
haftmann@34947
   401
  unfolding set_of_def msetprod_def
nipkow@31719
   402
  apply (subgoal_tac "f : multiset")
nipkow@31719
   403
  apply (auto simp only: Abs_multiset_inverse)
nipkow@31719
   404
  unfolding multiset_def apply force 
nipkow@31719
   405
done
nipkow@31719
   406
nipkow@31952
   407
lemma prime_factors_characterization_nat: "S = {p. 0 < f (p::nat)} \<Longrightarrow> 
nipkow@31719
   408
    finite S \<Longrightarrow> (ALL p:S. prime p) \<Longrightarrow> n = (PROD p:S. p ^ f p) \<Longrightarrow>
nipkow@31719
   409
      prime_factors n = S"
nipkow@31952
   410
  by (rule prime_factorization_unique_nat [THEN conjunct1, symmetric],
nipkow@31719
   411
    assumption+)
nipkow@31719
   412
nipkow@31952
   413
lemma prime_factors_characterization'_nat: 
nipkow@31719
   414
  "finite {p. 0 < f (p::nat)} \<Longrightarrow>
nipkow@31719
   415
    (ALL p. 0 < f p \<longrightarrow> prime p) \<Longrightarrow>
nipkow@31719
   416
      prime_factors (PROD p | 0 < f p . p ^ f p) = {p. 0 < f p}"
nipkow@31952
   417
  apply (rule prime_factors_characterization_nat)
nipkow@31719
   418
  apply auto
nipkow@31719
   419
done
nipkow@31719
   420
nipkow@31719
   421
(* A minor glitch:*)
nipkow@31719
   422
nipkow@31952
   423
thm prime_factors_characterization'_nat 
nipkow@31719
   424
    [where f = "%x. f (int (x::nat))", 
nipkow@31719
   425
      transferred direction: nat "op <= (0::int)", rule_format]
nipkow@31719
   426
nipkow@31719
   427
(*
nipkow@31719
   428
  Transfer isn't smart enough to know that the "0 < f p" should 
nipkow@31719
   429
  remain a comparison between nats. But the transfer still works. 
nipkow@31719
   430
*)
nipkow@31719
   431
nipkow@31952
   432
lemma primes_characterization'_int [rule_format]: 
nipkow@31719
   433
    "finite {p. p >= 0 & 0 < f (p::int)} \<Longrightarrow>
nipkow@31719
   434
      (ALL p. 0 < f p \<longrightarrow> prime p) \<Longrightarrow>
nipkow@31719
   435
        prime_factors (PROD p | p >=0 & 0 < f p . p ^ f p) = 
nipkow@31719
   436
          {p. p >= 0 & 0 < f p}"
nipkow@31719
   437
nipkow@31952
   438
  apply (insert prime_factors_characterization'_nat 
nipkow@31719
   439
    [where f = "%x. f (int (x::nat))", 
nipkow@31719
   440
    transferred direction: nat "op <= (0::int)"])
nipkow@31719
   441
  apply auto
nipkow@31719
   442
done
nipkow@31719
   443
nipkow@31952
   444
lemma prime_factors_characterization_int: "S = {p. 0 < f (p::int)} \<Longrightarrow> 
nipkow@31719
   445
    finite S \<Longrightarrow> (ALL p:S. prime p) \<Longrightarrow> n = (PROD p:S. p ^ f p) \<Longrightarrow>
nipkow@31719
   446
      prime_factors n = S"
nipkow@31719
   447
  apply simp
nipkow@31719
   448
  apply (subgoal_tac "{p. 0 < f p} = {p. 0 <= p & 0 < f p}")
nipkow@31719
   449
  apply (simp only:)
nipkow@31952
   450
  apply (subst primes_characterization'_int)
nipkow@31719
   451
  apply auto
nipkow@31952
   452
  apply (auto simp add: prime_ge_0_int)
nipkow@31719
   453
done
nipkow@31719
   454
nipkow@31952
   455
lemma multiplicity_characterization_nat: "S = {p. 0 < f (p::nat)} \<Longrightarrow> 
nipkow@31719
   456
    finite S \<Longrightarrow> (ALL p:S. prime p) \<Longrightarrow> n = (PROD p:S. p ^ f p) \<Longrightarrow>
nipkow@31719
   457
      multiplicity p n = f p"
nipkow@31952
   458
  by (frule prime_factorization_unique_nat [THEN conjunct2, rule_format, 
nipkow@31719
   459
    symmetric], auto)
nipkow@31719
   460
nipkow@31952
   461
lemma multiplicity_characterization'_nat: "finite {p. 0 < f (p::nat)} \<longrightarrow>
nipkow@31719
   462
    (ALL p. 0 < f p \<longrightarrow> prime p) \<longrightarrow>
nipkow@31719
   463
      multiplicity p (PROD p | 0 < f p . p ^ f p) = f p"
nipkow@31719
   464
  apply (rule impI)+
nipkow@31952
   465
  apply (rule multiplicity_characterization_nat)
nipkow@31719
   466
  apply auto
nipkow@31719
   467
done
nipkow@31719
   468
nipkow@31952
   469
lemma multiplicity_characterization'_int [rule_format]: 
nipkow@31719
   470
  "finite {p. p >= 0 & 0 < f (p::int)} \<Longrightarrow>
nipkow@31719
   471
    (ALL p. 0 < f p \<longrightarrow> prime p) \<Longrightarrow> p >= 0 \<Longrightarrow>
nipkow@31719
   472
      multiplicity p (PROD p | p >= 0 & 0 < f p . p ^ f p) = f p"
nipkow@31719
   473
nipkow@31952
   474
  apply (insert multiplicity_characterization'_nat 
nipkow@31719
   475
    [where f = "%x. f (int (x::nat))", 
nipkow@31719
   476
      transferred direction: nat "op <= (0::int)", rule_format])
nipkow@31719
   477
  apply auto
nipkow@31719
   478
done
nipkow@31719
   479
nipkow@31952
   480
lemma multiplicity_characterization_int: "S = {p. 0 < f (p::int)} \<Longrightarrow> 
nipkow@31719
   481
    finite S \<Longrightarrow> (ALL p:S. prime p) \<Longrightarrow> n = (PROD p:S. p ^ f p) \<Longrightarrow>
nipkow@31719
   482
      p >= 0 \<Longrightarrow> multiplicity p n = f p"
nipkow@31719
   483
  apply simp
nipkow@31719
   484
  apply (subgoal_tac "{p. 0 < f p} = {p. 0 <= p & 0 < f p}")
nipkow@31719
   485
  apply (simp only:)
nipkow@31952
   486
  apply (subst multiplicity_characterization'_int)
nipkow@31719
   487
  apply auto
nipkow@31952
   488
  apply (auto simp add: prime_ge_0_int)
nipkow@31719
   489
done
nipkow@31719
   490
nipkow@31952
   491
lemma multiplicity_zero_nat [simp]: "multiplicity (p::nat) 0 = 0"
nipkow@31719
   492
  by (simp add: multiplicity_nat_def multiset_prime_factorization_def)
nipkow@31719
   493
nipkow@31952
   494
lemma multiplicity_zero_int [simp]: "multiplicity (p::int) 0 = 0"
nipkow@31719
   495
  by (simp add: multiplicity_int_def) 
nipkow@31719
   496
nipkow@31952
   497
lemma multiplicity_one_nat [simp]: "multiplicity p (1::nat) = 0"
nipkow@31952
   498
  by (subst multiplicity_characterization_nat [where f = "%x. 0"], auto)
nipkow@31719
   499
nipkow@31952
   500
lemma multiplicity_one_int [simp]: "multiplicity p (1::int) = 0"
nipkow@31719
   501
  by (simp add: multiplicity_int_def)
nipkow@31719
   502
nipkow@31952
   503
lemma multiplicity_prime_nat [simp]: "prime (p::nat) \<Longrightarrow> multiplicity p p = 1"
nipkow@31952
   504
  apply (subst multiplicity_characterization_nat
nipkow@31719
   505
      [where f = "(%q. if q = p then 1 else 0)"])
nipkow@31719
   506
  apply auto
nipkow@31719
   507
  apply (case_tac "x = p")
nipkow@31719
   508
  apply auto
nipkow@31719
   509
done
nipkow@31719
   510
nipkow@31952
   511
lemma multiplicity_prime_int [simp]: "prime (p::int) \<Longrightarrow> multiplicity p p = 1"
nipkow@31719
   512
  unfolding prime_int_def multiplicity_int_def by auto
nipkow@31719
   513
nipkow@31952
   514
lemma multiplicity_prime_power_nat [simp]: "prime (p::nat) \<Longrightarrow> 
nipkow@31719
   515
    multiplicity p (p^n) = n"
nipkow@31719
   516
  apply (case_tac "n = 0")
nipkow@31719
   517
  apply auto
nipkow@31952
   518
  apply (subst multiplicity_characterization_nat
nipkow@31719
   519
      [where f = "(%q. if q = p then n else 0)"])
nipkow@31719
   520
  apply auto
nipkow@31719
   521
  apply (case_tac "x = p")
nipkow@31719
   522
  apply auto
nipkow@31719
   523
done
nipkow@31719
   524
nipkow@31952
   525
lemma multiplicity_prime_power_int [simp]: "prime (p::int) \<Longrightarrow> 
nipkow@31719
   526
    multiplicity p (p^n) = n"
nipkow@31952
   527
  apply (frule prime_ge_0_int)
nipkow@31719
   528
  apply (auto simp add: prime_int_def multiplicity_int_def nat_power_eq)
nipkow@31719
   529
done
nipkow@31719
   530
nipkow@31952
   531
lemma multiplicity_nonprime_nat [simp]: "~ prime (p::nat) \<Longrightarrow> 
nipkow@31719
   532
    multiplicity p n = 0"
nipkow@31719
   533
  apply (case_tac "n = 0")
nipkow@31719
   534
  apply auto
nipkow@31719
   535
  apply (frule multiset_prime_factorization)
nipkow@31719
   536
  apply (auto simp add: set_of_def multiplicity_nat_def)
nipkow@31719
   537
done
nipkow@31719
   538
nipkow@31952
   539
lemma multiplicity_nonprime_int [simp]: "~ prime (p::int) \<Longrightarrow> multiplicity p n = 0"
nipkow@31719
   540
  by (unfold multiplicity_int_def prime_int_def, auto)
nipkow@31719
   541
nipkow@31952
   542
lemma multiplicity_not_factor_nat [simp]: 
nipkow@31719
   543
    "p ~: prime_factors (n::nat) \<Longrightarrow> multiplicity p n = 0"
nipkow@31952
   544
  by (subst (asm) prime_factors_altdef_nat, auto)
nipkow@31719
   545
nipkow@31952
   546
lemma multiplicity_not_factor_int [simp]: 
nipkow@31719
   547
    "p >= 0 \<Longrightarrow> p ~: prime_factors (n::int) \<Longrightarrow> multiplicity p n = 0"
nipkow@31952
   548
  by (subst (asm) prime_factors_altdef_int, auto)
nipkow@31719
   549
nipkow@31952
   550
lemma multiplicity_product_aux_nat: "(k::nat) > 0 \<Longrightarrow> l > 0 \<Longrightarrow>
nipkow@31719
   551
    (prime_factors k) Un (prime_factors l) = prime_factors (k * l) &
nipkow@31719
   552
    (ALL p. multiplicity p k + multiplicity p l = multiplicity p (k * l))"
nipkow@31952
   553
  apply (rule prime_factorization_unique_nat)
nipkow@31952
   554
  apply (simp only: prime_factors_altdef_nat)
nipkow@31719
   555
  apply auto
nipkow@31719
   556
  apply (subst power_add)
nipkow@31719
   557
  apply (subst setprod_timesf)
nipkow@31719
   558
  apply (rule arg_cong2)back back
nipkow@31719
   559
  apply (subgoal_tac "prime_factors k Un prime_factors l = prime_factors k Un 
nipkow@31719
   560
      (prime_factors l - prime_factors k)")
nipkow@31719
   561
  apply (erule ssubst)
nipkow@31719
   562
  apply (subst setprod_Un_disjoint)
nipkow@31719
   563
  apply auto
nipkow@31719
   564
  apply (subgoal_tac "(\<Prod>p\<in>prime_factors l - prime_factors k. p ^ multiplicity p k) = 
nipkow@31719
   565
      (\<Prod>p\<in>prime_factors l - prime_factors k. 1)")
nipkow@31719
   566
  apply (erule ssubst)
nipkow@31719
   567
  apply (simp add: setprod_1)
nipkow@31952
   568
  apply (erule prime_factorization_nat)
nipkow@31719
   569
  apply (rule setprod_cong, auto)
nipkow@31719
   570
  apply (subgoal_tac "prime_factors k Un prime_factors l = prime_factors l Un 
nipkow@31719
   571
      (prime_factors k - prime_factors l)")
nipkow@31719
   572
  apply (erule ssubst)
nipkow@31719
   573
  apply (subst setprod_Un_disjoint)
nipkow@31719
   574
  apply auto
nipkow@31719
   575
  apply (subgoal_tac "(\<Prod>p\<in>prime_factors k - prime_factors l. p ^ multiplicity p l) = 
nipkow@31719
   576
      (\<Prod>p\<in>prime_factors k - prime_factors l. 1)")
nipkow@31719
   577
  apply (erule ssubst)
nipkow@31719
   578
  apply (simp add: setprod_1)
nipkow@31952
   579
  apply (erule prime_factorization_nat)
nipkow@31719
   580
  apply (rule setprod_cong, auto)
nipkow@31719
   581
done
nipkow@31719
   582
nipkow@31719
   583
(* transfer doesn't have the same problem here with the right 
nipkow@31719
   584
   choice of rules. *)
nipkow@31719
   585
nipkow@31952
   586
lemma multiplicity_product_aux_int: 
nipkow@31719
   587
  assumes "(k::int) > 0" and "l > 0"
nipkow@31719
   588
  shows 
nipkow@31719
   589
    "(prime_factors k) Un (prime_factors l) = prime_factors (k * l) &
nipkow@31719
   590
    (ALL p >= 0. multiplicity p k + multiplicity p l = multiplicity p (k * l))"
nipkow@31719
   591
nipkow@31952
   592
  apply (rule multiplicity_product_aux_nat [transferred, of l k])
nipkow@31719
   593
  using prems apply auto
nipkow@31719
   594
done
nipkow@31719
   595
nipkow@31952
   596
lemma prime_factors_product_nat: "(k::nat) > 0 \<Longrightarrow> l > 0 \<Longrightarrow> prime_factors (k * l) = 
nipkow@31719
   597
    prime_factors k Un prime_factors l"
nipkow@31952
   598
  by (rule multiplicity_product_aux_nat [THEN conjunct1, symmetric])
nipkow@31719
   599
nipkow@31952
   600
lemma prime_factors_product_int: "(k::int) > 0 \<Longrightarrow> l > 0 \<Longrightarrow> prime_factors (k * l) = 
nipkow@31719
   601
    prime_factors k Un prime_factors l"
nipkow@31952
   602
  by (rule multiplicity_product_aux_int [THEN conjunct1, symmetric])
nipkow@31719
   603
nipkow@31952
   604
lemma multiplicity_product_nat: "(k::nat) > 0 \<Longrightarrow> l > 0 \<Longrightarrow> multiplicity p (k * l) = 
nipkow@31719
   605
    multiplicity p k + multiplicity p l"
nipkow@31952
   606
  by (rule multiplicity_product_aux_nat [THEN conjunct2, rule_format, 
nipkow@31719
   607
      symmetric])
nipkow@31719
   608
nipkow@31952
   609
lemma multiplicity_product_int: "(k::int) > 0 \<Longrightarrow> l > 0 \<Longrightarrow> p >= 0 \<Longrightarrow> 
nipkow@31719
   610
    multiplicity p (k * l) = multiplicity p k + multiplicity p l"
nipkow@31952
   611
  by (rule multiplicity_product_aux_int [THEN conjunct2, rule_format, 
nipkow@31719
   612
      symmetric])
nipkow@31719
   613
nipkow@31952
   614
lemma multiplicity_setprod_nat: "finite S \<Longrightarrow> (ALL x : S. f x > 0) \<Longrightarrow> 
nipkow@31719
   615
    multiplicity (p::nat) (PROD x : S. f x) = 
nipkow@31719
   616
      (SUM x : S. multiplicity p (f x))"
nipkow@31719
   617
  apply (induct set: finite)
nipkow@31719
   618
  apply auto
nipkow@31952
   619
  apply (subst multiplicity_product_nat)
nipkow@31719
   620
  apply auto
nipkow@31719
   621
done
nipkow@31719
   622
nipkow@31719
   623
(* Transfer is delicate here for two reasons: first, because there is
nipkow@31719
   624
   an implicit quantifier over functions (f), and, second, because the 
nipkow@31719
   625
   product over the multiplicity should not be translated to an integer 
nipkow@31719
   626
   product.
nipkow@31719
   627
nipkow@31719
   628
   The way to handle the first is to use quantifier rules for functions.
nipkow@31719
   629
   The way to handle the second is to turn off the offending rule.
nipkow@31719
   630
*)
nipkow@31719
   631
nipkow@31719
   632
lemma transfer_nat_int_sum_prod_closure3:
nipkow@31719
   633
  "(SUM x : A. int (f x)) >= 0"
nipkow@31719
   634
  "(PROD x : A. int (f x)) >= 0"
nipkow@31719
   635
  apply (rule setsum_nonneg, auto)
nipkow@31719
   636
  apply (rule setprod_nonneg, auto)
nipkow@31719
   637
done
nipkow@31719
   638
haftmann@35644
   639
declare transfer_morphism_nat_int[transfer 
nipkow@31719
   640
  add return: transfer_nat_int_sum_prod_closure3
nipkow@31719
   641
  del: transfer_nat_int_sum_prod2 (1)]
nipkow@31719
   642
nipkow@31952
   643
lemma multiplicity_setprod_int: "p >= 0 \<Longrightarrow> finite S \<Longrightarrow> 
nipkow@31719
   644
  (ALL x : S. f x > 0) \<Longrightarrow> 
nipkow@31719
   645
    multiplicity (p::int) (PROD x : S. f x) = 
nipkow@31719
   646
      (SUM x : S. multiplicity p (f x))"
nipkow@31719
   647
nipkow@31952
   648
  apply (frule multiplicity_setprod_nat
nipkow@31719
   649
    [where f = "%x. nat(int(nat(f x)))", 
nipkow@31719
   650
      transferred direction: nat "op <= (0::int)"])
nipkow@31719
   651
  apply auto
nipkow@31719
   652
  apply (subst (asm) setprod_cong)
nipkow@31719
   653
  apply (rule refl)
nipkow@31719
   654
  apply (rule if_P)
nipkow@31719
   655
  apply auto
nipkow@31719
   656
  apply (rule setsum_cong)
nipkow@31719
   657
  apply auto
nipkow@31719
   658
done
nipkow@31719
   659
haftmann@35644
   660
declare transfer_morphism_nat_int[transfer 
nipkow@31719
   661
  add return: transfer_nat_int_sum_prod2 (1)]
nipkow@31719
   662
nipkow@31952
   663
lemma multiplicity_prod_prime_powers_nat:
nipkow@31719
   664
    "finite S \<Longrightarrow> (ALL p : S. prime (p::nat)) \<Longrightarrow>
nipkow@31719
   665
       multiplicity p (PROD p : S. p ^ f p) = (if p : S then f p else 0)"
nipkow@31719
   666
  apply (subgoal_tac "(PROD p : S. p ^ f p) = 
nipkow@31719
   667
      (PROD p : S. p ^ (%x. if x : S then f x else 0) p)")
nipkow@31719
   668
  apply (erule ssubst)
nipkow@31952
   669
  apply (subst multiplicity_characterization_nat)
nipkow@31719
   670
  prefer 5 apply (rule refl)
nipkow@31719
   671
  apply (rule refl)
nipkow@31719
   672
  apply auto
nipkow@31719
   673
  apply (subst setprod_mono_one_right)
nipkow@31719
   674
  apply assumption
nipkow@31719
   675
  prefer 3
nipkow@31719
   676
  apply (rule setprod_cong)
nipkow@31719
   677
  apply (rule refl)
nipkow@31719
   678
  apply auto
nipkow@31719
   679
done
nipkow@31719
   680
nipkow@31719
   681
(* Here the issue with transfer is the implicit quantifier over S *)
nipkow@31719
   682
nipkow@31952
   683
lemma multiplicity_prod_prime_powers_int:
nipkow@31719
   684
    "(p::int) >= 0 \<Longrightarrow> finite S \<Longrightarrow> (ALL p : S. prime p) \<Longrightarrow>
nipkow@31719
   685
       multiplicity p (PROD p : S. p ^ f p) = (if p : S then f p else 0)"
nipkow@31719
   686
nipkow@31719
   687
  apply (subgoal_tac "int ` nat ` S = S")
nipkow@31952
   688
  apply (frule multiplicity_prod_prime_powers_nat [where f = "%x. f(int x)" 
nipkow@31719
   689
    and S = "nat ` S", transferred])
nipkow@31719
   690
  apply auto
nipkow@31719
   691
  apply (subst prime_int_def [symmetric])
nipkow@31719
   692
  apply auto
nipkow@31719
   693
  apply (subgoal_tac "xb >= 0")
nipkow@31719
   694
  apply force
nipkow@31952
   695
  apply (rule prime_ge_0_int)
nipkow@31719
   696
  apply force
nipkow@31719
   697
  apply (subst transfer_nat_int_set_return_embed)
nipkow@31719
   698
  apply (unfold nat_set_def, auto)
nipkow@31719
   699
done
nipkow@31719
   700
nipkow@31952
   701
lemma multiplicity_distinct_prime_power_nat: "prime (p::nat) \<Longrightarrow> prime q \<Longrightarrow>
nipkow@31719
   702
    p ~= q \<Longrightarrow> multiplicity p (q^n) = 0"
nipkow@31719
   703
  apply (subgoal_tac "q^n = setprod (%x. x^n) {q}")
nipkow@31719
   704
  apply (erule ssubst)
nipkow@31952
   705
  apply (subst multiplicity_prod_prime_powers_nat)
nipkow@31719
   706
  apply auto
nipkow@31719
   707
done
nipkow@31719
   708
nipkow@31952
   709
lemma multiplicity_distinct_prime_power_int: "prime (p::int) \<Longrightarrow> prime q \<Longrightarrow>
nipkow@31719
   710
    p ~= q \<Longrightarrow> multiplicity p (q^n) = 0"
nipkow@31952
   711
  apply (frule prime_ge_0_int [of q])
nipkow@31952
   712
  apply (frule multiplicity_distinct_prime_power_nat [transferred leaving: n]) 
nipkow@31719
   713
  prefer 4
nipkow@31719
   714
  apply assumption
nipkow@31719
   715
  apply auto
nipkow@31719
   716
done
nipkow@31719
   717
nipkow@31952
   718
lemma dvd_multiplicity_nat: 
nipkow@31719
   719
    "(0::nat) < y \<Longrightarrow> x dvd y \<Longrightarrow> multiplicity p x <= multiplicity p y"
nipkow@31719
   720
  apply (case_tac "x = 0")
nipkow@31952
   721
  apply (auto simp add: dvd_def multiplicity_product_nat)
nipkow@31719
   722
done
nipkow@31719
   723
nipkow@31952
   724
lemma dvd_multiplicity_int: 
nipkow@31719
   725
    "(0::int) < y \<Longrightarrow> 0 <= x \<Longrightarrow> x dvd y \<Longrightarrow> p >= 0 \<Longrightarrow> 
nipkow@31719
   726
      multiplicity p x <= multiplicity p y"
nipkow@31719
   727
  apply (case_tac "x = 0")
nipkow@31719
   728
  apply (auto simp add: dvd_def)
nipkow@31719
   729
  apply (subgoal_tac "0 < k")
nipkow@31952
   730
  apply (auto simp add: multiplicity_product_int)
nipkow@31719
   731
  apply (erule zero_less_mult_pos)
nipkow@31719
   732
  apply arith
nipkow@31719
   733
done
nipkow@31719
   734
nipkow@31952
   735
lemma dvd_prime_factors_nat [intro]:
nipkow@31719
   736
    "0 < (y::nat) \<Longrightarrow> x dvd y \<Longrightarrow> prime_factors x <= prime_factors y"
nipkow@31952
   737
  apply (simp only: prime_factors_altdef_nat)
nipkow@31719
   738
  apply auto
nipkow@31952
   739
  apply (frule dvd_multiplicity_nat)
nipkow@31719
   740
  apply auto
nipkow@31719
   741
(* It is a shame that auto and arith don't get this. *)
nipkow@31719
   742
  apply (erule order_less_le_trans)back
nipkow@31719
   743
  apply assumption
nipkow@31719
   744
done
nipkow@31719
   745
nipkow@31952
   746
lemma dvd_prime_factors_int [intro]:
nipkow@31719
   747
    "0 < (y::int) \<Longrightarrow> 0 <= x \<Longrightarrow> x dvd y \<Longrightarrow> prime_factors x <= prime_factors y"
nipkow@31952
   748
  apply (auto simp add: prime_factors_altdef_int)
nipkow@31719
   749
  apply (erule order_less_le_trans)
nipkow@31952
   750
  apply (rule dvd_multiplicity_int)
nipkow@31719
   751
  apply auto
nipkow@31719
   752
done
nipkow@31719
   753
nipkow@31952
   754
lemma multiplicity_dvd_nat: "0 < (x::nat) \<Longrightarrow> 0 < y \<Longrightarrow> 
nipkow@31719
   755
    ALL p. multiplicity p x <= multiplicity p y \<Longrightarrow>
nipkow@31719
   756
      x dvd y"
nipkow@31952
   757
  apply (subst prime_factorization_nat [of x], assumption)
nipkow@31952
   758
  apply (subst prime_factorization_nat [of y], assumption)
nipkow@31719
   759
  apply (rule setprod_dvd_setprod_subset2)
nipkow@31719
   760
  apply force
nipkow@31952
   761
  apply (subst prime_factors_altdef_nat)+
nipkow@31719
   762
  apply auto
nipkow@31719
   763
(* Again, a shame that auto and arith don't get this. *)
nipkow@31719
   764
  apply (drule_tac x = xa in spec, auto)
nipkow@31719
   765
  apply (rule le_imp_power_dvd)
nipkow@31719
   766
  apply blast
nipkow@31719
   767
done
nipkow@31719
   768
nipkow@31952
   769
lemma multiplicity_dvd_int: "0 < (x::int) \<Longrightarrow> 0 < y \<Longrightarrow> 
nipkow@31719
   770
    ALL p >= 0. multiplicity p x <= multiplicity p y \<Longrightarrow>
nipkow@31719
   771
      x dvd y"
nipkow@31952
   772
  apply (subst prime_factorization_int [of x], assumption)
nipkow@31952
   773
  apply (subst prime_factorization_int [of y], assumption)
nipkow@31719
   774
  apply (rule setprod_dvd_setprod_subset2)
nipkow@31719
   775
  apply force
nipkow@31952
   776
  apply (subst prime_factors_altdef_int)+
nipkow@31719
   777
  apply auto
nipkow@31719
   778
  apply (rule dvd_power_le)
nipkow@31719
   779
  apply auto
nipkow@31719
   780
  apply (drule_tac x = xa in spec)
nipkow@31719
   781
  apply (erule impE)
nipkow@31719
   782
  apply auto
nipkow@31719
   783
done
nipkow@31719
   784
nipkow@31952
   785
lemma multiplicity_dvd'_nat: "(0::nat) < x \<Longrightarrow> 
nipkow@31719
   786
    \<forall>p. prime p \<longrightarrow> multiplicity p x \<le> multiplicity p y \<Longrightarrow> x dvd y"
nipkow@31719
   787
  apply (cases "y = 0")
nipkow@31719
   788
  apply auto
nipkow@31952
   789
  apply (rule multiplicity_dvd_nat, auto)
nipkow@31719
   790
  apply (case_tac "prime p")
nipkow@31719
   791
  apply auto
nipkow@31719
   792
done
nipkow@31719
   793
nipkow@31952
   794
lemma multiplicity_dvd'_int: "(0::int) < x \<Longrightarrow> 0 <= y \<Longrightarrow>
nipkow@31719
   795
    \<forall>p. prime p \<longrightarrow> multiplicity p x \<le> multiplicity p y \<Longrightarrow> x dvd y"
nipkow@31719
   796
  apply (cases "y = 0")
nipkow@31719
   797
  apply auto
nipkow@31952
   798
  apply (rule multiplicity_dvd_int, auto)
nipkow@31719
   799
  apply (case_tac "prime p")
nipkow@31719
   800
  apply auto
nipkow@31719
   801
done
nipkow@31719
   802
nipkow@31952
   803
lemma dvd_multiplicity_eq_nat: "0 < (x::nat) \<Longrightarrow> 0 < y \<Longrightarrow>
nipkow@31719
   804
    (x dvd y) = (ALL p. multiplicity p x <= multiplicity p y)"
nipkow@31952
   805
  by (auto intro: dvd_multiplicity_nat multiplicity_dvd_nat)
nipkow@31719
   806
nipkow@31952
   807
lemma dvd_multiplicity_eq_int: "0 < (x::int) \<Longrightarrow> 0 < y \<Longrightarrow>
nipkow@31719
   808
    (x dvd y) = (ALL p >= 0. multiplicity p x <= multiplicity p y)"
nipkow@31952
   809
  by (auto intro: dvd_multiplicity_int multiplicity_dvd_int)
nipkow@31719
   810
nipkow@31952
   811
lemma prime_factors_altdef2_nat: "(n::nat) > 0 \<Longrightarrow> 
nipkow@31719
   812
    (p : prime_factors n) = (prime p & p dvd n)"
nipkow@31719
   813
  apply (case_tac "prime p")
nipkow@31719
   814
  apply auto
nipkow@31952
   815
  apply (subst prime_factorization_nat [where n = n], assumption)
nipkow@31719
   816
  apply (rule dvd_trans) 
nipkow@31719
   817
  apply (rule dvd_power [where x = p and n = "multiplicity p n"])
nipkow@31952
   818
  apply (subst (asm) prime_factors_altdef_nat, force)
nipkow@31719
   819
  apply (rule dvd_setprod)
nipkow@31719
   820
  apply auto  
nipkow@31952
   821
  apply (subst prime_factors_altdef_nat)
nipkow@31952
   822
  apply (subst (asm) dvd_multiplicity_eq_nat)
nipkow@31719
   823
  apply auto
nipkow@31719
   824
  apply (drule spec [where x = p])
nipkow@31719
   825
  apply auto
nipkow@31719
   826
done
nipkow@31719
   827
nipkow@31952
   828
lemma prime_factors_altdef2_int: 
nipkow@31719
   829
  assumes "(n::int) > 0" 
nipkow@31719
   830
  shows "(p : prime_factors n) = (prime p & p dvd n)"
nipkow@31719
   831
nipkow@31719
   832
  apply (case_tac "p >= 0")
nipkow@31952
   833
  apply (rule prime_factors_altdef2_nat [transferred])
nipkow@31719
   834
  using prems apply auto
nipkow@31952
   835
  apply (auto simp add: prime_ge_0_int prime_factors_ge_0_int)
nipkow@31719
   836
done
nipkow@31719
   837
nipkow@31952
   838
lemma multiplicity_eq_nat:
nipkow@31719
   839
  fixes x and y::nat 
nipkow@31719
   840
  assumes [arith]: "x > 0" "y > 0" and
nipkow@31719
   841
    mult_eq [simp]: "!!p. prime p \<Longrightarrow> multiplicity p x = multiplicity p y"
nipkow@31719
   842
  shows "x = y"
nipkow@31719
   843
nipkow@33657
   844
  apply (rule dvd_antisym)
nipkow@31952
   845
  apply (auto intro: multiplicity_dvd'_nat) 
nipkow@31719
   846
done
nipkow@31719
   847
nipkow@31952
   848
lemma multiplicity_eq_int:
nipkow@31719
   849
  fixes x and y::int 
nipkow@31719
   850
  assumes [arith]: "x > 0" "y > 0" and
nipkow@31719
   851
    mult_eq [simp]: "!!p. prime p \<Longrightarrow> multiplicity p x = multiplicity p y"
nipkow@31719
   852
  shows "x = y"
nipkow@31719
   853
nipkow@33657
   854
  apply (rule dvd_antisym [transferred])
nipkow@31952
   855
  apply (auto intro: multiplicity_dvd'_int) 
nipkow@31719
   856
done
nipkow@31719
   857
nipkow@31719
   858
nipkow@31719
   859
subsection {* An application *}
nipkow@31719
   860
nipkow@31952
   861
lemma gcd_eq_nat: 
nipkow@31719
   862
  assumes pos [arith]: "x > 0" "y > 0"
nipkow@31719
   863
  shows "gcd (x::nat) y = 
nipkow@31719
   864
    (PROD p: prime_factors x Un prime_factors y. 
nipkow@31719
   865
      p ^ (min (multiplicity p x) (multiplicity p y)))"
nipkow@31719
   866
proof -
nipkow@31719
   867
  def z == "(PROD p: prime_factors (x::nat) Un prime_factors y. 
nipkow@31719
   868
      p ^ (min (multiplicity p x) (multiplicity p y)))"
nipkow@31719
   869
  have [arith]: "z > 0"
nipkow@31719
   870
    unfolding z_def by (rule setprod_pos_nat, auto)
nipkow@31719
   871
  have aux: "!!p. prime p \<Longrightarrow> multiplicity p z = 
nipkow@31719
   872
      min (multiplicity p x) (multiplicity p y)"
nipkow@31719
   873
    unfolding z_def
nipkow@31952
   874
    apply (subst multiplicity_prod_prime_powers_nat)
nipkow@31952
   875
    apply (auto simp add: multiplicity_not_factor_nat)
nipkow@31719
   876
    done
nipkow@31719
   877
  have "z dvd x" 
nipkow@31952
   878
    by (intro multiplicity_dvd'_nat, auto simp add: aux)
nipkow@31719
   879
  moreover have "z dvd y" 
nipkow@31952
   880
    by (intro multiplicity_dvd'_nat, auto simp add: aux)
nipkow@31719
   881
  moreover have "ALL w. w dvd x & w dvd y \<longrightarrow> w dvd z"
nipkow@31719
   882
    apply auto
nipkow@31719
   883
    apply (case_tac "w = 0", auto)
nipkow@31952
   884
    apply (erule multiplicity_dvd'_nat)
nipkow@31952
   885
    apply (auto intro: dvd_multiplicity_nat simp add: aux)
nipkow@31719
   886
    done
nipkow@31719
   887
  ultimately have "z = gcd x y"
nipkow@31952
   888
    by (subst gcd_unique_nat [symmetric], blast)
nipkow@31719
   889
  thus ?thesis
nipkow@31719
   890
    unfolding z_def by auto
nipkow@31719
   891
qed
nipkow@31719
   892
nipkow@31952
   893
lemma lcm_eq_nat: 
nipkow@31719
   894
  assumes pos [arith]: "x > 0" "y > 0"
nipkow@31719
   895
  shows "lcm (x::nat) y = 
nipkow@31719
   896
    (PROD p: prime_factors x Un prime_factors y. 
nipkow@31719
   897
      p ^ (max (multiplicity p x) (multiplicity p y)))"
nipkow@31719
   898
proof -
nipkow@31719
   899
  def z == "(PROD p: prime_factors (x::nat) Un prime_factors y. 
nipkow@31719
   900
      p ^ (max (multiplicity p x) (multiplicity p y)))"
nipkow@31719
   901
  have [arith]: "z > 0"
nipkow@31719
   902
    unfolding z_def by (rule setprod_pos_nat, auto)
nipkow@31719
   903
  have aux: "!!p. prime p \<Longrightarrow> multiplicity p z = 
nipkow@31719
   904
      max (multiplicity p x) (multiplicity p y)"
nipkow@31719
   905
    unfolding z_def
nipkow@31952
   906
    apply (subst multiplicity_prod_prime_powers_nat)
nipkow@31952
   907
    apply (auto simp add: multiplicity_not_factor_nat)
nipkow@31719
   908
    done
nipkow@31719
   909
  have "x dvd z" 
nipkow@31952
   910
    by (intro multiplicity_dvd'_nat, auto simp add: aux)
nipkow@31719
   911
  moreover have "y dvd z" 
nipkow@31952
   912
    by (intro multiplicity_dvd'_nat, auto simp add: aux)
nipkow@31719
   913
  moreover have "ALL w. x dvd w & y dvd w \<longrightarrow> z dvd w"
nipkow@31719
   914
    apply auto
nipkow@31719
   915
    apply (case_tac "w = 0", auto)
nipkow@31952
   916
    apply (rule multiplicity_dvd'_nat)
nipkow@31952
   917
    apply (auto intro: dvd_multiplicity_nat simp add: aux)
nipkow@31719
   918
    done
nipkow@31719
   919
  ultimately have "z = lcm x y"
nipkow@31952
   920
    by (subst lcm_unique_nat [symmetric], blast)
nipkow@31719
   921
  thus ?thesis
nipkow@31719
   922
    unfolding z_def by auto
nipkow@31719
   923
qed
nipkow@31719
   924
nipkow@31952
   925
lemma multiplicity_gcd_nat: 
nipkow@31719
   926
  assumes [arith]: "x > 0" "y > 0"
nipkow@31719
   927
  shows "multiplicity (p::nat) (gcd x y) = 
nipkow@31719
   928
    min (multiplicity p x) (multiplicity p y)"
nipkow@31719
   929
nipkow@31952
   930
  apply (subst gcd_eq_nat)
nipkow@31719
   931
  apply auto
nipkow@31952
   932
  apply (subst multiplicity_prod_prime_powers_nat)
nipkow@31719
   933
  apply auto
nipkow@31719
   934
done
nipkow@31719
   935
nipkow@31952
   936
lemma multiplicity_lcm_nat: 
nipkow@31719
   937
  assumes [arith]: "x > 0" "y > 0"
nipkow@31719
   938
  shows "multiplicity (p::nat) (lcm x y) = 
nipkow@31719
   939
    max (multiplicity p x) (multiplicity p y)"
nipkow@31719
   940
nipkow@31952
   941
  apply (subst lcm_eq_nat)
nipkow@31719
   942
  apply auto
nipkow@31952
   943
  apply (subst multiplicity_prod_prime_powers_nat)
nipkow@31719
   944
  apply auto
nipkow@31719
   945
done
nipkow@31719
   946
nipkow@31952
   947
lemma gcd_lcm_distrib_nat: "gcd (x::nat) (lcm y z) = lcm (gcd x y) (gcd x z)"
nipkow@31719
   948
  apply (case_tac "x = 0 | y = 0 | z = 0") 
nipkow@31719
   949
  apply auto
nipkow@31952
   950
  apply (rule multiplicity_eq_nat)
nipkow@31952
   951
  apply (auto simp add: multiplicity_gcd_nat multiplicity_lcm_nat 
nipkow@31952
   952
      lcm_pos_nat)
nipkow@31719
   953
done
nipkow@31719
   954
nipkow@31952
   955
lemma gcd_lcm_distrib_int: "gcd (x::int) (lcm y z) = lcm (gcd x y) (gcd x z)"
nipkow@31952
   956
  apply (subst (1 2 3) gcd_abs_int)
nipkow@31952
   957
  apply (subst lcm_abs_int)
nipkow@31719
   958
  apply (subst (2) abs_of_nonneg)
nipkow@31719
   959
  apply force
nipkow@31952
   960
  apply (rule gcd_lcm_distrib_nat [transferred])
nipkow@31719
   961
  apply auto
nipkow@31719
   962
done
nipkow@31719
   963
nipkow@31719
   964
end