| author | haftmann |
| Thu, 11 Sep 2014 23:12:32 +0200 | |
| changeset 58320 | 351810c45a48 |
| parent 58295 | c8a8e7c37986 |
| child 58437 | 8d124c73c37a |
| permissions | -rw-r--r-- |
|
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 |
|
|
5fbe474b5da8
explicit theory with additional, less commonly used list operations
haftmann
parents:
diff
changeset
|
4 |
header \<open>Less common functions on lists\<close> |
|
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 | 14 |
lemma strip_while_rev [simp]: |
15 |
"strip_while P (rev xs) = rev (dropWhile P xs)" |
|
16 |
by (simp add: strip_while_def) |
|
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 | 67 |
|
68 |
definition no_leading :: "('a \<Rightarrow> bool) \<Rightarrow> 'a list \<Rightarrow> bool"
|
|
69 |
where |
|
70 |
"no_leading P xs \<longleftrightarrow> (xs \<noteq> [] \<longrightarrow> \<not> P (hd xs))" |
|
71 |
||
72 |
lemma no_leading_Nil [simp, intro!]: |
|
73 |
"no_leading P []" |
|
74 |
by (simp add: no_leading_def) |
|
75 |
||
76 |
lemma no_leading_Cons [simp, intro!]: |
|
77 |
"no_leading P (x # xs) \<longleftrightarrow> \<not> P x" |
|
78 |
by (simp add: no_leading_def) |
|
79 |
||
80 |
lemma no_leading_append [simp]: |
|
81 |
"no_leading P (xs @ ys) \<longleftrightarrow> no_leading P xs \<and> (xs = [] \<longrightarrow> no_leading P ys)" |
|
82 |
by (induct xs) simp_all |
|
83 |
||
84 |
lemma no_leading_dropWhile [simp]: |
|
85 |
"no_leading P (dropWhile P xs)" |
|
86 |
by (induct xs) simp_all |
|
87 |
||
88 |
lemma dropWhile_eq_obtain_leading: |
|
89 |
assumes "dropWhile P xs = ys" |
|
90 |
obtains zs where "xs = zs @ ys" and "\<And>z. z \<in> set zs \<Longrightarrow> P z" and "no_leading P ys" |
|
91 |
proof - |
|
92 |
from assms have "\<exists>zs. xs = zs @ ys \<and> (\<forall>z \<in> set zs. P z) \<and> no_leading P ys" |
|
93 |
proof (induct xs arbitrary: ys) |
|
94 |
case Nil then show ?case by simp |
|
95 |
next |
|
96 |
case (Cons x xs ys) |
|
97 |
show ?case proof (cases "P x") |
|
98 |
case True with Cons.hyps [of ys] Cons.prems |
|
99 |
have "\<exists>zs. xs = zs @ ys \<and> (\<forall>a\<in>set zs. P a) \<and> no_leading P ys" |
|
100 |
by simp |
|
101 |
then obtain zs where "xs = zs @ ys" and "\<And>z. z \<in> set zs \<Longrightarrow> P z" |
|
102 |
and *: "no_leading P ys" |
|
103 |
by blast |
|
104 |
with True have "x # xs = (x # zs) @ ys" and "\<And>z. z \<in> set (x # zs) \<Longrightarrow> P z" |
|
105 |
by auto |
|
106 |
with * show ?thesis |
|
107 |
by blast next |
|
108 |
case False |
|
109 |
with Cons show ?thesis by (cases ys) simp_all |
|
110 |
qed |
|
111 |
qed |
|
112 |
with that show thesis |
|
113 |
by blast |
|
114 |
qed |
|
115 |
||
116 |
lemma dropWhile_idem_iff: |
|
117 |
"dropWhile P xs = xs \<longleftrightarrow> no_leading P xs" |
|
118 |
by (cases xs) (auto elim: dropWhile_eq_obtain_leading) |
|
119 |
||
|
58199
5fbe474b5da8
explicit theory with additional, less commonly used list operations
haftmann
parents:
diff
changeset
|
120 |
|
| 58295 | 121 |
abbreviation no_trailing :: "('a \<Rightarrow> bool) \<Rightarrow> 'a list \<Rightarrow> bool"
|
122 |
where |
|
123 |
"no_trailing P xs \<equiv> no_leading P (rev xs)" |
|
124 |
||
125 |
lemma no_trailing_unfold: |
|
126 |
"no_trailing P xs \<longleftrightarrow> (xs \<noteq> [] \<longrightarrow> \<not> P (last xs))" |
|
127 |
by (induct xs) simp_all |
|
128 |
||
129 |
lemma no_trailing_Nil [simp, intro!]: |
|
130 |
"no_trailing P []" |
|
131 |
by simp |
|
132 |
||
133 |
lemma no_trailing_Cons [simp]: |
|
134 |
"no_trailing P (x # xs) \<longleftrightarrow> no_trailing P xs \<and> (xs = [] \<longrightarrow> \<not> P x)" |
|
135 |
by simp |
|
136 |
||
137 |
lemma no_trailing_append_Cons [simp]: |
|
138 |
"no_trailing P (xs @ y # ys) \<longleftrightarrow> no_trailing P (y # ys)" |
|
139 |
by simp |
|
140 |
||
141 |
lemma no_trailing_strip_while [simp]: |
|
142 |
"no_trailing P (strip_while P xs)" |
|
143 |
by (induct xs rule: rev_induct) simp_all |
|
144 |
||
145 |
lemma strip_while_eq_obtain_trailing: |
|
146 |
assumes "strip_while P xs = ys" |
|
147 |
obtains zs where "xs = ys @ zs" and "\<And>z. z \<in> set zs \<Longrightarrow> P z" and "no_trailing P ys" |
|
148 |
proof - |
|
149 |
from assms have "rev (rev (dropWhile P (rev xs))) = rev ys" |
|
150 |
by (simp add: strip_while_def) |
|
151 |
then have "dropWhile P (rev xs) = rev ys" |
|
152 |
by simp |
|
153 |
then obtain zs where A: "rev xs = zs @ rev ys" and B: "\<And>z. z \<in> set zs \<Longrightarrow> P z" |
|
154 |
and C: "no_trailing P ys" |
|
155 |
using dropWhile_eq_obtain_leading by blast |
|
156 |
from A have "rev (rev xs) = rev (zs @ rev ys)" |
|
157 |
by simp |
|
158 |
then have "xs = ys @ rev zs" |
|
159 |
by simp |
|
160 |
moreover from B have "\<And>z. z \<in> set (rev zs) \<Longrightarrow> P z" |
|
161 |
by simp |
|
162 |
ultimately show thesis using that C by blast |
|
163 |
qed |
|
164 |
||
165 |
lemma strip_while_idem_iff: |
|
166 |
"strip_while P xs = xs \<longleftrightarrow> no_trailing P xs" |
|
167 |
proof - |
|
168 |
def ys \<equiv> "rev xs" |
|
169 |
moreover have "strip_while P (rev ys) = rev ys \<longleftrightarrow> no_trailing P (rev ys)" |
|
170 |
by (simp add: dropWhile_idem_iff) |
|
171 |
ultimately show ?thesis by simp |
|
172 |
qed |
|
173 |
||
174 |
lemma no_trailing_map: |
|
175 |
"no_trailing P (map f xs) = no_trailing (P \<circ> f) xs" |
|
176 |
by (simp add: last_map no_trailing_unfold) |
|
177 |
||
178 |
lemma no_trailing_upt [simp]: |
|
179 |
"no_trailing P [n..<m] \<longleftrightarrow> (n < m \<longrightarrow> \<not> P (m - 1))" |
|
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 |
|
5fbe474b5da8
explicit theory with additional, less commonly used list operations
haftmann
parents:
diff
changeset
|
185 |
"nth_default x xs n = (if n < length xs then xs ! n else x)" |
|
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_Nil [simp]: |
|
5fbe474b5da8
explicit theory with additional, less commonly used list operations
haftmann
parents:
diff
changeset
|
188 |
"nth_default y [] n = y" |
|
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_Cons_0 [simp]: |
|
5fbe474b5da8
explicit theory with additional, less commonly used list operations
haftmann
parents:
diff
changeset
|
192 |
"nth_default y (x # xs) 0 = x" |
|
5fbe474b5da8
explicit theory with additional, less commonly used list operations
haftmann
parents:
diff
changeset
|
193 |
by (simp add: nth_default_def) |
|
5fbe474b5da8
explicit theory with additional, less commonly used list operations
haftmann
parents:
diff
changeset
|
194 |
|
|
5fbe474b5da8
explicit theory with additional, less commonly used list operations
haftmann
parents:
diff
changeset
|
195 |
lemma nth_default_Cons_Suc [simp]: |
|
5fbe474b5da8
explicit theory with additional, less commonly used list operations
haftmann
parents:
diff
changeset
|
196 |
"nth_default y (x # xs) (Suc n) = nth_default y xs n" |
|
5fbe474b5da8
explicit theory with additional, less commonly used list operations
haftmann
parents:
diff
changeset
|
197 |
by (simp add: nth_default_def) |
|
5fbe474b5da8
explicit theory with additional, less commonly used list operations
haftmann
parents:
diff
changeset
|
198 |
|
|
5fbe474b5da8
explicit theory with additional, less commonly used list operations
haftmann
parents:
diff
changeset
|
199 |
lemma nth_default_map_eq: |
|
5fbe474b5da8
explicit theory with additional, less commonly used list operations
haftmann
parents:
diff
changeset
|
200 |
"f y = x \<Longrightarrow> nth_default x (map f xs) n = f (nth_default y xs n)" |
|
5fbe474b5da8
explicit theory with additional, less commonly used list operations
haftmann
parents:
diff
changeset
|
201 |
by (simp add: nth_default_def) |
|
5fbe474b5da8
explicit theory with additional, less commonly used list operations
haftmann
parents:
diff
changeset
|
202 |
|
|
5fbe474b5da8
explicit theory with additional, less commonly used list operations
haftmann
parents:
diff
changeset
|
203 |
lemma nth_default_strip_while_eq [simp]: |
|
5fbe474b5da8
explicit theory with additional, less commonly used list operations
haftmann
parents:
diff
changeset
|
204 |
"nth_default x (strip_while (HOL.eq x) xs) n = nth_default x xs n" |
|
5fbe474b5da8
explicit theory with additional, less commonly used list operations
haftmann
parents:
diff
changeset
|
205 |
proof - |
|
5fbe474b5da8
explicit theory with additional, less commonly used list operations
haftmann
parents:
diff
changeset
|
206 |
from split_strip_while_append obtain ys zs |
|
5fbe474b5da8
explicit theory with additional, less commonly used list operations
haftmann
parents:
diff
changeset
|
207 |
where "strip_while (HOL.eq x) xs = ys" and "\<forall>z\<in>set zs. x = z" and "xs = ys @ zs" by blast |
|
5fbe474b5da8
explicit theory with additional, less commonly used list operations
haftmann
parents:
diff
changeset
|
208 |
then show ?thesis by (simp add: nth_default_def not_less nth_append) |
|
5fbe474b5da8
explicit theory with additional, less commonly used list operations
haftmann
parents:
diff
changeset
|
209 |
qed |
|
5fbe474b5da8
explicit theory with additional, less commonly used list operations
haftmann
parents:
diff
changeset
|
210 |
|
|
5fbe474b5da8
explicit theory with additional, less commonly used list operations
haftmann
parents:
diff
changeset
|
211 |
lemma nth_default_Cons: |
|
5fbe474b5da8
explicit theory with additional, less commonly used list operations
haftmann
parents:
diff
changeset
|
212 |
"nth_default y (x # xs) n = (case n of 0 \<Rightarrow> x | Suc n' \<Rightarrow> nth_default y xs n')" |
|
5fbe474b5da8
explicit theory with additional, less commonly used list operations
haftmann
parents:
diff
changeset
|
213 |
by (simp split: nat.split) |
|
5fbe474b5da8
explicit theory with additional, less commonly used list operations
haftmann
parents:
diff
changeset
|
214 |
|
|
5fbe474b5da8
explicit theory with additional, less commonly used list operations
haftmann
parents:
diff
changeset
|
215 |
lemma nth_default_nth: |
|
5fbe474b5da8
explicit theory with additional, less commonly used list operations
haftmann
parents:
diff
changeset
|
216 |
"n < length xs \<Longrightarrow> nth_default y xs n = xs ! n" |
|
5fbe474b5da8
explicit theory with additional, less commonly used list operations
haftmann
parents:
diff
changeset
|
217 |
by (simp add: nth_default_def) |
|
5fbe474b5da8
explicit theory with additional, less commonly used list operations
haftmann
parents:
diff
changeset
|
218 |
|
|
5fbe474b5da8
explicit theory with additional, less commonly used list operations
haftmann
parents:
diff
changeset
|
219 |
lemma nth_default_beyond: |
|
5fbe474b5da8
explicit theory with additional, less commonly used list operations
haftmann
parents:
diff
changeset
|
220 |
"length xs \<le> n \<Longrightarrow> nth_default y xs n = y" |
|
5fbe474b5da8
explicit theory with additional, less commonly used list operations
haftmann
parents:
diff
changeset
|
221 |
by (simp add: nth_default_def) |
|
5fbe474b5da8
explicit theory with additional, less commonly used list operations
haftmann
parents:
diff
changeset
|
222 |
|
|
5fbe474b5da8
explicit theory with additional, less commonly used list operations
haftmann
parents:
diff
changeset
|
223 |
lemma range_nth_default [simp]: |
|
5fbe474b5da8
explicit theory with additional, less commonly used list operations
haftmann
parents:
diff
changeset
|
224 |
"range (nth_default dflt xs) = insert dflt (set xs)" |
|
5fbe474b5da8
explicit theory with additional, less commonly used list operations
haftmann
parents:
diff
changeset
|
225 |
by (auto simp add: nth_default_def[abs_def] in_set_conv_nth) |
|
5fbe474b5da8
explicit theory with additional, less commonly used list operations
haftmann
parents:
diff
changeset
|
226 |
|
|
5fbe474b5da8
explicit theory with additional, less commonly used list operations
haftmann
parents:
diff
changeset
|
227 |
lemma nth_strip_while: |
|
5fbe474b5da8
explicit theory with additional, less commonly used list operations
haftmann
parents:
diff
changeset
|
228 |
assumes "n < length (strip_while P xs)" |
|
5fbe474b5da8
explicit theory with additional, less commonly used list operations
haftmann
parents:
diff
changeset
|
229 |
shows "strip_while P xs ! n = xs ! n" |
|
5fbe474b5da8
explicit theory with additional, less commonly used list operations
haftmann
parents:
diff
changeset
|
230 |
proof - |
|
5fbe474b5da8
explicit theory with additional, less commonly used list operations
haftmann
parents:
diff
changeset
|
231 |
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
|
232 |
by (subst add.commute) |
|
5fbe474b5da8
explicit theory with additional, less commonly used list operations
haftmann
parents:
diff
changeset
|
233 |
(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
|
234 |
then show ?thesis using assms |
|
5fbe474b5da8
explicit theory with additional, less commonly used list operations
haftmann
parents:
diff
changeset
|
235 |
by (simp add: strip_while_def rev_nth dropWhile_nth) |
|
5fbe474b5da8
explicit theory with additional, less commonly used list operations
haftmann
parents:
diff
changeset
|
236 |
qed |
|
5fbe474b5da8
explicit theory with additional, less commonly used list operations
haftmann
parents:
diff
changeset
|
237 |
|
|
5fbe474b5da8
explicit theory with additional, less commonly used list operations
haftmann
parents:
diff
changeset
|
238 |
lemma length_strip_while_le: |
|
5fbe474b5da8
explicit theory with additional, less commonly used list operations
haftmann
parents:
diff
changeset
|
239 |
"length (strip_while P xs) \<le> length xs" |
|
5fbe474b5da8
explicit theory with additional, less commonly used list operations
haftmann
parents:
diff
changeset
|
240 |
unfolding strip_while_def o_def length_rev |
|
5fbe474b5da8
explicit theory with additional, less commonly used list operations
haftmann
parents:
diff
changeset
|
241 |
by (subst (2) length_rev[symmetric]) |
|
5fbe474b5da8
explicit theory with additional, less commonly used list operations
haftmann
parents:
diff
changeset
|
242 |
(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
|
243 |
|
|
5fbe474b5da8
explicit theory with additional, less commonly used list operations
haftmann
parents:
diff
changeset
|
244 |
lemma finite_nth_default_neq_default [simp]: |
|
5fbe474b5da8
explicit theory with additional, less commonly used list operations
haftmann
parents:
diff
changeset
|
245 |
"finite {k. nth_default dflt xs k \<noteq> dflt}"
|
|
5fbe474b5da8
explicit theory with additional, less commonly used list operations
haftmann
parents:
diff
changeset
|
246 |
by (simp add: nth_default_def) |
|
5fbe474b5da8
explicit theory with additional, less commonly used list operations
haftmann
parents:
diff
changeset
|
247 |
|
|
5fbe474b5da8
explicit theory with additional, less commonly used list operations
haftmann
parents:
diff
changeset
|
248 |
lemma sorted_list_of_set_nth_default: |
|
5fbe474b5da8
explicit theory with additional, less commonly used list operations
haftmann
parents:
diff
changeset
|
249 |
"sorted_list_of_set {k. nth_default dflt xs k \<noteq> dflt} = map fst (filter (\<lambda>(_, x). x \<noteq> dflt) (zip [0..<length xs] xs))"
|
|
5fbe474b5da8
explicit theory with additional, less commonly used list operations
haftmann
parents:
diff
changeset
|
250 |
by (rule sorted_distinct_set_unique) (auto simp add: nth_default_def in_set_conv_nth sorted_filter distinct_map_filter intro: rev_image_eqI) |
|
5fbe474b5da8
explicit theory with additional, less commonly used list operations
haftmann
parents:
diff
changeset
|
251 |
|
|
5fbe474b5da8
explicit theory with additional, less commonly used list operations
haftmann
parents:
diff
changeset
|
252 |
lemma nth_default_snoc_default [simp]: |
|
5fbe474b5da8
explicit theory with additional, less commonly used list operations
haftmann
parents:
diff
changeset
|
253 |
"nth_default dflt (xs @ [dflt]) = nth_default dflt xs" |
|
5fbe474b5da8
explicit theory with additional, less commonly used list operations
haftmann
parents:
diff
changeset
|
254 |
by (auto simp add: nth_default_def fun_eq_iff nth_append) |
|
5fbe474b5da8
explicit theory with additional, less commonly used list operations
haftmann
parents:
diff
changeset
|
255 |
|
|
5fbe474b5da8
explicit theory with additional, less commonly used list operations
haftmann
parents:
diff
changeset
|
256 |
lemma nth_default_strip_while_dflt [simp]: |
|
5fbe474b5da8
explicit theory with additional, less commonly used list operations
haftmann
parents:
diff
changeset
|
257 |
"nth_default dflt (strip_while (op = dflt) xs) = nth_default dflt xs" |
|
5fbe474b5da8
explicit theory with additional, less commonly used list operations
haftmann
parents:
diff
changeset
|
258 |
by (induct xs rule: rev_induct) auto |
|
5fbe474b5da8
explicit theory with additional, less commonly used list operations
haftmann
parents:
diff
changeset
|
259 |
|
|
5fbe474b5da8
explicit theory with additional, less commonly used list operations
haftmann
parents:
diff
changeset
|
260 |
lemma nth_default_eq_dflt_iff: |
|
5fbe474b5da8
explicit theory with additional, less commonly used list operations
haftmann
parents:
diff
changeset
|
261 |
"nth_default dflt xs k = dflt \<longleftrightarrow> (k < length xs \<longrightarrow> xs ! k = dflt)" |
|
5fbe474b5da8
explicit theory with additional, less commonly used list operations
haftmann
parents:
diff
changeset
|
262 |
by (simp add: nth_default_def) |
|
5fbe474b5da8
explicit theory with additional, less commonly used list operations
haftmann
parents:
diff
changeset
|
263 |
|
|
5fbe474b5da8
explicit theory with additional, less commonly used list operations
haftmann
parents:
diff
changeset
|
264 |
end |
| 58295 | 265 |