src/HOL/Word/Word_Miscellaneous.thy
author paulson <lp15@cam.ac.uk>
Tue Apr 25 16:39:54 2017 +0100 (2017-04-25)
changeset 65578 e4997c181cce
parent 65363 5eb619751b14
child 66453 cc19f7ca2ed6
permissions -rw-r--r--
New material from PNT proof, as well as more default [simp] declarations. Also removed duplicate theorems about geometric series
     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 "~~/src/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 y: "\<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 y)
    36   apply fastforce
    37   done
    38 
    39 lemma list_exhaust_size_eq0:
    40   assumes y: "y = [] \<Longrightarrow> P"
    41   shows "length y = 0 \<Longrightarrow> P"
    42   apply (cases y)
    43    apply (rule y)
    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> "simplifications for specific word lengths"
    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 (* note - if_Cons can cause blowup in the size, if p is complex,
    99   so make a simproc *)
   100 lemma if_Cons: "(if p then x # xs else y # ys) = If p x y # If p xs ys"
   101   by auto
   102 
   103 lemma if_single: "(if xc then [xab] else [an]) = [if xc then xab else an]"
   104   by auto
   105 
   106 lemma if_bool_simps:
   107   "If p True y = (p \<or> y) \<and> If p False y = (\<not> p \<and> y) \<and>
   108     If p y True = (p \<longrightarrow> y) \<and> If p y False = (p \<and> y)"
   109   by auto
   110 
   111 lemmas if_simps =
   112   if_x_Not if_Not_x if_cancel if_True if_False if_bool_simps
   113 
   114 lemmas seqr = eq_reflection [where x = "size w"] for w (* FIXME: delete *)
   115 
   116 lemma the_elemI: "y = {x} \<Longrightarrow> the_elem y = x"
   117   by simp
   118 
   119 lemma nonemptyE: "S \<noteq> {} \<Longrightarrow> (\<And>x. x \<in> S \<Longrightarrow> R) \<Longrightarrow> R"
   120   by auto
   121 
   122 lemma gt_or_eq_0: "0 < y \<or> 0 = y"
   123   for y :: nat
   124   by arith
   125 
   126 lemmas xtr1 = xtrans(1)
   127 lemmas xtr2 = xtrans(2)
   128 lemmas xtr3 = xtrans(3)
   129 lemmas xtr4 = xtrans(4)
   130 lemmas xtr5 = xtrans(5)
   131 lemmas xtr6 = xtrans(6)
   132 lemmas xtr7 = xtrans(7)
   133 lemmas xtr8 = xtrans(8)
   134 
   135 lemmas nat_simps = diff_add_inverse2 diff_add_inverse
   136 lemmas nat_iffs = le_add1 le_add2
   137 
   138 lemma sum_imp_diff: "j = k + i \<Longrightarrow> j - i = k"
   139   for k :: nat
   140   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 = 0 \<or> n mod 2 = 1"
   146   for n :: int
   147   by arith
   148 
   149 lemmas eme1p = emep1 [simplified add.commute]
   150 
   151 lemma le_diff_eq': "a \<le> c - b \<longleftrightarrow> b + a \<le> c"
   152   for a b c :: int
   153   by arith
   154 
   155 lemma less_diff_eq': "a < c - b \<longleftrightarrow> b + a < c"
   156   for a b c :: int
   157   by arith
   158 
   159 lemma diff_less_eq': "a - b < c \<longleftrightarrow> a < b + c"
   160   for a b c :: int
   161   by arith
   162 
   163 lemmas m1mod22k = mult_pos_pos [OF zless2 zless2p, THEN zmod_minus1]
   164 
   165 lemma z1pdiv2: "(2 * b + 1) div 2 = b"
   166   for b :: int
   167   by arith
   168 
   169 lemmas zdiv_le_dividend = xtr3 [OF div_by_1 [symmetric] zdiv_mono2,
   170   simplified int_one_le_iff_zero_less, simplified]
   171 
   172 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"
   173   for a b m n :: int
   174   by arith
   175 
   176 lemma axxmod2: "(1 + x + x) mod 2 = 1 \<and> (0 + x + x) mod 2 = 0"
   177   for x :: int
   178   by arith
   179 
   180 lemma axxdiv2: "(1 + x + x) div 2 = x \<and> (0 + x + x) div 2 = x"
   181   for x :: int
   182   by arith
   183 
   184 lemmas iszero_minus =
   185   trans [THEN trans, OF iszero_def neg_equal_0_iff_equal iszero_def [symmetric]]
   186 
   187 lemmas zadd_diff_inverse =
   188   trans [OF diff_add_cancel [symmetric] add.commute]
   189 
   190 lemmas add_diff_cancel2 =
   191   add.commute [THEN diff_eq_eq [THEN iffD2]]
   192 
   193 lemmas rdmods [symmetric] = mod_minus_eq
   194   mod_diff_left_eq mod_diff_right_eq mod_add_left_eq
   195   mod_add_right_eq mod_mult_right_eq mod_mult_left_eq
   196 
   197 lemma mod_plus_right: "(a + x) mod m = (b + x) mod m \<longleftrightarrow> a mod m = b mod m"
   198   for a b m x :: nat
   199   by (induct x) (simp_all add: mod_Suc, arith)
   200 
   201 lemma nat_minus_mod: "(n - n mod m) mod m = 0"
   202   for m n :: nat
   203   by (induct n) (simp_all add: mod_Suc)
   204 
   205 lemmas nat_minus_mod_plus_right =
   206   trans [OF nat_minus_mod mod_0 [symmetric],
   207     THEN mod_plus_right [THEN iffD2], simplified]
   208 
   209 lemmas push_mods' = mod_add_eq
   210   mod_mult_eq mod_diff_eq
   211   mod_minus_eq
   212 
   213 lemmas push_mods = push_mods' [THEN eq_reflection]
   214 lemmas pull_mods = push_mods [symmetric] rdmods [THEN eq_reflection]
   215 
   216 lemma nat_mod_eq: "b < n \<Longrightarrow> a mod n = b mod n \<Longrightarrow> a mod n = b"
   217   for a b n :: nat
   218   by (induct a) auto
   219 
   220 lemmas nat_mod_eq' = refl [THEN [2] nat_mod_eq]
   221 
   222 lemma nat_mod_lem: "0 < n \<Longrightarrow> b < n \<longleftrightarrow> b mod n = b"
   223   for b n :: nat
   224   apply safe
   225    apply (erule nat_mod_eq')
   226   apply (erule subst)
   227   apply (erule mod_less_divisor)
   228   done
   229 
   230 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)"
   231   for x y z :: nat
   232   apply (rule nat_mod_eq)
   233    apply auto
   234   apply (rule trans)
   235    apply (rule le_mod_geq)
   236    apply simp
   237   apply (rule nat_mod_eq')
   238   apply arith
   239   done
   240 
   241 lemma mod_nat_sub: "x < z \<Longrightarrow> (x - y) mod z = x - y"
   242   for x y :: nat
   243   by (rule nat_mod_eq') arith
   244 
   245 lemma int_mod_eq: "0 \<le> b \<Longrightarrow> b < n \<Longrightarrow> a mod n = b mod n \<Longrightarrow> a mod n = b"
   246   for a b n :: int
   247   by (metis mod_pos_pos_trivial)
   248 
   249 lemmas int_mod_eq' = mod_pos_pos_trivial (* FIXME delete *)
   250 
   251 lemma int_mod_le: "0 \<le> a \<Longrightarrow> a mod n \<le> a"
   252   for a :: int
   253   by (fact Divides.semiring_numeral_div_class.mod_less_eq_dividend) (* FIXME: delete *)
   254 
   255 lemma mod_add_if_z:
   256   "x < z \<Longrightarrow> y < z \<Longrightarrow> 0 \<le> y \<Longrightarrow> 0 \<le> x \<Longrightarrow> 0 \<le> z \<Longrightarrow>
   257     (x + y) mod z = (if x + y < z then x + y else x + y - z)"
   258   for x y z :: int
   259   by (auto intro: int_mod_eq)
   260 
   261 lemma mod_sub_if_z:
   262   "x < z \<Longrightarrow> y < z \<Longrightarrow> 0 \<le> y \<Longrightarrow> 0 \<le> x \<Longrightarrow> 0 \<le> z \<Longrightarrow>
   263     (x - y) mod z = (if y \<le> x then x - y else x - y + z)"
   264   for x y z :: int
   265   by (auto intro: int_mod_eq)
   266 
   267 lemmas zmde = mult_div_mod_eq [symmetric, THEN diff_eq_eq [THEN iffD2], symmetric]
   268 lemmas mcl = mult_cancel_left [THEN iffD1, THEN make_pos_rule]
   269 
   270 (* already have this for naturals, div_mult_self1/2, but not for ints *)
   271 lemma zdiv_mult_self: "m \<noteq> 0 \<Longrightarrow> (a + m * n) div m = a div m + n"
   272   for a m n :: int
   273   apply (rule mcl)
   274    prefer 2
   275    apply (erule asm_rl)
   276   apply (simp add: zmde ring_distribs)
   277   done
   278 
   279 lemma mod_power_lem: "a > 1 \<Longrightarrow> a ^ n mod a ^ m = (if m \<le> n then 0 else a ^ n)"
   280   for a :: int
   281   apply clarsimp
   282   apply safe
   283    apply (simp add: dvd_eq_mod_eq_0 [symmetric])
   284    apply (drule le_iff_add [THEN iffD1])
   285    apply (force simp: power_add)
   286   apply (rule mod_pos_pos_trivial)
   287    apply (simp)
   288   apply (rule power_strict_increasing)
   289    apply auto
   290   done
   291 
   292 lemma pl_pl_rels: "a + b = c + d \<Longrightarrow> a \<ge> c \<and> b \<le> d \<or> a \<le> c \<and> b \<ge> d"
   293   for a b c d :: nat
   294   by arith
   295 
   296 lemmas pl_pl_rels' = add.commute [THEN [2] trans, THEN pl_pl_rels]
   297 
   298 lemma minus_eq: "m - k = m \<longleftrightarrow> k = 0 \<or> m = 0"
   299   for k m :: nat
   300   by arith
   301 
   302 lemma pl_pl_mm: "a + b = c + d \<Longrightarrow> a - c = d - b"
   303   for a b c d :: nat
   304   by arith
   305 
   306 lemmas pl_pl_mm' = add.commute [THEN [2] trans, THEN pl_pl_mm]
   307 
   308 lemmas dme = div_mult_mod_eq
   309 lemmas dtle = xtr3 [OF dme [symmetric] le_add1]
   310 lemmas th2 = order_trans [OF order_refl [THEN [2] mult_le_mono] dtle]
   311 
   312 lemma td_gal: "0 < c \<Longrightarrow> a \<ge> b * c \<longleftrightarrow> a div c \<ge> b"
   313   for a b c :: nat
   314   apply safe
   315    apply (erule (1) xtr4 [OF div_le_mono div_mult_self_is_m])
   316   apply (erule th2)
   317   done
   318 
   319 lemmas td_gal_lt = td_gal [simplified not_less [symmetric], simplified]
   320 
   321 lemma div_mult_le: "a div b * b \<le> a"
   322   for a b :: nat
   323   by (fact dtle)
   324 
   325 lemmas sdl = split_div_lemma [THEN iffD1, symmetric]
   326 
   327 lemma given_quot: "f > 0 \<Longrightarrow> (f * l + (f - 1)) div f = l"
   328   for f l :: nat
   329   by (rule sdl, assumption) (simp (no_asm))
   330 
   331 lemma given_quot_alt: "f > 0 \<Longrightarrow> (l * f + f - Suc 0) div f = l"
   332   for f l :: nat
   333   apply (frule given_quot)
   334   apply (rule trans)
   335    prefer 2
   336    apply (erule asm_rl)
   337   apply (rule_tac f="\<lambda>n. n div f" in arg_cong)
   338   apply (simp add : ac_simps)
   339   done
   340 
   341 lemma diff_mod_le: "a < d \<Longrightarrow> b dvd d \<Longrightarrow> a - a mod b \<le> d - b"
   342   for a b d :: nat
   343   apply (unfold dvd_def)
   344   apply clarify
   345   apply (case_tac k)
   346    apply clarsimp
   347   apply clarify
   348   apply (cases "b > 0")
   349    apply (drule mult.commute [THEN xtr1])
   350    apply (frule (1) td_gal_lt [THEN iffD1])
   351    apply (clarsimp simp: le_simps)
   352    apply (rule minus_mod_eq_mult_div [symmetric, THEN [2] xtr4])
   353    apply (rule mult_mono)
   354       apply auto
   355   done
   356 
   357 lemma less_le_mult': "w * c < b * c \<Longrightarrow> 0 \<le> c \<Longrightarrow> (w + 1) * c \<le> b * c"
   358   for b c w :: int
   359   apply (rule mult_right_mono)
   360    apply (rule zless_imp_add1_zle)
   361    apply (erule (1) mult_right_less_imp_less)
   362   apply assumption
   363   done
   364 
   365 lemma less_le_mult: "w * c < b * c \<Longrightarrow> 0 \<le> c \<Longrightarrow> w * c + c \<le> b * c"
   366   for b c w :: int
   367   using less_le_mult' [of w c b] by (simp add: algebra_simps)
   368 
   369 lemmas less_le_mult_minus = iffD2 [OF le_diff_eq less_le_mult,
   370   simplified left_diff_distrib]
   371 
   372 lemma gen_minus: "0 < n \<Longrightarrow> f n = f (Suc (n - 1))"
   373   by auto
   374 
   375 lemma mpl_lem: "j \<le> i \<Longrightarrow> k < j \<Longrightarrow> i - j + k < i"
   376   for i j k :: nat
   377   by arith
   378 
   379 lemma nonneg_mod_div: "0 \<le> a \<Longrightarrow> 0 \<le> b \<Longrightarrow> 0 \<le> (a mod b) \<and> 0 \<le> a div b"
   380   for a b :: int
   381   by (cases "b = 0") (auto intro: pos_imp_zdiv_nonneg_iff [THEN iffD2])
   382 
   383 declare iszero_0 [intro]
   384 
   385 lemma min_pm [simp]: "min a b + (a - b) = a"
   386   for a b :: nat
   387   by arith
   388 
   389 lemma min_pm1 [simp]: "a - b + min a b = a"
   390   for a b :: nat
   391   by arith
   392 
   393 lemma rev_min_pm [simp]: "min b a + (a - b) = a"
   394   for a b :: nat
   395   by arith
   396 
   397 lemma rev_min_pm1 [simp]: "a - b + min b a = a"
   398   for a b :: nat
   399   by arith
   400 
   401 lemma min_minus [simp]: "min m (m - k) = m - k"
   402   for m k :: nat
   403   by arith
   404 
   405 lemma min_minus' [simp]: "min (m - k) m = m - k"
   406   for m k :: nat
   407   by arith
   408 
   409 end