src/HOL/Library/List_Prefix.thy
author nipkow
Thu Dec 06 19:58:21 2007 +0100 (2007-12-06)
changeset 25564 4ca31a3706a4
parent 25356 059c03630d6e
child 25595 6c48275f9c76
permissions -rw-r--r--
R&F: added sgn lemma
Prefix: sledge-hammered
     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 Main
    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 (*
    68   proof (cases zs rule: rev_cases)
    69     assume "zs = []"
    70     with zs have "xs = ys @ [y]" by simp
    71     then show ?thesis ..
    72   next
    73     fix z zs' assume "zs = zs' @ [z]"
    74     with zs have "ys = xs @ zs'" by simp
    75     then have "xs \<le> ys" ..
    76     then show ?thesis ..
    77   qed
    78 *)
    79 next
    80   assume "xs = ys @ [y] \<or> xs \<le> ys"
    81   then show "xs \<le> ys @ [y]"
    82     by (metis order_eq_iff strict_prefixE strict_prefixI' xt1(7))
    83 (*
    84   proof
    85     assume "xs = ys @ [y]"
    86     then show ?thesis by simp
    87   next
    88     assume "xs \<le> ys"
    89     then obtain zs where "ys = xs @ zs" ..
    90     then have "ys @ [y] = xs @ (zs @ [y])" by simp
    91     then show ?thesis ..
    92   qed
    93 *)
    94 qed
    95 
    96 lemma Cons_prefix_Cons [simp]: "(x # xs \<le> y # ys) = (x = y \<and> xs \<le> ys)"
    97   by (auto simp add: prefix_def)
    98 
    99 lemma same_prefix_prefix [simp]: "(xs @ ys \<le> xs @ zs) = (ys \<le> zs)"
   100   by (induct xs) simp_all
   101 
   102 lemma same_prefix_nil [iff]: "(xs @ ys \<le> xs) = (ys = [])"
   103 by (metis append_Nil2 append_self_conv order_eq_iff prefixI)
   104 (*
   105 proof -
   106   have "(xs @ ys \<le> xs @ []) = (ys \<le> [])" by (rule same_prefix_prefix)
   107   then show ?thesis by simp
   108 qed
   109 *)
   110 lemma prefix_prefix [simp]: "xs \<le> ys ==> xs \<le> ys @ zs"
   111 by (metis order_le_less_trans prefixI strict_prefixE strict_prefixI)
   112 (*
   113 proof -
   114   assume "xs \<le> ys"
   115   then obtain us where "ys = xs @ us" ..
   116   then have "ys @ zs = xs @ (us @ zs)" by simp
   117   then show ?thesis ..
   118 qed
   119 *)
   120 lemma append_prefixD: "xs @ ys \<le> zs \<Longrightarrow> xs \<le> zs"
   121   by (auto simp add: prefix_def)
   122 
   123 theorem prefix_Cons: "(xs \<le> y # ys) = (xs = [] \<or> (\<exists>zs. xs = y # zs \<and> zs \<le> ys))"
   124   by (cases xs) (auto simp add: prefix_def)
   125 
   126 theorem prefix_append:
   127   "(xs \<le> ys @ zs) = (xs \<le> ys \<or> (\<exists>us. xs = ys @ us \<and> us \<le> zs))"
   128   apply (induct zs rule: rev_induct)
   129    apply force
   130   apply (simp del: append_assoc add: append_assoc [symmetric])
   131   apply (metis append_eq_appendI)
   132 (*
   133   apply simp
   134   apply blast
   135 *)
   136   done
   137 
   138 lemma append_one_prefix:
   139   "xs \<le> ys ==> length xs < length ys ==> xs @ [ys ! length xs] \<le> ys"
   140 by (unfold prefix_def)
   141    (metis Cons_eq_appendI append_eq_appendI append_eq_conv_conj eq_Nil_appendI nth_drop')
   142 (*
   143   apply (auto simp add: nth_append)
   144   apply (case_tac zs)
   145    apply auto
   146   done
   147 *)
   148 theorem prefix_length_le: "xs \<le> ys ==> length xs \<le> length ys"
   149   by (auto simp add: prefix_def)
   150 
   151 lemma prefix_same_cases:
   152   "(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"
   153 by (unfold prefix_def) (metis append_eq_append_conv2)
   154 (*
   155   apply (erule exE)+
   156   apply (simp add: append_eq_append_conv_if split: if_splits)
   157    apply (rule disjI2)
   158    apply (rule_tac x = "drop (size xs\<^isub>2) xs\<^isub>1" in exI)
   159    apply clarify
   160    apply (drule sym)
   161    apply (insert append_take_drop_id [of "length xs\<^isub>2" xs\<^isub>1])
   162    apply simp
   163   apply (rule disjI1)
   164   apply (rule_tac x = "drop (size xs\<^isub>1) xs\<^isub>2" in exI)
   165   apply clarify
   166   apply (insert append_take_drop_id [of "length xs\<^isub>1" xs\<^isub>2])
   167   apply simp
   168   done
   169 *)
   170 lemma set_mono_prefix: "xs \<le> ys \<Longrightarrow> set xs \<subseteq> set ys"
   171 by (auto simp add: prefix_def)
   172 
   173 lemma take_is_prefix: "take n xs \<le> xs"
   174 by (unfold prefix_def) (metis append_take_drop_id)
   175 (*
   176   apply (rule_tac x="drop n xs" in exI)
   177   apply simp
   178   done
   179 *)
   180 lemma map_prefixI:
   181   "xs \<le> ys \<Longrightarrow> map f xs \<le> map f ys"
   182 by (clarsimp simp: prefix_def)
   183 
   184 lemma prefix_length_less:
   185   "xs < ys \<Longrightarrow> length xs < length ys"
   186 by (clarsimp simp: strict_prefix_def prefix_def)
   187 (*
   188   apply (frule prefix_length_le)
   189   apply (rule ccontr, simp)
   190   apply (clarsimp simp: prefix_def)
   191   done
   192 *)
   193 lemma strict_prefix_simps [simp]:
   194   "xs < [] = False"
   195   "[] < (x # xs) = True"
   196   "(x # xs) < (y # ys) = (x = y \<and> xs < ys)"
   197 by (simp_all add: strict_prefix_def cong: conj_cong)
   198 
   199 lemma take_strict_prefix: "xs < ys \<Longrightarrow> take n xs < ys"
   200 apply (induct n arbitrary: xs ys)
   201  apply (case_tac ys, simp_all)[1]
   202 apply (metis order_less_trans strict_prefixI take_is_prefix)
   203 (*
   204   apply (case_tac xs, simp)
   205   apply (case_tac ys, simp_all)
   206 *)
   207 done
   208 
   209 lemma not_prefix_cases:
   210   assumes pfx: "\<not> ps \<le> ls"
   211   obtains
   212     (c1) "ps \<noteq> []" and "ls = []"
   213   | (c2) a as x xs where "ps = a#as" and "ls = x#xs" and "x = a" and "\<not> as \<le> xs"
   214   | (c3) a as x xs where "ps = a#as" and "ls = x#xs" and "x \<noteq> a"
   215 proof (cases ps)
   216   case Nil thus ?thesis using pfx by simp
   217 next
   218   case (Cons a as)
   219   hence c: "ps = a#as" .
   220   show ?thesis
   221   proof (cases ls)
   222     case Nil thus ?thesis by (metis append_Nil2 pfx c1 same_prefix_nil)
   223 (*
   224     have "ps \<noteq> []" by (simp add: Nil Cons)
   225     from this and Nil show ?thesis by (rule c1)
   226 *)
   227   next
   228     case (Cons x xs)
   229     show ?thesis
   230     proof (cases "x = a")
   231       case True
   232       have "\<not> as \<le> xs" using pfx c Cons True by simp
   233       with c Cons True show ?thesis by (rule c2)
   234     next
   235       case False
   236       with c Cons show ?thesis by (rule c3)
   237     qed
   238   qed
   239 qed
   240 
   241 lemma not_prefix_induct [consumes 1, case_names Nil Neq Eq]:
   242   assumes np: "\<not> ps \<le> ls"
   243     and base: "\<And>x xs. P (x#xs) []"
   244     and r1: "\<And>x xs y ys. x \<noteq> y \<Longrightarrow> P (x#xs) (y#ys)"
   245     and r2: "\<And>x xs y ys. \<lbrakk> x = y; \<not> xs \<le> ys; P xs ys \<rbrakk> \<Longrightarrow> P (x#xs) (y#ys)"
   246   shows "P ps ls" using np
   247 proof (induct ls arbitrary: ps)
   248   case Nil then show ?case
   249     by (auto simp: neq_Nil_conv elim!: not_prefix_cases intro!: base)
   250 next
   251   case (Cons y ys)
   252   then have npfx: "\<not> ps \<le> (y # ys)" by simp
   253   then obtain x xs where pv: "ps = x # xs"
   254     by (rule not_prefix_cases) auto
   255   show ?case by (metis Cons.hyps Cons_prefix_Cons npfx pv r1 r2)
   256 (*
   257   from Cons
   258   have ih: "\<And>ps. \<not>ps \<le> ys \<Longrightarrow> P ps ys" by simp
   259 
   260   show ?case using npfx
   261     by (simp only: pv) (erule not_prefix_cases, auto intro: r1 r2 ih)
   262 *)
   263 qed
   264 
   265 
   266 subsection {* Parallel lists *}
   267 
   268 definition
   269   parallel :: "'a list => 'a list => bool"  (infixl "\<parallel>" 50) where
   270   "(xs \<parallel> ys) = (\<not> xs \<le> ys \<and> \<not> ys \<le> xs)"
   271 
   272 lemma parallelI [intro]: "\<not> xs \<le> ys ==> \<not> ys \<le> xs ==> xs \<parallel> ys"
   273 unfolding parallel_def by blast
   274 
   275 lemma parallelE [elim]:
   276 assumes "xs \<parallel> ys"
   277 obtains "\<not> xs \<le> ys \<and> \<not> ys \<le> xs"
   278 using assms unfolding parallel_def by blast
   279 
   280 theorem prefix_cases:
   281 obtains "xs \<le> ys" | "ys < xs" | "xs \<parallel> ys"
   282 unfolding parallel_def strict_prefix_def by blast
   283 
   284 theorem parallel_decomp:
   285   "xs \<parallel> ys ==> \<exists>as b bs c cs. b \<noteq> c \<and> xs = as @ b # bs \<and> ys = as @ c # cs"
   286 proof (induct xs rule: rev_induct)
   287   case Nil
   288   then have False by auto
   289   then show ?case ..
   290 next
   291   case (snoc x xs)
   292   show ?case
   293   proof (rule prefix_cases)
   294     assume le: "xs \<le> ys"
   295     then obtain ys' where ys: "ys = xs @ ys'" ..
   296     show ?thesis
   297     proof (cases ys')
   298       assume "ys' = []"
   299       thus ?thesis by (metis append_Nil2 parallelE prefixI snoc.prems ys)
   300 (*
   301       with ys have "xs = ys" by simp
   302       with snoc have "[x] \<parallel> []" by auto
   303       then have False by blast
   304       then show ?thesis ..
   305 *)
   306     next
   307       fix c cs assume ys': "ys' = c # cs"
   308       thus ?thesis
   309 	by (metis Cons_eq_appendI eq_Nil_appendI parallelE prefixI same_prefix_prefix snoc.prems ys)
   310 (*
   311       with snoc ys have "xs @ [x] \<parallel> xs @ c # cs" by (simp only:)
   312       then have "x \<noteq> c" by auto
   313       moreover have "xs @ [x] = xs @ x # []" by simp
   314       moreover from ys ys' have "ys = xs @ c # cs" by (simp only:)
   315       ultimately show ?thesis by blast
   316 *)
   317     qed
   318   next
   319     assume "ys < xs" then have "ys \<le> xs @ [x]" by (simp add: strict_prefix_def)
   320     with snoc have False by blast
   321     then show ?thesis ..
   322   next
   323     assume "xs \<parallel> ys"
   324     with snoc obtain as b bs c cs where neq: "(b::'a) \<noteq> c"
   325       and xs: "xs = as @ b # bs" and ys: "ys = as @ c # cs"
   326       by blast
   327     from xs have "xs @ [x] = as @ b # (bs @ [x])" by simp
   328     with neq ys show ?thesis by blast
   329   qed
   330 qed
   331 
   332 lemma parallel_append: "a \<parallel> b \<Longrightarrow> a @ c \<parallel> b @ d"
   333 by (rule parallelI)
   334    (erule parallelE, erule conjE,
   335           induct rule: not_prefix_induct, simp+)+
   336 
   337 lemma parallel_appendI: "\<lbrakk> xs \<parallel> ys; x = xs @ xs' ; y = ys @ ys' \<rbrakk> \<Longrightarrow> x \<parallel> y"
   338 by simp (rule parallel_append)
   339 
   340 lemma parallel_commute: "(a \<parallel> b) = (b \<parallel> a)"
   341 unfolding parallel_def by auto
   342 
   343 
   344 subsection {* Postfix order on lists *}
   345 
   346 definition
   347   postfix :: "'a list => 'a list => bool"  ("(_/ >>= _)" [51, 50] 50) where
   348   "(xs >>= ys) = (\<exists>zs. xs = zs @ ys)"
   349 
   350 lemma postfixI [intro?]: "xs = zs @ ys ==> xs >>= ys"
   351 unfolding postfix_def by blast
   352 
   353 lemma postfixE [elim?]:
   354 assumes "xs >>= ys"
   355 obtains zs where "xs = zs @ ys"
   356 using assms unfolding postfix_def by blast
   357 
   358 lemma postfix_refl [iff]: "xs >>= xs"
   359   by (auto simp add: postfix_def)
   360 lemma postfix_trans: "\<lbrakk>xs >>= ys; ys >>= zs\<rbrakk> \<Longrightarrow> xs >>= zs"
   361   by (auto simp add: postfix_def)
   362 lemma postfix_antisym: "\<lbrakk>xs >>= ys; ys >>= xs\<rbrakk> \<Longrightarrow> xs = ys"
   363   by (auto simp add: postfix_def)
   364 
   365 lemma Nil_postfix [iff]: "xs >>= []"
   366   by (simp add: postfix_def)
   367 lemma postfix_Nil [simp]: "([] >>= xs) = (xs = [])"
   368   by (auto simp add: postfix_def)
   369 
   370 lemma postfix_ConsI: "xs >>= ys \<Longrightarrow> x#xs >>= ys"
   371   by (auto simp add: postfix_def)
   372 lemma postfix_ConsD: "xs >>= y#ys \<Longrightarrow> xs >>= ys"
   373   by (auto simp add: postfix_def)
   374 
   375 lemma postfix_appendI: "xs >>= ys \<Longrightarrow> zs @ xs >>= ys"
   376   by (auto simp add: postfix_def)
   377 lemma postfix_appendD: "xs >>= zs @ ys \<Longrightarrow> xs >>= ys"
   378   by (auto simp add: postfix_def)
   379 
   380 lemma postfix_is_subset: "xs >>= ys ==> set ys \<subseteq> set xs"
   381 proof -
   382   assume "xs >>= ys"
   383   then obtain zs where "xs = zs @ ys" ..
   384   then show ?thesis by (induct zs) auto
   385 qed
   386 
   387 lemma postfix_ConsD2: "x#xs >>= y#ys ==> xs >>= ys"
   388 proof -
   389   assume "x#xs >>= y#ys"
   390   then obtain zs where "x#xs = zs @ y#ys" ..
   391   then show ?thesis
   392     by (induct zs) (auto intro!: postfix_appendI postfix_ConsI)
   393 qed
   394 
   395 lemma postfix_to_prefix: "xs >>= ys \<longleftrightarrow> rev ys \<le> rev xs"
   396 proof
   397   assume "xs >>= ys"
   398   then obtain zs where "xs = zs @ ys" ..
   399   then have "rev xs = rev ys @ rev zs" by simp
   400   then show "rev ys <= rev xs" ..
   401 next
   402   assume "rev ys <= rev xs"
   403   then obtain zs where "rev xs = rev ys @ zs" ..
   404   then have "rev (rev xs) = rev zs @ rev (rev ys)" by simp
   405   then have "xs = rev zs @ ys" by simp
   406   then show "xs >>= ys" ..
   407 qed
   408 
   409 lemma distinct_postfix: "distinct xs \<Longrightarrow> xs >>= ys \<Longrightarrow> distinct ys"
   410 by (clarsimp elim!: postfixE)
   411 
   412 lemma postfix_map: "xs >>= ys \<Longrightarrow> map f xs >>= map f ys"
   413 by (auto elim!: postfixE intro: postfixI)
   414 
   415 lemma postfix_drop: "as >>= drop n as"
   416 unfolding postfix_def
   417 by (rule exI [where x = "take n as"]) simp
   418 
   419 lemma postfix_take: "xs >>= ys \<Longrightarrow> xs = take (length xs - length ys) xs @ ys"
   420 by (clarsimp elim!: postfixE)
   421 
   422 lemma parallelD1: "x \<parallel> y \<Longrightarrow> \<not> x \<le> y"
   423 by blast
   424 
   425 lemma parallelD2: "x \<parallel> y \<Longrightarrow> \<not> y \<le> x"
   426 by blast
   427 
   428 lemma parallel_Nil1 [simp]: "\<not> x \<parallel> []"
   429 unfolding parallel_def by simp
   430 
   431 lemma parallel_Nil2 [simp]: "\<not> [] \<parallel> x"
   432 unfolding parallel_def by simp
   433 
   434 lemma Cons_parallelI1: "a \<noteq> b \<Longrightarrow> a # as \<parallel> b # bs"
   435 by auto
   436 
   437 lemma Cons_parallelI2: "\<lbrakk> a = b; as \<parallel> bs \<rbrakk> \<Longrightarrow> a # as \<parallel> b # bs"
   438 by (metis Cons_prefix_Cons parallelE parallelI)
   439 (*
   440   apply simp
   441   apply (rule parallelI)
   442    apply simp
   443    apply (erule parallelD1)
   444   apply simp
   445   apply (erule parallelD2)
   446  done
   447 *)
   448 lemma not_equal_is_parallel:
   449   assumes neq: "xs \<noteq> ys"
   450     and len: "length xs = length ys"
   451   shows "xs \<parallel> ys"
   452   using len neq
   453 proof (induct rule: list_induct2)
   454   case 1
   455   then show ?case by simp
   456 next
   457   case (2 a as b bs)
   458   have ih: "as \<noteq> bs \<Longrightarrow> as \<parallel> bs" by fact
   459   show ?case
   460   proof (cases "a = b")
   461     case True
   462     then have "as \<noteq> bs" using 2 by simp
   463     then show ?thesis by (rule Cons_parallelI2 [OF True ih])
   464   next
   465     case False
   466     then show ?thesis by (rule Cons_parallelI1)
   467   qed
   468 qed
   469 
   470 
   471 subsection {* Executable code *}
   472 
   473 lemma less_eq_code [code func]:
   474     "([]\<Colon>'a\<Colon>{eq, ord} list) \<le> xs \<longleftrightarrow> True"
   475     "(x\<Colon>'a\<Colon>{eq, ord}) # xs \<le> [] \<longleftrightarrow> False"
   476     "(x\<Colon>'a\<Colon>{eq, ord}) # xs \<le> y # ys \<longleftrightarrow> x = y \<and> xs \<le> ys"
   477   by simp_all
   478 
   479 lemma less_code [code func]:
   480     "xs < ([]\<Colon>'a\<Colon>{eq, ord} list) \<longleftrightarrow> False"
   481     "[] < (x\<Colon>'a\<Colon>{eq, ord})# xs \<longleftrightarrow> True"
   482     "(x\<Colon>'a\<Colon>{eq, ord}) # xs < y # ys \<longleftrightarrow> x = y \<and> xs < ys"
   483   unfolding strict_prefix_def by auto
   484 
   485 lemmas [code func] = postfix_to_prefix
   486 
   487 end