src/HOL/List.thy
changeset 45841 fe1ef1f3ee55
parent 45794 16e8e4d33c42
child 45891 d73605c829cc
equal deleted inserted replaced
45840:dadd139192d1 45841:fe1ef1f3ee55
  1352 lemma nth_map [simp]: "n < length xs ==> (map f xs)!n = f(xs!n)"
  1352 lemma nth_map [simp]: "n < length xs ==> (map f xs)!n = f(xs!n)"
  1353 apply (induct xs arbitrary: n, simp)
  1353 apply (induct xs arbitrary: n, simp)
  1354 apply (case_tac n, auto)
  1354 apply (case_tac n, auto)
  1355 done
  1355 done
  1356 
  1356 
       
  1357 lemma nth_tl:
       
  1358   assumes "n < length (tl x)" shows "tl x ! n = x ! Suc n"
       
  1359 using assms by (induct x) auto
       
  1360 
  1357 lemma hd_conv_nth: "xs \<noteq> [] \<Longrightarrow> hd xs = xs!0"
  1361 lemma hd_conv_nth: "xs \<noteq> [] \<Longrightarrow> hd xs = xs!0"
  1358 by(cases xs) simp_all
  1362 by(cases xs) simp_all
  1359 
  1363 
  1360 
  1364 
  1361 lemma list_eq_iff_nth_eq:
  1365 lemma list_eq_iff_nth_eq:
  1543 by(simp add:last_append)
  1547 by(simp add:last_append)
  1544 
  1548 
  1545 lemma last_appendR[simp]: "ys \<noteq> [] \<Longrightarrow> last(xs @ ys) = last ys"
  1549 lemma last_appendR[simp]: "ys \<noteq> [] \<Longrightarrow> last(xs @ ys) = last ys"
  1546 by(simp add:last_append)
  1550 by(simp add:last_append)
  1547 
  1551 
       
  1552 lemma last_tl: "xs = [] \<or> tl xs \<noteq> [] \<Longrightarrow>last (tl xs) = last xs"
       
  1553 by (induct xs) simp_all
       
  1554 
       
  1555 lemma butlast_tl: "butlast (tl xs) = tl (butlast xs)"
       
  1556 by (induct xs) simp_all
       
  1557 
  1548 lemma hd_rev: "xs \<noteq> [] \<Longrightarrow> hd(rev xs) = last xs"
  1558 lemma hd_rev: "xs \<noteq> [] \<Longrightarrow> hd(rev xs) = last xs"
  1549 by(rule rev_exhaust[of xs]) simp_all
  1559 by(rule rev_exhaust[of xs]) simp_all
  1550 
  1560 
  1551 lemma last_rev: "xs \<noteq> [] \<Longrightarrow> last(rev xs) = hd xs"
  1561 lemma last_rev: "xs \<noteq> [] \<Longrightarrow> last(rev xs) = hd xs"
  1552 by(cases xs) simp_all
  1562 by(cases xs) simp_all
  1575 lemma last_drop[simp]: "n < length xs \<Longrightarrow> last (drop n xs) = last xs"
  1585 lemma last_drop[simp]: "n < length xs \<Longrightarrow> last (drop n xs) = last xs"
  1576 apply (induct xs arbitrary: n)
  1586 apply (induct xs arbitrary: n)
  1577  apply simp
  1587  apply simp
  1578 apply (auto split:nat.split)
  1588 apply (auto split:nat.split)
  1579 done
  1589 done
       
  1590 
       
  1591 lemma nth_butlast:
       
  1592   assumes "n < length (butlast xs)" shows "butlast xs ! n = xs ! n"
       
  1593 proof (cases xs)
       
  1594   case (Cons y ys)
       
  1595   moreover from assms have "butlast xs ! n = (butlast xs @ [last xs]) ! n"
       
  1596     by (simp add: nth_append)
       
  1597   ultimately show ?thesis using append_butlast_last_id by simp
       
  1598 qed simp
  1580 
  1599 
  1581 lemma last_conv_nth: "xs\<noteq>[] \<Longrightarrow> last xs = xs!(length xs - 1)"
  1600 lemma last_conv_nth: "xs\<noteq>[] \<Longrightarrow> last xs = xs!(length xs - 1)"
  1582 by(induct xs)(auto simp:neq_Nil_conv)
  1601 by(induct xs)(auto simp:neq_Nil_conv)
  1583 
  1602 
  1584 lemma butlast_conv_take: "butlast xs = take (length xs - 1) xs"
  1603 lemma butlast_conv_take: "butlast xs = take (length xs - 1) xs"
  1896 by (induct xs) auto
  1915 by (induct xs) auto
  1897 
  1916 
  1898 lemma dropWhile_append2 [simp]:
  1917 lemma dropWhile_append2 [simp]:
  1899 "(!!x. x:set xs ==> P(x)) ==> dropWhile P (xs @ ys) = dropWhile P ys"
  1918 "(!!x. x:set xs ==> P(x)) ==> dropWhile P (xs @ ys) = dropWhile P ys"
  1900 by (induct xs) auto
  1919 by (induct xs) auto
       
  1920 
       
  1921 lemma dropWhile_append3:
       
  1922   "\<not> P y \<Longrightarrow>dropWhile P (xs @ y # ys) = dropWhile P xs @ y # ys"
       
  1923 by (induct xs) auto
       
  1924 
       
  1925 lemma dropWhile_last:
       
  1926   "x \<in> set xs \<Longrightarrow> \<not> P x \<Longrightarrow> last (dropWhile P xs) = last xs"
       
  1927 by (auto simp add: dropWhile_append3 in_set_conv_decomp)
       
  1928 
       
  1929 lemma set_dropWhileD: "x \<in> set (dropWhile P xs) \<Longrightarrow> x \<in> set xs"
       
  1930 by (induct xs) (auto split: split_if_asm)
  1901 
  1931 
  1902 lemma set_takeWhileD: "x : set (takeWhile P xs) ==> x : set xs \<and> P x"
  1932 lemma set_takeWhileD: "x : set (takeWhile P xs) ==> x : set xs \<and> P x"
  1903 by (induct xs) (auto split: split_if_asm)
  1933 by (induct xs) (auto split: split_if_asm)
  1904 
  1934 
  1905 lemma takeWhile_eq_all_conv[simp]:
  1935 lemma takeWhile_eq_all_conv[simp]:
  2887 apply (induct n == "length ws" arbitrary:ws) apply simp
  2917 apply (induct n == "length ws" arbitrary:ws) apply simp
  2888 apply(case_tac ws) apply simp
  2918 apply(case_tac ws) apply simp
  2889 apply (simp split:split_if_asm)
  2919 apply (simp split:split_if_asm)
  2890 apply (metis Cons_eq_appendI eq_Nil_appendI split_list)
  2920 apply (metis Cons_eq_appendI eq_Nil_appendI split_list)
  2891 done
  2921 done
       
  2922 
       
  2923 lemma not_distinct_conv_prefix:
       
  2924   defines "dec as xs y ys \<equiv> y \<in> set xs \<and> distinct xs \<and> as = xs @ y # ys"
       
  2925   shows "\<not>distinct as \<longleftrightarrow> (\<exists>xs y ys. dec as xs y ys)" (is "?L = ?R")
       
  2926 proof
       
  2927   assume "?L" then show "?R"
       
  2928   proof (induct "length as" arbitrary: as rule: less_induct)
       
  2929     case less
       
  2930     obtain xs ys zs y where decomp: "as = (xs @ y # ys) @ y # zs"
       
  2931       using not_distinct_decomp[OF less.prems] by auto
       
  2932     show ?case
       
  2933     proof (cases "distinct (xs @ y # ys)")
       
  2934       case True
       
  2935       with decomp have "dec as (xs @ y # ys) y zs" by (simp add: dec_def)
       
  2936       then show ?thesis by blast
       
  2937     next
       
  2938       case False
       
  2939       with less decomp obtain xs' y' ys' where "dec (xs @ y # ys) xs' y' ys'"
       
  2940         by atomize_elim auto
       
  2941       with decomp have "dec as xs' y' (ys' @ y # zs)" by (simp add: dec_def)
       
  2942       then show ?thesis by blast
       
  2943     qed
       
  2944   qed
       
  2945 qed (auto simp: dec_def)
  2892 
  2946 
  2893 lemma length_remdups_concat:
  2947 lemma length_remdups_concat:
  2894   "length (remdups (concat xss)) = card (\<Union>xs\<in>set xss. set xs)"
  2948   "length (remdups (concat xss)) = card (\<Union>xs\<in>set xss. set xs)"
  2895   by (simp add: distinct_card [symmetric])
  2949   by (simp add: distinct_card [symmetric])
  2896 
  2950