author | paulson <lp15@cam.ac.uk> |
Fri, 21 Mar 2025 10:45:56 +0000 | |
changeset 82310 | 41f5266e5595 |
parent 82218 | cbf9f856d3e0 |
child 82691 | b69e4da2604b |
permissions | -rw-r--r-- |
49087 | 1 |
(* Title: HOL/Library/Sublist.thy |
65956
639eb3617a86
reorganised material on sublists
eberlm <eberlm@in.tum.de>
parents:
65954
diff
changeset
|
2 |
Author: Tobias Nipkow and Markus Wenzel, TU München |
49087 | 3 |
Author: Christian Sternagel, JAIST |
65956
639eb3617a86
reorganised material on sublists
eberlm <eberlm@in.tum.de>
parents:
65954
diff
changeset
|
4 |
Author: Manuel Eberl, TU München |
10330
4362e906b745
"List prefixes" library theory (replaces old Lex/Prefix);
wenzelm
parents:
diff
changeset
|
5 |
*) |
4362e906b745
"List prefixes" library theory (replaces old Lex/Prefix);
wenzelm
parents:
diff
changeset
|
6 |
|
60500 | 7 |
section \<open>List prefixes, suffixes, and homeomorphic embedding\<close> |
10330
4362e906b745
"List prefixes" library theory (replaces old Lex/Prefix);
wenzelm
parents:
diff
changeset
|
8 |
|
49087 | 9 |
theory Sublist |
10 |
imports Main |
|
15131 | 11 |
begin |
10330
4362e906b745
"List prefixes" library theory (replaces old Lex/Prefix);
wenzelm
parents:
diff
changeset
|
12 |
|
60500 | 13 |
subsection \<open>Prefix order on lists\<close> |
55579
207538943038
reverted ba7392b52a7c: List_Prefix not needed anymore by codatatypes
traytel
parents:
54538
diff
changeset
|
14 |
|
63117 | 15 |
definition prefix :: "'a list \<Rightarrow> 'a list \<Rightarrow> bool" |
16 |
where "prefix xs ys \<longleftrightarrow> (\<exists>zs. ys = xs @ zs)" |
|
55579
207538943038
reverted ba7392b52a7c: List_Prefix not needed anymore by codatatypes
traytel
parents:
54538
diff
changeset
|
17 |
|
63117 | 18 |
definition strict_prefix :: "'a list \<Rightarrow> 'a list \<Rightarrow> bool" |
19 |
where "strict_prefix xs ys \<longleftrightarrow> prefix xs ys \<and> xs \<noteq> ys" |
|
55579
207538943038
reverted ba7392b52a7c: List_Prefix not needed anymore by codatatypes
traytel
parents:
54538
diff
changeset
|
20 |
|
73411 | 21 |
global_interpretation prefix_order: ordering prefix strict_prefix |
22 |
by standard (auto simp add: prefix_def strict_prefix_def) |
|
23 |
||
63117 | 24 |
interpretation prefix_order: order prefix strict_prefix |
25 |
by standard (auto simp: prefix_def strict_prefix_def) |
|
55579
207538943038
reverted ba7392b52a7c: List_Prefix not needed anymore by codatatypes
traytel
parents:
54538
diff
changeset
|
26 |
|
73411 | 27 |
global_interpretation prefix_bot: ordering_top \<open>\<lambda>xs ys. prefix ys xs\<close> \<open>\<lambda>xs ys. strict_prefix ys xs\<close> \<open>[]\<close> |
28 |
by standard (simp add: prefix_def) |
|
29 |
||
63117 | 30 |
interpretation prefix_bot: order_bot Nil prefix strict_prefix |
31 |
by standard (simp add: prefix_def) |
|
55579
207538943038
reverted ba7392b52a7c: List_Prefix not needed anymore by codatatypes
traytel
parents:
54538
diff
changeset
|
32 |
|
63117 | 33 |
lemma prefixI [intro?]: "ys = xs @ zs \<Longrightarrow> prefix xs ys" |
34 |
unfolding prefix_def by blast |
|
55579
207538943038
reverted ba7392b52a7c: List_Prefix not needed anymore by codatatypes
traytel
parents:
54538
diff
changeset
|
35 |
|
63117 | 36 |
lemma prefixE [elim?]: |
37 |
assumes "prefix xs ys" |
|
55579
207538943038
reverted ba7392b52a7c: List_Prefix not needed anymore by codatatypes
traytel
parents:
54538
diff
changeset
|
38 |
obtains zs where "ys = xs @ zs" |
63117 | 39 |
using assms unfolding prefix_def by blast |
55579
207538943038
reverted ba7392b52a7c: List_Prefix not needed anymore by codatatypes
traytel
parents:
54538
diff
changeset
|
40 |
|
63117 | 41 |
lemma strict_prefixI' [intro?]: "ys = xs @ z # zs \<Longrightarrow> strict_prefix xs ys" |
42 |
unfolding strict_prefix_def prefix_def by blast |
|
55579
207538943038
reverted ba7392b52a7c: List_Prefix not needed anymore by codatatypes
traytel
parents:
54538
diff
changeset
|
43 |
|
63117 | 44 |
lemma strict_prefixE' [elim?]: |
45 |
assumes "strict_prefix xs ys" |
|
55579
207538943038
reverted ba7392b52a7c: List_Prefix not needed anymore by codatatypes
traytel
parents:
54538
diff
changeset
|
46 |
obtains z zs where "ys = xs @ z # zs" |
207538943038
reverted ba7392b52a7c: List_Prefix not needed anymore by codatatypes
traytel
parents:
54538
diff
changeset
|
47 |
proof - |
63117 | 48 |
from \<open>strict_prefix xs ys\<close> obtain us where "ys = xs @ us" and "xs \<noteq> ys" |
49 |
unfolding strict_prefix_def prefix_def by blast |
|
55579
207538943038
reverted ba7392b52a7c: List_Prefix not needed anymore by codatatypes
traytel
parents:
54538
diff
changeset
|
50 |
with that show ?thesis by (auto simp add: neq_Nil_conv) |
207538943038
reverted ba7392b52a7c: List_Prefix not needed anymore by codatatypes
traytel
parents:
54538
diff
changeset
|
51 |
qed |
207538943038
reverted ba7392b52a7c: List_Prefix not needed anymore by codatatypes
traytel
parents:
54538
diff
changeset
|
52 |
|
63155 | 53 |
(* FIXME rm *) |
63117 | 54 |
lemma strict_prefixI [intro?]: "prefix xs ys \<Longrightarrow> xs \<noteq> ys \<Longrightarrow> strict_prefix xs ys" |
63155 | 55 |
by(fact prefix_order.le_neq_trans) |
55579
207538943038
reverted ba7392b52a7c: List_Prefix not needed anymore by codatatypes
traytel
parents:
54538
diff
changeset
|
56 |
|
63117 | 57 |
lemma strict_prefixE [elim?]: |
55579
207538943038
reverted ba7392b52a7c: List_Prefix not needed anymore by codatatypes
traytel
parents:
54538
diff
changeset
|
58 |
fixes xs ys :: "'a list" |
63117 | 59 |
assumes "strict_prefix xs ys" |
60 |
obtains "prefix xs ys" and "xs \<noteq> ys" |
|
61 |
using assms unfolding strict_prefix_def by blast |
|
55579
207538943038
reverted ba7392b52a7c: List_Prefix not needed anymore by codatatypes
traytel
parents:
54538
diff
changeset
|
62 |
|
207538943038
reverted ba7392b52a7c: List_Prefix not needed anymore by codatatypes
traytel
parents:
54538
diff
changeset
|
63 |
|
60500 | 64 |
subsection \<open>Basic properties of prefixes\<close> |
55579
207538943038
reverted ba7392b52a7c: List_Prefix not needed anymore by codatatypes
traytel
parents:
54538
diff
changeset
|
65 |
|
65869 | 66 |
theorem Nil_prefix [simp]: "prefix [] xs" |
67 |
by (fact prefix_bot.bot_least) |
|
55579
207538943038
reverted ba7392b52a7c: List_Prefix not needed anymore by codatatypes
traytel
parents:
54538
diff
changeset
|
68 |
|
63117 | 69 |
theorem prefix_Nil [simp]: "(prefix xs []) = (xs = [])" |
65869 | 70 |
by (fact prefix_bot.bot_unique) |
55579
207538943038
reverted ba7392b52a7c: List_Prefix not needed anymore by codatatypes
traytel
parents:
54538
diff
changeset
|
71 |
|
63117 | 72 |
lemma prefix_snoc [simp]: "prefix xs (ys @ [y]) \<longleftrightarrow> xs = ys @ [y] \<or> prefix xs ys" |
55579
207538943038
reverted ba7392b52a7c: List_Prefix not needed anymore by codatatypes
traytel
parents:
54538
diff
changeset
|
73 |
proof |
63117 | 74 |
assume "prefix xs (ys @ [y])" |
55579
207538943038
reverted ba7392b52a7c: List_Prefix not needed anymore by codatatypes
traytel
parents:
54538
diff
changeset
|
75 |
then obtain zs where zs: "ys @ [y] = xs @ zs" .. |
63117 | 76 |
show "xs = ys @ [y] \<or> prefix xs ys" |
77 |
by (metis append_Nil2 butlast_append butlast_snoc prefixI zs) |
|
55579
207538943038
reverted ba7392b52a7c: List_Prefix not needed anymore by codatatypes
traytel
parents:
54538
diff
changeset
|
78 |
next |
63117 | 79 |
assume "xs = ys @ [y] \<or> prefix xs ys" |
80 |
then show "prefix xs (ys @ [y])" |
|
82218
cbf9f856d3e0
Some new lemmas and some tidying
paulson <lp15@cam.ac.uk>
parents:
81332
diff
changeset
|
81 |
using prefix_def prefix_order.order_trans by blast |
55579
207538943038
reverted ba7392b52a7c: List_Prefix not needed anymore by codatatypes
traytel
parents:
54538
diff
changeset
|
82 |
qed |
207538943038
reverted ba7392b52a7c: List_Prefix not needed anymore by codatatypes
traytel
parents:
54538
diff
changeset
|
83 |
|
63117 | 84 |
lemma Cons_prefix_Cons [simp]: "prefix (x # xs) (y # ys) = (x = y \<and> prefix xs ys)" |
85 |
by (auto simp add: prefix_def) |
|
55579
207538943038
reverted ba7392b52a7c: List_Prefix not needed anymore by codatatypes
traytel
parents:
54538
diff
changeset
|
86 |
|
63117 | 87 |
lemma prefix_code [code]: |
88 |
"prefix [] xs \<longleftrightarrow> True" |
|
89 |
"prefix (x # xs) [] \<longleftrightarrow> False" |
|
90 |
"prefix (x # xs) (y # ys) \<longleftrightarrow> x = y \<and> prefix xs ys" |
|
55579
207538943038
reverted ba7392b52a7c: List_Prefix not needed anymore by codatatypes
traytel
parents:
54538
diff
changeset
|
91 |
by simp_all |
207538943038
reverted ba7392b52a7c: List_Prefix not needed anymore by codatatypes
traytel
parents:
54538
diff
changeset
|
92 |
|
63117 | 93 |
lemma same_prefix_prefix [simp]: "prefix (xs @ ys) (xs @ zs) = prefix ys zs" |
55579
207538943038
reverted ba7392b52a7c: List_Prefix not needed anymore by codatatypes
traytel
parents:
54538
diff
changeset
|
94 |
by (induct xs) simp_all |
207538943038
reverted ba7392b52a7c: List_Prefix not needed anymore by codatatypes
traytel
parents:
54538
diff
changeset
|
95 |
|
65869 | 96 |
lemma same_prefix_nil [simp]: "prefix (xs @ ys) xs = (ys = [])" |
81332 | 97 |
by (simp add: prefix_def) |
55579
207538943038
reverted ba7392b52a7c: List_Prefix not needed anymore by codatatypes
traytel
parents:
54538
diff
changeset
|
98 |
|
63117 | 99 |
lemma prefix_prefix [simp]: "prefix xs ys \<Longrightarrow> prefix xs (ys @ zs)" |
64886 | 100 |
unfolding prefix_def by fastforce |
55579
207538943038
reverted ba7392b52a7c: List_Prefix not needed anymore by codatatypes
traytel
parents:
54538
diff
changeset
|
101 |
|
63117 | 102 |
lemma append_prefixD: "prefix (xs @ ys) zs \<Longrightarrow> prefix xs zs" |
103 |
by (auto simp add: prefix_def) |
|
55579
207538943038
reverted ba7392b52a7c: List_Prefix not needed anymore by codatatypes
traytel
parents:
54538
diff
changeset
|
104 |
|
63117 | 105 |
theorem prefix_Cons: "prefix xs (y # ys) = (xs = [] \<or> (\<exists>zs. xs = y # zs \<and> prefix zs ys))" |
106 |
by (cases xs) (auto simp add: prefix_def) |
|
55579
207538943038
reverted ba7392b52a7c: List_Prefix not needed anymore by codatatypes
traytel
parents:
54538
diff
changeset
|
107 |
|
63117 | 108 |
theorem prefix_append: |
109 |
"prefix xs (ys @ zs) = (prefix xs ys \<or> (\<exists>us. xs = ys @ us \<and> prefix us zs))" |
|
82218
cbf9f856d3e0
Some new lemmas and some tidying
paulson <lp15@cam.ac.uk>
parents:
81332
diff
changeset
|
110 |
proof (induct zs rule: rev_induct) |
cbf9f856d3e0
Some new lemmas and some tidying
paulson <lp15@cam.ac.uk>
parents:
81332
diff
changeset
|
111 |
case Nil |
cbf9f856d3e0
Some new lemmas and some tidying
paulson <lp15@cam.ac.uk>
parents:
81332
diff
changeset
|
112 |
then show ?case by force |
cbf9f856d3e0
Some new lemmas and some tidying
paulson <lp15@cam.ac.uk>
parents:
81332
diff
changeset
|
113 |
next |
cbf9f856d3e0
Some new lemmas and some tidying
paulson <lp15@cam.ac.uk>
parents:
81332
diff
changeset
|
114 |
case (snoc x xs) |
cbf9f856d3e0
Some new lemmas and some tidying
paulson <lp15@cam.ac.uk>
parents:
81332
diff
changeset
|
115 |
then show ?case |
cbf9f856d3e0
Some new lemmas and some tidying
paulson <lp15@cam.ac.uk>
parents:
81332
diff
changeset
|
116 |
by (metis append.assoc prefix_snoc) |
cbf9f856d3e0
Some new lemmas and some tidying
paulson <lp15@cam.ac.uk>
parents:
81332
diff
changeset
|
117 |
qed |
55579
207538943038
reverted ba7392b52a7c: List_Prefix not needed anymore by codatatypes
traytel
parents:
54538
diff
changeset
|
118 |
|
63117 | 119 |
lemma append_one_prefix: |
120 |
"prefix xs ys \<Longrightarrow> length xs < length ys \<Longrightarrow> prefix (xs @ [ys ! length xs]) ys" |
|
82218
cbf9f856d3e0
Some new lemmas and some tidying
paulson <lp15@cam.ac.uk>
parents:
81332
diff
changeset
|
121 |
proof (unfold prefix_def) |
cbf9f856d3e0
Some new lemmas and some tidying
paulson <lp15@cam.ac.uk>
parents:
81332
diff
changeset
|
122 |
assume a1: "\<exists>zs. ys = xs @ zs" |
cbf9f856d3e0
Some new lemmas and some tidying
paulson <lp15@cam.ac.uk>
parents:
81332
diff
changeset
|
123 |
then obtain sk :: "'a list" where sk: "ys = xs @ sk" by fastforce |
cbf9f856d3e0
Some new lemmas and some tidying
paulson <lp15@cam.ac.uk>
parents:
81332
diff
changeset
|
124 |
assume a2: "length xs < length ys" |
cbf9f856d3e0
Some new lemmas and some tidying
paulson <lp15@cam.ac.uk>
parents:
81332
diff
changeset
|
125 |
have f1: "\<And>v. ([]::'a list) @ v = v" using append_Nil2 by simp |
cbf9f856d3e0
Some new lemmas and some tidying
paulson <lp15@cam.ac.uk>
parents:
81332
diff
changeset
|
126 |
have "[] \<noteq> sk" using a1 a2 sk less_not_refl by force |
cbf9f856d3e0
Some new lemmas and some tidying
paulson <lp15@cam.ac.uk>
parents:
81332
diff
changeset
|
127 |
hence "\<exists>v. xs @ hd sk # v = ys" using sk by (metis hd_Cons_tl) |
cbf9f856d3e0
Some new lemmas and some tidying
paulson <lp15@cam.ac.uk>
parents:
81332
diff
changeset
|
128 |
thus "\<exists>zs. ys = (xs @ [ys ! length xs]) @ zs" using f1 by fastforce |
cbf9f856d3e0
Some new lemmas and some tidying
paulson <lp15@cam.ac.uk>
parents:
81332
diff
changeset
|
129 |
qed |
55579
207538943038
reverted ba7392b52a7c: List_Prefix not needed anymore by codatatypes
traytel
parents:
54538
diff
changeset
|
130 |
|
63117 | 131 |
theorem prefix_length_le: "prefix xs ys \<Longrightarrow> length xs \<le> length ys" |
132 |
by (auto simp add: prefix_def) |
|
55579
207538943038
reverted ba7392b52a7c: List_Prefix not needed anymore by codatatypes
traytel
parents:
54538
diff
changeset
|
133 |
|
63117 | 134 |
lemma prefix_same_cases: |
135 |
"prefix (xs\<^sub>1::'a list) ys \<Longrightarrow> prefix xs\<^sub>2 ys \<Longrightarrow> prefix xs\<^sub>1 xs\<^sub>2 \<or> prefix xs\<^sub>2 xs\<^sub>1" |
|
136 |
unfolding prefix_def by (force simp: append_eq_append_conv2) |
|
55579
207538943038
reverted ba7392b52a7c: List_Prefix not needed anymore by codatatypes
traytel
parents:
54538
diff
changeset
|
137 |
|
63173 | 138 |
lemma prefix_length_prefix: |
139 |
"prefix ps xs \<Longrightarrow> prefix qs xs \<Longrightarrow> length ps \<le> length qs \<Longrightarrow> prefix ps qs" |
|
140 |
by (auto simp: prefix_def) (metis append_Nil2 append_eq_append_conv_if) |
|
141 |
||
63117 | 142 |
lemma set_mono_prefix: "prefix xs ys \<Longrightarrow> set xs \<subseteq> set ys" |
143 |
by (auto simp add: prefix_def) |
|
55579
207538943038
reverted ba7392b52a7c: List_Prefix not needed anymore by codatatypes
traytel
parents:
54538
diff
changeset
|
144 |
|
63117 | 145 |
lemma take_is_prefix: "prefix (take n xs) xs" |
146 |
unfolding prefix_def by (metis append_take_drop_id) |
|
55579
207538943038
reverted ba7392b52a7c: List_Prefix not needed anymore by codatatypes
traytel
parents:
54538
diff
changeset
|
147 |
|
73380
99c1c4f89605
added lemmas takeWhile_is_prefix, suffix_dropWhile, and sublist_(take|drop)While
desharna
parents:
73186
diff
changeset
|
148 |
lemma takeWhile_is_prefix: "prefix (takeWhile P xs) xs" |
99c1c4f89605
added lemmas takeWhile_is_prefix, suffix_dropWhile, and sublist_(take|drop)While
desharna
parents:
73186
diff
changeset
|
149 |
unfolding prefix_def by (metis takeWhile_dropWhile_id) |
99c1c4f89605
added lemmas takeWhile_is_prefix, suffix_dropWhile, and sublist_(take|drop)While
desharna
parents:
73186
diff
changeset
|
150 |
|
63155 | 151 |
lemma prefixeq_butlast: "prefix (butlast xs) xs" |
82218
cbf9f856d3e0
Some new lemmas and some tidying
paulson <lp15@cam.ac.uk>
parents:
81332
diff
changeset
|
152 |
by (simp add: butlast_conv_take take_is_prefix) |
63155 | 153 |
|
71789 | 154 |
lemma prefix_map_rightE: |
155 |
assumes "prefix xs (map f ys)" |
|
156 |
shows "\<exists>xs'. prefix xs' ys \<and> xs = map f xs'" |
|
157 |
proof - |
|
158 |
define n where "n = length xs" |
|
159 |
have "xs = take n (map f ys)" |
|
160 |
using assms by (auto simp: prefix_def n_def) |
|
161 |
thus ?thesis |
|
162 |
by (intro exI[of _ "take n ys"]) (auto simp: take_map take_is_prefix) |
|
163 |
qed |
|
164 |
||
67606 | 165 |
lemma map_mono_prefix: "prefix xs ys \<Longrightarrow> prefix (map f xs) (map f ys)" |
82218
cbf9f856d3e0
Some new lemmas and some tidying
paulson <lp15@cam.ac.uk>
parents:
81332
diff
changeset
|
166 |
by (auto simp: prefix_def) |
67606 | 167 |
|
168 |
lemma filter_mono_prefix: "prefix xs ys \<Longrightarrow> prefix (filter P xs) (filter P ys)" |
|
82218
cbf9f856d3e0
Some new lemmas and some tidying
paulson <lp15@cam.ac.uk>
parents:
81332
diff
changeset
|
169 |
by (auto simp: prefix_def) |
55579
207538943038
reverted ba7392b52a7c: List_Prefix not needed anymore by codatatypes
traytel
parents:
54538
diff
changeset
|
170 |
|
67612 | 171 |
lemma sorted_antimono_prefix: "prefix xs ys \<Longrightarrow> sorted ys \<Longrightarrow> sorted xs" |
82218
cbf9f856d3e0
Some new lemmas and some tidying
paulson <lp15@cam.ac.uk>
parents:
81332
diff
changeset
|
172 |
by (metis sorted_append prefix_def) |
67612 | 173 |
|
63117 | 174 |
lemma prefix_length_less: "strict_prefix xs ys \<Longrightarrow> length xs < length ys" |
175 |
by (auto simp: strict_prefix_def prefix_def) |
|
55579
207538943038
reverted ba7392b52a7c: List_Prefix not needed anymore by codatatypes
traytel
parents:
54538
diff
changeset
|
176 |
|
63155 | 177 |
lemma prefix_snocD: "prefix (xs@[x]) ys \<Longrightarrow> strict_prefix xs ys" |
178 |
by (simp add: strict_prefixI' prefix_order.dual_order.strict_trans1) |
|
179 |
||
63117 | 180 |
lemma strict_prefix_simps [simp, code]: |
181 |
"strict_prefix xs [] \<longleftrightarrow> False" |
|
182 |
"strict_prefix [] (x # xs) \<longleftrightarrow> True" |
|
183 |
"strict_prefix (x # xs) (y # ys) \<longleftrightarrow> x = y \<and> strict_prefix xs ys" |
|
184 |
by (simp_all add: strict_prefix_def cong: conj_cong) |
|
55579
207538943038
reverted ba7392b52a7c: List_Prefix not needed anymore by codatatypes
traytel
parents:
54538
diff
changeset
|
185 |
|
63117 | 186 |
lemma take_strict_prefix: "strict_prefix xs ys \<Longrightarrow> strict_prefix (take n xs) ys" |
63649 | 187 |
proof (induct n arbitrary: xs ys) |
188 |
case 0 |
|
189 |
then show ?case by (cases ys) simp_all |
|
190 |
next |
|
191 |
case (Suc n) |
|
192 |
then show ?case by (metis prefix_order.less_trans strict_prefixI take_is_prefix) |
|
193 |
qed |
|
55579
207538943038
reverted ba7392b52a7c: List_Prefix not needed anymore by codatatypes
traytel
parents:
54538
diff
changeset
|
194 |
|
71789 | 195 |
lemma prefix_takeWhile: |
196 |
assumes "prefix xs ys" |
|
197 |
shows "prefix (takeWhile P xs) (takeWhile P ys)" |
|
198 |
proof - |
|
199 |
from assms obtain zs where ys: "ys = xs @ zs" |
|
200 |
by (auto simp: prefix_def) |
|
201 |
have "prefix (takeWhile P xs) (takeWhile P (xs @ zs))" |
|
202 |
by (induction xs) auto |
|
203 |
thus ?thesis by (simp add: ys) |
|
204 |
qed |
|
205 |
||
206 |
lemma prefix_dropWhile: |
|
207 |
assumes "prefix xs ys" |
|
208 |
shows "prefix (dropWhile P xs) (dropWhile P ys)" |
|
209 |
proof - |
|
210 |
from assms obtain zs where ys: "ys = xs @ zs" |
|
211 |
by (auto simp: prefix_def) |
|
212 |
have "prefix (dropWhile P xs) (dropWhile P (xs @ zs))" |
|
213 |
by (induction xs) auto |
|
214 |
thus ?thesis by (simp add: ys) |
|
215 |
qed |
|
216 |
||
217 |
lemma prefix_remdups_adj: |
|
218 |
assumes "prefix xs ys" |
|
219 |
shows "prefix (remdups_adj xs) (remdups_adj ys)" |
|
220 |
using assms |
|
221 |
proof (induction "length xs" arbitrary: xs ys rule: less_induct) |
|
222 |
case (less xs) |
|
223 |
show ?case |
|
224 |
proof (cases xs) |
|
225 |
case [simp]: (Cons x xs') |
|
226 |
then obtain y ys' where [simp]: "ys = y # ys'" |
|
227 |
using \<open>prefix xs ys\<close> by (cases ys) auto |
|
228 |
from less show ?thesis |
|
229 |
by (auto simp: remdups_adj_Cons' less_Suc_eq_le length_dropWhile_le |
|
230 |
intro!: less prefix_dropWhile) |
|
231 |
qed auto |
|
232 |
qed |
|
233 |
||
63117 | 234 |
lemma not_prefix_cases: |
235 |
assumes pfx: "\<not> prefix ps ls" |
|
55579
207538943038
reverted ba7392b52a7c: List_Prefix not needed anymore by codatatypes
traytel
parents:
54538
diff
changeset
|
236 |
obtains |
207538943038
reverted ba7392b52a7c: List_Prefix not needed anymore by codatatypes
traytel
parents:
54538
diff
changeset
|
237 |
(c1) "ps \<noteq> []" and "ls = []" |
63117 | 238 |
| (c2) a as x xs where "ps = a#as" and "ls = x#xs" and "x = a" and "\<not> prefix as xs" |
55579
207538943038
reverted ba7392b52a7c: List_Prefix not needed anymore by codatatypes
traytel
parents:
54538
diff
changeset
|
239 |
| (c3) a as x xs where "ps = a#as" and "ls = x#xs" and "x \<noteq> a" |
207538943038
reverted ba7392b52a7c: List_Prefix not needed anymore by codatatypes
traytel
parents:
54538
diff
changeset
|
240 |
proof (cases ps) |
207538943038
reverted ba7392b52a7c: List_Prefix not needed anymore by codatatypes
traytel
parents:
54538
diff
changeset
|
241 |
case Nil |
207538943038
reverted ba7392b52a7c: List_Prefix not needed anymore by codatatypes
traytel
parents:
54538
diff
changeset
|
242 |
then show ?thesis using pfx by simp |
207538943038
reverted ba7392b52a7c: List_Prefix not needed anymore by codatatypes
traytel
parents:
54538
diff
changeset
|
243 |
next |
207538943038
reverted ba7392b52a7c: List_Prefix not needed anymore by codatatypes
traytel
parents:
54538
diff
changeset
|
244 |
case (Cons a as) |
60500 | 245 |
note c = \<open>ps = a#as\<close> |
55579
207538943038
reverted ba7392b52a7c: List_Prefix not needed anymore by codatatypes
traytel
parents:
54538
diff
changeset
|
246 |
show ?thesis |
207538943038
reverted ba7392b52a7c: List_Prefix not needed anymore by codatatypes
traytel
parents:
54538
diff
changeset
|
247 |
proof (cases ls) |
63117 | 248 |
case Nil then show ?thesis by (metis append_Nil2 pfx c1 same_prefix_nil) |
55579
207538943038
reverted ba7392b52a7c: List_Prefix not needed anymore by codatatypes
traytel
parents:
54538
diff
changeset
|
249 |
next |
207538943038
reverted ba7392b52a7c: List_Prefix not needed anymore by codatatypes
traytel
parents:
54538
diff
changeset
|
250 |
case (Cons x xs) |
207538943038
reverted ba7392b52a7c: List_Prefix not needed anymore by codatatypes
traytel
parents:
54538
diff
changeset
|
251 |
show ?thesis |
207538943038
reverted ba7392b52a7c: List_Prefix not needed anymore by codatatypes
traytel
parents:
54538
diff
changeset
|
252 |
proof (cases "x = a") |
207538943038
reverted ba7392b52a7c: List_Prefix not needed anymore by codatatypes
traytel
parents:
54538
diff
changeset
|
253 |
case True |
63117 | 254 |
have "\<not> prefix as xs" using pfx c Cons True by simp |
55579
207538943038
reverted ba7392b52a7c: List_Prefix not needed anymore by codatatypes
traytel
parents:
54538
diff
changeset
|
255 |
with c Cons True show ?thesis by (rule c2) |
207538943038
reverted ba7392b52a7c: List_Prefix not needed anymore by codatatypes
traytel
parents:
54538
diff
changeset
|
256 |
next |
207538943038
reverted ba7392b52a7c: List_Prefix not needed anymore by codatatypes
traytel
parents:
54538
diff
changeset
|
257 |
case False |
207538943038
reverted ba7392b52a7c: List_Prefix not needed anymore by codatatypes
traytel
parents:
54538
diff
changeset
|
258 |
with c Cons show ?thesis by (rule c3) |
207538943038
reverted ba7392b52a7c: List_Prefix not needed anymore by codatatypes
traytel
parents:
54538
diff
changeset
|
259 |
qed |
207538943038
reverted ba7392b52a7c: List_Prefix not needed anymore by codatatypes
traytel
parents:
54538
diff
changeset
|
260 |
qed |
207538943038
reverted ba7392b52a7c: List_Prefix not needed anymore by codatatypes
traytel
parents:
54538
diff
changeset
|
261 |
qed |
207538943038
reverted ba7392b52a7c: List_Prefix not needed anymore by codatatypes
traytel
parents:
54538
diff
changeset
|
262 |
|
63117 | 263 |
lemma not_prefix_induct [consumes 1, case_names Nil Neq Eq]: |
264 |
assumes np: "\<not> prefix ps ls" |
|
55579
207538943038
reverted ba7392b52a7c: List_Prefix not needed anymore by codatatypes
traytel
parents:
54538
diff
changeset
|
265 |
and base: "\<And>x xs. P (x#xs) []" |
207538943038
reverted ba7392b52a7c: List_Prefix not needed anymore by codatatypes
traytel
parents:
54538
diff
changeset
|
266 |
and r1: "\<And>x xs y ys. x \<noteq> y \<Longrightarrow> P (x#xs) (y#ys)" |
63117 | 267 |
and r2: "\<And>x xs y ys. \<lbrakk> x = y; \<not> prefix xs ys; P xs ys \<rbrakk> \<Longrightarrow> P (x#xs) (y#ys)" |
55579
207538943038
reverted ba7392b52a7c: List_Prefix not needed anymore by codatatypes
traytel
parents:
54538
diff
changeset
|
268 |
shows "P ps ls" using np |
207538943038
reverted ba7392b52a7c: List_Prefix not needed anymore by codatatypes
traytel
parents:
54538
diff
changeset
|
269 |
proof (induct ls arbitrary: ps) |
63649 | 270 |
case Nil |
271 |
then show ?case |
|
63117 | 272 |
by (auto simp: neq_Nil_conv elim!: not_prefix_cases intro!: base) |
55579
207538943038
reverted ba7392b52a7c: List_Prefix not needed anymore by codatatypes
traytel
parents:
54538
diff
changeset
|
273 |
next |
207538943038
reverted ba7392b52a7c: List_Prefix not needed anymore by codatatypes
traytel
parents:
54538
diff
changeset
|
274 |
case (Cons y ys) |
63117 | 275 |
then have npfx: "\<not> prefix ps (y # ys)" by simp |
55579
207538943038
reverted ba7392b52a7c: List_Prefix not needed anymore by codatatypes
traytel
parents:
54538
diff
changeset
|
276 |
then obtain x xs where pv: "ps = x # xs" |
63117 | 277 |
by (rule not_prefix_cases) auto |
278 |
show ?case by (metis Cons.hyps Cons_prefix_Cons npfx pv r1 r2) |
|
55579
207538943038
reverted ba7392b52a7c: List_Prefix not needed anymore by codatatypes
traytel
parents:
54538
diff
changeset
|
279 |
qed |
207538943038
reverted ba7392b52a7c: List_Prefix not needed anymore by codatatypes
traytel
parents:
54538
diff
changeset
|
280 |
|
207538943038
reverted ba7392b52a7c: List_Prefix not needed anymore by codatatypes
traytel
parents:
54538
diff
changeset
|
281 |
|
63155 | 282 |
subsection \<open>Prefixes\<close> |
283 |
||
65956
639eb3617a86
reorganised material on sublists
eberlm <eberlm@in.tum.de>
parents:
65954
diff
changeset
|
284 |
primrec prefixes where |
82218
cbf9f856d3e0
Some new lemmas and some tidying
paulson <lp15@cam.ac.uk>
parents:
81332
diff
changeset
|
285 |
"prefixes [] = [[]]" | |
cbf9f856d3e0
Some new lemmas and some tidying
paulson <lp15@cam.ac.uk>
parents:
81332
diff
changeset
|
286 |
"prefixes (x#xs) = [] # map ((#) x) (prefixes xs)" |
63155 | 287 |
|
288 |
lemma in_set_prefixes[simp]: "xs \<in> set (prefixes ys) \<longleftrightarrow> prefix xs ys" |
|
63649 | 289 |
proof (induct xs arbitrary: ys) |
290 |
case Nil |
|
291 |
then show ?case by (cases ys) auto |
|
292 |
next |
|
293 |
case (Cons a xs) |
|
294 |
then show ?case by (cases ys) auto |
|
295 |
qed |
|
63155 | 296 |
|
297 |
lemma length_prefixes[simp]: "length (prefixes xs) = length xs+1" |
|
65869 | 298 |
by (induction xs) auto |
81332 | 299 |
|
65869 | 300 |
lemma distinct_prefixes [intro]: "distinct (prefixes xs)" |
301 |
by (induction xs) (auto simp: distinct_map) |
|
302 |
||
303 |
lemma prefixes_snoc [simp]: "prefixes (xs@[x]) = prefixes xs @ [xs@[x]]" |
|
304 |
by (induction xs) auto |
|
305 |
||
306 |
lemma prefixes_not_Nil [simp]: "prefixes xs \<noteq> []" |
|
307 |
by (cases xs) auto |
|
63155 | 308 |
|
65869 | 309 |
lemma hd_prefixes [simp]: "hd (prefixes xs) = []" |
310 |
by (cases xs) simp_all |
|
63155 | 311 |
|
65869 | 312 |
lemma last_prefixes [simp]: "last (prefixes xs) = xs" |
313 |
by (induction xs) (simp_all add: last_map) |
|
81332 | 314 |
|
315 |
lemma prefixes_append: |
|
65869 | 316 |
"prefixes (xs @ ys) = prefixes xs @ map (\<lambda>ys'. xs @ ys') (tl (prefixes ys))" |
317 |
proof (induction xs) |
|
318 |
case Nil |
|
319 |
thus ?case by (cases ys) auto |
|
320 |
qed simp_all |
|
321 |
||
322 |
lemma prefixes_eq_snoc: |
|
63155 | 323 |
"prefixes ys = xs @ [x] \<longleftrightarrow> |
324 |
(ys = [] \<and> xs = [] \<or> (\<exists>z zs. ys = zs@[z] \<and> xs = prefixes zs)) \<and> x = ys" |
|
65869 | 325 |
by (cases ys rule: rev_cases) auto |
326 |
||
81332 | 327 |
lemma prefixes_tailrec [code]: |
65869 | 328 |
"prefixes xs = rev (snd (foldl (\<lambda>(acc1, acc2) x. (x#acc1, rev (x#acc1)#acc2)) ([],[[]]) xs))" |
329 |
proof - |
|
330 |
have "foldl (\<lambda>(acc1, acc2) x. (x#acc1, rev (x#acc1)#acc2)) (ys, rev ys # zs) xs = |
|
331 |
(rev xs @ ys, rev (map (\<lambda>as. rev ys @ as) (prefixes xs)) @ zs)" for ys zs |
|
332 |
proof (induction xs arbitrary: ys zs) |
|
333 |
case (Cons x xs ys zs) |
|
334 |
from Cons.IH[of "x # ys" "rev ys # zs"] |
|
335 |
show ?case by (simp add: o_def) |
|
336 |
qed simp_all |
|
337 |
from this [of "[]" "[]"] show ?thesis by simp |
|
338 |
qed |
|
81332 | 339 |
|
65869 | 340 |
lemma set_prefixes_eq: "set (prefixes xs) = {ys. prefix ys xs}" |
341 |
by auto |
|
342 |
||
343 |
lemma card_set_prefixes [simp]: "card (set (prefixes xs)) = Suc (length xs)" |
|
344 |
by (subst distinct_card) auto |
|
345 |
||
81332 | 346 |
lemma set_prefixes_append: |
65869 | 347 |
"set (prefixes (xs @ ys)) = set (prefixes xs) \<union> {xs @ ys' |ys'. ys' \<in> set (prefixes ys)}" |
348 |
by (subst prefixes_append, cases ys) auto |
|
63155 | 349 |
|
350 |
||
63173 | 351 |
subsection \<open>Longest Common Prefix\<close> |
352 |
||
353 |
definition Longest_common_prefix :: "'a list set \<Rightarrow> 'a list" where |
|
65954 | 354 |
"Longest_common_prefix L = (ARG_MAX length ps. \<forall>xs \<in> L. prefix ps xs)" |
63173 | 355 |
|
356 |
lemma Longest_common_prefix_ex: "L \<noteq> {} \<Longrightarrow> |
|
357 |
\<exists>ps. (\<forall>xs \<in> L. prefix ps xs) \<and> (\<forall>qs. (\<forall>xs \<in> L. prefix qs xs) \<longrightarrow> size qs \<le> size ps)" |
|
358 |
(is "_ \<Longrightarrow> \<exists>ps. ?P L ps") |
|
359 |
proof(induction "LEAST n. \<exists>xs \<in>L. n = length xs" arbitrary: L) |
|
360 |
case 0 |
|
67613 | 361 |
have "[] \<in> L" using "0.hyps" LeastI[of "\<lambda>n. \<exists>xs\<in>L. n = length xs"] \<open>L \<noteq> {}\<close> |
63173 | 362 |
by auto |
363 |
hence "?P L []" by(auto) |
|
364 |
thus ?case .. |
|
365 |
next |
|
366 |
case (Suc n) |
|
367 |
let ?EX = "\<lambda>n. \<exists>xs\<in>L. n = length xs" |
|
368 |
obtain x xs where xxs: "x#xs \<in> L" "size xs = n" using Suc.prems Suc.hyps(2) |
|
369 |
by(metis LeastI_ex[of ?EX] Suc_length_conv ex_in_conv) |
|
370 |
hence "[] \<notin> L" using Suc.hyps(2) by auto |
|
371 |
show ?case |
|
372 |
proof (cases "\<forall>xs \<in> L. \<exists>ys. xs = x#ys") |
|
373 |
case True |
|
374 |
let ?L = "{ys. x#ys \<in> L}" |
|
375 |
have 1: "(LEAST n. \<exists>xs \<in> ?L. n = length xs) = n" |
|
376 |
using xxs Suc.prems Suc.hyps(2) Least_le[of "?EX"] |
|
377 |
by - (rule Least_equality, fastforce+) |
|
378 |
have 2: "?L \<noteq> {}" using \<open>x # xs \<in> L\<close> by auto |
|
379 |
from Suc.hyps(1)[OF 1[symmetric] 2] obtain ps where IH: "?P ?L ps" .. |
|
81332 | 380 |
have "length qs \<le> Suc (length ps)" |
381 |
if "\<forall>qs. (\<forall>xa. x # xa \<in> L \<longrightarrow> prefix qs xa) \<longrightarrow> length qs \<le> length ps" |
|
382 |
and "\<forall>xs\<in>L. prefix qs xs" for qs |
|
383 |
proof - |
|
384 |
from that have "length (tl qs) \<le> length ps" |
|
385 |
by (metis Cons_prefix_Cons hd_Cons_tl list.sel(2) Nil_prefix) |
|
386 |
thus ?thesis by auto |
|
387 |
qed |
|
63173 | 388 |
hence "?P L (x#ps)" using True IH by auto |
389 |
thus ?thesis .. |
|
390 |
next |
|
391 |
case False |
|
392 |
then obtain y ys where yys: "x\<noteq>y" "y#ys \<in> L" using \<open>[] \<notin> L\<close> |
|
393 |
by (auto) (metis list.exhaust) |
|
394 |
have "\<forall>qs. (\<forall>xs\<in>L. prefix qs xs) \<longrightarrow> qs = []" using yys \<open>x#xs \<in> L\<close> |
|
395 |
by auto (metis Cons_prefix_Cons prefix_Cons) |
|
396 |
hence "?P L []" by auto |
|
397 |
thus ?thesis .. |
|
398 |
qed |
|
399 |
qed |
|
400 |
||
73411 | 401 |
lemma Longest_common_prefix_unique: |
402 |
\<open>\<exists>! ps. (\<forall>xs \<in> L. prefix ps xs) \<and> (\<forall>qs. (\<forall>xs \<in> L. prefix qs xs) \<longrightarrow> length qs \<le> length ps)\<close> |
|
403 |
if \<open>L \<noteq> {}\<close> |
|
82218
cbf9f856d3e0
Some new lemmas and some tidying
paulson <lp15@cam.ac.uk>
parents:
81332
diff
changeset
|
404 |
apply (intro ex_ex1I[OF Longest_common_prefix_ex [OF that]]) |
cbf9f856d3e0
Some new lemmas and some tidying
paulson <lp15@cam.ac.uk>
parents:
81332
diff
changeset
|
405 |
by (meson that all_not_in_conv prefix_length_prefix prefix_order.dual_order.eq_iff) |
63173 | 406 |
|
407 |
lemma Longest_common_prefix_eq: |
|
82218
cbf9f856d3e0
Some new lemmas and some tidying
paulson <lp15@cam.ac.uk>
parents:
81332
diff
changeset
|
408 |
"\<lbrakk> L \<noteq> {}; \<forall>xs \<in> L. prefix ps xs; |
63173 | 409 |
\<forall>qs. (\<forall>xs \<in> L. prefix qs xs) \<longrightarrow> size qs \<le> size ps \<rbrakk> |
410 |
\<Longrightarrow> Longest_common_prefix L = ps" |
|
82218
cbf9f856d3e0
Some new lemmas and some tidying
paulson <lp15@cam.ac.uk>
parents:
81332
diff
changeset
|
411 |
unfolding Longest_common_prefix_def arg_max_def is_arg_max_linorder |
cbf9f856d3e0
Some new lemmas and some tidying
paulson <lp15@cam.ac.uk>
parents:
81332
diff
changeset
|
412 |
by(rule some1_equality[OF Longest_common_prefix_unique]) auto |
63173 | 413 |
|
414 |
lemma Longest_common_prefix_prefix: |
|
415 |
"xs \<in> L \<Longrightarrow> prefix (Longest_common_prefix L) xs" |
|
82218
cbf9f856d3e0
Some new lemmas and some tidying
paulson <lp15@cam.ac.uk>
parents:
81332
diff
changeset
|
416 |
unfolding Longest_common_prefix_def arg_max_def is_arg_max_linorder |
cbf9f856d3e0
Some new lemmas and some tidying
paulson <lp15@cam.ac.uk>
parents:
81332
diff
changeset
|
417 |
by(rule someI2_ex[OF Longest_common_prefix_ex]) auto |
63173 | 418 |
|
419 |
lemma Longest_common_prefix_longest: |
|
420 |
"L \<noteq> {} \<Longrightarrow> \<forall>xs\<in>L. prefix ps xs \<Longrightarrow> length ps \<le> length(Longest_common_prefix L)" |
|
82218
cbf9f856d3e0
Some new lemmas and some tidying
paulson <lp15@cam.ac.uk>
parents:
81332
diff
changeset
|
421 |
unfolding Longest_common_prefix_def arg_max_def is_arg_max_linorder |
cbf9f856d3e0
Some new lemmas and some tidying
paulson <lp15@cam.ac.uk>
parents:
81332
diff
changeset
|
422 |
by(rule someI2_ex[OF Longest_common_prefix_ex]) auto |
63173 | 423 |
|
424 |
lemma Longest_common_prefix_max_prefix: |
|
425 |
"L \<noteq> {} \<Longrightarrow> \<forall>xs\<in>L. prefix ps xs \<Longrightarrow> prefix ps (Longest_common_prefix L)" |
|
82218
cbf9f856d3e0
Some new lemmas and some tidying
paulson <lp15@cam.ac.uk>
parents:
81332
diff
changeset
|
426 |
by(metis Longest_common_prefix_prefix Longest_common_prefix_longest |
cbf9f856d3e0
Some new lemmas and some tidying
paulson <lp15@cam.ac.uk>
parents:
81332
diff
changeset
|
427 |
prefix_length_prefix ex_in_conv) |
63173 | 428 |
|
429 |
lemma Longest_common_prefix_Nil: "[] \<in> L \<Longrightarrow> Longest_common_prefix L = []" |
|
82218
cbf9f856d3e0
Some new lemmas and some tidying
paulson <lp15@cam.ac.uk>
parents:
81332
diff
changeset
|
430 |
using Longest_common_prefix_prefix prefix_Nil by blast |
63173 | 431 |
|
82218
cbf9f856d3e0
Some new lemmas and some tidying
paulson <lp15@cam.ac.uk>
parents:
81332
diff
changeset
|
432 |
lemma Longest_common_prefix_image_Cons: |
cbf9f856d3e0
Some new lemmas and some tidying
paulson <lp15@cam.ac.uk>
parents:
81332
diff
changeset
|
433 |
assumes "L \<noteq> {}" |
cbf9f856d3e0
Some new lemmas and some tidying
paulson <lp15@cam.ac.uk>
parents:
81332
diff
changeset
|
434 |
shows "Longest_common_prefix ((#) x ` L) = x # Longest_common_prefix L" |
cbf9f856d3e0
Some new lemmas and some tidying
paulson <lp15@cam.ac.uk>
parents:
81332
diff
changeset
|
435 |
proof (intro Longest_common_prefix_eq strip) |
cbf9f856d3e0
Some new lemmas and some tidying
paulson <lp15@cam.ac.uk>
parents:
81332
diff
changeset
|
436 |
show "\<And>qs. \<forall>xs\<in>(#) x ` L. prefix qs xs \<Longrightarrow> |
cbf9f856d3e0
Some new lemmas and some tidying
paulson <lp15@cam.ac.uk>
parents:
81332
diff
changeset
|
437 |
length qs \<le> length (x # Longest_common_prefix L)" |
cbf9f856d3e0
Some new lemmas and some tidying
paulson <lp15@cam.ac.uk>
parents:
81332
diff
changeset
|
438 |
by (metis assms Longest_common_prefix_longest[of L] Cons_prefix_Cons Suc_le_mono hd_Cons_tl |
cbf9f856d3e0
Some new lemmas and some tidying
paulson <lp15@cam.ac.uk>
parents:
81332
diff
changeset
|
439 |
image_eqI length_Cons prefix_bot.bot_least prefix_length_le) |
cbf9f856d3e0
Some new lemmas and some tidying
paulson <lp15@cam.ac.uk>
parents:
81332
diff
changeset
|
440 |
qed (auto simp add: assms Longest_common_prefix_prefix) |
63173 | 441 |
|
442 |
lemma Longest_common_prefix_eq_Cons: assumes "L \<noteq> {}" "[] \<notin> L" "\<forall>xs\<in>L. hd xs = x" |
|
443 |
shows "Longest_common_prefix L = x # Longest_common_prefix {ys. x#ys \<in> L}" |
|
444 |
proof - |
|
67399 | 445 |
have "L = (#) x ` {ys. x#ys \<in> L}" using assms(2,3) |
63173 | 446 |
by (auto simp: image_def)(metis hd_Cons_tl) |
447 |
thus ?thesis |
|
448 |
by (metis Longest_common_prefix_image_Cons image_is_empty assms(1)) |
|
449 |
qed |
|
450 |
||
451 |
lemma Longest_common_prefix_eq_Nil: |
|
452 |
"\<lbrakk>x#ys \<in> L; y#zs \<in> L; x \<noteq> y \<rbrakk> \<Longrightarrow> Longest_common_prefix L = []" |
|
82218
cbf9f856d3e0
Some new lemmas and some tidying
paulson <lp15@cam.ac.uk>
parents:
81332
diff
changeset
|
453 |
by (metis Longest_common_prefix_prefix list.inject prefix_Cons) |
63173 | 454 |
|
455 |
fun longest_common_prefix :: "'a list \<Rightarrow> 'a list \<Rightarrow> 'a list" where |
|
82218
cbf9f856d3e0
Some new lemmas and some tidying
paulson <lp15@cam.ac.uk>
parents:
81332
diff
changeset
|
456 |
"longest_common_prefix (x#xs) (y#ys) = |
63173 | 457 |
(if x=y then x # longest_common_prefix xs ys else [])" | |
82218
cbf9f856d3e0
Some new lemmas and some tidying
paulson <lp15@cam.ac.uk>
parents:
81332
diff
changeset
|
458 |
"longest_common_prefix _ _ = []" |
63173 | 459 |
|
460 |
lemma longest_common_prefix_prefix1: |
|
461 |
"prefix (longest_common_prefix xs ys) xs" |
|
82218
cbf9f856d3e0
Some new lemmas and some tidying
paulson <lp15@cam.ac.uk>
parents:
81332
diff
changeset
|
462 |
by(induction xs ys rule: longest_common_prefix.induct) auto |
63173 | 463 |
|
464 |
lemma longest_common_prefix_prefix2: |
|
465 |
"prefix (longest_common_prefix xs ys) ys" |
|
82218
cbf9f856d3e0
Some new lemmas and some tidying
paulson <lp15@cam.ac.uk>
parents:
81332
diff
changeset
|
466 |
by(induction xs ys rule: longest_common_prefix.induct) auto |
63173 | 467 |
|
468 |
lemma longest_common_prefix_max_prefix: |
|
469 |
"\<lbrakk> prefix ps xs; prefix ps ys \<rbrakk> |
|
470 |
\<Longrightarrow> prefix ps (longest_common_prefix xs ys)" |
|
82218
cbf9f856d3e0
Some new lemmas and some tidying
paulson <lp15@cam.ac.uk>
parents:
81332
diff
changeset
|
471 |
by(induction xs ys arbitrary: ps rule: longest_common_prefix.induct) |
cbf9f856d3e0
Some new lemmas and some tidying
paulson <lp15@cam.ac.uk>
parents:
81332
diff
changeset
|
472 |
(auto simp: prefix_Cons) |
63173 | 473 |
|
474 |
||
60500 | 475 |
subsection \<open>Parallel lists\<close> |
10389 | 476 |
|
80914
d97fdabd9e2b
standardize mixfix annotations via "isabelle update -a -u mixfix_cartouches" --- to simplify systematic editing;
wenzelm
parents:
75564
diff
changeset
|
477 |
definition parallel :: "'a list \<Rightarrow> 'a list \<Rightarrow> bool" (infixl \<open>\<parallel>\<close> 50) |
63117 | 478 |
where "(xs \<parallel> ys) = (\<not> prefix xs ys \<and> \<not> prefix ys xs)" |
10389 | 479 |
|
63117 | 480 |
lemma parallelI [intro]: "\<not> prefix xs ys \<Longrightarrow> \<not> prefix ys xs \<Longrightarrow> xs \<parallel> ys" |
25692 | 481 |
unfolding parallel_def by blast |
10330
4362e906b745
"List prefixes" library theory (replaces old Lex/Prefix);
wenzelm
parents:
diff
changeset
|
482 |
|
10389 | 483 |
lemma parallelE [elim]: |
25692 | 484 |
assumes "xs \<parallel> ys" |
63117 | 485 |
obtains "\<not> prefix xs ys \<and> \<not> prefix ys xs" |
25692 | 486 |
using assms unfolding parallel_def by blast |
10330
4362e906b745
"List prefixes" library theory (replaces old Lex/Prefix);
wenzelm
parents:
diff
changeset
|
487 |
|
63117 | 488 |
theorem prefix_cases: |
489 |
obtains "prefix xs ys" | "strict_prefix ys xs" | "xs \<parallel> ys" |
|
490 |
unfolding parallel_def strict_prefix_def by blast |
|
10330
4362e906b745
"List prefixes" library theory (replaces old Lex/Prefix);
wenzelm
parents:
diff
changeset
|
491 |
|
73186 | 492 |
lemma parallel_cancel: "a#xs \<parallel> a#ys \<Longrightarrow> xs \<parallel> ys" |
493 |
by (simp add: parallel_def) |
|
494 |
||
10389 | 495 |
theorem parallel_decomp: |
50516 | 496 |
"xs \<parallel> ys \<Longrightarrow> \<exists>as b bs c cs. b \<noteq> c \<and> xs = as @ b # bs \<and> ys = as @ c # cs" |
73186 | 497 |
proof (induct rule: list_induct2', blast, force, force) |
498 |
case (4 x xs y ys) |
|
499 |
then show ?case |
|
500 |
proof (cases "x \<noteq> y", blast) |
|
501 |
assume "\<not> x \<noteq> y" hence "x = y" by blast |
|
502 |
then show ?thesis |
|
503 |
using "4.hyps"[OF parallel_cancel[OF "4.prems"[folded \<open>x = y\<close>]]] |
|
504 |
by (meson Cons_eq_appendI) |
|
10389 | 505 |
qed |
506 |
qed |
|
10330
4362e906b745
"List prefixes" library theory (replaces old Lex/Prefix);
wenzelm
parents:
diff
changeset
|
507 |
|
25564 | 508 |
lemma parallel_append: "a \<parallel> b \<Longrightarrow> a @ c \<parallel> b @ d" |
82218
cbf9f856d3e0
Some new lemmas and some tidying
paulson <lp15@cam.ac.uk>
parents:
81332
diff
changeset
|
509 |
by (meson parallelE parallelI prefixI prefix_order.trans prefix_same_cases) |
25299 | 510 |
|
25692 | 511 |
lemma parallel_appendI: "xs \<parallel> ys \<Longrightarrow> x = xs @ xs' \<Longrightarrow> y = ys @ ys' \<Longrightarrow> x \<parallel> y" |
512 |
by (simp add: parallel_append) |
|
25299 | 513 |
|
25692 | 514 |
lemma parallel_commute: "a \<parallel> b \<longleftrightarrow> b \<parallel> a" |
515 |
unfolding parallel_def by auto |
|
14538
1d9d75a8efae
removed o2l and fold_rel; moved postfix to Library/List_Prefix.thy
oheimb
parents:
14300
diff
changeset
|
516 |
|
25356 | 517 |
|
60500 | 518 |
subsection \<open>Suffix order on lists\<close> |
17201 | 519 |
|
63149 | 520 |
definition suffix :: "'a list \<Rightarrow> 'a list \<Rightarrow> bool" |
521 |
where "suffix xs ys = (\<exists>zs. ys = zs @ xs)" |
|
49087 | 522 |
|
63149 | 523 |
definition strict_suffix :: "'a list \<Rightarrow> 'a list \<Rightarrow> bool" |
65869 | 524 |
where "strict_suffix xs ys \<longleftrightarrow> suffix xs ys \<and> xs \<noteq> ys" |
14538
1d9d75a8efae
removed o2l and fold_rel; moved postfix to Library/List_Prefix.thy
oheimb
parents:
14300
diff
changeset
|
525 |
|
73411 | 526 |
global_interpretation suffix_order: ordering suffix strict_suffix |
527 |
by standard (auto simp: suffix_def strict_suffix_def) |
|
528 |
||
65869 | 529 |
interpretation suffix_order: order suffix strict_suffix |
530 |
by standard (auto simp: suffix_def strict_suffix_def) |
|
531 |
||
73411 | 532 |
global_interpretation suffix_bot: ordering_top \<open>\<lambda>xs ys. suffix ys xs\<close> \<open>\<lambda>xs ys. strict_suffix ys xs\<close> \<open>[]\<close> |
533 |
by standard (simp add: suffix_def) |
|
534 |
||
65869 | 535 |
interpretation suffix_bot: order_bot Nil suffix strict_suffix |
536 |
by standard (simp add: suffix_def) |
|
49087 | 537 |
|
63149 | 538 |
lemma suffixI [intro?]: "ys = zs @ xs \<Longrightarrow> suffix xs ys" |
539 |
unfolding suffix_def by blast |
|
21305 | 540 |
|
63149 | 541 |
lemma suffixE [elim?]: |
542 |
assumes "suffix xs ys" |
|
49087 | 543 |
obtains zs where "ys = zs @ xs" |
63149 | 544 |
using assms unfolding suffix_def by blast |
81332 | 545 |
|
63149 | 546 |
lemma suffix_tl [simp]: "suffix (tl xs) xs" |
49087 | 547 |
by (induct xs) (auto simp: suffix_def) |
14538
1d9d75a8efae
removed o2l and fold_rel; moved postfix to Library/List_Prefix.thy
oheimb
parents:
14300
diff
changeset
|
548 |
|
63149 | 549 |
lemma strict_suffix_tl [simp]: "xs \<noteq> [] \<Longrightarrow> strict_suffix (tl xs) xs" |
65869 | 550 |
by (induct xs) (auto simp: strict_suffix_def suffix_def) |
63149 | 551 |
|
65869 | 552 |
lemma Nil_suffix [simp]: "suffix [] xs" |
63149 | 553 |
by (simp add: suffix_def) |
49087 | 554 |
|
63149 | 555 |
lemma suffix_Nil [simp]: "(suffix xs []) = (xs = [])" |
556 |
by (auto simp add: suffix_def) |
|
557 |
||
558 |
lemma suffix_ConsI: "suffix xs ys \<Longrightarrow> suffix xs (y # ys)" |
|
559 |
by (auto simp add: suffix_def) |
|
560 |
||
561 |
lemma suffix_ConsD: "suffix (x # xs) ys \<Longrightarrow> suffix xs ys" |
|
562 |
by (auto simp add: suffix_def) |
|
14538
1d9d75a8efae
removed o2l and fold_rel; moved postfix to Library/List_Prefix.thy
oheimb
parents:
14300
diff
changeset
|
563 |
|
63149 | 564 |
lemma suffix_appendI: "suffix xs ys \<Longrightarrow> suffix xs (zs @ ys)" |
565 |
by (auto simp add: suffix_def) |
|
566 |
||
567 |
lemma suffix_appendD: "suffix (zs @ xs) ys \<Longrightarrow> suffix xs ys" |
|
568 |
by (auto simp add: suffix_def) |
|
49087 | 569 |
|
63149 | 570 |
lemma strict_suffix_set_subset: "strict_suffix xs ys \<Longrightarrow> set xs \<subseteq> set ys" |
65869 | 571 |
by (auto simp: strict_suffix_def suffix_def) |
14538
1d9d75a8efae
removed o2l and fold_rel; moved postfix to Library/List_Prefix.thy
oheimb
parents:
14300
diff
changeset
|
572 |
|
67606 | 573 |
lemma set_mono_suffix: "suffix xs ys \<Longrightarrow> set xs \<subseteq> set ys" |
574 |
by (auto simp: suffix_def) |
|
49087 | 575 |
|
67612 | 576 |
lemma sorted_antimono_suffix: "suffix xs ys \<Longrightarrow> sorted ys \<Longrightarrow> sorted xs" |
577 |
by (metis sorted_append suffix_def) |
|
578 |
||
63149 | 579 |
lemma suffix_ConsD2: "suffix (x # xs) (y # ys) \<Longrightarrow> suffix xs ys" |
21305 | 580 |
proof - |
63149 | 581 |
assume "suffix (x # xs) (y # ys)" |
49107 | 582 |
then obtain zs where "y # ys = zs @ x # xs" .. |
49087 | 583 |
then show ?thesis |
63149 | 584 |
by (induct zs) (auto intro!: suffix_appendI suffix_ConsI) |
21305 | 585 |
qed |
14538
1d9d75a8efae
removed o2l and fold_rel; moved postfix to Library/List_Prefix.thy
oheimb
parents:
14300
diff
changeset
|
586 |
|
63149 | 587 |
lemma suffix_to_prefix [code]: "suffix xs ys \<longleftrightarrow> prefix (rev xs) (rev ys)" |
49087 | 588 |
proof |
63149 | 589 |
assume "suffix xs ys" |
49087 | 590 |
then obtain zs where "ys = zs @ xs" .. |
591 |
then have "rev ys = rev xs @ rev zs" by simp |
|
63117 | 592 |
then show "prefix (rev xs) (rev ys)" .. |
49087 | 593 |
next |
63117 | 594 |
assume "prefix (rev xs) (rev ys)" |
49087 | 595 |
then obtain zs where "rev ys = rev xs @ zs" .. |
596 |
then have "rev (rev ys) = rev zs @ rev (rev xs)" by simp |
|
597 |
then have "ys = rev zs @ xs" by simp |
|
63149 | 598 |
then show "suffix xs ys" .. |
21305 | 599 |
qed |
81332 | 600 |
|
65869 | 601 |
lemma strict_suffix_to_prefix [code]: "strict_suffix xs ys \<longleftrightarrow> strict_prefix (rev xs) (rev ys)" |
602 |
by (auto simp: suffix_to_prefix strict_suffix_def strict_prefix_def) |
|
14538
1d9d75a8efae
removed o2l and fold_rel; moved postfix to Library/List_Prefix.thy
oheimb
parents:
14300
diff
changeset
|
603 |
|
63149 | 604 |
lemma distinct_suffix: "distinct ys \<Longrightarrow> suffix xs ys \<Longrightarrow> distinct xs" |
605 |
by (clarsimp elim!: suffixE) |
|
17201 | 606 |
|
67606 | 607 |
lemma map_mono_suffix: "suffix xs ys \<Longrightarrow> suffix (map f xs) (map f ys)" |
608 |
by (auto elim!: suffixE intro: suffixI) |
|
609 |
||
75564 | 610 |
lemma map_mono_strict_suffix: "strict_suffix xs ys \<Longrightarrow> strict_suffix (map f xs) (map f ys)" |
611 |
by (auto simp: strict_suffix_def suffix_def) |
|
612 |
||
67606 | 613 |
lemma filter_mono_suffix: "suffix xs ys \<Longrightarrow> suffix (filter P xs) (filter P ys)" |
614 |
by (auto simp: suffix_def) |
|
25299 | 615 |
|
63149 | 616 |
lemma suffix_drop: "suffix (drop n as) as" |
73380
99c1c4f89605
added lemmas takeWhile_is_prefix, suffix_dropWhile, and sublist_(take|drop)While
desharna
parents:
73186
diff
changeset
|
617 |
unfolding suffix_def by (metis append_take_drop_id) |
99c1c4f89605
added lemmas takeWhile_is_prefix, suffix_dropWhile, and sublist_(take|drop)While
desharna
parents:
73186
diff
changeset
|
618 |
|
99c1c4f89605
added lemmas takeWhile_is_prefix, suffix_dropWhile, and sublist_(take|drop)While
desharna
parents:
73186
diff
changeset
|
619 |
lemma suffix_dropWhile: "suffix (dropWhile P xs) xs" |
99c1c4f89605
added lemmas takeWhile_is_prefix, suffix_dropWhile, and sublist_(take|drop)While
desharna
parents:
73186
diff
changeset
|
620 |
unfolding suffix_def by (metis takeWhile_dropWhile_id) |
25299 | 621 |
|
63149 | 622 |
lemma suffix_take: "suffix xs ys \<Longrightarrow> ys = take (length ys - length xs) ys @ xs" |
623 |
by (auto elim!: suffixE) |
|
25299 | 624 |
|
63149 | 625 |
lemma strict_suffix_reflclp_conv: "strict_suffix\<^sup>=\<^sup>= = suffix" |
65869 | 626 |
by (intro ext) (auto simp: suffix_def strict_suffix_def) |
63149 | 627 |
|
628 |
lemma suffix_lists: "suffix xs ys \<Longrightarrow> ys \<in> lists A \<Longrightarrow> xs \<in> lists A" |
|
629 |
unfolding suffix_def by auto |
|
49087 | 630 |
|
65869 | 631 |
lemma suffix_snoc [simp]: "suffix xs (ys @ [y]) \<longleftrightarrow> xs = [] \<or> (\<exists>zs. xs = zs @ [y] \<and> suffix zs ys)" |
632 |
by (cases xs rule: rev_cases) (auto simp: suffix_def) |
|
633 |
||
634 |
lemma snoc_suffix_snoc [simp]: "suffix (xs @ [x]) (ys @ [y]) = (x = y \<and> suffix xs ys)" |
|
635 |
by (auto simp add: suffix_def) |
|
636 |
||
637 |
lemma same_suffix_suffix [simp]: "suffix (ys @ xs) (zs @ xs) = suffix ys zs" |
|
638 |
by (simp add: suffix_to_prefix) |
|
639 |
||
640 |
lemma same_suffix_nil [simp]: "suffix (ys @ xs) xs = (ys = [])" |
|
641 |
by (simp add: suffix_to_prefix) |
|
642 |
||
643 |
theorem suffix_Cons: "suffix xs (y # ys) \<longleftrightarrow> xs = y # ys \<or> suffix xs ys" |
|
644 |
unfolding suffix_def by (auto simp: Cons_eq_append_conv) |
|
645 |
||
81332 | 646 |
theorem suffix_append: |
65869 | 647 |
"suffix xs (ys @ zs) \<longleftrightarrow> suffix xs zs \<or> (\<exists>xs'. xs = xs' @ zs \<and> suffix xs' ys)" |
648 |
by (auto simp: suffix_def append_eq_append_conv2) |
|
649 |
||
650 |
theorem suffix_length_le: "suffix xs ys \<Longrightarrow> length xs \<le> length ys" |
|
651 |
by (auto simp add: suffix_def) |
|
652 |
||
653 |
lemma suffix_same_cases: |
|
654 |
"suffix (xs\<^sub>1::'a list) ys \<Longrightarrow> suffix xs\<^sub>2 ys \<Longrightarrow> suffix xs\<^sub>1 xs\<^sub>2 \<or> suffix xs\<^sub>2 xs\<^sub>1" |
|
655 |
unfolding suffix_def by (force simp: append_eq_append_conv2) |
|
656 |
||
657 |
lemma suffix_length_suffix: |
|
658 |
"suffix ps xs \<Longrightarrow> suffix qs xs \<Longrightarrow> length ps \<le> length qs \<Longrightarrow> suffix ps qs" |
|
659 |
by (auto simp: suffix_to_prefix intro: prefix_length_prefix) |
|
660 |
||
661 |
lemma suffix_length_less: "strict_suffix xs ys \<Longrightarrow> length xs < length ys" |
|
662 |
by (auto simp: strict_suffix_def suffix_def) |
|
663 |
||
664 |
lemma suffix_ConsD': "suffix (x#xs) ys \<Longrightarrow> strict_suffix xs ys" |
|
665 |
by (auto simp: strict_suffix_def suffix_def) |
|
666 |
||
667 |
lemma drop_strict_suffix: "strict_suffix xs ys \<Longrightarrow> strict_suffix (drop n xs) ys" |
|
668 |
proof (induct n arbitrary: xs ys) |
|
669 |
case 0 |
|
670 |
then show ?case by (cases ys) simp_all |
|
671 |
next |
|
672 |
case (Suc n) |
|
81332 | 673 |
then show ?case |
65869 | 674 |
by (cases xs) (auto intro: Suc dest: suffix_ConsD' suffix_order.less_imp_le) |
675 |
qed |
|
676 |
||
71789 | 677 |
lemma suffix_map_rightE: |
678 |
assumes "suffix xs (map f ys)" |
|
679 |
shows "\<exists>xs'. suffix xs' ys \<and> xs = map f xs'" |
|
680 |
proof - |
|
681 |
from assms obtain xs' where xs': "map f ys = xs' @ xs" |
|
682 |
by (auto simp: suffix_def) |
|
683 |
define n where "n = length xs'" |
|
684 |
have "xs = drop n (map f ys)" |
|
685 |
by (simp add: xs' n_def) |
|
686 |
thus ?thesis |
|
687 |
by (intro exI[of _ "drop n ys"]) (auto simp: drop_map suffix_drop) |
|
688 |
qed |
|
689 |
||
690 |
lemma suffix_remdups_adj: "suffix xs ys \<Longrightarrow> suffix (remdups_adj xs) (remdups_adj ys)" |
|
691 |
using prefix_remdups_adj[of "rev xs" "rev ys"] |
|
692 |
by (simp add: suffix_to_prefix) |
|
693 |
||
65869 | 694 |
lemma not_suffix_cases: |
695 |
assumes pfx: "\<not> suffix ps ls" |
|
696 |
obtains |
|
697 |
(c1) "ps \<noteq> []" and "ls = []" |
|
698 |
| (c2) a as x xs where "ps = as@[a]" and "ls = xs@[x]" and "x = a" and "\<not> suffix as xs" |
|
699 |
| (c3) a as x xs where "ps = as@[a]" and "ls = xs@[x]" and "x \<noteq> a" |
|
700 |
proof (cases ps rule: rev_cases) |
|
701 |
case Nil |
|
702 |
then show ?thesis using pfx by simp |
|
703 |
next |
|
704 |
case (snoc as a) |
|
705 |
note c = \<open>ps = as@[a]\<close> |
|
706 |
show ?thesis |
|
707 |
proof (cases ls rule: rev_cases) |
|
708 |
case Nil then show ?thesis by (metis append_Nil2 pfx c1 same_suffix_nil) |
|
709 |
next |
|
710 |
case (snoc xs x) |
|
711 |
show ?thesis |
|
712 |
proof (cases "x = a") |
|
713 |
case True |
|
714 |
have "\<not> suffix as xs" using pfx c snoc True by simp |
|
715 |
with c snoc True show ?thesis by (rule c2) |
|
716 |
next |
|
717 |
case False |
|
718 |
with c snoc show ?thesis by (rule c3) |
|
719 |
qed |
|
720 |
qed |
|
721 |
qed |
|
722 |
||
723 |
lemma not_suffix_induct [consumes 1, case_names Nil Neq Eq]: |
|
724 |
assumes np: "\<not> suffix ps ls" |
|
725 |
and base: "\<And>x xs. P (xs@[x]) []" |
|
726 |
and r1: "\<And>x xs y ys. x \<noteq> y \<Longrightarrow> P (xs@[x]) (ys@[y])" |
|
727 |
and r2: "\<And>x xs y ys. \<lbrakk> x = y; \<not> suffix xs ys; P xs ys \<rbrakk> \<Longrightarrow> P (xs@[x]) (ys@[y])" |
|
728 |
shows "P ps ls" using np |
|
729 |
proof (induct ls arbitrary: ps rule: rev_induct) |
|
730 |
case Nil |
|
731 |
then show ?case by (cases ps rule: rev_cases) (auto intro: base) |
|
732 |
next |
|
733 |
case (snoc y ys ps) |
|
734 |
then have npfx: "\<not> suffix ps (ys @ [y])" by simp |
|
735 |
then obtain x xs where pv: "ps = xs @ [x]" |
|
736 |
by (rule not_suffix_cases) auto |
|
737 |
show ?case by (metis snoc.hyps snoc_suffix_snoc npfx pv r1 r2) |
|
738 |
qed |
|
739 |
||
740 |
||
63117 | 741 |
lemma parallelD1: "x \<parallel> y \<Longrightarrow> \<not> prefix x y" |
25692 | 742 |
by blast |
25299 | 743 |
|
63117 | 744 |
lemma parallelD2: "x \<parallel> y \<Longrightarrow> \<not> prefix y x" |
25692 | 745 |
by blast |
25355 | 746 |
|
747 |
lemma parallel_Nil1 [simp]: "\<not> x \<parallel> []" |
|
25692 | 748 |
unfolding parallel_def by simp |
25355 | 749 |
|
25299 | 750 |
lemma parallel_Nil2 [simp]: "\<not> [] \<parallel> x" |
25692 | 751 |
unfolding parallel_def by simp |
25299 | 752 |
|
25564 | 753 |
lemma Cons_parallelI1: "a \<noteq> b \<Longrightarrow> a # as \<parallel> b # bs" |
25692 | 754 |
by auto |
25299 | 755 |
|
25564 | 756 |
lemma Cons_parallelI2: "\<lbrakk> a = b; as \<parallel> bs \<rbrakk> \<Longrightarrow> a # as \<parallel> b # bs" |
63117 | 757 |
by (metis Cons_prefix_Cons parallelE parallelI) |
25665 | 758 |
|
25299 | 759 |
lemma not_equal_is_parallel: |
760 |
assumes neq: "xs \<noteq> ys" |
|
25356 | 761 |
and len: "length xs = length ys" |
762 |
shows "xs \<parallel> ys" |
|
25299 | 763 |
using len neq |
25355 | 764 |
proof (induct rule: list_induct2) |
26445 | 765 |
case Nil |
25356 | 766 |
then show ?case by simp |
25299 | 767 |
next |
26445 | 768 |
case (Cons a as b bs) |
25355 | 769 |
have ih: "as \<noteq> bs \<Longrightarrow> as \<parallel> bs" by fact |
25299 | 770 |
show ?case |
771 |
proof (cases "a = b") |
|
25355 | 772 |
case True |
26445 | 773 |
then have "as \<noteq> bs" using Cons by simp |
25355 | 774 |
then show ?thesis by (rule Cons_parallelI2 [OF True ih]) |
25299 | 775 |
next |
776 |
case False |
|
25355 | 777 |
then show ?thesis by (rule Cons_parallelI1) |
25299 | 778 |
qed |
779 |
qed |
|
22178 | 780 |
|
71789 | 781 |
|
65869 | 782 |
subsection \<open>Suffixes\<close> |
783 |
||
65956
639eb3617a86
reorganised material on sublists
eberlm <eberlm@in.tum.de>
parents:
65954
diff
changeset
|
784 |
primrec suffixes where |
65869 | 785 |
"suffixes [] = [[]]" |
786 |
| "suffixes (x#xs) = suffixes xs @ [x # xs]" |
|
787 |
||
788 |
lemma in_set_suffixes [simp]: "xs \<in> set (suffixes ys) \<longleftrightarrow> suffix xs ys" |
|
789 |
by (induction ys) (auto simp: suffix_def Cons_eq_append_conv) |
|
790 |
||
791 |
lemma distinct_suffixes [intro]: "distinct (suffixes xs)" |
|
792 |
by (induction xs) (auto simp: suffix_def) |
|
793 |
||
794 |
lemma length_suffixes [simp]: "length (suffixes xs) = Suc (length xs)" |
|
795 |
by (induction xs) auto |
|
796 |
||
797 |
lemma suffixes_snoc [simp]: "suffixes (xs @ [x]) = [] # map (\<lambda>ys. ys @ [x]) (suffixes xs)" |
|
798 |
by (induction xs) auto |
|
799 |
||
800 |
lemma suffixes_not_Nil [simp]: "suffixes xs \<noteq> []" |
|
801 |
by (cases xs) auto |
|
802 |
||
803 |
lemma hd_suffixes [simp]: "hd (suffixes xs) = []" |
|
804 |
by (induction xs) simp_all |
|
805 |
||
806 |
lemma last_suffixes [simp]: "last (suffixes xs) = xs" |
|
807 |
by (cases xs) simp_all |
|
808 |
||
81332 | 809 |
lemma suffixes_append: |
65869 | 810 |
"suffixes (xs @ ys) = suffixes ys @ map (\<lambda>xs'. xs' @ ys) (tl (suffixes xs))" |
811 |
proof (induction ys rule: rev_induct) |
|
812 |
case Nil |
|
813 |
thus ?case by (cases xs rule: rev_cases) auto |
|
814 |
next |
|
815 |
case (snoc y ys) |
|
816 |
show ?case |
|
817 |
by (simp only: append.assoc [symmetric] suffixes_snoc snoc.IH) simp |
|
818 |
qed |
|
819 |
||
820 |
lemma suffixes_eq_snoc: |
|
821 |
"suffixes ys = xs @ [x] \<longleftrightarrow> |
|
822 |
(ys = [] \<and> xs = [] \<or> (\<exists>z zs. ys = z#zs \<and> xs = suffixes zs)) \<and> x = ys" |
|
823 |
by (cases ys) auto |
|
824 |
||
81332 | 825 |
lemma suffixes_tailrec [code]: |
65869 | 826 |
"suffixes xs = rev (snd (foldl (\<lambda>(acc1, acc2) x. (x#acc1, (x#acc1)#acc2)) ([],[[]]) (rev xs)))" |
827 |
proof - |
|
828 |
have "foldl (\<lambda>(acc1, acc2) x. (x#acc1, (x#acc1)#acc2)) (ys, ys # zs) (rev xs) = |
|
829 |
(xs @ ys, rev (map (\<lambda>as. as @ ys) (suffixes xs)) @ zs)" for ys zs |
|
830 |
proof (induction xs arbitrary: ys zs) |
|
831 |
case (Cons x xs ys zs) |
|
832 |
from Cons.IH[of ys zs] |
|
833 |
show ?case by (simp add: o_def case_prod_unfold) |
|
834 |
qed simp_all |
|
835 |
from this [of "[]" "[]"] show ?thesis by simp |
|
836 |
qed |
|
81332 | 837 |
|
65869 | 838 |
lemma set_suffixes_eq: "set (suffixes xs) = {ys. suffix ys xs}" |
839 |
by auto |
|
81332 | 840 |
|
65869 | 841 |
lemma card_set_suffixes [simp]: "card (set (suffixes xs)) = Suc (length xs)" |
842 |
by (subst distinct_card) auto |
|
81332 | 843 |
|
844 |
lemma set_suffixes_append: |
|
65869 | 845 |
"set (suffixes (xs @ ys)) = set (suffixes ys) \<union> {xs' @ ys |xs'. xs' \<in> set (suffixes xs)}" |
846 |
by (subst suffixes_append, cases xs rule: rev_cases) auto |
|
847 |
||
848 |
||
849 |
lemma suffixes_conv_prefixes: "suffixes xs = map rev (prefixes (rev xs))" |
|
850 |
by (induction xs) auto |
|
851 |
||
852 |
lemma prefixes_conv_suffixes: "prefixes xs = map rev (suffixes (rev xs))" |
|
853 |
by (induction xs) auto |
|
81332 | 854 |
|
65869 | 855 |
lemma prefixes_rev: "prefixes (rev xs) = map rev (suffixes xs)" |
856 |
by (induction xs) auto |
|
81332 | 857 |
|
65869 | 858 |
lemma suffixes_rev: "suffixes (rev xs) = map rev (prefixes xs)" |
859 |
by (induction xs) auto |
|
860 |
||
49087 | 861 |
|
60500 | 862 |
subsection \<open>Homeomorphic embedding on lists\<close> |
49087 | 863 |
|
57497
4106a2bc066a
renamed "list_hembeq" into slightly shorter "list_emb"
Christian Sternagel
parents:
55579
diff
changeset
|
864 |
inductive list_emb :: "('a \<Rightarrow> 'a \<Rightarrow> bool) \<Rightarrow> 'a list \<Rightarrow> 'a list \<Rightarrow> bool" |
49087 | 865 |
for P :: "('a \<Rightarrow> 'a \<Rightarrow> bool)" |
866 |
where |
|
57497
4106a2bc066a
renamed "list_hembeq" into slightly shorter "list_emb"
Christian Sternagel
parents:
55579
diff
changeset
|
867 |
list_emb_Nil [intro, simp]: "list_emb P [] ys" |
4106a2bc066a
renamed "list_hembeq" into slightly shorter "list_emb"
Christian Sternagel
parents:
55579
diff
changeset
|
868 |
| list_emb_Cons [intro] : "list_emb P xs ys \<Longrightarrow> list_emb P xs (y#ys)" |
57498
ea44ec62a574
no built-in reflexivity of list embedding (which is more standard; now embedding is reflexive whenever the base-order is)
Christian Sternagel
parents:
57497
diff
changeset
|
869 |
| list_emb_Cons2 [intro]: "P x y \<Longrightarrow> list_emb P xs ys \<Longrightarrow> list_emb P (x#xs) (y#ys)" |
50516 | 870 |
|
81332 | 871 |
lemma list_emb_mono: |
57499
7e22776f2d32
added monotonicity lemma for list embedding
Christian Sternagel
parents:
57498
diff
changeset
|
872 |
assumes "\<And>x y. P x y \<longrightarrow> Q x y" |
7e22776f2d32
added monotonicity lemma for list embedding
Christian Sternagel
parents:
57498
diff
changeset
|
873 |
shows "list_emb P xs ys \<longrightarrow> list_emb Q xs ys" |
81332 | 874 |
proof |
875 |
assume "list_emb P xs ys" |
|
57499
7e22776f2d32
added monotonicity lemma for list embedding
Christian Sternagel
parents:
57498
diff
changeset
|
876 |
then show "list_emb Q xs ys" by (induct) (auto simp: assms) |
81332 | 877 |
qed |
57499
7e22776f2d32
added monotonicity lemma for list embedding
Christian Sternagel
parents:
57498
diff
changeset
|
878 |
|
57497
4106a2bc066a
renamed "list_hembeq" into slightly shorter "list_emb"
Christian Sternagel
parents:
55579
diff
changeset
|
879 |
lemma list_emb_Nil2 [simp]: |
4106a2bc066a
renamed "list_hembeq" into slightly shorter "list_emb"
Christian Sternagel
parents:
55579
diff
changeset
|
880 |
assumes "list_emb P xs []" shows "xs = []" |
4106a2bc066a
renamed "list_hembeq" into slightly shorter "list_emb"
Christian Sternagel
parents:
55579
diff
changeset
|
881 |
using assms by (cases rule: list_emb.cases) auto |
49087 | 882 |
|
57498
ea44ec62a574
no built-in reflexivity of list embedding (which is more standard; now embedding is reflexive whenever the base-order is)
Christian Sternagel
parents:
57497
diff
changeset
|
883 |
lemma list_emb_refl: |
ea44ec62a574
no built-in reflexivity of list embedding (which is more standard; now embedding is reflexive whenever the base-order is)
Christian Sternagel
parents:
57497
diff
changeset
|
884 |
assumes "\<And>x. x \<in> set xs \<Longrightarrow> P x x" |
ea44ec62a574
no built-in reflexivity of list embedding (which is more standard; now embedding is reflexive whenever the base-order is)
Christian Sternagel
parents:
57497
diff
changeset
|
885 |
shows "list_emb P xs xs" |
ea44ec62a574
no built-in reflexivity of list embedding (which is more standard; now embedding is reflexive whenever the base-order is)
Christian Sternagel
parents:
57497
diff
changeset
|
886 |
using assms by (induct xs) auto |
49087 | 887 |
|
57497
4106a2bc066a
renamed "list_hembeq" into slightly shorter "list_emb"
Christian Sternagel
parents:
55579
diff
changeset
|
888 |
lemma list_emb_Cons_Nil [simp]: "list_emb P (x#xs) [] = False" |
81332 | 889 |
proof |
890 |
show False if "list_emb P (x#xs) []" |
|
891 |
using list_emb_Nil2 [OF that] by simp |
|
892 |
show "list_emb P (x#xs) []" if False |
|
893 |
using that .. |
|
49087 | 894 |
qed |
895 |
||
57497
4106a2bc066a
renamed "list_hembeq" into slightly shorter "list_emb"
Christian Sternagel
parents:
55579
diff
changeset
|
896 |
lemma list_emb_append2 [intro]: "list_emb P xs ys \<Longrightarrow> list_emb P xs (zs @ ys)" |
49087 | 897 |
by (induct zs) auto |
898 |
||
57497
4106a2bc066a
renamed "list_hembeq" into slightly shorter "list_emb"
Christian Sternagel
parents:
55579
diff
changeset
|
899 |
lemma list_emb_prefix [intro]: |
4106a2bc066a
renamed "list_hembeq" into slightly shorter "list_emb"
Christian Sternagel
parents:
55579
diff
changeset
|
900 |
assumes "list_emb P xs ys" shows "list_emb P xs (ys @ zs)" |
49087 | 901 |
using assms |
902 |
by (induct arbitrary: zs) auto |
|
903 |
||
57497
4106a2bc066a
renamed "list_hembeq" into slightly shorter "list_emb"
Christian Sternagel
parents:
55579
diff
changeset
|
904 |
lemma list_emb_ConsD: |
4106a2bc066a
renamed "list_hembeq" into slightly shorter "list_emb"
Christian Sternagel
parents:
55579
diff
changeset
|
905 |
assumes "list_emb P (x#xs) ys" |
57498
ea44ec62a574
no built-in reflexivity of list embedding (which is more standard; now embedding is reflexive whenever the base-order is)
Christian Sternagel
parents:
57497
diff
changeset
|
906 |
shows "\<exists>us v vs. ys = us @ v # vs \<and> P x v \<and> list_emb P xs vs" |
49087 | 907 |
using assms |
49107 | 908 |
proof (induct x \<equiv> "x # xs" ys arbitrary: x xs) |
57497
4106a2bc066a
renamed "list_hembeq" into slightly shorter "list_emb"
Christian Sternagel
parents:
55579
diff
changeset
|
909 |
case list_emb_Cons |
49107 | 910 |
then show ?case by (metis append_Cons) |
49087 | 911 |
next |
57497
4106a2bc066a
renamed "list_hembeq" into slightly shorter "list_emb"
Christian Sternagel
parents:
55579
diff
changeset
|
912 |
case (list_emb_Cons2 x y xs ys) |
54483 | 913 |
then show ?case by blast |
49087 | 914 |
qed |
915 |
||
57497
4106a2bc066a
renamed "list_hembeq" into slightly shorter "list_emb"
Christian Sternagel
parents:
55579
diff
changeset
|
916 |
lemma list_emb_appendD: |
4106a2bc066a
renamed "list_hembeq" into slightly shorter "list_emb"
Christian Sternagel
parents:
55579
diff
changeset
|
917 |
assumes "list_emb P (xs @ ys) zs" |
4106a2bc066a
renamed "list_hembeq" into slightly shorter "list_emb"
Christian Sternagel
parents:
55579
diff
changeset
|
918 |
shows "\<exists>us vs. zs = us @ vs \<and> list_emb P xs us \<and> list_emb P ys vs" |
49087 | 919 |
using assms |
920 |
proof (induction xs arbitrary: ys zs) |
|
49107 | 921 |
case Nil then show ?case by auto |
49087 | 922 |
next |
923 |
case (Cons x xs) |
|
54483 | 924 |
then obtain us v vs where |
57498
ea44ec62a574
no built-in reflexivity of list embedding (which is more standard; now embedding is reflexive whenever the base-order is)
Christian Sternagel
parents:
57497
diff
changeset
|
925 |
zs: "zs = us @ v # vs" and p: "P x v" and lh: "list_emb P (xs @ ys) vs" |
57497
4106a2bc066a
renamed "list_hembeq" into slightly shorter "list_emb"
Christian Sternagel
parents:
55579
diff
changeset
|
926 |
by (auto dest: list_emb_ConsD) |
54483 | 927 |
obtain sk\<^sub>0 :: "'a list \<Rightarrow> 'a list \<Rightarrow> 'a list" and sk\<^sub>1 :: "'a list \<Rightarrow> 'a list \<Rightarrow> 'a list" where |
57497
4106a2bc066a
renamed "list_hembeq" into slightly shorter "list_emb"
Christian Sternagel
parents:
55579
diff
changeset
|
928 |
sk: "\<forall>x\<^sub>0 x\<^sub>1. \<not> list_emb P (xs @ x\<^sub>0) x\<^sub>1 \<or> sk\<^sub>0 x\<^sub>0 x\<^sub>1 @ sk\<^sub>1 x\<^sub>0 x\<^sub>1 = x\<^sub>1 \<and> list_emb P xs (sk\<^sub>0 x\<^sub>0 x\<^sub>1) \<and> list_emb P x\<^sub>0 (sk\<^sub>1 x\<^sub>0 x\<^sub>1)" |
54483 | 929 |
using Cons(1) by (metis (no_types)) |
57497
4106a2bc066a
renamed "list_hembeq" into slightly shorter "list_emb"
Christian Sternagel
parents:
55579
diff
changeset
|
930 |
hence "\<forall>x\<^sub>2. list_emb P (x # xs) (x\<^sub>2 @ v # sk\<^sub>0 ys vs)" using p lh by auto |
54483 | 931 |
thus ?case using lh zs sk by (metis (no_types) append_Cons append_assoc) |
49087 | 932 |
qed |
933 |
||
63149 | 934 |
lemma list_emb_strict_suffix: |
935 |
assumes "list_emb P xs ys" and "strict_suffix ys zs" |
|
936 |
shows "list_emb P xs zs" |
|
65869 | 937 |
using assms(2) and list_emb_append2 [OF assms(1)] by (auto simp: strict_suffix_def suffix_def) |
63149 | 938 |
|
57497
4106a2bc066a
renamed "list_hembeq" into slightly shorter "list_emb"
Christian Sternagel
parents:
55579
diff
changeset
|
939 |
lemma list_emb_suffix: |
4106a2bc066a
renamed "list_hembeq" into slightly shorter "list_emb"
Christian Sternagel
parents:
55579
diff
changeset
|
940 |
assumes "list_emb P xs ys" and "suffix ys zs" |
4106a2bc066a
renamed "list_hembeq" into slightly shorter "list_emb"
Christian Sternagel
parents:
55579
diff
changeset
|
941 |
shows "list_emb P xs zs" |
63149 | 942 |
using assms and list_emb_strict_suffix |
943 |
unfolding strict_suffix_reflclp_conv[symmetric] by auto |
|
49087 | 944 |
|
57497
4106a2bc066a
renamed "list_hembeq" into slightly shorter "list_emb"
Christian Sternagel
parents:
55579
diff
changeset
|
945 |
lemma list_emb_length: "list_emb P xs ys \<Longrightarrow> length xs \<le> length ys" |
4106a2bc066a
renamed "list_hembeq" into slightly shorter "list_emb"
Christian Sternagel
parents:
55579
diff
changeset
|
946 |
by (induct rule: list_emb.induct) auto |
49087 | 947 |
|
57497
4106a2bc066a
renamed "list_hembeq" into slightly shorter "list_emb"
Christian Sternagel
parents:
55579
diff
changeset
|
948 |
lemma list_emb_trans: |
57500
5a8b3e9d82a4
weaker assumption for "list_emb_trans"; added lemma
Christian Sternagel
parents:
57499
diff
changeset
|
949 |
assumes "\<And>x y z. \<lbrakk>x \<in> set xs; y \<in> set ys; z \<in> set zs; P x y; P y z\<rbrakk> \<Longrightarrow> P x z" |
5a8b3e9d82a4
weaker assumption for "list_emb_trans"; added lemma
Christian Sternagel
parents:
57499
diff
changeset
|
950 |
shows "\<lbrakk>list_emb P xs ys; list_emb P ys zs\<rbrakk> \<Longrightarrow> list_emb P xs zs" |
50516 | 951 |
proof - |
57497
4106a2bc066a
renamed "list_hembeq" into slightly shorter "list_emb"
Christian Sternagel
parents:
55579
diff
changeset
|
952 |
assume "list_emb P xs ys" and "list_emb P ys zs" |
57500
5a8b3e9d82a4
weaker assumption for "list_emb_trans"; added lemma
Christian Sternagel
parents:
57499
diff
changeset
|
953 |
then show "list_emb P xs zs" using assms |
49087 | 954 |
proof (induction arbitrary: zs) |
57497
4106a2bc066a
renamed "list_hembeq" into slightly shorter "list_emb"
Christian Sternagel
parents:
55579
diff
changeset
|
955 |
case list_emb_Nil show ?case by blast |
49087 | 956 |
next |
57497
4106a2bc066a
renamed "list_hembeq" into slightly shorter "list_emb"
Christian Sternagel
parents:
55579
diff
changeset
|
957 |
case (list_emb_Cons xs ys y) |
60500 | 958 |
from list_emb_ConsD [OF \<open>list_emb P (y#ys) zs\<close>] obtain us v vs |
57500
5a8b3e9d82a4
weaker assumption for "list_emb_trans"; added lemma
Christian Sternagel
parents:
57499
diff
changeset
|
959 |
where zs: "zs = us @ v # vs" and "P\<^sup>=\<^sup>= y v" and "list_emb P ys vs" by blast |
57497
4106a2bc066a
renamed "list_hembeq" into slightly shorter "list_emb"
Christian Sternagel
parents:
55579
diff
changeset
|
960 |
then have "list_emb P ys (v#vs)" by blast |
4106a2bc066a
renamed "list_hembeq" into slightly shorter "list_emb"
Christian Sternagel
parents:
55579
diff
changeset
|
961 |
then have "list_emb P ys zs" unfolding zs by (rule list_emb_append2) |
57500
5a8b3e9d82a4
weaker assumption for "list_emb_trans"; added lemma
Christian Sternagel
parents:
57499
diff
changeset
|
962 |
from list_emb_Cons.IH [OF this] and list_emb_Cons.prems show ?case by auto |
49087 | 963 |
next |
57497
4106a2bc066a
renamed "list_hembeq" into slightly shorter "list_emb"
Christian Sternagel
parents:
55579
diff
changeset
|
964 |
case (list_emb_Cons2 x y xs ys) |
60500 | 965 |
from list_emb_ConsD [OF \<open>list_emb P (y#ys) zs\<close>] obtain us v vs |
57498
ea44ec62a574
no built-in reflexivity of list embedding (which is more standard; now embedding is reflexive whenever the base-order is)
Christian Sternagel
parents:
57497
diff
changeset
|
966 |
where zs: "zs = us @ v # vs" and "P y v" and "list_emb P ys vs" by blast |
57500
5a8b3e9d82a4
weaker assumption for "list_emb_trans"; added lemma
Christian Sternagel
parents:
57499
diff
changeset
|
967 |
with list_emb_Cons2 have "list_emb P xs vs" by auto |
57498
ea44ec62a574
no built-in reflexivity of list embedding (which is more standard; now embedding is reflexive whenever the base-order is)
Christian Sternagel
parents:
57497
diff
changeset
|
968 |
moreover have "P x v" |
49087 | 969 |
proof - |
57500
5a8b3e9d82a4
weaker assumption for "list_emb_trans"; added lemma
Christian Sternagel
parents:
57499
diff
changeset
|
970 |
from zs have "v \<in> set zs" by auto |
5a8b3e9d82a4
weaker assumption for "list_emb_trans"; added lemma
Christian Sternagel
parents:
57499
diff
changeset
|
971 |
moreover have "x \<in> set (x#xs)" and "y \<in> set (y#ys)" by simp_all |
50516 | 972 |
ultimately show ?thesis |
60500 | 973 |
using \<open>P x y\<close> and \<open>P y v\<close> and list_emb_Cons2 |
50516 | 974 |
by blast |
49087 | 975 |
qed |
57497
4106a2bc066a
renamed "list_hembeq" into slightly shorter "list_emb"
Christian Sternagel
parents:
55579
diff
changeset
|
976 |
ultimately have "list_emb P (x#xs) (v#vs)" by blast |
4106a2bc066a
renamed "list_hembeq" into slightly shorter "list_emb"
Christian Sternagel
parents:
55579
diff
changeset
|
977 |
then show ?case unfolding zs by (rule list_emb_append2) |
49087 | 978 |
qed |
979 |
qed |
|
980 |
||
57500
5a8b3e9d82a4
weaker assumption for "list_emb_trans"; added lemma
Christian Sternagel
parents:
57499
diff
changeset
|
981 |
lemma list_emb_set: |
5a8b3e9d82a4
weaker assumption for "list_emb_trans"; added lemma
Christian Sternagel
parents:
57499
diff
changeset
|
982 |
assumes "list_emb P xs ys" and "x \<in> set xs" |
5a8b3e9d82a4
weaker assumption for "list_emb_trans"; added lemma
Christian Sternagel
parents:
57499
diff
changeset
|
983 |
obtains y where "y \<in> set ys" and "P x y" |
5a8b3e9d82a4
weaker assumption for "list_emb_trans"; added lemma
Christian Sternagel
parents:
57499
diff
changeset
|
984 |
using assms by (induct) auto |
5a8b3e9d82a4
weaker assumption for "list_emb_trans"; added lemma
Christian Sternagel
parents:
57499
diff
changeset
|
985 |
|
65869 | 986 |
lemma list_emb_Cons_iff1 [simp]: |
987 |
assumes "P x y" |
|
988 |
shows "list_emb P (x#xs) (y#ys) \<longleftrightarrow> list_emb P xs ys" |
|
989 |
using assms by (subst list_emb.simps) (auto dest: list_emb_ConsD) |
|
990 |
||
991 |
lemma list_emb_Cons_iff2 [simp]: |
|
992 |
assumes "\<not>P x y" |
|
993 |
shows "list_emb P (x#xs) (y#ys) \<longleftrightarrow> list_emb P (x#xs) ys" |
|
994 |
using assms by (subst list_emb.simps) auto |
|
995 |
||
996 |
lemma list_emb_code [code]: |
|
997 |
"list_emb P [] ys \<longleftrightarrow> True" |
|
998 |
"list_emb P (x#xs) [] \<longleftrightarrow> False" |
|
999 |
"list_emb P (x#xs) (y#ys) \<longleftrightarrow> (if P x y then list_emb P xs ys else list_emb P (x#xs) ys)" |
|
1000 |
by simp_all |
|
81332 | 1001 |
|
65869 | 1002 |
|
65956
639eb3617a86
reorganised material on sublists
eberlm <eberlm@in.tum.de>
parents:
65954
diff
changeset
|
1003 |
subsection \<open>Subsequences (special case of homeomorphic embedding)\<close> |
49087 | 1004 |
|
65956
639eb3617a86
reorganised material on sublists
eberlm <eberlm@in.tum.de>
parents:
65954
diff
changeset
|
1005 |
abbreviation subseq :: "'a list \<Rightarrow> 'a list \<Rightarrow> bool" |
67399 | 1006 |
where "subseq xs ys \<equiv> list_emb (=) xs ys" |
81332 | 1007 |
|
65956
639eb3617a86
reorganised material on sublists
eberlm <eberlm@in.tum.de>
parents:
65954
diff
changeset
|
1008 |
definition strict_subseq where "strict_subseq xs ys \<longleftrightarrow> xs \<noteq> ys \<and> subseq xs ys" |
49087 | 1009 |
|
65956
639eb3617a86
reorganised material on sublists
eberlm <eberlm@in.tum.de>
parents:
65954
diff
changeset
|
1010 |
lemma subseq_Cons2: "subseq xs ys \<Longrightarrow> subseq (x#xs) (x#ys)" by auto |
49087 | 1011 |
|
65956
639eb3617a86
reorganised material on sublists
eberlm <eberlm@in.tum.de>
parents:
65954
diff
changeset
|
1012 |
lemma subseq_same_length: |
639eb3617a86
reorganised material on sublists
eberlm <eberlm@in.tum.de>
parents:
65954
diff
changeset
|
1013 |
assumes "subseq xs ys" and "length xs = length ys" shows "xs = ys" |
57497
4106a2bc066a
renamed "list_hembeq" into slightly shorter "list_emb"
Christian Sternagel
parents:
55579
diff
changeset
|
1014 |
using assms by (induct) (auto dest: list_emb_length) |
49087 | 1015 |
|
65956
639eb3617a86
reorganised material on sublists
eberlm <eberlm@in.tum.de>
parents:
65954
diff
changeset
|
1016 |
lemma not_subseq_length [simp]: "length ys < length xs \<Longrightarrow> \<not> subseq xs ys" |
57497
4106a2bc066a
renamed "list_hembeq" into slightly shorter "list_emb"
Christian Sternagel
parents:
55579
diff
changeset
|
1017 |
by (metis list_emb_length linorder_not_less) |
49087 | 1018 |
|
65956
639eb3617a86
reorganised material on sublists
eberlm <eberlm@in.tum.de>
parents:
65954
diff
changeset
|
1019 |
lemma subseq_Cons': "subseq (x#xs) ys \<Longrightarrow> subseq xs ys" |
57497
4106a2bc066a
renamed "list_hembeq" into slightly shorter "list_emb"
Christian Sternagel
parents:
55579
diff
changeset
|
1020 |
by (induct xs, simp, blast dest: list_emb_ConsD) |
49087 | 1021 |
|
65956
639eb3617a86
reorganised material on sublists
eberlm <eberlm@in.tum.de>
parents:
65954
diff
changeset
|
1022 |
lemma subseq_Cons2': |
639eb3617a86
reorganised material on sublists
eberlm <eberlm@in.tum.de>
parents:
65954
diff
changeset
|
1023 |
assumes "subseq (x#xs) (x#ys)" shows "subseq xs ys" |
639eb3617a86
reorganised material on sublists
eberlm <eberlm@in.tum.de>
parents:
65954
diff
changeset
|
1024 |
using assms by (cases) (rule subseq_Cons') |
49087 | 1025 |
|
65956
639eb3617a86
reorganised material on sublists
eberlm <eberlm@in.tum.de>
parents:
65954
diff
changeset
|
1026 |
lemma subseq_Cons2_neq: |
639eb3617a86
reorganised material on sublists
eberlm <eberlm@in.tum.de>
parents:
65954
diff
changeset
|
1027 |
assumes "subseq (x#xs) (y#ys)" |
639eb3617a86
reorganised material on sublists
eberlm <eberlm@in.tum.de>
parents:
65954
diff
changeset
|
1028 |
shows "x \<noteq> y \<Longrightarrow> subseq (x#xs) ys" |
49087 | 1029 |
using assms by (cases) auto |
1030 |
||
65956
639eb3617a86
reorganised material on sublists
eberlm <eberlm@in.tum.de>
parents:
65954
diff
changeset
|
1031 |
lemma subseq_Cons2_iff [simp]: |
639eb3617a86
reorganised material on sublists
eberlm <eberlm@in.tum.de>
parents:
65954
diff
changeset
|
1032 |
"subseq (x#xs) (y#ys) = (if x = y then subseq xs ys else subseq (x#xs) ys)" |
65869 | 1033 |
by simp |
49087 | 1034 |
|
65956
639eb3617a86
reorganised material on sublists
eberlm <eberlm@in.tum.de>
parents:
65954
diff
changeset
|
1035 |
lemma subseq_append': "subseq (zs @ xs) (zs @ ys) \<longleftrightarrow> subseq xs ys" |
49087 | 1036 |
by (induct zs) simp_all |
73411 | 1037 |
|
1038 |
global_interpretation subseq_order: ordering subseq strict_subseq |
|
65869 | 1039 |
proof |
73411 | 1040 |
show \<open>subseq xs xs\<close> for xs :: \<open>'a list\<close> |
1041 |
using refl by (rule list_emb_refl) |
|
1042 |
show \<open>subseq xs zs\<close> if \<open>subseq xs ys\<close> and \<open>subseq ys zs\<close> |
|
1043 |
for xs ys zs :: \<open>'a list\<close> |
|
1044 |
using trans [OF refl] that by (rule list_emb_trans) simp |
|
1045 |
show \<open>xs = ys\<close> if \<open>subseq xs ys\<close> and \<open>subseq ys xs\<close> |
|
1046 |
for xs ys :: \<open>'a list\<close> |
|
1047 |
using that proof induction |
|
1048 |
case list_emb_Nil |
|
1049 |
from list_emb_Nil2 [OF this] show ?case by simp |
|
1050 |
next |
|
1051 |
case list_emb_Cons2 |
|
1052 |
then show ?case by simp |
|
1053 |
next |
|
1054 |
case list_emb_Cons |
|
1055 |
hence False using subseq_Cons' by fastforce |
|
1056 |
then show ?case .. |
|
1057 |
qed |
|
1058 |
show \<open>strict_subseq xs ys \<longleftrightarrow> subseq xs ys \<and> xs \<noteq> ys\<close> |
|
1059 |
for xs ys :: \<open>'a list\<close> |
|
65956
639eb3617a86
reorganised material on sublists
eberlm <eberlm@in.tum.de>
parents:
65954
diff
changeset
|
1060 |
by (auto simp: strict_subseq_def) |
73411 | 1061 |
qed |
1062 |
||
1063 |
interpretation subseq_order: order subseq strict_subseq |
|
1064 |
by (rule ordering_orderI) standard |
|
49087 | 1065 |
|
65956
639eb3617a86
reorganised material on sublists
eberlm <eberlm@in.tum.de>
parents:
65954
diff
changeset
|
1066 |
lemma in_set_subseqs [simp]: "xs \<in> set (subseqs ys) \<longleftrightarrow> subseq xs ys" |
65869 | 1067 |
proof |
65956
639eb3617a86
reorganised material on sublists
eberlm <eberlm@in.tum.de>
parents:
65954
diff
changeset
|
1068 |
assume "xs \<in> set (subseqs ys)" |
639eb3617a86
reorganised material on sublists
eberlm <eberlm@in.tum.de>
parents:
65954
diff
changeset
|
1069 |
thus "subseq xs ys" |
65869 | 1070 |
by (induction ys arbitrary: xs) (auto simp: Let_def) |
49087 | 1071 |
next |
81332 | 1072 |
have [simp]: "[] \<in> set (subseqs ys)" for ys :: "'a list" |
65869 | 1073 |
by (induction ys) (auto simp: Let_def) |
65956
639eb3617a86
reorganised material on sublists
eberlm <eberlm@in.tum.de>
parents:
65954
diff
changeset
|
1074 |
assume "subseq xs ys" |
639eb3617a86
reorganised material on sublists
eberlm <eberlm@in.tum.de>
parents:
65954
diff
changeset
|
1075 |
thus "xs \<in> set (subseqs ys)" |
65869 | 1076 |
by (induction xs ys rule: list_emb.induct) (auto simp: Let_def) |
49087 | 1077 |
qed |
1078 |
||
65956
639eb3617a86
reorganised material on sublists
eberlm <eberlm@in.tum.de>
parents:
65954
diff
changeset
|
1079 |
lemma set_subseqs_eq: "set (subseqs ys) = {xs. subseq xs ys}" |
65869 | 1080 |
by auto |
49087 | 1081 |
|
65956
639eb3617a86
reorganised material on sublists
eberlm <eberlm@in.tum.de>
parents:
65954
diff
changeset
|
1082 |
lemma subseq_append_le_same_iff: "subseq (xs @ ys) ys \<longleftrightarrow> xs = []" |
57497
4106a2bc066a
renamed "list_hembeq" into slightly shorter "list_emb"
Christian Sternagel
parents:
55579
diff
changeset
|
1083 |
by (auto dest: list_emb_length) |
49087 | 1084 |
|
65956
639eb3617a86
reorganised material on sublists
eberlm <eberlm@in.tum.de>
parents:
65954
diff
changeset
|
1085 |
lemma subseq_singleton_left: "subseq [x] ys \<longleftrightarrow> x \<in> set ys" |
64886 | 1086 |
by (fastforce dest: list_emb_ConsD split_list_last) |
1087 |
||
57497
4106a2bc066a
renamed "list_hembeq" into slightly shorter "list_emb"
Christian Sternagel
parents:
55579
diff
changeset
|
1088 |
lemma list_emb_append_mono: |
4106a2bc066a
renamed "list_hembeq" into slightly shorter "list_emb"
Christian Sternagel
parents:
55579
diff
changeset
|
1089 |
"\<lbrakk> list_emb P xs xs'; list_emb P ys ys' \<rbrakk> \<Longrightarrow> list_emb P (xs@ys) (xs'@ys')" |
65957 | 1090 |
by (induct rule: list_emb.induct) auto |
1091 |
||
1092 |
lemma prefix_imp_subseq [intro]: "prefix xs ys \<Longrightarrow> subseq xs ys" |
|
1093 |
by (auto simp: prefix_def) |
|
1094 |
||
1095 |
lemma suffix_imp_subseq [intro]: "suffix xs ys \<Longrightarrow> subseq xs ys" |
|
1096 |
by (auto simp: suffix_def) |
|
49087 | 1097 |
|
82310
41f5266e5595
New theorems, mostly from the number theory project
paulson <lp15@cam.ac.uk>
parents:
82218
diff
changeset
|
1098 |
text \<open>a subsequence of a sorted list\<close> |
41f5266e5595
New theorems, mostly from the number theory project
paulson <lp15@cam.ac.uk>
parents:
82218
diff
changeset
|
1099 |
lemma sorted_subset_imp_subseq: |
41f5266e5595
New theorems, mostly from the number theory project
paulson <lp15@cam.ac.uk>
parents:
82218
diff
changeset
|
1100 |
fixes xs :: "'a::order list" |
41f5266e5595
New theorems, mostly from the number theory project
paulson <lp15@cam.ac.uk>
parents:
82218
diff
changeset
|
1101 |
assumes "set xs \<subseteq> set ys" "sorted_wrt (<) xs" "sorted_wrt (\<le>) ys" |
41f5266e5595
New theorems, mostly from the number theory project
paulson <lp15@cam.ac.uk>
parents:
82218
diff
changeset
|
1102 |
shows "subseq xs ys" |
41f5266e5595
New theorems, mostly from the number theory project
paulson <lp15@cam.ac.uk>
parents:
82218
diff
changeset
|
1103 |
using assms |
41f5266e5595
New theorems, mostly from the number theory project
paulson <lp15@cam.ac.uk>
parents:
82218
diff
changeset
|
1104 |
proof (induction xs arbitrary: ys) |
41f5266e5595
New theorems, mostly from the number theory project
paulson <lp15@cam.ac.uk>
parents:
82218
diff
changeset
|
1105 |
case Nil |
41f5266e5595
New theorems, mostly from the number theory project
paulson <lp15@cam.ac.uk>
parents:
82218
diff
changeset
|
1106 |
then show ?case |
41f5266e5595
New theorems, mostly from the number theory project
paulson <lp15@cam.ac.uk>
parents:
82218
diff
changeset
|
1107 |
by auto |
41f5266e5595
New theorems, mostly from the number theory project
paulson <lp15@cam.ac.uk>
parents:
82218
diff
changeset
|
1108 |
next |
41f5266e5595
New theorems, mostly from the number theory project
paulson <lp15@cam.ac.uk>
parents:
82218
diff
changeset
|
1109 |
case (Cons x xs) |
41f5266e5595
New theorems, mostly from the number theory project
paulson <lp15@cam.ac.uk>
parents:
82218
diff
changeset
|
1110 |
then have "x \<in> set ys" |
41f5266e5595
New theorems, mostly from the number theory project
paulson <lp15@cam.ac.uk>
parents:
82218
diff
changeset
|
1111 |
by auto |
41f5266e5595
New theorems, mostly from the number theory project
paulson <lp15@cam.ac.uk>
parents:
82218
diff
changeset
|
1112 |
then obtain us vs where \<section>: "ys = us @ [x] @ vs" |
41f5266e5595
New theorems, mostly from the number theory project
paulson <lp15@cam.ac.uk>
parents:
82218
diff
changeset
|
1113 |
by (metis append.left_neutral append_eq_Cons_conv split_list) |
41f5266e5595
New theorems, mostly from the number theory project
paulson <lp15@cam.ac.uk>
parents:
82218
diff
changeset
|
1114 |
moreover |
41f5266e5595
New theorems, mostly from the number theory project
paulson <lp15@cam.ac.uk>
parents:
82218
diff
changeset
|
1115 |
have "set xs \<subseteq> set vs" |
41f5266e5595
New theorems, mostly from the number theory project
paulson <lp15@cam.ac.uk>
parents:
82218
diff
changeset
|
1116 |
using Cons.prems by (fastforce simp: \<section> sorted_wrt_append) |
41f5266e5595
New theorems, mostly from the number theory project
paulson <lp15@cam.ac.uk>
parents:
82218
diff
changeset
|
1117 |
with Cons have "subseq xs vs" |
41f5266e5595
New theorems, mostly from the number theory project
paulson <lp15@cam.ac.uk>
parents:
82218
diff
changeset
|
1118 |
by (metis \<section> sorted_wrt.simps(2) sorted_wrt_append) |
41f5266e5595
New theorems, mostly from the number theory project
paulson <lp15@cam.ac.uk>
parents:
82218
diff
changeset
|
1119 |
ultimately show ?case |
41f5266e5595
New theorems, mostly from the number theory project
paulson <lp15@cam.ac.uk>
parents:
82218
diff
changeset
|
1120 |
by auto |
41f5266e5595
New theorems, mostly from the number theory project
paulson <lp15@cam.ac.uk>
parents:
82218
diff
changeset
|
1121 |
qed |
49087 | 1122 |
|
60500 | 1123 |
subsection \<open>Appending elements\<close> |
49087 | 1124 |
|
65956
639eb3617a86
reorganised material on sublists
eberlm <eberlm@in.tum.de>
parents:
65954
diff
changeset
|
1125 |
lemma subseq_append [simp]: |
639eb3617a86
reorganised material on sublists
eberlm <eberlm@in.tum.de>
parents:
65954
diff
changeset
|
1126 |
"subseq (xs @ zs) (ys @ zs) \<longleftrightarrow> subseq xs ys" (is "?l = ?r") |
49087 | 1127 |
proof |
81332 | 1128 |
have "xs' = xs @ zs \<and> ys' = ys @ zs \<longrightarrow> subseq xs ys" |
1129 |
if "subseq xs' ys'" for xs' ys' xs ys zs :: "'a list" |
|
1130 |
using that |
|
1131 |
proof (induct arbitrary: xs ys zs) |
|
1132 |
case list_emb_Nil |
|
1133 |
show ?case by simp |
|
1134 |
next |
|
1135 |
case (list_emb_Cons xs' ys' x) |
|
1136 |
have ?case if "ys = []" |
|
1137 |
using list_emb_Cons(1) that by auto |
|
1138 |
moreover |
|
1139 |
have ?case if "ys = x#us" for us |
|
1140 |
using list_emb_Cons(2) that by (simp add: list_emb.list_emb_Cons) |
|
1141 |
ultimately show ?case |
|
1142 |
by (auto simp: Cons_eq_append_conv) |
|
1143 |
next |
|
1144 |
case (list_emb_Cons2 x y xs' ys') |
|
1145 |
have ?case if "xs = []" |
|
1146 |
using list_emb_Cons2(1) that by auto |
|
1147 |
moreover |
|
1148 |
have ?case if "xs = x#us" "ys = x#vs" for us vs |
|
1149 |
using list_emb_Cons2 that by auto |
|
1150 |
moreover |
|
1151 |
have ?case if "xs = x#us" "ys = []" for us |
|
1152 |
using list_emb_Cons2(2) that by bestsimp |
|
1153 |
ultimately show ?case |
|
1154 |
using \<open>x = y\<close> by (auto simp: Cons_eq_append_conv) |
|
1155 |
qed |
|
1156 |
then show "?l \<Longrightarrow> ?r" by blast |
|
1157 |
show "?r \<Longrightarrow> ?l" by (metis list_emb_append_mono subseq_order.order_refl) |
|
49087 | 1158 |
qed |
1159 |
||
81332 | 1160 |
lemma subseq_append_iff: |
65956
639eb3617a86
reorganised material on sublists
eberlm <eberlm@in.tum.de>
parents:
65954
diff
changeset
|
1161 |
"subseq xs (ys @ zs) \<longleftrightarrow> (\<exists>xs1 xs2. xs = xs1 @ xs2 \<and> subseq xs1 ys \<and> subseq xs2 zs)" |
65869 | 1162 |
(is "?lhs = ?rhs") |
1163 |
proof |
|
1164 |
assume ?lhs thus ?rhs |
|
1165 |
proof (induction xs "ys @ zs" arbitrary: ys zs rule: list_emb.induct) |
|
1166 |
case (list_emb_Cons xs ws y ys zs) |
|
1167 |
from list_emb_Cons(2)[of "tl ys" zs] and list_emb_Cons(2)[of "[]" "tl zs"] and list_emb_Cons(1,3) |
|
81332 | 1168 |
show ?case by (cases ys) auto |
65869 | 1169 |
next |
1170 |
case (list_emb_Cons2 x y xs ws ys zs) |
|
1171 |
from list_emb_Cons2(3)[of "tl ys" zs] and list_emb_Cons2(3)[of "[]" "tl zs"] |
|
1172 |
and list_emb_Cons2(1,2,4) |
|
1173 |
show ?case by (cases ys) (auto simp: Cons_eq_append_conv) |
|
1174 |
qed auto |
|
1175 |
qed (auto intro: list_emb_append_mono) |
|
1176 |
||
81332 | 1177 |
lemma subseq_appendE [case_names append]: |
65956
639eb3617a86
reorganised material on sublists
eberlm <eberlm@in.tum.de>
parents:
65954
diff
changeset
|
1178 |
assumes "subseq xs (ys @ zs)" |
639eb3617a86
reorganised material on sublists
eberlm <eberlm@in.tum.de>
parents:
65954
diff
changeset
|
1179 |
obtains xs1 xs2 where "xs = xs1 @ xs2" "subseq xs1 ys" "subseq xs2 zs" |
639eb3617a86
reorganised material on sublists
eberlm <eberlm@in.tum.de>
parents:
65954
diff
changeset
|
1180 |
using assms by (subst (asm) subseq_append_iff) auto |
65869 | 1181 |
|
65956
639eb3617a86
reorganised material on sublists
eberlm <eberlm@in.tum.de>
parents:
65954
diff
changeset
|
1182 |
lemma subseq_drop_many: "subseq xs ys \<Longrightarrow> subseq xs (zs @ ys)" |
49087 | 1183 |
by (induct zs) auto |
1184 |
||
65956
639eb3617a86
reorganised material on sublists
eberlm <eberlm@in.tum.de>
parents:
65954
diff
changeset
|
1185 |
lemma subseq_rev_drop_many: "subseq xs ys \<Longrightarrow> subseq xs (ys @ zs)" |
57497
4106a2bc066a
renamed "list_hembeq" into slightly shorter "list_emb"
Christian Sternagel
parents:
55579
diff
changeset
|
1186 |
by (metis append_Nil2 list_emb_Nil list_emb_append_mono) |
49087 | 1187 |
|
1188 |
||
60500 | 1189 |
subsection \<open>Relation to standard list operations\<close> |
49087 | 1190 |
|
65956
639eb3617a86
reorganised material on sublists
eberlm <eberlm@in.tum.de>
parents:
65954
diff
changeset
|
1191 |
lemma subseq_map: |
639eb3617a86
reorganised material on sublists
eberlm <eberlm@in.tum.de>
parents:
65954
diff
changeset
|
1192 |
assumes "subseq xs ys" shows "subseq (map f xs) (map f ys)" |
49087 | 1193 |
using assms by (induct) auto |
1194 |
||
65956
639eb3617a86
reorganised material on sublists
eberlm <eberlm@in.tum.de>
parents:
65954
diff
changeset
|
1195 |
lemma subseq_filter_left [simp]: "subseq (filter P xs) xs" |
49087 | 1196 |
by (induct xs) auto |
1197 |
||
65956
639eb3617a86
reorganised material on sublists
eberlm <eberlm@in.tum.de>
parents:
65954
diff
changeset
|
1198 |
lemma subseq_filter [simp]: |
639eb3617a86
reorganised material on sublists
eberlm <eberlm@in.tum.de>
parents:
65954
diff
changeset
|
1199 |
assumes "subseq xs ys" shows "subseq (filter P xs) (filter P ys)" |
54483 | 1200 |
using assms by induct auto |
49087 | 1201 |
|
81332 | 1202 |
lemma subseq_conv_nths: "subseq xs ys \<longleftrightarrow> (\<exists>N. xs = nths ys N)" |
1203 |
(is "?L = ?R") |
|
49087 | 1204 |
proof |
81332 | 1205 |
show ?R if ?L using that |
49087 | 1206 |
proof (induct) |
81332 | 1207 |
case list_emb_Nil |
1208 |
show ?case by (metis nths_empty) |
|
49087 | 1209 |
next |
57497
4106a2bc066a
renamed "list_hembeq" into slightly shorter "list_emb"
Christian Sternagel
parents:
55579
diff
changeset
|
1210 |
case (list_emb_Cons xs ys x) |
65956
639eb3617a86
reorganised material on sublists
eberlm <eberlm@in.tum.de>
parents:
65954
diff
changeset
|
1211 |
then obtain N where "xs = nths ys N" by blast |
639eb3617a86
reorganised material on sublists
eberlm <eberlm@in.tum.de>
parents:
65954
diff
changeset
|
1212 |
then have "xs = nths (x#ys) (Suc ` N)" |
639eb3617a86
reorganised material on sublists
eberlm <eberlm@in.tum.de>
parents:
65954
diff
changeset
|
1213 |
by (clarsimp simp add: nths_Cons inj_image_mem_iff) |
49107 | 1214 |
then show ?case by blast |
49087 | 1215 |
next |
57497
4106a2bc066a
renamed "list_hembeq" into slightly shorter "list_emb"
Christian Sternagel
parents:
55579
diff
changeset
|
1216 |
case (list_emb_Cons2 x y xs ys) |
65956
639eb3617a86
reorganised material on sublists
eberlm <eberlm@in.tum.de>
parents:
65954
diff
changeset
|
1217 |
then obtain N where "xs = nths ys N" by blast |
639eb3617a86
reorganised material on sublists
eberlm <eberlm@in.tum.de>
parents:
65954
diff
changeset
|
1218 |
then have "x#xs = nths (x#ys) (insert 0 (Suc ` N))" |
639eb3617a86
reorganised material on sublists
eberlm <eberlm@in.tum.de>
parents:
65954
diff
changeset
|
1219 |
by (clarsimp simp add: nths_Cons inj_image_mem_iff) |
57497
4106a2bc066a
renamed "list_hembeq" into slightly shorter "list_emb"
Christian Sternagel
parents:
55579
diff
changeset
|
1220 |
moreover from list_emb_Cons2 have "x = y" by simp |
50516 | 1221 |
ultimately show ?case by blast |
49087 | 1222 |
qed |
81332 | 1223 |
show ?L if ?R |
1224 |
proof - |
|
1225 |
from that obtain N where "xs = nths ys N" .. |
|
1226 |
moreover have "subseq (nths ys N) ys" |
|
1227 |
proof (induct ys arbitrary: N) |
|
1228 |
case Nil |
|
1229 |
show ?case by simp |
|
1230 |
next |
|
1231 |
case Cons |
|
1232 |
then show ?case by (auto simp: nths_Cons) |
|
1233 |
qed |
|
1234 |
ultimately show ?thesis by simp |
|
49087 | 1235 |
qed |
1236 |
qed |
|
81332 | 1237 |
|
1238 |
||
65956
639eb3617a86
reorganised material on sublists
eberlm <eberlm@in.tum.de>
parents:
65954
diff
changeset
|
1239 |
subsection \<open>Contiguous sublists\<close> |
639eb3617a86
reorganised material on sublists
eberlm <eberlm@in.tum.de>
parents:
65954
diff
changeset
|
1240 |
|
71789 | 1241 |
subsubsection \<open>\<open>sublist\<close>\<close> |
1242 |
||
81332 | 1243 |
definition sublist :: "'a list \<Rightarrow> 'a list \<Rightarrow> bool" where |
65956
639eb3617a86
reorganised material on sublists
eberlm <eberlm@in.tum.de>
parents:
65954
diff
changeset
|
1244 |
"sublist xs ys = (\<exists>ps ss. ys = ps @ xs @ ss)" |
81332 | 1245 |
|
1246 |
definition strict_sublist :: "'a list \<Rightarrow> 'a list \<Rightarrow> bool" where |
|
65956
639eb3617a86
reorganised material on sublists
eberlm <eberlm@in.tum.de>
parents:
65954
diff
changeset
|
1247 |
"strict_sublist xs ys \<longleftrightarrow> sublist xs ys \<and> xs \<noteq> ys" |
639eb3617a86
reorganised material on sublists
eberlm <eberlm@in.tum.de>
parents:
65954
diff
changeset
|
1248 |
|
639eb3617a86
reorganised material on sublists
eberlm <eberlm@in.tum.de>
parents:
65954
diff
changeset
|
1249 |
interpretation sublist_order: order sublist strict_sublist |
639eb3617a86
reorganised material on sublists
eberlm <eberlm@in.tum.de>
parents:
65954
diff
changeset
|
1250 |
proof |
639eb3617a86
reorganised material on sublists
eberlm <eberlm@in.tum.de>
parents:
65954
diff
changeset
|
1251 |
fix xs ys zs :: "'a list" |
639eb3617a86
reorganised material on sublists
eberlm <eberlm@in.tum.de>
parents:
65954
diff
changeset
|
1252 |
assume "sublist xs ys" "sublist ys zs" |
639eb3617a86
reorganised material on sublists
eberlm <eberlm@in.tum.de>
parents:
65954
diff
changeset
|
1253 |
then obtain xs1 xs2 ys1 ys2 where "ys = xs1 @ xs @ xs2" "zs = ys1 @ ys @ ys2" |
639eb3617a86
reorganised material on sublists
eberlm <eberlm@in.tum.de>
parents:
65954
diff
changeset
|
1254 |
by (auto simp: sublist_def) |
639eb3617a86
reorganised material on sublists
eberlm <eberlm@in.tum.de>
parents:
65954
diff
changeset
|
1255 |
hence "zs = (ys1 @ xs1) @ xs @ (xs2 @ ys2)" by simp |
639eb3617a86
reorganised material on sublists
eberlm <eberlm@in.tum.de>
parents:
65954
diff
changeset
|
1256 |
thus "sublist xs zs" unfolding sublist_def by blast |
639eb3617a86
reorganised material on sublists
eberlm <eberlm@in.tum.de>
parents:
65954
diff
changeset
|
1257 |
next |
639eb3617a86
reorganised material on sublists
eberlm <eberlm@in.tum.de>
parents:
65954
diff
changeset
|
1258 |
fix xs ys :: "'a list" |
81332 | 1259 |
show "xs = ys" if "sublist xs ys" "sublist ys xs" |
1260 |
proof - |
|
1261 |
from that obtain as bs cs ds where xs: "xs = as @ ys @ bs" and ys: "ys = cs @ xs @ ds" |
|
65956
639eb3617a86
reorganised material on sublists
eberlm <eberlm@in.tum.de>
parents:
65954
diff
changeset
|
1262 |
by (auto simp: sublist_def) |
639eb3617a86
reorganised material on sublists
eberlm <eberlm@in.tum.de>
parents:
65954
diff
changeset
|
1263 |
have "xs = as @ cs @ xs @ ds @ bs" by (subst xs, subst ys) auto |
81332 | 1264 |
also have "length \<dots> = length as + length cs + length xs + length bs + length ds" |
65956
639eb3617a86
reorganised material on sublists
eberlm <eberlm@in.tum.de>
parents:
65954
diff
changeset
|
1265 |
by simp |
639eb3617a86
reorganised material on sublists
eberlm <eberlm@in.tum.de>
parents:
65954
diff
changeset
|
1266 |
finally have "as = []" "bs = []" by simp_all |
81332 | 1267 |
with xs show ?thesis by simp |
1268 |
qed |
|
1269 |
thus "strict_sublist xs ys \<longleftrightarrow> (sublist xs ys \<and> \<not> sublist ys xs)" |
|
65956
639eb3617a86
reorganised material on sublists
eberlm <eberlm@in.tum.de>
parents:
65954
diff
changeset
|
1270 |
by (auto simp: strict_sublist_def) |
639eb3617a86
reorganised material on sublists
eberlm <eberlm@in.tum.de>
parents:
65954
diff
changeset
|
1271 |
qed (auto simp: strict_sublist_def sublist_def intro: exI[of _ "[]"]) |
81332 | 1272 |
|
65956
639eb3617a86
reorganised material on sublists
eberlm <eberlm@in.tum.de>
parents:
65954
diff
changeset
|
1273 |
lemma sublist_Nil_left [simp, intro]: "sublist [] ys" |
639eb3617a86
reorganised material on sublists
eberlm <eberlm@in.tum.de>
parents:
65954
diff
changeset
|
1274 |
by (auto simp: sublist_def) |
81332 | 1275 |
|
65956
639eb3617a86
reorganised material on sublists
eberlm <eberlm@in.tum.de>
parents:
65954
diff
changeset
|
1276 |
lemma sublist_Cons_Nil [simp]: "\<not>sublist (x#xs) []" |
639eb3617a86
reorganised material on sublists
eberlm <eberlm@in.tum.de>
parents:
65954
diff
changeset
|
1277 |
by (auto simp: sublist_def) |
81332 | 1278 |
|
65956
639eb3617a86
reorganised material on sublists
eberlm <eberlm@in.tum.de>
parents:
65954
diff
changeset
|
1279 |
lemma sublist_Nil_right [simp]: "sublist xs [] \<longleftrightarrow> xs = []" |
639eb3617a86
reorganised material on sublists
eberlm <eberlm@in.tum.de>
parents:
65954
diff
changeset
|
1280 |
by (cases xs) auto |
81332 | 1281 |
|
65956
639eb3617a86
reorganised material on sublists
eberlm <eberlm@in.tum.de>
parents:
65954
diff
changeset
|
1282 |
lemma sublist_appendI [simp, intro]: "sublist xs (ps @ xs @ ss)" |
639eb3617a86
reorganised material on sublists
eberlm <eberlm@in.tum.de>
parents:
65954
diff
changeset
|
1283 |
by (auto simp: sublist_def) |
81332 | 1284 |
|
65956
639eb3617a86
reorganised material on sublists
eberlm <eberlm@in.tum.de>
parents:
65954
diff
changeset
|
1285 |
lemma sublist_append_leftI [simp, intro]: "sublist xs (ps @ xs)" |
639eb3617a86
reorganised material on sublists
eberlm <eberlm@in.tum.de>
parents:
65954
diff
changeset
|
1286 |
by (auto simp: sublist_def intro: exI[of _ "[]"]) |
81332 | 1287 |
|
65956
639eb3617a86
reorganised material on sublists
eberlm <eberlm@in.tum.de>
parents:
65954
diff
changeset
|
1288 |
lemma sublist_append_rightI [simp, intro]: "sublist xs (xs @ ss)" |
82218
cbf9f856d3e0
Some new lemmas and some tidying
paulson <lp15@cam.ac.uk>
parents:
81332
diff
changeset
|
1289 |
by (metis append_eq_append_conv2 sublist_appendI) |
65956
639eb3617a86
reorganised material on sublists
eberlm <eberlm@in.tum.de>
parents:
65954
diff
changeset
|
1290 |
|
639eb3617a86
reorganised material on sublists
eberlm <eberlm@in.tum.de>
parents:
65954
diff
changeset
|
1291 |
lemma sublist_altdef: "sublist xs ys \<longleftrightarrow> (\<exists>ys'. prefix ys' ys \<and> suffix xs ys')" |
82218
cbf9f856d3e0
Some new lemmas and some tidying
paulson <lp15@cam.ac.uk>
parents:
81332
diff
changeset
|
1292 |
by (metis append_assoc prefix_def sublist_def suffix_def) |
81332 | 1293 |
|
65956
639eb3617a86
reorganised material on sublists
eberlm <eberlm@in.tum.de>
parents:
65954
diff
changeset
|
1294 |
lemma sublist_altdef': "sublist xs ys \<longleftrightarrow> (\<exists>ys'. suffix ys' ys \<and> prefix xs ys')" |
82218
cbf9f856d3e0
Some new lemmas and some tidying
paulson <lp15@cam.ac.uk>
parents:
81332
diff
changeset
|
1295 |
by (metis prefixE prefixI sublist_appendI sublist_def suffixE suffixI) |
65956
639eb3617a86
reorganised material on sublists
eberlm <eberlm@in.tum.de>
parents:
65954
diff
changeset
|
1296 |
|
639eb3617a86
reorganised material on sublists
eberlm <eberlm@in.tum.de>
parents:
65954
diff
changeset
|
1297 |
lemma sublist_Cons_right: "sublist xs (y # ys) \<longleftrightarrow> prefix xs (y # ys) \<or> sublist xs ys" |
639eb3617a86
reorganised material on sublists
eberlm <eberlm@in.tum.de>
parents:
65954
diff
changeset
|
1298 |
by (auto simp: sublist_def prefix_def Cons_eq_append_conv) |
81332 | 1299 |
|
65956
639eb3617a86
reorganised material on sublists
eberlm <eberlm@in.tum.de>
parents:
65954
diff
changeset
|
1300 |
lemma sublist_code [code]: |
639eb3617a86
reorganised material on sublists
eberlm <eberlm@in.tum.de>
parents:
65954
diff
changeset
|
1301 |
"sublist [] ys \<longleftrightarrow> True" |
639eb3617a86
reorganised material on sublists
eberlm <eberlm@in.tum.de>
parents:
65954
diff
changeset
|
1302 |
"sublist (x # xs) [] \<longleftrightarrow> False" |
639eb3617a86
reorganised material on sublists
eberlm <eberlm@in.tum.de>
parents:
65954
diff
changeset
|
1303 |
"sublist (x # xs) (y # ys) \<longleftrightarrow> prefix (x # xs) (y # ys) \<or> sublist (x # xs) ys" |
639eb3617a86
reorganised material on sublists
eberlm <eberlm@in.tum.de>
parents:
65954
diff
changeset
|
1304 |
by (simp_all add: sublist_Cons_right) |
639eb3617a86
reorganised material on sublists
eberlm <eberlm@in.tum.de>
parents:
65954
diff
changeset
|
1305 |
|
639eb3617a86
reorganised material on sublists
eberlm <eberlm@in.tum.de>
parents:
65954
diff
changeset
|
1306 |
lemma sublist_append: |
81332 | 1307 |
"sublist xs (ys @ zs) \<longleftrightarrow> |
65956
639eb3617a86
reorganised material on sublists
eberlm <eberlm@in.tum.de>
parents:
65954
diff
changeset
|
1308 |
sublist xs ys \<or> sublist xs zs \<or> (\<exists>xs1 xs2. xs = xs1 @ xs2 \<and> suffix xs1 ys \<and> prefix xs2 zs)" |
71789 | 1309 |
by (auto simp: sublist_altdef prefix_append suffix_append) |
65956
639eb3617a86
reorganised material on sublists
eberlm <eberlm@in.tum.de>
parents:
65954
diff
changeset
|
1310 |
|
71789 | 1311 |
lemma map_mono_sublist: |
1312 |
assumes "sublist xs ys" |
|
1313 |
shows "sublist (map f xs) (map f ys)" |
|
1314 |
proof - |
|
1315 |
from assms obtain xs1 xs2 where ys: "ys = xs1 @ xs @ xs2" |
|
1316 |
by (auto simp: sublist_def) |
|
1317 |
have "map f ys = map f xs1 @ map f xs @ map f xs2" |
|
1318 |
by (auto simp: ys) |
|
1319 |
thus ?thesis |
|
1320 |
by (auto simp: sublist_def) |
|
1321 |
qed |
|
65956
639eb3617a86
reorganised material on sublists
eberlm <eberlm@in.tum.de>
parents:
65954
diff
changeset
|
1322 |
|
639eb3617a86
reorganised material on sublists
eberlm <eberlm@in.tum.de>
parents:
65954
diff
changeset
|
1323 |
lemma sublist_length_le: "sublist xs ys \<Longrightarrow> length xs \<le> length ys" |
639eb3617a86
reorganised material on sublists
eberlm <eberlm@in.tum.de>
parents:
65954
diff
changeset
|
1324 |
by (auto simp add: sublist_def) |
639eb3617a86
reorganised material on sublists
eberlm <eberlm@in.tum.de>
parents:
65954
diff
changeset
|
1325 |
|
639eb3617a86
reorganised material on sublists
eberlm <eberlm@in.tum.de>
parents:
65954
diff
changeset
|
1326 |
lemma set_mono_sublist: "sublist xs ys \<Longrightarrow> set xs \<subseteq> set ys" |
639eb3617a86
reorganised material on sublists
eberlm <eberlm@in.tum.de>
parents:
65954
diff
changeset
|
1327 |
by (auto simp add: sublist_def) |
81332 | 1328 |
|
65956
639eb3617a86
reorganised material on sublists
eberlm <eberlm@in.tum.de>
parents:
65954
diff
changeset
|
1329 |
lemma prefix_imp_sublist [simp, intro]: "prefix xs ys \<Longrightarrow> sublist xs ys" |
639eb3617a86
reorganised material on sublists
eberlm <eberlm@in.tum.de>
parents:
65954
diff
changeset
|
1330 |
by (auto simp: sublist_def prefix_def intro: exI[of _ "[]"]) |
81332 | 1331 |
|
65956
639eb3617a86
reorganised material on sublists
eberlm <eberlm@in.tum.de>
parents:
65954
diff
changeset
|
1332 |
lemma suffix_imp_sublist [simp, intro]: "suffix xs ys \<Longrightarrow> sublist xs ys" |
639eb3617a86
reorganised material on sublists
eberlm <eberlm@in.tum.de>
parents:
65954
diff
changeset
|
1333 |
by (auto simp: sublist_def suffix_def intro: exI[of _ "[]"]) |
639eb3617a86
reorganised material on sublists
eberlm <eberlm@in.tum.de>
parents:
65954
diff
changeset
|
1334 |
|
639eb3617a86
reorganised material on sublists
eberlm <eberlm@in.tum.de>
parents:
65954
diff
changeset
|
1335 |
lemma sublist_take [simp, intro]: "sublist (take n xs) xs" |
73380
99c1c4f89605
added lemmas takeWhile_is_prefix, suffix_dropWhile, and sublist_(take|drop)While
desharna
parents:
73186
diff
changeset
|
1336 |
by (rule prefix_imp_sublist[OF take_is_prefix]) |
99c1c4f89605
added lemmas takeWhile_is_prefix, suffix_dropWhile, and sublist_(take|drop)While
desharna
parents:
73186
diff
changeset
|
1337 |
|
99c1c4f89605
added lemmas takeWhile_is_prefix, suffix_dropWhile, and sublist_(take|drop)While
desharna
parents:
73186
diff
changeset
|
1338 |
lemma sublist_takeWhile [simp, intro]: "sublist (takeWhile P xs) xs" |
99c1c4f89605
added lemmas takeWhile_is_prefix, suffix_dropWhile, and sublist_(take|drop)While
desharna
parents:
73186
diff
changeset
|
1339 |
by (rule prefix_imp_sublist[OF takeWhile_is_prefix]) |
65956
639eb3617a86
reorganised material on sublists
eberlm <eberlm@in.tum.de>
parents:
65954
diff
changeset
|
1340 |
|
639eb3617a86
reorganised material on sublists
eberlm <eberlm@in.tum.de>
parents:
65954
diff
changeset
|
1341 |
lemma sublist_drop [simp, intro]: "sublist (drop n xs) xs" |
73380
99c1c4f89605
added lemmas takeWhile_is_prefix, suffix_dropWhile, and sublist_(take|drop)While
desharna
parents:
73186
diff
changeset
|
1342 |
by (rule suffix_imp_sublist[OF suffix_drop]) |
99c1c4f89605
added lemmas takeWhile_is_prefix, suffix_dropWhile, and sublist_(take|drop)While
desharna
parents:
73186
diff
changeset
|
1343 |
|
99c1c4f89605
added lemmas takeWhile_is_prefix, suffix_dropWhile, and sublist_(take|drop)While
desharna
parents:
73186
diff
changeset
|
1344 |
lemma sublist_dropWhile [simp, intro]: "sublist (dropWhile P xs) xs" |
99c1c4f89605
added lemmas takeWhile_is_prefix, suffix_dropWhile, and sublist_(take|drop)While
desharna
parents:
73186
diff
changeset
|
1345 |
by (rule suffix_imp_sublist[OF suffix_dropWhile]) |
81332 | 1346 |
|
65956
639eb3617a86
reorganised material on sublists
eberlm <eberlm@in.tum.de>
parents:
65954
diff
changeset
|
1347 |
lemma sublist_tl [simp, intro]: "sublist (tl xs) xs" |
639eb3617a86
reorganised material on sublists
eberlm <eberlm@in.tum.de>
parents:
65954
diff
changeset
|
1348 |
by (rule suffix_imp_sublist) (simp_all add: suffix_drop) |
81332 | 1349 |
|
65956
639eb3617a86
reorganised material on sublists
eberlm <eberlm@in.tum.de>
parents:
65954
diff
changeset
|
1350 |
lemma sublist_butlast [simp, intro]: "sublist (butlast xs) xs" |
639eb3617a86
reorganised material on sublists
eberlm <eberlm@in.tum.de>
parents:
65954
diff
changeset
|
1351 |
by (rule prefix_imp_sublist) (simp_all add: prefixeq_butlast) |
81332 | 1352 |
|
65956
639eb3617a86
reorganised material on sublists
eberlm <eberlm@in.tum.de>
parents:
65954
diff
changeset
|
1353 |
lemma sublist_rev [simp]: "sublist (rev xs) (rev ys) = sublist xs ys" |
639eb3617a86
reorganised material on sublists
eberlm <eberlm@in.tum.de>
parents:
65954
diff
changeset
|
1354 |
proof |
639eb3617a86
reorganised material on sublists
eberlm <eberlm@in.tum.de>
parents:
65954
diff
changeset
|
1355 |
assume "sublist (rev xs) (rev ys)" |
639eb3617a86
reorganised material on sublists
eberlm <eberlm@in.tum.de>
parents:
65954
diff
changeset
|
1356 |
then obtain as bs where "rev ys = as @ rev xs @ bs" |
639eb3617a86
reorganised material on sublists
eberlm <eberlm@in.tum.de>
parents:
65954
diff
changeset
|
1357 |
by (auto simp: sublist_def) |
639eb3617a86
reorganised material on sublists
eberlm <eberlm@in.tum.de>
parents:
65954
diff
changeset
|
1358 |
also have "rev \<dots> = rev bs @ xs @ rev as" by simp |
639eb3617a86
reorganised material on sublists
eberlm <eberlm@in.tum.de>
parents:
65954
diff
changeset
|
1359 |
finally show "sublist xs ys" by simp |
639eb3617a86
reorganised material on sublists
eberlm <eberlm@in.tum.de>
parents:
65954
diff
changeset
|
1360 |
next |
639eb3617a86
reorganised material on sublists
eberlm <eberlm@in.tum.de>
parents:
65954
diff
changeset
|
1361 |
assume "sublist xs ys" |
639eb3617a86
reorganised material on sublists
eberlm <eberlm@in.tum.de>
parents:
65954
diff
changeset
|
1362 |
then obtain as bs where "ys = as @ xs @ bs" |
639eb3617a86
reorganised material on sublists
eberlm <eberlm@in.tum.de>
parents:
65954
diff
changeset
|
1363 |
by (auto simp: sublist_def) |
639eb3617a86
reorganised material on sublists
eberlm <eberlm@in.tum.de>
parents:
65954
diff
changeset
|
1364 |
also have "rev \<dots> = rev bs @ rev xs @ rev as" by simp |
639eb3617a86
reorganised material on sublists
eberlm <eberlm@in.tum.de>
parents:
65954
diff
changeset
|
1365 |
finally show "sublist (rev xs) (rev ys)" by simp |
639eb3617a86
reorganised material on sublists
eberlm <eberlm@in.tum.de>
parents:
65954
diff
changeset
|
1366 |
qed |
81332 | 1367 |
|
65956
639eb3617a86
reorganised material on sublists
eberlm <eberlm@in.tum.de>
parents:
65954
diff
changeset
|
1368 |
lemma sublist_rev_left: "sublist (rev xs) ys = sublist xs (rev ys)" |
639eb3617a86
reorganised material on sublists
eberlm <eberlm@in.tum.de>
parents:
65954
diff
changeset
|
1369 |
by (subst sublist_rev [symmetric]) (simp only: rev_rev_ident) |
81332 | 1370 |
|
65956
639eb3617a86
reorganised material on sublists
eberlm <eberlm@in.tum.de>
parents:
65954
diff
changeset
|
1371 |
lemma sublist_rev_right: "sublist xs (rev ys) = sublist (rev xs) ys" |
639eb3617a86
reorganised material on sublists
eberlm <eberlm@in.tum.de>
parents:
65954
diff
changeset
|
1372 |
by (subst sublist_rev [symmetric]) (simp only: rev_rev_ident) |
639eb3617a86
reorganised material on sublists
eberlm <eberlm@in.tum.de>
parents:
65954
diff
changeset
|
1373 |
|
81332 | 1374 |
lemma snoc_sublist_snoc: |
1375 |
"sublist (xs @ [x]) (ys @ [y]) \<longleftrightarrow> |
|
65956
639eb3617a86
reorganised material on sublists
eberlm <eberlm@in.tum.de>
parents:
65954
diff
changeset
|
1376 |
(x = y \<and> suffix xs ys \<or> sublist (xs @ [x]) ys) " |
639eb3617a86
reorganised material on sublists
eberlm <eberlm@in.tum.de>
parents:
65954
diff
changeset
|
1377 |
by (subst (1 2) sublist_rev [symmetric]) |
639eb3617a86
reorganised material on sublists
eberlm <eberlm@in.tum.de>
parents:
65954
diff
changeset
|
1378 |
(simp del: sublist_rev add: sublist_Cons_right suffix_to_prefix) |
639eb3617a86
reorganised material on sublists
eberlm <eberlm@in.tum.de>
parents:
65954
diff
changeset
|
1379 |
|
639eb3617a86
reorganised material on sublists
eberlm <eberlm@in.tum.de>
parents:
65954
diff
changeset
|
1380 |
lemma sublist_snoc: |
639eb3617a86
reorganised material on sublists
eberlm <eberlm@in.tum.de>
parents:
65954
diff
changeset
|
1381 |
"sublist xs (ys @ [y]) \<longleftrightarrow> suffix xs (ys @ [y]) \<or> sublist xs ys" |
639eb3617a86
reorganised material on sublists
eberlm <eberlm@in.tum.de>
parents:
65954
diff
changeset
|
1382 |
by (subst (1 2) sublist_rev [symmetric]) |
81332 | 1383 |
(simp del: sublist_rev add: sublist_Cons_right suffix_to_prefix) |
1384 |
||
65957 | 1385 |
lemma sublist_imp_subseq [intro]: "sublist xs ys \<Longrightarrow> subseq xs ys" |
1386 |
by (auto simp: sublist_def) |
|
65956
639eb3617a86
reorganised material on sublists
eberlm <eberlm@in.tum.de>
parents:
65954
diff
changeset
|
1387 |
|
71789 | 1388 |
lemma sublist_map_rightE: |
1389 |
assumes "sublist xs (map f ys)" |
|
1390 |
shows "\<exists>xs'. sublist xs' ys \<and> xs = map f xs'" |
|
1391 |
proof - |
|
1392 |
note takedrop = sublist_take sublist_drop |
|
1393 |
define n where "n = (length ys - length xs)" |
|
1394 |
from assms obtain xs1 xs2 where xs12: "map f ys = xs1 @ xs @ xs2" |
|
1395 |
by (auto simp: sublist_def) |
|
1396 |
define n where "n = length xs1" |
|
1397 |
have "xs = take (length xs) (drop n (map f ys))" |
|
1398 |
by (simp add: xs12 n_def) |
|
1399 |
thus ?thesis |
|
1400 |
by (intro exI[of _ "take (length xs) (drop n ys)"]) |
|
1401 |
(auto simp: take_map drop_map intro!: takedrop[THEN sublist_order.order.trans]) |
|
1402 |
qed |
|
1403 |
||
1404 |
lemma sublist_remdups_adj: |
|
1405 |
assumes "sublist xs ys" |
|
1406 |
shows "sublist (remdups_adj xs) (remdups_adj ys)" |
|
1407 |
proof - |
|
1408 |
from assms obtain xs1 xs2 where ys: "ys = xs1 @ xs @ xs2" |
|
1409 |
by (auto simp: sublist_def) |
|
1410 |
have "suffix (remdups_adj (xs @ xs2)) (remdups_adj (xs1 @ xs @ xs2))" |
|
1411 |
by (rule suffix_remdups_adj, rule suffix_appendI) auto |
|
1412 |
then obtain zs1 where zs1: "remdups_adj (xs1 @ xs @ xs2) = zs1 @ remdups_adj (xs @ xs2)" |
|
1413 |
by (auto simp: suffix_def) |
|
1414 |
have "prefix (remdups_adj xs) (remdups_adj (xs @ xs2))" |
|
1415 |
by (intro prefix_remdups_adj) auto |
|
1416 |
then obtain zs2 where zs2: "remdups_adj (xs @ xs2) = remdups_adj xs @ zs2" |
|
1417 |
by (auto simp: prefix_def) |
|
1418 |
show ?thesis |
|
1419 |
by (simp add: ys zs1 zs2) |
|
1420 |
qed |
|
1421 |
||
1422 |
subsubsection \<open>\<open>sublists\<close>\<close> |
|
1423 |
||
1424 |
primrec sublists :: "'a list \<Rightarrow> 'a list list" where |
|
1425 |
"sublists [] = [[]]" |
|
1426 |
| "sublists (x # xs) = sublists xs @ map ((#) x) (prefixes xs)" |
|
1427 |
||
81332 | 1428 |
lemma in_set_sublists [simp]: "xs \<in> set (sublists ys) \<longleftrightarrow> sublist xs ys" |
71789 | 1429 |
by (induction ys arbitrary: xs) (auto simp: sublist_Cons_right prefix_Cons) |
1430 |
||
1431 |
lemma set_sublists_eq: "set (sublists xs) = {ys. sublist ys xs}" |
|
1432 |
by auto |
|
1433 |
||
1434 |
lemma length_sublists [simp]: "length (sublists xs) = Suc (length xs * Suc (length xs) div 2)" |
|
1435 |
by (induction xs) simp_all |
|
1436 |
||
1437 |
||
65956
639eb3617a86
reorganised material on sublists
eberlm <eberlm@in.tum.de>
parents:
65954
diff
changeset
|
1438 |
subsection \<open>Parametricity\<close> |
639eb3617a86
reorganised material on sublists
eberlm <eberlm@in.tum.de>
parents:
65954
diff
changeset
|
1439 |
|
639eb3617a86
reorganised material on sublists
eberlm <eberlm@in.tum.de>
parents:
65954
diff
changeset
|
1440 |
context includes lifting_syntax |
81332 | 1441 |
begin |
1442 |
||
65956
639eb3617a86
reorganised material on sublists
eberlm <eberlm@in.tum.de>
parents:
65954
diff
changeset
|
1443 |
private lemma prefix_primrec: |
639eb3617a86
reorganised material on sublists
eberlm <eberlm@in.tum.de>
parents:
65954
diff
changeset
|
1444 |
"prefix = rec_list (\<lambda>xs. True) (\<lambda>x xs xsa ys. |
639eb3617a86
reorganised material on sublists
eberlm <eberlm@in.tum.de>
parents:
65954
diff
changeset
|
1445 |
case ys of [] \<Rightarrow> False | y # ys \<Rightarrow> x = y \<and> xsa ys)" |
639eb3617a86
reorganised material on sublists
eberlm <eberlm@in.tum.de>
parents:
65954
diff
changeset
|
1446 |
proof (intro ext, goal_cases) |
639eb3617a86
reorganised material on sublists
eberlm <eberlm@in.tum.de>
parents:
65954
diff
changeset
|
1447 |
case (1 xs ys) |
639eb3617a86
reorganised material on sublists
eberlm <eberlm@in.tum.de>
parents:
65954
diff
changeset
|
1448 |
show ?case by (induction xs arbitrary: ys) (auto simp: prefix_Cons split: list.splits) |
639eb3617a86
reorganised material on sublists
eberlm <eberlm@in.tum.de>
parents:
65954
diff
changeset
|
1449 |
qed |
639eb3617a86
reorganised material on sublists
eberlm <eberlm@in.tum.de>
parents:
65954
diff
changeset
|
1450 |
|
639eb3617a86
reorganised material on sublists
eberlm <eberlm@in.tum.de>
parents:
65954
diff
changeset
|
1451 |
private lemma sublist_primrec: |
639eb3617a86
reorganised material on sublists
eberlm <eberlm@in.tum.de>
parents:
65954
diff
changeset
|
1452 |
"sublist = (\<lambda>xs ys. rec_list (\<lambda>xs. xs = []) (\<lambda>y ys ysa xs. prefix xs (y # ys) \<or> ysa xs) ys xs)" |
639eb3617a86
reorganised material on sublists
eberlm <eberlm@in.tum.de>
parents:
65954
diff
changeset
|
1453 |
proof (intro ext, goal_cases) |
639eb3617a86
reorganised material on sublists
eberlm <eberlm@in.tum.de>
parents:
65954
diff
changeset
|
1454 |
case (1 xs ys) |
639eb3617a86
reorganised material on sublists
eberlm <eberlm@in.tum.de>
parents:
65954
diff
changeset
|
1455 |
show ?case by (induction ys) (auto simp: sublist_Cons_right) |
639eb3617a86
reorganised material on sublists
eberlm <eberlm@in.tum.de>
parents:
65954
diff
changeset
|
1456 |
qed |
639eb3617a86
reorganised material on sublists
eberlm <eberlm@in.tum.de>
parents:
65954
diff
changeset
|
1457 |
|
639eb3617a86
reorganised material on sublists
eberlm <eberlm@in.tum.de>
parents:
65954
diff
changeset
|
1458 |
private lemma list_emb_primrec: |
82218
cbf9f856d3e0
Some new lemmas and some tidying
paulson <lp15@cam.ac.uk>
parents:
81332
diff
changeset
|
1459 |
"list_emb = (\<lambda>uu l' l. rec_list (\<lambda>P xs. List.null xs) (\<lambda>y ys ysa P xs. case xs of [] \<Rightarrow> True |
cbf9f856d3e0
Some new lemmas and some tidying
paulson <lp15@cam.ac.uk>
parents:
81332
diff
changeset
|
1460 |
| x # xs \<Rightarrow> if P x y then ysa P xs else ysa P (x # xs)) l uu l')" |
65956
639eb3617a86
reorganised material on sublists
eberlm <eberlm@in.tum.de>
parents:
65954
diff
changeset
|
1461 |
proof (intro ext, goal_cases) |
639eb3617a86
reorganised material on sublists
eberlm <eberlm@in.tum.de>
parents:
65954
diff
changeset
|
1462 |
case (1 P xs ys) |
639eb3617a86
reorganised material on sublists
eberlm <eberlm@in.tum.de>
parents:
65954
diff
changeset
|
1463 |
show ?case |
639eb3617a86
reorganised material on sublists
eberlm <eberlm@in.tum.de>
parents:
65954
diff
changeset
|
1464 |
by (induction ys arbitrary: xs) |
639eb3617a86
reorganised material on sublists
eberlm <eberlm@in.tum.de>
parents:
65954
diff
changeset
|
1465 |
(auto simp: list_emb_code List.null_def split: list.splits) |
639eb3617a86
reorganised material on sublists
eberlm <eberlm@in.tum.de>
parents:
65954
diff
changeset
|
1466 |
qed |
639eb3617a86
reorganised material on sublists
eberlm <eberlm@in.tum.de>
parents:
65954
diff
changeset
|
1467 |
|
639eb3617a86
reorganised material on sublists
eberlm <eberlm@in.tum.de>
parents:
65954
diff
changeset
|
1468 |
lemma prefix_transfer [transfer_rule]: |
639eb3617a86
reorganised material on sublists
eberlm <eberlm@in.tum.de>
parents:
65954
diff
changeset
|
1469 |
assumes [transfer_rule]: "bi_unique A" |
81332 | 1470 |
shows "(list_all2 A ===> list_all2 A ===> (=)) prefix prefix" |
65956
639eb3617a86
reorganised material on sublists
eberlm <eberlm@in.tum.de>
parents:
65954
diff
changeset
|
1471 |
unfolding prefix_primrec by transfer_prover |
81332 | 1472 |
|
65956
639eb3617a86
reorganised material on sublists
eberlm <eberlm@in.tum.de>
parents:
65954
diff
changeset
|
1473 |
lemma suffix_transfer [transfer_rule]: |
639eb3617a86
reorganised material on sublists
eberlm <eberlm@in.tum.de>
parents:
65954
diff
changeset
|
1474 |
assumes [transfer_rule]: "bi_unique A" |
81332 | 1475 |
shows "(list_all2 A ===> list_all2 A ===> (=)) suffix suffix" |
65956
639eb3617a86
reorganised material on sublists
eberlm <eberlm@in.tum.de>
parents:
65954
diff
changeset
|
1476 |
unfolding suffix_to_prefix [abs_def] by transfer_prover |
639eb3617a86
reorganised material on sublists
eberlm <eberlm@in.tum.de>
parents:
65954
diff
changeset
|
1477 |
|
639eb3617a86
reorganised material on sublists
eberlm <eberlm@in.tum.de>
parents:
65954
diff
changeset
|
1478 |
lemma sublist_transfer [transfer_rule]: |
639eb3617a86
reorganised material on sublists
eberlm <eberlm@in.tum.de>
parents:
65954
diff
changeset
|
1479 |
assumes [transfer_rule]: "bi_unique A" |
67399 | 1480 |
shows "(list_all2 A ===> list_all2 A ===> (=)) sublist sublist" |
65956
639eb3617a86
reorganised material on sublists
eberlm <eberlm@in.tum.de>
parents:
65954
diff
changeset
|
1481 |
unfolding sublist_primrec by transfer_prover |
639eb3617a86
reorganised material on sublists
eberlm <eberlm@in.tum.de>
parents:
65954
diff
changeset
|
1482 |
|
639eb3617a86
reorganised material on sublists
eberlm <eberlm@in.tum.de>
parents:
65954
diff
changeset
|
1483 |
lemma parallel_transfer [transfer_rule]: |
639eb3617a86
reorganised material on sublists
eberlm <eberlm@in.tum.de>
parents:
65954
diff
changeset
|
1484 |
assumes [transfer_rule]: "bi_unique A" |
67399 | 1485 |
shows "(list_all2 A ===> list_all2 A ===> (=)) parallel parallel" |
65956
639eb3617a86
reorganised material on sublists
eberlm <eberlm@in.tum.de>
parents:
65954
diff
changeset
|
1486 |
unfolding parallel_def by transfer_prover |
81332 | 1487 |
|
65956
639eb3617a86
reorganised material on sublists
eberlm <eberlm@in.tum.de>
parents:
65954
diff
changeset
|
1488 |
|
639eb3617a86
reorganised material on sublists
eberlm <eberlm@in.tum.de>
parents:
65954
diff
changeset
|
1489 |
|
639eb3617a86
reorganised material on sublists
eberlm <eberlm@in.tum.de>
parents:
65954
diff
changeset
|
1490 |
lemma list_emb_transfer [transfer_rule]: |
67399 | 1491 |
"((A ===> A ===> (=)) ===> list_all2 A ===> list_all2 A ===> (=)) list_emb list_emb" |
65956
639eb3617a86
reorganised material on sublists
eberlm <eberlm@in.tum.de>
parents:
65954
diff
changeset
|
1492 |
unfolding list_emb_primrec by transfer_prover |
639eb3617a86
reorganised material on sublists
eberlm <eberlm@in.tum.de>
parents:
65954
diff
changeset
|
1493 |
|
639eb3617a86
reorganised material on sublists
eberlm <eberlm@in.tum.de>
parents:
65954
diff
changeset
|
1494 |
lemma strict_prefix_transfer [transfer_rule]: |
639eb3617a86
reorganised material on sublists
eberlm <eberlm@in.tum.de>
parents:
65954
diff
changeset
|
1495 |
assumes [transfer_rule]: "bi_unique A" |
81332 | 1496 |
shows "(list_all2 A ===> list_all2 A ===> (=)) strict_prefix strict_prefix" |
65956
639eb3617a86
reorganised material on sublists
eberlm <eberlm@in.tum.de>
parents:
65954
diff
changeset
|
1497 |
unfolding strict_prefix_def by transfer_prover |
81332 | 1498 |
|
65956
639eb3617a86
reorganised material on sublists
eberlm <eberlm@in.tum.de>
parents:
65954
diff
changeset
|
1499 |
lemma strict_suffix_transfer [transfer_rule]: |
639eb3617a86
reorganised material on sublists
eberlm <eberlm@in.tum.de>
parents:
65954
diff
changeset
|
1500 |
assumes [transfer_rule]: "bi_unique A" |
81332 | 1501 |
shows "(list_all2 A ===> list_all2 A ===> (=)) strict_suffix strict_suffix" |
65956
639eb3617a86
reorganised material on sublists
eberlm <eberlm@in.tum.de>
parents:
65954
diff
changeset
|
1502 |
unfolding strict_suffix_def by transfer_prover |
81332 | 1503 |
|
65956
639eb3617a86
reorganised material on sublists
eberlm <eberlm@in.tum.de>
parents:
65954
diff
changeset
|
1504 |
lemma strict_subseq_transfer [transfer_rule]: |
639eb3617a86
reorganised material on sublists
eberlm <eberlm@in.tum.de>
parents:
65954
diff
changeset
|
1505 |
assumes [transfer_rule]: "bi_unique A" |
81332 | 1506 |
shows "(list_all2 A ===> list_all2 A ===> (=)) strict_subseq strict_subseq" |
65956
639eb3617a86
reorganised material on sublists
eberlm <eberlm@in.tum.de>
parents:
65954
diff
changeset
|
1507 |
unfolding strict_subseq_def by transfer_prover |
81332 | 1508 |
|
65956
639eb3617a86
reorganised material on sublists
eberlm <eberlm@in.tum.de>
parents:
65954
diff
changeset
|
1509 |
lemma strict_sublist_transfer [transfer_rule]: |
639eb3617a86
reorganised material on sublists
eberlm <eberlm@in.tum.de>
parents:
65954
diff
changeset
|
1510 |
assumes [transfer_rule]: "bi_unique A" |
81332 | 1511 |
shows "(list_all2 A ===> list_all2 A ===> (=)) strict_sublist strict_sublist" |
65956
639eb3617a86
reorganised material on sublists
eberlm <eberlm@in.tum.de>
parents:
65954
diff
changeset
|
1512 |
unfolding strict_sublist_def by transfer_prover |
639eb3617a86
reorganised material on sublists
eberlm <eberlm@in.tum.de>
parents:
65954
diff
changeset
|
1513 |
|
639eb3617a86
reorganised material on sublists
eberlm <eberlm@in.tum.de>
parents:
65954
diff
changeset
|
1514 |
lemma prefixes_transfer [transfer_rule]: |
639eb3617a86
reorganised material on sublists
eberlm <eberlm@in.tum.de>
parents:
65954
diff
changeset
|
1515 |
assumes [transfer_rule]: "bi_unique A" |
639eb3617a86
reorganised material on sublists
eberlm <eberlm@in.tum.de>
parents:
65954
diff
changeset
|
1516 |
shows "(list_all2 A ===> list_all2 (list_all2 A)) prefixes prefixes" |
639eb3617a86
reorganised material on sublists
eberlm <eberlm@in.tum.de>
parents:
65954
diff
changeset
|
1517 |
unfolding prefixes_def by transfer_prover |
81332 | 1518 |
|
65956
639eb3617a86
reorganised material on sublists
eberlm <eberlm@in.tum.de>
parents:
65954
diff
changeset
|
1519 |
lemma suffixes_transfer [transfer_rule]: |
639eb3617a86
reorganised material on sublists
eberlm <eberlm@in.tum.de>
parents:
65954
diff
changeset
|
1520 |
assumes [transfer_rule]: "bi_unique A" |
639eb3617a86
reorganised material on sublists
eberlm <eberlm@in.tum.de>
parents:
65954
diff
changeset
|
1521 |
shows "(list_all2 A ===> list_all2 (list_all2 A)) suffixes suffixes" |
639eb3617a86
reorganised material on sublists
eberlm <eberlm@in.tum.de>
parents:
65954
diff
changeset
|
1522 |
unfolding suffixes_def by transfer_prover |
81332 | 1523 |
|
65956
639eb3617a86
reorganised material on sublists
eberlm <eberlm@in.tum.de>
parents:
65954
diff
changeset
|
1524 |
lemma sublists_transfer [transfer_rule]: |
639eb3617a86
reorganised material on sublists
eberlm <eberlm@in.tum.de>
parents:
65954
diff
changeset
|
1525 |
assumes [transfer_rule]: "bi_unique A" |
639eb3617a86
reorganised material on sublists
eberlm <eberlm@in.tum.de>
parents:
65954
diff
changeset
|
1526 |
shows "(list_all2 A ===> list_all2 (list_all2 A)) sublists sublists" |
639eb3617a86
reorganised material on sublists
eberlm <eberlm@in.tum.de>
parents:
65954
diff
changeset
|
1527 |
unfolding sublists_def by transfer_prover |
49087 | 1528 |
|
10330
4362e906b745
"List prefixes" library theory (replaces old Lex/Prefix);
wenzelm
parents:
diff
changeset
|
1529 |
end |
65956
639eb3617a86
reorganised material on sublists
eberlm <eberlm@in.tum.de>
parents:
65954
diff
changeset
|
1530 |
|
639eb3617a86
reorganised material on sublists
eberlm <eberlm@in.tum.de>
parents:
65954
diff
changeset
|
1531 |
end |