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