src/HOL/ex/Code_Nat_examples.thy
author huffman
Mon, 02 Apr 2012 16:06:24 +0200
changeset 47299 e705ef5ffe95
parent 47108 2a1953f0d20d
permissions -rw-r--r--
add lemma Suc_1
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents:
diff changeset
     1
(*  Title:      HOL/ex/Code_Nat_examples.thy
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents:
diff changeset
     2
    Author:     Florian Haftmann, TU Muenchen
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents:
diff changeset
     3
*)
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents:
diff changeset
     4
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents:
diff changeset
     5
header {* Simple examples for Code\_Numeral\_Nat theory. *}
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents:
diff changeset
     6
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents:
diff changeset
     7
theory Code_Nat_examples
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents:
diff changeset
     8
imports Complex_Main "~~/src/HOL/Library/Efficient_Nat"
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents:
diff changeset
     9
begin
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents:
diff changeset
    10
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents:
diff changeset
    11
fun to_n :: "nat \<Rightarrow> nat list"
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents:
diff changeset
    12
where
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents:
diff changeset
    13
  "to_n 0 = []"
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents:
diff changeset
    14
| "to_n (Suc 0) = []"
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents:
diff changeset
    15
| "to_n (Suc (Suc 0)) = []"
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents:
diff changeset
    16
| "to_n (Suc n) = n # to_n n"
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents:
diff changeset
    17
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents:
diff changeset
    18
definition naive_prime :: "nat \<Rightarrow> bool"
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents:
diff changeset
    19
where
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents:
diff changeset
    20
  "naive_prime n \<longleftrightarrow> n \<ge> 2 \<and> filter (\<lambda>m. n mod m = 0) (to_n n) = []"
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents:
diff changeset
    21
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents:
diff changeset
    22
primrec fac :: "nat \<Rightarrow> nat"
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents:
diff changeset
    23
where
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents:
diff changeset
    24
  "fac 0 = 1"
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents:
diff changeset
    25
| "fac (Suc n) = Suc n * fac n"
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents:
diff changeset
    26
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents:
diff changeset
    27
primrec harmonic :: "nat \<Rightarrow> rat"
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents:
diff changeset
    28
where
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents:
diff changeset
    29
  "harmonic 0 = 0"
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents:
diff changeset
    30
| "harmonic (Suc n) = 1 / of_nat (Suc n) + harmonic n"
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents:
diff changeset
    31
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents:
diff changeset
    32
lemma "harmonic 200 \<ge> 5"
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents:
diff changeset
    33
  by eval
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents:
diff changeset
    34
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents:
diff changeset
    35
lemma "(let (q, r) = quotient_of (harmonic 8) in q div r) \<ge> 2"
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents:
diff changeset
    36
  by normalization
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents:
diff changeset
    37
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents:
diff changeset
    38
lemma "naive_prime 89"
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents:
diff changeset
    39
  by eval
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents:
diff changeset
    40
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents:
diff changeset
    41
lemma "naive_prime 89"
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents:
diff changeset
    42
  by normalization
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents:
diff changeset
    43
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents:
diff changeset
    44
lemma "\<not> naive_prime 87"
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents:
diff changeset
    45
  by eval
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents:
diff changeset
    46
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents:
diff changeset
    47
lemma "\<not> naive_prime 87"
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents:
diff changeset
    48
  by normalization
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents:
diff changeset
    49
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents:
diff changeset
    50
lemma "fac 10 > 3000000"
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents:
diff changeset
    51
  by eval
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents:
diff changeset
    52
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents:
diff changeset
    53
lemma "fac 10 > 3000000"
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents:
diff changeset
    54
  by normalization
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents:
diff changeset
    55
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents:
diff changeset
    56
end
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents:
diff changeset
    57