src/HOL/ex/Sqrt.thy
author nipkow
Fri Mar 06 17:38:47 2009 +0100 (2009-03-06)
changeset 30313 b2441b0c8d38
parent 28952 15a4b2cf8c34
child 30411 9c9b6511ad1b
permissions -rw-r--r--
added lemmas
haftmann@28952
     1
(*  Title:      HOL/ex/Sqrt.thy
paulson@13957
     2
    Author:     Markus Wenzel, TU Muenchen
kleing@14981
     3
paulson@13957
     4
*)
paulson@13957
     5
paulson@13957
     6
header {*  Square roots of primes are irrational *}
paulson@13957
     7
nipkow@15149
     8
theory Sqrt
haftmann@28952
     9
imports Complex_Main Primes
nipkow@15149
    10
begin
paulson@13957
    11
nipkow@28001
    12
text {* The definition and the key representation theorem for the set of
nipkow@28001
    13
rational numbers has been moved to a core theory.  *}
paulson@13957
    14
nipkow@28001
    15
declare Rats_abs_nat_div_natE[elim?]
paulson@13957
    16
paulson@13957
    17
subsection {* Main theorem *}
paulson@13957
    18
paulson@13957
    19
text {*
paulson@13957
    20
  The square root of any prime number (including @{text 2}) is
paulson@13957
    21
  irrational.
paulson@13957
    22
*}
paulson@13957
    23
wenzelm@19086
    24
theorem sqrt_prime_irrational:
wenzelm@19086
    25
  assumes "prime p"
wenzelm@19086
    26
  shows "sqrt (real p) \<notin> \<rat>"
paulson@13957
    27
proof
wenzelm@19086
    28
  from `prime p` have p: "1 < p" by (simp add: prime_def)
paulson@13957
    29
  assume "sqrt (real p) \<in> \<rat>"
paulson@13957
    30
  then obtain m n where
paulson@13957
    31
      n: "n \<noteq> 0" and sqrt_rat: "\<bar>sqrt (real p)\<bar> = real m / real n"
haftmann@27556
    32
    and gcd: "gcd m n = 1" ..
paulson@13957
    33
  have eq: "m\<twosuperior> = p * n\<twosuperior>"
paulson@13957
    34
  proof -
paulson@13957
    35
    from n and sqrt_rat have "real m = \<bar>sqrt (real p)\<bar> * real n" by simp
paulson@13957
    36
    then have "real (m\<twosuperior>) = (sqrt (real p))\<twosuperior> * real (n\<twosuperior>)"
paulson@14353
    37
      by (auto simp add: power2_eq_square)
paulson@13957
    38
    also have "(sqrt (real p))\<twosuperior> = real p" by simp
paulson@13957
    39
    also have "\<dots> * real (n\<twosuperior>) = real (p * n\<twosuperior>)" by simp
paulson@13957
    40
    finally show ?thesis ..
paulson@13957
    41
  qed
paulson@13957
    42
  have "p dvd m \<and> p dvd n"
paulson@13957
    43
  proof
paulson@13957
    44
    from eq have "p dvd m\<twosuperior>" ..
wenzelm@19086
    45
    with `prime p` show "p dvd m" by (rule prime_dvd_power_two)
paulson@13957
    46
    then obtain k where "m = p * k" ..
paulson@14353
    47
    with eq have "p * n\<twosuperior> = p\<twosuperior> * k\<twosuperior>" by (auto simp add: power2_eq_square mult_ac)
paulson@14353
    48
    with p have "n\<twosuperior> = p * k\<twosuperior>" by (simp add: power2_eq_square)
paulson@13957
    49
    then have "p dvd n\<twosuperior>" ..
wenzelm@19086
    50
    with `prime p` show "p dvd n" by (rule prime_dvd_power_two)
paulson@13957
    51
  qed
haftmann@27556
    52
  then have "p dvd gcd m n" ..
paulson@13957
    53
  with gcd have "p dvd 1" by simp
paulson@13957
    54
  then have "p \<le> 1" by (simp add: dvd_imp_le)
paulson@13957
    55
  with p show False by simp
paulson@13957
    56
qed
paulson@13957
    57
paulson@13957
    58
corollary "sqrt (real (2::nat)) \<notin> \<rat>"
paulson@13957
    59
  by (rule sqrt_prime_irrational) (rule two_is_prime)
paulson@13957
    60
paulson@13957
    61
paulson@13957
    62
subsection {* Variations *}
paulson@13957
    63
paulson@13957
    64
text {*
paulson@13957
    65
  Here is an alternative version of the main proof, using mostly
paulson@13957
    66
  linear forward-reasoning.  While this results in less top-down
paulson@13957
    67
  structure, it is probably closer to proofs seen in mathematics.
paulson@13957
    68
*}
paulson@13957
    69
wenzelm@19086
    70
theorem
wenzelm@19086
    71
  assumes "prime p"
wenzelm@19086
    72
  shows "sqrt (real p) \<notin> \<rat>"
paulson@13957
    73
proof
wenzelm@19086
    74
  from `prime p` have p: "1 < p" by (simp add: prime_def)
paulson@13957
    75
  assume "sqrt (real p) \<in> \<rat>"
paulson@13957
    76
  then obtain m n where
paulson@13957
    77
      n: "n \<noteq> 0" and sqrt_rat: "\<bar>sqrt (real p)\<bar> = real m / real n"
haftmann@27556
    78
    and gcd: "gcd m n = 1" ..
paulson@13957
    79
  from n and sqrt_rat have "real m = \<bar>sqrt (real p)\<bar> * real n" by simp
paulson@13957
    80
  then have "real (m\<twosuperior>) = (sqrt (real p))\<twosuperior> * real (n\<twosuperior>)"
paulson@14353
    81
    by (auto simp add: power2_eq_square)
paulson@13957
    82
  also have "(sqrt (real p))\<twosuperior> = real p" by simp
paulson@13957
    83
  also have "\<dots> * real (n\<twosuperior>) = real (p * n\<twosuperior>)" by simp
paulson@13957
    84
  finally have eq: "m\<twosuperior> = p * n\<twosuperior>" ..
paulson@13957
    85
  then have "p dvd m\<twosuperior>" ..
wenzelm@19086
    86
  with `prime p` have dvd_m: "p dvd m" by (rule prime_dvd_power_two)
paulson@13957
    87
  then obtain k where "m = p * k" ..
paulson@14353
    88
  with eq have "p * n\<twosuperior> = p\<twosuperior> * k\<twosuperior>" by (auto simp add: power2_eq_square mult_ac)
paulson@14353
    89
  with p have "n\<twosuperior> = p * k\<twosuperior>" by (simp add: power2_eq_square)
paulson@13957
    90
  then have "p dvd n\<twosuperior>" ..
wenzelm@19086
    91
  with `prime p` have "p dvd n" by (rule prime_dvd_power_two)
haftmann@27556
    92
  with dvd_m have "p dvd gcd m n" by (rule gcd_greatest)
paulson@13957
    93
  with gcd have "p dvd 1" by simp
paulson@13957
    94
  then have "p \<le> 1" by (simp add: dvd_imp_le)
paulson@13957
    95
  with p show False by simp
paulson@13957
    96
qed
paulson@13957
    97
paulson@13957
    98
end