src/HOL/Library/List_Prefix.thy
author nipkow
Mon Dec 17 17:01:54 2007 +0100 (2007-12-17)
changeset 25665 faabc08af882
parent 25595 6c48275f9c76
child 25692 eda4958ab0d2
permissions -rw-r--r--
removed legacy proofs
     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
     9 imports List
    10 begin
    11 
    12 subsection {* Prefix order on lists *}
    13 
    14 instance list :: (type) ord ..
    15 
    16 defs (overloaded)
    17   prefix_def: "xs \<le> ys == \<exists>zs. ys = xs @ zs"
    18   strict_prefix_def: "xs < ys == xs \<le> ys \<and> xs \<noteq> (ys::'a list)"
    19 
    20 instance list :: (type) order
    21   by intro_classes (auto simp add: prefix_def strict_prefix_def)
    22 
    23 lemma prefixI [intro?]: "ys = xs @ zs ==> xs \<le> ys"
    24   unfolding prefix_def by blast
    25 
    26 lemma prefixE [elim?]:
    27   assumes "xs \<le> ys"
    28   obtains zs where "ys = xs @ zs"
    29   using assms unfolding prefix_def by blast
    30 
    31 lemma strict_prefixI' [intro?]: "ys = xs @ z # zs ==> xs < ys"
    32   unfolding strict_prefix_def prefix_def by blast
    33 
    34 lemma strict_prefixE' [elim?]:
    35   assumes "xs < ys"
    36   obtains z zs where "ys = xs @ z # zs"
    37 proof -
    38   from `xs < ys` obtain us where "ys = xs @ us" and "xs \<noteq> ys"
    39     unfolding strict_prefix_def prefix_def by blast
    40   with that show ?thesis by (auto simp add: neq_Nil_conv)
    41 qed
    42 
    43 lemma strict_prefixI [intro?]: "xs \<le> ys ==> xs \<noteq> ys ==> xs < (ys::'a list)"
    44   unfolding strict_prefix_def by blast
    45 
    46 lemma strict_prefixE [elim?]:
    47   fixes xs ys :: "'a list"
    48   assumes "xs < ys"
    49   obtains "xs \<le> ys" and "xs \<noteq> ys"
    50   using assms unfolding strict_prefix_def by blast
    51 
    52 
    53 subsection {* Basic properties of prefixes *}
    54 
    55 theorem Nil_prefix [iff]: "[] \<le> xs"
    56   by (simp add: prefix_def)
    57 
    58 theorem prefix_Nil [simp]: "(xs \<le> []) = (xs = [])"
    59   by (induct xs) (simp_all add: prefix_def)
    60 
    61 lemma prefix_snoc [simp]: "(xs \<le> ys @ [y]) = (xs = ys @ [y] \<or> xs \<le> ys)"
    62 proof
    63   assume "xs \<le> ys @ [y]"
    64   then obtain zs where zs: "ys @ [y] = xs @ zs" ..
    65   show "xs = ys @ [y] \<or> xs \<le> ys"
    66     by (metis append_Nil2 butlast_append butlast_snoc prefixI zs)
    67 next
    68   assume "xs = ys @ [y] \<or> xs \<le> ys"
    69   then show "xs \<le> ys @ [y]"
    70     by (metis order_eq_iff strict_prefixE strict_prefixI' xt1(7))
    71 qed
    72 
    73 lemma Cons_prefix_Cons [simp]: "(x # xs \<le> y # ys) = (x = y \<and> xs \<le> ys)"
    74   by (auto simp add: prefix_def)
    75 
    76 lemma same_prefix_prefix [simp]: "(xs @ ys \<le> xs @ zs) = (ys \<le> zs)"
    77   by (induct xs) simp_all
    78 
    79 lemma same_prefix_nil [iff]: "(xs @ ys \<le> xs) = (ys = [])"
    80 by (metis append_Nil2 append_self_conv order_eq_iff prefixI)
    81 
    82 lemma prefix_prefix [simp]: "xs \<le> ys ==> xs \<le> ys @ zs"
    83 by (metis order_le_less_trans prefixI strict_prefixE strict_prefixI)
    84 
    85 lemma append_prefixD: "xs @ ys \<le> zs \<Longrightarrow> xs \<le> zs"
    86   by (auto simp add: prefix_def)
    87 
    88 theorem prefix_Cons: "(xs \<le> y # ys) = (xs = [] \<or> (\<exists>zs. xs = y # zs \<and> zs \<le> ys))"
    89   by (cases xs) (auto simp add: prefix_def)
    90 
    91 theorem prefix_append:
    92   "(xs \<le> ys @ zs) = (xs \<le> ys \<or> (\<exists>us. xs = ys @ us \<and> us \<le> zs))"
    93   apply (induct zs rule: rev_induct)
    94    apply force
    95   apply (simp del: append_assoc add: append_assoc [symmetric])
    96   apply (metis append_eq_appendI)
    97   done
    98 
    99 lemma append_one_prefix:
   100   "xs \<le> ys ==> length xs < length ys ==> xs @ [ys ! length xs] \<le> ys"
   101 by (unfold prefix_def)
   102    (metis Cons_eq_appendI append_eq_appendI append_eq_conv_conj eq_Nil_appendI nth_drop')
   103 
   104 theorem prefix_length_le: "xs \<le> ys ==> length xs \<le> length ys"
   105   by (auto simp add: prefix_def)
   106 
   107 lemma prefix_same_cases:
   108   "(xs\<^isub>1::'a list) \<le> ys \<Longrightarrow> xs\<^isub>2 \<le> ys \<Longrightarrow> xs\<^isub>1 \<le> xs\<^isub>2 \<or> xs\<^isub>2 \<le> xs\<^isub>1"
   109 by (unfold prefix_def) (metis append_eq_append_conv2)
   110 
   111 lemma set_mono_prefix: "xs \<le> ys \<Longrightarrow> set xs \<subseteq> set ys"
   112 by (auto simp add: prefix_def)
   113 
   114 lemma take_is_prefix: "take n xs \<le> xs"
   115 by (unfold prefix_def) (metis append_take_drop_id)
   116 
   117 lemma map_prefixI:
   118   "xs \<le> ys \<Longrightarrow> map f xs \<le> map f ys"
   119 by (clarsimp simp: prefix_def)
   120 
   121 lemma prefix_length_less:
   122   "xs < ys \<Longrightarrow> length xs < length ys"
   123 by (clarsimp simp: strict_prefix_def prefix_def)
   124 
   125 lemma strict_prefix_simps [simp]:
   126   "xs < [] = False"
   127   "[] < (x # xs) = True"
   128   "(x # xs) < (y # ys) = (x = y \<and> xs < ys)"
   129 by (simp_all add: strict_prefix_def cong: conj_cong)
   130 
   131 lemma take_strict_prefix: "xs < ys \<Longrightarrow> take n xs < ys"
   132 apply (induct n arbitrary: xs ys)
   133  apply (case_tac ys, simp_all)[1]
   134 apply (metis order_less_trans strict_prefixI take_is_prefix)
   135 done
   136 
   137 lemma not_prefix_cases:
   138   assumes pfx: "\<not> ps \<le> ls"
   139   obtains
   140     (c1) "ps \<noteq> []" and "ls = []"
   141   | (c2) a as x xs where "ps = a#as" and "ls = x#xs" and "x = a" and "\<not> as \<le> xs"
   142   | (c3) a as x xs where "ps = a#as" and "ls = x#xs" and "x \<noteq> a"
   143 proof (cases ps)
   144   case Nil thus ?thesis using pfx by simp
   145 next
   146   case (Cons a as)
   147   hence c: "ps = a#as" .
   148   show ?thesis
   149   proof (cases ls)
   150     case Nil thus ?thesis by (metis append_Nil2 pfx c1 same_prefix_nil)
   151   next
   152     case (Cons x xs)
   153     show ?thesis
   154     proof (cases "x = a")
   155       case True
   156       have "\<not> as \<le> xs" using pfx c Cons True by simp
   157       with c Cons True show ?thesis by (rule c2)
   158     next
   159       case False
   160       with c Cons show ?thesis by (rule c3)
   161     qed
   162   qed
   163 qed
   164 
   165 lemma not_prefix_induct [consumes 1, case_names Nil Neq Eq]:
   166   assumes np: "\<not> ps \<le> ls"
   167     and base: "\<And>x xs. P (x#xs) []"
   168     and r1: "\<And>x xs y ys. x \<noteq> y \<Longrightarrow> P (x#xs) (y#ys)"
   169     and r2: "\<And>x xs y ys. \<lbrakk> x = y; \<not> xs \<le> ys; P xs ys \<rbrakk> \<Longrightarrow> P (x#xs) (y#ys)"
   170   shows "P ps ls" using np
   171 proof (induct ls arbitrary: ps)
   172   case Nil then show ?case
   173     by (auto simp: neq_Nil_conv elim!: not_prefix_cases intro!: base)
   174 next
   175   case (Cons y ys)
   176   then have npfx: "\<not> ps \<le> (y # ys)" by simp
   177   then obtain x xs where pv: "ps = x # xs"
   178     by (rule not_prefix_cases) auto
   179   show ?case by (metis Cons.hyps Cons_prefix_Cons npfx pv r1 r2)
   180 qed
   181 
   182 
   183 subsection {* Parallel lists *}
   184 
   185 definition
   186   parallel :: "'a list => 'a list => bool"  (infixl "\<parallel>" 50) where
   187   "(xs \<parallel> ys) = (\<not> xs \<le> ys \<and> \<not> ys \<le> xs)"
   188 
   189 lemma parallelI [intro]: "\<not> xs \<le> ys ==> \<not> ys \<le> xs ==> xs \<parallel> ys"
   190 unfolding parallel_def by blast
   191 
   192 lemma parallelE [elim]:
   193 assumes "xs \<parallel> ys"
   194 obtains "\<not> xs \<le> ys \<and> \<not> ys \<le> xs"
   195 using assms unfolding parallel_def by blast
   196 
   197 theorem prefix_cases:
   198 obtains "xs \<le> ys" | "ys < xs" | "xs \<parallel> ys"
   199 unfolding parallel_def strict_prefix_def by blast
   200 
   201 theorem parallel_decomp:
   202   "xs \<parallel> ys ==> \<exists>as b bs c cs. b \<noteq> c \<and> xs = as @ b # bs \<and> ys = as @ c # cs"
   203 proof (induct xs rule: rev_induct)
   204   case Nil
   205   then have False by auto
   206   then show ?case ..
   207 next
   208   case (snoc x xs)
   209   show ?case
   210   proof (rule prefix_cases)
   211     assume le: "xs \<le> ys"
   212     then obtain ys' where ys: "ys = xs @ ys'" ..
   213     show ?thesis
   214     proof (cases ys')
   215       assume "ys' = []"
   216       thus ?thesis by (metis append_Nil2 parallelE prefixI snoc.prems ys)
   217     next
   218       fix c cs assume ys': "ys' = c # cs"
   219       thus ?thesis
   220 	by (metis Cons_eq_appendI eq_Nil_appendI parallelE prefixI same_prefix_prefix snoc.prems ys)
   221     qed
   222   next
   223     assume "ys < xs" then have "ys \<le> xs @ [x]" by (simp add: strict_prefix_def)
   224     with snoc have False by blast
   225     then show ?thesis ..
   226   next
   227     assume "xs \<parallel> ys"
   228     with snoc obtain as b bs c cs where neq: "(b::'a) \<noteq> c"
   229       and xs: "xs = as @ b # bs" and ys: "ys = as @ c # cs"
   230       by blast
   231     from xs have "xs @ [x] = as @ b # (bs @ [x])" by simp
   232     with neq ys show ?thesis by blast
   233   qed
   234 qed
   235 
   236 lemma parallel_append: "a \<parallel> b \<Longrightarrow> a @ c \<parallel> b @ d"
   237 by (rule parallelI)
   238    (erule parallelE, erule conjE,
   239           induct rule: not_prefix_induct, simp+)+
   240 
   241 lemma parallel_appendI: "\<lbrakk> xs \<parallel> ys; x = xs @ xs' ; y = ys @ ys' \<rbrakk> \<Longrightarrow> x \<parallel> y"
   242 by simp (rule parallel_append)
   243 
   244 lemma parallel_commute: "(a \<parallel> b) = (b \<parallel> a)"
   245 unfolding parallel_def by auto
   246 
   247 
   248 subsection {* Postfix order on lists *}
   249 
   250 definition
   251   postfix :: "'a list => 'a list => bool"  ("(_/ >>= _)" [51, 50] 50) where
   252   "(xs >>= ys) = (\<exists>zs. xs = zs @ ys)"
   253 
   254 lemma postfixI [intro?]: "xs = zs @ ys ==> xs >>= ys"
   255 unfolding postfix_def by blast
   256 
   257 lemma postfixE [elim?]:
   258 assumes "xs >>= ys"
   259 obtains zs where "xs = zs @ ys"
   260 using assms unfolding postfix_def by blast
   261 
   262 lemma postfix_refl [iff]: "xs >>= xs"
   263   by (auto simp add: postfix_def)
   264 lemma postfix_trans: "\<lbrakk>xs >>= ys; ys >>= zs\<rbrakk> \<Longrightarrow> xs >>= zs"
   265   by (auto simp add: postfix_def)
   266 lemma postfix_antisym: "\<lbrakk>xs >>= ys; ys >>= xs\<rbrakk> \<Longrightarrow> xs = ys"
   267   by (auto simp add: postfix_def)
   268 
   269 lemma Nil_postfix [iff]: "xs >>= []"
   270   by (simp add: postfix_def)
   271 lemma postfix_Nil [simp]: "([] >>= xs) = (xs = [])"
   272   by (auto simp add: postfix_def)
   273 
   274 lemma postfix_ConsI: "xs >>= ys \<Longrightarrow> x#xs >>= ys"
   275   by (auto simp add: postfix_def)
   276 lemma postfix_ConsD: "xs >>= y#ys \<Longrightarrow> xs >>= ys"
   277   by (auto simp add: postfix_def)
   278 
   279 lemma postfix_appendI: "xs >>= ys \<Longrightarrow> zs @ xs >>= ys"
   280   by (auto simp add: postfix_def)
   281 lemma postfix_appendD: "xs >>= zs @ ys \<Longrightarrow> xs >>= ys"
   282   by (auto simp add: postfix_def)
   283 
   284 lemma postfix_is_subset: "xs >>= ys ==> set ys \<subseteq> set xs"
   285 proof -
   286   assume "xs >>= ys"
   287   then obtain zs where "xs = zs @ ys" ..
   288   then show ?thesis by (induct zs) auto
   289 qed
   290 
   291 lemma postfix_ConsD2: "x#xs >>= y#ys ==> xs >>= ys"
   292 proof -
   293   assume "x#xs >>= y#ys"
   294   then obtain zs where "x#xs = zs @ y#ys" ..
   295   then show ?thesis
   296     by (induct zs) (auto intro!: postfix_appendI postfix_ConsI)
   297 qed
   298 
   299 lemma postfix_to_prefix: "xs >>= ys \<longleftrightarrow> rev ys \<le> rev xs"
   300 proof
   301   assume "xs >>= ys"
   302   then obtain zs where "xs = zs @ ys" ..
   303   then have "rev xs = rev ys @ rev zs" by simp
   304   then show "rev ys <= rev xs" ..
   305 next
   306   assume "rev ys <= rev xs"
   307   then obtain zs where "rev xs = rev ys @ zs" ..
   308   then have "rev (rev xs) = rev zs @ rev (rev ys)" by simp
   309   then have "xs = rev zs @ ys" by simp
   310   then show "xs >>= ys" ..
   311 qed
   312 
   313 lemma distinct_postfix: "distinct xs \<Longrightarrow> xs >>= ys \<Longrightarrow> distinct ys"
   314 by (clarsimp elim!: postfixE)
   315 
   316 lemma postfix_map: "xs >>= ys \<Longrightarrow> map f xs >>= map f ys"
   317 by (auto elim!: postfixE intro: postfixI)
   318 
   319 lemma postfix_drop: "as >>= drop n as"
   320 unfolding postfix_def
   321 by (rule exI [where x = "take n as"]) simp
   322 
   323 lemma postfix_take: "xs >>= ys \<Longrightarrow> xs = take (length xs - length ys) xs @ ys"
   324 by (clarsimp elim!: postfixE)
   325 
   326 lemma parallelD1: "x \<parallel> y \<Longrightarrow> \<not> x \<le> y"
   327 by blast
   328 
   329 lemma parallelD2: "x \<parallel> y \<Longrightarrow> \<not> y \<le> x"
   330 by blast
   331 
   332 lemma parallel_Nil1 [simp]: "\<not> x \<parallel> []"
   333 unfolding parallel_def by simp
   334 
   335 lemma parallel_Nil2 [simp]: "\<not> [] \<parallel> x"
   336 unfolding parallel_def by simp
   337 
   338 lemma Cons_parallelI1: "a \<noteq> b \<Longrightarrow> a # as \<parallel> b # bs"
   339 by auto
   340 
   341 lemma Cons_parallelI2: "\<lbrakk> a = b; as \<parallel> bs \<rbrakk> \<Longrightarrow> a # as \<parallel> b # bs"
   342 by (metis Cons_prefix_Cons parallelE parallelI)
   343 
   344 lemma not_equal_is_parallel:
   345   assumes neq: "xs \<noteq> ys"
   346     and len: "length xs = length ys"
   347   shows "xs \<parallel> ys"
   348   using len neq
   349 proof (induct rule: list_induct2)
   350   case 1
   351   then show ?case by simp
   352 next
   353   case (2 a as b bs)
   354   have ih: "as \<noteq> bs \<Longrightarrow> as \<parallel> bs" by fact
   355   show ?case
   356   proof (cases "a = b")
   357     case True
   358     then have "as \<noteq> bs" using 2 by simp
   359     then show ?thesis by (rule Cons_parallelI2 [OF True ih])
   360   next
   361     case False
   362     then show ?thesis by (rule Cons_parallelI1)
   363   qed
   364 qed
   365 
   366 
   367 subsection {* Executable code *}
   368 
   369 lemma less_eq_code [code func]:
   370     "([]\<Colon>'a\<Colon>{eq, ord} list) \<le> xs \<longleftrightarrow> True"
   371     "(x\<Colon>'a\<Colon>{eq, ord}) # xs \<le> [] \<longleftrightarrow> False"
   372     "(x\<Colon>'a\<Colon>{eq, ord}) # xs \<le> y # ys \<longleftrightarrow> x = y \<and> xs \<le> ys"
   373   by simp_all
   374 
   375 lemma less_code [code func]:
   376     "xs < ([]\<Colon>'a\<Colon>{eq, ord} list) \<longleftrightarrow> False"
   377     "[] < (x\<Colon>'a\<Colon>{eq, ord})# xs \<longleftrightarrow> True"
   378     "(x\<Colon>'a\<Colon>{eq, ord}) # xs < y # ys \<longleftrightarrow> x = y \<and> xs < ys"
   379   unfolding strict_prefix_def by auto
   380 
   381 lemmas [code func] = postfix_to_prefix
   382 
   383 end