src/HOL/Word/Word_Miscellaneous.thy
 author haftmann Sun Oct 08 22:28:21 2017 +0200 (20 months ago) changeset 66808 1907167b6038 parent 66801 f3fda9777f9a child 66886 960509bfd47e permissions -rw-r--r--
elementary definition of division on natural numbers
```     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 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 lemmas int_mod_le = zmod_le_nonneg_dividend (* FIXME: delete *)
```
```   252
```
```   253 lemma mod_add_if_z:
```
```   254   "x < z \<Longrightarrow> y < z \<Longrightarrow> 0 \<le> y \<Longrightarrow> 0 \<le> x \<Longrightarrow> 0 \<le> z \<Longrightarrow>
```
```   255     (x + y) mod z = (if x + y < z then x + y else x + y - z)"
```
```   256   for x y z :: int
```
```   257   by (auto intro: int_mod_eq)
```
```   258
```
```   259 lemma mod_sub_if_z:
```
```   260   "x < z \<Longrightarrow> y < z \<Longrightarrow> 0 \<le> y \<Longrightarrow> 0 \<le> x \<Longrightarrow> 0 \<le> z \<Longrightarrow>
```
```   261     (x - y) mod z = (if y \<le> x then x - y else x - y + z)"
```
```   262   for x y z :: int
```
```   263   by (auto intro: int_mod_eq)
```
```   264
```
```   265 lemmas zmde = mult_div_mod_eq [symmetric, THEN diff_eq_eq [THEN iffD2], symmetric]
```
```   266 lemmas mcl = mult_cancel_left [THEN iffD1, THEN make_pos_rule]
```
```   267
```
```   268 (* already have this for naturals, div_mult_self1/2, but not for ints *)
```
```   269 lemma zdiv_mult_self: "m \<noteq> 0 \<Longrightarrow> (a + m * n) div m = a div m + n"
```
```   270   for a m n :: int
```
```   271   apply (rule mcl)
```
```   272    prefer 2
```
```   273    apply (erule asm_rl)
```
```   274   apply (simp add: zmde ring_distribs)
```
```   275   done
```
```   276
```
```   277 lemma mod_power_lem: "a > 1 \<Longrightarrow> a ^ n mod a ^ m = (if m \<le> n then 0 else a ^ n)"
```
```   278   for a :: int
```
```   279   apply clarsimp
```
```   280   apply safe
```
```   281    apply (simp add: dvd_eq_mod_eq_0 [symmetric])
```
```   282    apply (drule le_iff_add [THEN iffD1])
```
```   283    apply (force simp: power_add)
```
```   284   apply (rule mod_pos_pos_trivial)
```
```   285    apply (simp)
```
```   286   apply (rule power_strict_increasing)
```
```   287    apply auto
```
```   288   done
```
```   289
```
```   290 lemma pl_pl_rels: "a + b = c + d \<Longrightarrow> a \<ge> c \<and> b \<le> d \<or> a \<le> c \<and> b \<ge> d"
```
```   291   for a b c d :: nat
```
```   292   by arith
```
```   293
```
```   294 lemmas pl_pl_rels' = add.commute [THEN [2] trans, THEN pl_pl_rels]
```
```   295
```
```   296 lemma minus_eq: "m - k = m \<longleftrightarrow> k = 0 \<or> m = 0"
```
```   297   for k m :: nat
```
```   298   by arith
```
```   299
```
```   300 lemma pl_pl_mm: "a + b = c + d \<Longrightarrow> a - c = d - b"
```
```   301   for a b c d :: nat
```
```   302   by arith
```
```   303
```
```   304 lemmas pl_pl_mm' = add.commute [THEN [2] trans, THEN pl_pl_mm]
```
```   305
```
```   306 lemmas dme = div_mult_mod_eq
```
```   307 lemmas dtle = div_times_less_eq_dividend
```
```   308 lemmas th2 = order_trans [OF order_refl [THEN [2] mult_le_mono] div_times_less_eq_dividend]
```
```   309
```
```   310 lemma td_gal: "0 < c \<Longrightarrow> a \<ge> b * c \<longleftrightarrow> a div c \<ge> b"
```
```   311   for a b c :: nat
```
```   312   apply safe
```
```   313    apply (erule (1) xtr4 [OF div_le_mono div_mult_self_is_m])
```
```   314   apply (erule th2)
```
```   315   done
```
```   316
```
```   317 lemmas td_gal_lt = td_gal [simplified not_less [symmetric], simplified]
```
```   318
```
```   319 lemmas div_mult_le = div_times_less_eq_dividend
```
```   320
```
```   321 lemmas sdl = div_nat_eqI
```
```   322
```
```   323 lemma given_quot: "f > 0 \<Longrightarrow> (f * l + (f - 1)) div f = l"
```
```   324   for f l :: nat
```
```   325   by (rule div_nat_eqI) (simp_all)
```
```   326
```
```   327 lemma given_quot_alt: "f > 0 \<Longrightarrow> (l * f + f - Suc 0) div f = l"
```
```   328   for f l :: nat
```
```   329   apply (frule given_quot)
```
```   330   apply (rule trans)
```
```   331    prefer 2
```
```   332    apply (erule asm_rl)
```
```   333   apply (rule_tac f="\<lambda>n. n div f" in arg_cong)
```
```   334   apply (simp add : ac_simps)
```
```   335   done
```
```   336
```
```   337 lemma diff_mod_le: "a < d \<Longrightarrow> b dvd d \<Longrightarrow> a - a mod b \<le> d - b"
```
```   338   for a b d :: nat
```
```   339   apply (unfold dvd_def)
```
```   340   apply clarify
```
```   341   apply (case_tac k)
```
```   342    apply clarsimp
```
```   343   apply clarify
```
```   344   apply (cases "b > 0")
```
```   345    apply (drule mult.commute [THEN xtr1])
```
```   346    apply (frule (1) td_gal_lt [THEN iffD1])
```
```   347    apply (clarsimp simp: le_simps)
```
```   348    apply (rule minus_mod_eq_mult_div [symmetric, THEN [2] xtr4])
```
```   349    apply (rule mult_mono)
```
```   350       apply auto
```
```   351   done
```
```   352
```
```   353 lemma less_le_mult': "w * c < b * c \<Longrightarrow> 0 \<le> c \<Longrightarrow> (w + 1) * c \<le> b * c"
```
```   354   for b c w :: int
```
```   355   apply (rule mult_right_mono)
```
```   356    apply (rule zless_imp_add1_zle)
```
```   357    apply (erule (1) mult_right_less_imp_less)
```
```   358   apply assumption
```
```   359   done
```
```   360
```
```   361 lemma less_le_mult: "w * c < b * c \<Longrightarrow> 0 \<le> c \<Longrightarrow> w * c + c \<le> b * c"
```
```   362   for b c w :: int
```
```   363   using less_le_mult' [of w c b] by (simp add: algebra_simps)
```
```   364
```
```   365 lemmas less_le_mult_minus = iffD2 [OF le_diff_eq less_le_mult,
```
```   366   simplified left_diff_distrib]
```
```   367
```
```   368 lemma gen_minus: "0 < n \<Longrightarrow> f n = f (Suc (n - 1))"
```
```   369   by auto
```
```   370
```
```   371 lemma mpl_lem: "j \<le> i \<Longrightarrow> k < j \<Longrightarrow> i - j + k < i"
```
```   372   for i j k :: nat
```
```   373   by arith
```
```   374
```
```   375 lemma nonneg_mod_div: "0 \<le> a \<Longrightarrow> 0 \<le> b \<Longrightarrow> 0 \<le> (a mod b) \<and> 0 \<le> a div b"
```
```   376   for a b :: int
```
```   377   by (cases "b = 0") (auto intro: pos_imp_zdiv_nonneg_iff [THEN iffD2])
```
```   378
```
```   379 declare iszero_0 [intro]
```
```   380
```
```   381 lemma min_pm [simp]: "min a b + (a - b) = a"
```
```   382   for a b :: nat
```
```   383   by arith
```
```   384
```
```   385 lemma min_pm1 [simp]: "a - b + min a b = a"
```
```   386   for a b :: nat
```
```   387   by arith
```
```   388
```
```   389 lemma rev_min_pm [simp]: "min b a + (a - b) = a"
```
```   390   for a b :: nat
```
```   391   by arith
```
```   392
```
```   393 lemma rev_min_pm1 [simp]: "a - b + min b a = a"
```
```   394   for a b :: nat
```
```   395   by arith
```
```   396
```
```   397 lemma min_minus [simp]: "min m (m - k) = m - k"
```
```   398   for m k :: nat
```
```   399   by arith
```
```   400
```
```   401 lemma min_minus' [simp]: "min (m - k) m = m - k"
```
```   402   for m k :: nat
```
```   403   by arith
```
```   404
```
```   405 end
```