src/HOL/List.thy
changeset 37107 1535aa1c943a
parent 37020 6c699a8e6927
child 37123 9cce71cd4bf0
equal deleted inserted replaced
37106:d56e0b30ce5a 37107:1535aa1c943a
  2968 
  2968 
  2969 lemma insert_remdups:
  2969 lemma insert_remdups:
  2970   "List.insert x (remdups xs) = remdups (List.insert x xs)"
  2970   "List.insert x (remdups xs) = remdups (List.insert x xs)"
  2971   by (simp add: List.insert_def)
  2971   by (simp add: List.insert_def)
  2972 
  2972 
       
  2973 lemma distinct_induct [consumes 1, case_names Nil insert]:
       
  2974   assumes "distinct xs"
       
  2975   assumes "P []"
       
  2976   assumes insert: "\<And>x xs. distinct xs \<Longrightarrow> x \<notin> set xs
       
  2977     \<Longrightarrow> P xs \<Longrightarrow> P (List.insert x xs)"
       
  2978   shows "P xs"
       
  2979 using `distinct xs` proof (induct xs)
       
  2980   case Nil from `P []` show ?case .
       
  2981 next
       
  2982   case (Cons x xs)
       
  2983   then have "distinct xs" and "x \<notin> set xs" and "P xs" by simp_all
       
  2984   with insert have "P (List.insert x xs)" .
       
  2985   with `x \<notin> set xs` show ?case by simp
       
  2986 qed
       
  2987 
  2973 
  2988 
  2974 subsubsection {* @{text remove1} *}
  2989 subsubsection {* @{text remove1} *}
  2975 
  2990 
  2976 lemma remove1_append:
  2991 lemma remove1_append:
  2977   "remove1 x (xs @ ys) =
  2992   "remove1 x (xs @ ys) =
  3020 by (induct xs) simp_all
  3035 by (induct xs) simp_all
  3021 
  3036 
  3022 lemma remove1_remdups:
  3037 lemma remove1_remdups:
  3023   "distinct xs \<Longrightarrow> remove1 x (remdups xs) = remdups (remove1 x xs)"
  3038   "distinct xs \<Longrightarrow> remove1 x (remdups xs) = remdups (remove1 x xs)"
  3024   by (induct xs) simp_all
  3039   by (induct xs) simp_all
       
  3040 
       
  3041 lemma remove1_idem:
       
  3042   assumes "x \<notin> set xs"
       
  3043   shows "remove1 x xs = xs"
       
  3044   using assms by (induct xs) simp_all
  3025 
  3045 
  3026 
  3046 
  3027 subsubsection {* @{text removeAll} *}
  3047 subsubsection {* @{text removeAll} *}
  3028 
  3048 
  3029 lemma removeAll_filter_not_eq:
  3049 lemma removeAll_filter_not_eq:
  3799 lemma sorted_insort_insert:
  3819 lemma sorted_insort_insert:
  3800   assumes "sorted xs"
  3820   assumes "sorted xs"
  3801   shows "sorted (insort_insert x xs)"
  3821   shows "sorted (insort_insert x xs)"
  3802   using assms by (simp add: insort_insert_def sorted_insort)
  3822   using assms by (simp add: insort_insert_def sorted_insort)
  3803 
  3823 
       
  3824 lemma filter_insort_key_triv:
       
  3825   "\<not> P x \<Longrightarrow> filter P (insort_key f x xs) = filter P xs"
       
  3826   by (induct xs) simp_all
       
  3827 
       
  3828 lemma filter_insort_key:
       
  3829   "sorted (map f xs) \<Longrightarrow> P x \<Longrightarrow> filter P (insort_key f x xs) = insort_key f x (filter P xs)"
       
  3830   using assms by (induct xs)
       
  3831     (auto simp add: sorted_Cons, subst insort_is_Cons, auto)
       
  3832 
       
  3833 lemma filter_sort_key:
       
  3834   "filter P (sort_key f xs) = sort_key f (filter P xs)"
       
  3835   by (induct xs) (simp_all add: filter_insort_key_triv filter_insort_key)
       
  3836 
       
  3837 lemma sorted_same [simp]:
       
  3838   "sorted [x\<leftarrow>xs. x = f xs]"
       
  3839 proof (induct xs arbitrary: f)
       
  3840   case Nil then show ?case by simp
       
  3841 next
       
  3842   case (Cons x xs)
       
  3843   then have "sorted [y\<leftarrow>xs . y = (\<lambda>xs. x) xs]" .
       
  3844   moreover from Cons have "sorted [y\<leftarrow>xs . y = (f \<circ> Cons x) xs]" .
       
  3845   ultimately show ?case by (simp_all add: sorted_Cons)
       
  3846 qed
       
  3847 
       
  3848 lemma remove1_insort [simp]:
       
  3849   "remove1 x (insort x xs) = xs"
       
  3850   by (induct xs) simp_all
       
  3851 
  3804 end
  3852 end
  3805 
  3853 
  3806 lemma sorted_upt[simp]: "sorted[i..<j]"
  3854 lemma sorted_upt[simp]: "sorted[i..<j]"
  3807 by (induct j) (simp_all add:sorted_append)
  3855 by (induct j) (simp_all add:sorted_append)
  3808 
  3856 
  3997 proof -
  4045 proof -
  3998   interpret fun_left_comm insort by (fact fun_left_comm_insort)
  4046   interpret fun_left_comm insort by (fact fun_left_comm_insort)
  3999   show ?thesis by (simp add: sort_foldl_insort sorted_list_of_set_def fold_set_remdups)
  4047   show ?thesis by (simp add: sort_foldl_insort sorted_list_of_set_def fold_set_remdups)
  4000 qed
  4048 qed
  4001 
  4049 
       
  4050 lemma sorted_list_of_set_remove:
       
  4051   assumes "finite A"
       
  4052   shows "sorted_list_of_set (A - {x}) = remove1 x (sorted_list_of_set A)"
       
  4053 proof (cases "x \<in> A")
       
  4054   case False with assms have "x \<notin> set (sorted_list_of_set A)" by simp
       
  4055   with False show ?thesis by (simp add: remove1_idem)
       
  4056 next
       
  4057   case True then obtain B where A: "A = insert x B" by (rule Set.set_insert)
       
  4058   with assms show ?thesis by simp
       
  4059 qed
       
  4060 
  4002 end
  4061 end
       
  4062 
       
  4063 lemma sorted_list_of_set_range [simp]:
       
  4064   "sorted_list_of_set {m..<n} = [m..<n]"
       
  4065   by (rule sorted_distinct_set_unique) simp_all
       
  4066 
  4003 
  4067 
  4004 
  4068 
  4005 subsubsection {* @{text lists}: the list-forming operator over sets *}
  4069 subsubsection {* @{text lists}: the list-forming operator over sets *}
  4006 
  4070 
  4007 inductive_set
  4071 inductive_set