src/HOL/Word/Word_Miscellaneous.thy
author haftmann
Fri Jun 19 07:53:35 2015 +0200 (2015-06-19)
changeset 60517 f16e4fb20652
parent 60104 243cee7c1e19
child 61799 4cf66f21b764
permissions -rw-r--r--
separate class for notions specific for integral (semi)domains, in contrast to fields where these are trivial
     1 (*  Title:      HOL/Word/Word_Miscellaneous.thy
     2     Author:     Miscellaneous
     3 *)
     4 
     5 section {* Miscellaneous lemmas, of at least doubtful value *}
     6 
     7 theory Word_Miscellaneous
     8 imports Main "~~/src/HOL/Library/Bit" Misc_Numeric
     9 begin
    10 
    11 lemma power_minus_simp:
    12   "0 < n \<Longrightarrow> a ^ n = a * a ^ (n - 1)"
    13   by (auto dest: gr0_implies_Suc)
    14 
    15 lemma funpow_minus_simp:
    16   "0 < n \<Longrightarrow> f ^^ n = f \<circ> f ^^ (n - 1)"
    17   by (auto dest: gr0_implies_Suc)
    18 
    19 lemma power_numeral:
    20   "a ^ numeral k = a * a ^ (pred_numeral k)"
    21   by (simp add: numeral_eq_Suc)
    22 
    23 lemma funpow_numeral [simp]:
    24   "f ^^ numeral k = f \<circ> f ^^ (pred_numeral k)"
    25   by (simp add: numeral_eq_Suc)
    26 
    27 lemma replicate_numeral [simp]:
    28   "replicate (numeral k) x = x # replicate (pred_numeral k) x"
    29   by (simp add: numeral_eq_Suc)
    30 
    31 lemma rco_alt: "(f o g) ^^ n o f = f o (g o f) ^^ n"
    32   apply (rule ext)
    33   apply (induct n)
    34    apply (simp_all add: o_def)
    35   done
    36 
    37 lemma list_exhaust_size_gt0:
    38   assumes y: "\<And>a list. y = a # list \<Longrightarrow> P"
    39   shows "0 < length y \<Longrightarrow> P"
    40   apply (cases y, simp)
    41   apply (rule y)
    42   apply fastforce
    43   done
    44 
    45 lemma list_exhaust_size_eq0:
    46   assumes y: "y = [] \<Longrightarrow> P"
    47   shows "length y = 0 \<Longrightarrow> P"
    48   apply (cases y)
    49    apply (rule y, simp)
    50   apply simp
    51   done
    52 
    53 lemma size_Cons_lem_eq:
    54   "y = xa # list ==> size y = Suc k ==> size list = k"
    55   by auto
    56 
    57 lemmas ls_splits = prod.split prod.split_asm split_if_asm
    58 
    59 lemma not_B1_is_B0: "y \<noteq> (1::bit) \<Longrightarrow> y = (0::bit)"
    60   by (cases y) auto
    61 
    62 lemma B1_ass_B0: 
    63   assumes y: "y = (0::bit) \<Longrightarrow> y = (1::bit)"
    64   shows "y = (1::bit)"
    65   apply (rule classical)
    66   apply (drule not_B1_is_B0)
    67   apply (erule y)
    68   done
    69 
    70 -- "simplifications for specific word lengths"
    71 lemmas n2s_ths [THEN eq_reflection] = add_2_eq_Suc add_2_eq_Suc'
    72 
    73 lemmas s2n_ths = n2s_ths [symmetric]
    74 
    75 lemma and_len: "xs = ys ==> xs = ys & length xs = length ys"
    76   by auto
    77 
    78 lemma size_if: "size (if p then xs else ys) = (if p then size xs else size ys)"
    79   by auto
    80 
    81 lemma tl_if: "tl (if p then xs else ys) = (if p then tl xs else tl ys)"
    82   by auto
    83 
    84 lemma hd_if: "hd (if p then xs else ys) = (if p then hd xs else hd ys)"
    85   by auto
    86 
    87 lemma if_Not_x: "(if p then ~ x else x) = (p = (~ x))"
    88   by auto
    89 
    90 lemma if_x_Not: "(if p then x else ~ x) = (p = x)"
    91   by auto
    92 
    93 lemma if_same_and: "(If p x y & If p u v) = (if p then x & u else y & v)"
    94   by auto
    95 
    96 lemma if_same_eq: "(If p x y  = (If p u v)) = (if p then x = (u) else y = (v))"
    97   by auto
    98 
    99 lemma if_same_eq_not:
   100   "(If p x y  = (~ If p u v)) = (if p then x = (~u) else y = (~v))"
   101   by auto
   102 
   103 (* note - if_Cons can cause blowup in the size, if p is complex,
   104   so make a simproc *)
   105 lemma if_Cons: "(if p then x # xs else y # ys) = If p x y # If p xs ys"
   106   by auto
   107 
   108 lemma if_single:
   109   "(if xc then [xab] else [an]) = [if xc then xab else an]"
   110   by auto
   111 
   112 lemma if_bool_simps:
   113   "If p True y = (p | y) & If p False y = (~p & y) & 
   114     If p y True = (p --> y) & If p y False = (p & y)"
   115   by auto
   116 
   117 lemmas if_simps = if_x_Not if_Not_x if_cancel if_True if_False if_bool_simps
   118 
   119 lemmas seqr = eq_reflection [where x = "size w"] for w (* FIXME: delete *)
   120 
   121 lemma the_elemI: "y = {x} ==> the_elem y = x" 
   122   by simp
   123 
   124 lemma nonemptyE: "S ~= {} ==> (!!x. x : S ==> R) ==> R" by auto
   125 
   126 lemma gt_or_eq_0: "0 < y \<or> 0 = (y::nat)" by arith 
   127 
   128 lemmas xtr1 = xtrans(1)
   129 lemmas xtr2 = xtrans(2)
   130 lemmas xtr3 = xtrans(3)
   131 lemmas xtr4 = xtrans(4)
   132 lemmas xtr5 = xtrans(5)
   133 lemmas xtr6 = xtrans(6)
   134 lemmas xtr7 = xtrans(7)
   135 lemmas xtr8 = xtrans(8)
   136 
   137 lemmas nat_simps = diff_add_inverse2 diff_add_inverse
   138 lemmas nat_iffs = le_add1 le_add2
   139 
   140 lemma sum_imp_diff: "j = k + i ==> j - i = (k :: nat)" by arith
   141 
   142 lemmas pos_mod_sign2 = zless2 [THEN pos_mod_sign [where b = "2::int"]]
   143 lemmas pos_mod_bound2 = zless2 [THEN pos_mod_bound [where b = "2::int"]]
   144 
   145 lemma nmod2: "n mod (2::int) = 0 | n mod 2 = 1"
   146   by arith
   147 
   148 lemmas eme1p = emep1 [simplified add.commute]
   149 
   150 lemma le_diff_eq': "(a \<le> c - b) = (b + a \<le> (c::int))" by arith
   151 
   152 lemma less_diff_eq': "(a < c - b) = (b + a < (c::int))" by arith
   153 
   154 lemma diff_less_eq': "(a - b < c) = (a < b + (c::int))" by arith
   155 
   156 lemmas m1mod22k = mult_pos_pos [OF zless2 zless2p, THEN zmod_minus1]
   157 
   158 lemma z1pdiv2:
   159   "(2 * b + 1) div 2 = (b::int)" by arith
   160 
   161 lemmas zdiv_le_dividend = xtr3 [OF div_by_1 [symmetric] zdiv_mono2,
   162   simplified int_one_le_iff_zero_less, simplified]
   163   
   164 lemma axxbyy:
   165   "a + m + m = b + n + n ==> (a = 0 | a = 1) ==> (b = 0 | b = 1) ==>  
   166    a = b & m = (n :: int)" by arith
   167 
   168 lemma axxmod2:
   169   "(1 + x + x) mod 2 = (1 :: int) & (0 + x + x) mod 2 = (0 :: int)" by arith
   170 
   171 lemma axxdiv2:
   172   "(1 + x + x) div 2 = (x :: int) & (0 + x + x) div 2 = (x :: int)"  by arith
   173 
   174 lemmas iszero_minus = trans [THEN trans,
   175   OF iszero_def neg_equal_0_iff_equal iszero_def [symmetric]]
   176 
   177 lemmas zadd_diff_inverse = trans [OF diff_add_cancel [symmetric] add.commute]
   178 
   179 lemmas add_diff_cancel2 = add.commute [THEN diff_eq_eq [THEN iffD2]]
   180 
   181 lemmas rdmods [symmetric] = mod_minus_eq
   182   mod_diff_left_eq mod_diff_right_eq mod_add_left_eq
   183   mod_add_right_eq mod_mult_right_eq mod_mult_left_eq
   184 
   185 lemma mod_plus_right:
   186   "((a + x) mod m = (b + x) mod m) = (a mod m = b mod (m :: nat))"
   187   apply (induct x)
   188    apply (simp_all add: mod_Suc)
   189   apply arith
   190   done
   191 
   192 lemma nat_minus_mod: "(n - n mod m) mod m = (0 :: nat)"
   193   by (induct n) (simp_all add : mod_Suc)
   194 
   195 lemmas nat_minus_mod_plus_right = trans [OF nat_minus_mod mod_0 [symmetric],
   196   THEN mod_plus_right [THEN iffD2], simplified]
   197 
   198 lemmas push_mods' = mod_add_eq
   199   mod_mult_eq mod_diff_eq
   200   mod_minus_eq
   201 
   202 lemmas push_mods = push_mods' [THEN eq_reflection]
   203 lemmas pull_mods = push_mods [symmetric] rdmods [THEN eq_reflection]
   204 lemmas mod_simps = 
   205   mod_mult_self2_is_0 [THEN eq_reflection]
   206   mod_mult_self1_is_0 [THEN eq_reflection]
   207   mod_mod_trivial [THEN eq_reflection]
   208 
   209 lemma nat_mod_eq:
   210   "!!b. b < n ==> a mod n = b mod n ==> a mod n = (b :: nat)" 
   211   by (induct a) auto
   212 
   213 lemmas nat_mod_eq' = refl [THEN [2] nat_mod_eq]
   214 
   215 lemma nat_mod_lem: 
   216   "(0 :: nat) < n ==> b < n = (b mod n = b)"
   217   apply safe
   218    apply (erule nat_mod_eq')
   219   apply (erule subst)
   220   apply (erule mod_less_divisor)
   221   done
   222 
   223 lemma mod_nat_add: 
   224   "(x :: nat) < z ==> y < z ==> 
   225    (x + y) mod z = (if x + y < z then x + y else x + y - z)"
   226   apply (rule nat_mod_eq)
   227    apply auto
   228   apply (rule trans)
   229    apply (rule le_mod_geq)
   230    apply simp
   231   apply (rule nat_mod_eq')
   232   apply arith
   233   done
   234 
   235 lemma mod_nat_sub: 
   236   "(x :: nat) < z ==> (x - y) mod z = x - y"
   237   by (rule nat_mod_eq') arith
   238 
   239 lemma int_mod_eq:
   240   "(0 :: int) <= b ==> b < n ==> a mod n = b mod n ==> a mod n = b"
   241   by (metis mod_pos_pos_trivial)
   242 
   243 lemmas int_mod_eq' = mod_pos_pos_trivial (* FIXME delete *)
   244 
   245 lemma int_mod_le: "(0::int) <= a ==> a mod n <= a"
   246   by (fact Divides.semiring_numeral_div_class.mod_less_eq_dividend) (* FIXME: delete *)
   247 
   248 lemma mod_add_if_z:
   249   "(x :: int) < z ==> y < z ==> 0 <= y ==> 0 <= x ==> 0 <= z ==> 
   250    (x + y) mod z = (if x + y < z then x + y else x + y - z)"
   251   by (auto intro: int_mod_eq)
   252 
   253 lemma mod_sub_if_z:
   254   "(x :: int) < z ==> y < z ==> 0 <= y ==> 0 <= x ==> 0 <= z ==> 
   255    (x - y) mod z = (if y <= x then x - y else x - y + z)"
   256   by (auto intro: int_mod_eq)
   257 
   258 lemmas zmde = zmod_zdiv_equality [THEN diff_eq_eq [THEN iffD2], symmetric]
   259 lemmas mcl = mult_cancel_left [THEN iffD1, THEN make_pos_rule]
   260 
   261 (* already have this for naturals, div_mult_self1/2, but not for ints *)
   262 lemma zdiv_mult_self: "m ~= (0 :: int) ==> (a + m * n) div m = a div m + n"
   263   apply (rule mcl)
   264    prefer 2
   265    apply (erule asm_rl)
   266   apply (simp add: zmde ring_distribs)
   267   done
   268 
   269 lemma mod_power_lem:
   270   "a > 1 ==> a ^ n mod a ^ m = (if m <= n then 0 else (a :: int) ^ n)"
   271   apply clarsimp
   272   apply safe
   273    apply (simp add: dvd_eq_mod_eq_0 [symmetric])
   274    apply (drule le_iff_add [THEN iffD1])
   275    apply (force simp: power_add)
   276   apply (rule mod_pos_pos_trivial)
   277    apply (simp)
   278   apply (rule power_strict_increasing)
   279    apply auto
   280   done
   281 
   282 lemma pl_pl_rels: 
   283   "a + b = c + d ==> 
   284    a >= c & b <= d | a <= c & b >= (d :: nat)" by arith
   285 
   286 lemmas pl_pl_rels' = add.commute [THEN [2] trans, THEN pl_pl_rels]
   287 
   288 lemma minus_eq: "(m - k = m) = (k = 0 | m = (0 :: nat))"  by arith
   289 
   290 lemma pl_pl_mm: "(a :: nat) + b = c + d ==> a - c = d - b"  by arith
   291 
   292 lemmas pl_pl_mm' = add.commute [THEN [2] trans, THEN pl_pl_mm]
   293 
   294 lemmas dme = box_equals [OF div_mod_equality add_0_right add_0_right]
   295 lemmas dtle = xtr3 [OF dme [symmetric] le_add1]
   296 lemmas th2 = order_trans [OF order_refl [THEN [2] mult_le_mono] dtle]
   297 
   298 lemma td_gal: 
   299   "0 < c ==> (a >= b * c) = (a div c >= (b :: nat))"
   300   apply safe
   301    apply (erule (1) xtr4 [OF div_le_mono div_mult_self_is_m])
   302   apply (erule th2)
   303   done
   304   
   305 lemmas td_gal_lt = td_gal [simplified not_less [symmetric], simplified]
   306 
   307 lemma div_mult_le: "(a :: nat) div b * b <= a"
   308   by (fact dtle)
   309 
   310 lemmas sdl = split_div_lemma [THEN iffD1, symmetric]
   311 
   312 lemma given_quot: "f > (0 :: nat) ==> (f * l + (f - 1)) div f = l"
   313   by (rule sdl, assumption) (simp (no_asm))
   314 
   315 lemma given_quot_alt: "f > (0 :: nat) ==> (l * f + f - Suc 0) div f = l"
   316   apply (frule given_quot)
   317   apply (rule trans)
   318    prefer 2
   319    apply (erule asm_rl)
   320   apply (rule_tac f="%n. n div f" in arg_cong)
   321   apply (simp add : ac_simps)
   322   done
   323     
   324 lemma diff_mod_le: "(a::nat) < d ==> b dvd d ==> a - a mod b <= d - b"
   325   apply (unfold dvd_def)
   326   apply clarify
   327   apply (case_tac k)
   328    apply clarsimp
   329   apply clarify
   330   apply (cases "b > 0")
   331    apply (drule mult.commute [THEN xtr1])
   332    apply (frule (1) td_gal_lt [THEN iffD1])
   333    apply (clarsimp simp: le_simps)
   334    apply (rule mult_div_cancel [THEN [2] xtr4])
   335    apply (rule mult_mono)
   336       apply auto
   337   done
   338 
   339 lemma less_le_mult':
   340   "w * c < b * c ==> 0 \<le> c ==> (w + 1) * c \<le> b * (c::int)"
   341   apply (rule mult_right_mono)
   342    apply (rule zless_imp_add1_zle)
   343    apply (erule (1) mult_right_less_imp_less)
   344   apply assumption
   345   done
   346 
   347 lemma less_le_mult:
   348   "w * c < b * c \<Longrightarrow> 0 \<le> c \<Longrightarrow> w * c + c \<le> b * (c::int)"
   349   using less_le_mult' [of w c b] by (simp add: algebra_simps)
   350 
   351 lemmas less_le_mult_minus = iffD2 [OF le_diff_eq less_le_mult, 
   352   simplified left_diff_distrib]
   353 
   354 lemma gen_minus: "0 < n ==> f n = f (Suc (n - 1))"
   355   by auto
   356 
   357 lemma mpl_lem: "j <= (i :: nat) ==> k < j ==> i - j + k < i" by arith
   358 
   359 lemma nonneg_mod_div:
   360   "0 <= a ==> 0 <= b ==> 0 <= (a mod b :: int) & 0 <= a div b"
   361   apply (cases "b = 0", clarsimp)
   362   apply (auto intro: pos_imp_zdiv_nonneg_iff [THEN iffD2])
   363   done
   364 
   365 declare iszero_0 [intro]
   366 
   367 lemma min_pm [simp]:
   368   "min a b + (a - b) = (a :: nat)"
   369   by arith
   370   
   371 lemma min_pm1 [simp]:
   372   "a - b + min a b = (a :: nat)"
   373   by arith
   374 
   375 lemma rev_min_pm [simp]:
   376   "min b a + (a - b) = (a :: nat)"
   377   by arith
   378 
   379 lemma rev_min_pm1 [simp]:
   380   "a - b + min b a = (a :: nat)"
   381   by arith
   382 
   383 lemma min_minus [simp]:
   384   "min m (m - k) = (m - k :: nat)"
   385   by arith
   386   
   387 lemma min_minus' [simp]:
   388   "min (m - k) m = (m - k :: nat)"
   389   by arith
   390 
   391 end
   392