Attic/Pr.thy
author Chengsong
Mon, 10 Jul 2023 14:32:48 +0100
changeset 663 0d1e68268d0f
parent 95 a33d3040bf7e
permissions -rw-r--r--
more explanation for the name "closed form" and their intuition
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
46
79336e47e14d added Pr theory
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     1
theory Pr
79336e47e14d added Pr theory
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     2
imports Main "~~/src/HOL/Number_Theory/Primes" "~~/src/HOL/Real"
79336e47e14d added Pr theory
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     3
begin
79336e47e14d added Pr theory
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     4
81
7ac7782a7318 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 46
diff changeset
     5
fun 
7ac7782a7318 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 46
diff changeset
     6
  add :: "nat \<Rightarrow> nat \<Rightarrow> nat" 
7ac7782a7318 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 46
diff changeset
     7
where
7ac7782a7318 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 46
diff changeset
     8
  "add 0 n = n" |
7ac7782a7318 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 46
diff changeset
     9
  "add (Suc m) n = Suc (add m n)"
7ac7782a7318 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 46
diff changeset
    10
7ac7782a7318 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 46
diff changeset
    11
fun 
7ac7782a7318 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 46
diff changeset
    12
  doub :: "nat \<Rightarrow> nat" 
7ac7782a7318 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 46
diff changeset
    13
where
7ac7782a7318 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 46
diff changeset
    14
  "doub 0 = 0" |
7ac7782a7318 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 46
diff changeset
    15
  "doub n = n + n"
7ac7782a7318 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 46
diff changeset
    16
7ac7782a7318 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 46
diff changeset
    17
lemma add_lem:
7ac7782a7318 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 46
diff changeset
    18
  "add m n = m + n"
7ac7782a7318 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 46
diff changeset
    19
apply(induct m arbitrary: )
7ac7782a7318 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 46
diff changeset
    20
apply(auto)
7ac7782a7318 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 46
diff changeset
    21
done
7ac7782a7318 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 46
diff changeset
    22
7ac7782a7318 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 46
diff changeset
    23
fun 
7ac7782a7318 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 46
diff changeset
    24
  count :: "'a \<Rightarrow> 'a list \<Rightarrow> nat"
7ac7782a7318 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 46
diff changeset
    25
where
7ac7782a7318 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 46
diff changeset
    26
  "count n Nil = 0" |
7ac7782a7318 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 46
diff changeset
    27
  "count n (x # xs) = (if n = x then (add 1 (count n xs)) else count n xs)"
7ac7782a7318 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 46
diff changeset
    28
7ac7782a7318 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 46
diff changeset
    29
value "count 3 [1,2,3,3,4,3,5]"
7ac7782a7318 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 46
diff changeset
    30
7ac7782a7318 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 46
diff changeset
    31
fun 
7ac7782a7318 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 46
diff changeset
    32
  count2 :: "nat \<Rightarrow> nat list \<Rightarrow> nat" 
7ac7782a7318 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 46
diff changeset
    33
where
7ac7782a7318 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 46
diff changeset
    34
"count2 n Nil = 0" |
7ac7782a7318 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 46
diff changeset
    35
"count2 n (Cons x xs) = (if n = x then (add 1 (count2 n xs)) else count2 n xs)"
7ac7782a7318 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 46
diff changeset
    36
7ac7782a7318 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 46
diff changeset
    37
value "count2 (2::nat) [2,2,3::nat]"
7ac7782a7318 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 46
diff changeset
    38
7ac7782a7318 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 46
diff changeset
    39
lemma
7ac7782a7318 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 46
diff changeset
    40
  "count2 x xs \<le> length xs"
7ac7782a7318 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 46
diff changeset
    41
apply(induct xs)
7ac7782a7318 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 46
diff changeset
    42
apply(simp)
7ac7782a7318 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 46
diff changeset
    43
apply(simp)
7ac7782a7318 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 46
diff changeset
    44
apply(auto)
7ac7782a7318 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 46
diff changeset
    45
done
7ac7782a7318 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 46
diff changeset
    46
7ac7782a7318 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 46
diff changeset
    47
fun 
7ac7782a7318 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 46
diff changeset
    48
  sum :: "nat \<Rightarrow> nat"
7ac7782a7318 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 46
diff changeset
    49
where
7ac7782a7318 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 46
diff changeset
    50
  "sum 0 = 0"
7ac7782a7318 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 46
diff changeset
    51
| "sum (Suc n) = (Suc n) + sum n"
7ac7782a7318 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 46
diff changeset
    52
7ac7782a7318 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 46
diff changeset
    53
lemma
7ac7782a7318 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 46
diff changeset
    54
  "sum n = (n * (Suc n)) div 2"
7ac7782a7318 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 46
diff changeset
    55
apply(induct n)
7ac7782a7318 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 46
diff changeset
    56
apply(auto)
7ac7782a7318 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 46
diff changeset
    57
done
7ac7782a7318 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 46
diff changeset
    58
7ac7782a7318 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 46
diff changeset
    59
7ac7782a7318 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 46
diff changeset
    60
lemma
7ac7782a7318 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 46
diff changeset
    61
  "doub n = add n n"
7ac7782a7318 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 46
diff changeset
    62
apply(induct n)
7ac7782a7318 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 46
diff changeset
    63
apply(simp)
7ac7782a7318 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 46
diff changeset
    64
apply(simp add: add_lem)
7ac7782a7318 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 46
diff changeset
    65
done
7ac7782a7318 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 46
diff changeset
    66
46
79336e47e14d added Pr theory
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    67
lemma 
79336e47e14d added Pr theory
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    68
  fixes a b::nat
79336e47e14d added Pr theory
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    69
  shows "(a + b) ^ 2 = a ^ 2 + 2 * a * b + b ^ 2"
79336e47e14d added Pr theory
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    70
apply(simp add: power2_sum)
79336e47e14d added Pr theory
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    71
done
79336e47e14d added Pr theory
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    72
79336e47e14d added Pr theory
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    73
lemma
79336e47e14d added Pr theory
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    74
  fixes a b c::"real"
79336e47e14d added Pr theory
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    75
  assumes eq: "a * c \<le> b * c" and ineq: "b < a"
79336e47e14d added Pr theory
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    76
  shows "c \<le> 0"
79336e47e14d added Pr theory
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    77
proof -
79336e47e14d added Pr theory
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    78
  {
79336e47e14d added Pr theory
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    79
    assume  "0 < c" 
79336e47e14d added Pr theory
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    80
    then have "b * c < a * c" using ineq by(auto)
79336e47e14d added Pr theory
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    81
    then have "False" using eq by auto
79336e47e14d added Pr theory
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    82
  } then show "c \<le> 0" by (auto simp add: not_le[symmetric]) 
79336e47e14d added Pr theory
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    83
qed
79336e47e14d added Pr theory
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    84
    
79336e47e14d added Pr theory
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    85
79336e47e14d added Pr theory
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    86
79336e47e14d added Pr theory
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    87
79336e47e14d added Pr theory
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    88
lemma "n > 1 \<Longrightarrow> \<not>(prime (2 * n))"
79336e47e14d added Pr theory
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    89
by (metis One_nat_def Suc_leI less_Suc0 not_le numeral_eq_one_iff prime_product semiring_norm(85))
79336e47e14d added Pr theory
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    90
79336e47e14d added Pr theory
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    91
79336e47e14d added Pr theory
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    92
79336e47e14d added Pr theory
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    93
lemma 
79336e47e14d added Pr theory
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    94
 fixes n::"nat"
79336e47e14d added Pr theory
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    95
 assumes a: "n > 1"
79336e47e14d added Pr theory
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    96
    and  b: "\<not>(prime n)"
79336e47e14d added Pr theory
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    97
 shows "\<not>(prime ((2 ^ n) - 1))"   
79336e47e14d added Pr theory
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    98
using a b
79336e47e14d added Pr theory
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    99
apply(induct n)
79336e47e14d added Pr theory
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   100
apply(simp)
79336e47e14d added Pr theory
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   101
apply(simp)
79336e47e14d added Pr theory
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   102
79336e47e14d added Pr theory
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   103
79336e47e14d added Pr theory
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   104
79336e47e14d added Pr theory
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   105
end