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