src/HOL/Library/List_Set.thy
 author wenzelm Thu Feb 11 23:00:22 2010 +0100 (2010-02-11) changeset 35115 446c5063e4fd parent 34977 27ceb64d41ea child 37023 efc202e1677e permissions -rw-r--r--
modernized translations;
formal markup of @{syntax_const} and @{const_syntax};
minor tuning;
2 (* Author: Florian Haftmann, TU Muenchen *)
4 header {* Relating (finite) sets and lists *}
6 theory List_Set
7 imports Main
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 fun_left_comm_idem_remove:
19   "fun_left_comm_idem remove"
20 proof -
21   have rem: "remove = (\<lambda>x A. A - {x})" by (simp add: expand_fun_eq remove_def)
22   show ?thesis by (simp only: fun_left_comm_idem_remove rem)
23 qed
25 lemma minus_fold_remove:
26   assumes "finite A"
27   shows "B - A = fold remove B A"
28 proof -
29   have rem: "remove = (\<lambda>x A. A - {x})" by (simp add: expand_fun_eq 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> null xs"
41   by (simp add: is_empty_def null_empty)
43 lemma ball_set:
44   "(\<forall>x\<in>set xs. P x) \<longleftrightarrow> list_all P xs"
45   by (rule list_ball_code)
47 lemma bex_set:
48   "(\<exists>x\<in>set xs. P x) \<longleftrightarrow> list_ex P xs"
49   by (rule list_bex_code)
51 lemma empty_set:
52   "{} = set []"
53   by simp
55 lemma insert_set_compl:
56   "insert x (- set xs) = - set (removeAll x xs)"
57   by auto
59 lemma remove_set_compl:
60   "remove x (- set xs) = - set (List.insert x xs)"
61   by (auto simp del: mem_def simp add: remove_def List.insert_def)
63 lemma image_set:
64   "image f (set xs) = set (map f xs)"
65   by simp
67 lemma project_set:
68   "project P (set xs) = set (filter P xs)"
69   by (auto simp add: project_def)
72 subsection {* Functorial set operations *}
74 lemma union_set:
75   "set xs \<union> A = foldl (\<lambda>A x. Set.insert x A) A xs"
76 proof -
77   interpret fun_left_comm_idem Set.insert
78     by (fact fun_left_comm_idem_insert)
79   show ?thesis by (simp add: union_fold_insert fold_set)
80 qed
82 lemma minus_set:
83   "A - set xs = foldl (\<lambda>A x. remove x A) A xs"
84 proof -
85   interpret fun_left_comm_idem remove
86     by (fact fun_left_comm_idem_remove)
87   show ?thesis
88     by (simp add: minus_fold_remove [of _ A] fold_set)
89 qed
92 subsection {* Derived set operations *}
94 lemma member:
95   "a \<in> A \<longleftrightarrow> (\<exists>x\<in>A. a = x)"
96   by simp
98 lemma subset_eq:
99   "A \<subseteq> B \<longleftrightarrow> (\<forall>x\<in>A. x \<in> B)"
100   by (fact subset_eq)
102 lemma subset:
103   "A \<subset> B \<longleftrightarrow> A \<subseteq> B \<and> \<not> B \<subseteq> A"
104   by (fact less_le_not_le)
106 lemma set_eq:
107   "A = B \<longleftrightarrow> A \<subseteq> B \<and> B \<subseteq> A"
108   by (fact eq_iff)
110 lemma inter:
111   "A \<inter> B = project (\<lambda>x. x \<in> A) B"
112   by (auto simp add: project_def)
114 end