src/HOL/Word/Misc_Numeric.thy
author haftmann
Wed, 25 Dec 2013 17:39:06 +0100
changeset 54863 82acc20ded73
parent 54847 d6cf9a5b9be9
child 54869 0046711700c8
permissions -rw-r--r--
prefer more canonical names for lemmas on min/max
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
     1
(* 
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
     2
  Author:  Jeremy Dawson, NICTA
24350
4d74f37c6367 headers for document generation
huffman
parents: 24333
diff changeset
     3
*) 
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
     4
24350
4d74f37c6367 headers for document generation
huffman
parents: 24333
diff changeset
     5
header {* Useful Numerical Lemmas *}
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
     6
37655
f4d616d41a59 more speaking theory names
haftmann
parents: 37591
diff changeset
     7
theory Misc_Numeric
51301
6822aa82aafa simplified imports;
wenzelm
parents: 49962
diff changeset
     8
imports Main Parity
25592
e8ddaf6bf5df explicit import of theory Main
haftmann
parents: 25349
diff changeset
     9
begin
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    10
24465
70f0214b3ecc revert to Word library version from 2007/08/20
huffman
parents: 24414
diff changeset
    11
declare iszero_0 [iff]
70f0214b3ecc revert to Word library version from 2007/08/20
huffman
parents: 24414
diff changeset
    12
53062
3af1a6020014 some vague grouping of related theorems, with slight tuning of headings and sorting out of dubious lemmas into separate theory
haftmann
parents: 51301
diff changeset
    13
lemma min_pm [simp]: "min a b + (a - b) = (a :: nat)" by arith
3af1a6020014 some vague grouping of related theorems, with slight tuning of headings and sorting out of dubious lemmas into separate theory
haftmann
parents: 51301
diff changeset
    14
  
3af1a6020014 some vague grouping of related theorems, with slight tuning of headings and sorting out of dubious lemmas into separate theory
haftmann
parents: 51301
diff changeset
    15
lemmas min_pm1 [simp] = trans [OF add_commute min_pm]
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    16
53062
3af1a6020014 some vague grouping of related theorems, with slight tuning of headings and sorting out of dubious lemmas into separate theory
haftmann
parents: 51301
diff changeset
    17
lemma rev_min_pm [simp]: "min b a + (a - b) = (a::nat)" by arith
24465
70f0214b3ecc revert to Word library version from 2007/08/20
huffman
parents: 24414
diff changeset
    18
53062
3af1a6020014 some vague grouping of related theorems, with slight tuning of headings and sorting out of dubious lemmas into separate theory
haftmann
parents: 51301
diff changeset
    19
lemmas rev_min_pm1 [simp] = trans [OF add_commute rev_min_pm]
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    20
53062
3af1a6020014 some vague grouping of related theorems, with slight tuning of headings and sorting out of dubious lemmas into separate theory
haftmann
parents: 51301
diff changeset
    21
lemma min_minus [simp] : "min m (m - k) = (m - k :: nat)" by arith
3af1a6020014 some vague grouping of related theorems, with slight tuning of headings and sorting out of dubious lemmas into separate theory
haftmann
parents: 51301
diff changeset
    22
  
54863
82acc20ded73 prefer more canonical names for lemmas on min/max
haftmann
parents: 54847
diff changeset
    23
lemmas min_minus' [simp] = trans [OF min.commute min_minus]
24465
70f0214b3ecc revert to Word library version from 2007/08/20
huffman
parents: 24414
diff changeset
    24
54847
d6cf9a5b9be9 prefer plain bool over dedicated type for binary digits
haftmann
parents: 54221
diff changeset
    25
lemma mod_2_neq_1_eq_eq_0:
d6cf9a5b9be9 prefer plain bool over dedicated type for binary digits
haftmann
parents: 54221
diff changeset
    26
  fixes k :: int
d6cf9a5b9be9 prefer plain bool over dedicated type for binary digits
haftmann
parents: 54221
diff changeset
    27
  shows "k mod 2 \<noteq> 1 \<longleftrightarrow> k mod 2 = 0"
d6cf9a5b9be9 prefer plain bool over dedicated type for binary digits
haftmann
parents: 54221
diff changeset
    28
  by arith
d6cf9a5b9be9 prefer plain bool over dedicated type for binary digits
haftmann
parents: 54221
diff changeset
    29
53062
3af1a6020014 some vague grouping of related theorems, with slight tuning of headings and sorting out of dubious lemmas into separate theory
haftmann
parents: 51301
diff changeset
    30
lemma z1pmod2:
3af1a6020014 some vague grouping of related theorems, with slight tuning of headings and sorting out of dubious lemmas into separate theory
haftmann
parents: 51301
diff changeset
    31
  "(2 * b + 1) mod 2 = (1::int)" by arith
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    32
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    33
lemma emep1:
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    34
  "even n ==> even d ==> 0 <= d ==> (n + 1) mod (d :: int) = (n mod d) + 1"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    35
  apply (simp add: add_commute)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    36
  apply (safe dest!: even_equiv_def [THEN iffD1])
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    37
  apply (subst pos_zmod_mult_2)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    38
   apply arith
30943
eb3dbbe971f6 zmod_zmult_zmult1 now subsumed by mod_mult_mult1
haftmann
parents: 30445
diff changeset
    39
  apply (simp add: mod_mult_mult1)
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    40
 done
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    41
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    42
lemma int_mod_lem: 
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    43
  "(0 :: int) < n ==> (0 <= b & b < n) = (b mod n = b)"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    44
  apply safe
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    45
    apply (erule (1) mod_pos_pos_trivial)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    46
   apply (erule_tac [!] subst)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    47
   apply auto
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    48
  done
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    49
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    50
lemma int_mod_ge: "a < n ==> 0 < (n :: int) ==> a <= a mod n"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    51
  apply (cases "0 <= a")
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    52
   apply (drule (1) mod_pos_pos_trivial)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    53
   apply simp
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    54
  apply (rule order_trans [OF _ pos_mod_sign])
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    55
   apply simp
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    56
  apply assumption
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    57
  done
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    58
25349
0d46bea01741 eliminated illegal schematic variables in where/of;
wenzelm
parents: 24465
diff changeset
    59
lemma int_mod_ge': "b < 0 ==> 0 < (n :: int) ==> b + n <= b mod n"
0d46bea01741 eliminated illegal schematic variables in where/of;
wenzelm
parents: 24465
diff changeset
    60
  by (rule int_mod_ge [where a = "b + n" and n = n, simplified])
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    61
53062
3af1a6020014 some vague grouping of related theorems, with slight tuning of headings and sorting out of dubious lemmas into separate theory
haftmann
parents: 51301
diff changeset
    62
lemma zless2:
3af1a6020014 some vague grouping of related theorems, with slight tuning of headings and sorting out of dubious lemmas into separate theory
haftmann
parents: 51301
diff changeset
    63
  "0 < (2 :: int)"
3af1a6020014 some vague grouping of related theorems, with slight tuning of headings and sorting out of dubious lemmas into separate theory
haftmann
parents: 51301
diff changeset
    64
  by arith
24465
70f0214b3ecc revert to Word library version from 2007/08/20
huffman
parents: 24414
diff changeset
    65
53062
3af1a6020014 some vague grouping of related theorems, with slight tuning of headings and sorting out of dubious lemmas into separate theory
haftmann
parents: 51301
diff changeset
    66
lemma zless2p:
3af1a6020014 some vague grouping of related theorems, with slight tuning of headings and sorting out of dubious lemmas into separate theory
haftmann
parents: 51301
diff changeset
    67
  "0 < (2 ^ n :: int)"
3af1a6020014 some vague grouping of related theorems, with slight tuning of headings and sorting out of dubious lemmas into separate theory
haftmann
parents: 51301
diff changeset
    68
  by arith
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    69
53062
3af1a6020014 some vague grouping of related theorems, with slight tuning of headings and sorting out of dubious lemmas into separate theory
haftmann
parents: 51301
diff changeset
    70
lemma zle2p:
3af1a6020014 some vague grouping of related theorems, with slight tuning of headings and sorting out of dubious lemmas into separate theory
haftmann
parents: 51301
diff changeset
    71
  "0 \<le> (2 ^ n :: int)"
3af1a6020014 some vague grouping of related theorems, with slight tuning of headings and sorting out of dubious lemmas into separate theory
haftmann
parents: 51301
diff changeset
    72
  by arith
24465
70f0214b3ecc revert to Word library version from 2007/08/20
huffman
parents: 24414
diff changeset
    73
53062
3af1a6020014 some vague grouping of related theorems, with slight tuning of headings and sorting out of dubious lemmas into separate theory
haftmann
parents: 51301
diff changeset
    74
lemma int_mod_le': "(0::int) <= b - n ==> b mod n <= b - n"
3af1a6020014 some vague grouping of related theorems, with slight tuning of headings and sorting out of dubious lemmas into separate theory
haftmann
parents: 51301
diff changeset
    75
  using zmod_le_nonneg_dividend [of "b - n" "n"] by simp
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    76
53062
3af1a6020014 some vague grouping of related theorems, with slight tuning of headings and sorting out of dubious lemmas into separate theory
haftmann
parents: 51301
diff changeset
    77
lemma diff_le_eq': "(a - b \<le> c) = (a \<le> b + (c::int))" by arith
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    78
53062
3af1a6020014 some vague grouping of related theorems, with slight tuning of headings and sorting out of dubious lemmas into separate theory
haftmann
parents: 51301
diff changeset
    79
lemma m1mod2k:
3af1a6020014 some vague grouping of related theorems, with slight tuning of headings and sorting out of dubious lemmas into separate theory
haftmann
parents: 51301
diff changeset
    80
  "-1 mod 2 ^ n = (2 ^ n - 1 :: int)"
3af1a6020014 some vague grouping of related theorems, with slight tuning of headings and sorting out of dubious lemmas into separate theory
haftmann
parents: 51301
diff changeset
    81
  using zless2p by (rule zmod_minus1)
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    82
53062
3af1a6020014 some vague grouping of related theorems, with slight tuning of headings and sorting out of dubious lemmas into separate theory
haftmann
parents: 51301
diff changeset
    83
lemma p1mod22k':
3af1a6020014 some vague grouping of related theorems, with slight tuning of headings and sorting out of dubious lemmas into separate theory
haftmann
parents: 51301
diff changeset
    84
  fixes b :: int
3af1a6020014 some vague grouping of related theorems, with slight tuning of headings and sorting out of dubious lemmas into separate theory
haftmann
parents: 51301
diff changeset
    85
  shows "(1 + 2 * b) mod (2 * 2 ^ n) = 1 + 2 * (b mod 2 ^ n)"
3af1a6020014 some vague grouping of related theorems, with slight tuning of headings and sorting out of dubious lemmas into separate theory
haftmann
parents: 51301
diff changeset
    86
  using zle2p by (rule pos_zmod_mult_2) 
24465
70f0214b3ecc revert to Word library version from 2007/08/20
huffman
parents: 24414
diff changeset
    87
53062
3af1a6020014 some vague grouping of related theorems, with slight tuning of headings and sorting out of dubious lemmas into separate theory
haftmann
parents: 51301
diff changeset
    88
lemma p1mod22k:
3af1a6020014 some vague grouping of related theorems, with slight tuning of headings and sorting out of dubious lemmas into separate theory
haftmann
parents: 51301
diff changeset
    89
  fixes b :: int
3af1a6020014 some vague grouping of related theorems, with slight tuning of headings and sorting out of dubious lemmas into separate theory
haftmann
parents: 51301
diff changeset
    90
  shows "(2 * b + 1) mod (2 * 2 ^ n) = 2 * (b mod 2 ^ n) + 1"
3af1a6020014 some vague grouping of related theorems, with slight tuning of headings and sorting out of dubious lemmas into separate theory
haftmann
parents: 51301
diff changeset
    91
  by (simp add: p1mod22k' add_commute)
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    92
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    93
lemma lrlem':
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    94
  assumes d: "(i::nat) \<le> j \<or> m < j'"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    95
  assumes R1: "i * k \<le> j * k \<Longrightarrow> R"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    96
  assumes R2: "Suc m * k' \<le> j' * k' \<Longrightarrow> R"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    97
  shows "R" using d
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    98
  apply safe
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    99
   apply (rule R1, erule mult_le_mono1)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   100
  apply (rule R2, erule Suc_le_eq [THEN iffD2 [THEN mult_le_mono1]])
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   101
  done
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   102
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   103
lemma lrlem: "(0::nat) < sc ==>
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   104
    (sc - n + (n + lb * n) <= m * n) = (sc + lb * n <= m * n)"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   105
  apply safe
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   106
   apply arith
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   107
  apply (case_tac "sc >= n")
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   108
   apply arith
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   109
  apply (insert linorder_le_less_linear [of m lb])
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   110
  apply (erule_tac k=n and k'=n in lrlem')
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   111
   apply arith
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   112
  apply simp
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   113
  done
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   114
53062
3af1a6020014 some vague grouping of related theorems, with slight tuning of headings and sorting out of dubious lemmas into separate theory
haftmann
parents: 51301
diff changeset
   115
end
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   116