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