17632
|
1 |
(* Title: HOL/Library/ExecutableSet.thy
|
|
2 |
ID: $Id$
|
|
3 |
Author: Stefan Berghofer, TU Muenchen
|
|
4 |
*)
|
|
5 |
|
|
6 |
header {* Implementation of finite sets by lists *}
|
|
7 |
|
|
8 |
theory ExecutableSet
|
|
9 |
imports Main
|
|
10 |
begin
|
|
11 |
|
19791
|
12 |
section {* Definitional rewrites *}
|
|
13 |
|
|
14 |
lemma [code target: Set]:
|
|
15 |
"(A = B) = (A \<subseteq> B \<and> B \<subseteq> A)"
|
17632
|
16 |
by blast
|
|
17 |
|
|
18 |
declare bex_triv_one_point1 [symmetric, standard, code]
|
|
19 |
|
19791
|
20 |
section {* HOL definitions *}
|
|
21 |
|
|
22 |
subsection {* Basic definitions *}
|
|
23 |
|
|
24 |
definition
|
|
25 |
flip :: "('a \<Rightarrow> 'b \<Rightarrow> 'c) \<Rightarrow> 'b \<Rightarrow> 'a \<Rightarrow> 'c"
|
|
26 |
"flip f a b = f b a"
|
|
27 |
member :: "'a list \<Rightarrow> 'a \<Rightarrow> bool"
|
|
28 |
"member xs x = (x \<in> set xs)"
|
|
29 |
insertl :: "'a \<Rightarrow> 'a list \<Rightarrow> 'a list"
|
|
30 |
"insertl x xs = (if member xs x then xs else x#xs)"
|
|
31 |
|
|
32 |
lemma
|
|
33 |
[code target: List]: "member [] y = False"
|
|
34 |
and [code target: List]: "member (x#xs) y = (y = x \<or> member xs y)"
|
|
35 |
unfolding member_def by (induct xs) simp_all
|
|
36 |
|
|
37 |
consts
|
|
38 |
drop_first :: "('a \<Rightarrow> bool) \<Rightarrow> 'a list \<Rightarrow> 'a list"
|
|
39 |
|
|
40 |
primrec
|
|
41 |
"drop_first f [] = []"
|
|
42 |
"drop_first f (x#xs) = (if f x then xs else x # drop_first f xs)"
|
|
43 |
declare drop_first.simps [code del]
|
|
44 |
declare drop_first.simps [code target: List]
|
|
45 |
|
|
46 |
declare remove1.simps [code del]
|
|
47 |
lemma [code target: List]:
|
|
48 |
"remove1 x xs = (if member xs x then drop_first (\<lambda>y. y = x) xs else xs)"
|
|
49 |
proof (cases "member xs x")
|
|
50 |
case False thus ?thesis unfolding member_def by (induct xs) auto
|
|
51 |
next
|
|
52 |
case True
|
|
53 |
have "remove1 x xs = drop_first (\<lambda>y. y = x) xs" by (induct xs) simp_all
|
|
54 |
with True show ?thesis by simp
|
|
55 |
qed
|
|
56 |
|
|
57 |
lemma member_nil [simp]:
|
|
58 |
"member [] = (\<lambda>x. False)"
|
|
59 |
proof
|
|
60 |
fix x
|
|
61 |
show "member [] x = False" unfolding member_def by simp
|
|
62 |
qed
|
|
63 |
|
|
64 |
lemma member_insertl [simp]:
|
|
65 |
"x \<in> set (insertl x xs)"
|
|
66 |
unfolding insertl_def member_def mem_iff by simp
|
|
67 |
|
|
68 |
lemma insertl_member [simp]:
|
|
69 |
fixes xs x
|
|
70 |
assumes member: "member xs x"
|
|
71 |
shows "insertl x xs = xs"
|
|
72 |
using member unfolding insertl_def by simp
|
|
73 |
|
|
74 |
lemma insertl_not_member [simp]:
|
|
75 |
fixes xs x
|
|
76 |
assumes member: "\<not> (member xs x)"
|
|
77 |
shows "insertl x xs = x # xs"
|
|
78 |
using member unfolding insertl_def by simp
|
|
79 |
|
|
80 |
lemma foldr_remove1_empty [simp]:
|
|
81 |
"foldr remove1 xs [] = []"
|
|
82 |
by (induct xs) simp_all
|
|
83 |
|
|
84 |
|
|
85 |
subsection {* Derived definitions *}
|
|
86 |
|
|
87 |
consts
|
|
88 |
unionl :: "'a list \<Rightarrow> 'a list \<Rightarrow> 'a list"
|
|
89 |
intersect :: "'a list \<Rightarrow> 'a list \<Rightarrow> 'a list"
|
|
90 |
subtract :: "'a list \<Rightarrow> 'a list \<Rightarrow> 'a list"
|
|
91 |
map_distinct :: "('a \<Rightarrow> 'b) \<Rightarrow> 'a list \<Rightarrow> 'b list"
|
|
92 |
unions :: "'a list list \<Rightarrow> 'a list"
|
|
93 |
intersects :: "'a list list \<Rightarrow> 'a list"
|
|
94 |
|
|
95 |
function
|
|
96 |
"unionl [] ys = ys"
|
|
97 |
"unionl xs ys = foldr insertl xs ys"
|
|
98 |
by pat_completeness auto
|
|
99 |
termination unionl by (auto_term "{}")
|
|
100 |
lemmas unionl_def = unionl.simps(2)
|
|
101 |
|
|
102 |
function
|
|
103 |
"intersect [] ys = []"
|
|
104 |
"intersect xs [] = []"
|
|
105 |
"intersect xs ys = filter (member xs) ys"
|
|
106 |
by pat_completeness simp_all
|
|
107 |
termination intersect by (auto_term "{}")
|
|
108 |
lemmas intersect_def = intersect.simps(3)
|
|
109 |
|
|
110 |
function
|
|
111 |
"subtract [] ys = ys"
|
|
112 |
"subtract xs [] = []"
|
|
113 |
"subtract xs ys = foldr remove1 xs ys"
|
|
114 |
by pat_completeness simp_all
|
|
115 |
termination subtract by (auto_term "{}")
|
|
116 |
lemmas subtract_def = subtract.simps(3)
|
|
117 |
|
|
118 |
function
|
|
119 |
"map_distinct f [] = []"
|
|
120 |
"map_distinct f xs = foldr (insertl o f) xs []"
|
|
121 |
by pat_completeness simp_all
|
|
122 |
termination map_distinct by (auto_term "{}")
|
|
123 |
lemmas map_distinct_def = map_distinct.simps(2)
|
|
124 |
|
|
125 |
function
|
|
126 |
"unions [] = []"
|
|
127 |
"unions xs = foldr unionl xs []"
|
|
128 |
by pat_completeness simp_all
|
|
129 |
termination unions by (auto_term "{}")
|
|
130 |
lemmas unions_def = unions.simps(2)
|
|
131 |
|
|
132 |
primrec
|
|
133 |
"intersects (x#xs) = foldr intersect xs x"
|
|
134 |
|
|
135 |
definition
|
|
136 |
map_union :: "'a list \<Rightarrow> ('a \<Rightarrow> 'b list) \<Rightarrow> 'b list"
|
|
137 |
"map_union xs f = unions (map f xs)"
|
|
138 |
map_inter :: "'a list \<Rightarrow> ('a \<Rightarrow> 'b list) \<Rightarrow> 'b list"
|
|
139 |
"map_inter xs f = intersects (map f xs)"
|
|
140 |
|
|
141 |
|
|
142 |
section {* Isomorphism proofs *}
|
|
143 |
|
|
144 |
lemma iso_member:
|
|
145 |
"member xs x = (x \<in> set xs)"
|
|
146 |
unfolding member_def mem_iff ..
|
|
147 |
|
|
148 |
lemma iso_insert:
|
|
149 |
"set (insertl x xs) = insert x (set xs)"
|
|
150 |
unfolding insertl_def iso_member by (simp add: Set.insert_absorb)
|
|
151 |
|
|
152 |
lemma iso_remove1:
|
|
153 |
assumes distnct: "distinct xs"
|
|
154 |
shows "set (remove1 x xs) = set xs - {x}"
|
|
155 |
using distnct set_remove1_eq by auto
|
|
156 |
|
|
157 |
lemma iso_union:
|
|
158 |
"set (unionl xs ys) = set xs \<union> set ys"
|
|
159 |
unfolding unionl_def by (induct xs fixing: ys) (simp_all add: iso_insert)
|
|
160 |
|
|
161 |
lemma iso_intersect:
|
|
162 |
"set (intersect xs ys) = set xs \<inter> set ys"
|
|
163 |
unfolding intersect_def Int_def by (simp add: Int_def iso_member) auto
|
|
164 |
|
|
165 |
lemma iso_subtract:
|
|
166 |
fixes ys
|
|
167 |
assumes distnct: "distinct ys"
|
|
168 |
shows "set (subtract xs ys) = set ys - set xs"
|
|
169 |
and "distinct (subtract xs ys)"
|
|
170 |
unfolding subtract_def using distnct by (induct xs fixing: ys) (simp_all, auto)
|
|
171 |
|
|
172 |
corollary iso_subtract':
|
|
173 |
fixes xs ys
|
|
174 |
assumes distnct: "distinct xs"
|
|
175 |
shows "set ((flip subtract) xs ys) = set xs - set ys"
|
|
176 |
proof -
|
|
177 |
from distnct iso_subtract have "set (subtract ys xs) = set xs - set ys" by auto
|
|
178 |
thus ?thesis unfolding flip_def by auto
|
|
179 |
qed
|
|
180 |
|
|
181 |
lemma iso_map_distinct:
|
|
182 |
"set (map_distinct f xs) = image f (set xs)"
|
|
183 |
unfolding map_distinct_def by (induct xs) (simp_all add: iso_insert)
|
|
184 |
|
|
185 |
lemma iso_unions:
|
|
186 |
"set (unions xss) = \<Union> set (map set xss)"
|
|
187 |
unfolding unions_def proof (induct xss)
|
|
188 |
case Nil show ?case by simp
|
|
189 |
next
|
|
190 |
case (Cons xs xss) thus ?case by (induct xs) (simp_all add: iso_insert)
|
|
191 |
qed
|
|
192 |
|
|
193 |
lemma iso_intersects:
|
|
194 |
"set (intersects (xs#xss)) = \<Inter> set (map set (xs#xss))"
|
|
195 |
by (induct xss) (simp_all add: Int_def iso_member, auto)
|
|
196 |
|
|
197 |
lemma iso_UNION:
|
|
198 |
"set (map_union xs f) = UNION (set xs) (set o f)"
|
|
199 |
unfolding map_union_def iso_unions by simp
|
|
200 |
|
|
201 |
lemma iso_INTER:
|
|
202 |
"set (map_inter (x#xs) f) = INTER (set (x#xs)) (set o f)"
|
|
203 |
unfolding map_inter_def iso_intersects by (induct xs) (simp_all add: iso_member, auto)
|
|
204 |
|
|
205 |
definition
|
|
206 |
Blall :: "'a list \<Rightarrow> ('a \<Rightarrow> bool) \<Rightarrow> bool"
|
|
207 |
"Blall = flip list_all"
|
|
208 |
Blex :: "'a list \<Rightarrow> ('a \<Rightarrow> bool) \<Rightarrow> bool"
|
|
209 |
"Blex = flip list_ex"
|
|
210 |
|
|
211 |
lemma iso_Ball:
|
|
212 |
"Blall xs f = Ball (set xs) f"
|
|
213 |
unfolding Blall_def flip_def by (induct xs) simp_all
|
|
214 |
|
|
215 |
lemma iso_Bex:
|
|
216 |
"Blex xs f = Bex (set xs) f"
|
|
217 |
unfolding Blex_def flip_def by (induct xs) simp_all
|
|
218 |
|
|
219 |
|
|
220 |
section {* code generator setup *}
|
|
221 |
|
|
222 |
subsection {* type serializations *}
|
|
223 |
|
17632
|
224 |
types_code
|
|
225 |
set ("_ list")
|
|
226 |
attach (term_of) {*
|
|
227 |
fun term_of_set f T [] = Const ("{}", Type ("set", [T]))
|
|
228 |
| term_of_set f T (x :: xs) = Const ("insert",
|
|
229 |
T --> Type ("set", [T]) --> Type ("set", [T])) $ f x $ term_of_set f T xs;
|
|
230 |
*}
|
|
231 |
attach (test) {*
|
|
232 |
fun gen_set' aG i j = frequency
|
|
233 |
[(i, fn () => aG j :: gen_set' aG (i-1) j), (1, fn () => [])] ()
|
|
234 |
and gen_set aG i = gen_set' aG i i;
|
|
235 |
*}
|
|
236 |
|
19889
|
237 |
code_typapp set
|
18702
|
238 |
ml ("_ list")
|
18757
|
239 |
haskell (target_atom "[_]")
|
18702
|
240 |
|
19791
|
241 |
|
|
242 |
subsection {* const serializations *}
|
18702
|
243 |
|
17632
|
244 |
consts_code
|
|
245 |
"{}" ("[]")
|
19791
|
246 |
"insert" ("{*insertl*}")
|
|
247 |
"op Un" ("{*unionl*}")
|
|
248 |
"op Int" ("{*intersect*}")
|
|
249 |
"HOL.minus" :: "'a set \<Rightarrow> 'a set \<Rightarrow> 'a set"
|
|
250 |
("{*flip subtract*}")
|
|
251 |
"image" ("{*map_distinct*}")
|
|
252 |
"Union" ("{*unions*}")
|
|
253 |
"Inter" ("{*intersects*}")
|
|
254 |
"UNION" ("{*map_union*}")
|
|
255 |
"INTER" ("{*map_inter*}")
|
|
256 |
"Ball" ("{*Blall*}")
|
|
257 |
"Bex" ("{*Blex*}")
|
17632
|
258 |
|
19791
|
259 |
code_alias
|
|
260 |
"ExecutableSet.member" "List.member"
|
|
261 |
"ExecutableSet.insertl" "List.insertl"
|
|
262 |
"ExecutableSet.drop_first" "List.drop_first"
|
18963
|
263 |
|
19889
|
264 |
code_generate (ml, haskell)
|
|
265 |
insertl unionl intersect flip subtract map_distinct
|
|
266 |
unions intersects map_union map_inter Blall Blex
|
|
267 |
|
|
268 |
code_constapp
|
19791
|
269 |
"{}"
|
|
270 |
ml (target_atom "[]")
|
|
271 |
haskell (target_atom "[]")
|
|
272 |
insert
|
|
273 |
ml ("{*insertl*}")
|
|
274 |
haskell ("{*insertl*}")
|
|
275 |
"op \<union>"
|
|
276 |
ml ("{*unionl*}")
|
|
277 |
haskell ("{*unionl*}")
|
|
278 |
"op \<inter>"
|
|
279 |
ml ("{*intersect*}")
|
|
280 |
haskell ("{*intersect*}")
|
|
281 |
"op - :: 'a set \<Rightarrow> 'a set \<Rightarrow> 'a set"
|
|
282 |
ml ("{*flip subtract*}")
|
|
283 |
haskell ("{*flip subtract*}")
|
|
284 |
image
|
|
285 |
ml ("{*map_distinct*}")
|
|
286 |
haskell ("{*map_distinct*}")
|
|
287 |
"Union"
|
|
288 |
ml ("{*unions*}")
|
|
289 |
haskell ("{*unions*}")
|
|
290 |
"Inter"
|
|
291 |
ml ("{*intersects*}")
|
|
292 |
haskell ("{*intersects*}")
|
|
293 |
UNION
|
|
294 |
ml ("{*map_union*}")
|
|
295 |
haskell ("{*map_union*}")
|
|
296 |
INTER
|
|
297 |
ml ("{*map_inter*}")
|
|
298 |
haskell ("{*map_inter*}")
|
|
299 |
Ball
|
|
300 |
ml ("{*Blall*}")
|
|
301 |
haskell ("{*Blall*}")
|
|
302 |
Bex
|
|
303 |
ml ("{*Blex*}")
|
|
304 |
haskell ("{*Blex*}")
|
18702
|
305 |
|
17632
|
306 |
end
|