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