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