src/HOL/ex/Sqrt.thy
author wenzelm
Sat, 05 Jun 2021 12:45:00 +0200
changeset 73810 1c5dcba6925f
parent 73809 ce9529a616fd
permissions -rw-r--r--
tuned proofs;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
28952
15a4b2cf8c34 made repository layout more coherent with logical distribution structure; stripped some $Id$s
haftmann
parents: 28001
diff changeset
     1
(*  Title:      HOL/ex/Sqrt.thy
73810
1c5dcba6925f tuned proofs;
wenzelm
parents: 73809
diff changeset
     2
    Author:     Makarius
1c5dcba6925f tuned proofs;
wenzelm
parents: 73809
diff changeset
     3
    Author:     Tobias Nipkow, TU Muenchen
13957
10dbf16be15f new session Complex for the complex numbers
paulson
parents:
diff changeset
     4
*)
10dbf16be15f new session Complex for the complex numbers
paulson
parents:
diff changeset
     5
59031
4c3bb56b8ce7 misc tuning and modernization;
wenzelm
parents: 58889
diff changeset
     6
section \<open>Square roots of primes are irrational\<close>
13957
10dbf16be15f new session Complex for the complex numbers
paulson
parents:
diff changeset
     7
15149
c5c4884634b7 new import syntax
nipkow
parents: 14981
diff changeset
     8
theory Sqrt
73810
1c5dcba6925f tuned proofs;
wenzelm
parents: 73809
diff changeset
     9
  imports Complex_Main "HOL-Computational_Algebra.Primes"
15149
c5c4884634b7 new import syntax
nipkow
parents: 14981
diff changeset
    10
begin
13957
10dbf16be15f new session Complex for the complex numbers
paulson
parents:
diff changeset
    11
73810
1c5dcba6925f tuned proofs;
wenzelm
parents: 73809
diff changeset
    12
text \<open>
1c5dcba6925f tuned proofs;
wenzelm
parents: 73809
diff changeset
    13
  The square root of any prime number (including 2) is irrational.
1c5dcba6925f tuned proofs;
wenzelm
parents: 73809
diff changeset
    14
\<close>
13957
10dbf16be15f new session Complex for the complex numbers
paulson
parents:
diff changeset
    15
19086
1b3780be6cc2 new-style definitions/abbreviations;
wenzelm
parents: 16663
diff changeset
    16
theorem sqrt_prime_irrational:
73810
1c5dcba6925f tuned proofs;
wenzelm
parents: 73809
diff changeset
    17
  fixes p :: nat
1c5dcba6925f tuned proofs;
wenzelm
parents: 73809
diff changeset
    18
  assumes "prime p"
51708
5188a18c33b1 use automatic type coerctions in Sqrt example
hoelzl
parents: 46495
diff changeset
    19
  shows "sqrt p \<notin> \<rat>"
13957
10dbf16be15f new session Complex for the complex numbers
paulson
parents:
diff changeset
    20
proof
73810
1c5dcba6925f tuned proofs;
wenzelm
parents: 73809
diff changeset
    21
  from \<open>prime p\<close> have p: "p > 1" by (rule prime_gt_1_nat)
51708
5188a18c33b1 use automatic type coerctions in Sqrt example
hoelzl
parents: 46495
diff changeset
    22
  assume "sqrt p \<in> \<rat>"
73810
1c5dcba6925f tuned proofs;
wenzelm
parents: 73809
diff changeset
    23
  then obtain m n :: nat
1c5dcba6925f tuned proofs;
wenzelm
parents: 73809
diff changeset
    24
    where n: "n \<noteq> 0"
1c5dcba6925f tuned proofs;
wenzelm
parents: 73809
diff changeset
    25
      and sqrt_rat: "\<bar>sqrt p\<bar> = m / n"
1c5dcba6925f tuned proofs;
wenzelm
parents: 73809
diff changeset
    26
      and "coprime m n" by (rule Rats_abs_nat_div_natE)
53015
a1119cf551e8 standardized symbols via "isabelle update_sub_sup", excluding src/Pure and src/Tools/WWW_Find;
wenzelm
parents: 51708
diff changeset
    27
  have eq: "m\<^sup>2 = p * n\<^sup>2"
13957
10dbf16be15f new session Complex for the complex numbers
paulson
parents:
diff changeset
    28
  proof -
51708
5188a18c33b1 use automatic type coerctions in Sqrt example
hoelzl
parents: 46495
diff changeset
    29
    from n and sqrt_rat have "m = \<bar>sqrt p\<bar> * n" by simp
73810
1c5dcba6925f tuned proofs;
wenzelm
parents: 73809
diff changeset
    30
    then have "m\<^sup>2 = (sqrt p)\<^sup>2 * n\<^sup>2" by (simp add: power_mult_distrib)
53015
a1119cf551e8 standardized symbols via "isabelle update_sub_sup", excluding src/Pure and src/Tools/WWW_Find;
wenzelm
parents: 51708
diff changeset
    31
    also have "(sqrt p)\<^sup>2 = p" by simp
a1119cf551e8 standardized symbols via "isabelle update_sub_sup", excluding src/Pure and src/Tools/WWW_Find;
wenzelm
parents: 51708
diff changeset
    32
    also have "\<dots> * n\<^sup>2 = p * n\<^sup>2" by simp
73810
1c5dcba6925f tuned proofs;
wenzelm
parents: 73809
diff changeset
    33
    finally show ?thesis by linarith
13957
10dbf16be15f new session Complex for the complex numbers
paulson
parents:
diff changeset
    34
  qed
10dbf16be15f new session Complex for the complex numbers
paulson
parents:
diff changeset
    35
  have "p dvd m \<and> p dvd n"
10dbf16be15f new session Complex for the complex numbers
paulson
parents:
diff changeset
    36
  proof
53015
a1119cf551e8 standardized symbols via "isabelle update_sub_sup", excluding src/Pure and src/Tools/WWW_Find;
wenzelm
parents: 51708
diff changeset
    37
    from eq have "p dvd m\<^sup>2" ..
73810
1c5dcba6925f tuned proofs;
wenzelm
parents: 73809
diff changeset
    38
    with \<open>prime p\<close> show "p dvd m" by (rule prime_dvd_power)
13957
10dbf16be15f new session Complex for the complex numbers
paulson
parents:
diff changeset
    39
    then obtain k where "m = p * k" ..
73810
1c5dcba6925f tuned proofs;
wenzelm
parents: 73809
diff changeset
    40
    with eq have "p * n\<^sup>2 = p\<^sup>2 * k\<^sup>2" by algebra
53015
a1119cf551e8 standardized symbols via "isabelle update_sub_sup", excluding src/Pure and src/Tools/WWW_Find;
wenzelm
parents: 51708
diff changeset
    41
    with p have "n\<^sup>2 = p * k\<^sup>2" by (simp add: power2_eq_square)
a1119cf551e8 standardized symbols via "isabelle update_sub_sup", excluding src/Pure and src/Tools/WWW_Find;
wenzelm
parents: 51708
diff changeset
    42
    then have "p dvd n\<^sup>2" ..
73810
1c5dcba6925f tuned proofs;
wenzelm
parents: 73809
diff changeset
    43
    with \<open>prime p\<close> show "p dvd n" by (rule prime_dvd_power)
13957
10dbf16be15f new session Complex for the complex numbers
paulson
parents:
diff changeset
    44
  qed
60690
a9e45c9588c3 tuned facts
haftmann
parents: 59031
diff changeset
    45
  then have "p dvd gcd m n" by simp
a9e45c9588c3 tuned facts
haftmann
parents: 59031
diff changeset
    46
  with \<open>coprime m n\<close> have "p = 1" by simp
13957
10dbf16be15f new session Complex for the complex numbers
paulson
parents:
diff changeset
    47
  with p show False by simp
10dbf16be15f new session Complex for the complex numbers
paulson
parents:
diff changeset
    48
qed
10dbf16be15f new session Complex for the complex numbers
paulson
parents:
diff changeset
    49
51708
5188a18c33b1 use automatic type coerctions in Sqrt example
hoelzl
parents: 46495
diff changeset
    50
corollary sqrt_2_not_rat: "sqrt 2 \<notin> \<rat>"
73810
1c5dcba6925f tuned proofs;
wenzelm
parents: 73809
diff changeset
    51
  using sqrt_prime_irrational [of 2] by simp
59031
4c3bb56b8ce7 misc tuning and modernization;
wenzelm
parents: 58889
diff changeset
    52
4c3bb56b8ce7 misc tuning and modernization;
wenzelm
parents: 58889
diff changeset
    53
text \<open>
73810
1c5dcba6925f tuned proofs;
wenzelm
parents: 73809
diff changeset
    54
  Here is an alternative version of the main proof, using mostly linear
1c5dcba6925f tuned proofs;
wenzelm
parents: 73809
diff changeset
    55
  forward-reasoning. While this results in less top-down structure, it is
1c5dcba6925f tuned proofs;
wenzelm
parents: 73809
diff changeset
    56
  probably closer to proofs seen in mathematics.
59031
4c3bb56b8ce7 misc tuning and modernization;
wenzelm
parents: 58889
diff changeset
    57
\<close>
13957
10dbf16be15f new session Complex for the complex numbers
paulson
parents:
diff changeset
    58
19086
1b3780be6cc2 new-style definitions/abbreviations;
wenzelm
parents: 16663
diff changeset
    59
theorem
73810
1c5dcba6925f tuned proofs;
wenzelm
parents: 73809
diff changeset
    60
  fixes p :: nat
1c5dcba6925f tuned proofs;
wenzelm
parents: 73809
diff changeset
    61
  assumes "prime p"
51708
5188a18c33b1 use automatic type coerctions in Sqrt example
hoelzl
parents: 46495
diff changeset
    62
  shows "sqrt p \<notin> \<rat>"
13957
10dbf16be15f new session Complex for the complex numbers
paulson
parents:
diff changeset
    63
proof
73810
1c5dcba6925f tuned proofs;
wenzelm
parents: 73809
diff changeset
    64
  from \<open>prime p\<close> have p: "p > 1" by (rule prime_gt_1_nat)
51708
5188a18c33b1 use automatic type coerctions in Sqrt example
hoelzl
parents: 46495
diff changeset
    65
  assume "sqrt p \<in> \<rat>"
73810
1c5dcba6925f tuned proofs;
wenzelm
parents: 73809
diff changeset
    66
  then obtain m n :: nat
1c5dcba6925f tuned proofs;
wenzelm
parents: 73809
diff changeset
    67
    where n: "n \<noteq> 0"
1c5dcba6925f tuned proofs;
wenzelm
parents: 73809
diff changeset
    68
      and sqrt_rat: "\<bar>sqrt p\<bar> = m / n"
1c5dcba6925f tuned proofs;
wenzelm
parents: 73809
diff changeset
    69
      and "coprime m n" by (rule Rats_abs_nat_div_natE)
51708
5188a18c33b1 use automatic type coerctions in Sqrt example
hoelzl
parents: 46495
diff changeset
    70
  from n and sqrt_rat have "m = \<bar>sqrt p\<bar> * n" by simp
73810
1c5dcba6925f tuned proofs;
wenzelm
parents: 73809
diff changeset
    71
  then have "m\<^sup>2 = (sqrt p)\<^sup>2 * n\<^sup>2" by (auto simp add: power2_eq_square)
53015
a1119cf551e8 standardized symbols via "isabelle update_sub_sup", excluding src/Pure and src/Tools/WWW_Find;
wenzelm
parents: 51708
diff changeset
    72
  also have "(sqrt p)\<^sup>2 = p" by simp
a1119cf551e8 standardized symbols via "isabelle update_sub_sup", excluding src/Pure and src/Tools/WWW_Find;
wenzelm
parents: 51708
diff changeset
    73
  also have "\<dots> * n\<^sup>2 = p * n\<^sup>2" by simp
73810
1c5dcba6925f tuned proofs;
wenzelm
parents: 73809
diff changeset
    74
  finally have eq: "m\<^sup>2 = p * n\<^sup>2" by linarith
53015
a1119cf551e8 standardized symbols via "isabelle update_sub_sup", excluding src/Pure and src/Tools/WWW_Find;
wenzelm
parents: 51708
diff changeset
    75
  then have "p dvd m\<^sup>2" ..
73810
1c5dcba6925f tuned proofs;
wenzelm
parents: 73809
diff changeset
    76
  with \<open>prime p\<close> have dvd_m: "p dvd m" by (rule prime_dvd_power)
13957
10dbf16be15f new session Complex for the complex numbers
paulson
parents:
diff changeset
    77
  then obtain k where "m = p * k" ..
73810
1c5dcba6925f tuned proofs;
wenzelm
parents: 73809
diff changeset
    78
  with eq have "p * n\<^sup>2 = p\<^sup>2 * k\<^sup>2" by algebra
53015
a1119cf551e8 standardized symbols via "isabelle update_sub_sup", excluding src/Pure and src/Tools/WWW_Find;
wenzelm
parents: 51708
diff changeset
    79
  with p have "n\<^sup>2 = p * k\<^sup>2" by (simp add: power2_eq_square)
a1119cf551e8 standardized symbols via "isabelle update_sub_sup", excluding src/Pure and src/Tools/WWW_Find;
wenzelm
parents: 51708
diff changeset
    80
  then have "p dvd n\<^sup>2" ..
73810
1c5dcba6925f tuned proofs;
wenzelm
parents: 73809
diff changeset
    81
  with \<open>prime p\<close> have "p dvd n" by (rule prime_dvd_power)
62348
9a5f43dac883 dropped various legacy fact bindings
haftmann
parents: 61762
diff changeset
    82
  with dvd_m have "p dvd gcd m n" by (rule gcd_greatest)
60690
a9e45c9588c3 tuned facts
haftmann
parents: 59031
diff changeset
    83
  with \<open>coprime m n\<close> have "p = 1" by simp
13957
10dbf16be15f new session Complex for the complex numbers
paulson
parents:
diff changeset
    84
  with p show False by simp
10dbf16be15f new session Complex for the complex numbers
paulson
parents:
diff changeset
    85
qed
10dbf16be15f new session Complex for the complex numbers
paulson
parents:
diff changeset
    86
45917
1ce1bc9ff64a added old chestnut
nipkow
parents: 32479
diff changeset
    87
73810
1c5dcba6925f tuned proofs;
wenzelm
parents: 73809
diff changeset
    88
text \<open>
1c5dcba6925f tuned proofs;
wenzelm
parents: 73809
diff changeset
    89
  Another old chestnut, which is a consequence of the irrationality of
1c5dcba6925f tuned proofs;
wenzelm
parents: 73809
diff changeset
    90
  \<^term>\<open>sqrt 2\<close>.
1c5dcba6925f tuned proofs;
wenzelm
parents: 73809
diff changeset
    91
\<close>
45917
1ce1bc9ff64a added old chestnut
nipkow
parents: 32479
diff changeset
    92
59031
4c3bb56b8ce7 misc tuning and modernization;
wenzelm
parents: 58889
diff changeset
    93
lemma "\<exists>a b::real. a \<notin> \<rat> \<and> b \<notin> \<rat> \<and> a powr b \<in> \<rat>" (is "\<exists>a b. ?P a b")
73809
ce9529a616fd misc tuning --- following hints by Jørgen Villadsen (see also 1ce1bc9ff64a);
wenzelm
parents: 66453
diff changeset
    94
proof (cases "sqrt 2 powr sqrt 2 \<in> \<rat>")
ce9529a616fd misc tuning --- following hints by Jørgen Villadsen (see also 1ce1bc9ff64a);
wenzelm
parents: 66453
diff changeset
    95
  case True
ce9529a616fd misc tuning --- following hints by Jørgen Villadsen (see also 1ce1bc9ff64a);
wenzelm
parents: 66453
diff changeset
    96
  with sqrt_2_not_rat have "?P (sqrt 2) (sqrt 2)" by simp
46495
8e8a339e176f uniform Isar source formatting for this file;
wenzelm
parents: 45917
diff changeset
    97
  then show ?thesis by blast
45917
1ce1bc9ff64a added old chestnut
nipkow
parents: 32479
diff changeset
    98
next
73809
ce9529a616fd misc tuning --- following hints by Jørgen Villadsen (see also 1ce1bc9ff64a);
wenzelm
parents: 66453
diff changeset
    99
  case False
ce9529a616fd misc tuning --- following hints by Jørgen Villadsen (see also 1ce1bc9ff64a);
wenzelm
parents: 66453
diff changeset
   100
  with sqrt_2_not_rat powr_powr have "?P (sqrt 2 powr sqrt 2) (sqrt 2)" by simp
46495
8e8a339e176f uniform Isar source formatting for this file;
wenzelm
parents: 45917
diff changeset
   101
  then show ?thesis by blast
45917
1ce1bc9ff64a added old chestnut
nipkow
parents: 32479
diff changeset
   102
qed
1ce1bc9ff64a added old chestnut
nipkow
parents: 32479
diff changeset
   103
13957
10dbf16be15f new session Complex for the complex numbers
paulson
parents:
diff changeset
   104
end