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