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