src/HOL/Word/Num_Lemmas.thy
author kleing
Mon, 20 Aug 2007 04:34:31 +0200
changeset 24333 e77ea0ea7f2c
child 24350 4d74f37c6367
permissions -rw-r--r--
* HOL-Word: New extensive library and type for generic, fixed size machine words, with arithemtic, bit-wise, shifting and rotating operations, reflection into int, nat, and bool lists, automation for linear arithmetic (by automatic reflection into nat or int), including lemmas on overflow and monotonicity. Instantiated to all appropriate arithmetic type classes, supporting automatic simplification of numerals on all operations. Jointly developed by NICTA, Galois, and PSU. * still to do: README.html/document + moving some of the generic lemmas to appropriate place in distribution
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
  ID:      $Id$
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
     3
  Author:  Jeremy Dawson, NICTA
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
     4
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
     5
  useful numerical lemmas 
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
     6
*) 
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
     7
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
     8
theory Num_Lemmas imports Parity begin
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
     9
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    10
lemma contentsI: "y = {x} ==> contents y = x" 
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    11
  unfolding contents_def by auto
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    12
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    13
lemma prod_case_split: "prod_case = split"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    14
  by (rule ext)+ auto
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    15
 
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    16
lemmas split_split = prod.split [unfolded prod_case_split] 
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    17
lemmas split_split_asm = prod.split_asm [unfolded prod_case_split]
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    18
lemmas "split.splits" = split_split split_split_asm 
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    19
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    20
lemmas funpow_0 = funpow.simps(1)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    21
lemmas funpow_Suc = funpow.simps(2)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    22
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    23
lemma nonemptyE: "S ~= {} ==> (!!x. x : S ==> R) ==> R"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    24
  apply (erule contrapos_np)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    25
  apply (rule equals0I)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    26
  apply auto
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    27
  done
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    28
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    29
lemma int_number_of: "number_of (y::int) = y" 
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    30
  by (simp add: number_of_eq)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    31
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    32
lemma gt_or_eq_0: "0 < y \<or> 0 = (y::nat)" by auto
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    33
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    34
constdefs
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    35
  mod_alt :: "'a => 'a => 'a :: Divides.div"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    36
  "mod_alt n m == n mod m" 
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    37
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    38
  -- "alternative way of defining @{text bin_last}, @{text bin_rest}"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    39
  bin_rl :: "int => int * bit" 
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    40
  "bin_rl w == SOME (r, l). w = r BIT l"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    41
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    42
declare iszero_0 [iff]
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    43
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    44
lemmas xtr1 = xtrans(1)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    45
lemmas xtr2 = xtrans(2)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    46
lemmas xtr3 = xtrans(3)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    47
lemmas xtr4 = xtrans(4)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    48
lemmas xtr5 = xtrans(5)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    49
lemmas xtr6 = xtrans(6)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    50
lemmas xtr7 = xtrans(7)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    51
lemmas xtr8 = xtrans(8)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    52
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    53
lemma Min_ne_Pls [iff]:  
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    54
  "Numeral.Min ~= Numeral.Pls"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    55
  unfolding Min_def Pls_def by auto
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    56
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    57
lemmas Pls_ne_Min [iff] = Min_ne_Pls [symmetric]
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    58
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    59
lemmas PlsMin_defs [intro!] = 
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    60
  Pls_def Min_def Pls_def [symmetric] Min_def [symmetric]
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    61
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    62
lemmas PlsMin_simps [simp] = PlsMin_defs [THEN Eq_TrueI]
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    63
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    64
lemma number_of_False_cong: 
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    65
  "False ==> number_of x = number_of y" 
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    66
  by auto
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    67
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    68
lemmas nat_simps = diff_add_inverse2 diff_add_inverse
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    69
lemmas nat_iffs = le_add1 le_add2
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    70
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    71
lemma sum_imp_diff: "j = k + i ==> j - i = (k :: nat)"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    72
  by (clarsimp simp add: nat_simps)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    73
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    74
lemma nobm1:
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    75
  "0 < (number_of w :: nat) ==> 
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    76
   number_of w - (1 :: nat) = number_of (Numeral.pred w)"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    77
  apply (unfold nat_number_of_def One_nat_def nat_1 [symmetric] pred_def)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    78
  apply (simp add: number_of_eq nat_diff_distrib [symmetric])
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    79
  done
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    80
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    81
lemma of_int_power:
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    82
  "of_int (a ^ n) = (of_int a ^ n :: 'a :: {recpower, comm_ring_1})" 
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    83
  by (induct n) (auto simp add: power_Suc)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    84
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    85
lemma zless2: "0 < (2 :: int)" 
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    86
  by auto
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    87
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    88
lemmas zless2p [simp] = zless2 [THEN zero_less_power]
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    89
lemmas zle2p [simp] = zless2p [THEN order_less_imp_le]
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    90
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    91
lemmas pos_mod_sign2 = zless2 [THEN pos_mod_sign [where b = "2::int"]]
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    92
lemmas pos_mod_bound2 = zless2 [THEN pos_mod_bound [where b = "2::int"]]
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    93
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    94
-- "the inverse(s) of @{text number_of}"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    95
lemma nmod2: "n mod (2::int) = 0 | n mod 2 = 1"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    96
  using pos_mod_sign2 [of n] pos_mod_bound2 [of n]
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    97
  unfolding mod_alt_def [symmetric] by auto
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    98
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    99
lemma emep1:
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   100
  "even n ==> even d ==> 0 <= d ==> (n + 1) mod (d :: int) = (n mod d) + 1"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   101
  apply (simp add: add_commute)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   102
  apply (safe dest!: even_equiv_def [THEN iffD1])
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   103
  apply (subst pos_zmod_mult_2)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   104
   apply arith
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   105
  apply (simp add: zmod_zmult_zmult1)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   106
 done
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   107
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   108
lemmas eme1p = emep1 [simplified add_commute]
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   109
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   110
lemma le_diff_eq': "(a \<le> c - b) = (b + a \<le> (c::int))"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   111
  by (simp add: le_diff_eq add_commute)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   112
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   113
lemma less_diff_eq': "(a < c - b) = (b + a < (c::int))"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   114
  by (simp add: less_diff_eq add_commute)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   115
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   116
lemma diff_le_eq': "(a - b \<le> c) = (a \<le> b + (c::int))"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   117
  by (simp add: diff_le_eq add_commute)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   118
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   119
lemma diff_less_eq': "(a - b < c) = (a < b + (c::int))"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   120
  by (simp add: diff_less_eq add_commute)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   121
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   122
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   123
lemmas m1mod2k = zless2p [THEN zmod_minus1]
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   124
lemmas m1mod22k = mult_pos_pos [OF zless2 zless2p, THEN zmod_minus1]
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   125
lemmas p1mod22k' = zless2p [THEN order_less_imp_le, THEN pos_zmod_mult_2]
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   126
lemmas z1pmod2' = zero_le_one [THEN pos_zmod_mult_2, simplified]
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   127
lemmas z1pdiv2' = zero_le_one [THEN pos_zdiv_mult_2, simplified]
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   128
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   129
lemma p1mod22k:
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   130
  "(2 * b + 1) mod (2 * 2 ^ n) = 2 * (b mod 2 ^ n) + (1::int)"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   131
  by (simp add: p1mod22k' add_commute)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   132
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   133
lemma z1pmod2:
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   134
  "(2 * b + 1) mod 2 = (1::int)"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   135
  by (simp add: z1pmod2' add_commute)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   136
  
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   137
lemma z1pdiv2:
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   138
  "(2 * b + 1) div 2 = (b::int)"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   139
  by (simp add: z1pdiv2' add_commute)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   140
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   141
lemmas zdiv_le_dividend = xtr3 [OF zdiv_1 [symmetric] zdiv_mono2,
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   142
  simplified int_one_le_iff_zero_less, simplified, standard]
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   143
  
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   144
(** ways in which type Bin resembles a datatype **)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   145
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   146
lemma BIT_eq: "u BIT b = v BIT c ==> u = v & b = c"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   147
  apply (unfold Bit_def)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   148
  apply (simp (no_asm_use) split: bit.split_asm)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   149
     apply simp_all
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   150
   apply (drule_tac f=even in arg_cong, clarsimp)+
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   151
  done
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   152
     
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   153
lemmas BIT_eqE [elim!] = BIT_eq [THEN conjE, standard]
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   154
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   155
lemma BIT_eq_iff [simp]: 
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   156
  "(u BIT b = v BIT c) = (u = v \<and> b = c)"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   157
  by (rule iffI) auto
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   158
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   159
lemmas BIT_eqI [intro!] = conjI [THEN BIT_eq_iff [THEN iffD2]]
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   160
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   161
lemma less_Bits: 
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   162
  "(v BIT b < w BIT c) = (v < w | v <= w & b = bit.B0 & c = bit.B1)"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   163
  unfolding Bit_def by (auto split: bit.split)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   164
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   165
lemma le_Bits: 
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   166
  "(v BIT b <= w BIT c) = (v < w | v <= w & (b ~= bit.B1 | c ~= bit.B0))" 
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   167
  unfolding Bit_def by (auto split: bit.split)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   168
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   169
lemma neB1E [elim!]:
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   170
  assumes ne: "y \<noteq> bit.B1"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   171
  assumes y: "y = bit.B0 \<Longrightarrow> P"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   172
  shows "P"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   173
  apply (rule y)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   174
  apply (cases y rule: bit.exhaust, simp)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   175
  apply (simp add: ne)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   176
  done
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   177
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   178
lemma bin_ex_rl: "EX w b. w BIT b = bin"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   179
  apply (unfold Bit_def)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   180
  apply (cases "even bin")
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   181
   apply (clarsimp simp: even_equiv_def)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   182
   apply (auto simp: odd_equiv_def split: bit.split)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   183
  done
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   184
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   185
lemma bin_exhaust:
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   186
  assumes Q: "\<And>x b. bin = x BIT b \<Longrightarrow> Q"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   187
  shows "Q"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   188
  apply (insert bin_ex_rl [of bin])  
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   189
  apply (erule exE)+
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   190
  apply (rule Q)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   191
  apply force
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   192
  done
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   193
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   194
lemma bin_rl_char: "(bin_rl w = (r, l)) = (r BIT l = w)"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   195
  apply (unfold bin_rl_def)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   196
  apply safe
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   197
   apply (cases w rule: bin_exhaust)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   198
   apply auto
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   199
  done
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   200
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   201
lemmas bin_rl_simps [THEN bin_rl_char [THEN iffD2], standard, simp] =
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   202
  Pls_0_eq Min_1_eq refl 
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   203
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   204
lemma bin_abs_lem:
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   205
  "bin = (w BIT b) ==> ~ bin = Numeral.Min --> ~ bin = Numeral.Pls -->
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   206
    nat (abs w) < nat (abs bin)"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   207
  apply (clarsimp simp add: bin_rl_char)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   208
  apply (unfold Pls_def Min_def Bit_def)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   209
  apply (cases b)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   210
   apply (clarsimp, arith)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   211
  apply (clarsimp, arith)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   212
  done
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   213
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   214
lemma bin_induct:
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   215
  assumes PPls: "P Numeral.Pls"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   216
    and PMin: "P Numeral.Min"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   217
    and PBit: "!!bin bit. P bin ==> P (bin BIT bit)"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   218
  shows "P bin"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   219
  apply (rule_tac P=P and a=bin and f1="nat o abs" 
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   220
                  in wf_measure [THEN wf_induct])
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   221
  apply (simp add: measure_def inv_image_def)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   222
  apply (case_tac x rule: bin_exhaust)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   223
  apply (frule bin_abs_lem)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   224
  apply (auto simp add : PPls PMin PBit)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   225
  done
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   226
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   227
lemma no_no [simp]: "number_of (number_of i) = i"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   228
  unfolding number_of_eq by simp
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   229
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   230
lemma Bit_B0:
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   231
  "k BIT bit.B0 = k + k"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   232
   by (unfold Bit_def) simp
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   233
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   234
lemma Bit_B1:
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   235
  "k BIT bit.B1 = k + k + 1"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   236
   by (unfold Bit_def) simp
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   237
  
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   238
lemma Bit_B0_2t: "k BIT bit.B0 = 2 * k"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   239
  by (rule trans, rule Bit_B0) simp
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   240
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   241
lemma Bit_B1_2t: "k BIT bit.B1 = 2 * k + 1"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   242
  by (rule trans, rule Bit_B1) simp
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   243
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   244
lemma B_mod_2': 
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   245
  "X = 2 ==> (w BIT bit.B1) mod X = 1 & (w BIT bit.B0) mod X = 0"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   246
  apply (simp (no_asm) only: Bit_B0 Bit_B1)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   247
  apply (simp add: z1pmod2)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   248
  done
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   249
    
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   250
lemmas B1_mod_2 [simp] = B_mod_2' [OF refl, THEN conjunct1, standard]
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   251
lemmas B0_mod_2 [simp] = B_mod_2' [OF refl, THEN conjunct2, standard]
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   252
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   253
lemma axxbyy:
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   254
  "a + m + m = b + n + n ==> (a = 0 | a = 1) ==> (b = 0 | b = 1) ==>  
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   255
   a = b & m = (n :: int)"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   256
  apply auto
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   257
   apply (drule_tac f="%n. n mod 2" in arg_cong)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   258
   apply (clarsimp simp: z1pmod2)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   259
  apply (drule_tac f="%n. n mod 2" in arg_cong)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   260
  apply (clarsimp simp: z1pmod2)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   261
  done
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   262
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   263
lemma axxmod2:
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   264
  "(1 + x + x) mod 2 = (1 :: int) & (0 + x + x) mod 2 = (0 :: int)" 
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   265
  by simp (rule z1pmod2)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   266
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   267
lemma axxdiv2:
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   268
  "(1 + x + x) div 2 = (x :: int) & (0 + x + x) div 2 = (x :: int)" 
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   269
  by simp (rule z1pdiv2)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   270
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   271
lemmas iszero_minus = trans [THEN trans,
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   272
  OF iszero_def neg_equal_0_iff_equal iszero_def [symmetric], standard]
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   273
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   274
lemmas zadd_diff_inverse = trans [OF diff_add_cancel [symmetric] add_commute,
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   275
  standard]
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   276
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   277
lemmas add_diff_cancel2 = add_commute [THEN diff_eq_eq [THEN iffD2], standard]
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   278
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   279
lemma zmod_uminus: "- ((a :: int) mod b) mod b = -a mod b"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   280
  by (simp add : zmod_zminus1_eq_if)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   281
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   282
lemma zmod_zsub_distrib: "((a::int) - b) mod c = (a mod c - b mod c) mod c"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   283
  apply (unfold diff_int_def)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   284
  apply (rule trans [OF _ zmod_zadd1_eq [symmetric]])
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   285
  apply (simp add: zmod_uminus zmod_zadd1_eq [symmetric])
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   286
  done
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   287
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   288
lemma zmod_zsub_right_eq: "((a::int) - b) mod c = (a - b mod c) mod c"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   289
  apply (unfold diff_int_def)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   290
  apply (rule trans [OF _ zmod_zadd_right_eq [symmetric]])
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   291
  apply (simp add : zmod_uminus zmod_zadd_right_eq [symmetric])
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   292
  done
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   293
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   294
lemmas zmod_zsub_left_eq = 
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   295
  zmod_zadd_left_eq [where b = "- ?b", simplified diff_int_def [symmetric]]
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   296
  
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   297
lemma zmod_zsub_self [simp]: 
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   298
  "((b :: int) - a) mod a = b mod a"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   299
  by (simp add: zmod_zsub_right_eq)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   300
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   301
lemma zmod_zmult1_eq_rev:
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   302
  "b * a mod c = b mod c * a mod (c::int)"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   303
  apply (simp add: mult_commute)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   304
  apply (subst zmod_zmult1_eq)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   305
  apply simp
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   306
  done
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   307
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   308
lemmas rdmods [symmetric] = zmod_uminus [symmetric]
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   309
  zmod_zsub_left_eq zmod_zsub_right_eq zmod_zadd_left_eq
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   310
  zmod_zadd_right_eq zmod_zmult1_eq zmod_zmult1_eq_rev
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   311
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   312
lemma mod_plus_right:
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   313
  "((a + x) mod m = (b + x) mod m) = (a mod m = b mod (m :: nat))"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   314
  apply (induct x)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   315
   apply (simp_all add: mod_Suc)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   316
  apply arith
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   317
  done
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   318
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   319
lemma nat_minus_mod: "(n - n mod m) mod m = (0 :: nat)"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   320
  by (induct n) (simp_all add : mod_Suc)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   321
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   322
lemmas nat_minus_mod_plus_right = trans [OF nat_minus_mod mod_0 [symmetric],
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   323
  THEN mod_plus_right [THEN iffD2], standard, simplified]
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   324
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   325
lemmas push_mods' = zmod_zadd1_eq [standard]
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   326
  zmod_zmult_distrib [standard] zmod_zsub_distrib [standard]
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   327
  zmod_uminus [symmetric, standard]
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   328
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   329
lemmas push_mods = push_mods' [THEN eq_reflection, standard]
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   330
lemmas pull_mods = push_mods [symmetric] rdmods [THEN eq_reflection, standard]
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   331
lemmas mod_simps = 
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   332
  zmod_zmult_self1 [THEN eq_reflection] zmod_zmult_self2 [THEN eq_reflection]
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   333
  mod_mod_trivial [THEN eq_reflection]
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   334
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   335
lemma nat_mod_eq:
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   336
  "!!b. b < n ==> a mod n = b mod n ==> a mod n = (b :: nat)" 
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   337
  by (induct a) auto
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   338
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   339
lemmas nat_mod_eq' = refl [THEN [2] nat_mod_eq]
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   340
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   341
lemma nat_mod_lem: 
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   342
  "(0 :: nat) < n ==> b < n = (b mod n = b)"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   343
  apply safe
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   344
   apply (erule nat_mod_eq')
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   345
  apply (erule subst)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   346
  apply (erule mod_less_divisor)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   347
  done
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   348
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   349
lemma mod_nat_add: 
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   350
  "(x :: nat) < z ==> y < z ==> 
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   351
   (x + y) mod z = (if x + y < z then x + y else x + y - z)"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   352
  apply (rule nat_mod_eq)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   353
   apply auto
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   354
  apply (rule trans)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   355
   apply (rule le_mod_geq)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   356
   apply simp
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   357
  apply (rule nat_mod_eq')
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   358
  apply arith
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   359
  done
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   360
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   361
lemma mod_nat_sub: 
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   362
  "(x :: nat) < z ==> (x - y) mod z = x - y"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   363
  by (rule nat_mod_eq') arith
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   364
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   365
lemma int_mod_lem: 
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   366
  "(0 :: int) < n ==> (0 <= b & b < n) = (b mod n = b)"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   367
  apply safe
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   368
    apply (erule (1) mod_pos_pos_trivial)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   369
   apply (erule_tac [!] subst)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   370
   apply auto
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   371
  done
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   372
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   373
lemma int_mod_eq:
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   374
  "(0 :: int) <= b ==> b < n ==> a mod n = b mod n ==> a mod n = b"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   375
  by clarsimp (rule mod_pos_pos_trivial)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   376
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   377
lemmas int_mod_eq' = refl [THEN [3] int_mod_eq]
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   378
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   379
lemma int_mod_le: "0 <= a ==> 0 < (n :: int) ==> a mod n <= a"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   380
  apply (cases "a < n")
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   381
   apply (auto dest: mod_pos_pos_trivial pos_mod_bound [where a=a])
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   382
  done
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   383
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   384
lemmas int_mod_le' = int_mod_le [where a = "?b - ?n", simplified]
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   385
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   386
lemma int_mod_ge: "a < n ==> 0 < (n :: int) ==> a <= a mod n"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   387
  apply (cases "0 <= a")
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   388
   apply (drule (1) mod_pos_pos_trivial)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   389
   apply simp
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   390
  apply (rule order_trans [OF _ pos_mod_sign])
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   391
   apply simp
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   392
  apply assumption
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   393
  done
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   394
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   395
lemmas int_mod_ge' = int_mod_ge [where a = "?b + ?n", simplified]
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   396
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   397
lemma mod_add_if_z:
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   398
  "(x :: int) < z ==> y < z ==> 0 <= y ==> 0 <= x ==> 0 <= z ==> 
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   399
   (x + y) mod z = (if x + y < z then x + y else x + y - z)"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   400
  by (auto intro: int_mod_eq)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   401
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   402
lemma mod_sub_if_z:
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   403
  "(x :: int) < z ==> y < z ==> 0 <= y ==> 0 <= x ==> 0 <= z ==> 
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   404
   (x - y) mod z = (if y <= x then x - y else x - y + z)"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   405
  by (auto intro: int_mod_eq)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   406
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   407
lemmas zmde = zmod_zdiv_equality [THEN diff_eq_eq [THEN iffD2], symmetric]
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   408
lemmas mcl = mult_cancel_left [THEN iffD1, THEN make_pos_rule]
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   409
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   410
(* already have this for naturals, div_mult_self1/2, but not for ints *)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   411
lemma zdiv_mult_self: "m ~= (0 :: int) ==> (a + m * n) div m = a div m + n"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   412
  apply (rule mcl)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   413
   prefer 2
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   414
   apply (erule asm_rl)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   415
  apply (simp add: zmde ring_distribs)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   416
  apply (simp add: push_mods)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   417
  done
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   418
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   419
(** Rep_Integ **)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   420
lemma eqne: "equiv A r ==> X : A // r ==> X ~= {}"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   421
  unfolding equiv_def refl_def quotient_def Image_def by auto
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   422
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   423
lemmas Rep_Integ_ne = Integ.Rep_Integ 
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   424
  [THEN equiv_intrel [THEN eqne, simplified Integ_def [symmetric]], standard]
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   425
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   426
lemmas riq = Integ.Rep_Integ [simplified Integ_def]
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   427
lemmas intrel_refl = refl [THEN equiv_intrel_iff [THEN iffD1], standard]
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   428
lemmas Rep_Integ_equiv = quotient_eq_iff
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   429
  [OF equiv_intrel riq riq, simplified Integ.Rep_Integ_inject, standard]
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   430
lemmas Rep_Integ_same = 
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   431
  Rep_Integ_equiv [THEN intrel_refl [THEN rev_iffD2], standard]
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   432
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   433
lemma RI_int: "(a, 0) : Rep_Integ (int a)"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   434
  unfolding int_def by auto
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   435
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   436
lemmas RI_intrel [simp] = UNIV_I [THEN quotientI,
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   437
  THEN Integ.Abs_Integ_inverse [simplified Integ_def], standard]
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   438
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   439
lemma RI_minus: "(a, b) : Rep_Integ x ==> (b, a) : Rep_Integ (- x)"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   440
  apply (rule_tac z=x in eq_Abs_Integ)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   441
  apply (clarsimp simp: minus)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   442
  done
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   443
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   444
lemma RI_add: 
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   445
  "(a, b) : Rep_Integ x ==> (c, d) : Rep_Integ y ==> 
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   446
   (a + c, b + d) : Rep_Integ (x + y)"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   447
  apply (rule_tac z=x in eq_Abs_Integ)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   448
  apply (rule_tac z=y in eq_Abs_Integ) 
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   449
  apply (clarsimp simp: add)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   450
  done
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   451
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   452
lemma mem_same: "a : S ==> a = b ==> b : S"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   453
  by fast
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   454
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   455
(* two alternative proofs of this *)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   456
lemma RI_eq_diff': "(a, b) : Rep_Integ (int a - int b)"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   457
  apply (unfold diff_def)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   458
  apply (rule mem_same)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   459
   apply (rule RI_minus RI_add RI_int)+
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   460
  apply simp
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   461
  done
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   462
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   463
lemma RI_eq_diff: "((a, b) : Rep_Integ x) = (int a - int b = x)"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   464
  apply safe
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   465
   apply (rule Rep_Integ_same)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   466
    prefer 2
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   467
    apply (erule asm_rl)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   468
   apply (rule RI_eq_diff')+
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   469
  done
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   470
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   471
lemma mod_power_lem:
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   472
  "a > 1 ==> a ^ n mod a ^ m = (if m <= n then 0 else (a :: int) ^ n)"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   473
  apply clarsimp
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   474
  apply safe
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   475
   apply (simp add: zdvd_iff_zmod_eq_0 [symmetric])
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   476
   apply (drule le_iff_add [THEN iffD1])
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   477
   apply (force simp: zpower_zadd_distrib)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   478
  apply (rule mod_pos_pos_trivial)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   479
   apply (simp add: zero_le_power)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   480
  apply (rule power_strict_increasing)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   481
   apply auto
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   482
  done
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   483
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   484
lemma min_pm [simp]: "min a b + (a - b) = (a :: nat)"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   485
  by arith
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   486
  
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   487
lemmas min_pm1 [simp] = trans [OF add_commute min_pm]
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   488
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   489
lemma rev_min_pm [simp]: "min b a + (a - b) = (a::nat)"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   490
  by simp
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   491
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   492
lemmas rev_min_pm1 [simp] = trans [OF add_commute rev_min_pm]
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   493
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   494
lemma pl_pl_rels: 
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   495
  "a + b = c + d ==> 
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   496
   a >= c & b <= d | a <= c & b >= (d :: nat)"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   497
  apply (cut_tac n=a and m=c in nat_le_linear)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   498
  apply (safe dest!: le_iff_add [THEN iffD1])
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   499
         apply arith+
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   500
  done
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   501
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   502
lemmas pl_pl_rels' = add_commute [THEN [2] trans, THEN pl_pl_rels]
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   503
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   504
lemma minus_eq: "(m - k = m) = (k = 0 | m = (0 :: nat))"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   505
  by arith
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   506
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   507
lemma pl_pl_mm: "(a :: nat) + b = c + d ==> a - c = d - b"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   508
  by arith
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   509
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   510
lemmas pl_pl_mm' = add_commute [THEN [2] trans, THEN pl_pl_mm]
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   511
 
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   512
lemma min_minus [simp] : "min m (m - k) = (m - k :: nat)"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   513
  by arith
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   514
  
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   515
lemmas min_minus' [simp] = trans [OF min_max.inf_commute min_minus]
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   516
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   517
lemma nat_no_eq_iff: 
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   518
  "(number_of b :: int) >= 0 ==> (number_of c :: int) >= 0 ==> 
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   519
   (number_of b = (number_of c :: nat)) = (b = c)"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   520
  apply (unfold nat_number_of_def)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   521
  apply safe
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   522
  apply (drule (2) eq_nat_nat_iff [THEN iffD1])
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   523
  apply (simp add: number_of_eq)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   524
  done
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   525
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   526
lemmas dme = box_equals [OF div_mod_equality add_0_right add_0_right]
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   527
lemmas dtle = xtr3 [OF dme [symmetric] le_add1]
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   528
lemmas th2 = order_trans [OF order_refl [THEN [2] mult_le_mono] dtle]
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   529
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   530
lemma td_gal: 
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   531
  "0 < c ==> (a >= b * c) = (a div c >= (b :: nat))"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   532
  apply safe
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   533
   apply (erule (1) xtr4 [OF div_le_mono div_mult_self_is_m])
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   534
  apply (erule th2)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   535
  done
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   536
  
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   537
lemmas td_gal_lt = td_gal [simplified le_def, simplified]
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   538
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   539
lemma div_mult_le: "(a :: nat) div b * b <= a"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   540
  apply (cases b)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   541
   prefer 2
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   542
   apply (rule order_refl [THEN th2])
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   543
  apply auto
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   544
  done
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   545
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   546
lemmas sdl = split_div_lemma [THEN iffD1, symmetric]
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   547
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   548
lemma given_quot: "f > (0 :: nat) ==> (f * l + (f - 1)) div f = l"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   549
  by (rule sdl, assumption) (simp (no_asm))
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   550
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   551
lemma given_quot_alt: "f > (0 :: nat) ==> (l * f + f - Suc 0) div f = l"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   552
  apply (frule given_quot)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   553
  apply (rule trans)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   554
   prefer 2
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   555
   apply (erule asm_rl)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   556
  apply (rule_tac f="%n. n div f" in arg_cong)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   557
  apply (simp add : mult_ac)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   558
  done
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   559
    
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   560
lemma diff_mod_le: "(a::nat) < d ==> b dvd d ==> a - a mod b <= d - b"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   561
  apply (unfold dvd_def)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   562
  apply clarify
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   563
  apply (case_tac k)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   564
   apply clarsimp
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   565
  apply clarify
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   566
  apply (cases "b > 0")
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   567
   apply (drule mult_commute [THEN xtr1])
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   568
   apply (frule (1) td_gal_lt [THEN iffD1])
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   569
   apply (clarsimp simp: le_simps)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   570
   apply (rule mult_div_cancel [THEN [2] xtr4])
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   571
   apply (rule mult_mono)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   572
      apply auto
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   573
  done
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   574
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   575
lemma less_le_mult':
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   576
  "w * c < b * c ==> 0 \<le> c ==> (w + 1) * c \<le> b * (c::int)"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   577
  apply (rule mult_right_mono)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   578
   apply (rule zless_imp_add1_zle)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   579
   apply (erule (1) mult_right_less_imp_less)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   580
  apply assumption
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   581
  done
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   582
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   583
lemmas less_le_mult = less_le_mult' [simplified left_distrib, simplified]
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   584
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   585
lemmas less_le_mult_minus = iffD2 [OF le_diff_eq less_le_mult, 
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   586
  simplified left_diff_distrib, standard]
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   587
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   588
lemma lrlem':
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   589
  assumes d: "(i::nat) \<le> j \<or> m < j'"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   590
  assumes R1: "i * k \<le> j * k \<Longrightarrow> R"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   591
  assumes R2: "Suc m * k' \<le> j' * k' \<Longrightarrow> R"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   592
  shows "R" using d
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   593
  apply safe
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   594
   apply (rule R1, erule mult_le_mono1)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   595
  apply (rule R2, erule Suc_le_eq [THEN iffD2 [THEN mult_le_mono1]])
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   596
  done
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   597
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   598
lemma lrlem: "(0::nat) < sc ==>
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   599
    (sc - n + (n + lb * n) <= m * n) = (sc + lb * n <= m * n)"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   600
  apply safe
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   601
   apply arith
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   602
  apply (case_tac "sc >= n")
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   603
   apply arith
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   604
  apply (insert linorder_le_less_linear [of m lb])
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   605
  apply (erule_tac k=n and k'=n in lrlem')
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   606
   apply arith
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   607
  apply simp
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   608
  done
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   609
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   610
lemma gen_minus: "0 < n ==> f n = f (Suc (n - 1))"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   611
  by auto
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   612
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   613
lemma mpl_lem: "j <= (i :: nat) ==> k < j ==> i - j + k < i"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   614
  apply (induct i, clarsimp)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   615
  apply (cases j, clarsimp)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   616
  apply arith
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   617
  done
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   618
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   619
lemma nonneg_mod_div:
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   620
  "0 <= a ==> 0 <= b ==> 0 <= (a mod b :: int) & 0 <= a div b"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   621
  apply (cases "b = 0", clarsimp)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   622
  apply (auto intro: pos_imp_zdiv_nonneg_iff [THEN iffD2])
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   623
  done
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   624
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   625
end