src/HOL/NumberTheory/Factorization.thy
author wenzelm
Fri Mar 28 19:43:54 2008 +0100 (2008-03-28)
changeset 26462 dac4e2bce00d
parent 26316 9e9e67e33557
child 27368 9f90ac19e32b
permissions -rw-r--r--
avoid rebinding of existing facts;
wenzelm@11049
     1
(*  Title:      HOL/NumberTheory/Factorization.thy
paulson@9944
     2
    ID:         $Id$
paulson@9944
     3
    Author:     Thomas Marthedal Rasmussen
paulson@9944
     4
    Copyright   2000  University of Cambridge
wenzelm@11049
     5
*)
paulson@9944
     6
wenzelm@11049
     7
header {* Fundamental Theorem of Arithmetic (unique factorization into primes) *}
wenzelm@11049
     8
haftmann@16417
     9
theory Factorization imports Primes Permutation begin
paulson@9944
    10
paulson@9944
    11
wenzelm@11049
    12
subsection {* Definitions *}
paulson@9944
    13
wenzelm@19670
    14
definition
wenzelm@21404
    15
  primel :: "nat list => bool" where
wenzelm@19670
    16
  "primel xs = (\<forall>p \<in> set xs. prime p)"
wenzelm@19670
    17
paulson@9944
    18
consts
wenzelm@11049
    19
  nondec :: "nat list => bool "
wenzelm@11049
    20
  prod :: "nat list => nat"
wenzelm@11049
    21
  oinsert :: "nat => nat list => nat list"
wenzelm@11049
    22
  sort :: "nat list => nat list"
paulson@9944
    23
wenzelm@11049
    24
primrec
wenzelm@11049
    25
  "nondec [] = True"
wenzelm@11049
    26
  "nondec (x # xs) = (case xs of [] => True | y # ys => x \<le> y \<and> nondec xs)"
paulson@9944
    27
paulson@9944
    28
primrec
wenzelm@11701
    29
  "prod [] = Suc 0"
wenzelm@11049
    30
  "prod (x # xs) = x * prod xs"
wenzelm@11049
    31
wenzelm@11049
    32
primrec
wenzelm@11049
    33
  "oinsert x [] = [x]"
wenzelm@11049
    34
  "oinsert x (y # ys) = (if x \<le> y then x # y # ys else y # oinsert x ys)"
paulson@9944
    35
paulson@9944
    36
primrec
wenzelm@11049
    37
  "sort [] = []"
wenzelm@11049
    38
  "sort (x # xs) = oinsert x (sort xs)"
wenzelm@11049
    39
wenzelm@11049
    40
wenzelm@11049
    41
subsection {* Arithmetic *}
wenzelm@11049
    42
wenzelm@11701
    43
lemma one_less_m: "(m::nat) \<noteq> m * k ==> m \<noteq> Suc 0 ==> Suc 0 < m"
wenzelm@19670
    44
  apply (cases m)
wenzelm@11049
    45
   apply auto
wenzelm@11049
    46
  done
wenzelm@11049
    47
wenzelm@11701
    48
lemma one_less_k: "(m::nat) \<noteq> m * k ==> Suc 0 < m * k ==> Suc 0 < k"
wenzelm@19670
    49
  apply (cases k)
wenzelm@11049
    50
   apply auto
wenzelm@11049
    51
  done
wenzelm@11049
    52
wenzelm@11049
    53
lemma mult_left_cancel: "(0::nat) < k ==> k * n = k * m ==> n = m"
wenzelm@11049
    54
  apply auto
wenzelm@11049
    55
  done
wenzelm@11049
    56
wenzelm@11701
    57
lemma mn_eq_m_one: "(0::nat) < m ==> m * n = m ==> n = Suc 0"
wenzelm@19670
    58
  apply (cases n)
wenzelm@11049
    59
   apply auto
wenzelm@11049
    60
  done
wenzelm@11049
    61
wenzelm@11049
    62
lemma prod_mn_less_k:
wenzelm@11701
    63
    "(0::nat) < n ==> 0 < k ==> Suc 0 < m ==> m * n = k ==> n < k"
wenzelm@11049
    64
  apply (induct m)
wenzelm@11049
    65
   apply auto
wenzelm@11049
    66
  done
wenzelm@11049
    67
wenzelm@11049
    68
wenzelm@11049
    69
subsection {* Prime list and product *}
wenzelm@11049
    70
wenzelm@11049
    71
lemma prod_append: "prod (xs @ ys) = prod xs * prod ys"
wenzelm@11049
    72
  apply (induct xs)
wenzelm@11049
    73
   apply (simp_all add: mult_assoc)
wenzelm@11049
    74
  done
wenzelm@11049
    75
wenzelm@11049
    76
lemma prod_xy_prod:
wenzelm@11049
    77
    "prod (x # xs) = prod (y # ys) ==> x * prod xs = y * prod ys"
wenzelm@11049
    78
  apply auto
wenzelm@11049
    79
  done
wenzelm@11049
    80
wenzelm@11049
    81
lemma primel_append: "primel (xs @ ys) = (primel xs \<and> primel ys)"
wenzelm@11049
    82
  apply (unfold primel_def)
wenzelm@11049
    83
  apply auto
wenzelm@11049
    84
  done
wenzelm@11049
    85
nipkow@16663
    86
lemma prime_primel: "prime n ==> primel [n] \<and> prod [n] = n"
wenzelm@11049
    87
  apply (unfold primel_def)
wenzelm@11049
    88
  apply auto
wenzelm@11049
    89
  done
wenzelm@11049
    90
nipkow@16663
    91
lemma prime_nd_one: "prime p ==> \<not> p dvd Suc 0"
wenzelm@11049
    92
  apply (unfold prime_def dvd_def)
wenzelm@11049
    93
  apply auto
wenzelm@11049
    94
  done
wenzelm@11049
    95
paulson@23814
    96
lemma hd_dvd_prod: "prod (x # xs) = prod ys ==> x dvd (prod ys)" 
paulson@23814
    97
  by (metis dvd_mult_left dvd_refl prod.simps(2))
wenzelm@11049
    98
wenzelm@11049
    99
lemma primel_tl: "primel (x # xs) ==> primel xs"
wenzelm@11049
   100
  apply (unfold primel_def)
wenzelm@11049
   101
  apply auto
wenzelm@11049
   102
  done
wenzelm@11049
   103
nipkow@16663
   104
lemma primel_hd_tl: "(primel (x # xs)) = (prime x \<and> primel xs)"
wenzelm@11049
   105
  apply (unfold primel_def)
wenzelm@11049
   106
  apply auto
wenzelm@11049
   107
  done
wenzelm@11049
   108
nipkow@16663
   109
lemma primes_eq: "prime p ==> prime q ==> p dvd q ==> p = q"
wenzelm@11049
   110
  apply (unfold prime_def)
wenzelm@11049
   111
  apply auto
wenzelm@11049
   112
  done
wenzelm@11049
   113
wenzelm@11701
   114
lemma primel_one_empty: "primel xs ==> prod xs = Suc 0 ==> xs = []"
wenzelm@19670
   115
  apply (cases xs)
wenzelm@19670
   116
   apply (simp_all add: primel_def prime_def)
wenzelm@11049
   117
  done
wenzelm@11049
   118
nipkow@16663
   119
lemma prime_g_one: "prime p ==> Suc 0 < p"
wenzelm@11049
   120
  apply (unfold prime_def)
wenzelm@11049
   121
  apply auto
wenzelm@11049
   122
  done
wenzelm@11049
   123
nipkow@16663
   124
lemma prime_g_zero: "prime p ==> 0 < p"
wenzelm@11049
   125
  apply (unfold prime_def)
wenzelm@11049
   126
  apply auto
wenzelm@11049
   127
  done
wenzelm@11049
   128
wenzelm@19670
   129
lemma primel_nempty_g_one:
wenzelm@19670
   130
    "primel xs \<Longrightarrow> xs \<noteq> [] \<Longrightarrow> Suc 0 < prod xs"
wenzelm@11049
   131
  apply (induct xs)
wenzelm@19670
   132
   apply simp
wenzelm@19670
   133
  apply (fastsimp simp: primel_def prime_def elim: one_less_mult)
wenzelm@11049
   134
  done
wenzelm@11049
   135
wenzelm@11049
   136
lemma primel_prod_gz: "primel xs ==> 0 < prod xs"
wenzelm@11049
   137
  apply (induct xs)
wenzelm@19670
   138
   apply (auto simp: primel_def prime_def)
wenzelm@11049
   139
  done
wenzelm@11049
   140
wenzelm@11049
   141
wenzelm@11049
   142
subsection {* Sorting *}
wenzelm@11049
   143
wenzelm@19670
   144
lemma nondec_oinsert: "nondec xs \<Longrightarrow> nondec (oinsert x xs)"
wenzelm@11049
   145
  apply (induct xs)
wenzelm@19670
   146
   apply simp
wenzelm@19670
   147
   apply (case_tac xs)
wenzelm@11049
   148
    apply (simp_all cong del: list.weak_case_cong)
wenzelm@11049
   149
  done
wenzelm@11049
   150
wenzelm@11049
   151
lemma nondec_sort: "nondec (sort xs)"
wenzelm@11049
   152
  apply (induct xs)
wenzelm@11049
   153
   apply simp_all
wenzelm@11049
   154
  apply (erule nondec_oinsert)
wenzelm@11049
   155
  done
wenzelm@11049
   156
wenzelm@11049
   157
lemma x_less_y_oinsert: "x \<le> y ==> l = y # ys ==> x # l = oinsert x l"
wenzelm@11049
   158
  apply simp_all
wenzelm@11049
   159
  done
wenzelm@11049
   160
wenzelm@19670
   161
lemma nondec_sort_eq [rule_format]: "nondec xs \<longrightarrow> xs = sort xs"
wenzelm@11049
   162
  apply (induct xs)
wenzelm@11049
   163
   apply safe
wenzelm@11049
   164
    apply simp_all
nipkow@15236
   165
   apply (case_tac xs)
wenzelm@11049
   166
    apply simp_all
nipkow@15236
   167
  apply (case_tac xs)
wenzelm@11049
   168
   apply simp
nipkow@15236
   169
  apply (rule_tac y = aa and ys = list in x_less_y_oinsert)
wenzelm@11049
   170
   apply simp_all
wenzelm@11049
   171
  done
wenzelm@11049
   172
wenzelm@11049
   173
lemma oinsert_x_y: "oinsert x (oinsert y l) = oinsert y (oinsert x l)"
wenzelm@11049
   174
  apply (induct l)
wenzelm@11049
   175
  apply auto
wenzelm@11049
   176
  done
wenzelm@11049
   177
wenzelm@11049
   178
wenzelm@11049
   179
subsection {* Permutation *}
wenzelm@11049
   180
wenzelm@11049
   181
lemma perm_primel [rule_format]: "xs <~~> ys ==> primel xs --> primel ys"
wenzelm@11049
   182
  apply (unfold primel_def)
wenzelm@19670
   183
  apply (induct set: perm)
nipkow@16663
   184
     apply simp
nipkow@16663
   185
    apply simp
nipkow@16663
   186
   apply (simp (no_asm))
nipkow@16663
   187
   apply blast
nipkow@16663
   188
  apply blast
wenzelm@11049
   189
  done
wenzelm@11049
   190
wenzelm@19670
   191
lemma perm_prod: "xs <~~> ys ==> prod xs = prod ys"
wenzelm@19670
   192
  apply (induct set: perm)
wenzelm@11049
   193
     apply (simp_all add: mult_ac)
wenzelm@11049
   194
  done
paulson@9944
   195
wenzelm@11049
   196
lemma perm_subst_oinsert: "xs <~~> ys ==> oinsert a xs <~~> oinsert a ys"
wenzelm@19670
   197
  apply (induct set: perm)
wenzelm@11049
   198
     apply auto
wenzelm@11049
   199
  done
wenzelm@11049
   200
wenzelm@11049
   201
lemma perm_oinsert: "x # xs <~~> oinsert x xs"
wenzelm@11049
   202
  apply (induct xs)
wenzelm@11049
   203
   apply auto
wenzelm@11049
   204
  done
wenzelm@11049
   205
wenzelm@11049
   206
lemma perm_sort: "xs <~~> sort xs"
wenzelm@11049
   207
  apply (induct xs)
wenzelm@11049
   208
  apply (auto intro: perm_oinsert elim: perm_subst_oinsert)
wenzelm@11049
   209
  done
wenzelm@11049
   210
wenzelm@11049
   211
lemma perm_sort_eq: "xs <~~> ys ==> sort xs = sort ys"
wenzelm@19670
   212
  apply (induct set: perm)
wenzelm@11049
   213
     apply (simp_all add: oinsert_x_y)
wenzelm@11049
   214
  done
wenzelm@11049
   215
wenzelm@11049
   216
wenzelm@11049
   217
subsection {* Existence *}
wenzelm@11049
   218
wenzelm@11049
   219
lemma ex_nondec_lemma:
wenzelm@11049
   220
    "primel xs ==> \<exists>ys. primel ys \<and> nondec ys \<and> prod ys = prod xs"
wenzelm@11049
   221
  apply (blast intro: nondec_sort perm_prod perm_primel perm_sort perm_sym)
wenzelm@11049
   222
  done
wenzelm@11049
   223
wenzelm@11049
   224
lemma not_prime_ex_mk:
nipkow@16663
   225
  "Suc 0 < n \<and> \<not> prime n ==>
wenzelm@11701
   226
    \<exists>m k. Suc 0 < m \<and> Suc 0 < k \<and> m < n \<and> k < n \<and> n = m * k"
wenzelm@11049
   227
  apply (unfold prime_def dvd_def)
wenzelm@11049
   228
  apply (auto intro: n_less_m_mult_n n_less_n_mult_m one_less_m one_less_k)
wenzelm@11049
   229
  done
wenzelm@11049
   230
wenzelm@11049
   231
lemma split_primel:
wenzelm@25687
   232
  "primel xs \<Longrightarrow> primel ys \<Longrightarrow> \<exists>l. primel l \<and> prod l = prod xs * prod ys"
wenzelm@25687
   233
  apply (rule exI)
wenzelm@25687
   234
  apply safe
wenzelm@25687
   235
   apply (rule_tac [2] prod_append)
wenzelm@25687
   236
  apply (simp add: primel_append)
wenzelm@25687
   237
  done
wenzelm@11049
   238
wenzelm@11701
   239
lemma factor_exists [rule_format]: "Suc 0 < n --> (\<exists>l. primel l \<and> prod l = n)"
wenzelm@11049
   240
  apply (induct n rule: nat_less_induct)
wenzelm@11049
   241
  apply (rule impI)
nipkow@16663
   242
  apply (case_tac "prime n")
wenzelm@11049
   243
   apply (rule exI)
wenzelm@11049
   244
   apply (erule prime_primel)
wenzelm@11049
   245
  apply (cut_tac n = n in not_prime_ex_mk)
wenzelm@11049
   246
   apply (auto intro!: split_primel)
wenzelm@11049
   247
  done
wenzelm@11049
   248
wenzelm@11701
   249
lemma nondec_factor_exists: "Suc 0 < n ==> \<exists>l. primel l \<and> nondec l \<and> prod l = n"
wenzelm@11049
   250
  apply (erule factor_exists [THEN exE])
wenzelm@11049
   251
  apply (blast intro!: ex_nondec_lemma)
wenzelm@11049
   252
  done
wenzelm@11049
   253
wenzelm@11049
   254
wenzelm@11049
   255
subsection {* Uniqueness *}
wenzelm@11049
   256
wenzelm@11049
   257
lemma prime_dvd_mult_list [rule_format]:
nipkow@16663
   258
    "prime p ==> p dvd (prod xs) --> (\<exists>m. m:set xs \<and> p dvd m)"
wenzelm@11049
   259
  apply (induct xs)
paulson@11364
   260
   apply (force simp add: prime_def)
paulson@11364
   261
   apply (force dest: prime_dvd_mult)
wenzelm@11049
   262
  done
wenzelm@11049
   263
wenzelm@11049
   264
lemma hd_xs_dvd_prod:
wenzelm@11049
   265
  "primel (x # xs) ==> primel ys ==> prod (x # xs) = prod ys
wenzelm@11049
   266
    ==> \<exists>m. m \<in> set ys \<and> x dvd m"
wenzelm@11049
   267
  apply (rule prime_dvd_mult_list)
wenzelm@11049
   268
   apply (simp add: primel_hd_tl)
wenzelm@11049
   269
  apply (erule hd_dvd_prod)
wenzelm@11049
   270
  done
wenzelm@11049
   271
wenzelm@11049
   272
lemma prime_dvd_eq: "primel (x # xs) ==> primel ys ==> m \<in> set ys ==> x dvd m ==> x = m"
wenzelm@11049
   273
  apply (rule primes_eq)
wenzelm@11049
   274
    apply (auto simp add: primel_def primel_hd_tl)
wenzelm@11049
   275
  done
paulson@9944
   276
wenzelm@11049
   277
lemma hd_xs_eq_prod:
wenzelm@11049
   278
  "primel (x # xs) ==>
wenzelm@11049
   279
    primel ys ==> prod (x # xs) = prod ys ==> x \<in> set ys"
wenzelm@11049
   280
  apply (frule hd_xs_dvd_prod)
wenzelm@11049
   281
    apply auto
wenzelm@11049
   282
  apply (drule prime_dvd_eq)
wenzelm@11049
   283
     apply auto
wenzelm@11049
   284
  done
wenzelm@11049
   285
wenzelm@11049
   286
lemma perm_primel_ex:
wenzelm@11049
   287
  "primel (x # xs) ==>
wenzelm@11049
   288
    primel ys ==> prod (x # xs) = prod ys ==> \<exists>l. ys <~~> (x # l)"
wenzelm@11049
   289
  apply (rule exI)
wenzelm@11049
   290
  apply (rule perm_remove)
wenzelm@11049
   291
  apply (erule hd_xs_eq_prod)
wenzelm@11049
   292
   apply simp_all
wenzelm@11049
   293
  done
wenzelm@11049
   294
wenzelm@11049
   295
lemma primel_prod_less:
wenzelm@11049
   296
  "primel (x # xs) ==>
wenzelm@11049
   297
    primel ys ==> prod (x # xs) = prod ys ==> prod xs < prod ys"
wenzelm@26316
   298
  by (metis less_asym linorder_neqE_nat mult_less_cancel2 nat_0_less_mult_iff
wenzelm@25180
   299
    nat_less_le nat_mult_1 prime_def primel_hd_tl primel_prod_gz prod.simps(2))
wenzelm@11049
   300
wenzelm@11049
   301
lemma prod_one_empty:
nipkow@16663
   302
    "primel xs ==> p * prod xs = p ==> prime p ==> xs = []"
wenzelm@11049
   303
  apply (auto intro: primel_one_empty simp add: prime_def)
wenzelm@11049
   304
  done
wenzelm@11049
   305
wenzelm@11049
   306
lemma uniq_ex_aux:
wenzelm@11049
   307
  "\<forall>m. m < prod ys --> (\<forall>xs ys. primel xs \<and> primel ys \<and>
wenzelm@11049
   308
      prod xs = prod ys \<and> prod xs = m --> xs <~~> ys) ==>
wenzelm@11049
   309
    primel list ==> primel x ==> prod list = prod x ==> prod x < prod ys
wenzelm@11049
   310
    ==> x <~~> list"
wenzelm@11049
   311
  apply simp
wenzelm@11049
   312
  done
paulson@9944
   313
wenzelm@11049
   314
lemma factor_unique [rule_format]:
wenzelm@11049
   315
  "\<forall>xs ys. primel xs \<and> primel ys \<and> prod xs = prod ys \<and> prod xs = n
wenzelm@11049
   316
    --> xs <~~> ys"
wenzelm@11049
   317
  apply (induct n rule: nat_less_induct)
wenzelm@11049
   318
  apply safe
wenzelm@11049
   319
  apply (case_tac xs)
wenzelm@11049
   320
   apply (force intro: primel_one_empty)
wenzelm@11049
   321
  apply (rule perm_primel_ex [THEN exE])
wenzelm@11049
   322
     apply simp_all
wenzelm@11049
   323
  apply (rule perm.trans [THEN perm_sym])
wenzelm@11049
   324
  apply assumption
wenzelm@11049
   325
  apply (rule perm.Cons)
wenzelm@11049
   326
  apply (case_tac "x = []")
paulson@25493
   327
   apply (metis perm_prod perm_refl prime_primel primel_hd_tl primel_tl prod_one_empty)
paulson@25493
   328
  apply (metis nat_0_less_mult_iff nat_mult_eq_cancel1 perm_primel perm_prod primel_prod_gz primel_prod_less primel_tl prod.simps(2))
wenzelm@11049
   329
  done
wenzelm@11049
   330
wenzelm@11049
   331
lemma perm_nondec_unique:
wenzelm@11049
   332
    "xs <~~> ys ==> nondec xs ==> nondec ys ==> xs = ys"
paulson@23814
   333
  by (metis nondec_sort_eq perm_sort_eq)
paulson@23814
   334
paulson@25493
   335
theorem unique_prime_factorization [rule_format]:
wenzelm@11701
   336
    "\<forall>n. Suc 0 < n --> (\<exists>!l. primel l \<and> nondec l \<and> prod l = n)"
paulson@25493
   337
  by (metis factor_unique nondec_factor_exists perm_nondec_unique)
wenzelm@11049
   338
wenzelm@11049
   339
end