src/HOL/Library/List_Prefix.thy
author kleing
Mon Jun 21 10:25:57 2004 +0200 (2004-06-21)
changeset 14981 e73f8140af78
parent 14706 71590b7733b7
child 15131 c69542757a4d
permissions -rw-r--r--
Merged in license change from Isabelle2004
wenzelm@10330
     1
(*  Title:      HOL/Library/List_Prefix.thy
wenzelm@10330
     2
    ID:         $Id$
wenzelm@10330
     3
    Author:     Tobias Nipkow and Markus Wenzel, TU Muenchen
wenzelm@10330
     4
*)
wenzelm@10330
     5
wenzelm@14706
     6
header {* List prefixes and postfixes *}
wenzelm@10330
     7
wenzelm@10330
     8
theory List_Prefix = Main:
wenzelm@10330
     9
wenzelm@10330
    10
subsection {* Prefix order on lists *}
wenzelm@10330
    11
wenzelm@12338
    12
instance list :: (type) ord ..
wenzelm@10330
    13
wenzelm@10330
    14
defs (overloaded)
wenzelm@10389
    15
  prefix_def: "xs \<le> ys == \<exists>zs. ys = xs @ zs"
wenzelm@10389
    16
  strict_prefix_def: "xs < ys == xs \<le> ys \<and> xs \<noteq> (ys::'a list)"
wenzelm@10330
    17
wenzelm@12338
    18
instance list :: (type) order
wenzelm@10389
    19
  by intro_classes (auto simp add: prefix_def strict_prefix_def)
wenzelm@10330
    20
wenzelm@10389
    21
lemma prefixI [intro?]: "ys = xs @ zs ==> xs \<le> ys"
wenzelm@10389
    22
  by (unfold prefix_def) blast
wenzelm@10330
    23
wenzelm@10389
    24
lemma prefixE [elim?]: "xs \<le> ys ==> (!!zs. ys = xs @ zs ==> C) ==> C"
wenzelm@10389
    25
  by (unfold prefix_def) blast
wenzelm@10330
    26
wenzelm@10870
    27
lemma strict_prefixI' [intro?]: "ys = xs @ z # zs ==> xs < ys"
wenzelm@10870
    28
  by (unfold strict_prefix_def prefix_def) blast
wenzelm@10870
    29
wenzelm@10870
    30
lemma strict_prefixE' [elim?]:
wenzelm@10870
    31
    "xs < ys ==> (!!z zs. ys = xs @ z # zs ==> C) ==> C"
wenzelm@10870
    32
proof -
wenzelm@10870
    33
  assume r: "!!z zs. ys = xs @ z # zs ==> C"
wenzelm@10870
    34
  assume "xs < ys"
wenzelm@10870
    35
  then obtain us where "ys = xs @ us" and "xs \<noteq> ys"
wenzelm@10870
    36
    by (unfold strict_prefix_def prefix_def) blast
wenzelm@10870
    37
  with r show ?thesis by (auto simp add: neq_Nil_conv)
wenzelm@10870
    38
qed
wenzelm@10870
    39
wenzelm@10389
    40
lemma strict_prefixI [intro?]: "xs \<le> ys ==> xs \<noteq> ys ==> xs < (ys::'a list)"
wenzelm@10389
    41
  by (unfold strict_prefix_def) blast
wenzelm@10330
    42
wenzelm@10389
    43
lemma strict_prefixE [elim?]:
wenzelm@10389
    44
    "xs < ys ==> (xs \<le> ys ==> xs \<noteq> (ys::'a list) ==> C) ==> C"
wenzelm@10389
    45
  by (unfold strict_prefix_def) blast
wenzelm@10330
    46
wenzelm@10330
    47
wenzelm@10389
    48
subsection {* Basic properties of prefixes *}
wenzelm@10330
    49
wenzelm@10330
    50
theorem Nil_prefix [iff]: "[] \<le> xs"
wenzelm@10389
    51
  by (simp add: prefix_def)
wenzelm@10330
    52
wenzelm@10330
    53
theorem prefix_Nil [simp]: "(xs \<le> []) = (xs = [])"
wenzelm@10389
    54
  by (induct xs) (simp_all add: prefix_def)
wenzelm@10330
    55
wenzelm@10330
    56
lemma prefix_snoc [simp]: "(xs \<le> ys @ [y]) = (xs = ys @ [y] \<or> xs \<le> ys)"
wenzelm@10389
    57
proof
wenzelm@10389
    58
  assume "xs \<le> ys @ [y]"
wenzelm@10389
    59
  then obtain zs where zs: "ys @ [y] = xs @ zs" ..
wenzelm@10389
    60
  show "xs = ys @ [y] \<or> xs \<le> ys"
wenzelm@10389
    61
  proof (cases zs rule: rev_cases)
wenzelm@10389
    62
    assume "zs = []"
wenzelm@10389
    63
    with zs have "xs = ys @ [y]" by simp
wenzelm@10389
    64
    thus ?thesis ..
wenzelm@10389
    65
  next
wenzelm@10389
    66
    fix z zs' assume "zs = zs' @ [z]"
wenzelm@10389
    67
    with zs have "ys = xs @ zs'" by simp
wenzelm@10389
    68
    hence "xs \<le> ys" ..
wenzelm@10389
    69
    thus ?thesis ..
wenzelm@10389
    70
  qed
wenzelm@10389
    71
next
wenzelm@10389
    72
  assume "xs = ys @ [y] \<or> xs \<le> ys"
wenzelm@10389
    73
  thus "xs \<le> ys @ [y]"
wenzelm@10389
    74
  proof
wenzelm@10389
    75
    assume "xs = ys @ [y]"
wenzelm@10389
    76
    thus ?thesis by simp
wenzelm@10389
    77
  next
wenzelm@10389
    78
    assume "xs \<le> ys"
wenzelm@10389
    79
    then obtain zs where "ys = xs @ zs" ..
wenzelm@10389
    80
    hence "ys @ [y] = xs @ (zs @ [y])" by simp
wenzelm@10389
    81
    thus ?thesis ..
wenzelm@10389
    82
  qed
wenzelm@10389
    83
qed
wenzelm@10330
    84
wenzelm@10330
    85
lemma Cons_prefix_Cons [simp]: "(x # xs \<le> y # ys) = (x = y \<and> xs \<le> ys)"
wenzelm@10389
    86
  by (auto simp add: prefix_def)
wenzelm@10330
    87
wenzelm@10330
    88
lemma same_prefix_prefix [simp]: "(xs @ ys \<le> xs @ zs) = (ys \<le> zs)"
wenzelm@10389
    89
  by (induct xs) simp_all
wenzelm@10330
    90
wenzelm@10389
    91
lemma same_prefix_nil [iff]: "(xs @ ys \<le> xs) = (ys = [])"
wenzelm@10389
    92
proof -
wenzelm@10389
    93
  have "(xs @ ys \<le> xs @ []) = (ys \<le> [])" by (rule same_prefix_prefix)
wenzelm@10389
    94
  thus ?thesis by simp
wenzelm@10389
    95
qed
wenzelm@10330
    96
wenzelm@10330
    97
lemma prefix_prefix [simp]: "xs \<le> ys ==> xs \<le> ys @ zs"
wenzelm@10389
    98
proof -
wenzelm@10389
    99
  assume "xs \<le> ys"
wenzelm@10389
   100
  then obtain us where "ys = xs @ us" ..
wenzelm@10389
   101
  hence "ys @ zs = xs @ (us @ zs)" by simp
wenzelm@10389
   102
  thus ?thesis ..
wenzelm@10389
   103
qed
wenzelm@10330
   104
nipkow@14300
   105
lemma append_prefixD: "xs @ ys \<le> zs \<Longrightarrow> xs \<le> zs"
nipkow@14300
   106
by(simp add:prefix_def) blast
nipkow@14300
   107
wenzelm@10330
   108
theorem prefix_Cons: "(xs \<le> y # ys) = (xs = [] \<or> (\<exists>zs. xs = y # zs \<and> zs \<le> ys))"
wenzelm@10389
   109
  by (cases xs) (auto simp add: prefix_def)
wenzelm@10330
   110
wenzelm@10330
   111
theorem prefix_append:
wenzelm@10330
   112
    "(xs \<le> ys @ zs) = (xs \<le> ys \<or> (\<exists>us. xs = ys @ us \<and> us \<le> zs))"
wenzelm@10330
   113
  apply (induct zs rule: rev_induct)
wenzelm@10330
   114
   apply force
wenzelm@10330
   115
  apply (simp del: append_assoc add: append_assoc [symmetric])
wenzelm@10330
   116
  apply simp
wenzelm@10330
   117
  apply blast
wenzelm@10330
   118
  done
wenzelm@10330
   119
wenzelm@10330
   120
lemma append_one_prefix:
wenzelm@10330
   121
    "xs \<le> ys ==> length xs < length ys ==> xs @ [ys ! length xs] \<le> ys"
wenzelm@10330
   122
  apply (unfold prefix_def)
wenzelm@10330
   123
  apply (auto simp add: nth_append)
wenzelm@10389
   124
  apply (case_tac zs)
wenzelm@10330
   125
   apply auto
wenzelm@10330
   126
  done
wenzelm@10330
   127
wenzelm@10330
   128
theorem prefix_length_le: "xs \<le> ys ==> length xs \<le> length ys"
wenzelm@10389
   129
  by (auto simp add: prefix_def)
wenzelm@10330
   130
wenzelm@10330
   131
nipkow@14300
   132
lemma prefix_same_cases:
nipkow@14300
   133
 "\<lbrakk> (xs\<^isub>1::'a list) \<le> ys; xs\<^isub>2 \<le> ys \<rbrakk> \<Longrightarrow> xs\<^isub>1 \<le> xs\<^isub>2 \<or> xs\<^isub>2 \<le> xs\<^isub>1"
nipkow@14300
   134
apply(simp add:prefix_def)
nipkow@14300
   135
apply(erule exE)+
nipkow@14300
   136
apply(simp add: append_eq_append_conv_if split:if_splits)
nipkow@14300
   137
 apply(rule disjI2)
nipkow@14300
   138
 apply(rule_tac x = "drop (size xs\<^isub>2) xs\<^isub>1" in exI)
nipkow@14300
   139
 apply clarify
nipkow@14300
   140
 apply(drule sym)
nipkow@14300
   141
 apply(insert append_take_drop_id[of "length xs\<^isub>2" xs\<^isub>1])
nipkow@14300
   142
 apply simp
nipkow@14300
   143
apply(rule disjI1)
nipkow@14300
   144
apply(rule_tac x = "drop (size xs\<^isub>1) xs\<^isub>2" in exI)
nipkow@14300
   145
apply clarify
nipkow@14300
   146
apply(insert append_take_drop_id[of "length xs\<^isub>1" xs\<^isub>2])
nipkow@14300
   147
apply simp
nipkow@14300
   148
done
nipkow@14300
   149
nipkow@14300
   150
lemma set_mono_prefix:
nipkow@14300
   151
 "xs \<le> ys \<Longrightarrow> set xs \<subseteq> set ys"
nipkow@14300
   152
by(fastsimp simp add:prefix_def)
nipkow@14300
   153
nipkow@14300
   154
wenzelm@10389
   155
subsection {* Parallel lists *}
wenzelm@10389
   156
wenzelm@10389
   157
constdefs
wenzelm@10389
   158
  parallel :: "'a list => 'a list => bool"    (infixl "\<parallel>" 50)
wenzelm@10389
   159
  "xs \<parallel> ys == \<not> xs \<le> ys \<and> \<not> ys \<le> xs"
wenzelm@10389
   160
wenzelm@10389
   161
lemma parallelI [intro]: "\<not> xs \<le> ys ==> \<not> ys \<le> xs ==> xs \<parallel> ys"
wenzelm@10389
   162
  by (unfold parallel_def) blast
wenzelm@10330
   163
wenzelm@10389
   164
lemma parallelE [elim]:
wenzelm@10389
   165
    "xs \<parallel> ys ==> (\<not> xs \<le> ys ==> \<not> ys \<le> xs ==> C) ==> C"
wenzelm@10389
   166
  by (unfold parallel_def) blast
wenzelm@10330
   167
wenzelm@10389
   168
theorem prefix_cases:
wenzelm@10389
   169
  "(xs \<le> ys ==> C) ==>
wenzelm@10512
   170
    (ys < xs ==> C) ==>
wenzelm@10389
   171
    (xs \<parallel> ys ==> C) ==> C"
wenzelm@10512
   172
  by (unfold parallel_def strict_prefix_def) blast
wenzelm@10330
   173
wenzelm@10389
   174
theorem parallel_decomp:
wenzelm@10389
   175
  "xs \<parallel> ys ==> \<exists>as b bs c cs. b \<noteq> c \<and> xs = as @ b # bs \<and> ys = as @ c # cs"
wenzelm@10408
   176
proof (induct xs rule: rev_induct)
wenzelm@11987
   177
  case Nil
wenzelm@11987
   178
  hence False by auto
wenzelm@11987
   179
  thus ?case ..
wenzelm@10408
   180
next
wenzelm@11987
   181
  case (snoc x xs)
wenzelm@11987
   182
  show ?case
wenzelm@10408
   183
  proof (rule prefix_cases)
wenzelm@10408
   184
    assume le: "xs \<le> ys"
wenzelm@10408
   185
    then obtain ys' where ys: "ys = xs @ ys'" ..
wenzelm@10408
   186
    show ?thesis
wenzelm@10408
   187
    proof (cases ys')
wenzelm@10408
   188
      assume "ys' = []" with ys have "xs = ys" by simp
wenzelm@11987
   189
      with snoc have "[x] \<parallel> []" by auto
wenzelm@10408
   190
      hence False by blast
wenzelm@10389
   191
      thus ?thesis ..
wenzelm@10389
   192
    next
wenzelm@10408
   193
      fix c cs assume ys': "ys' = c # cs"
wenzelm@11987
   194
      with snoc ys have "xs @ [x] \<parallel> xs @ c # cs" by (simp only:)
wenzelm@10408
   195
      hence "x \<noteq> c" by auto
wenzelm@10408
   196
      moreover have "xs @ [x] = xs @ x # []" by simp
wenzelm@10408
   197
      moreover from ys ys' have "ys = xs @ c # cs" by (simp only:)
wenzelm@10408
   198
      ultimately show ?thesis by blast
wenzelm@10389
   199
    qed
wenzelm@10408
   200
  next
wenzelm@10512
   201
    assume "ys < xs" hence "ys \<le> xs @ [x]" by (simp add: strict_prefix_def)
wenzelm@11987
   202
    with snoc have False by blast
wenzelm@10408
   203
    thus ?thesis ..
wenzelm@10408
   204
  next
wenzelm@10408
   205
    assume "xs \<parallel> ys"
wenzelm@11987
   206
    with snoc obtain as b bs c cs where neq: "(b::'a) \<noteq> c"
wenzelm@10408
   207
      and xs: "xs = as @ b # bs" and ys: "ys = as @ c # cs"
wenzelm@10408
   208
      by blast
wenzelm@10408
   209
    from xs have "xs @ [x] = as @ b # (bs @ [x])" by simp
wenzelm@10408
   210
    with neq ys show ?thesis by blast
wenzelm@10389
   211
  qed
wenzelm@10389
   212
qed
wenzelm@10330
   213
oheimb@14538
   214
oheimb@14538
   215
subsection {* Postfix order on lists *}
oheimb@14538
   216
oheimb@14538
   217
constdefs
oheimb@14538
   218
  postfix :: "'a list => 'a list => bool"  ("(_/ >= _)" [51, 50] 50)
oheimb@14538
   219
  "xs >= ys == \<exists>zs. xs = zs @ ys"
oheimb@14538
   220
wenzelm@14706
   221
lemma postfix_refl [simp, intro!]: "xs >= xs"
wenzelm@14706
   222
  by (auto simp add: postfix_def)
wenzelm@14706
   223
lemma postfix_trans: "\<lbrakk>xs >= ys; ys >= zs\<rbrakk> \<Longrightarrow> xs >= zs"
wenzelm@14706
   224
  by (auto simp add: postfix_def)
wenzelm@14706
   225
lemma postfix_antisym: "\<lbrakk>xs >= ys; ys >= xs\<rbrakk> \<Longrightarrow> xs = ys"
wenzelm@14706
   226
  by (auto simp add: postfix_def)
oheimb@14538
   227
wenzelm@14706
   228
lemma Nil_postfix [iff]: "xs >= []"
wenzelm@14706
   229
  by (simp add: postfix_def)
wenzelm@14706
   230
lemma postfix_Nil [simp]: "([] >= xs) = (xs = [])"
wenzelm@14706
   231
  by (auto simp add:postfix_def)
oheimb@14538
   232
wenzelm@14706
   233
lemma postfix_ConsI: "xs >= ys \<Longrightarrow> x#xs >= ys"
wenzelm@14706
   234
  by (auto simp add: postfix_def)
wenzelm@14706
   235
lemma postfix_ConsD: "xs >= y#ys \<Longrightarrow> xs >= ys"
wenzelm@14706
   236
  by (auto simp add: postfix_def)
oheimb@14538
   237
wenzelm@14706
   238
lemma postfix_appendI: "xs >= ys \<Longrightarrow> zs @ xs >= ys"
wenzelm@14706
   239
  by (auto simp add: postfix_def)
wenzelm@14706
   240
lemma postfix_appendD: "xs >= zs @ ys \<Longrightarrow> xs >= ys"
wenzelm@14706
   241
  by(auto simp add: postfix_def)
oheimb@14538
   242
oheimb@14538
   243
lemma postfix_is_subset_lemma: "xs = zs @ ys \<Longrightarrow> set ys \<subseteq> set xs"
wenzelm@14706
   244
  by (induct zs, auto)
oheimb@14538
   245
lemma postfix_is_subset: "xs >= ys \<Longrightarrow> set ys \<subseteq> set xs"
wenzelm@14706
   246
  by (unfold postfix_def, erule exE, erule postfix_is_subset_lemma)
oheimb@14538
   247
oheimb@14538
   248
lemma postfix_ConsD2_lemma [rule_format]: "x#xs = zs @ y#ys \<longrightarrow> xs >= ys"
wenzelm@14706
   249
  by (induct zs, auto intro!: postfix_appendI postfix_ConsI)
oheimb@14538
   250
lemma postfix_ConsD2: "x#xs >= y#ys \<Longrightarrow> xs >= ys"
wenzelm@14706
   251
  by (auto simp add: postfix_def dest!: postfix_ConsD2_lemma)
oheimb@14538
   252
oheimb@14538
   253
lemma postfix2prefix: "(xs >= ys) = (rev ys <= rev xs)"
wenzelm@14706
   254
  apply (unfold prefix_def postfix_def, safe)
wenzelm@14706
   255
  apply (rule_tac x = "rev zs" in exI, simp)
wenzelm@14706
   256
  apply (rule_tac x = "rev zs" in exI)
wenzelm@14706
   257
  apply (rule rev_is_rev_conv [THEN iffD1], simp)
wenzelm@14706
   258
  done
oheimb@14538
   259
wenzelm@10330
   260
end