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