
wenzelm@38159: (*  Title:      HOL/Old_Number_Theory/WilsonBij.thy
wenzelm@38159:     Author:     Thomas M. Rasmussen
wenzelm@11049:     Copyright   2000  University of Cambridge
paulson@9508: *)
paulson@9508: 
wenzelm@11049: header {* Wilson's Theorem using a more abstract approach *}
wenzelm@11049: 
wenzelm@38159: theory WilsonBij
wenzelm@38159: imports BijectionRel IntFact
wenzelm@38159: begin
wenzelm@11049: 
wenzelm@11049: text {*
wenzelm@11049:   Wilson's Theorem using a more ``abstract'' approach based on
wenzelm@11049:   bijections between sets.  Does not use Fermat's Little Theorem
wenzelm@11049:   (unlike Russinoff).
wenzelm@11049: *}
wenzelm@11049: 
wenzelm@11049: 
wenzelm@11049: subsection {* Definitions and lemmas *}
wenzelm@11049: 
wenzelm@38159: definition reciR :: "int => int => int => bool"
wenzelm@38159:   where "reciR p = (\<lambda>a b. zcong (a * b) 1 p \<and> 1 < a \<and> a < p - 1 \<and> 1 < b \<and> b < p - 1)"
wenzelm@21404: 
wenzelm@38159: definition inv :: "int => int => int" where
wenzelm@19670:   "inv p a =
wenzelm@19670:     (if zprime p \<and> 0 < a \<and> a < p then
paulson@11868:       (SOME x. 0 \<le> x \<and> x < p \<and> zcong (a * x) 1 p)
wenzelm@19670:      else 0)"
wenzelm@11049: 
wenzelm@11049: 
wenzelm@11049: text {* \medskip Inverse *}
wenzelm@11049: 
wenzelm@11049: lemma inv_correct:
nipkow@16663:   "zprime p ==> 0 < a ==> a < p
paulson@11868:     ==> 0 \<le> inv p a \<and> inv p a < p \<and> [a * inv p a = 1] (mod p)"
wenzelm@11049:   apply (unfold inv_def)
wenzelm@11049:   apply (simp (no_asm_simp))
wenzelm@11049:   apply (rule zcong_lineq_unique [THEN ex1_implies_ex, THEN someI_ex])
wenzelm@11049:    apply (erule_tac [2] zless_zprime_imp_zrelprime)
wenzelm@11049:     apply (unfold zprime_def)
wenzelm@11049:     apply auto
wenzelm@11049:   done
wenzelm@11049: 
wenzelm@45605: lemmas inv_ge = inv_correct [THEN conjunct1]
wenzelm@45605: lemmas inv_less = inv_correct [THEN conjunct2, THEN conjunct1]
wenzelm@45605: lemmas inv_is_inv = inv_correct [THEN conjunct2, THEN conjunct2]
wenzelm@11049: 
wenzelm@11049: lemma inv_not_0:
nipkow@16663:   "zprime p ==> 1 < a ==> a < p - 1 ==> inv p a \<noteq> 0"
wenzelm@11049:   -- {* same as @{text WilsonRuss} *}
wenzelm@11049:   apply safe
wenzelm@11049:   apply (cut_tac a = a and p = p in inv_is_inv)
wenzelm@11049:      apply (unfold zcong_def)
wenzelm@11049:      apply auto
wenzelm@11049:   done
paulson@9508: 
wenzelm@11049: lemma inv_not_1:
nipkow@16663:   "zprime p ==> 1 < a ==> a < p - 1 ==> inv p a \<noteq> 1"
wenzelm@11049:   -- {* same as @{text WilsonRuss} *}
wenzelm@11049:   apply safe
wenzelm@11049:   apply (cut_tac a = a and p = p in inv_is_inv)
wenzelm@11049:      prefer 4
wenzelm@11049:      apply simp
paulson@11868:      apply (subgoal_tac "a = 1")
wenzelm@11049:       apply (rule_tac [2] zcong_zless_imp_eq)
wenzelm@11049:           apply auto
wenzelm@11049:   done
wenzelm@11049: 
paulson@11868: lemma aux: "[a * (p - 1) = 1] (mod p) = [a = p - 1] (mod p)"
wenzelm@11049:   -- {* same as @{text WilsonRuss} *}
wenzelm@11049:   apply (unfold zcong_def)
huffman@44766:   apply (simp add: diff_diff_eq diff_diff_eq2 right_diff_distrib)
paulson@11868:   apply (rule_tac s = "p dvd -((a + 1) + (p * -a))" in trans)
haftmann@35048:    apply (simp add: algebra_simps)
nipkow@30042:   apply (subst dvd_minus_iff)
wenzelm@11049:   apply (subst zdvd_reduce)
paulson@11868:   apply (rule_tac s = "p dvd (a + 1) + (p * -1)" in trans)
wenzelm@11049:    apply (subst zdvd_reduce)
wenzelm@11049:    apply auto
wenzelm@11049:   done
wenzelm@11049: 
wenzelm@11049: lemma inv_not_p_minus_1:
nipkow@16663:   "zprime p ==> 1 < a ==> a < p - 1 ==> inv p a \<noteq> p - 1"
wenzelm@11049:   -- {* same as @{text WilsonRuss} *}
wenzelm@11049:   apply safe
wenzelm@11049:   apply (cut_tac a = a and p = p in inv_is_inv)
wenzelm@11049:      apply auto
wenzelm@11049:   apply (simp add: aux)
paulson@11868:   apply (subgoal_tac "a = p - 1")
wenzelm@11049:    apply (rule_tac [2] zcong_zless_imp_eq)
wenzelm@11049:        apply auto
wenzelm@11049:   done
wenzelm@11049: 
wenzelm@11049: text {*
wenzelm@11049:   Below is slightly different as we don't expand @{term [source] inv}
wenzelm@11049:   but use ``@{text correct}'' theorems.
wenzelm@11049: *}
wenzelm@11049: 
nipkow@16663: lemma inv_g_1: "zprime p ==> 1 < a ==> a < p - 1 ==> 1 < inv p a"
paulson@11868:   apply (subgoal_tac "inv p a \<noteq> 1")
paulson@11868:    apply (subgoal_tac "inv p a \<noteq> 0")
wenzelm@11049:     apply (subst order_less_le)
wenzelm@11049:     apply (subst zle_add1_eq_le [symmetric])
wenzelm@11049:     apply (subst order_less_le)
wenzelm@11049:     apply (rule_tac [2] inv_not_0)
wenzelm@11049:       apply (rule_tac [5] inv_not_1)
wenzelm@11049:         apply auto
wenzelm@11049:   apply (rule inv_ge)
wenzelm@11049:     apply auto
wenzelm@11049:   done
wenzelm@11049: 
wenzelm@11049: lemma inv_less_p_minus_1:
nipkow@16663:   "zprime p ==> 1 < a ==> a < p - 1 ==> inv p a < p - 1"
wenzelm@11049:   -- {* ditto *}
wenzelm@11049:   apply (subst order_less_le)
wenzelm@11049:   apply (simp add: inv_not_p_minus_1 inv_less)
wenzelm@11049:   done
wenzelm@11049: 
wenzelm@11049: 
wenzelm@11049: text {* \medskip Bijection *}
wenzelm@11049: 
paulson@11868: lemma aux1: "1 < x ==> 0 \<le> (x::int)"
wenzelm@11049:   apply auto
wenzelm@11049:   done
paulson@9508: 
paulson@11868: lemma aux2: "1 < x ==> 0 < (x::int)"
wenzelm@11049:   apply auto
wenzelm@11049:   done
wenzelm@11049: 
wenzelm@11704: lemma aux3: "x \<le> p - 2 ==> x < (p::int)"
wenzelm@11049:   apply auto
wenzelm@11049:   done
wenzelm@11049: 
paulson@11868: lemma aux4: "x \<le> p - 2 ==> x < (p::int) - 1"
wenzelm@11049:   apply auto
wenzelm@11049:   done
wenzelm@11049: 
nipkow@16663: lemma inv_inj: "zprime p ==> inj_on (inv p) (d22set (p - 2))"
wenzelm@11049:   apply (unfold inj_on_def)
wenzelm@11049:   apply auto
wenzelm@11049:   apply (rule zcong_zless_imp_eq)
wenzelm@39159:       apply (tactic {* stac (@{thm zcong_cancel} RS sym) 5 *})
wenzelm@11049:         apply (rule_tac [7] zcong_trans)
wenzelm@39159:          apply (tactic {* stac @{thm zcong_sym} 8 *})
wenzelm@11049:          apply (erule_tac [7] inv_is_inv)
wenzelm@51717:           apply (tactic "asm_simp_tac @{context} 9")
wenzelm@11049:           apply (erule_tac [9] inv_is_inv)
wenzelm@11049:            apply (rule_tac [6] zless_zprime_imp_zrelprime)
wenzelm@11049:              apply (rule_tac [8] inv_less)
wenzelm@11049:                apply (rule_tac [7] inv_g_1 [THEN aux2])
wenzelm@11049:                  apply (unfold zprime_def)
wenzelm@11049:                  apply (auto intro: d22set_g_1 d22set_le
wenzelm@32960:                    aux1 aux2 aux3 aux4)
wenzelm@11049:   done
wenzelm@11049: 
wenzelm@11049: lemma inv_d22set_d22set:
nipkow@16663:     "zprime p ==> inv p ` d22set (p - 2) = d22set (p - 2)"
wenzelm@11049:   apply (rule endo_inj_surj)
wenzelm@11049:     apply (rule d22set_fin)
wenzelm@11049:    apply (erule_tac [2] inv_inj)
wenzelm@11049:   apply auto
wenzelm@11049:   apply (rule d22set_mem)
wenzelm@11049:    apply (erule inv_g_1)
paulson@11868:     apply (subgoal_tac [3] "inv p xa < p - 1")
wenzelm@11049:      apply (erule_tac [4] inv_less_p_minus_1)
wenzelm@11049:       apply (auto intro: d22set_g_1 d22set_le aux4)
wenzelm@11049:   done
wenzelm@11049: 
wenzelm@11049: lemma d22set_d22set_bij:
nipkow@16663:     "zprime p ==> (d22set (p - 2), d22set (p - 2)) \<in> bijR (reciR p)"
wenzelm@11049:   apply (unfold reciR_def)
wenzelm@11704:   apply (rule_tac s = "(d22set (p - 2), inv p ` d22set (p - 2))" in subst)
wenzelm@11049:    apply (simp add: inv_d22set_d22set)
wenzelm@11049:   apply (rule inj_func_bijR)
wenzelm@11049:     apply (rule_tac [3] d22set_fin)
wenzelm@11049:    apply (erule_tac [2] inv_inj)
wenzelm@11049:   apply auto
wenzelm@11049:       apply (erule inv_is_inv)
wenzelm@11049:        apply (erule_tac [5] inv_g_1)
wenzelm@11049:         apply (erule_tac [7] inv_less_p_minus_1)
wenzelm@11049:          apply (auto intro: d22set_g_1 d22set_le aux2 aux3 aux4)
wenzelm@11049:   done
wenzelm@11049: 
nipkow@16663: lemma reciP_bijP: "zprime p ==> bijP (reciR p) (d22set (p - 2))"
wenzelm@11049:   apply (unfold reciR_def bijP_def)
wenzelm@11049:   apply auto
wenzelm@11049:   apply (rule d22set_mem)
wenzelm@11049:    apply auto
wenzelm@11049:   done
wenzelm@11049: 
nipkow@16663: lemma reciP_uniq: "zprime p ==> uniqP (reciR p)"
wenzelm@11049:   apply (unfold reciR_def uniqP_def)
wenzelm@11049:   apply auto
wenzelm@11049:    apply (rule zcong_zless_imp_eq)
wenzelm@39159:        apply (tactic {* stac (@{thm zcong_cancel2} RS sym) 5 *})
wenzelm@11049:          apply (rule_tac [7] zcong_trans)
wenzelm@39159:           apply (tactic {* stac @{thm zcong_sym} 8 *})
wenzelm@11049:           apply (rule_tac [6] zless_zprime_imp_zrelprime)
wenzelm@11049:             apply auto
wenzelm@11049:   apply (rule zcong_zless_imp_eq)
wenzelm@39159:       apply (tactic {* stac (@{thm zcong_cancel} RS sym) 5 *})
wenzelm@11049:         apply (rule_tac [7] zcong_trans)
wenzelm@39159:          apply (tactic {* stac @{thm zcong_sym} 8 *})
wenzelm@11049:          apply (rule_tac [6] zless_zprime_imp_zrelprime)
wenzelm@11049:            apply auto
wenzelm@11049:   done
wenzelm@11049: 
nipkow@16663: lemma reciP_sym: "zprime p ==> symP (reciR p)"
wenzelm@11049:   apply (unfold reciR_def symP_def)
haftmann@57512:   apply (simp add: mult.commute)
wenzelm@11049:   apply auto
wenzelm@11049:   done
wenzelm@11049: 
nipkow@16663: lemma bijER_d22set: "zprime p ==> d22set (p - 2) \<in> bijER (reciR p)"
wenzelm@11049:   apply (rule bijR_bijER)
wenzelm@11049:      apply (erule d22set_d22set_bij)
wenzelm@11049:     apply (erule reciP_bijP)
wenzelm@11049:    apply (erule reciP_uniq)
wenzelm@11049:   apply (erule reciP_sym)
wenzelm@11049:   done
wenzelm@11049: 
wenzelm@11049: 
wenzelm@11049: subsection {* Wilson *}
wenzelm@11049: 
wenzelm@11049: lemma bijER_zcong_prod_1:
nipkow@16663:     "zprime p ==> A \<in> bijER (reciR p) ==> [\<Prod>A = 1] (mod p)"
wenzelm@11049:   apply (unfold reciR_def)
wenzelm@11049:   apply (erule bijER.induct)
paulson@11868:     apply (subgoal_tac [2] "a = 1 \<or> a = p - 1")
wenzelm@11049:      apply (rule_tac [3] zcong_square_zless)
wenzelm@11049:         apply auto
haftmann@57418:   apply (subst setprod.insert)
wenzelm@11049:     prefer 3
haftmann@57418:     apply (subst setprod.insert)
wenzelm@11049:       apply (auto simp add: fin_bijER)
nipkow@15392:   apply (subgoal_tac "zcong ((a * b) * \<Prod>A) (1 * 1) p")
haftmann@57512:    apply (simp add: mult.assoc)
wenzelm@11049:   apply (rule zcong_zmult)
wenzelm@11049:    apply auto
wenzelm@11049:   done
wenzelm@11049: 
nipkow@16663: theorem Wilson_Bij: "zprime p ==> [zfact (p - 1) = -1] (mod p)"
paulson@11868:   apply (subgoal_tac "zcong ((p - 1) * zfact (p - 2)) (-1 * 1) p")
wenzelm@11049:    apply (rule_tac [2] zcong_zmult)
wenzelm@11049:     apply (simp add: zprime_def)
wenzelm@11049:     apply (subst zfact.simps)
paulson@11868:     apply (rule_tac t = "p - 1 - 1" and s = "p - 2" in subst)
wenzelm@11049:      apply auto
wenzelm@11049:    apply (simp add: zcong_def)
wenzelm@11049:   apply (subst d22set_prod_zfact [symmetric])
wenzelm@11049:   apply (rule bijER_zcong_prod_1)
wenzelm@11049:    apply (rule_tac [2] bijER_d22set)
wenzelm@11049:    apply auto
wenzelm@11049:   done
paulson@9508: 
paulson@9508: end




