src/HOL/Library/Sublist.thy
changeset 49082 d71cdd1171c3
parent 45236 ac4a2a66707d
equal deleted inserted replaced
49081:092668a120cc 49082:d71cdd1171c3
   263 
   263 
   264 definition
   264 definition
   265   suffixeq :: "'a list => 'a list => bool" where
   265   suffixeq :: "'a list => 'a list => bool" where
   266   "suffixeq xs ys = (\<exists>zs. ys = zs @ xs)"
   266   "suffixeq xs ys = (\<exists>zs. ys = zs @ xs)"
   267 
   267 
       
   268 definition suffix :: "'a list \<Rightarrow> 'a list \<Rightarrow> bool" where
       
   269   "suffix xs ys \<equiv> \<exists>us. ys = us @ xs \<and> us \<noteq> []"
       
   270 
       
   271 lemma suffix_imp_suffixeq:
       
   272   "suffix xs ys \<Longrightarrow> suffixeq xs ys"
       
   273   by (auto simp: suffixeq_def suffix_def)
       
   274 
   268 lemma suffixeqI [intro?]: "ys = zs @ xs ==> suffixeq xs ys"
   275 lemma suffixeqI [intro?]: "ys = zs @ xs ==> suffixeq xs ys"
   269   unfolding suffixeq_def by blast
   276   unfolding suffixeq_def by blast
   270 
   277 
   271 lemma suffixeqE [elim?]:
   278 lemma suffixeqE [elim?]:
   272   assumes "suffixeq xs ys"
   279   assumes "suffixeq xs ys"
   273   obtains zs where "ys = zs @ xs"
   280   obtains zs where "ys = zs @ xs"
   274   using assms unfolding suffixeq_def by blast
   281   using assms unfolding suffixeq_def by blast
   275 
   282 
   276 lemma suffixeq_refl [iff]: "suffixeq xs xs"
   283 lemma suffixeq_refl [iff]: "suffixeq xs xs"
   277   by (auto simp add: suffixeq_def)
   284   by (auto simp add: suffixeq_def)
       
   285 lemma suffix_trans:
       
   286   "suffix xs ys \<Longrightarrow> suffix ys zs \<Longrightarrow> suffix xs zs"
       
   287   by (auto simp: suffix_def)
   278 lemma suffixeq_trans: "\<lbrakk>suffixeq xs ys; suffixeq ys zs\<rbrakk> \<Longrightarrow> suffixeq xs zs"
   288 lemma suffixeq_trans: "\<lbrakk>suffixeq xs ys; suffixeq ys zs\<rbrakk> \<Longrightarrow> suffixeq xs zs"
   279   by (auto simp add: suffixeq_def)
   289   by (auto simp add: suffixeq_def)
   280 lemma suffixeq_antisym: "\<lbrakk>suffixeq xs ys; suffixeq ys xs\<rbrakk> \<Longrightarrow> xs = ys"
   290 lemma suffixeq_antisym: "\<lbrakk>suffixeq xs ys; suffixeq ys xs\<rbrakk> \<Longrightarrow> xs = ys"
   281   by (auto simp add: suffixeq_def)
   291   by (auto simp add: suffixeq_def)
       
   292 
       
   293 lemma suffixeq_tl [simp]: "suffixeq (tl xs) xs"
       
   294   by (induct xs) (auto simp: suffixeq_def)
       
   295 
       
   296 lemma suffix_tl [simp]: "xs \<noteq> [] \<Longrightarrow> suffix (tl xs) xs"
       
   297   by (induct xs) (auto simp: suffix_def)
   282 
   298 
   283 lemma Nil_suffixeq [iff]: "suffixeq [] xs"
   299 lemma Nil_suffixeq [iff]: "suffixeq [] xs"
   284   by (simp add: suffixeq_def)
   300   by (simp add: suffixeq_def)
   285 lemma suffixeq_Nil [simp]: "(suffixeq xs []) = (xs = [])"
   301 lemma suffixeq_Nil [simp]: "(suffixeq xs []) = (xs = [])"
   286   by (auto simp add: suffixeq_def)
   302   by (auto simp add: suffixeq_def)
   293 lemma suffixeq_appendI: "suffixeq xs ys \<Longrightarrow> suffixeq xs (zs @ ys)"
   309 lemma suffixeq_appendI: "suffixeq xs ys \<Longrightarrow> suffixeq xs (zs @ ys)"
   294   by (auto simp add: suffixeq_def)
   310   by (auto simp add: suffixeq_def)
   295 lemma suffixeq_appendD: "suffixeq (zs @ xs) ys \<Longrightarrow> suffixeq xs ys"
   311 lemma suffixeq_appendD: "suffixeq (zs @ xs) ys \<Longrightarrow> suffixeq xs ys"
   296   by (auto simp add: suffixeq_def)
   312   by (auto simp add: suffixeq_def)
   297 
   313 
   298 lemma suffixeq_is_subset: "suffixeq xs ys ==> set xs \<subseteq> set ys"
   314 lemma suffix_set_subset:
   299 proof -
   315   "suffix xs ys \<Longrightarrow> set xs \<subseteq> set ys" by (auto simp: suffix_def)
   300   assume "suffixeq xs ys"
   316 
   301   then obtain zs where "ys = zs @ xs" ..
   317 lemma suffixeq_set_subset:
   302   then show ?thesis by (induct zs) auto
   318   "suffixeq xs ys \<Longrightarrow> set xs \<subseteq> set ys" by (auto simp: suffixeq_def)
   303 qed
       
   304 
   319 
   305 lemma suffixeq_ConsD2: "suffixeq (x#xs) (y#ys) ==> suffixeq xs ys"
   320 lemma suffixeq_ConsD2: "suffixeq (x#xs) (y#ys) ==> suffixeq xs ys"
   306 proof -
   321 proof -
   307   assume "suffixeq (x#xs) (y#ys)"
   322   assume "suffixeq (x#xs) (y#ys)"
   308   then obtain zs where "y#ys = zs @ x#xs" ..
   323   then obtain zs where "y#ys = zs @ x#xs" ..
   336   apply simp
   351   apply simp
   337   done
   352   done
   338 
   353 
   339 lemma suffixeq_take: "suffixeq xs ys \<Longrightarrow> ys = take (length ys - length xs) ys @ xs"
   354 lemma suffixeq_take: "suffixeq xs ys \<Longrightarrow> ys = take (length ys - length xs) ys @ xs"
   340   by (clarsimp elim!: suffixeqE)
   355   by (clarsimp elim!: suffixeqE)
       
   356 
       
   357 lemma suffixeq_suffix_reflclp_conv:
       
   358   "suffixeq = suffix\<^sup>=\<^sup>="
       
   359 proof (intro ext iffI)
       
   360   fix xs ys :: "'a list"
       
   361   assume "suffixeq xs ys"
       
   362   show "suffix\<^sup>=\<^sup>= xs ys"
       
   363   proof
       
   364     assume "xs \<noteq> ys"
       
   365     with `suffixeq xs ys` show "suffix xs ys" by (auto simp: suffixeq_def suffix_def)
       
   366   qed
       
   367 next
       
   368   fix xs ys :: "'a list"
       
   369   assume "suffix\<^sup>=\<^sup>= xs ys"
       
   370   thus "suffixeq xs ys"
       
   371   proof
       
   372     assume "suffix xs ys" thus "suffixeq xs ys" by (rule suffix_imp_suffixeq)
       
   373   next
       
   374     assume "xs = ys" thus "suffixeq xs ys" by (auto simp: suffixeq_def)
       
   375   qed
       
   376 qed
   341 
   377 
   342 lemma parallelD1: "x \<parallel> y \<Longrightarrow> \<not> x \<le> y"
   378 lemma parallelD1: "x \<parallel> y \<Longrightarrow> \<not> x \<le> y"
   343   by blast
   379   by blast
   344 
   380 
   345 lemma parallelD2: "x \<parallel> y \<Longrightarrow> \<not> y \<le> x"
   381 lemma parallelD2: "x \<parallel> y \<Longrightarrow> \<not> y \<le> x"
   377     case False
   413     case False
   378     then show ?thesis by (rule Cons_parallelI1)
   414     then show ?thesis by (rule Cons_parallelI1)
   379   qed
   415   qed
   380 qed
   416 qed
   381 
   417 
       
   418 lemma suffix_reflclp_conv:
       
   419   "suffix\<^sup>=\<^sup>= = suffixeq"
       
   420   by (intro ext) (auto simp: suffixeq_def suffix_def)
       
   421 
       
   422 lemma suffix_lists:
       
   423   "suffix xs ys \<Longrightarrow> ys \<in> lists A \<Longrightarrow> xs \<in> lists A"
       
   424   unfolding suffix_def by auto
       
   425 
   382 
   426 
   383 subsection {* Embedding on lists *}
   427 subsection {* Embedding on lists *}
   384 
   428 
   385 inductive
   429 inductive
   386   emb :: "('a \<Rightarrow> 'a \<Rightarrow> bool) \<Rightarrow> 'a list \<Rightarrow> 'a list \<Rightarrow> bool"
   430   emb :: "('a \<Rightarrow> 'a \<Rightarrow> bool) \<Rightarrow> 'a list \<Rightarrow> 'a list \<Rightarrow> bool"
   425   then obtain us v vs where "zs = us @ v # vs"
   469   then obtain us v vs where "zs = us @ v # vs"
   426     and "P x v" and "emb P (xs @ ys) vs" by (auto dest: emb_ConsD)
   470     and "P x v" and "emb P (xs @ ys) vs" by (auto dest: emb_ConsD)
   427   with Cons show ?case by (metis append_Cons append_assoc emb_Cons2 emb_append2)
   471   with Cons show ?case by (metis append_Cons append_assoc emb_Cons2 emb_append2)
   428 qed
   472 qed
   429 
   473 
       
   474 lemma emb_suffix:
       
   475   assumes "emb P xs ys" and "suffix ys zs"
       
   476   shows "emb P xs zs"
       
   477   using assms(2) and emb_append2 [OF assms(1)] by (auto simp: suffix_def)
       
   478 
       
   479 lemma emb_suffixeq:
       
   480   assumes "emb P xs ys" and "suffixeq ys zs"
       
   481   shows "emb P xs zs"
       
   482   using assms and emb_suffix unfolding suffixeq_suffix_reflclp_conv by auto
       
   483 
   430 end
   484 end