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