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