src/HOL/Library/More_Set.thy
 author hoelzl Tue Jul 19 14:37:49 2011 +0200 (2011-07-19) changeset 43922 c6f35921056e parent 42871 1c0b99f950d9 child 44326 2b088d74beb3 permissions -rw-r--r--
add nat => enat coercion
2 (* Author: Florian Haftmann, TU Muenchen *)
4 header {* Relating (finite) sets and lists *}
6 theory More_Set
7 imports Main More_List
8 begin
10 subsection {* Various additional set functions *}
12 definition is_empty :: "'a set \<Rightarrow> bool" where
13   "is_empty A \<longleftrightarrow> A = {}"
15 definition remove :: "'a \<Rightarrow> 'a set \<Rightarrow> 'a set" where
16   "remove x A = A - {x}"
18 lemma comp_fun_idem_remove:
19   "comp_fun_idem remove"
20 proof -
21   have rem: "remove = (\<lambda>x A. A - {x})" by (simp add: fun_eq_iff remove_def)
22   show ?thesis by (simp only: comp_fun_idem_remove rem)
23 qed
25 lemma minus_fold_remove:
26   assumes "finite A"
27   shows "B - A = Finite_Set.fold remove B A"
28 proof -
29   have rem: "remove = (\<lambda>x A. A - {x})" by (simp add: fun_eq_iff remove_def)
30   show ?thesis by (simp only: rem assms minus_fold_remove)
31 qed
33 definition project :: "('a \<Rightarrow> bool) \<Rightarrow> 'a set \<Rightarrow> 'a set" where
34   "project P A = {a\<in>A. P a}"
37 subsection {* Basic set operations *}
39 lemma is_empty_set:
40   "is_empty (set xs) \<longleftrightarrow> List.null xs"
41   by (simp add: is_empty_def null_def)
43 lemma empty_set:
44   "{} = set []"
45   by simp
47 lemma insert_set_compl:
48   "insert x (- set xs) = - set (removeAll x xs)"
49   by auto
51 lemma remove_set_compl:
52   "remove x (- set xs) = - set (List.insert x xs)"
53   by (auto simp del: mem_def simp add: remove_def List.insert_def)
55 lemma image_set:
56   "image f (set xs) = set (map f xs)"
57   by simp
59 lemma project_set:
60   "project P (set xs) = set (filter P xs)"
61   by (auto simp add: project_def)
64 subsection {* Functorial set operations *}
66 lemma union_set:
67   "set xs \<union> A = fold Set.insert xs A"
68 proof -
69   interpret comp_fun_idem Set.insert
70     by (fact comp_fun_idem_insert)
71   show ?thesis by (simp add: union_fold_insert fold_set)
72 qed
74 lemma union_set_foldr:
75   "set xs \<union> A = foldr Set.insert xs A"
76 proof -
77   have "\<And>x y :: 'a. insert y \<circ> insert x = insert x \<circ> insert y"
78     by (auto intro: ext)
79   then show ?thesis by (simp add: union_set foldr_fold)
80 qed
82 lemma minus_set:
83   "A - set xs = fold remove xs A"
84 proof -
85   interpret comp_fun_idem remove
86     by (fact comp_fun_idem_remove)
87   show ?thesis
88     by (simp add: minus_fold_remove [of _ A] fold_set)
89 qed
91 lemma minus_set_foldr:
92   "A - set xs = foldr remove xs A"
93 proof -
94   have "\<And>x y :: 'a. remove y \<circ> remove x = remove x \<circ> remove y"
95     by (auto simp add: remove_def intro: ext)
96   then show ?thesis by (simp add: minus_set foldr_fold)
97 qed
100 subsection {* Derived set operations *}
102 lemma member:
103   "a \<in> A \<longleftrightarrow> (\<exists>x\<in>A. a = x)"
104   by simp
106 lemma subset_eq:
107   "A \<subseteq> B \<longleftrightarrow> (\<forall>x\<in>A. x \<in> B)"
108   by (fact subset_eq)
110 lemma subset:
111   "A \<subset> B \<longleftrightarrow> A \<subseteq> B \<and> \<not> B \<subseteq> A"
112   by (fact less_le_not_le)
114 lemma set_eq:
115   "A = B \<longleftrightarrow> A \<subseteq> B \<and> B \<subseteq> A"
116   by (fact eq_iff)
118 lemma inter:
119   "A \<inter> B = project (\<lambda>x. x \<in> A) B"
120   by (auto simp add: project_def)
123 subsection {* Various lemmas *}
125 lemma not_set_compl:
126   "Not \<circ> set xs = - set xs"
127   by (simp add: fun_Compl_def comp_def fun_eq_iff)
129 end