src/HOL/Library/List_Prefix.thy
author nipkow
Thu Dec 18 08:20:36 2003 +0100 (2003-12-18)
changeset 14300 bf8b8c9425c3
parent 12338 de0f4a63baa5
child 14538 1d9d75a8efae
permissions -rw-r--r--
*** empty log message ***
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@11780
     4
    License:    GPL (GNU GENERAL PUBLIC LICENSE)
wenzelm@10330
     5
*)
wenzelm@10330
     6
wenzelm@10330
     7
header {*
wenzelm@10330
     8
  \title{List prefixes}
wenzelm@10330
     9
  \author{Tobias Nipkow and Markus Wenzel}
wenzelm@10330
    10
*}
wenzelm@10330
    11
wenzelm@10330
    12
theory List_Prefix = Main:
wenzelm@10330
    13
wenzelm@10330
    14
subsection {* Prefix order on lists *}
wenzelm@10330
    15
wenzelm@12338
    16
instance list :: (type) ord ..
wenzelm@10330
    17
wenzelm@10330
    18
defs (overloaded)
wenzelm@10389
    19
  prefix_def: "xs \<le> ys == \<exists>zs. ys = xs @ zs"
wenzelm@10389
    20
  strict_prefix_def: "xs < ys == xs \<le> ys \<and> xs \<noteq> (ys::'a list)"
wenzelm@10330
    21
wenzelm@12338
    22
instance list :: (type) order
wenzelm@10389
    23
  by intro_classes (auto simp add: prefix_def strict_prefix_def)
wenzelm@10330
    24
wenzelm@10389
    25
lemma prefixI [intro?]: "ys = xs @ zs ==> xs \<le> ys"
wenzelm@10389
    26
  by (unfold prefix_def) blast
wenzelm@10330
    27
wenzelm@10389
    28
lemma prefixE [elim?]: "xs \<le> ys ==> (!!zs. ys = xs @ zs ==> C) ==> C"
wenzelm@10389
    29
  by (unfold prefix_def) blast
wenzelm@10330
    30
wenzelm@10870
    31
lemma strict_prefixI' [intro?]: "ys = xs @ z # zs ==> xs < ys"
wenzelm@10870
    32
  by (unfold strict_prefix_def prefix_def) blast
wenzelm@10870
    33
wenzelm@10870
    34
lemma strict_prefixE' [elim?]:
wenzelm@10870
    35
    "xs < ys ==> (!!z zs. ys = xs @ z # zs ==> C) ==> C"
wenzelm@10870
    36
proof -
wenzelm@10870
    37
  assume r: "!!z zs. ys = xs @ z # zs ==> C"
wenzelm@10870
    38
  assume "xs < ys"
wenzelm@10870
    39
  then obtain us where "ys = xs @ us" and "xs \<noteq> ys"
wenzelm@10870
    40
    by (unfold strict_prefix_def prefix_def) blast
wenzelm@10870
    41
  with r show ?thesis by (auto simp add: neq_Nil_conv)
wenzelm@10870
    42
qed
wenzelm@10870
    43
wenzelm@10389
    44
lemma strict_prefixI [intro?]: "xs \<le> ys ==> xs \<noteq> ys ==> xs < (ys::'a list)"
wenzelm@10389
    45
  by (unfold strict_prefix_def) blast
wenzelm@10330
    46
wenzelm@10389
    47
lemma strict_prefixE [elim?]:
wenzelm@10389
    48
    "xs < ys ==> (xs \<le> ys ==> xs \<noteq> (ys::'a list) ==> C) ==> C"
wenzelm@10389
    49
  by (unfold strict_prefix_def) blast
wenzelm@10330
    50
wenzelm@10330
    51
wenzelm@10389
    52
subsection {* Basic properties of prefixes *}
wenzelm@10330
    53
wenzelm@10330
    54
theorem Nil_prefix [iff]: "[] \<le> xs"
wenzelm@10389
    55
  by (simp add: prefix_def)
wenzelm@10330
    56
wenzelm@10330
    57
theorem prefix_Nil [simp]: "(xs \<le> []) = (xs = [])"
wenzelm@10389
    58
  by (induct xs) (simp_all add: prefix_def)
wenzelm@10330
    59
wenzelm@10330
    60
lemma prefix_snoc [simp]: "(xs \<le> ys @ [y]) = (xs = ys @ [y] \<or> xs \<le> ys)"
wenzelm@10389
    61
proof
wenzelm@10389
    62
  assume "xs \<le> ys @ [y]"
wenzelm@10389
    63
  then obtain zs where zs: "ys @ [y] = xs @ zs" ..
wenzelm@10389
    64
  show "xs = ys @ [y] \<or> xs \<le> ys"
wenzelm@10389
    65
  proof (cases zs rule: rev_cases)
wenzelm@10389
    66
    assume "zs = []"
wenzelm@10389
    67
    with zs have "xs = ys @ [y]" by simp
wenzelm@10389
    68
    thus ?thesis ..
wenzelm@10389
    69
  next
wenzelm@10389
    70
    fix z zs' assume "zs = zs' @ [z]"
wenzelm@10389
    71
    with zs have "ys = xs @ zs'" by simp
wenzelm@10389
    72
    hence "xs \<le> ys" ..
wenzelm@10389
    73
    thus ?thesis ..
wenzelm@10389
    74
  qed
wenzelm@10389
    75
next
wenzelm@10389
    76
  assume "xs = ys @ [y] \<or> xs \<le> ys"
wenzelm@10389
    77
  thus "xs \<le> ys @ [y]"
wenzelm@10389
    78
  proof
wenzelm@10389
    79
    assume "xs = ys @ [y]"
wenzelm@10389
    80
    thus ?thesis by simp
wenzelm@10389
    81
  next
wenzelm@10389
    82
    assume "xs \<le> ys"
wenzelm@10389
    83
    then obtain zs where "ys = xs @ zs" ..
wenzelm@10389
    84
    hence "ys @ [y] = xs @ (zs @ [y])" by simp
wenzelm@10389
    85
    thus ?thesis ..
wenzelm@10389
    86
  qed
wenzelm@10389
    87
qed
wenzelm@10330
    88
wenzelm@10330
    89
lemma Cons_prefix_Cons [simp]: "(x # xs \<le> y # ys) = (x = y \<and> xs \<le> ys)"
wenzelm@10389
    90
  by (auto simp add: prefix_def)
wenzelm@10330
    91
wenzelm@10330
    92
lemma same_prefix_prefix [simp]: "(xs @ ys \<le> xs @ zs) = (ys \<le> zs)"
wenzelm@10389
    93
  by (induct xs) simp_all
wenzelm@10330
    94
wenzelm@10389
    95
lemma same_prefix_nil [iff]: "(xs @ ys \<le> xs) = (ys = [])"
wenzelm@10389
    96
proof -
wenzelm@10389
    97
  have "(xs @ ys \<le> xs @ []) = (ys \<le> [])" by (rule same_prefix_prefix)
wenzelm@10389
    98
  thus ?thesis by simp
wenzelm@10389
    99
qed
wenzelm@10330
   100
wenzelm@10330
   101
lemma prefix_prefix [simp]: "xs \<le> ys ==> xs \<le> ys @ zs"
wenzelm@10389
   102
proof -
wenzelm@10389
   103
  assume "xs \<le> ys"
wenzelm@10389
   104
  then obtain us where "ys = xs @ us" ..
wenzelm@10389
   105
  hence "ys @ zs = xs @ (us @ zs)" by simp
wenzelm@10389
   106
  thus ?thesis ..
wenzelm@10389
   107
qed
wenzelm@10330
   108
nipkow@14300
   109
lemma append_prefixD: "xs @ ys \<le> zs \<Longrightarrow> xs \<le> zs"
nipkow@14300
   110
by(simp add:prefix_def) blast
nipkow@14300
   111
wenzelm@10330
   112
theorem prefix_Cons: "(xs \<le> y # ys) = (xs = [] \<or> (\<exists>zs. xs = y # zs \<and> zs \<le> ys))"
wenzelm@10389
   113
  by (cases xs) (auto simp add: prefix_def)
wenzelm@10330
   114
wenzelm@10330
   115
theorem prefix_append:
wenzelm@10330
   116
    "(xs \<le> ys @ zs) = (xs \<le> ys \<or> (\<exists>us. xs = ys @ us \<and> us \<le> zs))"
wenzelm@10330
   117
  apply (induct zs rule: rev_induct)
wenzelm@10330
   118
   apply force
wenzelm@10330
   119
  apply (simp del: append_assoc add: append_assoc [symmetric])
wenzelm@10330
   120
  apply simp
wenzelm@10330
   121
  apply blast
wenzelm@10330
   122
  done
wenzelm@10330
   123
wenzelm@10330
   124
lemma append_one_prefix:
wenzelm@10330
   125
    "xs \<le> ys ==> length xs < length ys ==> xs @ [ys ! length xs] \<le> ys"
wenzelm@10330
   126
  apply (unfold prefix_def)
wenzelm@10330
   127
  apply (auto simp add: nth_append)
wenzelm@10389
   128
  apply (case_tac zs)
wenzelm@10330
   129
   apply auto
wenzelm@10330
   130
  done
wenzelm@10330
   131
wenzelm@10330
   132
theorem prefix_length_le: "xs \<le> ys ==> length xs \<le> length ys"
wenzelm@10389
   133
  by (auto simp add: prefix_def)
wenzelm@10330
   134
wenzelm@10330
   135
nipkow@14300
   136
lemma prefix_same_cases:
nipkow@14300
   137
 "\<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
   138
apply(simp add:prefix_def)
nipkow@14300
   139
apply(erule exE)+
nipkow@14300
   140
apply(simp add: append_eq_append_conv_if split:if_splits)
nipkow@14300
   141
 apply(rule disjI2)
nipkow@14300
   142
 apply(rule_tac x = "drop (size xs\<^isub>2) xs\<^isub>1" in exI)
nipkow@14300
   143
 apply clarify
nipkow@14300
   144
 apply(drule sym)
nipkow@14300
   145
 apply(insert append_take_drop_id[of "length xs\<^isub>2" xs\<^isub>1])
nipkow@14300
   146
 apply simp
nipkow@14300
   147
apply(rule disjI1)
nipkow@14300
   148
apply(rule_tac x = "drop (size xs\<^isub>1) xs\<^isub>2" in exI)
nipkow@14300
   149
apply clarify
nipkow@14300
   150
apply(insert append_take_drop_id[of "length xs\<^isub>1" xs\<^isub>2])
nipkow@14300
   151
apply simp
nipkow@14300
   152
done
nipkow@14300
   153
nipkow@14300
   154
lemma set_mono_prefix:
nipkow@14300
   155
 "xs \<le> ys \<Longrightarrow> set xs \<subseteq> set ys"
nipkow@14300
   156
by(fastsimp simp add:prefix_def)
nipkow@14300
   157
nipkow@14300
   158
wenzelm@10389
   159
subsection {* Parallel lists *}
wenzelm@10389
   160
wenzelm@10389
   161
constdefs
wenzelm@10389
   162
  parallel :: "'a list => 'a list => bool"    (infixl "\<parallel>" 50)
wenzelm@10389
   163
  "xs \<parallel> ys == \<not> xs \<le> ys \<and> \<not> ys \<le> xs"
wenzelm@10389
   164
wenzelm@10389
   165
lemma parallelI [intro]: "\<not> xs \<le> ys ==> \<not> ys \<le> xs ==> xs \<parallel> ys"
wenzelm@10389
   166
  by (unfold parallel_def) blast
wenzelm@10330
   167
wenzelm@10389
   168
lemma parallelE [elim]:
wenzelm@10389
   169
    "xs \<parallel> ys ==> (\<not> xs \<le> ys ==> \<not> ys \<le> xs ==> C) ==> C"
wenzelm@10389
   170
  by (unfold parallel_def) blast
wenzelm@10330
   171
wenzelm@10389
   172
theorem prefix_cases:
wenzelm@10389
   173
  "(xs \<le> ys ==> C) ==>
wenzelm@10512
   174
    (ys < xs ==> C) ==>
wenzelm@10389
   175
    (xs \<parallel> ys ==> C) ==> C"
wenzelm@10512
   176
  by (unfold parallel_def strict_prefix_def) blast
wenzelm@10330
   177
wenzelm@10389
   178
theorem parallel_decomp:
wenzelm@10389
   179
  "xs \<parallel> ys ==> \<exists>as b bs c cs. b \<noteq> c \<and> xs = as @ b # bs \<and> ys = as @ c # cs"
wenzelm@10408
   180
proof (induct xs rule: rev_induct)
wenzelm@11987
   181
  case Nil
wenzelm@11987
   182
  hence False by auto
wenzelm@11987
   183
  thus ?case ..
wenzelm@10408
   184
next
wenzelm@11987
   185
  case (snoc x xs)
wenzelm@11987
   186
  show ?case
wenzelm@10408
   187
  proof (rule prefix_cases)
wenzelm@10408
   188
    assume le: "xs \<le> ys"
wenzelm@10408
   189
    then obtain ys' where ys: "ys = xs @ ys'" ..
wenzelm@10408
   190
    show ?thesis
wenzelm@10408
   191
    proof (cases ys')
wenzelm@10408
   192
      assume "ys' = []" with ys have "xs = ys" by simp
wenzelm@11987
   193
      with snoc have "[x] \<parallel> []" by auto
wenzelm@10408
   194
      hence False by blast
wenzelm@10389
   195
      thus ?thesis ..
wenzelm@10389
   196
    next
wenzelm@10408
   197
      fix c cs assume ys': "ys' = c # cs"
wenzelm@11987
   198
      with snoc ys have "xs @ [x] \<parallel> xs @ c # cs" by (simp only:)
wenzelm@10408
   199
      hence "x \<noteq> c" by auto
wenzelm@10408
   200
      moreover have "xs @ [x] = xs @ x # []" by simp
wenzelm@10408
   201
      moreover from ys ys' have "ys = xs @ c # cs" by (simp only:)
wenzelm@10408
   202
      ultimately show ?thesis by blast
wenzelm@10389
   203
    qed
wenzelm@10408
   204
  next
wenzelm@10512
   205
    assume "ys < xs" hence "ys \<le> xs @ [x]" by (simp add: strict_prefix_def)
wenzelm@11987
   206
    with snoc have False by blast
wenzelm@10408
   207
    thus ?thesis ..
wenzelm@10408
   208
  next
wenzelm@10408
   209
    assume "xs \<parallel> ys"
wenzelm@11987
   210
    with snoc obtain as b bs c cs where neq: "(b::'a) \<noteq> c"
wenzelm@10408
   211
      and xs: "xs = as @ b # bs" and ys: "ys = as @ c # cs"
wenzelm@10408
   212
      by blast
wenzelm@10408
   213
    from xs have "xs @ [x] = as @ b # (bs @ [x])" by simp
wenzelm@10408
   214
    with neq ys show ?thesis by blast
wenzelm@10389
   215
  qed
wenzelm@10389
   216
qed
wenzelm@10330
   217
wenzelm@10330
   218
end