
wenzelm@38159: (*  Title:      HOL/Old_Number_Theory/Residues.thy
paulson@13871:     Authors:    Jeremy Avigad, David Gray, and Adam Kramer
paulson@13871: *)
paulson@13871: 
wenzelm@58889: section {* Residue Sets *}
paulson@13871: 
wenzelm@38159: theory Residues
wenzelm@38159: imports Int2
wenzelm@38159: begin
paulson@13871: 
wenzelm@19670: text {*
wenzelm@19670:   \medskip Define the residue of a set, the standard residue,
wenzelm@19670:   quadratic residues, and prove some basic properties. *}
paulson@13871: 
wenzelm@38159: definition ResSet :: "int => int set => bool"
wenzelm@38159:   where "ResSet m X = (\<forall>y1 y2. (y1 \<in> X & y2 \<in> X & [y1 = y2] (mod m) --> y1 = y2))"
paulson@13871: 
wenzelm@38159: definition StandardRes :: "int => int => int"
wenzelm@38159:   where "StandardRes m x = x mod m"
paulson@13871: 
wenzelm@38159: definition QuadRes :: "int => int => bool"
wenzelm@53077:   where "QuadRes m x = (\<exists>y. ([y\<^sup>2 = x] (mod m)))"
paulson@13871: 
wenzelm@38159: definition Legendre :: "int => int => int" where
wenzelm@19670:   "Legendre a p = (if ([a = 0] (mod p)) then 0
paulson@13871:                      else if (QuadRes p a) then 1
paulson@13871:                      else -1)"
paulson@13871: 
wenzelm@38159: definition SR :: "int => int set"
wenzelm@38159:   where "SR p = {x. (0 \<le> x) & (x < p)}"
paulson@13871: 
wenzelm@38159: definition SRStar :: "int => int set"
wenzelm@38159:   where "SRStar p = {x. (0 < x) & (x < p)}"
paulson@13871: 
paulson@13871: 
wenzelm@19670: subsection {* Some useful properties of StandardRes *}
paulson@13871: 
wenzelm@18369: lemma StandardRes_prop1: "[x = StandardRes m x] (mod m)"
paulson@13871:   by (auto simp add: StandardRes_def zcong_zmod)
paulson@13871: 
paulson@13871: lemma StandardRes_prop2: "0 < m ==> (StandardRes m x1 = StandardRes m x2)
wenzelm@18369:       = ([x1 = x2] (mod m))"
paulson@13871:   by (auto simp add: StandardRes_def zcong_zmod_eq)
paulson@13871: 
wenzelm@18369: lemma StandardRes_prop3: "(~[x = 0] (mod p)) = (~(StandardRes p x = 0))"
nipkow@30042:   by (auto simp add: StandardRes_def zcong_def dvd_eq_mod_eq_0)
paulson@13871: 
paulson@13871: lemma StandardRes_prop4: "2 < m 
wenzelm@18369:      ==> [StandardRes m x * StandardRes m y = (x * y)] (mod m)"
paulson@13871:   by (auto simp add: StandardRes_def zcong_zmod_eq 
nipkow@29948:                      mod_mult_eq [of x y m])
paulson@13871: 
wenzelm@18369: lemma StandardRes_lbound: "0 < p ==> 0 \<le> StandardRes p x"
wenzelm@41541:   by (auto simp add: StandardRes_def)
paulson@13871: 
wenzelm@18369: lemma StandardRes_ubound: "0 < p ==> StandardRes p x < p"
wenzelm@41541:   by (auto simp add: StandardRes_def)
paulson@13871: 
paulson@13871: lemma StandardRes_eq_zcong: 
wenzelm@18369:    "(StandardRes m x = 0) = ([x = 0](mod m))"
paulson@13871:   by (auto simp add: StandardRes_def zcong_eq_zdvd_prop dvd_def) 
paulson@13871: 
paulson@13871: 
paulson@13871: subsection {* Relations between StandardRes, SRStar, and SR *}
paulson@13871: 
wenzelm@18369: lemma SRStar_SR_prop: "x \<in> SRStar p ==> x \<in> SR p"
paulson@13871:   by (auto simp add: SRStar_def SR_def)
paulson@13871: 
wenzelm@18369: lemma StandardRes_SR_prop: "x \<in> SR p ==> StandardRes p x = x"
paulson@13871:   by (auto simp add: SR_def StandardRes_def mod_pos_pos_trivial)
paulson@13871: 
paulson@13871: lemma StandardRes_SRStar_prop1: "2 < p ==> (StandardRes p x \<in> SRStar p) 
wenzelm@18369:      = (~[x = 0] (mod p))"
wenzelm@41541:   apply (auto simp add: StandardRes_prop3 StandardRes_def SRStar_def)
paulson@13871:   apply (subgoal_tac "0 < p")
wenzelm@18369:   apply (drule_tac a = x in pos_mod_sign, arith, simp)
wenzelm@18369:   done
paulson@13871: 
wenzelm@18369: lemma StandardRes_SRStar_prop1a: "x \<in> SRStar p ==> ~([x = 0] (mod p))"
paulson@13871:   by (auto simp add: SRStar_def zcong_def zdvd_not_zless)
paulson@13871: 
nipkow@16663: lemma StandardRes_SRStar_prop2: "[| 2 < p; zprime p; x \<in> SRStar p |] 
wenzelm@18369:      ==> StandardRes p (MultInv p x) \<in> SRStar p"
wenzelm@18369:   apply (frule_tac x = "(MultInv p x)" in StandardRes_SRStar_prop1, simp)
paulson@13871:   apply (rule MultInv_prop3)
paulson@13871:   apply (auto simp add: SRStar_def zcong_def zdvd_not_zless)
wenzelm@18369:   done
paulson@13871: 
wenzelm@18369: lemma StandardRes_SRStar_prop3: "x \<in> SRStar p ==> StandardRes p x = x"
paulson@13871:   by (auto simp add: SRStar_SR_prop StandardRes_SR_prop)
paulson@13871: 
nipkow@16663: lemma StandardRes_SRStar_prop4: "[| zprime p; 2 < p; x \<in> SRStar p |] 
wenzelm@18369:      ==> StandardRes p x \<in> SRStar p"
paulson@13871:   by (frule StandardRes_SRStar_prop3, auto)
paulson@13871: 
nipkow@16663: lemma SRStar_mult_prop1: "[| zprime p; 2 < p; x \<in> SRStar p; y \<in> SRStar p|] 
wenzelm@18369:      ==> (StandardRes p (x * y)):SRStar p"
paulson@13871:   apply (frule_tac x = x in StandardRes_SRStar_prop4, auto)
paulson@13871:   apply (frule_tac x = y in StandardRes_SRStar_prop4, auto)
paulson@13871:   apply (auto simp add: StandardRes_SRStar_prop1 zcong_zmult_prop3)
wenzelm@18369:   done
paulson@13871: 
nipkow@16663: lemma SRStar_mult_prop2: "[| zprime p; 2 < p; ~([a = 0](mod p)); 
paulson@13871:      x \<in> SRStar p |] 
wenzelm@18369:      ==> StandardRes p (a * MultInv p x) \<in> SRStar p"
paulson@13871:   apply (frule_tac x = x in StandardRes_SRStar_prop2, auto)
paulson@13871:   apply (frule_tac x = "MultInv p x" in StandardRes_SRStar_prop1)
paulson@13871:   apply (auto simp add: StandardRes_SRStar_prop1 zcong_zmult_prop3)
wenzelm@18369:   done
paulson@13871: 
wenzelm@18369: lemma SRStar_card: "2 < p ==> int(card(SRStar p)) = p - 1"
paulson@13871:   by (auto simp add: SRStar_def int_card_bdd_int_set_l_l)
paulson@13871: 
wenzelm@18369: lemma SRStar_finite: "2 < p ==> finite( SRStar p)"
paulson@13871:   by (auto simp add: SRStar_def bdd_int_set_l_l_finite)
paulson@13871: 
paulson@13871: 
paulson@13871: subsection {* Properties relating ResSets with StandardRes *}
paulson@13871: 
wenzelm@18369: lemma aux: "x mod m = y mod m ==> [x = y] (mod m)"
wenzelm@18369:   apply (subgoal_tac "x = y ==> [x = y](mod m)")
wenzelm@18369:   apply (subgoal_tac "[x mod m = y mod m] (mod m) ==> [x = y] (mod m)")
paulson@13871:   apply (auto simp add: zcong_zmod [of x y m])
wenzelm@18369:   done
paulson@13871: 
wenzelm@18369: lemma StandardRes_inj_on_ResSet: "ResSet m X ==> (inj_on (StandardRes m) X)"
paulson@13871:   apply (auto simp add: ResSet_def StandardRes_def inj_on_def)
paulson@13871:   apply (drule_tac m = m in aux, auto)
wenzelm@18369:   done
paulson@13871: 
paulson@13871: lemma StandardRes_Sum: "[| finite X; 0 < m |] 
wenzelm@18369:      ==> [setsum f X = setsum (StandardRes m o f) X](mod m)" 
paulson@13871:   apply (rule_tac F = X in finite_induct)
paulson@13871:   apply (auto intro!: zcong_zadd simp add: StandardRes_prop1)
wenzelm@18369:   done
paulson@13871: 
wenzelm@18369: lemma SR_pos: "0 < m ==> (StandardRes m ` X) \<subseteq> {x. 0 \<le> x & x < m}"
paulson@13871:   by (auto simp add: StandardRes_ubound StandardRes_lbound)
paulson@13871: 
wenzelm@18369: lemma ResSet_finite: "0 < m ==> ResSet m X ==> finite X"
paulson@13871:   apply (rule_tac f = "StandardRes m" in finite_imageD) 
wenzelm@18369:   apply (rule_tac B = "{x. (0 :: int) \<le> x & x < m}" in finite_subset)
wenzelm@18369:   apply (auto simp add: StandardRes_inj_on_ResSet bdd_int_set_l_finite SR_pos)
wenzelm@18369:   done
paulson@13871: 
wenzelm@18369: lemma mod_mod_is_mod: "[x = x mod m](mod m)"
paulson@13871:   by (auto simp add: zcong_zmod)
paulson@13871: 
paulson@13871: lemma StandardRes_prod: "[| finite X; 0 < m |] 
wenzelm@18369:      ==> [setprod f X = setprod (StandardRes m o f) X] (mod m)"
paulson@13871:   apply (rule_tac F = X in finite_induct)
wenzelm@18369:   apply (auto intro!: zcong_zmult simp add: StandardRes_prop1)
wenzelm@18369:   done
paulson@13871: 
wenzelm@19670: lemma ResSet_image:
wenzelm@19670:   "[| 0 < m; ResSet m A; \<forall>x \<in> A. \<forall>y \<in> A. ([f x = f y](mod m) --> x = y) |] ==>
wenzelm@19670:     ResSet m (f ` A)"
paulson@13871:   by (auto simp add: ResSet_def)
paulson@13871: 
wenzelm@19670: 
wenzelm@19670: subsection {* Property for SRStar *}
paulson@13871: 
wenzelm@18369: lemma ResSet_SRStar_prop: "ResSet p (SRStar p)"
paulson@13871:   by (auto simp add: SRStar_def ResSet_def zcong_zless_imp_eq)
paulson@13871: 
wenzelm@18369: end




