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