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