src/HOL/List.thy
changeset 58437 8d124c73c37a
parent 58310 91ea607a34d8
child 58807 5b068376ff20
equal deleted inserted replaced
58436:fe9de4089345 58437:8d124c73c37a
  2794 
  2794 
  2795 lemma fold_map [code_unfold]:
  2795 lemma fold_map [code_unfold]:
  2796   "fold g (map f xs) = fold (g o f) xs"
  2796   "fold g (map f xs) = fold (g o f) xs"
  2797   by (induct xs) simp_all
  2797   by (induct xs) simp_all
  2798 
  2798 
       
  2799 lemma fold_filter:
       
  2800   "fold f (filter P xs) = fold (\<lambda>x. if P x then f x else id) xs"
       
  2801   by (induct xs) simp_all
       
  2802 
  2799 lemma fold_rev:
  2803 lemma fold_rev:
  2800   assumes "\<And>x y. x \<in> set xs \<Longrightarrow> y \<in> set xs \<Longrightarrow> f y \<circ> f x = f x \<circ> f y"
  2804   assumes "\<And>x y. x \<in> set xs \<Longrightarrow> y \<in> set xs \<Longrightarrow> f y \<circ> f x = f x \<circ> f y"
  2801   shows "fold f (rev xs) = fold f xs"
  2805   shows "fold f (rev xs) = fold f xs"
  2802 using assms by (induct xs) (simp_all add: fold_commute_apply fun_eq_iff)
  2806 using assms by (induct xs) (simp_all add: fold_commute_apply fun_eq_iff)
  2803 
  2807 
  2940 
  2944 
  2941 lemma foldr_map [code_unfold]:
  2945 lemma foldr_map [code_unfold]:
  2942   "foldr g (map f xs) a = foldr (g o f) xs a"
  2946   "foldr g (map f xs) a = foldr (g o f) xs a"
  2943   by (simp add: foldr_conv_fold fold_map rev_map)
  2947   by (simp add: foldr_conv_fold fold_map rev_map)
  2944 
  2948 
       
  2949 lemma foldr_filter:
       
  2950   "foldr f (filter P xs) = foldr (\<lambda>x. if P x then f x else id) xs"
       
  2951   by (simp add: foldr_conv_fold rev_filter fold_filter)
       
  2952   
  2945 lemma foldl_map [code_unfold]:
  2953 lemma foldl_map [code_unfold]:
  2946   "foldl g a (map f xs) = foldl (\<lambda>a x. g a (f x)) a xs"
  2954   "foldl g a (map f xs) = foldl (\<lambda>a x. g a (f x)) a xs"
  2947   by (simp add: foldl_conv_fold fold_map comp_def)
  2955   by (simp add: foldl_conv_fold fold_map comp_def)
  2948 
  2956 
  2949 lemma concat_conv_foldr [code]:
  2957 lemma concat_conv_foldr [code]:
  3028 
  3036 
  3029 lemma map_decr_upt:
  3037 lemma map_decr_upt:
  3030   "map (\<lambda>n. n - Suc 0) [Suc m..<Suc n] = [m..<n]"
  3038   "map (\<lambda>n. n - Suc 0) [Suc m..<Suc n] = [m..<n]"
  3031   by (induct n) simp_all
  3039   by (induct n) simp_all
  3032 
  3040 
       
  3041  
  3033 lemma nth_take_lemma:
  3042 lemma nth_take_lemma:
  3034   "k <= length xs ==> k <= length ys ==>
  3043   "k <= length xs ==> k <= length ys ==>
  3035      (!!i. i < k --> xs!i = ys!i) ==> take k xs = take k ys"
  3044      (!!i. i < k --> xs!i = ys!i) ==> take k xs = take k ys"
  3036 apply (atomize, induct k arbitrary: xs ys)
  3045 apply (atomize, induct k arbitrary: xs ys)
  3037 apply (simp_all add: less_Suc_eq_0_disj all_conj_distrib, clarify)
  3046 apply (simp_all add: less_Suc_eq_0_disj all_conj_distrib, clarify)
  3477   assumes "inj f"
  3486   assumes "inj f"
  3478   shows "remdups_adj (map f xs) = map f (remdups_adj xs)"
  3487   shows "remdups_adj (map f xs) = map f (remdups_adj xs)"
  3479   by (induct xs rule: remdups_adj.induct, 
  3488   by (induct xs rule: remdups_adj.induct, 
  3480       auto simp add: injD[OF assms])
  3489       auto simp add: injD[OF assms])
  3481 
  3490 
       
  3491 lemma remdups_upt [simp]:
       
  3492   "remdups [m..<n] = [m..<n]"
       
  3493 proof (cases "m \<le> n")
       
  3494   case False then show ?thesis by simp
       
  3495 next
       
  3496   case True then obtain q where "n = m + q"
       
  3497     by (auto simp add: le_iff_add)
       
  3498   moreover have "remdups [m..<m + q] = [m..<m + q]"
       
  3499     by (induct q) simp_all
       
  3500   ultimately show ?thesis by simp
       
  3501 qed
       
  3502 
  3482 
  3503 
  3483 subsubsection {* @{const insert} *}
  3504 subsubsection {* @{const insert} *}
  3484 
  3505 
  3485 lemma in_set_insert [simp]:
  3506 lemma in_set_insert [simp]:
  3486   "x \<in> set xs \<Longrightarrow> List.insert x xs = xs"
  3507   "x \<in> set xs \<Longrightarrow> List.insert x xs = xs"
  3696 
  3717 
  3697 subsubsection {* @{const replicate} *}
  3718 subsubsection {* @{const replicate} *}
  3698 
  3719 
  3699 lemma length_replicate [simp]: "length (replicate n x) = n"
  3720 lemma length_replicate [simp]: "length (replicate n x) = n"
  3700 by (induct n) auto
  3721 by (induct n) auto
       
  3722 
       
  3723 lemma replicate_eqI:
       
  3724   assumes "length xs = n" and "\<And>y. y \<in> set xs \<Longrightarrow> y = x"
       
  3725   shows "xs = replicate n x"
       
  3726 using assms proof (induct xs arbitrary: n)
       
  3727   case Nil then show ?case by simp
       
  3728 next
       
  3729   case (Cons x xs) then show ?case by (cases n) simp_all
       
  3730 qed
  3701 
  3731 
  3702 lemma Ex_list_of_length: "\<exists>xs. length xs = n"
  3732 lemma Ex_list_of_length: "\<exists>xs. length xs = n"
  3703 by (rule exI[of _ "replicate n undefined"]) simp
  3733 by (rule exI[of _ "replicate n undefined"]) simp
  3704 
  3734 
  3705 lemma map_replicate [simp]: "map f (replicate n x) = replicate n (f x)"
  3735 lemma map_replicate [simp]: "map f (replicate n x) = replicate n (f x)"
  3949 
  3979 
  3950 lemma distinct_enumerate [simp]:
  3980 lemma distinct_enumerate [simp]:
  3951   "distinct (enumerate n xs)"
  3981   "distinct (enumerate n xs)"
  3952   by (simp add: enumerate_eq_zip distinct_zipI1)
  3982   by (simp add: enumerate_eq_zip distinct_zipI1)
  3953 
  3983 
       
  3984 lemma enumerate_append_eq:
       
  3985   "enumerate n (xs @ ys) = enumerate n xs @ enumerate (n + length xs) ys"
       
  3986   unfolding enumerate_eq_zip apply auto
       
  3987   apply (subst zip_append [symmetric]) apply simp
       
  3988   apply (subst upt_add_eq_append [symmetric])
       
  3989   apply (simp_all add: ac_simps)
       
  3990   done
       
  3991 
       
  3992 lemma enumerate_map_upt:
       
  3993   "enumerate n (map f [n..<m]) = map (\<lambda>k. (k, f k)) [n..<m]"
       
  3994   by (cases "n \<le> m") (simp_all add: zip_map2 zip_same_conv_map enumerate_eq_zip)
       
  3995   
  3954 
  3996 
  3955 subsubsection {* @{const rotate1} and @{const rotate} *}
  3997 subsubsection {* @{const rotate1} and @{const rotate} *}
  3956 
  3998 
  3957 lemma rotate0[simp]: "rotate 0 = id"
  3999 lemma rotate0[simp]: "rotate 0 = id"
  3958 by(simp add:rotate_def)
  4000 by(simp add:rotate_def)
  4753 end
  4795 end
  4754 
  4796 
  4755 lemma sorted_upt[simp]: "sorted[i..<j]"
  4797 lemma sorted_upt[simp]: "sorted[i..<j]"
  4756 by (induct j) (simp_all add:sorted_append)
  4798 by (induct j) (simp_all add:sorted_append)
  4757 
  4799 
       
  4800 lemma sort_upt [simp]:
       
  4801   "sort [m..<n] = [m..<n]"
       
  4802   by (rule sorted_sort_id) simp
       
  4803 
  4758 lemma sorted_upto[simp]: "sorted[i..j]"
  4804 lemma sorted_upto[simp]: "sorted[i..j]"
  4759 apply(induct i j rule:upto.induct)
  4805 apply(induct i j rule:upto.induct)
  4760 apply(subst upto.simps)
  4806 apply(subst upto.simps)
  4761 apply(simp add:sorted_Cons)
  4807 apply(simp add:sorted_Cons)
  4762 done
  4808 done
  4774     case False then have "{y. (y = x \<or> y \<in> set xs) \<and> P y} = {y \<in> set xs. P y}"
  4820     case False then have "{y. (y = x \<or> y \<in> set xs) \<and> P y} = {y \<in> set xs. P y}"
  4775       by auto
  4821       by auto
  4776     with Cons False show ?thesis by simp_all
  4822     with Cons False show ?thesis by simp_all
  4777   qed
  4823   qed
  4778 qed
  4824 qed
       
  4825 
       
  4826 lemma sorted_enumerate [simp]:
       
  4827   "sorted (map fst (enumerate n xs))"
       
  4828   by (simp add: enumerate_eq_zip)
  4779 
  4829 
  4780 
  4830 
  4781 subsubsection {* @{const transpose} on sorted lists *}
  4831 subsubsection {* @{const transpose} on sorted lists *}
  4782 
  4832 
  4783 lemma sorted_transpose[simp]:
  4833 lemma sorted_transpose[simp]: