src/HOL/Hyperreal/ex/Sqrt.thy
changeset 13958 c1c67582c9b5
parent 13957 10dbf16be15f
child 13959 0e0553e7d696
equal deleted inserted replaced
13957:10dbf16be15f 13958:c1c67582c9b5
     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
       
    27       n: "n \<noteq> 0" and x_rat: "\<bar>x\<bar> = real m / real n"
       
    28     by (unfold rationals_def) blast
       
    29   let ?gcd = "gcd (m, n)"
       
    30   from n have gcd: "?gcd \<noteq> 0" by (simp add: gcd_zero)
       
    31   let ?k = "m div ?gcd"
       
    32   let ?l = "n div ?gcd"
       
    33   let ?gcd' = "gcd (?k, ?l)"
       
    34   have "?gcd dvd m" .. then have gcd_k: "?gcd * ?k = m"
       
    35     by (rule dvd_mult_div_cancel)
       
    36   have "?gcd dvd n" .. then have gcd_l: "?gcd * ?l = n"
       
    37     by (rule dvd_mult_div_cancel)
       
    38 
       
    39   from n and gcd_l have "?l \<noteq> 0"
       
    40     by (auto iff del: neq0_conv)
       
    41   moreover
       
    42   have "\<bar>x\<bar> = real ?k / real ?l"
       
    43   proof -
       
    44     from gcd have "real ?k / real ?l =
       
    45         real (?gcd * ?k) / real (?gcd * ?l)"
       
    46       by (simp add: real_mult_div_cancel1)
       
    47     also from gcd_k and gcd_l have "\<dots> = real m / real n" by simp
       
    48     also from x_rat have "\<dots> = \<bar>x\<bar>" ..
       
    49     finally show ?thesis ..
       
    50   qed
       
    51   moreover
       
    52   have "?gcd' = 1"
       
    53   proof -
       
    54     have "?gcd * ?gcd' = gcd (?gcd * ?k, ?gcd * ?l)"
       
    55       by (rule gcd_mult_distrib2)
       
    56     with gcd_k gcd_l have "?gcd * ?gcd' = ?gcd" by simp
       
    57     with gcd show ?thesis by simp
       
    58   qed
       
    59   ultimately show ?thesis by blast
       
    60 qed
       
    61 
       
    62 lemma [elim?]: "r \<in> \<rat> \<Longrightarrow>
       
    63   (\<And>m n. n \<noteq> 0 \<Longrightarrow> \<bar>r\<bar> = real m / real n \<Longrightarrow> gcd (m, n) = 1 \<Longrightarrow> C)
       
    64     \<Longrightarrow> C"
       
    65   using rationals_rep by blast
       
    66 
       
    67 
       
    68 subsection {* Main theorem *}
       
    69 
       
    70 text {*
       
    71   The square root of any prime number (including @{text 2}) is
       
    72   irrational.
       
    73 *}
       
    74 
       
    75 theorem sqrt_prime_irrational: "p \<in> prime \<Longrightarrow> sqrt (real p) \<notin> \<rat>"
       
    76 proof
       
    77   assume p_prime: "p \<in> prime"
       
    78   then have p: "1 < p" by (simp add: prime_def)
       
    79   assume "sqrt (real p) \<in> \<rat>"
       
    80   then obtain m n where
       
    81       n: "n \<noteq> 0" and sqrt_rat: "\<bar>sqrt (real p)\<bar> = real m / real n"
       
    82     and gcd: "gcd (m, n) = 1" ..
       
    83   have eq: "m\<twosuperior> = p * n\<twosuperior>"
       
    84   proof -
       
    85     from n and sqrt_rat have "real m = \<bar>sqrt (real p)\<bar> * real n" by simp
       
    86     then have "real (m\<twosuperior>) = (sqrt (real p))\<twosuperior> * real (n\<twosuperior>)"
       
    87       by (auto simp add: power_two real_power_two)
       
    88     also have "(sqrt (real p))\<twosuperior> = real p" by simp
       
    89     also have "\<dots> * real (n\<twosuperior>) = real (p * n\<twosuperior>)" by simp
       
    90     finally show ?thesis ..
       
    91   qed
       
    92   have "p dvd m \<and> p dvd n"
       
    93   proof
       
    94     from eq have "p dvd m\<twosuperior>" ..
       
    95     with p_prime show "p dvd m" by (rule prime_dvd_power_two)
       
    96     then obtain k where "m = p * k" ..
       
    97     with eq have "p * n\<twosuperior> = p\<twosuperior> * k\<twosuperior>" by (auto simp add: power_two mult_ac)
       
    98     with p have "n\<twosuperior> = p * k\<twosuperior>" by (simp add: power_two)
       
    99     then have "p dvd n\<twosuperior>" ..
       
   100     with p_prime show "p dvd n" by (rule prime_dvd_power_two)
       
   101   qed
       
   102   then have "p dvd gcd (m, n)" ..
       
   103   with gcd have "p dvd 1" by simp
       
   104   then have "p \<le> 1" by (simp add: dvd_imp_le)
       
   105   with p show False by simp
       
   106 qed
       
   107 
       
   108 corollary "sqrt (real (2::nat)) \<notin> \<rat>"
       
   109   by (rule sqrt_prime_irrational) (rule two_is_prime)
       
   110 
       
   111 
       
   112 subsection {* Variations *}
       
   113 
       
   114 text {*
       
   115   Here is an alternative version of the main proof, using mostly
       
   116   linear forward-reasoning.  While this results in less top-down
       
   117   structure, it is probably closer to proofs seen in mathematics.
       
   118 *}
       
   119 
       
   120 theorem "p \<in> prime \<Longrightarrow> sqrt (real p) \<notin> \<rat>"
       
   121 proof
       
   122   assume p_prime: "p \<in> prime"
       
   123   then have p: "1 < p" by (simp add: prime_def)
       
   124   assume "sqrt (real p) \<in> \<rat>"
       
   125   then obtain m n where
       
   126       n: "n \<noteq> 0" and sqrt_rat: "\<bar>sqrt (real p)\<bar> = real m / real n"
       
   127     and gcd: "gcd (m, n) = 1" ..
       
   128   from n and sqrt_rat have "real m = \<bar>sqrt (real p)\<bar> * real n" by simp
       
   129   then have "real (m\<twosuperior>) = (sqrt (real p))\<twosuperior> * real (n\<twosuperior>)"
       
   130     by (auto simp add: power_two real_power_two)
       
   131   also have "(sqrt (real p))\<twosuperior> = real p" by simp
       
   132   also have "\<dots> * real (n\<twosuperior>) = real (p * n\<twosuperior>)" by simp
       
   133   finally have eq: "m\<twosuperior> = p * n\<twosuperior>" ..
       
   134   then have "p dvd m\<twosuperior>" ..
       
   135   with p_prime have dvd_m: "p dvd m" by (rule prime_dvd_power_two)
       
   136   then obtain k where "m = p * k" ..
       
   137   with eq have "p * n\<twosuperior> = p\<twosuperior> * k\<twosuperior>" by (auto simp add: power_two mult_ac)
       
   138   with p have "n\<twosuperior> = p * k\<twosuperior>" by (simp add: power_two)
       
   139   then have "p dvd n\<twosuperior>" ..
       
   140   with p_prime have "p dvd n" by (rule prime_dvd_power_two)
       
   141   with dvd_m have "p dvd gcd (m, n)" by (rule gcd_greatest)
       
   142   with gcd have "p dvd 1" by simp
       
   143   then have "p \<le> 1" by (simp add: dvd_imp_le)
       
   144   with p show False by simp
       
   145 qed
       
   146 
       
   147 end