src/HOL/Hyperreal/ex/Sqrt.thy
changeset 13029 84e4ba7fb033
child 13035 d3be9be2b307
equal deleted inserted replaced
13028:81c87faed78b 13029:84e4ba7fb033
       
     1 (*  Title:      HOL/Hyperreal/ex/Sqrt.thy
       
     2     ID:         $Id$
       
     3     Author:     Markus Wenzel, TU Muenchen
       
     4     License:    GPL (GNU GENERAL PUBLIC LICENSE)
       
     5 *)
       
     6 
       
     7 header {*  Square roots of primes are irrational *}
       
     8 
       
     9 theory Sqrt = Primes + Hyperreal:
       
    10 
       
    11 subsection {* Preliminaries *}
       
    12 
       
    13 text {*
       
    14   The set of rational numbers, including the key representation
       
    15   theorem.
       
    16 *}
       
    17 
       
    18 constdefs
       
    19   rationals :: "real set"    ("\<rat>")
       
    20   "\<rat> \<equiv> {x. \<exists>m n. n \<noteq> 0 \<and> \<bar>x\<bar> = real (m::nat) / real (n::nat)}"
       
    21 
       
    22 theorem rationals_rep: "x \<in> \<rat> \<Longrightarrow>
       
    23   \<exists>m n. n \<noteq> 0 \<and> \<bar>x\<bar> = real m / real n \<and> gcd (m, n) = 1"
       
    24 proof -
       
    25   assume "x \<in> \<rat>"
       
    26   then obtain m n :: nat where n: "n \<noteq> 0" and x_rat: "\<bar>x\<bar> = real m / real n"
       
    27     by (unfold rationals_def) blast
       
    28   let ?gcd = "gcd (m, n)"
       
    29   from n have gcd: "?gcd \<noteq> 0" by (simp add: gcd_zero)
       
    30   let ?k = "m div ?gcd"
       
    31   let ?l = "n div ?gcd"
       
    32   let ?gcd' = "gcd (?k, ?l)"
       
    33   have "?gcd dvd m" .. then have gcd_k: "?gcd * ?k = m"
       
    34     by (rule dvd_mult_div_cancel)
       
    35   have "?gcd dvd n" .. then have gcd_l: "?gcd * ?l = n"
       
    36     by (rule dvd_mult_div_cancel)
       
    37 
       
    38   from n and gcd_l have "?l \<noteq> 0"
       
    39     by (auto iff del: neq0_conv)
       
    40   moreover
       
    41   have "\<bar>x\<bar> = real ?k / real ?l"
       
    42   proof -
       
    43     from gcd have "real ?k / real ?l = real (?gcd * ?k) / real (?gcd * ?l)"
       
    44       by (simp add: real_mult_div_cancel1)
       
    45     also from gcd_k and gcd_l have "\<dots> = real m / real n" by simp
       
    46     also from x_rat have "\<dots> = \<bar>x\<bar>" ..
       
    47     finally show ?thesis ..
       
    48   qed
       
    49   moreover
       
    50   have "?gcd' = 1"
       
    51   proof -
       
    52     have "?gcd * ?gcd' = gcd (?gcd * ?k, ?gcd * ?l)"
       
    53       by (rule gcd_mult_distrib2)
       
    54     with gcd_k gcd_l have "?gcd * ?gcd' = ?gcd" by simp
       
    55     with gcd show ?thesis by simp
       
    56   qed
       
    57   ultimately show ?thesis by blast
       
    58 qed
       
    59 
       
    60 lemma [elim?]: "r \<in> \<rat> \<Longrightarrow>
       
    61   (\<And>m n. n \<noteq> 0 \<Longrightarrow> \<bar>r\<bar> = real m / real n \<Longrightarrow> gcd (m, n) = 1 \<Longrightarrow> C)
       
    62     \<Longrightarrow> C"
       
    63   using rationals_rep by blast
       
    64 
       
    65 
       
    66 subsection {* Main theorem *}
       
    67 
       
    68 text {*
       
    69   The square root of any prime number (including @{text 2}) is
       
    70   irrational.
       
    71 *}
       
    72 
       
    73 theorem sqrt_prime_irrational: "p \<in> prime \<Longrightarrow> sqrt (real p) \<notin> \<rat>"
       
    74 proof
       
    75   assume p_prime: "p \<in> prime"
       
    76   then have p: "1 < p" by (simp add: prime_def)
       
    77   assume "sqrt (real p) \<in> \<rat>"
       
    78   then obtain m n where
       
    79       n: "n \<noteq> 0" and sqrt_rat: "\<bar>sqrt (real p)\<bar> = real m / real n"
       
    80     and gcd: "gcd (m, n) = 1" ..
       
    81   have eq: "m\<twosuperior> = p * n\<twosuperior>"
       
    82   proof -
       
    83     from n and sqrt_rat have "real m = \<bar>sqrt (real p)\<bar> * real n" by simp
       
    84     then have "real (m\<twosuperior>) = (sqrt (real p))\<twosuperior> * real (n\<twosuperior>)"
       
    85       by (auto simp add: power_two real_power_two)
       
    86     also have "(sqrt (real p))\<twosuperior> = real p" by simp
       
    87     also have "\<dots> * real (n\<twosuperior>) = real (p * n\<twosuperior>)" by simp
       
    88     finally show ?thesis ..
       
    89   qed
       
    90   have "p dvd m \<and> p dvd n"
       
    91   proof
       
    92     from eq have "p dvd m\<twosuperior>" ..
       
    93     with p_prime show "p dvd m" by (rule prime_dvd_power_two)
       
    94     then obtain k where "m = p * k" ..
       
    95     with eq have "p * n\<twosuperior> = p\<twosuperior> * k\<twosuperior>" by (auto simp add: power_two mult_ac)
       
    96     with p have "n\<twosuperior> = p * k\<twosuperior>" by (simp add: power_two)
       
    97     then have "p dvd n\<twosuperior>" ..
       
    98     with p_prime show "p dvd n" by (rule prime_dvd_power_two)
       
    99   qed
       
   100   then have "p dvd gcd (m, n)" ..
       
   101   with gcd have "p dvd 1" by simp
       
   102   then have "p \<le> 1" by (simp add: dvd_imp_le)
       
   103   with p show False by simp
       
   104 qed
       
   105 
       
   106 text {*
       
   107   Just for the record: we instantiate the main theorem for the
       
   108   specific prime number @{text 2} (real mathematicians would never do
       
   109   this formally :-).
       
   110 *}
       
   111 
       
   112 corollary "sqrt (real (2::nat)) \<notin> \<rat>"
       
   113   by (rule sqrt_prime_irrational) (rule two_is_prime)
       
   114 
       
   115 
       
   116 subsection {* Variations *}
       
   117 
       
   118 text {*
       
   119   Here is an alternative version of the main proof, using mostly
       
   120   linear forward-reasoning.  While this results in less top-down
       
   121   structure, it is probably closer to proofs seen in mathematics.
       
   122 *}
       
   123 
       
   124 theorem "p \<in> prime \<Longrightarrow> sqrt (real p) \<notin> \<rat>"
       
   125 proof
       
   126   assume p_prime: "p \<in> prime"
       
   127   then have p: "1 < p" by (simp add: prime_def)
       
   128   assume "sqrt (real p) \<in> \<rat>"
       
   129   then obtain m n where
       
   130       n: "n \<noteq> 0" and sqrt_rat: "\<bar>sqrt (real p)\<bar> = real m / real n"
       
   131     and gcd: "gcd (m, n) = 1" ..
       
   132   from n and sqrt_rat have "real m = \<bar>sqrt (real p)\<bar> * real n" by simp
       
   133   then have "real (m\<twosuperior>) = (sqrt (real p))\<twosuperior> * real (n\<twosuperior>)"
       
   134     by (auto simp add: power_two real_power_two)
       
   135   also have "(sqrt (real p))\<twosuperior> = real p" by simp
       
   136   also have "\<dots> * real (n\<twosuperior>) = real (p * n\<twosuperior>)" by simp
       
   137   finally have eq: "m\<twosuperior> = p * n\<twosuperior>" ..
       
   138   then have "p dvd m\<twosuperior>" ..
       
   139   with p_prime have dvd_m: "p dvd m" by (rule prime_dvd_power_two)
       
   140   then obtain k where "m = p * k" ..
       
   141   with eq have "p * n\<twosuperior> = p\<twosuperior> * k\<twosuperior>" by (auto simp add: power_two mult_ac)
       
   142   with p have "n\<twosuperior> = p * k\<twosuperior>" by (simp add: power_two)
       
   143   then have "p dvd n\<twosuperior>" ..
       
   144   with p_prime have "p dvd n" by (rule prime_dvd_power_two)
       
   145   with dvd_m have "p dvd gcd (m, n)" by (rule gcd_greatest)
       
   146   with gcd have "p dvd 1" by simp
       
   147   then have "p \<le> 1" by (simp add: dvd_imp_le)
       
   148   with p show False by simp
       
   149 qed
       
   150 
       
   151 end