src/HOL/ex/Sqrt.thy
 author wenzelm Wed Feb 15 21:38:28 2012 +0100 (2012-02-15) changeset 46495 8e8a339e176f parent 45917 1ce1bc9ff64a child 51708 5188a18c33b1 permissions -rw-r--r--
uniform Isar source formatting for this file;
```     1 (*  Title:      HOL/ex/Sqrt.thy
```
```     2     Author:     Markus Wenzel, Tobias Nipkow, TU Muenchen
```
```     3 *)
```
```     4
```
```     5 header {*  Square roots of primes are irrational *}
```
```     6
```
```     7 theory Sqrt
```
```     8 imports Complex_Main "~~/src/HOL/Number_Theory/Primes"
```
```     9 begin
```
```    10
```
```    11 text {* The square root of any prime number (including 2) is irrational. *}
```
```    12
```
```    13 theorem sqrt_prime_irrational:
```
```    14   assumes "prime (p::nat)"
```
```    15   shows "sqrt (real p) \<notin> \<rat>"
```
```    16 proof
```
```    17   from `prime p` have p: "1 < p" by (simp add: prime_nat_def)
```
```    18   assume "sqrt (real p) \<in> \<rat>"
```
```    19   then obtain m n :: nat where
```
```    20       n: "n \<noteq> 0" and sqrt_rat: "\<bar>sqrt (real p)\<bar> = real m / real n"
```
```    21     and gcd: "gcd m n = 1" by (rule Rats_abs_nat_div_natE)
```
```    22   have eq: "m\<twosuperior> = p * n\<twosuperior>"
```
```    23   proof -
```
```    24     from n and sqrt_rat have "real m = \<bar>sqrt (real p)\<bar> * real n" by simp
```
```    25     then have "real (m\<twosuperior>) = (sqrt (real p))\<twosuperior> * real (n\<twosuperior>)"
```
```    26       by (auto simp add: power2_eq_square)
```
```    27     also have "(sqrt (real p))\<twosuperior> = real p" by simp
```
```    28     also have "\<dots> * real (n\<twosuperior>) = real (p * n\<twosuperior>)" by simp
```
```    29     finally show ?thesis ..
```
```    30   qed
```
```    31   have "p dvd m \<and> p dvd n"
```
```    32   proof
```
```    33     from eq have "p dvd m\<twosuperior>" ..
```
```    34     with `prime p` pos2 show "p dvd m" by (rule prime_dvd_power_nat)
```
```    35     then obtain k where "m = p * k" ..
```
```    36     with eq have "p * n\<twosuperior> = p\<twosuperior> * k\<twosuperior>" by (auto simp add: power2_eq_square mult_ac)
```
```    37     with p have "n\<twosuperior> = p * k\<twosuperior>" by (simp add: power2_eq_square)
```
```    38     then have "p dvd n\<twosuperior>" ..
```
```    39     with `prime p` pos2 show "p dvd n" by (rule prime_dvd_power_nat)
```
```    40   qed
```
```    41   then have "p dvd gcd m n" ..
```
```    42   with gcd have "p dvd 1" by simp
```
```    43   then have "p \<le> 1" by (simp add: dvd_imp_le)
```
```    44   with p show False by simp
```
```    45 qed
```
```    46
```
```    47 corollary sqrt_real_2_not_rat: "sqrt (real (2::nat)) \<notin> \<rat>"
```
```    48   by (rule sqrt_prime_irrational) (rule two_is_prime_nat)
```
```    49
```
```    50
```
```    51 subsection {* Variations *}
```
```    52
```
```    53 text {*
```
```    54   Here is an alternative version of the main proof, using mostly
```
```    55   linear forward-reasoning.  While this results in less top-down
```
```    56   structure, it is probably closer to proofs seen in mathematics.
```
```    57 *}
```
```    58
```
```    59 theorem
```
```    60   assumes "prime (p::nat)"
```
```    61   shows "sqrt (real p) \<notin> \<rat>"
```
```    62 proof
```
```    63   from `prime p` have p: "1 < p" by (simp add: prime_nat_def)
```
```    64   assume "sqrt (real p) \<in> \<rat>"
```
```    65   then obtain m n :: nat where
```
```    66       n: "n \<noteq> 0" and sqrt_rat: "\<bar>sqrt (real p)\<bar> = real m / real n"
```
```    67     and gcd: "gcd m n = 1" by (rule Rats_abs_nat_div_natE)
```
```    68   from n and sqrt_rat have "real m = \<bar>sqrt (real p)\<bar> * real n" by simp
```
```    69   then have "real (m\<twosuperior>) = (sqrt (real p))\<twosuperior> * real (n\<twosuperior>)"
```
```    70     by (auto simp add: power2_eq_square)
```
```    71   also have "(sqrt (real p))\<twosuperior> = real p" by simp
```
```    72   also have "\<dots> * real (n\<twosuperior>) = real (p * n\<twosuperior>)" by simp
```
```    73   finally have eq: "m\<twosuperior> = p * n\<twosuperior>" ..
```
```    74   then have "p dvd m\<twosuperior>" ..
```
```    75   with `prime p` pos2 have dvd_m: "p dvd m" by (rule prime_dvd_power_nat)
```
```    76   then obtain k where "m = p * k" ..
```
```    77   with eq have "p * n\<twosuperior> = p\<twosuperior> * k\<twosuperior>" by (auto simp add: power2_eq_square mult_ac)
```
```    78   with p have "n\<twosuperior> = p * k\<twosuperior>" by (simp add: power2_eq_square)
```
```    79   then have "p dvd n\<twosuperior>" ..
```
```    80   with `prime p` pos2 have "p dvd n" by (rule prime_dvd_power_nat)
```
```    81   with dvd_m have "p dvd gcd m n" by (rule gcd_greatest_nat)
```
```    82   with gcd have "p dvd 1" by simp
```
```    83   then have "p \<le> 1" by (simp add: dvd_imp_le)
```
```    84   with p show False by simp
```
```    85 qed
```
```    86
```
```    87
```
```    88 text {* Another old chestnut, which is a consequence of the irrationality of 2. *}
```
```    89
```
```    90 lemma "\<exists>a b::real. a \<notin> \<rat> \<and> b \<notin> \<rat> \<and> a powr b \<in> \<rat>" (is "EX a b. ?P a b")
```
```    91 proof cases
```
```    92   assume "sqrt 2 powr sqrt 2 \<in> \<rat>"
```
```    93   then have "?P (sqrt 2) (sqrt 2)"
```
```    94     by (metis sqrt_real_2_not_rat [simplified])
```
```    95   then show ?thesis by blast
```
```    96 next
```
```    97   assume 1: "sqrt 2 powr sqrt 2 \<notin> \<rat>"
```
```    98   have "(sqrt 2 powr sqrt 2) powr sqrt 2 = 2"
```
```    99     using powr_realpow [of _ 2]
```
```   100     by (simp add: powr_powr power2_eq_square [symmetric])
```
```   101   then have "?P (sqrt 2 powr sqrt 2) (sqrt 2)"
```
```   102     by (metis 1 Rats_number_of sqrt_real_2_not_rat [simplified])
```
```   103   then show ?thesis by blast
```
```   104 qed
```
```   105
```
```   106 end
```