src/HOL/Library/More_List.thy
author wenzelm
Fri Jul 22 11:00:43 2016 +0200 (2016-07-22)
changeset 63540 f8652d0534fa
parent 63040 eb4ddd18d635
child 65388 a8d868477bc0
permissions -rw-r--r--
tuned proofs -- avoid unstructured calculation;
haftmann@58199
     1
(* Author: Andreas Lochbihler, ETH Z├╝rich
haftmann@58199
     2
   Author: Florian Haftmann, TU Muenchen  *)
haftmann@58199
     3
wenzelm@58881
     4
section \<open>Less common functions on lists\<close>
haftmann@58199
     5
haftmann@58199
     6
theory More_List
haftmann@58199
     7
imports Main
haftmann@58199
     8
begin
haftmann@58199
     9
haftmann@58199
    10
definition strip_while :: "('a \<Rightarrow> bool) \<Rightarrow> 'a list \<Rightarrow> 'a list"
haftmann@58199
    11
where
haftmann@58199
    12
  "strip_while P = rev \<circ> dropWhile P \<circ> rev"
haftmann@58199
    13
haftmann@58295
    14
lemma strip_while_rev [simp]:
haftmann@58295
    15
  "strip_while P (rev xs) = rev (dropWhile P xs)"
haftmann@58295
    16
  by (simp add: strip_while_def)
haftmann@58295
    17
  
haftmann@58199
    18
lemma strip_while_Nil [simp]:
haftmann@58199
    19
  "strip_while P [] = []"
haftmann@58199
    20
  by (simp add: strip_while_def)
haftmann@58199
    21
haftmann@58199
    22
lemma strip_while_append [simp]:
haftmann@58199
    23
  "\<not> P x \<Longrightarrow> strip_while P (xs @ [x]) = xs @ [x]"
haftmann@58199
    24
  by (simp add: strip_while_def)
haftmann@58199
    25
haftmann@58199
    26
lemma strip_while_append_rec [simp]:
haftmann@58199
    27
  "P x \<Longrightarrow> strip_while P (xs @ [x]) = strip_while P xs"
haftmann@58199
    28
  by (simp add: strip_while_def)
haftmann@58199
    29
haftmann@58199
    30
lemma strip_while_Cons [simp]:
haftmann@58199
    31
  "\<not> P x \<Longrightarrow> strip_while P (x # xs) = x # strip_while P xs"
haftmann@58199
    32
  by (induct xs rule: rev_induct) (simp_all add: strip_while_def)
haftmann@58199
    33
haftmann@58199
    34
lemma strip_while_eq_Nil [simp]:
haftmann@58199
    35
  "strip_while P xs = [] \<longleftrightarrow> (\<forall>x\<in>set xs. P x)"
haftmann@58199
    36
  by (simp add: strip_while_def)
haftmann@58199
    37
haftmann@58199
    38
lemma strip_while_eq_Cons_rec:
haftmann@58199
    39
  "strip_while P (x # xs) = x # strip_while P xs \<longleftrightarrow> \<not> (P x \<and> (\<forall>x\<in>set xs. P x))"
haftmann@58199
    40
  by (induct xs rule: rev_induct) (simp_all add: strip_while_def)
haftmann@58199
    41
haftmann@58199
    42
lemma strip_while_not_last [simp]:
haftmann@58199
    43
  "\<not> P (last xs) \<Longrightarrow> strip_while P xs = xs"
haftmann@58199
    44
  by (cases xs rule: rev_cases) simp_all
haftmann@58199
    45
haftmann@58199
    46
lemma split_strip_while_append:
haftmann@58199
    47
  fixes xs :: "'a list"
haftmann@58199
    48
  obtains ys zs :: "'a list"
haftmann@58199
    49
  where "strip_while P xs = ys" and "\<forall>x\<in>set zs. P x" and "xs = ys @ zs"
haftmann@58199
    50
proof (rule that)
haftmann@58199
    51
  show "strip_while P xs = strip_while P xs" ..
haftmann@58199
    52
  show "\<forall>x\<in>set (rev (takeWhile P (rev xs))). P x" by (simp add: takeWhile_eq_all_conv [symmetric])
haftmann@58199
    53
  have "rev xs = rev (strip_while P xs @ rev (takeWhile P (rev xs)))"
haftmann@58199
    54
    by (simp add: strip_while_def)
haftmann@58199
    55
  then show "xs = strip_while P xs @ rev (takeWhile P (rev xs))"
haftmann@58199
    56
    by (simp only: rev_is_rev_conv)
haftmann@58199
    57
qed
haftmann@58199
    58
haftmann@58199
    59
lemma strip_while_snoc [simp]:
haftmann@58199
    60
  "strip_while P (xs @ [x]) = (if P x then strip_while P xs else xs @ [x])"
haftmann@58199
    61
  by (simp add: strip_while_def)
haftmann@58199
    62
haftmann@58199
    63
lemma strip_while_map:
haftmann@58199
    64
  "strip_while P (map f xs) = map f (strip_while (P \<circ> f) xs)"
haftmann@58199
    65
  by (simp add: strip_while_def rev_map dropWhile_map)
haftmann@58199
    66
haftmann@58295
    67
haftmann@58295
    68
definition no_leading :: "('a \<Rightarrow> bool) \<Rightarrow> 'a list \<Rightarrow> bool"
haftmann@58295
    69
where
haftmann@58295
    70
  "no_leading P xs \<longleftrightarrow> (xs \<noteq> [] \<longrightarrow> \<not> P (hd xs))"
haftmann@58295
    71
haftmann@58295
    72
lemma no_leading_Nil [simp, intro!]:
haftmann@58295
    73
  "no_leading P []"
haftmann@58295
    74
  by (simp add: no_leading_def)
haftmann@58295
    75
haftmann@58295
    76
lemma no_leading_Cons [simp, intro!]:
haftmann@58295
    77
  "no_leading P (x # xs) \<longleftrightarrow> \<not> P x"
haftmann@58295
    78
  by (simp add: no_leading_def)
haftmann@58295
    79
haftmann@58295
    80
lemma no_leading_append [simp]:
haftmann@58295
    81
  "no_leading P (xs @ ys) \<longleftrightarrow> no_leading P xs \<and> (xs = [] \<longrightarrow> no_leading P ys)"
haftmann@58295
    82
  by (induct xs) simp_all
haftmann@58295
    83
haftmann@58295
    84
lemma no_leading_dropWhile [simp]:
haftmann@58295
    85
  "no_leading P (dropWhile P xs)"
haftmann@58295
    86
  by (induct xs) simp_all
haftmann@58295
    87
haftmann@58295
    88
lemma dropWhile_eq_obtain_leading:
haftmann@58295
    89
  assumes "dropWhile P xs = ys"
haftmann@58295
    90
  obtains zs where "xs = zs @ ys" and "\<And>z. z \<in> set zs \<Longrightarrow> P z" and "no_leading P ys"
haftmann@58295
    91
proof -
haftmann@58295
    92
  from assms have "\<exists>zs. xs = zs @ ys \<and> (\<forall>z \<in> set zs. P z) \<and> no_leading P ys"
haftmann@58295
    93
  proof (induct xs arbitrary: ys)
haftmann@58295
    94
    case Nil then show ?case by simp
haftmann@58295
    95
  next
haftmann@58295
    96
    case (Cons x xs ys)
haftmann@58295
    97
    show ?case proof (cases "P x")
haftmann@58295
    98
      case True with Cons.hyps [of ys] Cons.prems
haftmann@58295
    99
      have "\<exists>zs. xs = zs @ ys \<and> (\<forall>a\<in>set zs. P a) \<and> no_leading P ys"
haftmann@58295
   100
        by simp
haftmann@58295
   101
      then obtain zs where "xs = zs @ ys" and "\<And>z. z \<in> set zs \<Longrightarrow> P z"
haftmann@58295
   102
        and *: "no_leading P ys"
haftmann@58295
   103
        by blast
haftmann@58295
   104
      with True have "x # xs = (x # zs) @ ys" and "\<And>z. z \<in> set (x # zs) \<Longrightarrow> P z"
haftmann@58295
   105
        by auto
haftmann@58295
   106
      with * show ?thesis
haftmann@58295
   107
        by blast    next
haftmann@58295
   108
      case False
haftmann@58295
   109
      with Cons show ?thesis by (cases ys) simp_all
haftmann@58295
   110
    qed
haftmann@58295
   111
  qed
haftmann@58295
   112
  with that show thesis
haftmann@58295
   113
    by blast
haftmann@58295
   114
qed
haftmann@58295
   115
haftmann@58295
   116
lemma dropWhile_idem_iff:
haftmann@58295
   117
  "dropWhile P xs = xs \<longleftrightarrow> no_leading P xs"
haftmann@58295
   118
  by (cases xs) (auto elim: dropWhile_eq_obtain_leading)
haftmann@58295
   119
haftmann@58199
   120
haftmann@58295
   121
abbreviation no_trailing :: "('a \<Rightarrow> bool) \<Rightarrow> 'a list \<Rightarrow> bool"
haftmann@58295
   122
where
haftmann@58295
   123
  "no_trailing P xs \<equiv> no_leading P (rev xs)"
haftmann@58295
   124
haftmann@58295
   125
lemma no_trailing_unfold:
haftmann@58295
   126
  "no_trailing P xs \<longleftrightarrow> (xs \<noteq> [] \<longrightarrow> \<not> P (last xs))"
haftmann@58295
   127
  by (induct xs) simp_all
haftmann@58295
   128
haftmann@58295
   129
lemma no_trailing_Nil [simp, intro!]:
haftmann@58295
   130
  "no_trailing P []"
haftmann@58295
   131
  by simp
haftmann@58295
   132
haftmann@58295
   133
lemma no_trailing_Cons [simp]:
haftmann@58295
   134
  "no_trailing P (x # xs) \<longleftrightarrow> no_trailing P xs \<and> (xs = [] \<longrightarrow> \<not> P x)"
haftmann@58295
   135
  by simp
haftmann@58295
   136
haftmann@58295
   137
lemma no_trailing_append_Cons [simp]:
haftmann@58295
   138
  "no_trailing P (xs @ y # ys) \<longleftrightarrow> no_trailing P (y # ys)"
haftmann@58295
   139
  by simp
haftmann@58295
   140
haftmann@58295
   141
lemma no_trailing_strip_while [simp]:
haftmann@58295
   142
  "no_trailing P (strip_while P xs)"
haftmann@58295
   143
  by (induct xs rule: rev_induct) simp_all
haftmann@58295
   144
haftmann@58295
   145
lemma strip_while_eq_obtain_trailing:
haftmann@58295
   146
  assumes "strip_while P xs = ys"
haftmann@58295
   147
  obtains zs where "xs = ys @ zs" and "\<And>z. z \<in> set zs \<Longrightarrow> P z" and "no_trailing P ys"
haftmann@58295
   148
proof -
haftmann@58295
   149
  from assms have "rev (rev (dropWhile P (rev xs))) = rev ys"
haftmann@58295
   150
    by (simp add: strip_while_def)
haftmann@58295
   151
  then have "dropWhile P (rev xs) = rev ys"
haftmann@58295
   152
    by simp
haftmann@58295
   153
  then obtain zs where A: "rev xs = zs @ rev ys" and B: "\<And>z. z \<in> set zs \<Longrightarrow> P z"
haftmann@58295
   154
    and C: "no_trailing P ys"
haftmann@58295
   155
    using dropWhile_eq_obtain_leading by blast
haftmann@58295
   156
  from A have "rev (rev xs) = rev (zs @ rev ys)"
haftmann@58295
   157
    by simp
haftmann@58295
   158
  then have "xs = ys @ rev zs"
haftmann@58295
   159
    by simp
haftmann@58295
   160
  moreover from B have "\<And>z. z \<in> set (rev zs) \<Longrightarrow> P z"
haftmann@58295
   161
    by simp
haftmann@58295
   162
  ultimately show thesis using that C by blast
haftmann@58295
   163
qed
haftmann@58295
   164
haftmann@58295
   165
lemma strip_while_idem_iff:
haftmann@58295
   166
  "strip_while P xs = xs \<longleftrightarrow> no_trailing P xs"
haftmann@58295
   167
proof -
wenzelm@63040
   168
  define ys where "ys = rev xs"
haftmann@58295
   169
  moreover have "strip_while P (rev ys) = rev ys \<longleftrightarrow> no_trailing P (rev ys)"
haftmann@58295
   170
    by (simp add: dropWhile_idem_iff)
haftmann@58295
   171
  ultimately show ?thesis by simp
haftmann@58295
   172
qed
haftmann@58295
   173
haftmann@58295
   174
lemma no_trailing_map:
haftmann@58295
   175
  "no_trailing P (map f xs) = no_trailing (P \<circ> f) xs"
haftmann@58295
   176
  by (simp add: last_map no_trailing_unfold)
haftmann@58295
   177
haftmann@58295
   178
lemma no_trailing_upt [simp]:
haftmann@58295
   179
  "no_trailing P [n..<m] \<longleftrightarrow> (n < m \<longrightarrow> \<not> P (m - 1))"
haftmann@58295
   180
  by (auto simp add: no_trailing_unfold)
haftmann@58199
   181
haftmann@58199
   182
haftmann@58199
   183
definition nth_default :: "'a \<Rightarrow> 'a list \<Rightarrow> nat \<Rightarrow> 'a"
haftmann@58199
   184
where
haftmann@58437
   185
  "nth_default dflt xs n = (if n < length xs then xs ! n else dflt)"
haftmann@58199
   186
haftmann@58199
   187
lemma nth_default_nth:
haftmann@58437
   188
  "n < length xs \<Longrightarrow> nth_default dflt xs n = xs ! n"
haftmann@58199
   189
  by (simp add: nth_default_def)
haftmann@58199
   190
haftmann@58199
   191
lemma nth_default_beyond:
haftmann@58437
   192
  "length xs \<le> n \<Longrightarrow> nth_default dflt xs n = dflt"
haftmann@58437
   193
  by (simp add: nth_default_def)
haftmann@58437
   194
haftmann@58437
   195
lemma nth_default_Nil [simp]:
haftmann@58437
   196
  "nth_default dflt [] n = dflt"
haftmann@58437
   197
  by (simp add: nth_default_def)
haftmann@58437
   198
haftmann@58437
   199
lemma nth_default_Cons:
haftmann@58437
   200
  "nth_default dflt (x # xs) n = (case n of 0 \<Rightarrow> x | Suc n' \<Rightarrow> nth_default dflt xs n')"
haftmann@58437
   201
  by (simp add: nth_default_def split: nat.split)
haftmann@58437
   202
haftmann@58437
   203
lemma nth_default_Cons_0 [simp]:
haftmann@58437
   204
  "nth_default dflt (x # xs) 0 = x"
haftmann@58437
   205
  by (simp add: nth_default_Cons)
haftmann@58437
   206
haftmann@58437
   207
lemma nth_default_Cons_Suc [simp]:
haftmann@58437
   208
  "nth_default dflt (x # xs) (Suc n) = nth_default dflt xs n"
haftmann@58437
   209
  by (simp add: nth_default_Cons)
haftmann@58437
   210
haftmann@58437
   211
lemma nth_default_replicate_dflt [simp]:
haftmann@58437
   212
  "nth_default dflt (replicate n dflt) m = dflt"
haftmann@58199
   213
  by (simp add: nth_default_def)
haftmann@58199
   214
haftmann@58437
   215
lemma nth_default_append:
haftmann@58437
   216
  "nth_default dflt (xs @ ys) n =
haftmann@58437
   217
    (if n < length xs then nth xs n else nth_default dflt ys (n - length xs))"
haftmann@58437
   218
  by (auto simp add: nth_default_def nth_append)
haftmann@58437
   219
haftmann@58437
   220
lemma nth_default_append_trailing [simp]:
haftmann@58437
   221
  "nth_default dflt (xs @ replicate n dflt) = nth_default dflt xs"
haftmann@58437
   222
  by (simp add: fun_eq_iff nth_default_append) (simp add: nth_default_def)
haftmann@58437
   223
haftmann@58437
   224
lemma nth_default_snoc_default [simp]:
haftmann@58437
   225
  "nth_default dflt (xs @ [dflt]) = nth_default dflt xs"
haftmann@58437
   226
  by (auto simp add: nth_default_def fun_eq_iff nth_append)
haftmann@58437
   227
haftmann@58437
   228
lemma nth_default_eq_dflt_iff:
haftmann@58437
   229
  "nth_default dflt xs k = dflt \<longleftrightarrow> (k < length xs \<longrightarrow> xs ! k = dflt)"
haftmann@58437
   230
  by (simp add: nth_default_def)
haftmann@58437
   231
haftmann@58437
   232
lemma in_enumerate_iff_nth_default_eq:
haftmann@58437
   233
  "x \<noteq> dflt \<Longrightarrow> (n, x) \<in> set (enumerate 0 xs) \<longleftrightarrow> nth_default dflt xs n = x"
haftmann@58437
   234
  by (auto simp add: nth_default_def in_set_conv_nth enumerate_eq_zip)
haftmann@58437
   235
haftmann@58437
   236
lemma last_conv_nth_default:
haftmann@58437
   237
  assumes "xs \<noteq> []"
haftmann@58437
   238
  shows "last xs = nth_default dflt xs (length xs - 1)"
haftmann@58437
   239
  using assms by (simp add: nth_default_def last_conv_nth)
haftmann@58437
   240
  
haftmann@58437
   241
lemma nth_default_map_eq:
haftmann@58437
   242
  "f dflt' = dflt \<Longrightarrow> nth_default dflt (map f xs) n = f (nth_default dflt' xs n)"
haftmann@58437
   243
  by (simp add: nth_default_def)
haftmann@58437
   244
haftmann@58437
   245
lemma finite_nth_default_neq_default [simp]:
haftmann@58437
   246
  "finite {k. nth_default dflt xs k \<noteq> dflt}"
haftmann@58437
   247
  by (simp add: nth_default_def)
haftmann@58437
   248
haftmann@58437
   249
lemma sorted_list_of_set_nth_default:
haftmann@58437
   250
  "sorted_list_of_set {k. nth_default dflt xs k \<noteq> dflt} = map fst (filter (\<lambda>(_, x). x \<noteq> dflt) (enumerate 0 xs))"
haftmann@58437
   251
  by (rule sorted_distinct_set_unique) (auto simp add: nth_default_def in_set_conv_nth
haftmann@58437
   252
    sorted_filter distinct_map_filter enumerate_eq_zip intro: rev_image_eqI)
haftmann@58437
   253
haftmann@58437
   254
lemma map_nth_default:
haftmann@58437
   255
  "map (nth_default x xs) [0..<length xs] = xs"
haftmann@58437
   256
proof -
haftmann@58437
   257
  have *: "map (nth_default x xs) [0..<length xs] = map (List.nth xs) [0..<length xs]"
haftmann@58437
   258
    by (rule map_cong) (simp_all add: nth_default_nth)
haftmann@58437
   259
  show ?thesis by (simp add: * map_nth)
haftmann@58437
   260
qed
haftmann@58437
   261
haftmann@58199
   262
lemma range_nth_default [simp]:
haftmann@58199
   263
  "range (nth_default dflt xs) = insert dflt (set xs)"
haftmann@58437
   264
  by (auto simp add: nth_default_def [abs_def] in_set_conv_nth)
haftmann@58199
   265
haftmann@58199
   266
lemma nth_strip_while:
haftmann@58199
   267
  assumes "n < length (strip_while P xs)"
haftmann@58199
   268
  shows "strip_while P xs ! n = xs ! n"
haftmann@58199
   269
proof -
haftmann@58199
   270
  have "length (dropWhile P (rev xs)) + length (takeWhile P (rev xs)) = length xs"
haftmann@58199
   271
    by (subst add.commute)
haftmann@58199
   272
      (simp add: arg_cong [where f=length, OF takeWhile_dropWhile_id, unfolded length_append])
haftmann@58199
   273
  then show ?thesis using assms
haftmann@58199
   274
    by (simp add: strip_while_def rev_nth dropWhile_nth)
haftmann@58199
   275
qed
haftmann@58199
   276
haftmann@58199
   277
lemma length_strip_while_le:
haftmann@58199
   278
  "length (strip_while P xs) \<le> length xs"
haftmann@58199
   279
  unfolding strip_while_def o_def length_rev
haftmann@58199
   280
  by (subst (2) length_rev[symmetric])
haftmann@58199
   281
    (simp add: strip_while_def length_dropWhile_le del: length_rev)
haftmann@58199
   282
haftmann@58199
   283
lemma nth_default_strip_while_dflt [simp]:
haftmann@58437
   284
  "nth_default dflt (strip_while (op = dflt) xs) = nth_default dflt xs"
haftmann@58199
   285
  by (induct xs rule: rev_induct) auto
haftmann@58199
   286
haftmann@58437
   287
lemma nth_default_eq_iff:
haftmann@58437
   288
  "nth_default dflt xs = nth_default dflt ys
haftmann@58437
   289
     \<longleftrightarrow> strip_while (HOL.eq dflt) xs = strip_while (HOL.eq dflt) ys" (is "?P \<longleftrightarrow> ?Q")
haftmann@58437
   290
proof
haftmann@58437
   291
  let ?xs = "strip_while (HOL.eq dflt) xs" and ?ys = "strip_while (HOL.eq dflt) ys"
haftmann@58437
   292
  assume ?P
haftmann@58437
   293
  then have eq: "nth_default dflt ?xs = nth_default dflt ?ys"
haftmann@58437
   294
    by simp
haftmann@58437
   295
  have len: "length ?xs = length ?ys"
haftmann@58437
   296
  proof (rule ccontr)
haftmann@58437
   297
    assume len: "length ?xs \<noteq> length ?ys"
haftmann@58437
   298
    { fix xs ys :: "'a list"
haftmann@58437
   299
      let ?xs = "strip_while (HOL.eq dflt) xs" and ?ys = "strip_while (HOL.eq dflt) ys"
haftmann@58437
   300
      assume eq: "nth_default dflt ?xs = nth_default dflt ?ys"
haftmann@58437
   301
      assume len: "length ?xs < length ?ys"
haftmann@58437
   302
      then have "length ?ys > 0" by arith
haftmann@58437
   303
      then have "?ys \<noteq> []" by simp
haftmann@58437
   304
      with last_conv_nth_default [of ?ys dflt]
haftmann@58437
   305
      have "last ?ys = nth_default dflt ?ys (length ?ys - 1)"
haftmann@58437
   306
        by auto
wenzelm@60500
   307
      moreover from \<open>?ys \<noteq> []\<close> no_trailing_strip_while [of "HOL.eq dflt" ys]
haftmann@58437
   308
        have "last ?ys \<noteq> dflt" by (simp add: no_trailing_unfold)
haftmann@58437
   309
      ultimately have "nth_default dflt ?xs (length ?ys - 1) \<noteq> dflt"
haftmann@58437
   310
        using eq by simp
haftmann@58437
   311
      moreover from len have "length ?ys - 1 \<ge> length ?xs" by simp
haftmann@58437
   312
      ultimately have False by (simp only: nth_default_beyond) simp
haftmann@58437
   313
    } 
haftmann@58437
   314
    from this [of xs ys] this [of ys xs] len eq show False
haftmann@58437
   315
      by (auto simp only: linorder_class.neq_iff)
haftmann@58437
   316
  qed
haftmann@58437
   317
  then show ?Q
haftmann@58437
   318
  proof (rule nth_equalityI [rule_format])
haftmann@58437
   319
    fix n
wenzelm@63540
   320
    assume n: "n < length ?xs"
wenzelm@63540
   321
    with len have "n < length ?ys"
haftmann@58437
   322
      by simp
wenzelm@63540
   323
    with n have xs: "nth_default dflt ?xs n = ?xs ! n"
haftmann@58437
   324
      and ys: "nth_default dflt ?ys n = ?ys ! n"
haftmann@58437
   325
      by (simp_all only: nth_default_nth)
haftmann@58437
   326
    with eq show "?xs ! n = ?ys ! n"
haftmann@58437
   327
      by simp
haftmann@58437
   328
  qed
haftmann@58437
   329
next
haftmann@58437
   330
  assume ?Q
haftmann@58437
   331
  then have "nth_default dflt (strip_while (HOL.eq dflt) xs) = nth_default dflt (strip_while (HOL.eq dflt) ys)"
haftmann@58437
   332
    by simp
haftmann@58437
   333
  then show ?P
haftmann@58437
   334
    by simp
haftmann@58437
   335
qed
haftmann@58199
   336
haftmann@58199
   337
end
haftmann@58295
   338