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