src/HOL/Word/Misc_Numeric.thy
author haftmann
Mon Jul 19 16:09:44 2010 +0200 (2010-07-19)
changeset 37887 2ae085b07f2f
parent 37655 f4d616d41a59
child 39910 10097e0a9dbd
permissions -rw-r--r--
diff_minus subsumes diff_def
kleing@24333
     1
(* 
kleing@24333
     2
  Author:  Jeremy Dawson, NICTA
huffman@24350
     3
*) 
kleing@24333
     4
huffman@24350
     5
header {* Useful Numerical Lemmas *}
kleing@24333
     6
haftmann@37655
     7
theory Misc_Numeric
haftmann@25592
     8
imports Main Parity
haftmann@25592
     9
begin
kleing@24333
    10
haftmann@26560
    11
lemma contentsI: "y = {x} ==> contents y = x" 
haftmann@26560
    12
  unfolding contents_def by auto -- {* FIXME move *}
huffman@26086
    13
haftmann@37591
    14
lemmas split_split = prod.split
haftmann@37591
    15
lemmas split_split_asm = prod.split_asm
wenzelm@30445
    16
lemmas split_splits = split_split split_split_asm
huffman@24465
    17
huffman@24465
    18
lemmas funpow_0 = funpow.simps(1)
kleing@24333
    19
lemmas funpow_Suc = funpow.simps(2)
huffman@24465
    20
chaieb@27570
    21
lemma nonemptyE: "S ~= {} ==> (!!x. x : S ==> R) ==> R" by auto
kleing@24333
    22
chaieb@27570
    23
lemma gt_or_eq_0: "0 < y \<or> 0 = (y::nat)" by arith 
kleing@24333
    24
huffman@24465
    25
declare iszero_0 [iff]
huffman@24465
    26
kleing@24333
    27
lemmas xtr1 = xtrans(1)
kleing@24333
    28
lemmas xtr2 = xtrans(2)
kleing@24333
    29
lemmas xtr3 = xtrans(3)
kleing@24333
    30
lemmas xtr4 = xtrans(4)
kleing@24333
    31
lemmas xtr5 = xtrans(5)
kleing@24333
    32
lemmas xtr6 = xtrans(6)
kleing@24333
    33
lemmas xtr7 = xtrans(7)
kleing@24333
    34
lemmas xtr8 = xtrans(8)
kleing@24333
    35
huffman@24465
    36
lemmas nat_simps = diff_add_inverse2 diff_add_inverse
huffman@24465
    37
lemmas nat_iffs = le_add1 le_add2
huffman@24465
    38
chaieb@27570
    39
lemma sum_imp_diff: "j = k + i ==> j - i = (k :: nat)" by arith
huffman@24465
    40
kleing@24333
    41
lemma nobm1:
kleing@24333
    42
  "0 < (number_of w :: nat) ==> 
chaieb@27570
    43
   number_of w - (1 :: nat) = number_of (Int.pred w)" 
kleing@24333
    44
  apply (unfold nat_number_of_def One_nat_def nat_1 [symmetric] pred_def)
kleing@24333
    45
  apply (simp add: number_of_eq nat_diff_distrib [symmetric])
kleing@24333
    46
  done
huffman@24465
    47
chaieb@27570
    48
lemma zless2: "0 < (2 :: int)" by arith
kleing@24333
    49
huffman@24465
    50
lemmas zless2p [simp] = zless2 [THEN zero_less_power]
huffman@24465
    51
lemmas zle2p [simp] = zless2p [THEN order_less_imp_le]
huffman@24465
    52
huffman@24465
    53
lemmas pos_mod_sign2 = zless2 [THEN pos_mod_sign [where b = "2::int"]]
huffman@24465
    54
lemmas pos_mod_bound2 = zless2 [THEN pos_mod_bound [where b = "2::int"]]
huffman@24465
    55
huffman@24465
    56
-- "the inverse(s) of @{text number_of}"
chaieb@27570
    57
lemma nmod2: "n mod (2::int) = 0 | n mod 2 = 1" by arith
kleing@24333
    58
kleing@24333
    59
lemma emep1:
kleing@24333
    60
  "even n ==> even d ==> 0 <= d ==> (n + 1) mod (d :: int) = (n mod d) + 1"
kleing@24333
    61
  apply (simp add: add_commute)
kleing@24333
    62
  apply (safe dest!: even_equiv_def [THEN iffD1])
kleing@24333
    63
  apply (subst pos_zmod_mult_2)
kleing@24333
    64
   apply arith
haftmann@30943
    65
  apply (simp add: mod_mult_mult1)
kleing@24333
    66
 done
kleing@24333
    67
kleing@24333
    68
lemmas eme1p = emep1 [simplified add_commute]
kleing@24333
    69
chaieb@27570
    70
lemma le_diff_eq': "(a \<le> c - b) = (b + a \<le> (c::int))" by arith
huffman@24465
    71
chaieb@27570
    72
lemma less_diff_eq': "(a < c - b) = (b + a < (c::int))" by arith
huffman@24465
    73
chaieb@27570
    74
lemma diff_le_eq': "(a - b \<le> c) = (a \<le> b + (c::int))" by arith
huffman@24465
    75
chaieb@27570
    76
lemma diff_less_eq': "(a - b < c) = (a < b + (c::int))" by arith
kleing@24333
    77
kleing@24333
    78
lemmas m1mod2k = zless2p [THEN zmod_minus1]
huffman@24465
    79
lemmas m1mod22k = mult_pos_pos [OF zless2 zless2p, THEN zmod_minus1]
kleing@24333
    80
lemmas p1mod22k' = zless2p [THEN order_less_imp_le, THEN pos_zmod_mult_2]
huffman@24465
    81
lemmas z1pmod2' = zero_le_one [THEN pos_zmod_mult_2, simplified]
huffman@24465
    82
lemmas z1pdiv2' = zero_le_one [THEN pos_zdiv_mult_2, simplified]
kleing@24333
    83
kleing@24333
    84
lemma p1mod22k:
kleing@24333
    85
  "(2 * b + 1) mod (2 * 2 ^ n) = 2 * (b mod 2 ^ n) + (1::int)"
kleing@24333
    86
  by (simp add: p1mod22k' add_commute)
huffman@24465
    87
huffman@24465
    88
lemma z1pmod2:
chaieb@27570
    89
  "(2 * b + 1) mod 2 = (1::int)" by arith
huffman@24465
    90
  
huffman@24465
    91
lemma z1pdiv2:
chaieb@27570
    92
  "(2 * b + 1) div 2 = (b::int)" by arith
kleing@24333
    93
nipkow@30031
    94
lemmas zdiv_le_dividend = xtr3 [OF div_by_1 [symmetric] zdiv_mono2,
kleing@24333
    95
  simplified int_one_le_iff_zero_less, simplified, standard]
huffman@24465
    96
  
huffman@24465
    97
lemma axxbyy:
huffman@24465
    98
  "a + m + m = b + n + n ==> (a = 0 | a = 1) ==> (b = 0 | b = 1) ==>  
chaieb@27570
    99
   a = b & m = (n :: int)" by arith
huffman@24465
   100
huffman@24465
   101
lemma axxmod2:
chaieb@27570
   102
  "(1 + x + x) mod 2 = (1 :: int) & (0 + x + x) mod 2 = (0 :: int)" by arith
huffman@24465
   103
huffman@24465
   104
lemma axxdiv2:
chaieb@27570
   105
  "(1 + x + x) div 2 = (x :: int) & (0 + x + x) div 2 = (x :: int)"  by arith
huffman@24465
   106
huffman@24465
   107
lemmas iszero_minus = trans [THEN trans,
huffman@24465
   108
  OF iszero_def neg_equal_0_iff_equal iszero_def [symmetric], standard]
kleing@24333
   109
kleing@24333
   110
lemmas zadd_diff_inverse = trans [OF diff_add_cancel [symmetric] add_commute,
kleing@24333
   111
  standard]
kleing@24333
   112
kleing@24333
   113
lemmas add_diff_cancel2 = add_commute [THEN diff_eq_eq [THEN iffD2], standard]
kleing@24333
   114
kleing@24333
   115
lemma zmod_uminus: "- ((a :: int) mod b) mod b = -a mod b"
kleing@24333
   116
  by (simp add : zmod_zminus1_eq_if)
huffman@24465
   117
huffman@24465
   118
lemma zmod_zsub_distrib: "((a::int) - b) mod c = (a mod c - b mod c) mod c"
huffman@24465
   119
  apply (unfold diff_int_def)
nipkow@29948
   120
  apply (rule trans [OF _ mod_add_eq [symmetric]])
nipkow@29948
   121
  apply (simp add: zmod_uminus mod_add_eq [symmetric])
huffman@24465
   122
  done
kleing@24333
   123
kleing@24333
   124
lemma zmod_zsub_right_eq: "((a::int) - b) mod c = (a - b mod c) mod c"
kleing@24333
   125
  apply (unfold diff_int_def)
nipkow@30034
   126
  apply (rule trans [OF _ mod_add_right_eq [symmetric]])
nipkow@30034
   127
  apply (simp add : zmod_uminus mod_add_right_eq [symmetric])
kleing@24333
   128
  done
kleing@24333
   129
wenzelm@25349
   130
lemma zmod_zsub_left_eq: "((a::int) - b) mod c = (a mod c - b) mod c"
nipkow@30034
   131
  by (rule mod_add_left_eq [where b = "- b", simplified diff_int_def [symmetric]])
wenzelm@25349
   132
kleing@24333
   133
lemma zmod_zsub_self [simp]: 
kleing@24333
   134
  "((b :: int) - a) mod a = b mod a"
kleing@24333
   135
  by (simp add: zmod_zsub_right_eq)
kleing@24333
   136
kleing@24333
   137
lemma zmod_zmult1_eq_rev:
kleing@24333
   138
  "b * a mod c = b mod c * a mod (c::int)"
kleing@24333
   139
  apply (simp add: mult_commute)
kleing@24333
   140
  apply (subst zmod_zmult1_eq)
kleing@24333
   141
  apply simp
kleing@24333
   142
  done
kleing@24333
   143
kleing@24333
   144
lemmas rdmods [symmetric] = zmod_uminus [symmetric]
nipkow@30034
   145
  zmod_zsub_left_eq zmod_zsub_right_eq mod_add_left_eq
nipkow@30034
   146
  mod_add_right_eq zmod_zmult1_eq zmod_zmult1_eq_rev
kleing@24333
   147
kleing@24333
   148
lemma mod_plus_right:
kleing@24333
   149
  "((a + x) mod m = (b + x) mod m) = (a mod m = b mod (m :: nat))"
kleing@24333
   150
  apply (induct x)
kleing@24333
   151
   apply (simp_all add: mod_Suc)
kleing@24333
   152
  apply arith
kleing@24333
   153
  done
kleing@24333
   154
huffman@24465
   155
lemma nat_minus_mod: "(n - n mod m) mod m = (0 :: nat)"
huffman@24465
   156
  by (induct n) (simp_all add : mod_Suc)
huffman@24465
   157
huffman@24465
   158
lemmas nat_minus_mod_plus_right = trans [OF nat_minus_mod mod_0 [symmetric],
huffman@24465
   159
  THEN mod_plus_right [THEN iffD2], standard, simplified]
huffman@24465
   160
nipkow@29948
   161
lemmas push_mods' = mod_add_eq [standard]
nipkow@29948
   162
  mod_mult_eq [standard] zmod_zsub_distrib [standard]
huffman@24465
   163
  zmod_uminus [symmetric, standard]
huffman@24465
   164
huffman@24465
   165
lemmas push_mods = push_mods' [THEN eq_reflection, standard]
huffman@24465
   166
lemmas pull_mods = push_mods [symmetric] rdmods [THEN eq_reflection, standard]
huffman@24465
   167
lemmas mod_simps = 
nipkow@30034
   168
  mod_mult_self2_is_0 [THEN eq_reflection]
nipkow@30034
   169
  mod_mult_self1_is_0 [THEN eq_reflection]
huffman@24465
   170
  mod_mod_trivial [THEN eq_reflection]
huffman@24465
   171
kleing@24333
   172
lemma nat_mod_eq:
kleing@24333
   173
  "!!b. b < n ==> a mod n = b mod n ==> a mod n = (b :: nat)" 
kleing@24333
   174
  by (induct a) auto
kleing@24333
   175
kleing@24333
   176
lemmas nat_mod_eq' = refl [THEN [2] nat_mod_eq]
kleing@24333
   177
kleing@24333
   178
lemma nat_mod_lem: 
kleing@24333
   179
  "(0 :: nat) < n ==> b < n = (b mod n = b)"
kleing@24333
   180
  apply safe
kleing@24333
   181
   apply (erule nat_mod_eq')
kleing@24333
   182
  apply (erule subst)
kleing@24333
   183
  apply (erule mod_less_divisor)
kleing@24333
   184
  done
kleing@24333
   185
kleing@24333
   186
lemma mod_nat_add: 
kleing@24333
   187
  "(x :: nat) < z ==> y < z ==> 
kleing@24333
   188
   (x + y) mod z = (if x + y < z then x + y else x + y - z)"
kleing@24333
   189
  apply (rule nat_mod_eq)
kleing@24333
   190
   apply auto
kleing@24333
   191
  apply (rule trans)
kleing@24333
   192
   apply (rule le_mod_geq)
kleing@24333
   193
   apply simp
kleing@24333
   194
  apply (rule nat_mod_eq')
kleing@24333
   195
  apply arith
kleing@24333
   196
  done
huffman@24465
   197
huffman@24465
   198
lemma mod_nat_sub: 
huffman@24465
   199
  "(x :: nat) < z ==> (x - y) mod z = x - y"
huffman@24465
   200
  by (rule nat_mod_eq') arith
kleing@24333
   201
kleing@24333
   202
lemma int_mod_lem: 
kleing@24333
   203
  "(0 :: int) < n ==> (0 <= b & b < n) = (b mod n = b)"
kleing@24333
   204
  apply safe
kleing@24333
   205
    apply (erule (1) mod_pos_pos_trivial)
kleing@24333
   206
   apply (erule_tac [!] subst)
kleing@24333
   207
   apply auto
kleing@24333
   208
  done
kleing@24333
   209
kleing@24333
   210
lemma int_mod_eq:
kleing@24333
   211
  "(0 :: int) <= b ==> b < n ==> a mod n = b mod n ==> a mod n = b"
kleing@24333
   212
  by clarsimp (rule mod_pos_pos_trivial)
kleing@24333
   213
kleing@24333
   214
lemmas int_mod_eq' = refl [THEN [3] int_mod_eq]
kleing@24333
   215
kleing@24333
   216
lemma int_mod_le: "0 <= a ==> 0 < (n :: int) ==> a mod n <= a"
kleing@24333
   217
  apply (cases "a < n")
kleing@24333
   218
   apply (auto dest: mod_pos_pos_trivial pos_mod_bound [where a=a])
kleing@24333
   219
  done
kleing@24333
   220
wenzelm@25349
   221
lemma int_mod_le': "0 <= b - n ==> 0 < (n :: int) ==> b mod n <= b - n"
wenzelm@25349
   222
  by (rule int_mod_le [where a = "b - n" and n = n, simplified])
kleing@24333
   223
kleing@24333
   224
lemma int_mod_ge: "a < n ==> 0 < (n :: int) ==> a <= a mod n"
kleing@24333
   225
  apply (cases "0 <= a")
kleing@24333
   226
   apply (drule (1) mod_pos_pos_trivial)
kleing@24333
   227
   apply simp
kleing@24333
   228
  apply (rule order_trans [OF _ pos_mod_sign])
kleing@24333
   229
   apply simp
kleing@24333
   230
  apply assumption
kleing@24333
   231
  done
kleing@24333
   232
wenzelm@25349
   233
lemma int_mod_ge': "b < 0 ==> 0 < (n :: int) ==> b + n <= b mod n"
wenzelm@25349
   234
  by (rule int_mod_ge [where a = "b + n" and n = n, simplified])
kleing@24333
   235
kleing@24333
   236
lemma mod_add_if_z:
kleing@24333
   237
  "(x :: int) < z ==> y < z ==> 0 <= y ==> 0 <= x ==> 0 <= z ==> 
kleing@24333
   238
   (x + y) mod z = (if x + y < z then x + y else x + y - z)"
kleing@24333
   239
  by (auto intro: int_mod_eq)
kleing@24333
   240
kleing@24333
   241
lemma mod_sub_if_z:
kleing@24333
   242
  "(x :: int) < z ==> y < z ==> 0 <= y ==> 0 <= x ==> 0 <= z ==> 
kleing@24333
   243
   (x - y) mod z = (if y <= x then x - y else x - y + z)"
kleing@24333
   244
  by (auto intro: int_mod_eq)
huffman@24465
   245
huffman@24465
   246
lemmas zmde = zmod_zdiv_equality [THEN diff_eq_eq [THEN iffD2], symmetric]
huffman@24465
   247
lemmas mcl = mult_cancel_left [THEN iffD1, THEN make_pos_rule]
huffman@24465
   248
huffman@24465
   249
(* already have this for naturals, div_mult_self1/2, but not for ints *)
huffman@24465
   250
lemma zdiv_mult_self: "m ~= (0 :: int) ==> (a + m * n) div m = a div m + n"
huffman@24465
   251
  apply (rule mcl)
huffman@24465
   252
   prefer 2
huffman@24465
   253
   apply (erule asm_rl)
huffman@24465
   254
  apply (simp add: zmde ring_distribs)
huffman@24465
   255
  done
huffman@24465
   256
huffman@24465
   257
(** Rep_Integ **)
huffman@24465
   258
lemma eqne: "equiv A r ==> X : A // r ==> X ~= {}"
nipkow@30198
   259
  unfolding equiv_def refl_on_def quotient_def Image_def by auto
huffman@24465
   260
huffman@24465
   261
lemmas Rep_Integ_ne = Integ.Rep_Integ 
huffman@24465
   262
  [THEN equiv_intrel [THEN eqne, simplified Integ_def [symmetric]], standard]
huffman@24465
   263
huffman@24465
   264
lemmas riq = Integ.Rep_Integ [simplified Integ_def]
huffman@24465
   265
lemmas intrel_refl = refl [THEN equiv_intrel_iff [THEN iffD1], standard]
huffman@24465
   266
lemmas Rep_Integ_equiv = quotient_eq_iff
huffman@24465
   267
  [OF equiv_intrel riq riq, simplified Integ.Rep_Integ_inject, standard]
huffman@24465
   268
lemmas Rep_Integ_same = 
huffman@24465
   269
  Rep_Integ_equiv [THEN intrel_refl [THEN rev_iffD2], standard]
huffman@24465
   270
huffman@24465
   271
lemma RI_int: "(a, 0) : Rep_Integ (int a)"
huffman@24465
   272
  unfolding int_def by auto
huffman@24465
   273
huffman@24465
   274
lemmas RI_intrel [simp] = UNIV_I [THEN quotientI,
huffman@24465
   275
  THEN Integ.Abs_Integ_inverse [simplified Integ_def], standard]
huffman@24465
   276
huffman@24465
   277
lemma RI_minus: "(a, b) : Rep_Integ x ==> (b, a) : Rep_Integ (- x)"
huffman@24465
   278
  apply (rule_tac z=x in eq_Abs_Integ)
huffman@24465
   279
  apply (clarsimp simp: minus)
huffman@24465
   280
  done
kleing@24333
   281
huffman@24465
   282
lemma RI_add: 
huffman@24465
   283
  "(a, b) : Rep_Integ x ==> (c, d) : Rep_Integ y ==> 
huffman@24465
   284
   (a + c, b + d) : Rep_Integ (x + y)"
huffman@24465
   285
  apply (rule_tac z=x in eq_Abs_Integ)
huffman@24465
   286
  apply (rule_tac z=y in eq_Abs_Integ) 
huffman@24465
   287
  apply (clarsimp simp: add)
huffman@24465
   288
  done
huffman@24465
   289
huffman@24465
   290
lemma mem_same: "a : S ==> a = b ==> b : S"
huffman@24465
   291
  by fast
huffman@24465
   292
huffman@24465
   293
(* two alternative proofs of this *)
huffman@24465
   294
lemma RI_eq_diff': "(a, b) : Rep_Integ (int a - int b)"
haftmann@37887
   295
  apply (unfold diff_minus)
huffman@24465
   296
  apply (rule mem_same)
huffman@24465
   297
   apply (rule RI_minus RI_add RI_int)+
huffman@24465
   298
  apply simp
huffman@24465
   299
  done
huffman@24465
   300
huffman@24465
   301
lemma RI_eq_diff: "((a, b) : Rep_Integ x) = (int a - int b = x)"
huffman@24465
   302
  apply safe
huffman@24465
   303
   apply (rule Rep_Integ_same)
huffman@24465
   304
    prefer 2
huffman@24465
   305
    apply (erule asm_rl)
huffman@24465
   306
   apply (rule RI_eq_diff')+
huffman@24465
   307
  done
huffman@24465
   308
huffman@24465
   309
lemma mod_power_lem:
huffman@24465
   310
  "a > 1 ==> a ^ n mod a ^ m = (if m <= n then 0 else (a :: int) ^ n)"
huffman@24465
   311
  apply clarsimp
huffman@24465
   312
  apply safe
nipkow@30042
   313
   apply (simp add: dvd_eq_mod_eq_0 [symmetric])
huffman@24465
   314
   apply (drule le_iff_add [THEN iffD1])
huffman@24465
   315
   apply (force simp: zpower_zadd_distrib)
huffman@24465
   316
  apply (rule mod_pos_pos_trivial)
nipkow@25875
   317
   apply (simp)
huffman@24465
   318
  apply (rule power_strict_increasing)
huffman@24465
   319
   apply auto
huffman@24465
   320
  done
kleing@24333
   321
chaieb@27570
   322
lemma min_pm [simp]: "min a b + (a - b) = (a :: nat)" by arith
kleing@24333
   323
  
kleing@24333
   324
lemmas min_pm1 [simp] = trans [OF add_commute min_pm]
kleing@24333
   325
chaieb@27570
   326
lemma rev_min_pm [simp]: "min b a + (a - b) = (a::nat)" by arith
kleing@24333
   327
kleing@24333
   328
lemmas rev_min_pm1 [simp] = trans [OF add_commute rev_min_pm]
kleing@24333
   329
huffman@24465
   330
lemma pl_pl_rels: 
huffman@24465
   331
  "a + b = c + d ==> 
chaieb@27570
   332
   a >= c & b <= d | a <= c & b >= (d :: nat)" by arith
huffman@24465
   333
huffman@24465
   334
lemmas pl_pl_rels' = add_commute [THEN [2] trans, THEN pl_pl_rels]
huffman@24465
   335
chaieb@27570
   336
lemma minus_eq: "(m - k = m) = (k = 0 | m = (0 :: nat))"  by arith
huffman@24465
   337
chaieb@27570
   338
lemma pl_pl_mm: "(a :: nat) + b = c + d ==> a - c = d - b"  by arith
huffman@24465
   339
huffman@24465
   340
lemmas pl_pl_mm' = add_commute [THEN [2] trans, THEN pl_pl_mm]
huffman@24465
   341
 
chaieb@27570
   342
lemma min_minus [simp] : "min m (m - k) = (m - k :: nat)" by arith
kleing@24333
   343
  
kleing@24333
   344
lemmas min_minus' [simp] = trans [OF min_max.inf_commute min_minus]
kleing@24333
   345
huffman@24465
   346
lemma nat_no_eq_iff: 
huffman@24465
   347
  "(number_of b :: int) >= 0 ==> (number_of c :: int) >= 0 ==> 
chaieb@27570
   348
   (number_of b = (number_of c :: nat)) = (b = c)" 
chaieb@27570
   349
  apply (unfold nat_number_of_def) 
huffman@24465
   350
  apply safe
huffman@24465
   351
  apply (drule (2) eq_nat_nat_iff [THEN iffD1])
huffman@24465
   352
  apply (simp add: number_of_eq)
huffman@24465
   353
  done
huffman@24465
   354
kleing@24333
   355
lemmas dme = box_equals [OF div_mod_equality add_0_right add_0_right]
kleing@24333
   356
lemmas dtle = xtr3 [OF dme [symmetric] le_add1]
kleing@24333
   357
lemmas th2 = order_trans [OF order_refl [THEN [2] mult_le_mono] dtle]
kleing@24333
   358
kleing@24333
   359
lemma td_gal: 
kleing@24333
   360
  "0 < c ==> (a >= b * c) = (a div c >= (b :: nat))"
kleing@24333
   361
  apply safe
kleing@24333
   362
   apply (erule (1) xtr4 [OF div_le_mono div_mult_self_is_m])
kleing@24333
   363
  apply (erule th2)
kleing@24333
   364
  done
kleing@24333
   365
  
haftmann@26072
   366
lemmas td_gal_lt = td_gal [simplified not_less [symmetric], simplified]
kleing@24333
   367
kleing@24333
   368
lemma div_mult_le: "(a :: nat) div b * b <= a"
kleing@24333
   369
  apply (cases b)
kleing@24333
   370
   prefer 2
kleing@24333
   371
   apply (rule order_refl [THEN th2])
kleing@24333
   372
  apply auto
kleing@24333
   373
  done
kleing@24333
   374
kleing@24333
   375
lemmas sdl = split_div_lemma [THEN iffD1, symmetric]
kleing@24333
   376
kleing@24333
   377
lemma given_quot: "f > (0 :: nat) ==> (f * l + (f - 1)) div f = l"
kleing@24333
   378
  by (rule sdl, assumption) (simp (no_asm))
kleing@24333
   379
kleing@24333
   380
lemma given_quot_alt: "f > (0 :: nat) ==> (l * f + f - Suc 0) div f = l"
kleing@24333
   381
  apply (frule given_quot)
kleing@24333
   382
  apply (rule trans)
kleing@24333
   383
   prefer 2
kleing@24333
   384
   apply (erule asm_rl)
kleing@24333
   385
  apply (rule_tac f="%n. n div f" in arg_cong)
kleing@24333
   386
  apply (simp add : mult_ac)
kleing@24333
   387
  done
kleing@24333
   388
    
huffman@24465
   389
lemma diff_mod_le: "(a::nat) < d ==> b dvd d ==> a - a mod b <= d - b"
huffman@24465
   390
  apply (unfold dvd_def)
huffman@24465
   391
  apply clarify
huffman@24465
   392
  apply (case_tac k)
huffman@24465
   393
   apply clarsimp
huffman@24465
   394
  apply clarify
huffman@24465
   395
  apply (cases "b > 0")
huffman@24465
   396
   apply (drule mult_commute [THEN xtr1])
huffman@24465
   397
   apply (frule (1) td_gal_lt [THEN iffD1])
huffman@24465
   398
   apply (clarsimp simp: le_simps)
huffman@24465
   399
   apply (rule mult_div_cancel [THEN [2] xtr4])
huffman@24465
   400
   apply (rule mult_mono)
huffman@24465
   401
      apply auto
huffman@24465
   402
  done
huffman@24465
   403
kleing@24333
   404
lemma less_le_mult':
kleing@24333
   405
  "w * c < b * c ==> 0 \<le> c ==> (w + 1) * c \<le> b * (c::int)"
kleing@24333
   406
  apply (rule mult_right_mono)
kleing@24333
   407
   apply (rule zless_imp_add1_zle)
kleing@24333
   408
   apply (erule (1) mult_right_less_imp_less)
kleing@24333
   409
  apply assumption
kleing@24333
   410
  done
kleing@24333
   411
kleing@24333
   412
lemmas less_le_mult = less_le_mult' [simplified left_distrib, simplified]
huffman@24465
   413
huffman@24465
   414
lemmas less_le_mult_minus = iffD2 [OF le_diff_eq less_le_mult, 
huffman@24465
   415
  simplified left_diff_distrib, standard]
kleing@24333
   416
kleing@24333
   417
lemma lrlem':
kleing@24333
   418
  assumes d: "(i::nat) \<le> j \<or> m < j'"
kleing@24333
   419
  assumes R1: "i * k \<le> j * k \<Longrightarrow> R"
kleing@24333
   420
  assumes R2: "Suc m * k' \<le> j' * k' \<Longrightarrow> R"
kleing@24333
   421
  shows "R" using d
kleing@24333
   422
  apply safe
kleing@24333
   423
   apply (rule R1, erule mult_le_mono1)
kleing@24333
   424
  apply (rule R2, erule Suc_le_eq [THEN iffD2 [THEN mult_le_mono1]])
kleing@24333
   425
  done
kleing@24333
   426
kleing@24333
   427
lemma lrlem: "(0::nat) < sc ==>
kleing@24333
   428
    (sc - n + (n + lb * n) <= m * n) = (sc + lb * n <= m * n)"
kleing@24333
   429
  apply safe
kleing@24333
   430
   apply arith
kleing@24333
   431
  apply (case_tac "sc >= n")
kleing@24333
   432
   apply arith
kleing@24333
   433
  apply (insert linorder_le_less_linear [of m lb])
kleing@24333
   434
  apply (erule_tac k=n and k'=n in lrlem')
kleing@24333
   435
   apply arith
kleing@24333
   436
  apply simp
kleing@24333
   437
  done
kleing@24333
   438
kleing@24333
   439
lemma gen_minus: "0 < n ==> f n = f (Suc (n - 1))"
kleing@24333
   440
  by auto
kleing@24333
   441
chaieb@27570
   442
lemma mpl_lem: "j <= (i :: nat) ==> k < j ==> i - j + k < i" by arith
kleing@24333
   443
huffman@24465
   444
lemma nonneg_mod_div:
huffman@24465
   445
  "0 <= a ==> 0 <= b ==> 0 <= (a mod b :: int) & 0 <= a div b"
huffman@24465
   446
  apply (cases "b = 0", clarsimp)
huffman@24465
   447
  apply (auto intro: pos_imp_zdiv_nonneg_iff [THEN iffD2])
huffman@24465
   448
  done
huffman@24399
   449
kleing@24333
   450
end