author | nipkow |
Tue, 17 Jun 2025 14:11:40 +0200 | |
changeset 82733 | 8b537e1af2ec |
parent 82596 | 267db8c321c4 |
permissions | -rw-r--r-- |
46237 | 1 |
(* Title: HOL/Library/DAList.thy |
58806 | 2 |
Author: Lukas Bulwahn, TU Muenchen |
3 |
*) |
|
46167 | 4 |
|
58881 | 5 |
section \<open>Abstract type of association lists with unique keys\<close> |
46167 | 6 |
|
46237 | 7 |
theory DAList |
46238
9ace9e5b79be
renaming theory AList_Impl back to AList (reverting 1fec5b365f9b; AList with distinct key invariant is called DAList)
bulwahn
parents:
46237
diff
changeset
|
8 |
imports AList |
46167 | 9 |
begin |
10 |
||
58806 | 11 |
text \<open>This was based on some existing fragments in the AFP-Collection framework.\<close> |
46167 | 12 |
|
58806 | 13 |
subsection \<open>Preliminaries\<close> |
47143
212f7a975d49
association lists with distinct keys uses the quotient infrastructure to obtain code certificates;
bulwahn
parents:
46507
diff
changeset
|
14 |
|
212f7a975d49
association lists with distinct keys uses the quotient infrastructure to obtain code certificates;
bulwahn
parents:
46507
diff
changeset
|
15 |
lemma distinct_map_fst_filter: |
58806 | 16 |
"distinct (map fst xs) \<Longrightarrow> distinct (map fst (List.filter P xs))" |
17 |
by (induct xs) auto |
|
18 |
||
47143
212f7a975d49
association lists with distinct keys uses the quotient infrastructure to obtain code certificates;
bulwahn
parents:
46507
diff
changeset
|
19 |
|
61585 | 20 |
subsection \<open>Type \<open>('key, 'value) alist\<close>\<close> |
46167 | 21 |
|
58806 | 22 |
typedef ('key, 'value) alist = "{xs :: ('key \<times> 'value) list. (distinct \<circ> map fst) xs}" |
46507 | 23 |
morphisms impl_of Alist |
24 |
proof |
|
67091 | 25 |
show "[] \<in> {xs. (distinct \<circ> map fst) xs}" |
58806 | 26 |
by simp |
46507 | 27 |
qed |
46167 | 28 |
|
47143
212f7a975d49
association lists with distinct keys uses the quotient infrastructure to obtain code certificates;
bulwahn
parents:
46507
diff
changeset
|
29 |
setup_lifting type_definition_alist |
212f7a975d49
association lists with distinct keys uses the quotient infrastructure to obtain code certificates;
bulwahn
parents:
46507
diff
changeset
|
30 |
|
46167 | 31 |
lemma alist_ext: "impl_of xs = impl_of ys \<Longrightarrow> xs = ys" |
58806 | 32 |
by (simp add: impl_of_inject) |
46167 | 33 |
|
34 |
lemma alist_eq_iff: "xs = ys \<longleftrightarrow> impl_of xs = impl_of ys" |
|
58806 | 35 |
by (simp add: impl_of_inject) |
46167 | 36 |
|
37 |
lemma impl_of_distinct [simp, intro]: "distinct (map fst (impl_of xs))" |
|
58806 | 38 |
using impl_of[of xs] by simp |
46167 | 39 |
|
82596 | 40 |
lemma impl_of_Alist: |
41 |
\<open>impl_of (Alist xs) = xs\<close> if \<open>distinct (map fst xs)\<close> |
|
42 |
using Alist_inverse [of xs] that by simp |
|
43 |
||
46167 | 44 |
lemma Alist_impl_of [code abstype]: "Alist (impl_of xs) = xs" |
58806 | 45 |
by (rule impl_of_inverse) |
46167 | 46 |
|
58806 | 47 |
|
48 |
subsection \<open>Primitive operations\<close> |
|
46167 | 49 |
|
55565
f663fc1e653b
simplify proofs because of the stronger reflexivity prover
kuncar
parents:
51143
diff
changeset
|
50 |
lift_definition lookup :: "('key, 'value) alist \<Rightarrow> 'key \<Rightarrow> 'value option" is map_of . |
46167 | 51 |
|
58806 | 52 |
lift_definition empty :: "('key, 'value) alist" is "[]" |
53 |
by simp |
|
46167 | 54 |
|
47308 | 55 |
lift_definition update :: "'key \<Rightarrow> 'value \<Rightarrow> ('key, 'value) alist \<Rightarrow> ('key, 'value) alist" |
56 |
is AList.update |
|
58806 | 57 |
by (simp add: distinct_update) |
46167 | 58 |
|
59 |
(* FIXME: we use an unoptimised delete operation. *) |
|
47308 | 60 |
lift_definition delete :: "'key \<Rightarrow> ('key, 'value) alist \<Rightarrow> ('key, 'value) alist" |
61 |
is AList.delete |
|
58806 | 62 |
by (simp add: distinct_delete) |
46167 | 63 |
|
58806 | 64 |
lift_definition map_entry :: |
65 |
"'key \<Rightarrow> ('value \<Rightarrow> 'value) \<Rightarrow> ('key, 'value) alist \<Rightarrow> ('key, 'value) alist" |
|
47308 | 66 |
is AList.map_entry |
58806 | 67 |
by (simp add: distinct_map_entry) |
46167 | 68 |
|
47308 | 69 |
lift_definition filter :: "('key \<times> 'value \<Rightarrow> bool) \<Rightarrow> ('key, 'value) alist \<Rightarrow> ('key, 'value) alist" |
70 |
is List.filter |
|
58806 | 71 |
by (simp add: distinct_map_fst_filter) |
46167 | 72 |
|
58806 | 73 |
lift_definition map_default :: |
74 |
"'key \<Rightarrow> 'value \<Rightarrow> ('value \<Rightarrow> 'value) \<Rightarrow> ('key, 'value) alist \<Rightarrow> ('key, 'value) alist" |
|
47308 | 75 |
is AList.map_default |
58806 | 76 |
by (simp add: distinct_map_default) |
46167 | 77 |
|
58806 | 78 |
|
79 |
subsection \<open>Abstract operation properties\<close> |
|
46167 | 80 |
|
81 |
(* FIXME: to be completed *) |
|
82 |
||
83 |
lemma lookup_empty [simp]: "lookup empty k = None" |
|
63684 | 84 |
by (simp add: empty_def lookup_def Alist_inverse) |
85 |
||
86 |
lemma lookup_update: |
|
87 |
"lookup (update k1 v xs) k2 = (if k1 = k2 then Some v else lookup xs k2)" |
|
88 |
by(transfer)(simp add: update_conv') |
|
89 |
||
90 |
lemma lookup_update_eq [simp]: |
|
91 |
"k1 = k2 \<Longrightarrow> lookup (update k1 v xs) k2 = Some v" |
|
92 |
by(simp add: lookup_update) |
|
93 |
||
94 |
lemma lookup_update_neq [simp]: |
|
95 |
"k1 \<noteq> k2 \<Longrightarrow> lookup (update k1 v xs) k2 = lookup xs k2" |
|
96 |
by(simp add: lookup_update) |
|
97 |
||
98 |
lemma update_update_eq [simp]: |
|
99 |
"k1 = k2 \<Longrightarrow> update k2 v2 (update k1 v1 xs) = update k2 v2 xs" |
|
100 |
by(transfer)(simp add: update_conv') |
|
46167 | 101 |
|
102 |
lemma lookup_delete [simp]: "lookup (delete k al) = (lookup al)(k := None)" |
|
58806 | 103 |
by (simp add: lookup_def delete_def Alist_inverse distinct_delete delete_conv') |
46167 | 104 |
|
58806 | 105 |
|
106 |
subsection \<open>Further operations\<close> |
|
46167 | 107 |
|
58806 | 108 |
subsubsection \<open>Equality\<close> |
46167 | 109 |
|
58806 | 110 |
instantiation alist :: (equal, equal) equal |
111 |
begin |
|
46167 | 112 |
|
113 |
definition "HOL.equal (xs :: ('a, 'b) alist) ys == impl_of xs = impl_of ys" |
|
114 |
||
115 |
instance |
|
60679 | 116 |
by standard (simp add: equal_alist_def impl_of_inject) |
46167 | 117 |
|
118 |
end |
|
119 |
||
58806 | 120 |
|
121 |
subsubsection \<open>Size\<close> |
|
46167 | 122 |
|
58806 | 123 |
instantiation alist :: (type, type) size |
124 |
begin |
|
46167 | 125 |
|
126 |
definition "size (al :: ('a, 'b) alist) = length (impl_of al)" |
|
127 |
||
128 |
instance .. |
|
129 |
||
130 |
end |
|
131 |
||
58806 | 132 |
|
133 |
subsection \<open>Quickcheck generators\<close> |
|
46167 | 134 |
|
72607 | 135 |
context |
81113 | 136 |
includes state_combinator_syntax and term_syntax |
72607 | 137 |
begin |
138 |
||
139 |
definition |
|
46167 | 140 |
valterm_empty :: "('key :: typerep, 'value :: typerep) alist \<times> (unit \<Rightarrow> Code_Evaluation.term)" |
58806 | 141 |
where "valterm_empty = Code_Evaluation.valtermify empty" |
46167 | 142 |
|
72607 | 143 |
definition |
46167 | 144 |
valterm_update :: "'key :: typerep \<times> (unit \<Rightarrow> Code_Evaluation.term) \<Rightarrow> |
145 |
'value :: typerep \<times> (unit \<Rightarrow> Code_Evaluation.term) \<Rightarrow> |
|
146 |
('key, 'value) alist \<times> (unit \<Rightarrow> Code_Evaluation.term) \<Rightarrow> |
|
147 |
('key, 'value) alist \<times> (unit \<Rightarrow> Code_Evaluation.term)" where |
|
148 |
[code_unfold]: "valterm_update k v a = Code_Evaluation.valtermify update {\<cdot>} k {\<cdot>} v {\<cdot>}a" |
|
149 |
||
72581 | 150 |
fun random_aux_alist |
46167 | 151 |
where |
58806 | 152 |
"random_aux_alist i j = |
153 |
(if i = 0 then Pair valterm_empty |
|
154 |
else Quickcheck_Random.collapse |
|
155 |
(Random.select_weight |
|
156 |
[(i, Quickcheck_Random.random j \<circ>\<rightarrow> (\<lambda>k. Quickcheck_Random.random j \<circ>\<rightarrow> |
|
157 |
(\<lambda>v. random_aux_alist (i - 1) j \<circ>\<rightarrow> (\<lambda>a. Pair (valterm_update k v a))))), |
|
158 |
(1, Pair valterm_empty)]))" |
|
46167 | 159 |
|
72581 | 160 |
end |
161 |
||
46167 | 162 |
instantiation alist :: (random, random) random |
163 |
begin |
|
164 |
||
165 |
definition random_alist |
|
166 |
where |
|
167 |
"random_alist i = random_aux_alist i i" |
|
58806 | 168 |
|
46167 | 169 |
instance .. |
170 |
||
171 |
end |
|
172 |
||
173 |
instantiation alist :: (exhaustive, exhaustive) exhaustive |
|
174 |
begin |
|
175 |
||
58806 | 176 |
fun exhaustive_alist :: |
177 |
"(('a, 'b) alist \<Rightarrow> (bool \<times> term list) option) \<Rightarrow> natural \<Rightarrow> (bool \<times> term list) option" |
|
46167 | 178 |
where |
58806 | 179 |
"exhaustive_alist f i = |
180 |
(if i = 0 then None |
|
181 |
else |
|
182 |
case f empty of |
|
183 |
Some ts \<Rightarrow> Some ts |
|
184 |
| None \<Rightarrow> |
|
185 |
exhaustive_alist |
|
186 |
(\<lambda>a. Quickcheck_Exhaustive.exhaustive |
|
187 |
(\<lambda>k. Quickcheck_Exhaustive.exhaustive (\<lambda>v. f (update k v a)) (i - 1)) (i - 1)) |
|
188 |
(i - 1))" |
|
46167 | 189 |
|
190 |
instance .. |
|
191 |
||
192 |
end |
|
193 |
||
194 |
instantiation alist :: (full_exhaustive, full_exhaustive) full_exhaustive |
|
195 |
begin |
|
196 |
||
58806 | 197 |
fun full_exhaustive_alist :: |
198 |
"(('a, 'b) alist \<times> (unit \<Rightarrow> term) \<Rightarrow> (bool \<times> term list) option) \<Rightarrow> natural \<Rightarrow> |
|
199 |
(bool \<times> term list) option" |
|
46167 | 200 |
where |
58806 | 201 |
"full_exhaustive_alist f i = |
202 |
(if i = 0 then None |
|
203 |
else |
|
204 |
case f valterm_empty of |
|
205 |
Some ts \<Rightarrow> Some ts |
|
206 |
| None \<Rightarrow> |
|
207 |
full_exhaustive_alist |
|
208 |
(\<lambda>a. |
|
209 |
Quickcheck_Exhaustive.full_exhaustive |
|
210 |
(\<lambda>k. Quickcheck_Exhaustive.full_exhaustive (\<lambda>v. f (valterm_update k v a)) (i - 1)) |
|
211 |
(i - 1)) |
|
212 |
(i - 1))" |
|
46167 | 213 |
|
214 |
instance .. |
|
215 |
||
216 |
end |
|
217 |
||
59581 | 218 |
|
219 |
section \<open>alist is a BNF\<close> |
|
220 |
||
60919 | 221 |
lift_bnf (dead 'k, set: 'v) alist [wits: "[] :: ('k \<times> 'v) list"] for map: map rel: rel |
222 |
by auto |
|
59581 | 223 |
|
46167 | 224 |
hide_const valterm_empty valterm_update random_aux_alist |
225 |
||
46171
19f68d7671f0
proper hiding of facts and constants in AList_Impl and AList theory
bulwahn
parents:
46167
diff
changeset
|
226 |
hide_fact (open) lookup_def empty_def update_def delete_def map_entry_def filter_def map_default_def |
59581 | 227 |
hide_const (open) impl_of lookup empty update delete map_entry filter map_default map set rel |
46167 | 228 |
|
46238
9ace9e5b79be
renaming theory AList_Impl back to AList (reverting 1fec5b365f9b; AList with distinct key invariant is called DAList)
bulwahn
parents:
46237
diff
changeset
|
229 |
end |