src/HOL/Word/Misc_Auxiliary.thy
author nipkow
Tue, 05 Nov 2019 21:07:03 +0100
changeset 71044 cb504351d058
parent 70193 49a65e3f04c9
child 71997 4a013c92a091
permissions -rw-r--r--
tuned
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
70190
ff9efdc84289 clarified structure of theories
haftmann
parents:
diff changeset
     1
(*  Title:      HOL/Word/Misc_Auxiliary.thy
ff9efdc84289 clarified structure of theories
haftmann
parents:
diff changeset
     2
    Author:     Jeremy Dawson, NICTA
ff9efdc84289 clarified structure of theories
haftmann
parents:
diff changeset
     3
*)
ff9efdc84289 clarified structure of theories
haftmann
parents:
diff changeset
     4
ff9efdc84289 clarified structure of theories
haftmann
parents:
diff changeset
     5
section \<open>Generic auxiliary\<close>
ff9efdc84289 clarified structure of theories
haftmann
parents:
diff changeset
     6
ff9efdc84289 clarified structure of theories
haftmann
parents:
diff changeset
     7
theory Misc_Auxiliary
ff9efdc84289 clarified structure of theories
haftmann
parents:
diff changeset
     8
  imports Main
ff9efdc84289 clarified structure of theories
haftmann
parents:
diff changeset
     9
begin
ff9efdc84289 clarified structure of theories
haftmann
parents:
diff changeset
    10
ff9efdc84289 clarified structure of theories
haftmann
parents:
diff changeset
    11
subsection \<open>Arithmetic lemmas\<close>
ff9efdc84289 clarified structure of theories
haftmann
parents:
diff changeset
    12
ff9efdc84289 clarified structure of theories
haftmann
parents:
diff changeset
    13
lemma int_mod_lem: "0 < n \<Longrightarrow> 0 \<le> b \<and> b < n \<longleftrightarrow> b mod n = b"
ff9efdc84289 clarified structure of theories
haftmann
parents:
diff changeset
    14
  for b n :: int
ff9efdc84289 clarified structure of theories
haftmann
parents:
diff changeset
    15
  apply safe
ff9efdc84289 clarified structure of theories
haftmann
parents:
diff changeset
    16
    apply (erule (1) mod_pos_pos_trivial)
ff9efdc84289 clarified structure of theories
haftmann
parents:
diff changeset
    17
   apply (erule_tac [!] subst)
ff9efdc84289 clarified structure of theories
haftmann
parents:
diff changeset
    18
   apply auto
ff9efdc84289 clarified structure of theories
haftmann
parents:
diff changeset
    19
  done
ff9efdc84289 clarified structure of theories
haftmann
parents:
diff changeset
    20
ff9efdc84289 clarified structure of theories
haftmann
parents:
diff changeset
    21
lemma int_mod_ge: "a < n \<Longrightarrow> 0 < n \<Longrightarrow> a \<le> a mod n"
ff9efdc84289 clarified structure of theories
haftmann
parents:
diff changeset
    22
  for a n :: int
ff9efdc84289 clarified structure of theories
haftmann
parents:
diff changeset
    23
  by (metis dual_order.trans le_cases mod_pos_pos_trivial pos_mod_conj)
ff9efdc84289 clarified structure of theories
haftmann
parents:
diff changeset
    24
ff9efdc84289 clarified structure of theories
haftmann
parents:
diff changeset
    25
lemma int_mod_ge': "b < 0 \<Longrightarrow> 0 < n \<Longrightarrow> b + n \<le> b mod n"
ff9efdc84289 clarified structure of theories
haftmann
parents:
diff changeset
    26
  for b n :: int
ff9efdc84289 clarified structure of theories
haftmann
parents:
diff changeset
    27
  by (metis add_less_same_cancel2 int_mod_ge mod_add_self2)
ff9efdc84289 clarified structure of theories
haftmann
parents:
diff changeset
    28
ff9efdc84289 clarified structure of theories
haftmann
parents:
diff changeset
    29
lemma int_mod_le': "0 \<le> b - n \<Longrightarrow> b mod n \<le> b - n"
ff9efdc84289 clarified structure of theories
haftmann
parents:
diff changeset
    30
  for b n :: int
ff9efdc84289 clarified structure of theories
haftmann
parents:
diff changeset
    31
  by (metis minus_mod_self2 zmod_le_nonneg_dividend)
ff9efdc84289 clarified structure of theories
haftmann
parents:
diff changeset
    32
ff9efdc84289 clarified structure of theories
haftmann
parents:
diff changeset
    33
lemma emep1: "even n \<Longrightarrow> even d \<Longrightarrow> 0 \<le> d \<Longrightarrow> (n + 1) mod d = (n mod d) + 1"
ff9efdc84289 clarified structure of theories
haftmann
parents:
diff changeset
    34
  for n d :: int
ff9efdc84289 clarified structure of theories
haftmann
parents:
diff changeset
    35
  by (auto simp add: pos_zmod_mult_2 add.commute dvd_def)
ff9efdc84289 clarified structure of theories
haftmann
parents:
diff changeset
    36
ff9efdc84289 clarified structure of theories
haftmann
parents:
diff changeset
    37
lemma m1mod2k: "- 1 mod 2 ^ n = (2 ^ n - 1 :: int)"
ff9efdc84289 clarified structure of theories
haftmann
parents:
diff changeset
    38
  by (rule zmod_minus1) simp
ff9efdc84289 clarified structure of theories
haftmann
parents:
diff changeset
    39
ff9efdc84289 clarified structure of theories
haftmann
parents:
diff changeset
    40
lemma sub_inc_One: "Num.sub (Num.inc n) num.One = numeral n"
ff9efdc84289 clarified structure of theories
haftmann
parents:
diff changeset
    41
  by (metis add_diff_cancel add_neg_numeral_special(3) add_uminus_conv_diff numeral_inc)
ff9efdc84289 clarified structure of theories
haftmann
parents:
diff changeset
    42
  
ff9efdc84289 clarified structure of theories
haftmann
parents:
diff changeset
    43
lemma inc_BitM: "Num.inc (Num.BitM n) = num.Bit0 n"
ff9efdc84289 clarified structure of theories
haftmann
parents:
diff changeset
    44
  by (simp add: BitM_plus_one[symmetric] add_One)
ff9efdc84289 clarified structure of theories
haftmann
parents:
diff changeset
    45
ff9efdc84289 clarified structure of theories
haftmann
parents:
diff changeset
    46
ff9efdc84289 clarified structure of theories
haftmann
parents:
diff changeset
    47
subsection \<open>Lemmas on list operations\<close>
ff9efdc84289 clarified structure of theories
haftmann
parents:
diff changeset
    48
ff9efdc84289 clarified structure of theories
haftmann
parents:
diff changeset
    49
lemma butlast_power: "(butlast ^^ n) bl = take (length bl - n) bl"
ff9efdc84289 clarified structure of theories
haftmann
parents:
diff changeset
    50
  by (induct n) (auto simp: butlast_take)
ff9efdc84289 clarified structure of theories
haftmann
parents:
diff changeset
    51
ff9efdc84289 clarified structure of theories
haftmann
parents:
diff changeset
    52
lemma nth_rev: "n < length xs \<Longrightarrow> rev xs ! n = xs ! (length xs - 1 - n)"
ff9efdc84289 clarified structure of theories
haftmann
parents:
diff changeset
    53
  using rev_nth by simp
ff9efdc84289 clarified structure of theories
haftmann
parents:
diff changeset
    54
ff9efdc84289 clarified structure of theories
haftmann
parents:
diff changeset
    55
lemma nth_rev_alt: "n < length ys \<Longrightarrow> ys ! n = rev ys ! (length ys - Suc n)"
ff9efdc84289 clarified structure of theories
haftmann
parents:
diff changeset
    56
  by (simp add: nth_rev)
ff9efdc84289 clarified structure of theories
haftmann
parents:
diff changeset
    57
ff9efdc84289 clarified structure of theories
haftmann
parents:
diff changeset
    58
lemma hd_butlast: "length xs > 1 \<Longrightarrow> hd (butlast xs) = hd xs"
ff9efdc84289 clarified structure of theories
haftmann
parents:
diff changeset
    59
  by (cases xs) auto
ff9efdc84289 clarified structure of theories
haftmann
parents:
diff changeset
    60
ff9efdc84289 clarified structure of theories
haftmann
parents:
diff changeset
    61
ff9efdc84289 clarified structure of theories
haftmann
parents:
diff changeset
    62
subsection \<open>Implicit augmentation of list prefixes\<close>
ff9efdc84289 clarified structure of theories
haftmann
parents:
diff changeset
    63
ff9efdc84289 clarified structure of theories
haftmann
parents:
diff changeset
    64
primrec takefill :: "'a \<Rightarrow> nat \<Rightarrow> 'a list \<Rightarrow> 'a list"
ff9efdc84289 clarified structure of theories
haftmann
parents:
diff changeset
    65
where
ff9efdc84289 clarified structure of theories
haftmann
parents:
diff changeset
    66
    Z: "takefill fill 0 xs = []"
ff9efdc84289 clarified structure of theories
haftmann
parents:
diff changeset
    67
  | Suc: "takefill fill (Suc n) xs =
ff9efdc84289 clarified structure of theories
haftmann
parents:
diff changeset
    68
      (case xs of
ff9efdc84289 clarified structure of theories
haftmann
parents:
diff changeset
    69
        [] \<Rightarrow> fill # takefill fill n xs
ff9efdc84289 clarified structure of theories
haftmann
parents:
diff changeset
    70
      | y # ys \<Rightarrow> y # takefill fill n ys)"
ff9efdc84289 clarified structure of theories
haftmann
parents:
diff changeset
    71
ff9efdc84289 clarified structure of theories
haftmann
parents:
diff changeset
    72
lemma nth_takefill: "m < n \<Longrightarrow> takefill fill n l ! m = (if m < length l then l ! m else fill)"
ff9efdc84289 clarified structure of theories
haftmann
parents:
diff changeset
    73
  apply (induct n arbitrary: m l)
ff9efdc84289 clarified structure of theories
haftmann
parents:
diff changeset
    74
   apply clarsimp
ff9efdc84289 clarified structure of theories
haftmann
parents:
diff changeset
    75
  apply clarsimp
ff9efdc84289 clarified structure of theories
haftmann
parents:
diff changeset
    76
  apply (case_tac m)
ff9efdc84289 clarified structure of theories
haftmann
parents:
diff changeset
    77
   apply (simp split: list.split)
ff9efdc84289 clarified structure of theories
haftmann
parents:
diff changeset
    78
  apply (simp split: list.split)
ff9efdc84289 clarified structure of theories
haftmann
parents:
diff changeset
    79
  done
ff9efdc84289 clarified structure of theories
haftmann
parents:
diff changeset
    80
ff9efdc84289 clarified structure of theories
haftmann
parents:
diff changeset
    81
lemma takefill_alt: "takefill fill n l = take n l @ replicate (n - length l) fill"
ff9efdc84289 clarified structure of theories
haftmann
parents:
diff changeset
    82
  by (induct n arbitrary: l) (auto split: list.split)
ff9efdc84289 clarified structure of theories
haftmann
parents:
diff changeset
    83
ff9efdc84289 clarified structure of theories
haftmann
parents:
diff changeset
    84
lemma takefill_replicate [simp]: "takefill fill n (replicate m fill) = replicate n fill"
ff9efdc84289 clarified structure of theories
haftmann
parents:
diff changeset
    85
  by (simp add: takefill_alt replicate_add [symmetric])
ff9efdc84289 clarified structure of theories
haftmann
parents:
diff changeset
    86
ff9efdc84289 clarified structure of theories
haftmann
parents:
diff changeset
    87
lemma takefill_le': "n = m + k \<Longrightarrow> takefill x m (takefill x n l) = takefill x m l"
ff9efdc84289 clarified structure of theories
haftmann
parents:
diff changeset
    88
  by (induct m arbitrary: l n) (auto split: list.split)
ff9efdc84289 clarified structure of theories
haftmann
parents:
diff changeset
    89
ff9efdc84289 clarified structure of theories
haftmann
parents:
diff changeset
    90
lemma length_takefill [simp]: "length (takefill fill n l) = n"
ff9efdc84289 clarified structure of theories
haftmann
parents:
diff changeset
    91
  by (simp add: takefill_alt)
ff9efdc84289 clarified structure of theories
haftmann
parents:
diff changeset
    92
ff9efdc84289 clarified structure of theories
haftmann
parents:
diff changeset
    93
lemma take_takefill': "n = k + m \<Longrightarrow> take k (takefill fill n w) = takefill fill k w"
ff9efdc84289 clarified structure of theories
haftmann
parents:
diff changeset
    94
  by (induct k arbitrary: w n) (auto split: list.split)
ff9efdc84289 clarified structure of theories
haftmann
parents:
diff changeset
    95
ff9efdc84289 clarified structure of theories
haftmann
parents:
diff changeset
    96
lemma drop_takefill: "drop k (takefill fill (m + k) w) = takefill fill m (drop k w)"
ff9efdc84289 clarified structure of theories
haftmann
parents:
diff changeset
    97
  by (induct k arbitrary: w) (auto split: list.split)
ff9efdc84289 clarified structure of theories
haftmann
parents:
diff changeset
    98
ff9efdc84289 clarified structure of theories
haftmann
parents:
diff changeset
    99
lemma takefill_le [simp]: "m \<le> n \<Longrightarrow> takefill x m (takefill x n l) = takefill x m l"
ff9efdc84289 clarified structure of theories
haftmann
parents:
diff changeset
   100
  by (auto simp: le_iff_add takefill_le')
ff9efdc84289 clarified structure of theories
haftmann
parents:
diff changeset
   101
ff9efdc84289 clarified structure of theories
haftmann
parents:
diff changeset
   102
lemma take_takefill [simp]: "m \<le> n \<Longrightarrow> take m (takefill fill n w) = takefill fill m w"
ff9efdc84289 clarified structure of theories
haftmann
parents:
diff changeset
   103
  by (auto simp: le_iff_add take_takefill')
ff9efdc84289 clarified structure of theories
haftmann
parents:
diff changeset
   104
ff9efdc84289 clarified structure of theories
haftmann
parents:
diff changeset
   105
lemma takefill_append: "takefill fill (m + length xs) (xs @ w) = xs @ (takefill fill m w)"
ff9efdc84289 clarified structure of theories
haftmann
parents:
diff changeset
   106
  by (induct xs) auto
ff9efdc84289 clarified structure of theories
haftmann
parents:
diff changeset
   107
ff9efdc84289 clarified structure of theories
haftmann
parents:
diff changeset
   108
lemma takefill_same': "l = length xs \<Longrightarrow> takefill fill l xs = xs"
ff9efdc84289 clarified structure of theories
haftmann
parents:
diff changeset
   109
  by (induct xs arbitrary: l) auto
ff9efdc84289 clarified structure of theories
haftmann
parents:
diff changeset
   110
ff9efdc84289 clarified structure of theories
haftmann
parents:
diff changeset
   111
lemmas takefill_same [simp] = takefill_same' [OF refl]
ff9efdc84289 clarified structure of theories
haftmann
parents:
diff changeset
   112
ff9efdc84289 clarified structure of theories
haftmann
parents:
diff changeset
   113
lemma tf_rev:
ff9efdc84289 clarified structure of theories
haftmann
parents:
diff changeset
   114
  "n + k = m + length bl \<Longrightarrow> takefill x m (rev (takefill y n bl)) =
ff9efdc84289 clarified structure of theories
haftmann
parents:
diff changeset
   115
    rev (takefill y m (rev (takefill x k (rev bl))))"
ff9efdc84289 clarified structure of theories
haftmann
parents:
diff changeset
   116
  apply (rule nth_equalityI)
ff9efdc84289 clarified structure of theories
haftmann
parents:
diff changeset
   117
   apply (auto simp add: nth_takefill nth_rev)
ff9efdc84289 clarified structure of theories
haftmann
parents:
diff changeset
   118
  apply (rule_tac f = "\<lambda>n. bl ! n" in arg_cong)
ff9efdc84289 clarified structure of theories
haftmann
parents:
diff changeset
   119
  apply arith
ff9efdc84289 clarified structure of theories
haftmann
parents:
diff changeset
   120
  done
ff9efdc84289 clarified structure of theories
haftmann
parents:
diff changeset
   121
ff9efdc84289 clarified structure of theories
haftmann
parents:
diff changeset
   122
lemma takefill_minus: "0 < n \<Longrightarrow> takefill fill (Suc (n - 1)) w = takefill fill n w"
ff9efdc84289 clarified structure of theories
haftmann
parents:
diff changeset
   123
  by auto
ff9efdc84289 clarified structure of theories
haftmann
parents:
diff changeset
   124
ff9efdc84289 clarified structure of theories
haftmann
parents:
diff changeset
   125
lemmas takefill_Suc_cases =
ff9efdc84289 clarified structure of theories
haftmann
parents:
diff changeset
   126
  list.cases [THEN takefill.Suc [THEN trans]]
ff9efdc84289 clarified structure of theories
haftmann
parents:
diff changeset
   127
ff9efdc84289 clarified structure of theories
haftmann
parents:
diff changeset
   128
lemmas takefill_Suc_Nil = takefill_Suc_cases (1)
ff9efdc84289 clarified structure of theories
haftmann
parents:
diff changeset
   129
lemmas takefill_Suc_Cons = takefill_Suc_cases (2)
ff9efdc84289 clarified structure of theories
haftmann
parents:
diff changeset
   130
ff9efdc84289 clarified structure of theories
haftmann
parents:
diff changeset
   131
lemmas takefill_minus_simps = takefill_Suc_cases [THEN [2]
ff9efdc84289 clarified structure of theories
haftmann
parents:
diff changeset
   132
  takefill_minus [symmetric, THEN trans]]
ff9efdc84289 clarified structure of theories
haftmann
parents:
diff changeset
   133
ff9efdc84289 clarified structure of theories
haftmann
parents:
diff changeset
   134
lemma takefill_numeral_Nil [simp]:
ff9efdc84289 clarified structure of theories
haftmann
parents:
diff changeset
   135
  "takefill fill (numeral k) [] = fill # takefill fill (pred_numeral k) []"
ff9efdc84289 clarified structure of theories
haftmann
parents:
diff changeset
   136
  by (simp add: numeral_eq_Suc)
ff9efdc84289 clarified structure of theories
haftmann
parents:
diff changeset
   137
ff9efdc84289 clarified structure of theories
haftmann
parents:
diff changeset
   138
lemma takefill_numeral_Cons [simp]:
ff9efdc84289 clarified structure of theories
haftmann
parents:
diff changeset
   139
  "takefill fill (numeral k) (x # xs) = x # takefill fill (pred_numeral k) xs"
ff9efdc84289 clarified structure of theories
haftmann
parents:
diff changeset
   140
  by (simp add: numeral_eq_Suc)
ff9efdc84289 clarified structure of theories
haftmann
parents:
diff changeset
   141
ff9efdc84289 clarified structure of theories
haftmann
parents:
diff changeset
   142
ff9efdc84289 clarified structure of theories
haftmann
parents:
diff changeset
   143
subsection \<open>Auxiliary: Range projection\<close>
ff9efdc84289 clarified structure of theories
haftmann
parents:
diff changeset
   144
ff9efdc84289 clarified structure of theories
haftmann
parents:
diff changeset
   145
definition bl_of_nth :: "nat \<Rightarrow> (nat \<Rightarrow> 'a) \<Rightarrow> 'a list"
ff9efdc84289 clarified structure of theories
haftmann
parents:
diff changeset
   146
  where "bl_of_nth n f = map f (rev [0..<n])"
ff9efdc84289 clarified structure of theories
haftmann
parents:
diff changeset
   147
ff9efdc84289 clarified structure of theories
haftmann
parents:
diff changeset
   148
lemma bl_of_nth_simps [simp, code]:
ff9efdc84289 clarified structure of theories
haftmann
parents:
diff changeset
   149
  "bl_of_nth 0 f = []"
ff9efdc84289 clarified structure of theories
haftmann
parents:
diff changeset
   150
  "bl_of_nth (Suc n) f = f n # bl_of_nth n f"
ff9efdc84289 clarified structure of theories
haftmann
parents:
diff changeset
   151
  by (simp_all add: bl_of_nth_def)
ff9efdc84289 clarified structure of theories
haftmann
parents:
diff changeset
   152
ff9efdc84289 clarified structure of theories
haftmann
parents:
diff changeset
   153
lemma length_bl_of_nth [simp]: "length (bl_of_nth n f) = n"
ff9efdc84289 clarified structure of theories
haftmann
parents:
diff changeset
   154
  by (simp add: bl_of_nth_def)
ff9efdc84289 clarified structure of theories
haftmann
parents:
diff changeset
   155
ff9efdc84289 clarified structure of theories
haftmann
parents:
diff changeset
   156
lemma nth_bl_of_nth [simp]: "m < n \<Longrightarrow> rev (bl_of_nth n f) ! m = f m"
ff9efdc84289 clarified structure of theories
haftmann
parents:
diff changeset
   157
  by (simp add: bl_of_nth_def rev_map)
ff9efdc84289 clarified structure of theories
haftmann
parents:
diff changeset
   158
ff9efdc84289 clarified structure of theories
haftmann
parents:
diff changeset
   159
lemma bl_of_nth_inj: "(\<And>k. k < n \<Longrightarrow> f k = g k) \<Longrightarrow> bl_of_nth n f = bl_of_nth n g"
ff9efdc84289 clarified structure of theories
haftmann
parents:
diff changeset
   160
  by (simp add: bl_of_nth_def)
ff9efdc84289 clarified structure of theories
haftmann
parents:
diff changeset
   161
ff9efdc84289 clarified structure of theories
haftmann
parents:
diff changeset
   162
lemma bl_of_nth_nth_le: "n \<le> length xs \<Longrightarrow> bl_of_nth n (nth (rev xs)) = drop (length xs - n) xs"
ff9efdc84289 clarified structure of theories
haftmann
parents:
diff changeset
   163
  apply (induct n arbitrary: xs)
ff9efdc84289 clarified structure of theories
haftmann
parents:
diff changeset
   164
   apply clarsimp
ff9efdc84289 clarified structure of theories
haftmann
parents:
diff changeset
   165
  apply clarsimp
ff9efdc84289 clarified structure of theories
haftmann
parents:
diff changeset
   166
  apply (rule trans [OF _ hd_Cons_tl])
ff9efdc84289 clarified structure of theories
haftmann
parents:
diff changeset
   167
   apply (frule Suc_le_lessD)
ff9efdc84289 clarified structure of theories
haftmann
parents:
diff changeset
   168
   apply (simp add: nth_rev trans [OF drop_Suc drop_tl, symmetric])
ff9efdc84289 clarified structure of theories
haftmann
parents:
diff changeset
   169
   apply (subst hd_drop_conv_nth)
ff9efdc84289 clarified structure of theories
haftmann
parents:
diff changeset
   170
    apply force
ff9efdc84289 clarified structure of theories
haftmann
parents:
diff changeset
   171
   apply simp_all
ff9efdc84289 clarified structure of theories
haftmann
parents:
diff changeset
   172
  apply (rule_tac f = "\<lambda>n. drop n xs" in arg_cong)
ff9efdc84289 clarified structure of theories
haftmann
parents:
diff changeset
   173
  apply simp
ff9efdc84289 clarified structure of theories
haftmann
parents:
diff changeset
   174
  done
ff9efdc84289 clarified structure of theories
haftmann
parents:
diff changeset
   175
ff9efdc84289 clarified structure of theories
haftmann
parents:
diff changeset
   176
lemma bl_of_nth_nth [simp]: "bl_of_nth (length xs) ((!) (rev xs)) = xs"
ff9efdc84289 clarified structure of theories
haftmann
parents:
diff changeset
   177
  by (simp add: bl_of_nth_nth_le)
ff9efdc84289 clarified structure of theories
haftmann
parents:
diff changeset
   178
ff9efdc84289 clarified structure of theories
haftmann
parents:
diff changeset
   179
end