src/HOL/Library/Sublist.thy
author nipkow
Thu May 26 09:05:00 2016 +0200 (2016-05-26)
changeset 63155 ea8540c71581
parent 63149 f5dbab18c404
child 63173 3413b1cf30cd
permissions -rw-r--r--
added function "prefixes" and some lemmas
Christian@49087
     1
(*  Title:      HOL/Library/Sublist.thy
wenzelm@10330
     2
    Author:     Tobias Nipkow and Markus Wenzel, TU Muenchen
Christian@49087
     3
    Author:     Christian Sternagel, JAIST
wenzelm@10330
     4
*)
wenzelm@10330
     5
wenzelm@60500
     6
section \<open>List prefixes, suffixes, and homeomorphic embedding\<close>
wenzelm@10330
     7
Christian@49087
     8
theory Sublist
Christian@49087
     9
imports Main
nipkow@15131
    10
begin
wenzelm@10330
    11
wenzelm@60500
    12
subsection \<open>Prefix order on lists\<close>
traytel@55579
    13
nipkow@63117
    14
definition prefix :: "'a list \<Rightarrow> 'a list \<Rightarrow> bool"
nipkow@63117
    15
  where "prefix xs ys \<longleftrightarrow> (\<exists>zs. ys = xs @ zs)"
traytel@55579
    16
nipkow@63117
    17
definition strict_prefix :: "'a list \<Rightarrow> 'a list \<Rightarrow> bool"
nipkow@63117
    18
  where "strict_prefix xs ys \<longleftrightarrow> prefix xs ys \<and> xs \<noteq> ys"
traytel@55579
    19
nipkow@63117
    20
interpretation prefix_order: order prefix strict_prefix
nipkow@63117
    21
  by standard (auto simp: prefix_def strict_prefix_def)
traytel@55579
    22
nipkow@63117
    23
interpretation prefix_bot: order_bot Nil prefix strict_prefix
nipkow@63117
    24
  by standard (simp add: prefix_def)
traytel@55579
    25
nipkow@63117
    26
lemma prefixI [intro?]: "ys = xs @ zs \<Longrightarrow> prefix xs ys"
nipkow@63117
    27
  unfolding prefix_def by blast
traytel@55579
    28
nipkow@63117
    29
lemma prefixE [elim?]:
nipkow@63117
    30
  assumes "prefix xs ys"
traytel@55579
    31
  obtains zs where "ys = xs @ zs"
nipkow@63117
    32
  using assms unfolding prefix_def by blast
traytel@55579
    33
nipkow@63117
    34
lemma strict_prefixI' [intro?]: "ys = xs @ z # zs \<Longrightarrow> strict_prefix xs ys"
nipkow@63117
    35
  unfolding strict_prefix_def prefix_def by blast
traytel@55579
    36
nipkow@63117
    37
lemma strict_prefixE' [elim?]:
nipkow@63117
    38
  assumes "strict_prefix xs ys"
traytel@55579
    39
  obtains z zs where "ys = xs @ z # zs"
traytel@55579
    40
proof -
nipkow@63117
    41
  from \<open>strict_prefix xs ys\<close> obtain us where "ys = xs @ us" and "xs \<noteq> ys"
nipkow@63117
    42
    unfolding strict_prefix_def prefix_def by blast
traytel@55579
    43
  with that show ?thesis by (auto simp add: neq_Nil_conv)
traytel@55579
    44
qed
traytel@55579
    45
nipkow@63155
    46
(* FIXME rm *)
nipkow@63117
    47
lemma strict_prefixI [intro?]: "prefix xs ys \<Longrightarrow> xs \<noteq> ys \<Longrightarrow> strict_prefix xs ys"
nipkow@63155
    48
by(fact prefix_order.le_neq_trans)
traytel@55579
    49
nipkow@63117
    50
lemma strict_prefixE [elim?]:
traytel@55579
    51
  fixes xs ys :: "'a list"
nipkow@63117
    52
  assumes "strict_prefix xs ys"
nipkow@63117
    53
  obtains "prefix xs ys" and "xs \<noteq> ys"
nipkow@63117
    54
  using assms unfolding strict_prefix_def by blast
traytel@55579
    55
traytel@55579
    56
wenzelm@60500
    57
subsection \<open>Basic properties of prefixes\<close>
traytel@55579
    58
nipkow@63155
    59
(* FIXME rm *)
nipkow@63117
    60
theorem Nil_prefix [iff]: "prefix [] xs"
nipkow@63155
    61
by(fact prefix_bot.bot_least)
traytel@55579
    62
nipkow@63155
    63
(* FIXME rm *)
nipkow@63117
    64
theorem prefix_Nil [simp]: "(prefix xs []) = (xs = [])"
nipkow@63155
    65
by(fact prefix_bot.bot_unique)
traytel@55579
    66
nipkow@63117
    67
lemma prefix_snoc [simp]: "prefix xs (ys @ [y]) \<longleftrightarrow> xs = ys @ [y] \<or> prefix xs ys"
traytel@55579
    68
proof
nipkow@63117
    69
  assume "prefix xs (ys @ [y])"
traytel@55579
    70
  then obtain zs where zs: "ys @ [y] = xs @ zs" ..
nipkow@63117
    71
  show "xs = ys @ [y] \<or> prefix xs ys"
nipkow@63117
    72
    by (metis append_Nil2 butlast_append butlast_snoc prefixI zs)
traytel@55579
    73
next
nipkow@63117
    74
  assume "xs = ys @ [y] \<or> prefix xs ys"
nipkow@63117
    75
  then show "prefix xs (ys @ [y])"
nipkow@63117
    76
    by (metis prefix_order.eq_iff prefix_order.order_trans prefixI)
traytel@55579
    77
qed
traytel@55579
    78
nipkow@63117
    79
lemma Cons_prefix_Cons [simp]: "prefix (x # xs) (y # ys) = (x = y \<and> prefix xs ys)"
nipkow@63117
    80
  by (auto simp add: prefix_def)
traytel@55579
    81
nipkow@63117
    82
lemma prefix_code [code]:
nipkow@63117
    83
  "prefix [] xs \<longleftrightarrow> True"
nipkow@63117
    84
  "prefix (x # xs) [] \<longleftrightarrow> False"
nipkow@63117
    85
  "prefix (x # xs) (y # ys) \<longleftrightarrow> x = y \<and> prefix xs ys"
traytel@55579
    86
  by simp_all
traytel@55579
    87
nipkow@63117
    88
lemma same_prefix_prefix [simp]: "prefix (xs @ ys) (xs @ zs) = prefix ys zs"
traytel@55579
    89
  by (induct xs) simp_all
traytel@55579
    90
nipkow@63117
    91
lemma same_prefix_nil [iff]: "prefix (xs @ ys) xs = (ys = [])"
nipkow@63117
    92
  by (metis append_Nil2 append_self_conv prefix_order.eq_iff prefixI)
traytel@55579
    93
nipkow@63117
    94
lemma prefix_prefix [simp]: "prefix xs ys \<Longrightarrow> prefix xs (ys @ zs)"
nipkow@63117
    95
  by (metis prefix_order.le_less_trans prefixI strict_prefixE strict_prefixI)
traytel@55579
    96
nipkow@63117
    97
lemma append_prefixD: "prefix (xs @ ys) zs \<Longrightarrow> prefix xs zs"
nipkow@63117
    98
  by (auto simp add: prefix_def)
traytel@55579
    99
nipkow@63117
   100
theorem prefix_Cons: "prefix xs (y # ys) = (xs = [] \<or> (\<exists>zs. xs = y # zs \<and> prefix zs ys))"
nipkow@63117
   101
  by (cases xs) (auto simp add: prefix_def)
traytel@55579
   102
nipkow@63117
   103
theorem prefix_append:
nipkow@63117
   104
  "prefix xs (ys @ zs) = (prefix xs ys \<or> (\<exists>us. xs = ys @ us \<and> prefix us zs))"
traytel@55579
   105
  apply (induct zs rule: rev_induct)
traytel@55579
   106
   apply force
traytel@55579
   107
  apply (simp del: append_assoc add: append_assoc [symmetric])
traytel@55579
   108
  apply (metis append_eq_appendI)
traytel@55579
   109
  done
traytel@55579
   110
nipkow@63117
   111
lemma append_one_prefix:
nipkow@63117
   112
  "prefix xs ys \<Longrightarrow> length xs < length ys \<Longrightarrow> prefix (xs @ [ys ! length xs]) ys"
nipkow@63117
   113
  proof (unfold prefix_def)
traytel@55579
   114
    assume a1: "\<exists>zs. ys = xs @ zs"
traytel@55579
   115
    then obtain sk :: "'a list" where sk: "ys = xs @ sk" by fastforce
traytel@55579
   116
    assume a2: "length xs < length ys"
wenzelm@61076
   117
    have f1: "\<And>v. ([]::'a list) @ v = v" using append_Nil2 by simp
traytel@55579
   118
    have "[] \<noteq> sk" using a1 a2 sk less_not_refl by force
traytel@55579
   119
    hence "\<exists>v. xs @ hd sk # v = ys" using sk by (metis hd_Cons_tl)
traytel@55579
   120
    thus "\<exists>zs. ys = (xs @ [ys ! length xs]) @ zs" using f1 by fastforce
traytel@55579
   121
  qed
traytel@55579
   122
nipkow@63117
   123
theorem prefix_length_le: "prefix xs ys \<Longrightarrow> length xs \<le> length ys"
nipkow@63117
   124
  by (auto simp add: prefix_def)
traytel@55579
   125
nipkow@63117
   126
lemma prefix_same_cases:
nipkow@63117
   127
  "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"
nipkow@63117
   128
  unfolding prefix_def by (force simp: append_eq_append_conv2)
traytel@55579
   129
nipkow@63117
   130
lemma set_mono_prefix: "prefix xs ys \<Longrightarrow> set xs \<subseteq> set ys"
nipkow@63117
   131
  by (auto simp add: prefix_def)
traytel@55579
   132
nipkow@63117
   133
lemma take_is_prefix: "prefix (take n xs) xs"
nipkow@63117
   134
  unfolding prefix_def by (metis append_take_drop_id)
traytel@55579
   135
nipkow@63155
   136
lemma prefixeq_butlast: "prefix (butlast xs) xs"
nipkow@63155
   137
by (simp add: butlast_conv_take take_is_prefix)
nipkow@63155
   138
nipkow@63117
   139
lemma map_prefixI: "prefix xs ys \<Longrightarrow> prefix (map f xs) (map f ys)"
nipkow@63117
   140
  by (auto simp: prefix_def)
traytel@55579
   141
nipkow@63117
   142
lemma prefix_length_less: "strict_prefix xs ys \<Longrightarrow> length xs < length ys"
nipkow@63117
   143
  by (auto simp: strict_prefix_def prefix_def)
traytel@55579
   144
nipkow@63155
   145
lemma prefix_snocD: "prefix (xs@[x]) ys \<Longrightarrow> strict_prefix xs ys"
nipkow@63155
   146
  by (simp add: strict_prefixI' prefix_order.dual_order.strict_trans1)
nipkow@63155
   147
nipkow@63117
   148
lemma strict_prefix_simps [simp, code]:
nipkow@63117
   149
  "strict_prefix xs [] \<longleftrightarrow> False"
nipkow@63117
   150
  "strict_prefix [] (x # xs) \<longleftrightarrow> True"
nipkow@63117
   151
  "strict_prefix (x # xs) (y # ys) \<longleftrightarrow> x = y \<and> strict_prefix xs ys"
nipkow@63117
   152
  by (simp_all add: strict_prefix_def cong: conj_cong)
traytel@55579
   153
nipkow@63117
   154
lemma take_strict_prefix: "strict_prefix xs ys \<Longrightarrow> strict_prefix (take n xs) ys"
traytel@55579
   155
  apply (induct n arbitrary: xs ys)
wenzelm@59997
   156
   apply (case_tac ys; simp)
nipkow@63117
   157
  apply (metis prefix_order.less_trans strict_prefixI take_is_prefix)
traytel@55579
   158
  done
traytel@55579
   159
nipkow@63117
   160
lemma not_prefix_cases:
nipkow@63117
   161
  assumes pfx: "\<not> prefix ps ls"
traytel@55579
   162
  obtains
traytel@55579
   163
    (c1) "ps \<noteq> []" and "ls = []"
nipkow@63117
   164
  | (c2) a as x xs where "ps = a#as" and "ls = x#xs" and "x = a" and "\<not> prefix as xs"
traytel@55579
   165
  | (c3) a as x xs where "ps = a#as" and "ls = x#xs" and "x \<noteq> a"
traytel@55579
   166
proof (cases ps)
traytel@55579
   167
  case Nil
traytel@55579
   168
  then show ?thesis using pfx by simp
traytel@55579
   169
next
traytel@55579
   170
  case (Cons a as)
wenzelm@60500
   171
  note c = \<open>ps = a#as\<close>
traytel@55579
   172
  show ?thesis
traytel@55579
   173
  proof (cases ls)
nipkow@63117
   174
    case Nil then show ?thesis by (metis append_Nil2 pfx c1 same_prefix_nil)
traytel@55579
   175
  next
traytel@55579
   176
    case (Cons x xs)
traytel@55579
   177
    show ?thesis
traytel@55579
   178
    proof (cases "x = a")
traytel@55579
   179
      case True
nipkow@63117
   180
      have "\<not> prefix as xs" using pfx c Cons True by simp
traytel@55579
   181
      with c Cons True show ?thesis by (rule c2)
traytel@55579
   182
    next
traytel@55579
   183
      case False
traytel@55579
   184
      with c Cons show ?thesis by (rule c3)
traytel@55579
   185
    qed
traytel@55579
   186
  qed
traytel@55579
   187
qed
traytel@55579
   188
nipkow@63117
   189
lemma not_prefix_induct [consumes 1, case_names Nil Neq Eq]:
nipkow@63117
   190
  assumes np: "\<not> prefix ps ls"
traytel@55579
   191
    and base: "\<And>x xs. P (x#xs) []"
traytel@55579
   192
    and r1: "\<And>x xs y ys. x \<noteq> y \<Longrightarrow> P (x#xs) (y#ys)"
nipkow@63117
   193
    and r2: "\<And>x xs y ys. \<lbrakk> x = y; \<not> prefix xs ys; P xs ys \<rbrakk> \<Longrightarrow> P (x#xs) (y#ys)"
traytel@55579
   194
  shows "P ps ls" using np
traytel@55579
   195
proof (induct ls arbitrary: ps)
traytel@55579
   196
  case Nil then show ?case
nipkow@63117
   197
    by (auto simp: neq_Nil_conv elim!: not_prefix_cases intro!: base)
traytel@55579
   198
next
traytel@55579
   199
  case (Cons y ys)
nipkow@63117
   200
  then have npfx: "\<not> prefix ps (y # ys)" by simp
traytel@55579
   201
  then obtain x xs where pv: "ps = x # xs"
nipkow@63117
   202
    by (rule not_prefix_cases) auto
nipkow@63117
   203
  show ?case by (metis Cons.hyps Cons_prefix_Cons npfx pv r1 r2)
traytel@55579
   204
qed
traytel@55579
   205
traytel@55579
   206
nipkow@63155
   207
subsection \<open>Prefixes\<close>
nipkow@63155
   208
nipkow@63155
   209
fun prefixes where
nipkow@63155
   210
"prefixes [] = [[]]" |
nipkow@63155
   211
"prefixes (x#xs) = [] # map (op # x) (prefixes xs)"
nipkow@63155
   212
nipkow@63155
   213
lemma in_set_prefixes[simp]: "xs \<in> set (prefixes ys) \<longleftrightarrow> prefix xs ys"
nipkow@63155
   214
by (induction "xs" arbitrary: "ys"; rename_tac "ys", case_tac "ys"; auto)
nipkow@63155
   215
nipkow@63155
   216
lemma length_prefixes[simp]: "length (prefixes xs) = length xs+1"
nipkow@63155
   217
by (induction xs) auto
nipkow@63155
   218
nipkow@63155
   219
lemma prefixes_snoc[simp]:
nipkow@63155
   220
  "prefixes (xs@[x]) = prefixes xs @ [xs@[x]]"
nipkow@63155
   221
by (induction xs) auto
nipkow@63155
   222
nipkow@63155
   223
lemma prefixes_eq_Snoc:
nipkow@63155
   224
  "prefixes ys = xs @ [x] \<longleftrightarrow>
nipkow@63155
   225
  (ys = [] \<and> xs = [] \<or> (\<exists>z zs. ys = zs@[z] \<and> xs = prefixes zs)) \<and> x = ys"
nipkow@63155
   226
by (cases ys rule: rev_cases) auto
nipkow@63155
   227
nipkow@63155
   228
wenzelm@60500
   229
subsection \<open>Parallel lists\<close>
wenzelm@10389
   230
Christian@50516
   231
definition parallel :: "'a list \<Rightarrow> 'a list \<Rightarrow> bool"  (infixl "\<parallel>" 50)
nipkow@63117
   232
  where "(xs \<parallel> ys) = (\<not> prefix xs ys \<and> \<not> prefix ys xs)"
wenzelm@10389
   233
nipkow@63117
   234
lemma parallelI [intro]: "\<not> prefix xs ys \<Longrightarrow> \<not> prefix ys xs \<Longrightarrow> xs \<parallel> ys"
wenzelm@25692
   235
  unfolding parallel_def by blast
wenzelm@10330
   236
wenzelm@10389
   237
lemma parallelE [elim]:
wenzelm@25692
   238
  assumes "xs \<parallel> ys"
nipkow@63117
   239
  obtains "\<not> prefix xs ys \<and> \<not> prefix ys xs"
wenzelm@25692
   240
  using assms unfolding parallel_def by blast
wenzelm@10330
   241
nipkow@63117
   242
theorem prefix_cases:
nipkow@63117
   243
  obtains "prefix xs ys" | "strict_prefix ys xs" | "xs \<parallel> ys"
nipkow@63117
   244
  unfolding parallel_def strict_prefix_def by blast
wenzelm@10330
   245
wenzelm@10389
   246
theorem parallel_decomp:
Christian@50516
   247
  "xs \<parallel> ys \<Longrightarrow> \<exists>as b bs c cs. b \<noteq> c \<and> xs = as @ b # bs \<and> ys = as @ c # cs"
wenzelm@10408
   248
proof (induct xs rule: rev_induct)
wenzelm@11987
   249
  case Nil
wenzelm@23254
   250
  then have False by auto
wenzelm@23254
   251
  then show ?case ..
wenzelm@10408
   252
next
wenzelm@11987
   253
  case (snoc x xs)
wenzelm@11987
   254
  show ?case
nipkow@63117
   255
  proof (rule prefix_cases)
nipkow@63117
   256
    assume le: "prefix xs ys"
wenzelm@10408
   257
    then obtain ys' where ys: "ys = xs @ ys'" ..
wenzelm@10408
   258
    show ?thesis
wenzelm@10408
   259
    proof (cases ys')
nipkow@25564
   260
      assume "ys' = []"
nipkow@63117
   261
      then show ?thesis by (metis append_Nil2 parallelE prefixI snoc.prems ys)
wenzelm@10389
   262
    next
wenzelm@10408
   263
      fix c cs assume ys': "ys' = c # cs"
blanchet@54483
   264
      have "x \<noteq> c" using snoc.prems ys ys' by fastforce
blanchet@54483
   265
      thus "\<exists>as b bs c cs. b \<noteq> c \<and> xs @ [x] = as @ b # bs \<and> ys = as @ c # cs"
blanchet@54483
   266
        using ys ys' by blast
wenzelm@10389
   267
    qed
wenzelm@10408
   268
  next
nipkow@63117
   269
    assume "strict_prefix ys xs"
nipkow@63117
   270
    then have "prefix ys (xs @ [x])" by (simp add: strict_prefix_def)
wenzelm@11987
   271
    with snoc have False by blast
wenzelm@23254
   272
    then show ?thesis ..
wenzelm@10408
   273
  next
wenzelm@10408
   274
    assume "xs \<parallel> ys"
wenzelm@11987
   275
    with snoc obtain as b bs c cs where neq: "(b::'a) \<noteq> c"
wenzelm@10408
   276
      and xs: "xs = as @ b # bs" and ys: "ys = as @ c # cs"
wenzelm@10408
   277
      by blast
wenzelm@10408
   278
    from xs have "xs @ [x] = as @ b # (bs @ [x])" by simp
wenzelm@10408
   279
    with neq ys show ?thesis by blast
wenzelm@10389
   280
  qed
wenzelm@10389
   281
qed
wenzelm@10330
   282
nipkow@25564
   283
lemma parallel_append: "a \<parallel> b \<Longrightarrow> a @ c \<parallel> b @ d"
wenzelm@25692
   284
  apply (rule parallelI)
wenzelm@25692
   285
    apply (erule parallelE, erule conjE,
nipkow@63117
   286
      induct rule: not_prefix_induct, simp+)+
wenzelm@25692
   287
  done
kleing@25299
   288
wenzelm@25692
   289
lemma parallel_appendI: "xs \<parallel> ys \<Longrightarrow> x = xs @ xs' \<Longrightarrow> y = ys @ ys' \<Longrightarrow> x \<parallel> y"
wenzelm@25692
   290
  by (simp add: parallel_append)
kleing@25299
   291
wenzelm@25692
   292
lemma parallel_commute: "a \<parallel> b \<longleftrightarrow> b \<parallel> a"
wenzelm@25692
   293
  unfolding parallel_def by auto
oheimb@14538
   294
wenzelm@25356
   295
wenzelm@60500
   296
subsection \<open>Suffix order on lists\<close>
wenzelm@17201
   297
nipkow@63149
   298
definition suffix :: "'a list \<Rightarrow> 'a list \<Rightarrow> bool"
nipkow@63149
   299
  where "suffix xs ys = (\<exists>zs. ys = zs @ xs)"
Christian@49087
   300
nipkow@63149
   301
definition strict_suffix :: "'a list \<Rightarrow> 'a list \<Rightarrow> bool"
nipkow@63149
   302
  where "strict_suffix xs ys \<longleftrightarrow> (\<exists>us. ys = us @ xs \<and> us \<noteq> [])"
oheimb@14538
   303
nipkow@63149
   304
lemma strict_suffix_imp_suffix:
nipkow@63149
   305
  "strict_suffix xs ys \<Longrightarrow> suffix xs ys"
nipkow@63149
   306
  by (auto simp: suffix_def strict_suffix_def)
Christian@49087
   307
nipkow@63149
   308
lemma suffixI [intro?]: "ys = zs @ xs \<Longrightarrow> suffix xs ys"
nipkow@63149
   309
  unfolding suffix_def by blast
wenzelm@21305
   310
nipkow@63149
   311
lemma suffixE [elim?]:
nipkow@63149
   312
  assumes "suffix xs ys"
Christian@49087
   313
  obtains zs where "ys = zs @ xs"
nipkow@63149
   314
  using assms unfolding suffix_def by blast
wenzelm@21305
   315
nipkow@63149
   316
lemma suffix_refl [iff]: "suffix xs xs"
nipkow@63149
   317
  by (auto simp add: suffix_def)
nipkow@63149
   318
Christian@49087
   319
lemma suffix_trans:
Christian@49087
   320
  "suffix xs ys \<Longrightarrow> suffix ys zs \<Longrightarrow> suffix xs zs"
Christian@49087
   321
  by (auto simp: suffix_def)
nipkow@63149
   322
nipkow@63149
   323
lemma strict_suffix_trans:
nipkow@63149
   324
  "\<lbrakk>strict_suffix xs ys; strict_suffix ys zs\<rbrakk> \<Longrightarrow> strict_suffix xs zs"
nipkow@63149
   325
by (auto simp add: strict_suffix_def)
Christian@49087
   326
nipkow@63149
   327
lemma suffix_antisym: "\<lbrakk>suffix xs ys; suffix ys xs\<rbrakk> \<Longrightarrow> xs = ys"
nipkow@63149
   328
  by (auto simp add: suffix_def)
oheimb@14538
   329
nipkow@63149
   330
lemma suffix_tl [simp]: "suffix (tl xs) xs"
Christian@49087
   331
  by (induct xs) (auto simp: suffix_def)
oheimb@14538
   332
nipkow@63149
   333
lemma strict_suffix_tl [simp]: "xs \<noteq> [] \<Longrightarrow> strict_suffix (tl xs) xs"
nipkow@63149
   334
  by (induct xs) (auto simp: strict_suffix_def)
nipkow@63149
   335
nipkow@63149
   336
lemma Nil_suffix [iff]: "suffix [] xs"
nipkow@63149
   337
  by (simp add: suffix_def)
Christian@49087
   338
nipkow@63149
   339
lemma suffix_Nil [simp]: "(suffix xs []) = (xs = [])"
nipkow@63149
   340
  by (auto simp add: suffix_def)
nipkow@63149
   341
nipkow@63149
   342
lemma suffix_ConsI: "suffix xs ys \<Longrightarrow> suffix xs (y # ys)"
nipkow@63149
   343
  by (auto simp add: suffix_def)
nipkow@63149
   344
nipkow@63149
   345
lemma suffix_ConsD: "suffix (x # xs) ys \<Longrightarrow> suffix xs ys"
nipkow@63149
   346
  by (auto simp add: suffix_def)
oheimb@14538
   347
nipkow@63149
   348
lemma suffix_appendI: "suffix xs ys \<Longrightarrow> suffix xs (zs @ ys)"
nipkow@63149
   349
  by (auto simp add: suffix_def)
nipkow@63149
   350
nipkow@63149
   351
lemma suffix_appendD: "suffix (zs @ xs) ys \<Longrightarrow> suffix xs ys"
nipkow@63149
   352
  by (auto simp add: suffix_def)
Christian@49087
   353
nipkow@63149
   354
lemma strict_suffix_set_subset: "strict_suffix xs ys \<Longrightarrow> set xs \<subseteq> set ys"
nipkow@63149
   355
by (auto simp: strict_suffix_def)
oheimb@14538
   356
nipkow@63149
   357
lemma suffix_set_subset: "suffix xs ys \<Longrightarrow> set xs \<subseteq> set ys"
nipkow@63149
   358
by (auto simp: suffix_def)
Christian@49087
   359
nipkow@63149
   360
lemma suffix_ConsD2: "suffix (x # xs) (y # ys) \<Longrightarrow> suffix xs ys"
wenzelm@21305
   361
proof -
nipkow@63149
   362
  assume "suffix (x # xs) (y # ys)"
wenzelm@49107
   363
  then obtain zs where "y # ys = zs @ x # xs" ..
Christian@49087
   364
  then show ?thesis
nipkow@63149
   365
    by (induct zs) (auto intro!: suffix_appendI suffix_ConsI)
wenzelm@21305
   366
qed
oheimb@14538
   367
nipkow@63149
   368
lemma suffix_to_prefix [code]: "suffix xs ys \<longleftrightarrow> prefix (rev xs) (rev ys)"
Christian@49087
   369
proof
nipkow@63149
   370
  assume "suffix xs ys"
Christian@49087
   371
  then obtain zs where "ys = zs @ xs" ..
Christian@49087
   372
  then have "rev ys = rev xs @ rev zs" by simp
nipkow@63117
   373
  then show "prefix (rev xs) (rev ys)" ..
Christian@49087
   374
next
nipkow@63117
   375
  assume "prefix (rev xs) (rev ys)"
Christian@49087
   376
  then obtain zs where "rev ys = rev xs @ zs" ..
Christian@49087
   377
  then have "rev (rev ys) = rev zs @ rev (rev xs)" by simp
Christian@49087
   378
  then have "ys = rev zs @ xs" by simp
nipkow@63149
   379
  then show "suffix xs ys" ..
wenzelm@21305
   380
qed
oheimb@14538
   381
nipkow@63149
   382
lemma distinct_suffix: "distinct ys \<Longrightarrow> suffix xs ys \<Longrightarrow> distinct xs"
nipkow@63149
   383
  by (clarsimp elim!: suffixE)
wenzelm@17201
   384
nipkow@63149
   385
lemma suffix_map: "suffix xs ys \<Longrightarrow> suffix (map f xs) (map f ys)"
nipkow@63149
   386
  by (auto elim!: suffixE intro: suffixI)
kleing@25299
   387
nipkow@63149
   388
lemma suffix_drop: "suffix (drop n as) as"
nipkow@63149
   389
  unfolding suffix_def
wenzelm@25692
   390
  apply (rule exI [where x = "take n as"])
wenzelm@25692
   391
  apply simp
wenzelm@25692
   392
  done
kleing@25299
   393
nipkow@63149
   394
lemma suffix_take: "suffix xs ys \<Longrightarrow> ys = take (length ys - length xs) ys @ xs"
nipkow@63149
   395
  by (auto elim!: suffixE)
kleing@25299
   396
nipkow@63149
   397
lemma strict_suffix_reflclp_conv: "strict_suffix\<^sup>=\<^sup>= = suffix"
nipkow@63149
   398
by (intro ext) (auto simp: suffix_def strict_suffix_def)
nipkow@63149
   399
nipkow@63149
   400
lemma suffix_lists: "suffix xs ys \<Longrightarrow> ys \<in> lists A \<Longrightarrow> xs \<in> lists A"
nipkow@63149
   401
  unfolding suffix_def by auto
Christian@49087
   402
nipkow@63117
   403
lemma parallelD1: "x \<parallel> y \<Longrightarrow> \<not> prefix x y"
wenzelm@25692
   404
  by blast
kleing@25299
   405
nipkow@63117
   406
lemma parallelD2: "x \<parallel> y \<Longrightarrow> \<not> prefix y x"
wenzelm@25692
   407
  by blast
wenzelm@25355
   408
wenzelm@25355
   409
lemma parallel_Nil1 [simp]: "\<not> x \<parallel> []"
wenzelm@25692
   410
  unfolding parallel_def by simp
wenzelm@25355
   411
kleing@25299
   412
lemma parallel_Nil2 [simp]: "\<not> [] \<parallel> x"
wenzelm@25692
   413
  unfolding parallel_def by simp
kleing@25299
   414
nipkow@25564
   415
lemma Cons_parallelI1: "a \<noteq> b \<Longrightarrow> a # as \<parallel> b # bs"
wenzelm@25692
   416
  by auto
kleing@25299
   417
nipkow@25564
   418
lemma Cons_parallelI2: "\<lbrakk> a = b; as \<parallel> bs \<rbrakk> \<Longrightarrow> a # as \<parallel> b # bs"
nipkow@63117
   419
  by (metis Cons_prefix_Cons parallelE parallelI)
nipkow@25665
   420
kleing@25299
   421
lemma not_equal_is_parallel:
kleing@25299
   422
  assumes neq: "xs \<noteq> ys"
wenzelm@25356
   423
    and len: "length xs = length ys"
wenzelm@25356
   424
  shows "xs \<parallel> ys"
kleing@25299
   425
  using len neq
wenzelm@25355
   426
proof (induct rule: list_induct2)
haftmann@26445
   427
  case Nil
wenzelm@25356
   428
  then show ?case by simp
kleing@25299
   429
next
haftmann@26445
   430
  case (Cons a as b bs)
wenzelm@25355
   431
  have ih: "as \<noteq> bs \<Longrightarrow> as \<parallel> bs" by fact
kleing@25299
   432
  show ?case
kleing@25299
   433
  proof (cases "a = b")
wenzelm@25355
   434
    case True
haftmann@26445
   435
    then have "as \<noteq> bs" using Cons by simp
wenzelm@25355
   436
    then show ?thesis by (rule Cons_parallelI2 [OF True ih])
kleing@25299
   437
  next
kleing@25299
   438
    case False
wenzelm@25355
   439
    then show ?thesis by (rule Cons_parallelI1)
kleing@25299
   440
  qed
kleing@25299
   441
qed
haftmann@22178
   442
Christian@49087
   443
wenzelm@60500
   444
subsection \<open>Homeomorphic embedding on lists\<close>
Christian@49087
   445
Christian@57497
   446
inductive list_emb :: "('a \<Rightarrow> 'a \<Rightarrow> bool) \<Rightarrow> 'a list \<Rightarrow> 'a list \<Rightarrow> bool"
Christian@49087
   447
  for P :: "('a \<Rightarrow> 'a \<Rightarrow> bool)"
Christian@49087
   448
where
Christian@57497
   449
  list_emb_Nil [intro, simp]: "list_emb P [] ys"
Christian@57497
   450
| list_emb_Cons [intro] : "list_emb P xs ys \<Longrightarrow> list_emb P xs (y#ys)"
Christian@57498
   451
| list_emb_Cons2 [intro]: "P x y \<Longrightarrow> list_emb P xs ys \<Longrightarrow> list_emb P (x#xs) (y#ys)"
Christian@50516
   452
Christian@57499
   453
lemma list_emb_mono:                         
Christian@57499
   454
  assumes "\<And>x y. P x y \<longrightarrow> Q x y"
Christian@57499
   455
  shows "list_emb P xs ys \<longrightarrow> list_emb Q xs ys"
Christian@57499
   456
proof                                        
Christian@57499
   457
  assume "list_emb P xs ys"                    
Christian@57499
   458
  then show "list_emb Q xs ys" by (induct) (auto simp: assms)
Christian@57499
   459
qed 
Christian@57499
   460
Christian@57497
   461
lemma list_emb_Nil2 [simp]:
Christian@57497
   462
  assumes "list_emb P xs []" shows "xs = []"
Christian@57497
   463
  using assms by (cases rule: list_emb.cases) auto
Christian@49087
   464
Christian@57498
   465
lemma list_emb_refl:
Christian@57498
   466
  assumes "\<And>x. x \<in> set xs \<Longrightarrow> P x x"
Christian@57498
   467
  shows "list_emb P xs xs"
Christian@57498
   468
  using assms by (induct xs) auto
Christian@49087
   469
Christian@57497
   470
lemma list_emb_Cons_Nil [simp]: "list_emb P (x#xs) [] = False"
Christian@49087
   471
proof -
Christian@57497
   472
  { assume "list_emb P (x#xs) []"
Christian@57497
   473
    from list_emb_Nil2 [OF this] have False by simp
Christian@49087
   474
  } moreover {
Christian@49087
   475
    assume False
Christian@57497
   476
    then have "list_emb P (x#xs) []" by simp
Christian@49087
   477
  } ultimately show ?thesis by blast
Christian@49087
   478
qed
Christian@49087
   479
Christian@57497
   480
lemma list_emb_append2 [intro]: "list_emb P xs ys \<Longrightarrow> list_emb P xs (zs @ ys)"
Christian@49087
   481
  by (induct zs) auto
Christian@49087
   482
Christian@57497
   483
lemma list_emb_prefix [intro]:
Christian@57497
   484
  assumes "list_emb P xs ys" shows "list_emb P xs (ys @ zs)"
Christian@49087
   485
  using assms
Christian@49087
   486
  by (induct arbitrary: zs) auto
Christian@49087
   487
Christian@57497
   488
lemma list_emb_ConsD:
Christian@57497
   489
  assumes "list_emb P (x#xs) ys"
Christian@57498
   490
  shows "\<exists>us v vs. ys = us @ v # vs \<and> P x v \<and> list_emb P xs vs"
Christian@49087
   491
using assms
wenzelm@49107
   492
proof (induct x \<equiv> "x # xs" ys arbitrary: x xs)
Christian@57497
   493
  case list_emb_Cons
wenzelm@49107
   494
  then show ?case by (metis append_Cons)
Christian@49087
   495
next
Christian@57497
   496
  case (list_emb_Cons2 x y xs ys)
blanchet@54483
   497
  then show ?case by blast
Christian@49087
   498
qed
Christian@49087
   499
Christian@57497
   500
lemma list_emb_appendD:
Christian@57497
   501
  assumes "list_emb P (xs @ ys) zs"
Christian@57497
   502
  shows "\<exists>us vs. zs = us @ vs \<and> list_emb P xs us \<and> list_emb P ys vs"
Christian@49087
   503
using assms
Christian@49087
   504
proof (induction xs arbitrary: ys zs)
wenzelm@49107
   505
  case Nil then show ?case by auto
Christian@49087
   506
next
Christian@49087
   507
  case (Cons x xs)
blanchet@54483
   508
  then obtain us v vs where
Christian@57498
   509
    zs: "zs = us @ v # vs" and p: "P x v" and lh: "list_emb P (xs @ ys) vs"
Christian@57497
   510
    by (auto dest: list_emb_ConsD)
blanchet@54483
   511
  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
Christian@57497
   512
    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)"
blanchet@54483
   513
    using Cons(1) by (metis (no_types))
Christian@57497
   514
  hence "\<forall>x\<^sub>2. list_emb P (x # xs) (x\<^sub>2 @ v # sk\<^sub>0 ys vs)" using p lh by auto
blanchet@54483
   515
  thus ?case using lh zs sk by (metis (no_types) append_Cons append_assoc)
Christian@49087
   516
qed
Christian@49087
   517
nipkow@63149
   518
lemma list_emb_strict_suffix:
nipkow@63149
   519
  assumes "list_emb P xs ys" and "strict_suffix ys zs"
nipkow@63149
   520
  shows "list_emb P xs zs"
nipkow@63149
   521
  using assms(2) and list_emb_append2 [OF assms(1)] by (auto simp: strict_suffix_def)
nipkow@63149
   522
Christian@57497
   523
lemma list_emb_suffix:
Christian@57497
   524
  assumes "list_emb P xs ys" and "suffix ys zs"
Christian@57497
   525
  shows "list_emb P xs zs"
nipkow@63149
   526
using assms and list_emb_strict_suffix
nipkow@63149
   527
unfolding strict_suffix_reflclp_conv[symmetric] by auto
Christian@49087
   528
Christian@57497
   529
lemma list_emb_length: "list_emb P xs ys \<Longrightarrow> length xs \<le> length ys"
Christian@57497
   530
  by (induct rule: list_emb.induct) auto
Christian@49087
   531
Christian@57497
   532
lemma list_emb_trans:
Christian@57500
   533
  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"
Christian@57500
   534
  shows "\<lbrakk>list_emb P xs ys; list_emb P ys zs\<rbrakk> \<Longrightarrow> list_emb P xs zs"
Christian@50516
   535
proof -
Christian@57497
   536
  assume "list_emb P xs ys" and "list_emb P ys zs"
Christian@57500
   537
  then show "list_emb P xs zs" using assms
Christian@49087
   538
  proof (induction arbitrary: zs)
Christian@57497
   539
    case list_emb_Nil show ?case by blast
Christian@49087
   540
  next
Christian@57497
   541
    case (list_emb_Cons xs ys y)
wenzelm@60500
   542
    from list_emb_ConsD [OF \<open>list_emb P (y#ys) zs\<close>] obtain us v vs
Christian@57500
   543
      where zs: "zs = us @ v # vs" and "P\<^sup>=\<^sup>= y v" and "list_emb P ys vs" by blast
Christian@57497
   544
    then have "list_emb P ys (v#vs)" by blast
Christian@57497
   545
    then have "list_emb P ys zs" unfolding zs by (rule list_emb_append2)
Christian@57500
   546
    from list_emb_Cons.IH [OF this] and list_emb_Cons.prems show ?case by auto
Christian@49087
   547
  next
Christian@57497
   548
    case (list_emb_Cons2 x y xs ys)
wenzelm@60500
   549
    from list_emb_ConsD [OF \<open>list_emb P (y#ys) zs\<close>] obtain us v vs
Christian@57498
   550
      where zs: "zs = us @ v # vs" and "P y v" and "list_emb P ys vs" by blast
Christian@57500
   551
    with list_emb_Cons2 have "list_emb P xs vs" by auto
Christian@57498
   552
    moreover have "P x v"
Christian@49087
   553
    proof -
Christian@57500
   554
      from zs have "v \<in> set zs" by auto
Christian@57500
   555
      moreover have "x \<in> set (x#xs)" and "y \<in> set (y#ys)" by simp_all
Christian@50516
   556
      ultimately show ?thesis
wenzelm@60500
   557
        using \<open>P x y\<close> and \<open>P y v\<close> and list_emb_Cons2
Christian@50516
   558
        by blast
Christian@49087
   559
    qed
Christian@57497
   560
    ultimately have "list_emb P (x#xs) (v#vs)" by blast
Christian@57497
   561
    then show ?case unfolding zs by (rule list_emb_append2)
Christian@49087
   562
  qed
Christian@49087
   563
qed
Christian@49087
   564
Christian@57500
   565
lemma list_emb_set:
Christian@57500
   566
  assumes "list_emb P xs ys" and "x \<in> set xs"
Christian@57500
   567
  obtains y where "y \<in> set ys" and "P x y"
Christian@57500
   568
  using assms by (induct) auto
Christian@57500
   569
Christian@49087
   570
wenzelm@60500
   571
subsection \<open>Sublists (special case of homeomorphic embedding)\<close>
Christian@49087
   572
Christian@50516
   573
abbreviation sublisteq :: "'a list \<Rightarrow> 'a list \<Rightarrow> bool"
Christian@57497
   574
  where "sublisteq xs ys \<equiv> list_emb (op =) xs ys"
Christian@49087
   575
Christian@50516
   576
lemma sublisteq_Cons2: "sublisteq xs ys \<Longrightarrow> sublisteq (x#xs) (x#ys)" by auto
Christian@49087
   577
Christian@50516
   578
lemma sublisteq_same_length:
Christian@50516
   579
  assumes "sublisteq xs ys" and "length xs = length ys" shows "xs = ys"
Christian@57497
   580
  using assms by (induct) (auto dest: list_emb_length)
Christian@49087
   581
Christian@50516
   582
lemma not_sublisteq_length [simp]: "length ys < length xs \<Longrightarrow> \<not> sublisteq xs ys"
Christian@57497
   583
  by (metis list_emb_length linorder_not_less)
Christian@49087
   584
Christian@49087
   585
lemma [code]:
Christian@57497
   586
  "list_emb P [] ys \<longleftrightarrow> True"
Christian@57497
   587
  "list_emb P (x#xs) [] \<longleftrightarrow> False"
Christian@49087
   588
  by (simp_all)
Christian@49087
   589
Christian@50516
   590
lemma sublisteq_Cons': "sublisteq (x#xs) ys \<Longrightarrow> sublisteq xs ys"
Christian@57497
   591
  by (induct xs, simp, blast dest: list_emb_ConsD)
Christian@49087
   592
Christian@50516
   593
lemma sublisteq_Cons2':
Christian@50516
   594
  assumes "sublisteq (x#xs) (x#ys)" shows "sublisteq xs ys"
Christian@50516
   595
  using assms by (cases) (rule sublisteq_Cons')
Christian@49087
   596
Christian@50516
   597
lemma sublisteq_Cons2_neq:
Christian@50516
   598
  assumes "sublisteq (x#xs) (y#ys)"
Christian@50516
   599
  shows "x \<noteq> y \<Longrightarrow> sublisteq (x#xs) ys"
Christian@49087
   600
  using assms by (cases) auto
Christian@49087
   601
Christian@50516
   602
lemma sublisteq_Cons2_iff [simp, code]:
Christian@50516
   603
  "sublisteq (x#xs) (y#ys) = (if x = y then sublisteq xs ys else sublisteq (x#xs) ys)"
Christian@57497
   604
  by (metis list_emb_Cons sublisteq_Cons2 sublisteq_Cons2' sublisteq_Cons2_neq)
Christian@49087
   605
Christian@50516
   606
lemma sublisteq_append': "sublisteq (zs @ xs) (zs @ ys) \<longleftrightarrow> sublisteq xs ys"
Christian@49087
   607
  by (induct zs) simp_all
Christian@49087
   608
Christian@50516
   609
lemma sublisteq_refl [simp, intro!]: "sublisteq xs xs" by (induct xs) simp_all
Christian@49087
   610
Christian@50516
   611
lemma sublisteq_antisym:
Christian@50516
   612
  assumes "sublisteq xs ys" and "sublisteq ys xs"
Christian@49087
   613
  shows "xs = ys"
Christian@49087
   614
using assms
Christian@49087
   615
proof (induct)
Christian@57497
   616
  case list_emb_Nil
Christian@57497
   617
  from list_emb_Nil2 [OF this] show ?case by simp
Christian@49087
   618
next
Christian@57497
   619
  case list_emb_Cons2
blanchet@54483
   620
  thus ?case by simp
Christian@49087
   621
next
Christian@57497
   622
  case list_emb_Cons
blanchet@54483
   623
  hence False using sublisteq_Cons' by fastforce
blanchet@54483
   624
  thus ?case ..
Christian@49087
   625
qed
Christian@49087
   626
Christian@50516
   627
lemma sublisteq_trans: "sublisteq xs ys \<Longrightarrow> sublisteq ys zs \<Longrightarrow> sublisteq xs zs"
Christian@57500
   628
  by (rule list_emb_trans [of _ _ _ "op ="]) auto
Christian@49087
   629
Christian@50516
   630
lemma sublisteq_append_le_same_iff: "sublisteq (xs @ ys) ys \<longleftrightarrow> xs = []"
Christian@57497
   631
  by (auto dest: list_emb_length)
Christian@49087
   632
Christian@57497
   633
lemma list_emb_append_mono:
Christian@57497
   634
  "\<lbrakk> list_emb P xs xs'; list_emb P ys ys' \<rbrakk> \<Longrightarrow> list_emb P (xs@ys) (xs'@ys')"
Christian@57497
   635
  apply (induct rule: list_emb.induct)
Christian@57497
   636
    apply (metis eq_Nil_appendI list_emb_append2)
Christian@57497
   637
   apply (metis append_Cons list_emb_Cons)
Christian@57497
   638
  apply (metis append_Cons list_emb_Cons2)
wenzelm@49107
   639
  done
Christian@49087
   640
Christian@49087
   641
wenzelm@60500
   642
subsection \<open>Appending elements\<close>
Christian@49087
   643
Christian@50516
   644
lemma sublisteq_append [simp]:
Christian@50516
   645
  "sublisteq (xs @ zs) (ys @ zs) \<longleftrightarrow> sublisteq xs ys" (is "?l = ?r")
Christian@49087
   646
proof
Christian@50516
   647
  { fix xs' ys' xs ys zs :: "'a list" assume "sublisteq xs' ys'"
Christian@50516
   648
    then have "xs' = xs @ zs & ys' = ys @ zs \<longrightarrow> sublisteq xs ys"
Christian@49087
   649
    proof (induct arbitrary: xs ys zs)
Christian@57497
   650
      case list_emb_Nil show ?case by simp
Christian@49087
   651
    next
Christian@57497
   652
      case (list_emb_Cons xs' ys' x)
Christian@57497
   653
      { assume "ys=[]" then have ?case using list_emb_Cons(1) by auto }
Christian@49087
   654
      moreover
Christian@49087
   655
      { fix us assume "ys = x#us"
Christian@57497
   656
        then have ?case using list_emb_Cons(2) by(simp add: list_emb.list_emb_Cons) }
Christian@49087
   657
      ultimately show ?case by (auto simp:Cons_eq_append_conv)
Christian@49087
   658
    next
Christian@57497
   659
      case (list_emb_Cons2 x y xs' ys')
Christian@57497
   660
      { assume "xs=[]" then have ?case using list_emb_Cons2(1) by auto }
Christian@49087
   661
      moreover
Christian@57497
   662
      { fix us vs assume "xs=x#us" "ys=x#vs" then have ?case using list_emb_Cons2 by auto}
Christian@49087
   663
      moreover
Christian@57497
   664
      { fix us assume "xs=x#us" "ys=[]" then have ?case using list_emb_Cons2(2) by bestsimp }
wenzelm@60500
   665
      ultimately show ?case using \<open>op = x y\<close> by (auto simp: Cons_eq_append_conv)
Christian@49087
   666
    qed }
Christian@49087
   667
  moreover assume ?l
Christian@49087
   668
  ultimately show ?r by blast
Christian@49087
   669
next
Christian@57497
   670
  assume ?r then show ?l by (metis list_emb_append_mono sublisteq_refl)
Christian@49087
   671
qed
Christian@49087
   672
Christian@50516
   673
lemma sublisteq_drop_many: "sublisteq xs ys \<Longrightarrow> sublisteq xs (zs @ ys)"
Christian@49087
   674
  by (induct zs) auto
Christian@49087
   675
Christian@50516
   676
lemma sublisteq_rev_drop_many: "sublisteq xs ys \<Longrightarrow> sublisteq xs (ys @ zs)"
Christian@57497
   677
  by (metis append_Nil2 list_emb_Nil list_emb_append_mono)
Christian@49087
   678
Christian@49087
   679
wenzelm@60500
   680
subsection \<open>Relation to standard list operations\<close>
Christian@49087
   681
Christian@50516
   682
lemma sublisteq_map:
Christian@50516
   683
  assumes "sublisteq xs ys" shows "sublisteq (map f xs) (map f ys)"
Christian@49087
   684
  using assms by (induct) auto
Christian@49087
   685
Christian@50516
   686
lemma sublisteq_filter_left [simp]: "sublisteq (filter P xs) xs"
Christian@49087
   687
  by (induct xs) auto
Christian@49087
   688
Christian@50516
   689
lemma sublisteq_filter [simp]:
Christian@50516
   690
  assumes "sublisteq xs ys" shows "sublisteq (filter P xs) (filter P ys)"
blanchet@54483
   691
  using assms by induct auto
Christian@49087
   692
Christian@50516
   693
lemma "sublisteq xs ys \<longleftrightarrow> (\<exists>N. xs = sublist ys N)" (is "?L = ?R")
Christian@49087
   694
proof
Christian@49087
   695
  assume ?L
wenzelm@49107
   696
  then show ?R
Christian@49087
   697
  proof (induct)
Christian@57497
   698
    case list_emb_Nil show ?case by (metis sublist_empty)
Christian@49087
   699
  next
Christian@57497
   700
    case (list_emb_Cons xs ys x)
Christian@49087
   701
    then obtain N where "xs = sublist ys N" by blast
wenzelm@49107
   702
    then have "xs = sublist (x#ys) (Suc ` N)"
Christian@49087
   703
      by (clarsimp simp add:sublist_Cons inj_image_mem_iff)
wenzelm@49107
   704
    then show ?case by blast
Christian@49087
   705
  next
Christian@57497
   706
    case (list_emb_Cons2 x y xs ys)
Christian@49087
   707
    then obtain N where "xs = sublist ys N" by blast
wenzelm@49107
   708
    then have "x#xs = sublist (x#ys) (insert 0 (Suc ` N))"
Christian@49087
   709
      by (clarsimp simp add:sublist_Cons inj_image_mem_iff)
Christian@57497
   710
    moreover from list_emb_Cons2 have "x = y" by simp
Christian@50516
   711
    ultimately show ?case by blast
Christian@49087
   712
  qed
Christian@49087
   713
next
Christian@49087
   714
  assume ?R
Christian@49087
   715
  then obtain N where "xs = sublist ys N" ..
Christian@50516
   716
  moreover have "sublisteq (sublist ys N) ys"
wenzelm@49107
   717
  proof (induct ys arbitrary: N)
Christian@49087
   718
    case Nil show ?case by simp
Christian@49087
   719
  next
wenzelm@49107
   720
    case Cons then show ?case by (auto simp: sublist_Cons)
Christian@49087
   721
  qed
Christian@49087
   722
  ultimately show ?L by simp
Christian@49087
   723
qed
Christian@49087
   724
wenzelm@10330
   725
end