src/HOL/Complex/ex/Sqrt.thy
changeset 19086 1b3780be6cc2
parent 16663 13e9c402308b
child 21404 eb85850d3eb7
equal deleted inserted replaced
19085:a1a251b297dd 19086:1b3780be6cc2
    15 text {*
    15 text {*
    16   The set of rational numbers, including the key representation
    16   The set of rational numbers, including the key representation
    17   theorem.
    17   theorem.
    18 *}
    18 *}
    19 
    19 
    20 constdefs
    20 definition
    21   rationals :: "real set"    ("\<rat>")
    21   rationals  ("\<rat>")
    22   "\<rat> \<equiv> {x. \<exists>m n. n \<noteq> 0 \<and> \<bar>x\<bar> = real (m::nat) / real (n::nat)}"
    22   "\<rat> = {x. \<exists>m n. n \<noteq> 0 \<and> \<bar>x\<bar> = real (m::nat) / real (n::nat)}"
    23 
    23 
    24 theorem rationals_rep: "x \<in> \<rat> \<Longrightarrow>
    24 theorem rationals_rep [elim?]:
    25   \<exists>m n. n \<noteq> 0 \<and> \<bar>x\<bar> = real m / real n \<and> gcd (m, n) = 1"
    25   assumes "x \<in> \<rat>"
       
    26   obtains m n where "n \<noteq> 0" and "\<bar>x\<bar> = real m / real n" and "gcd (m, n) = 1"
    26 proof -
    27 proof -
    27   assume "x \<in> \<rat>"
    28   from `x \<in> \<rat>` obtain m n :: nat where
    28   then obtain m n :: nat where
       
    29       n: "n \<noteq> 0" and x_rat: "\<bar>x\<bar> = real m / real n"
    29       n: "n \<noteq> 0" and x_rat: "\<bar>x\<bar> = real m / real n"
    30     by (unfold rationals_def) blast
    30     unfolding rationals_def by blast
    31   let ?gcd = "gcd (m, n)"
    31   let ?gcd = "gcd (m, n)"
    32   from n have gcd: "?gcd \<noteq> 0" by (simp add: gcd_zero)
    32   from n have gcd: "?gcd \<noteq> 0" by (simp add: gcd_zero)
    33   let ?k = "m div ?gcd"
    33   let ?k = "m div ?gcd"
    34   let ?l = "n div ?gcd"
    34   let ?l = "n div ?gcd"
    35   let ?gcd' = "gcd (?k, ?l)"
    35   let ?gcd' = "gcd (?k, ?l)"
    56     have "?gcd * ?gcd' = gcd (?gcd * ?k, ?gcd * ?l)"
    56     have "?gcd * ?gcd' = gcd (?gcd * ?k, ?gcd * ?l)"
    57       by (rule gcd_mult_distrib2)
    57       by (rule gcd_mult_distrib2)
    58     with gcd_k gcd_l have "?gcd * ?gcd' = ?gcd" by simp
    58     with gcd_k gcd_l have "?gcd * ?gcd' = ?gcd" by simp
    59     with gcd show ?thesis by simp
    59     with gcd show ?thesis by simp
    60   qed
    60   qed
    61   ultimately show ?thesis by blast
    61   ultimately show ?thesis ..
    62 qed
    62 qed
    63 
       
    64 lemma [elim?]: "r \<in> \<rat> \<Longrightarrow>
       
    65   (\<And>m n. n \<noteq> 0 \<Longrightarrow> \<bar>r\<bar> = real m / real n \<Longrightarrow> gcd (m, n) = 1 \<Longrightarrow> C)
       
    66     \<Longrightarrow> C"
       
    67   using rationals_rep by blast
       
    68 
    63 
    69 
    64 
    70 subsection {* Main theorem *}
    65 subsection {* Main theorem *}
    71 
    66 
    72 text {*
    67 text {*
    73   The square root of any prime number (including @{text 2}) is
    68   The square root of any prime number (including @{text 2}) is
    74   irrational.
    69   irrational.
    75 *}
    70 *}
    76 
    71 
    77 theorem sqrt_prime_irrational: "prime p \<Longrightarrow> sqrt (real p) \<notin> \<rat>"
    72 theorem sqrt_prime_irrational:
       
    73   assumes "prime p"
       
    74   shows "sqrt (real p) \<notin> \<rat>"
    78 proof
    75 proof
    79   assume p_prime: "prime p"
    76   from `prime p` have p: "1 < p" by (simp add: prime_def)
    80   then have p: "1 < p" by (simp add: prime_def)
       
    81   assume "sqrt (real p) \<in> \<rat>"
    77   assume "sqrt (real p) \<in> \<rat>"
    82   then obtain m n where
    78   then obtain m n where
    83       n: "n \<noteq> 0" and sqrt_rat: "\<bar>sqrt (real p)\<bar> = real m / real n"
    79       n: "n \<noteq> 0" and sqrt_rat: "\<bar>sqrt (real p)\<bar> = real m / real n"
    84     and gcd: "gcd (m, n) = 1" ..
    80     and gcd: "gcd (m, n) = 1" ..
    85   have eq: "m\<twosuperior> = p * n\<twosuperior>"
    81   have eq: "m\<twosuperior> = p * n\<twosuperior>"
    92     finally show ?thesis ..
    88     finally show ?thesis ..
    93   qed
    89   qed
    94   have "p dvd m \<and> p dvd n"
    90   have "p dvd m \<and> p dvd n"
    95   proof
    91   proof
    96     from eq have "p dvd m\<twosuperior>" ..
    92     from eq have "p dvd m\<twosuperior>" ..
    97     with p_prime show "p dvd m" by (rule prime_dvd_power_two)
    93     with `prime p` show "p dvd m" by (rule prime_dvd_power_two)
    98     then obtain k where "m = p * k" ..
    94     then obtain k where "m = p * k" ..
    99     with eq have "p * n\<twosuperior> = p\<twosuperior> * k\<twosuperior>" by (auto simp add: power2_eq_square mult_ac)
    95     with eq have "p * n\<twosuperior> = p\<twosuperior> * k\<twosuperior>" by (auto simp add: power2_eq_square mult_ac)
   100     with p have "n\<twosuperior> = p * k\<twosuperior>" by (simp add: power2_eq_square)
    96     with p have "n\<twosuperior> = p * k\<twosuperior>" by (simp add: power2_eq_square)
   101     then have "p dvd n\<twosuperior>" ..
    97     then have "p dvd n\<twosuperior>" ..
   102     with p_prime show "p dvd n" by (rule prime_dvd_power_two)
    98     with `prime p` show "p dvd n" by (rule prime_dvd_power_two)
   103   qed
    99   qed
   104   then have "p dvd gcd (m, n)" ..
   100   then have "p dvd gcd (m, n)" ..
   105   with gcd have "p dvd 1" by simp
   101   with gcd have "p dvd 1" by simp
   106   then have "p \<le> 1" by (simp add: dvd_imp_le)
   102   then have "p \<le> 1" by (simp add: dvd_imp_le)
   107   with p show False by simp
   103   with p show False by simp
   117   Here is an alternative version of the main proof, using mostly
   113   Here is an alternative version of the main proof, using mostly
   118   linear forward-reasoning.  While this results in less top-down
   114   linear forward-reasoning.  While this results in less top-down
   119   structure, it is probably closer to proofs seen in mathematics.
   115   structure, it is probably closer to proofs seen in mathematics.
   120 *}
   116 *}
   121 
   117 
   122 theorem "prime p \<Longrightarrow> sqrt (real p) \<notin> \<rat>"
   118 theorem
       
   119   assumes "prime p"
       
   120   shows "sqrt (real p) \<notin> \<rat>"
   123 proof
   121 proof
   124   assume p_prime: "prime p"
   122   from `prime p` have p: "1 < p" by (simp add: prime_def)
   125   then have p: "1 < p" by (simp add: prime_def)
       
   126   assume "sqrt (real p) \<in> \<rat>"
   123   assume "sqrt (real p) \<in> \<rat>"
   127   then obtain m n where
   124   then obtain m n where
   128       n: "n \<noteq> 0" and sqrt_rat: "\<bar>sqrt (real p)\<bar> = real m / real n"
   125       n: "n \<noteq> 0" and sqrt_rat: "\<bar>sqrt (real p)\<bar> = real m / real n"
   129     and gcd: "gcd (m, n) = 1" ..
   126     and gcd: "gcd (m, n) = 1" ..
   130   from n and sqrt_rat have "real m = \<bar>sqrt (real p)\<bar> * real n" by simp
   127   from n and sqrt_rat have "real m = \<bar>sqrt (real p)\<bar> * real n" by simp
   132     by (auto simp add: power2_eq_square)
   129     by (auto simp add: power2_eq_square)
   133   also have "(sqrt (real p))\<twosuperior> = real p" by simp
   130   also have "(sqrt (real p))\<twosuperior> = real p" by simp
   134   also have "\<dots> * real (n\<twosuperior>) = real (p * n\<twosuperior>)" by simp
   131   also have "\<dots> * real (n\<twosuperior>) = real (p * n\<twosuperior>)" by simp
   135   finally have eq: "m\<twosuperior> = p * n\<twosuperior>" ..
   132   finally have eq: "m\<twosuperior> = p * n\<twosuperior>" ..
   136   then have "p dvd m\<twosuperior>" ..
   133   then have "p dvd m\<twosuperior>" ..
   137   with p_prime have dvd_m: "p dvd m" by (rule prime_dvd_power_two)
   134   with `prime p` have dvd_m: "p dvd m" by (rule prime_dvd_power_two)
   138   then obtain k where "m = p * k" ..
   135   then obtain k where "m = p * k" ..
   139   with eq have "p * n\<twosuperior> = p\<twosuperior> * k\<twosuperior>" by (auto simp add: power2_eq_square mult_ac)
   136   with eq have "p * n\<twosuperior> = p\<twosuperior> * k\<twosuperior>" by (auto simp add: power2_eq_square mult_ac)
   140   with p have "n\<twosuperior> = p * k\<twosuperior>" by (simp add: power2_eq_square)
   137   with p have "n\<twosuperior> = p * k\<twosuperior>" by (simp add: power2_eq_square)
   141   then have "p dvd n\<twosuperior>" ..
   138   then have "p dvd n\<twosuperior>" ..
   142   with p_prime have "p dvd n" by (rule prime_dvd_power_two)
   139   with `prime p` have "p dvd n" by (rule prime_dvd_power_two)
   143   with dvd_m have "p dvd gcd (m, n)" by (rule gcd_greatest)
   140   with dvd_m have "p dvd gcd (m, n)" by (rule gcd_greatest)
   144   with gcd have "p dvd 1" by simp
   141   with gcd have "p dvd 1" by simp
   145   then have "p \<le> 1" by (simp add: dvd_imp_le)
   142   then have "p \<le> 1" by (simp add: dvd_imp_le)
   146   with p show False by simp
   143   with p show False by simp
   147 qed
   144 qed