src/HOL/Library/List_Set.thy
 changeset 31807 039893a9a77d child 31846 89c37daebfdd
```     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/src/HOL/Library/List_Set.thy	Thu Jun 25 17:07:18 2009 +0200
1.3 @@ -0,0 +1,163 @@
1.4 +
1.5 +(* Author: Florian Haftmann, TU Muenchen *)
1.6 +
1.7 +header {* Relating (finite) sets and lists *}
1.8 +
1.9 +theory List_Set
1.10 +imports Main
1.11 +begin
1.12 +
1.13 +subsection {* Various additional list functions *}
1.14 +
1.15 +definition insert :: "'a \<Rightarrow> 'a list \<Rightarrow> 'a list" where
1.16 +  "insert x xs = (if x \<in> set xs then xs else x # xs)"
1.17 +
1.18 +definition remove_all :: "'a \<Rightarrow> 'a list \<Rightarrow> 'a list" where
1.19 +  "remove_all x xs = filter (Not o op = x) xs"
1.20 +
1.21 +
1.22 +subsection {* Various additional set functions *}
1.23 +
1.24 +definition is_empty :: "'a set \<Rightarrow> bool" where
1.25 +  "is_empty A \<longleftrightarrow> A = {}"
1.26 +
1.27 +definition remove :: "'a \<Rightarrow> 'a set \<Rightarrow> 'a set" where
1.28 +  "remove x A = A - {x}"
1.29 +
1.30 +lemma fun_left_comm_idem_remove:
1.31 +  "fun_left_comm_idem remove"
1.32 +proof -
1.33 +  have rem: "remove = (\<lambda>x A. A - {x})" by (simp add: expand_fun_eq remove_def)
1.34 +  show ?thesis by (simp only: fun_left_comm_idem_remove rem)
1.35 +qed
1.36 +
1.37 +lemma minus_fold_remove:
1.38 +  assumes "finite A"
1.39 +  shows "B - A = fold remove B A"
1.40 +proof -
1.41 +  have rem: "remove = (\<lambda>x A. A - {x})" by (simp add: expand_fun_eq remove_def)
1.42 +  show ?thesis by (simp only: rem assms minus_fold_remove)
1.43 +qed
1.44 +
1.45 +definition project :: "('a \<Rightarrow> bool) \<Rightarrow> 'a set \<Rightarrow> 'a set" where
1.46 +  "project P A = {a\<in>A. P a}"
1.47 +
1.48 +
1.49 +subsection {* Basic set operations *}
1.50 +
1.51 +lemma is_empty_set:
1.52 +  "is_empty (set xs) \<longleftrightarrow> null xs"
1.53 +  by (simp add: is_empty_def null_empty)
1.54 +
1.55 +lemma ball_set:
1.56 +  "(\<forall>x\<in>set xs. P x) \<longleftrightarrow> list_all P xs"
1.57 +  by (rule list_ball_code)
1.58 +
1.59 +lemma bex_set:
1.60 +  "(\<exists>x\<in>set xs. P x) \<longleftrightarrow> list_ex P xs"
1.61 +  by (rule list_bex_code)
1.62 +
1.63 +lemma empty_set:
1.64 +  "{} = set []"
1.65 +  by simp
1.66 +
1.67 +lemma insert_set:
1.68 +  "Set.insert x (set xs) = set (insert x xs)"
1.69 +  by (auto simp add: insert_def)
1.70 +
1.71 +lemma remove_set:
1.72 +  "remove x (set xs) = set (remove_all x xs)"
1.73 +  by (auto simp add: remove_def remove_all_def)
1.74 +
1.75 +lemma image_set:
1.76 +  "image f (set xs) = set (remdups (map f xs))"
1.77 +  by simp
1.78 +
1.79 +lemma project_set:
1.80 +  "project P (set xs) = set (filter P xs)"
1.81 +  by (auto simp add: project_def)
1.82 +
1.83 +
1.84 +subsection {* Functorial set operations *}
1.85 +
1.86 +lemma union_set:
1.87 +  "set xs \<union> A = foldl (\<lambda>A x. Set.insert x A) A xs"
1.88 +proof -
1.89 +  interpret fun_left_comm_idem Set.insert
1.90 +    by (fact fun_left_comm_idem_insert)
1.91 +  show ?thesis by (simp add: union_fold_insert fold_set)
1.92 +qed
1.93 +
1.94 +lemma minus_set:
1.95 +  "A - set xs = foldl (\<lambda>A x. remove x A) A xs"
1.96 +proof -
1.97 +  interpret fun_left_comm_idem remove
1.98 +    by (fact fun_left_comm_idem_remove)
1.99 +  show ?thesis
1.100 +    by (simp add: minus_fold_remove [of _ A] fold_set)
1.101 +qed
1.102 +
1.103 +lemma Inter_set:
1.104 +  "Inter (set (A # As)) = foldl (op \<inter>) A As"
1.105 +proof -
1.106 +  have "finite (set (A # As))" by simp
1.107 +  moreover have "fold (op \<inter>) UNIV (set (A # As)) = foldl (\<lambda>y x. x \<inter> y) UNIV (A # As)"
1.108 +    by (rule fun_left_comm_idem.fold_set, unfold_locales, auto)
1.109 +  ultimately have "Inter (set (A # As)) = foldl (op \<inter>) UNIV (A # As)"
1.110 +    by (simp only: Inter_fold_inter Int_commute)
1.111 +  then show ?thesis by simp
1.112 +qed
1.113 +
1.114 +lemma Union_set:
1.115 +  "Union (set As) = foldl (op \<union>) {} As"
1.116 +proof -
1.117 +  have "fold (op \<union>) {} (set As) = foldl (\<lambda>y x. x \<union> y) {} As"
1.118 +    by (rule fun_left_comm_idem.fold_set, unfold_locales, auto)
1.119 +  then show ?thesis
1.120 +    by (simp only: Union_fold_union finite_set Un_commute)
1.121 +qed
1.122 +
1.123 +lemma INTER_set:
1.124 +  "INTER (set (A # As)) f = foldl (\<lambda>B A. f A \<inter> B) (f A) As"
1.125 +proof -
1.126 +  have "finite (set (A # As))" by simp
1.127 +  moreover have "fold (\<lambda>A. op \<inter> (f A)) UNIV (set (A # As)) = foldl (\<lambda>B A. f A \<inter> B) UNIV (A # As)"
1.128 +    by (rule fun_left_comm_idem.fold_set, unfold_locales, auto)
1.129 +  ultimately have "INTER (set (A # As)) f = foldl (\<lambda>B A. f A \<inter> B) UNIV (A # As)"
1.130 +    by (simp only: INTER_fold_inter)
1.131 +  then show ?thesis by simp
1.132 +qed
1.133 +
1.134 +lemma UNION_set:
1.135 +  "UNION (set As) f = foldl (\<lambda>B A. f A \<union> B) {} As"
1.136 +proof -
1.137 +  have "fold (\<lambda>A. op \<union> (f A)) {} (set As) = foldl (\<lambda>B A. f A \<union> B) {} As"
1.138 +    by (rule fun_left_comm_idem.fold_set, unfold_locales, auto)
1.139 +  then show ?thesis
1.140 +    by (simp only: UNION_fold_union finite_set)
1.141 +qed
1.142 +
1.143 +
1.144 +subsection {* Derived set operations *}
1.145 +
1.146 +lemma member:
1.147 +  "a \<in> A \<longleftrightarrow> (\<exists>x\<in>A. a = x)"
1.148 +  by simp
1.149 +
1.150 +lemma subset_eq:
1.151 +  "A \<subseteq> B \<longleftrightarrow> (\<forall>x\<in>A. x \<in> B)"
1.152 +  by (fact subset_eq)
1.153 +
1.154 +lemma subset:
1.155 +  "A \<subset> B \<longleftrightarrow> A \<subseteq> B \<and> \<not> B \<subseteq> A"
1.156 +  by (fact less_le_not_le)
1.157 +
1.158 +lemma set_eq:
1.159 +  "A = B \<longleftrightarrow> A \<subseteq> B \<and> B \<subseteq> A"
1.160 +  by (fact eq_iff)
1.161 +
1.162 +lemma inter:
1.163 +  "A \<inter> B = project (\<lambda>x. x \<in> A) B"
1.164 +  by (auto simp add: project_def)
1.165 +
1.166 +end
1.167 \ No newline at end of file
```