src/HOL/Algebra/Divisibility.thy
changeset 68684 9a42b84f8838
parent 68664 bd0df72c16d5
child 69597 ff784d5a5bfb
equal deleted inserted replaced
68683:d69127c6e80f 68684:9a42b84f8838
   545   assumes pf: "properfactor G a b"
   545   assumes pf: "properfactor G a b"
   546   shows "a divides b"
   546   shows "a divides b"
   547   using pf by (elim properfactorE)
   547   using pf by (elim properfactorE)
   548 
   548 
   549 lemma (in monoid) properfactor_trans1 [trans]:
   549 lemma (in monoid) properfactor_trans1 [trans]:
   550   assumes dvds: "a divides b"  "properfactor G b c"
   550   assumes "a divides b"  "properfactor G b c" "a \<in> carrier G"  "c \<in> carrier G"
   551     and carr: "a \<in> carrier G"  "c \<in> carrier G"
       
   552   shows "properfactor G a c"
   551   shows "properfactor G a c"
   553   using dvds carr
   552   by (meson divides_trans properfactorE properfactorI assms)
   554   apply (elim properfactorE, intro properfactorI)
       
   555    apply (iprover intro: divides_trans)+
       
   556   done
       
   557 
   553 
   558 lemma (in monoid) properfactor_trans2 [trans]:
   554 lemma (in monoid) properfactor_trans2 [trans]:
   559   assumes dvds: "properfactor G a b"  "b divides c"
   555   assumes "properfactor G a b"  "b divides c" "a \<in> carrier G"  "b \<in> carrier G"
   560     and carr: "a \<in> carrier G"  "b \<in> carrier G"
       
   561   shows "properfactor G a c"
   556   shows "properfactor G a c"
   562   using dvds carr
   557   by (meson divides_trans properfactorE properfactorI assms)
   563   apply (elim properfactorE, intro properfactorI)
       
   564    apply (iprover intro: divides_trans)+
       
   565   done
       
   566 
   558 
   567 lemma properfactor_lless:
   559 lemma properfactor_lless:
   568   fixes G (structure)
   560   fixes G (structure)
   569   shows "properfactor G = lless (division_rel G)"
   561   shows "properfactor G = lless (division_rel G)"
   570   by (force simp: lless_def properfactor_def associated_def)
   562   by (force simp: lless_def properfactor_def associated_def)
   658     and bcarr: "b \<in> carrier G"
   650     and bcarr: "b \<in> carrier G"
   659   shows "b \<in> Units G"
   651   shows "b \<in> Units G"
   660   using assms by (fast elim: irreducibleE)
   652   using assms by (fast elim: irreducibleE)
   661 
   653 
   662 lemma (in monoid_cancel) irreducible_cong [trans]:
   654 lemma (in monoid_cancel) irreducible_cong [trans]:
   663   assumes irred: "irreducible G a"
   655   assumes "irreducible G a" "a \<sim> a'" "a \<in> carrier G"  "a' \<in> carrier G"
   664     and aa': "a \<sim> a'" "a \<in> carrier G"  "a' \<in> carrier G"
       
   665   shows "irreducible G a'"
   656   shows "irreducible G a'"
       
   657 proof -
       
   658   have "a' divides a"
       
   659     by (meson \<open>a \<sim> a'\<close> associated_def)
       
   660   then show ?thesis
       
   661     by (metis (no_types) assms assoc_unit_l irreducibleE irreducibleI monoid.properfactor_trans2 monoid_axioms)
       
   662 qed
       
   663 
       
   664 lemma (in monoid) irreducible_prod_rI:
       
   665   assumes "irreducible G a" "b \<in> Units G" "a \<in> carrier G"  "b \<in> carrier G"
       
   666   shows "irreducible G (a \<otimes> b)"
   666   using assms
   667   using assms
   667   apply (auto simp: irreducible_def assoc_unit_l)
   668   by (metis (no_types, lifting) associatedI2' irreducible_def monoid.m_closed monoid_axioms prod_unit_r properfactor_cong_r)
   668   apply (metis aa' associated_sym properfactor_cong_r)
       
   669   done
       
   670 
       
   671 lemma (in monoid) irreducible_prod_rI:
       
   672   assumes airr: "irreducible G a"
       
   673     and bunit: "b \<in> Units G"
       
   674     and carr[simp]: "a \<in> carrier G"  "b \<in> carrier G"
       
   675   shows "irreducible G (a \<otimes> b)"
       
   676   using airr carr bunit
       
   677   apply (elim irreducibleE, intro irreducibleI)
       
   678   using prod_unit_r apply blast
       
   679   using associatedI2' properfactor_cong_r by auto
       
   680 
   669 
   681 lemma (in comm_monoid) irreducible_prod_lI:
   670 lemma (in comm_monoid) irreducible_prod_lI:
   682   assumes birr: "irreducible G b"
   671   assumes birr: "irreducible G b"
   683     and aunit: "a \<in> Units G"
   672     and aunit: "a \<in> Units G"
   684     and carr [simp]: "a \<in> carrier G"  "b \<in> carrier G"
   673     and carr [simp]: "a \<in> carrier G"  "b \<in> carrier G"
   762 lemma (in monoid_cancel) prime_cong [trans]:
   751 lemma (in monoid_cancel) prime_cong [trans]:
   763   assumes "prime G p"
   752   assumes "prime G p"
   764     and pp': "p \<sim> p'" "p \<in> carrier G"  "p' \<in> carrier G"
   753     and pp': "p \<sim> p'" "p \<in> carrier G"  "p' \<in> carrier G"
   765   shows "prime G p'"
   754   shows "prime G p'"
   766   using assms
   755   using assms
   767   apply (auto simp: prime_def assoc_unit_l)
   756   by (auto simp: prime_def assoc_unit_l) (metis pp' associated_sym divides_cong_l)
   768   apply (metis pp' associated_sym divides_cong_l)
       
   769   done
       
   770 
   757 
   771 (*by Paulo Emílio de Vilhena*)
   758 (*by Paulo Emílio de Vilhena*)
   772 lemma (in comm_monoid_cancel) prime_irreducible:
   759 lemma (in comm_monoid_cancel) prime_irreducible:
   773   assumes "prime G p"
   760   assumes "prime G p"
   774   shows "irreducible G p"
   761   shows "irreducible G p"
   847   assumes "\<forall>a\<in>set as. irreducible G a"
   834   assumes "\<forall>a\<in>set as. irreducible G a"
   848     and "as [\<sim>] bs"
   835     and "as [\<sim>] bs"
   849     and "set as \<subseteq> carrier G" and "set bs \<subseteq> carrier G"
   836     and "set as \<subseteq> carrier G" and "set bs \<subseteq> carrier G"
   850   shows "\<forall>a\<in>set bs. irreducible G a"
   837   shows "\<forall>a\<in>set bs. irreducible G a"
   851   using assms
   838   using assms
   852   apply (clarsimp simp add: list_all2_conv_all_nth set_conv_nth)
   839   by (fastforce simp add: list_all2_conv_all_nth set_conv_nth intro: irreducible_cong)
   853   apply (blast intro: irreducible_cong)
       
   854   done
       
   855 
   840 
   856 
   841 
   857 text \<open>Permutations\<close>
   842 text \<open>Permutations\<close>
   858 
   843 
   859 lemma perm_map [intro]:
   844 lemma perm_map [intro]:
   999 proof (induction fs)
   984 proof (induction fs)
  1000   case (Cons a fs)
   985   case (Cons a fs)
  1001   then have f: "f \<in> carrier G"
   986   then have f: "f \<in> carrier G"
  1002     by blast
   987     by blast
  1003   show ?case
   988   show ?case
  1004   proof (cases "f = a")
   989     using Cons.IH Cons.prems(1) Cons.prems(2) divides_prod_l f by auto
  1005     case True
       
  1006     then show ?thesis
       
  1007       using Cons.prems by auto
       
  1008   next
       
  1009     case False
       
  1010     with Cons show ?thesis
       
  1011       by clarsimp (metis f divides_prod_l multlist_closed)
       
  1012   qed
       
  1013 qed auto
   990 qed auto
  1014 
   991 
  1015 lemma (in comm_monoid_cancel) multlist_listassoc_cong:
   992 lemma (in comm_monoid_cancel) multlist_listassoc_cong:
  1016   assumes "fs [\<sim>] fs'"
   993   assumes "fs [\<sim>] fs'"
  1017     and "set fs \<subseteq> carrier G" and "set fs' \<subseteq> carrier G"
   994     and "set fs \<subseteq> carrier G" and "set fs' \<subseteq> carrier G"
  1049 lemma (in comm_monoid_cancel) multlist_ee_cong:
  1026 lemma (in comm_monoid_cancel) multlist_ee_cong:
  1050   assumes "essentially_equal G fs fs'"
  1027   assumes "essentially_equal G fs fs'"
  1051     and "set fs \<subseteq> carrier G" and "set fs' \<subseteq> carrier G"
  1028     and "set fs \<subseteq> carrier G" and "set fs' \<subseteq> carrier G"
  1052   shows "foldr (\<otimes>) fs \<one> \<sim> foldr (\<otimes>) fs' \<one>"
  1029   shows "foldr (\<otimes>) fs \<one> \<sim> foldr (\<otimes>) fs' \<one>"
  1053   using assms
  1030   using assms
  1054   apply (elim essentially_equalE)
  1031   by (metis essentially_equal_def multlist_listassoc_cong multlist_perm_cong perm_closed)
  1055   apply (simp add: multlist_perm_cong multlist_listassoc_cong perm_closed)
       
  1056   done
       
  1057 
  1032 
  1058 
  1033 
  1059 subsubsection \<open>Factorization in irreducible elements\<close>
  1034 subsubsection \<open>Factorization in irreducible elements\<close>
  1060 
  1035 
  1061 lemma wfactorsI:
  1036 lemma wfactorsI:
  1118   assumes aunit: "a \<in> Units G"
  1093   assumes aunit: "a \<in> Units G"
  1119     and wf: "wfactors G fs a"
  1094     and wf: "wfactors G fs a"
  1120     and carr[simp]: "set fs \<subseteq> carrier G"
  1095     and carr[simp]: "set fs \<subseteq> carrier G"
  1121   shows "fs = []"
  1096   shows "fs = []"
  1122 proof (cases fs)
  1097 proof (cases fs)
  1123   case Nil
       
  1124   then show ?thesis .
       
  1125 next
       
  1126   case fs: (Cons f fs')
  1098   case fs: (Cons f fs')
  1127   from carr have fcarr[simp]: "f \<in> carrier G" and carr'[simp]: "set fs' \<subseteq> carrier G"
  1099   from carr have fcarr[simp]: "f \<in> carrier G" and carr'[simp]: "set fs' \<subseteq> carrier G"
  1128     by (simp_all add: fs)
  1100     by (simp_all add: fs)
  1129 
  1101 
  1130   from fs wf have "irreducible G f" by (simp add: wfactors_def)
  1102   from fs wf have "irreducible G f" by (simp add: wfactors_def)
  1872   show "\<not> b divides a"
  1844   show "\<not> b divides a"
  1873     by (meson anb assms asubb factorial_monoid.divides_fmsubset factorial_monoid_axioms subset_mset.antisym)
  1845     by (meson anb assms asubb factorial_monoid.divides_fmsubset factorial_monoid_axioms subset_mset.antisym)
  1874 qed
  1846 qed
  1875 
  1847 
  1876 lemma (in factorial_monoid) properfactor_fmset:
  1848 lemma (in factorial_monoid) properfactor_fmset:
       
  1849   assumes "properfactor G a b"
       
  1850     and "wfactors G as a"
       
  1851     and "wfactors G bs b"
       
  1852     and "a \<in> carrier G"
       
  1853     and "b \<in> carrier G"
       
  1854     and "set as \<subseteq> carrier G"
       
  1855     and "set bs \<subseteq> carrier G"
       
  1856   shows "fmset G as \<subseteq># fmset G bs"
       
  1857   using assms
       
  1858   by (meson divides_as_fmsubset properfactor_divides)
       
  1859 
       
  1860 lemma (in factorial_monoid) properfactor_fmset_ne:
  1877   assumes pf: "properfactor G a b"
  1861   assumes pf: "properfactor G a b"
  1878     and "wfactors G as a"
  1862     and "wfactors G as a"
  1879     and "wfactors G bs b"
  1863     and "wfactors G bs b"
  1880     and "a \<in> carrier G"
  1864     and "a \<in> carrier G"
  1881     and "b \<in> carrier G"
  1865     and "b \<in> carrier G"
  1882     and "set as \<subseteq> carrier G"
  1866     and "set as \<subseteq> carrier G"
  1883     and "set bs \<subseteq> carrier G"
  1867     and "set bs \<subseteq> carrier G"
  1884   shows "fmset G as \<subseteq># fmset G bs \<and> fmset G as \<noteq> fmset G bs"
  1868   shows "fmset G as \<noteq> fmset G bs"
  1885   using pf
  1869   using properfactorE [OF pf] assms divides_as_fmsubset by force
  1886   apply safe
       
  1887    apply (meson assms divides_as_fmsubset monoid.properfactor_divides monoid_axioms)
       
  1888   by (meson assms associated_def comm_monoid_cancel.ee_wfactorsD comm_monoid_cancel.fmset_ee factorial_monoid_axioms factorial_monoid_def properfactorE)
       
  1889 
  1870 
  1890 subsection \<open>Irreducible Elements are Prime\<close>
  1871 subsection \<open>Irreducible Elements are Prime\<close>
  1891 
  1872 
  1892 lemma (in factorial_monoid) irreducible_prime:
  1873 lemma (in factorial_monoid) irreducible_prime:
  1893   assumes pirr: "irreducible G p" and pcarr: "p \<in> carrier G"
  1874   assumes pirr: "irreducible G p" and pcarr: "p \<in> carrier G"
  2244       by fast
  2225       by fast
  2245   qed
  2226   qed
  2246 qed
  2227 qed
  2247 
  2228 
  2248 lemma (in gcd_condition_monoid) gcdof_cong_l:
  2229 lemma (in gcd_condition_monoid) gcdof_cong_l:
  2249   assumes a'a: "a' \<sim> a"
  2230   assumes "a' \<sim> a" "a gcdof b c" "a' \<in> carrier G" and carr': "a \<in> carrier G"  "b \<in> carrier G"  "c \<in> carrier G"
  2250     and agcd: "a gcdof b c"
       
  2251     and a'carr: "a' \<in> carrier G" and carr': "a \<in> carrier G"  "b \<in> carrier G"  "c \<in> carrier G"
       
  2252   shows "a' gcdof b c"
  2231   shows "a' gcdof b c"
  2253 proof -
  2232 proof -
  2254   note carr = a'carr carr'
       
  2255   interpret weak_lower_semilattice "division_rel G" by simp
  2233   interpret weak_lower_semilattice "division_rel G" by simp
  2256   have "is_glb (division_rel G) a' {b, c}"
  2234   have "is_glb (division_rel G) a' {b, c}"
  2257     by (subst greatest_Lower_cong_l[of _ a]) (simp_all add: a'a carr gcdof_greatestLower[symmetric] agcd)
  2235     by (subst greatest_Lower_cong_l[of _ a]) (simp_all add: assms gcdof_greatestLower[symmetric])
  2258   then have "a' \<in> carrier G \<and> a' gcdof b c"
  2236   then have "a' \<in> carrier G \<and> a' gcdof b c"
  2259     by (simp add: gcdof_greatestLower carr')
  2237     by (simp add: gcdof_greatestLower carr')
  2260   then show ?thesis ..
  2238   then show ?thesis ..
  2261 qed
  2239 qed
  2262 
  2240 
  2263 lemma (in gcd_condition_monoid) gcd_closed [simp]:
  2241 lemma (in gcd_condition_monoid) gcd_closed [simp]:
  2264   assumes carr: "a \<in> carrier G"  "b \<in> carrier G"
  2242   assumes "a \<in> carrier G" "b \<in> carrier G"
  2265   shows "somegcd G a b \<in> carrier G"
  2243   shows "somegcd G a b \<in> carrier G"
  2266 proof -
  2244 proof -
  2267   interpret weak_lower_semilattice "division_rel G" by simp
  2245   interpret weak_lower_semilattice "division_rel G" by simp
  2268   show ?thesis
  2246   show ?thesis
  2269     apply (simp add: somegcd_meet[OF carr])
  2247   using  assms meet_closed by (simp add: somegcd_meet)
  2270     apply (rule meet_closed[simplified], fact+)
       
  2271     done
       
  2272 qed
  2248 qed
  2273 
  2249 
  2274 lemma (in gcd_condition_monoid) gcd_isgcd:
  2250 lemma (in gcd_condition_monoid) gcd_isgcd:
  2275   assumes carr: "a \<in> carrier G"  "b \<in> carrier G"
  2251   assumes "a \<in> carrier G"  "b \<in> carrier G"
  2276   shows "(somegcd G a b) gcdof a b"
  2252   shows "(somegcd G a b) gcdof a b"
  2277 proof -
  2253 proof -
  2278   interpret weak_lower_semilattice "division_rel G"
  2254   interpret weak_lower_semilattice "division_rel G"
  2279     by simp
  2255     by simp
  2280   from carr have "somegcd G a b \<in> carrier G \<and> (somegcd G a b) gcdof a b"
  2256   from assms have "somegcd G a b \<in> carrier G \<and> (somegcd G a b) gcdof a b"
  2281     by (simp add: gcdof_greatestLower inf_of_two_greatest meet_def somegcd_meet)
  2257     by (simp add: gcdof_greatestLower inf_of_two_greatest meet_def somegcd_meet)
  2282   then show "(somegcd G a b) gcdof a b"
  2258   then show "(somegcd G a b) gcdof a b"
  2283     by simp
  2259     by simp
  2284 qed
  2260 qed
  2285 
  2261 
  2286 lemma (in gcd_condition_monoid) gcd_exists:
  2262 lemma (in gcd_condition_monoid) gcd_exists:
  2287   assumes carr: "a \<in> carrier G"  "b \<in> carrier G"
  2263   assumes "a \<in> carrier G"  "b \<in> carrier G"
  2288   shows "\<exists>x\<in>carrier G. x = somegcd G a b"
  2264   shows "\<exists>x\<in>carrier G. x = somegcd G a b"
  2289 proof -
  2265 proof -
  2290   interpret weak_lower_semilattice "division_rel G"
  2266   interpret weak_lower_semilattice "division_rel G"
  2291     by simp
  2267     by simp
  2292   show ?thesis
  2268   show ?thesis
  2293     by (metis carr(1) carr(2) gcd_closed)
  2269     by (metis assms gcd_closed)
  2294 qed
  2270 qed
  2295 
  2271 
  2296 lemma (in gcd_condition_monoid) gcd_divides_l:
  2272 lemma (in gcd_condition_monoid) gcd_divides_l:
  2297   assumes carr: "a \<in> carrier G"  "b \<in> carrier G"
  2273   assumes "a \<in> carrier G" "b \<in> carrier G"
  2298   shows "(somegcd G a b) divides a"
  2274   shows "(somegcd G a b) divides a"
  2299 proof -
  2275 proof -
  2300   interpret weak_lower_semilattice "division_rel G"
  2276   interpret weak_lower_semilattice "division_rel G"
  2301     by simp
  2277     by simp
  2302   show ?thesis
  2278   show ?thesis
  2303     by (metis carr(1) carr(2) gcd_isgcd isgcd_def)
  2279     by (metis assms gcd_isgcd isgcd_def)
  2304 qed
  2280 qed
  2305 
  2281 
  2306 lemma (in gcd_condition_monoid) gcd_divides_r:
  2282 lemma (in gcd_condition_monoid) gcd_divides_r:
  2307   assumes carr: "a \<in> carrier G"  "b \<in> carrier G"
  2283   assumes "a \<in> carrier G"  "b \<in> carrier G"
  2308   shows "(somegcd G a b) divides b"
  2284   shows "(somegcd G a b) divides b"
  2309 proof -
  2285 proof -
  2310   interpret weak_lower_semilattice "division_rel G"
  2286   interpret weak_lower_semilattice "division_rel G"
  2311     by simp
  2287     by simp
  2312   show ?thesis
  2288   show ?thesis
  2313     by (metis carr gcd_isgcd isgcd_def)
  2289     by (metis assms gcd_isgcd isgcd_def)
  2314 qed
  2290 qed
  2315 
  2291 
  2316 lemma (in gcd_condition_monoid) gcd_divides:
  2292 lemma (in gcd_condition_monoid) gcd_divides:
  2317   assumes sub: "z divides x"  "z divides y"
  2293   assumes "z divides x" "z divides y"
  2318     and L: "x \<in> carrier G"  "y \<in> carrier G"  "z \<in> carrier G"
  2294     and L: "x \<in> carrier G"  "y \<in> carrier G"  "z \<in> carrier G"
  2319   shows "z divides (somegcd G x y)"
  2295   shows "z divides (somegcd G x y)"
  2320 proof -
  2296 proof -
  2321   interpret weak_lower_semilattice "division_rel G"
  2297   interpret weak_lower_semilattice "division_rel G"
  2322     by simp
  2298     by simp
  2323   show ?thesis
  2299   show ?thesis
  2324     by (metis gcd_isgcd isgcd_def assms)
  2300     by (metis gcd_isgcd isgcd_def assms)
  2325 qed
  2301 qed
  2326 
  2302 
  2327 lemma (in gcd_condition_monoid) gcd_cong_l:
  2303 lemma (in gcd_condition_monoid) gcd_cong_l:
  2328   assumes xx': "x \<sim> x'"
  2304   assumes "x \<sim> x'" "x \<in> carrier G"  "x' \<in> carrier G"  "y \<in> carrier G"
  2329     and carr: "x \<in> carrier G"  "x' \<in> carrier G"  "y \<in> carrier G"
       
  2330   shows "somegcd G x y \<sim> somegcd G x' y"
  2305   shows "somegcd G x y \<sim> somegcd G x' y"
  2331 proof -
  2306 proof -
  2332   interpret weak_lower_semilattice "division_rel G"
  2307   interpret weak_lower_semilattice "division_rel G"
  2333     by simp
  2308     by simp
  2334   show ?thesis
  2309   show ?thesis
  2335     apply (simp add: somegcd_meet carr)
  2310     using somegcd_meet assms
  2336     apply (rule meet_cong_l[simplified], fact+)
  2311     by (metis eq_object.select_convs(1) meet_cong_l partial_object.select_convs(1))
  2337     done
       
  2338 qed
  2312 qed
  2339 
  2313 
  2340 lemma (in gcd_condition_monoid) gcd_cong_r:
  2314 lemma (in gcd_condition_monoid) gcd_cong_r:
  2341   assumes carr: "x \<in> carrier G"  "y \<in> carrier G"  "y' \<in> carrier G"
  2315   assumes "y \<sim> y'" "x \<in> carrier G"  "y \<in> carrier G" "y' \<in> carrier G"
  2342     and yy': "y \<sim> y'"
       
  2343   shows "somegcd G x y \<sim> somegcd G x y'"
  2316   shows "somegcd G x y \<sim> somegcd G x y'"
  2344 proof -
  2317 proof -
  2345   interpret weak_lower_semilattice "division_rel G" by simp
  2318   interpret weak_lower_semilattice "division_rel G" by simp
  2346   show ?thesis
  2319   show ?thesis
  2347     apply (simp add: somegcd_meet carr)
  2320     by (meson associated_def assms gcd_closed gcd_divides gcd_divides_l gcd_divides_r monoid.divides_trans monoid_axioms)
  2348     apply (rule meet_cong_r[simplified], fact+)
  2321 qed
  2349     done
       
  2350 qed
       
  2351 
       
  2352 (*
       
  2353 lemma (in gcd_condition_monoid) asc_cong_gcd_l [intro]:
       
  2354   assumes carr: "b \<in> carrier G"
       
  2355   shows "asc_cong (\<lambda>a. somegcd G a b)"
       
  2356 using carr
       
  2357 unfolding CONG_def
       
  2358 by clarsimp (blast intro: gcd_cong_l)
       
  2359 
       
  2360 lemma (in gcd_condition_monoid) asc_cong_gcd_r [intro]:
       
  2361   assumes carr: "a \<in> carrier G"
       
  2362   shows "asc_cong (\<lambda>b. somegcd G a b)"
       
  2363 using carr
       
  2364 unfolding CONG_def
       
  2365 by clarsimp (blast intro: gcd_cong_r)
       
  2366 
       
  2367 lemmas (in gcd_condition_monoid) asc_cong_gcd_split [simp] =
       
  2368     assoc_split[OF _ asc_cong_gcd_l] assoc_split[OF _ asc_cong_gcd_r]
       
  2369 *)
       
  2370 
  2322 
  2371 lemma (in gcd_condition_monoid) gcdI:
  2323 lemma (in gcd_condition_monoid) gcdI:
  2372   assumes dvd: "a divides b"  "a divides c"
  2324   assumes dvd: "a divides b"  "a divides c"
  2373     and others: "\<And>y. \<lbrakk>y\<in>carrier G; y divides b; y divides c\<rbrakk> \<Longrightarrow> y divides a"
  2325     and others: "\<And>y. \<lbrakk>y\<in>carrier G; y divides b; y divides c\<rbrakk> \<Longrightarrow> y divides a"
  2374     and acarr: "a \<in> carrier G" and bcarr: "b \<in> carrier G" and ccarr: "c \<in> carrier G"
  2326     and acarr: "a \<in> carrier G" and bcarr: "b \<in> carrier G" and ccarr: "c \<in> carrier G"
  2388   using assms unfolding isgcd_def
  2340   using assms unfolding isgcd_def
  2389   by (simp add: gcdI)
  2341   by (simp add: gcdI)
  2390 
  2342 
  2391 lemma (in gcd_condition_monoid) SomeGcd_ex:
  2343 lemma (in gcd_condition_monoid) SomeGcd_ex:
  2392   assumes "finite A"  "A \<subseteq> carrier G"  "A \<noteq> {}"
  2344   assumes "finite A"  "A \<subseteq> carrier G"  "A \<noteq> {}"
  2393   shows "\<exists>x\<in> carrier G. x = SomeGcd G A"
  2345   shows "\<exists>x \<in> carrier G. x = SomeGcd G A"
  2394 proof -
  2346 proof -
  2395   interpret weak_lower_semilattice "division_rel G"
  2347   interpret weak_lower_semilattice "division_rel G"
  2396     by simp
  2348     by simp
  2397   show ?thesis
  2349   show ?thesis
  2398     apply (simp add: SomeGcd_def)
  2350     using finite_inf_closed by (simp add: assms SomeGcd_def)
  2399     apply (rule finite_inf_closed[simplified], fact+)
       
  2400     done
       
  2401 qed
  2351 qed
  2402 
  2352 
  2403 lemma (in gcd_condition_monoid) gcd_assoc:
  2353 lemma (in gcd_condition_monoid) gcd_assoc:
  2404   assumes carr: "a \<in> carrier G"  "b \<in> carrier G"  "c \<in> carrier G"
  2354   assumes "a \<in> carrier G"  "b \<in> carrier G"  "c \<in> carrier G"
  2405   shows "somegcd G (somegcd G a b) c \<sim> somegcd G a (somegcd G b c)"
  2355   shows "somegcd G (somegcd G a b) c \<sim> somegcd G a (somegcd G b c)"
  2406 proof -
  2356 proof -
  2407   interpret weak_lower_semilattice "division_rel G"
  2357   interpret weak_lower_semilattice "division_rel G"
  2408     by simp
  2358     by simp
  2409   show ?thesis
  2359   show ?thesis
  2410     unfolding associated_def
  2360     unfolding associated_def
  2411     by (meson carr divides_trans gcd_divides gcd_divides_l gcd_divides_r gcd_exists)
  2361     by (meson assms divides_trans gcd_divides gcd_divides_l gcd_divides_r gcd_exists)
  2412 qed
  2362 qed
  2413 
  2363 
  2414 lemma (in gcd_condition_monoid) gcd_mult:
  2364 lemma (in gcd_condition_monoid) gcd_mult:
  2415   assumes acarr: "a \<in> carrier G" and bcarr: "b \<in> carrier G" and ccarr: "c \<in> carrier G"
  2365   assumes acarr: "a \<in> carrier G" and bcarr: "b \<in> carrier G" and ccarr: "c \<in> carrier G"
  2416   shows "c \<otimes> somegcd G a b \<sim> somegcd G (c \<otimes> a) (c \<otimes> b)"
  2366   shows "c \<otimes> somegcd G a b \<sim> somegcd G (c \<otimes> a) (c \<otimes> b)"
  2639     by (intro prime_divides) simp+
  2589     by (intro prime_divides) simp+
  2640   then show ?case
  2590   then show ?case
  2641     using Cons.IH Cons.prems(1) by force
  2591     using Cons.IH Cons.prems(1) by force
  2642 qed
  2592 qed
  2643 
  2593 
  2644 
  2594 proposition (in primeness_condition_monoid) wfactors_unique:
  2645 lemma (in primeness_condition_monoid) wfactors_unique__hlp_induct:
  2595   assumes "wfactors G as a"  "wfactors G as' a"
  2646   "\<forall>a as'. a \<in> carrier G \<and> set as \<subseteq> carrier G \<and> set as' \<subseteq> carrier G \<and>
  2596     and "a \<in> carrier G"  "set as \<subseteq> carrier G"  "set as' \<subseteq> carrier G"
  2647            wfactors G as a \<and> wfactors G as' a \<longrightarrow> essentially_equal G as as'"
  2597   shows "essentially_equal G as as'"
  2648 proof (induct as)
  2598   using assms
       
  2599 proof (induct as arbitrary: a as')
  2649   case Nil
  2600   case Nil
  2650   show ?case
  2601   then have "a \<sim> \<one>"
  2651     apply (clarsimp simp: wfactors_def)
  2602     by (meson Units_one_closed one_closed perm.Nil perm_wfactorsD unit_wfactors)
  2652     by (metis Units_one_closed assoc_unit_r list_update_nonempty unit_wfactors_empty unitfactor_ee wfactorsI)
  2603   then have "as' = []"
       
  2604     using Nil.prems assoc_unit_l unit_wfactors_empty by blast
       
  2605   then show ?case
       
  2606     by auto
  2653 next
  2607 next
  2654   case (Cons ah as)
  2608   case (Cons ah as)
  2655   then show ?case
  2609   then have ahdvda: "ah divides a"
  2656   proof clarsimp
  2610     using wfactors_dividesI by auto
  2657     fix a as'
       
  2658     assume ih [rule_format]:
       
  2659       "\<forall>a as'. a \<in> carrier G \<and> set as' \<subseteq> carrier G \<and> wfactors G as a \<and>
       
  2660         wfactors G as' a \<longrightarrow> essentially_equal G as as'"
       
  2661       and acarr: "a \<in> carrier G" and ahcarr: "ah \<in> carrier G"
       
  2662       and ascarr: "set as \<subseteq> carrier G" and as'carr: "set as' \<subseteq> carrier G"
       
  2663       and afs: "wfactors G (ah # as) a"
       
  2664       and afs': "wfactors G as' a"
       
  2665     then have ahdvda: "ah divides a"
       
  2666       by (intro wfactors_dividesI[of "ah#as" "a"]) simp_all
       
  2667     then obtain a' where a'carr: "a' \<in> carrier G" and a: "a = ah \<otimes> a'"
  2611     then obtain a' where a'carr: "a' \<in> carrier G" and a: "a = ah \<otimes> a'"
  2668       by blast
  2612       by blast
       
  2613     have carr_ah: "ah \<in> carrier G" "set as \<subseteq> carrier G"
       
  2614       using Cons.prems by fastforce+
       
  2615     have "ah \<otimes> foldr (\<otimes>) as \<one> \<sim> a"
       
  2616       by (rule wfactorsE[OF \<open>wfactors G (ah # as) a\<close>]) auto
       
  2617     then have "foldr (\<otimes>) as \<one> \<sim> a'"
       
  2618       by (metis Cons.prems(4) a a'carr assoc_l_cancel insert_subset list.set(2) monoid.multlist_closed monoid_axioms)
       
  2619     then
  2669     have a'fs: "wfactors G as a'"
  2620     have a'fs: "wfactors G as a'"
  2670       apply (rule wfactorsE[OF afs], rule wfactorsI, simp)
  2621       by (meson Cons.prems(1) set_subset_Cons subset_iff wfactorsE wfactorsI)
  2671       by (metis a a'carr ahcarr ascarr assoc_l_cancel factorsI factors_def factors_mult_single list.set_intros(1) list.set_intros(2) multlist_closed)
  2622     then have ahirr: "irreducible G ah"
  2672     from afs have ahirr: "irreducible G ah"
  2623       by (meson Cons.prems(1) list.set_intros(1) wfactorsE)
  2673       by (elim wfactorsE) simp
  2624     with Cons have ahprime: "prime G ah"
  2674     with ascarr have ahprime: "prime G ah"
  2625       by (simp add: irreducible_prime)
  2675       by (intro irreducible_prime ahcarr)
       
  2676 
       
  2677     note carr [simp] = acarr ahcarr ascarr as'carr a'carr
       
  2678 
       
  2679     note ahdvda
  2626     note ahdvda
  2680     also from afs' have "a divides (foldr (\<otimes>) as' \<one>)"
  2627     also have "a divides (foldr (\<otimes>) as' \<one>)"
  2681       by (elim wfactorsE associatedE, simp)
  2628       by (meson Cons.prems(2) associatedE wfactorsE)
  2682     finally have "ah divides (foldr (\<otimes>) as' \<one>)"
  2629     finally have "ah divides (foldr (\<otimes>) as' \<one>)"
  2683       by simp
  2630       using Cons.prems(4) by auto
  2684     with ahprime have "\<exists>i<length as'. ah divides as'!i"
  2631     with ahprime have "\<exists>i<length as'. ah divides as'!i"
  2685       by (intro multlist_prime_pos) simp_all
  2632       by (intro multlist_prime_pos) (use Cons.prems in auto)
  2686     then obtain i where len: "i<length as'" and ahdvd: "ah divides as'!i"
  2633     then obtain i where len: "i<length as'" and ahdvd: "ah divides as'!i"
  2687       by blast
  2634       by blast
  2688     from afs' carr have irrasi: "irreducible G (as'!i)"
  2635     then obtain x where "x \<in> carrier G" and asi: "as'!i = ah \<otimes> x"
  2689       by (fast intro: nth_mem[OF len] elim: wfactorsE)
       
  2690     from len carr have asicarr[simp]: "as'!i \<in> carrier G"
       
  2691       unfolding set_conv_nth by force
       
  2692     note carr = carr asicarr
       
  2693 
       
  2694     from ahdvd obtain x where "x \<in> carrier G" and asi: "as'!i = ah \<otimes> x"
       
  2695       by blast
  2636       by blast
  2696     with carr irrasi[simplified asi] have asiah: "as'!i \<sim> ah"
  2637     have irrasi: "irreducible G (as'!i)"
  2697       by (metis ahprime associatedI2 irreducible_prodE primeE)
  2638       using nth_mem[OF len] wfactorsE
       
  2639       by (metis Cons.prems(2))
       
  2640     have asicarr[simp]: "as'!i \<in> carrier G"
       
  2641       using len \<open>set as' \<subseteq> carrier G\<close> nth_mem by blast
       
  2642     have asiah: "as'!i \<sim> ah"
       
  2643       by (metis \<open>ah \<in> carrier G\<close> \<open>x \<in> carrier G\<close> asi irrasi ahprime associatedI2 irreducible_prodE primeE)
  2698     note setparts = set_take_subset[of i as'] set_drop_subset[of "Suc i" as']
  2644     note setparts = set_take_subset[of i as'] set_drop_subset[of "Suc i" as']
  2699     note partscarr [simp] = setparts[THEN subset_trans[OF _ as'carr]]
       
  2700     note carr = carr partscarr
       
  2701 
       
  2702     have "\<exists>aa_1. aa_1 \<in> carrier G \<and> wfactors G (take i as') aa_1"
  2645     have "\<exists>aa_1. aa_1 \<in> carrier G \<and> wfactors G (take i as') aa_1"
  2703       by (meson afs' in_set_takeD partscarr(1) wfactorsE wfactors_prod_exists)
  2646       using Cons
  2704     then obtain aa_1 where aa1carr: "aa_1 \<in> carrier G" and aa1fs: "wfactors G (take i as') aa_1"
  2647       by (metis setparts(1) subset_trans in_set_takeD wfactorsE wfactors_prod_exists)
       
  2648     then obtain aa_1 where aa1carr [simp]: "aa_1 \<in> carrier G" and aa1fs: "wfactors G (take i as') aa_1"
  2705       by auto
  2649       by auto
  2706 
  2650     obtain aa_2 where aa2carr [simp]: "aa_2 \<in> carrier G"
  2707     have "\<exists>aa_2. aa_2 \<in> carrier G \<and> wfactors G (drop (Suc i) as') aa_2"
       
  2708       by (meson afs' in_set_dropD partscarr(2) wfactors_def wfactors_prod_exists)
       
  2709     then obtain aa_2 where aa2carr: "aa_2 \<in> carrier G"
       
  2710       and aa2fs: "wfactors G (drop (Suc i) as') aa_2"
  2651       and aa2fs: "wfactors G (drop (Suc i) as') aa_2"
  2711       by auto
  2652       by (metis Cons.prems(2) Cons.prems(5) subset_code(1) in_set_dropD wfactors_def wfactors_prod_exists)
  2712 
  2653 
  2713     note carr = carr aa1carr[simp] aa2carr[simp]
  2654     have set_drop: "set (drop (Suc i) as') \<subseteq> carrier G"
  2714 
  2655       using Cons.prems(5) setparts(2) by blast
  2715     from aa1fs aa2fs
  2656     moreover have set_take: "set (take i as') \<subseteq> carrier G"
  2716     have v1: "wfactors G (take i as' @ drop (Suc i) as') (aa_1 \<otimes> aa_2)"
  2657       using  Cons.prems(5) setparts by auto
  2717       by (intro wfactors_mult, simp+)
  2658     moreover have v1: "wfactors G (take i as' @ drop (Suc i) as') (aa_1 \<otimes> aa_2)"
  2718     then have v1': "wfactors G (as'!i # take i as' @ drop (Suc i) as') (as'!i \<otimes> (aa_1 \<otimes> aa_2))"
  2659       using aa1fs aa2fs \<open>set as' \<subseteq> carrier G\<close> by (force simp add: dest: in_set_takeD in_set_dropD)
  2719       using irrasi wfactors_mult_single by auto
  2660     ultimately have v1': "wfactors G (as'!i # take i as' @ drop (Suc i) as') (as'!i \<otimes> (aa_1 \<otimes> aa_2))"
  2720     from aa2carr carr aa1fs aa2fs have "wfactors G (as'!i # drop (Suc i) as') (as'!i \<otimes> aa_2)"
  2661       using irrasi wfactors_mult_single
  2721       by (metis irrasi wfactors_mult_single)
  2662         by (simp add: irrasi v1 wfactors_mult_single)      
  2722     with len carr aa1carr aa2carr aa1fs
  2663     have "wfactors G (as'!i # drop (Suc i) as') (as'!i \<otimes> aa_2)"
       
  2664       by (simp add: aa2fs irrasi set_drop wfactors_mult_single)
       
  2665     with len  aa1carr aa2carr aa1fs
  2723     have v2: "wfactors G (take i as' @ as'!i # drop (Suc i) as') (aa_1 \<otimes> (as'!i \<otimes> aa_2))"
  2666     have v2: "wfactors G (take i as' @ as'!i # drop (Suc i) as') (aa_1 \<otimes> (as'!i \<otimes> aa_2))"
  2724       using wfactors_mult by auto
  2667       using wfactors_mult  by (simp add: set_take set_drop) 
  2725     from len have as': "as' = (take i as' @ as'!i # drop (Suc i) as')"
  2668     from len have as': "as' = (take i as' @ as'!i # drop (Suc i) as')"
  2726       by (simp add: Cons_nth_drop_Suc)
  2669       by (simp add: Cons_nth_drop_Suc)
  2727     with carr have eer: "essentially_equal G (take i as' @ as'!i # drop (Suc i) as') as'"
  2670     have eer: "essentially_equal G (take i as' @ as'!i # drop (Suc i) as') as'"
  2728       by simp
  2671       using Cons.prems(5) as' by auto
  2729     with v2 afs' carr aa1carr aa2carr nth_mem[OF len] have "aa_1 \<otimes> (as'!i \<otimes> aa_2) \<sim> a"
  2672     with v2 aa1carr aa2carr nth_mem[OF len] have "aa_1 \<otimes> (as'!i \<otimes> aa_2) \<sim> a"
  2730       by (metis as' ee_wfactorsD m_closed)
  2673       using Cons.prems as' comm_monoid_cancel.ee_wfactorsD is_comm_monoid_cancel by fastforce
  2731     then have t1: "as'!i \<otimes> (aa_1 \<otimes> aa_2) \<sim> a"
  2674     then have t1: "as'!i \<otimes> (aa_1 \<otimes> aa_2) \<sim> a"
  2732       by (metis aa1carr aa2carr asicarr m_lcomm)
  2675       by (metis aa1carr aa2carr asicarr m_lcomm)
  2733     from carr asiah have "ah \<otimes> (aa_1 \<otimes> aa_2) \<sim> as'!i \<otimes> (aa_1 \<otimes> aa_2)"
  2676     from asiah have "ah \<otimes> (aa_1 \<otimes> aa_2) \<sim> as'!i \<otimes> (aa_1 \<otimes> aa_2)"
  2734       by (metis associated_sym m_closed mult_cong_l)
  2677       by (simp add: \<open>ah \<in> carrier G\<close> associated_sym mult_cong_l)
  2735     also note t1
  2678     also note t1
  2736     finally have "ah \<otimes> (aa_1 \<otimes> aa_2) \<sim> a" by simp
  2679     finally have "ah \<otimes> (aa_1 \<otimes> aa_2) \<sim> a"
  2737 
  2680       using Cons.prems(3) carr_ah aa1carr aa2carr by blast
  2738     with carr aa1carr aa2carr a'carr nth_mem[OF len] have a': "aa_1 \<otimes> aa_2 \<sim> a'"
  2681     with aa1carr aa2carr a'carr nth_mem[OF len] have a': "aa_1 \<otimes> aa_2 \<sim> a'"
  2739       by (simp add: a, fast intro: assoc_l_cancel[of ah _ a'])
  2682       using a assoc_l_cancel carr_ah(1) by blast
  2740 
       
  2741     note v1
  2683     note v1
  2742     also note a'
  2684     also note a'
  2743     finally have "wfactors G (take i as' @ drop (Suc i) as') a'"
  2685     finally have "wfactors G (take i as' @ drop (Suc i) as') a'"
  2744       by simp
  2686       by (simp add: a'carr set_drop set_take)
  2745 
  2687     from a'fs this have "essentially_equal G as (take i as' @ drop (Suc i) as')"
  2746     from a'fs this carr have "essentially_equal G as (take i as' @ drop (Suc i) as')"
  2688       using Cons.hyps a'carr carr_ah(2) set_drop set_take by auto
  2747       by (intro ih[of a']) simp
  2689     with carr_ah have ee1: "essentially_equal G (ah # as) (ah # take i as' @ drop (Suc i) as')"
  2748     then have ee1: "essentially_equal G (ah # as) (ah # take i as' @ drop (Suc i) as')"
  2690       by (auto simp: essentially_equal_def)
  2749       by (elim essentially_equalE) (fastforce intro: essentially_equalI)
  2691     have ee2: "essentially_equal G (ah # take i as' @ drop (Suc i) as')
  2750 
       
  2751     from carr have ee2: "essentially_equal G (ah # take i as' @ drop (Suc i) as')
       
  2752       (as' ! i # take i as' @ drop (Suc i) as')"
  2692       (as' ! i # take i as' @ drop (Suc i) as')"
  2753     proof (intro essentially_equalI)
  2693     proof (intro essentially_equalI)
  2754       show "ah # take i as' @ drop (Suc i) as' <~~> ah # take i as' @ drop (Suc i) as'"
  2694       show "ah # take i as' @ drop (Suc i) as' <~~> ah # take i as' @ drop (Suc i) as'"
  2755         by simp
  2695         by simp
  2756     next
  2696     next
  2757       show "ah # take i as' @ drop (Suc i) as' [\<sim>] as' ! i # take i as' @ drop (Suc i) as'"
  2697       show "ah # take i as' @ drop (Suc i) as' [\<sim>] as' ! i # take i as' @ drop (Suc i) as'"
  2758         by (simp add: list_all2_append) (simp add: asiah[symmetric])
  2698         by (simp add: asiah associated_sym set_drop set_take)
  2759     qed
  2699     qed
  2760 
  2700 
  2761     note ee1
  2701     note ee1
  2762     also note ee2
  2702     also note ee2
  2763     also have "essentially_equal G (as' ! i # take i as' @ drop (Suc i) as')
  2703     also have "essentially_equal G (as' ! i # take i as' @ drop (Suc i) as')
  2764                                    (take i as' @ as' ! i # drop (Suc i) as')"
  2704                                    (take i as' @ as' ! i # drop (Suc i) as')"
  2765       by (metis as' as'carr listassoc_refl essentially_equalI perm_append_Cons)
  2705       by (metis Cons.prems(5) as' essentially_equalI listassoc_refl perm_append_Cons)
  2766     finally have "essentially_equal G (ah # as) (take i as' @ as' ! i # drop (Suc i) as')"
  2706     finally have "essentially_equal G (ah # as) (take i as' @ as' ! i # drop (Suc i) as')"
  2767       by simp
  2707       using Cons.prems(4) set_drop set_take by auto
  2768     then show "essentially_equal G (ah # as) as'"
  2708     then show ?case
  2769       by (subst as')
  2709       using as' by auto
  2770   qed
  2710 qed
  2771 qed
       
  2772 
       
  2773 lemma (in primeness_condition_monoid) wfactors_unique:
       
  2774   assumes "wfactors G as a"  "wfactors G as' a"
       
  2775     and "a \<in> carrier G"  "set as \<subseteq> carrier G"  "set as' \<subseteq> carrier G"
       
  2776   shows "essentially_equal G as as'"
       
  2777   by (rule wfactors_unique__hlp_induct[rule_format, of a]) (simp add: assms)
       
  2778 
  2711 
  2779 
  2712 
  2780 subsubsection \<open>Application to factorial monoids\<close>
  2713 subsubsection \<open>Application to factorial monoids\<close>
  2781 
  2714 
  2782 text \<open>Number of factors for wellfoundedness\<close>
  2715 text \<open>Number of factors for wellfoundedness\<close>
  2839     by blast
  2772     by blast
  2840   then obtain cs where cscarr: "set cs \<subseteq> carrier G" and cfs: "wfactors G cs c"
  2773   then obtain cs where cscarr: "set cs \<subseteq> carrier G" and cfs: "wfactors G cs c"
  2841     by blast
  2774     by blast
  2842 
  2775 
  2843   note [simp] = acarr bcarr ccarr ascarr cscarr
  2776   note [simp] = acarr bcarr ccarr ascarr cscarr
  2844 
       
  2845   assume b: "b = a \<otimes> c"
  2777   assume b: "b = a \<otimes> c"
  2846   from afs cfs have "wfactors G (as@cs) (a \<otimes> c)"
  2778   from afs cfs have "wfactors G (as@cs) (a \<otimes> c)"
  2847     by (intro wfactors_mult) simp_all
  2779     by (intro wfactors_mult) simp_all
  2848   with b have "wfactors G (as@cs) b"
  2780   with b have "wfactors G (as@cs) b"
  2849     by simp
  2781     by simp
  2916 
  2848 
  2917 sublocale factorial_monoid \<subseteq> divisor_chain_condition_monoid
  2849 sublocale factorial_monoid \<subseteq> divisor_chain_condition_monoid
  2918   apply unfold_locales
  2850   apply unfold_locales
  2919   apply (rule wfUNIVI)
  2851   apply (rule wfUNIVI)
  2920   apply (rule measure_induct[of "factorcount G"])
  2852   apply (rule measure_induct[of "factorcount G"])
  2921   apply simp
  2853   using properfactor_fcount by auto
  2922   apply (metis properfactor_fcount)
       
  2923   done
       
  2924 
  2854 
  2925 sublocale factorial_monoid \<subseteq> primeness_condition_monoid
  2855 sublocale factorial_monoid \<subseteq> primeness_condition_monoid
  2926   by standard (rule irreducible_prime)
  2856   by standard (rule irreducible_prime)
  2927 
  2857 
  2928 
  2858