src/HOL/Word/Misc_Numeric.thy
author wenzelm
Tue, 03 Sep 2013 01:12:40 +0200
changeset 53374 a14d2a854c02
parent 53062 3af1a6020014
child 54221 56587960e444
permissions -rw-r--r--
tuned proofs -- clarified flow of facts wrt. calculation;
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
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
    11
lemma zmod_zsub_self [simp]: (* FIXME move to Divides.thy *) 
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
    12
  "((b :: int) - a) mod a = b mod a"
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
  by (simp add: mod_diff_right_eq)
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    14
24465
70f0214b3ecc revert to Word library version from 2007/08/20
huffman
parents: 24414
diff changeset
    15
declare iszero_0 [iff]
70f0214b3ecc revert to Word library version from 2007/08/20
huffman
parents: 24414
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 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
    18
  
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 min_pm1 [simp] = trans [OF add_commute 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 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
    22
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
    23
lemmas rev_min_pm1 [simp] = trans [OF add_commute rev_min_pm]
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    24
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
    25
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
    26
  
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
    27
lemmas min_minus' [simp] = trans [OF min_max.inf_commute min_minus]
24465
70f0214b3ecc revert to Word library version from 2007/08/20
huffman
parents: 24414
diff changeset
    28
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
    29
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
    30
  "(2 * b + 1) mod 2 = (1::int)" by arith
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    31
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    32
lemma emep1:
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    33
  "even n ==> even d ==> 0 <= d ==> (n + 1) mod (d :: int) = (n mod d) + 1"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    34
  apply (simp add: add_commute)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    35
  apply (safe dest!: even_equiv_def [THEN iffD1])
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    36
  apply (subst pos_zmod_mult_2)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    37
   apply arith
30943
eb3dbbe971f6 zmod_zmult_zmult1 now subsumed by mod_mult_mult1
haftmann
parents: 30445
diff changeset
    38
  apply (simp add: mod_mult_mult1)
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    39
 done
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    40
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    41
lemma int_mod_lem: 
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    42
  "(0 :: int) < n ==> (0 <= b & b < n) = (b mod n = b)"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    43
  apply safe
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    44
    apply (erule (1) mod_pos_pos_trivial)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    45
   apply (erule_tac [!] subst)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    46
   apply auto
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    47
  done
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    48
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    49
lemma int_mod_ge: "a < n ==> 0 < (n :: int) ==> a <= a mod n"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    50
  apply (cases "0 <= a")
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    51
   apply (drule (1) mod_pos_pos_trivial)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    52
   apply simp
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    53
  apply (rule order_trans [OF _ pos_mod_sign])
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    54
   apply simp
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    55
  apply assumption
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    56
  done
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    57
25349
0d46bea01741 eliminated illegal schematic variables in where/of;
wenzelm
parents: 24465
diff changeset
    58
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
    59
  by (rule int_mod_ge [where a = "b + n" and n = n, simplified])
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    60
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
    61
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
    62
  "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
    63
  by arith
24465
70f0214b3ecc revert to Word library version from 2007/08/20
huffman
parents: 24414
diff changeset
    64
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
    65
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
    66
  "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
    67
  by arith
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    68
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
    69
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
    70
  "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
    71
  by arith
24465
70f0214b3ecc revert to Word library version from 2007/08/20
huffman
parents: 24414
diff changeset
    72
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
    73
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
    74
  using zmod_le_nonneg_dividend [of "b - n" "n"] by simp
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    75
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
    76
lemma diff_le_eq': "(a - b \<le> c) = (a \<le> b + (c::int))" by arith
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    77
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
    78
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
    79
  "-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
    80
  using zless2p by (rule zmod_minus1)
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    81
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
    82
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
    83
  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
    84
  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
    85
  using zle2p by (rule pos_zmod_mult_2) 
24465
70f0214b3ecc revert to Word library version from 2007/08/20
huffman
parents: 24414
diff changeset
    86
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
    87
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
    88
  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
    89
  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
    90
  by (simp add: p1mod22k' add_commute)
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    91
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    92
lemma lrlem':
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    93
  assumes d: "(i::nat) \<le> j \<or> m < j'"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    94
  assumes R1: "i * k \<le> j * k \<Longrightarrow> R"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    95
  assumes R2: "Suc m * k' \<le> j' * k' \<Longrightarrow> R"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    96
  shows "R" using d
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    97
  apply safe
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    98
   apply (rule R1, erule mult_le_mono1)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    99
  apply (rule R2, erule Suc_le_eq [THEN iffD2 [THEN mult_le_mono1]])
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   100
  done
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   101
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   102
lemma lrlem: "(0::nat) < sc ==>
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   103
    (sc - n + (n + lb * n) <= m * n) = (sc + lb * n <= m * n)"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   104
  apply safe
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   105
   apply arith
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   106
  apply (case_tac "sc >= n")
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   107
   apply arith
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   108
  apply (insert linorder_le_less_linear [of m lb])
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   109
  apply (erule_tac k=n and k'=n in lrlem')
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   110
   apply arith
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   111
  apply simp
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   112
  done
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   113
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
   114
end
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   115