31807
|
1 |
|
|
2 |
(* Author: Florian Haftmann, TU Muenchen *)
|
|
3 |
|
|
4 |
header {* Relating (finite) sets and lists *}
|
|
5 |
|
|
6 |
theory List_Set
|
|
7 |
imports Main
|
|
8 |
begin
|
|
9 |
|
|
10 |
subsection {* Various additional list functions *}
|
|
11 |
|
|
12 |
definition insert :: "'a \<Rightarrow> 'a list \<Rightarrow> 'a list" where
|
|
13 |
"insert x xs = (if x \<in> set xs then xs else x # xs)"
|
|
14 |
|
|
15 |
definition remove_all :: "'a \<Rightarrow> 'a list \<Rightarrow> 'a list" where
|
|
16 |
"remove_all x xs = filter (Not o op = x) xs"
|
|
17 |
|
|
18 |
|
|
19 |
subsection {* Various additional set functions *}
|
|
20 |
|
|
21 |
definition is_empty :: "'a set \<Rightarrow> bool" where
|
|
22 |
"is_empty A \<longleftrightarrow> A = {}"
|
|
23 |
|
|
24 |
definition remove :: "'a \<Rightarrow> 'a set \<Rightarrow> 'a set" where
|
|
25 |
"remove x A = A - {x}"
|
|
26 |
|
|
27 |
lemma fun_left_comm_idem_remove:
|
|
28 |
"fun_left_comm_idem remove"
|
|
29 |
proof -
|
|
30 |
have rem: "remove = (\<lambda>x A. A - {x})" by (simp add: expand_fun_eq remove_def)
|
|
31 |
show ?thesis by (simp only: fun_left_comm_idem_remove rem)
|
|
32 |
qed
|
|
33 |
|
|
34 |
lemma minus_fold_remove:
|
|
35 |
assumes "finite A"
|
|
36 |
shows "B - A = fold remove B A"
|
|
37 |
proof -
|
|
38 |
have rem: "remove = (\<lambda>x A. A - {x})" by (simp add: expand_fun_eq remove_def)
|
|
39 |
show ?thesis by (simp only: rem assms minus_fold_remove)
|
|
40 |
qed
|
|
41 |
|
|
42 |
definition project :: "('a \<Rightarrow> bool) \<Rightarrow> 'a set \<Rightarrow> 'a set" where
|
|
43 |
"project P A = {a\<in>A. P a}"
|
|
44 |
|
|
45 |
|
|
46 |
subsection {* Basic set operations *}
|
|
47 |
|
|
48 |
lemma is_empty_set:
|
|
49 |
"is_empty (set xs) \<longleftrightarrow> null xs"
|
|
50 |
by (simp add: is_empty_def null_empty)
|
|
51 |
|
|
52 |
lemma ball_set:
|
|
53 |
"(\<forall>x\<in>set xs. P x) \<longleftrightarrow> list_all P xs"
|
|
54 |
by (rule list_ball_code)
|
|
55 |
|
|
56 |
lemma bex_set:
|
|
57 |
"(\<exists>x\<in>set xs. P x) \<longleftrightarrow> list_ex P xs"
|
|
58 |
by (rule list_bex_code)
|
|
59 |
|
|
60 |
lemma empty_set:
|
|
61 |
"{} = set []"
|
|
62 |
by simp
|
|
63 |
|
|
64 |
lemma insert_set:
|
|
65 |
"Set.insert x (set xs) = set (insert x xs)"
|
|
66 |
by (auto simp add: insert_def)
|
|
67 |
|
32880
|
68 |
lemma insert_set_compl:
|
|
69 |
"Set.insert x (- set xs) = - set (List_Set.remove_all x xs)"
|
|
70 |
by (auto simp del: mem_def simp add: remove_all_def)
|
|
71 |
|
31807
|
72 |
lemma remove_set:
|
|
73 |
"remove x (set xs) = set (remove_all x xs)"
|
|
74 |
by (auto simp add: remove_def remove_all_def)
|
|
75 |
|
32880
|
76 |
lemma remove_set_compl:
|
|
77 |
"List_Set.remove x (- set xs) = - set (List_Set.insert x xs)"
|
|
78 |
by (auto simp del: mem_def simp add: remove_def List_Set.insert_def)
|
|
79 |
|
31807
|
80 |
lemma image_set:
|
31846
|
81 |
"image f (set xs) = set (map f xs)"
|
31807
|
82 |
by simp
|
|
83 |
|
|
84 |
lemma project_set:
|
|
85 |
"project P (set xs) = set (filter P xs)"
|
|
86 |
by (auto simp add: project_def)
|
|
87 |
|
|
88 |
|
|
89 |
subsection {* Functorial set operations *}
|
|
90 |
|
|
91 |
lemma union_set:
|
|
92 |
"set xs \<union> A = foldl (\<lambda>A x. Set.insert x A) A xs"
|
|
93 |
proof -
|
|
94 |
interpret fun_left_comm_idem Set.insert
|
|
95 |
by (fact fun_left_comm_idem_insert)
|
|
96 |
show ?thesis by (simp add: union_fold_insert fold_set)
|
|
97 |
qed
|
|
98 |
|
|
99 |
lemma minus_set:
|
|
100 |
"A - set xs = foldl (\<lambda>A x. remove x A) A xs"
|
|
101 |
proof -
|
|
102 |
interpret fun_left_comm_idem remove
|
|
103 |
by (fact fun_left_comm_idem_remove)
|
|
104 |
show ?thesis
|
|
105 |
by (simp add: minus_fold_remove [of _ A] fold_set)
|
|
106 |
qed
|
|
107 |
|
|
108 |
lemma Inter_set:
|
32880
|
109 |
"Inter (set As) = foldl (op \<inter>) UNIV As"
|
31807
|
110 |
proof -
|
32880
|
111 |
have "fold (op \<inter>) UNIV (set As) = foldl (\<lambda>y x. x \<inter> y) UNIV As"
|
31807
|
112 |
by (rule fun_left_comm_idem.fold_set, unfold_locales, auto)
|
32880
|
113 |
then show ?thesis
|
|
114 |
by (simp only: Inter_fold_inter finite_set Int_commute)
|
31807
|
115 |
qed
|
|
116 |
|
|
117 |
lemma Union_set:
|
|
118 |
"Union (set As) = foldl (op \<union>) {} As"
|
|
119 |
proof -
|
|
120 |
have "fold (op \<union>) {} (set As) = foldl (\<lambda>y x. x \<union> y) {} As"
|
|
121 |
by (rule fun_left_comm_idem.fold_set, unfold_locales, auto)
|
|
122 |
then show ?thesis
|
|
123 |
by (simp only: Union_fold_union finite_set Un_commute)
|
|
124 |
qed
|
|
125 |
|
|
126 |
lemma INTER_set:
|
32880
|
127 |
"INTER (set As) f = foldl (\<lambda>B A. f A \<inter> B) UNIV As"
|
31807
|
128 |
proof -
|
32880
|
129 |
have "fold (\<lambda>A. op \<inter> (f A)) UNIV (set As) = foldl (\<lambda>B A. f A \<inter> B) UNIV As"
|
31807
|
130 |
by (rule fun_left_comm_idem.fold_set, unfold_locales, auto)
|
32880
|
131 |
then show ?thesis
|
|
132 |
by (simp only: INTER_fold_inter finite_set)
|
31807
|
133 |
qed
|
|
134 |
|
|
135 |
lemma UNION_set:
|
|
136 |
"UNION (set As) f = foldl (\<lambda>B A. f A \<union> B) {} As"
|
|
137 |
proof -
|
|
138 |
have "fold (\<lambda>A. op \<union> (f A)) {} (set As) = foldl (\<lambda>B A. f A \<union> B) {} As"
|
|
139 |
by (rule fun_left_comm_idem.fold_set, unfold_locales, auto)
|
|
140 |
then show ?thesis
|
|
141 |
by (simp only: UNION_fold_union finite_set)
|
|
142 |
qed
|
|
143 |
|
|
144 |
|
|
145 |
subsection {* Derived set operations *}
|
|
146 |
|
|
147 |
lemma member:
|
|
148 |
"a \<in> A \<longleftrightarrow> (\<exists>x\<in>A. a = x)"
|
|
149 |
by simp
|
|
150 |
|
|
151 |
lemma subset_eq:
|
|
152 |
"A \<subseteq> B \<longleftrightarrow> (\<forall>x\<in>A. x \<in> B)"
|
|
153 |
by (fact subset_eq)
|
|
154 |
|
|
155 |
lemma subset:
|
|
156 |
"A \<subset> B \<longleftrightarrow> A \<subseteq> B \<and> \<not> B \<subseteq> A"
|
|
157 |
by (fact less_le_not_le)
|
|
158 |
|
|
159 |
lemma set_eq:
|
|
160 |
"A = B \<longleftrightarrow> A \<subseteq> B \<and> B \<subseteq> A"
|
|
161 |
by (fact eq_iff)
|
|
162 |
|
|
163 |
lemma inter:
|
|
164 |
"A \<inter> B = project (\<lambda>x. x \<in> A) B"
|
|
165 |
by (auto simp add: project_def)
|
|
166 |
|
31851
|
167 |
|
|
168 |
hide (open) const insert
|
|
169 |
|
31807
|
170 |
end |