bulwahn@46237
|
1 |
(* Title: HOL/Library/DAList.thy
|
bulwahn@46167
|
2 |
Author: Lukas Bulwahn, TU Muenchen *)
|
bulwahn@46167
|
3 |
|
bulwahn@46167
|
4 |
header {* Abstract type of association lists with unique keys *}
|
bulwahn@46167
|
5 |
|
bulwahn@46237
|
6 |
theory DAList
|
bulwahn@46238
|
7 |
imports AList
|
bulwahn@46167
|
8 |
begin
|
bulwahn@46167
|
9 |
|
bulwahn@46167
|
10 |
text {* This was based on some existing fragments in the AFP-Collection framework. *}
|
bulwahn@46167
|
11 |
|
bulwahn@47143
|
12 |
subsection {* Preliminaries *}
|
bulwahn@47143
|
13 |
|
bulwahn@47143
|
14 |
lemma distinct_map_fst_filter:
|
bulwahn@47143
|
15 |
"distinct (map fst xs) ==> distinct (map fst (List.filter P xs))"
|
bulwahn@47143
|
16 |
by (induct xs) auto
|
bulwahn@47143
|
17 |
|
bulwahn@46167
|
18 |
subsection {* Type @{text "('key, 'value) alist" } *}
|
bulwahn@46167
|
19 |
|
wenzelm@49834
|
20 |
typedef ('key, 'value) alist = "{xs :: ('key \<times> 'value) list. (distinct o map fst) xs}"
|
wenzelm@46507
|
21 |
morphisms impl_of Alist
|
wenzelm@46507
|
22 |
proof
|
bulwahn@47143
|
23 |
show "[] \<in> {xs. (distinct o map fst) xs}" by simp
|
wenzelm@46507
|
24 |
qed
|
bulwahn@46167
|
25 |
|
bulwahn@47143
|
26 |
setup_lifting type_definition_alist
|
bulwahn@47143
|
27 |
|
bulwahn@46167
|
28 |
lemma alist_ext: "impl_of xs = impl_of ys \<Longrightarrow> xs = ys"
|
bulwahn@46167
|
29 |
by(simp add: impl_of_inject)
|
bulwahn@46167
|
30 |
|
bulwahn@46167
|
31 |
lemma alist_eq_iff: "xs = ys \<longleftrightarrow> impl_of xs = impl_of ys"
|
bulwahn@46167
|
32 |
by(simp add: impl_of_inject)
|
bulwahn@46167
|
33 |
|
bulwahn@46167
|
34 |
lemma impl_of_distinct [simp, intro]: "distinct (map fst (impl_of xs))"
|
bulwahn@46167
|
35 |
using impl_of[of xs] by simp
|
bulwahn@46167
|
36 |
|
bulwahn@46167
|
37 |
lemma Alist_impl_of [code abstype]: "Alist (impl_of xs) = xs"
|
bulwahn@46167
|
38 |
by(rule impl_of_inverse)
|
bulwahn@46167
|
39 |
|
bulwahn@46167
|
40 |
subsection {* Primitive operations *}
|
bulwahn@46167
|
41 |
|
kuncar@47308
|
42 |
lift_definition lookup :: "('key, 'value) alist \<Rightarrow> 'key \<Rightarrow> 'value option" is map_of ..
|
bulwahn@46167
|
43 |
|
kuncar@47308
|
44 |
lift_definition empty :: "('key, 'value) alist" is "[]" by simp
|
bulwahn@46167
|
45 |
|
kuncar@47308
|
46 |
lift_definition update :: "'key \<Rightarrow> 'value \<Rightarrow> ('key, 'value) alist \<Rightarrow> ('key, 'value) alist"
|
kuncar@47308
|
47 |
is AList.update
|
bulwahn@47143
|
48 |
by (simp add: distinct_update)
|
bulwahn@46167
|
49 |
|
bulwahn@46167
|
50 |
(* FIXME: we use an unoptimised delete operation. *)
|
kuncar@47308
|
51 |
lift_definition delete :: "'key \<Rightarrow> ('key, 'value) alist \<Rightarrow> ('key, 'value) alist"
|
kuncar@47308
|
52 |
is AList.delete
|
bulwahn@47143
|
53 |
by (simp add: distinct_delete)
|
bulwahn@46167
|
54 |
|
kuncar@47308
|
55 |
lift_definition map_entry :: "'key \<Rightarrow> ('value \<Rightarrow> 'value) \<Rightarrow> ('key, 'value) alist \<Rightarrow> ('key, 'value) alist"
|
kuncar@47308
|
56 |
is AList.map_entry
|
bulwahn@47143
|
57 |
by (simp add: distinct_map_entry)
|
bulwahn@46167
|
58 |
|
kuncar@47308
|
59 |
lift_definition filter :: "('key \<times> 'value \<Rightarrow> bool) \<Rightarrow> ('key, 'value) alist \<Rightarrow> ('key, 'value) alist"
|
kuncar@47308
|
60 |
is List.filter
|
bulwahn@47143
|
61 |
by (simp add: distinct_map_fst_filter)
|
bulwahn@46167
|
62 |
|
kuncar@47308
|
63 |
lift_definition map_default :: "'key => 'value => ('value => 'value) => ('key, 'value) alist => ('key, 'value) alist"
|
kuncar@47308
|
64 |
is AList.map_default
|
bulwahn@47143
|
65 |
by (simp add: distinct_map_default)
|
bulwahn@46167
|
66 |
|
bulwahn@46167
|
67 |
subsection {* Abstract operation properties *}
|
bulwahn@46167
|
68 |
|
bulwahn@46167
|
69 |
(* FIXME: to be completed *)
|
bulwahn@46167
|
70 |
|
bulwahn@46167
|
71 |
lemma lookup_empty [simp]: "lookup empty k = None"
|
bulwahn@46167
|
72 |
by(simp add: empty_def lookup_def Alist_inverse)
|
bulwahn@46167
|
73 |
|
bulwahn@46167
|
74 |
lemma lookup_delete [simp]: "lookup (delete k al) = (lookup al)(k := None)"
|
bulwahn@46167
|
75 |
by (simp add: lookup_def delete_def Alist_inverse distinct_delete delete_conv')
|
bulwahn@46167
|
76 |
|
bulwahn@46167
|
77 |
subsection {* Further operations *}
|
bulwahn@46167
|
78 |
|
bulwahn@46167
|
79 |
subsubsection {* Equality *}
|
bulwahn@46167
|
80 |
|
bulwahn@46167
|
81 |
instantiation alist :: (equal, equal) equal begin
|
bulwahn@46167
|
82 |
|
bulwahn@46167
|
83 |
definition "HOL.equal (xs :: ('a, 'b) alist) ys == impl_of xs = impl_of ys"
|
bulwahn@46167
|
84 |
|
bulwahn@46167
|
85 |
instance
|
bulwahn@46167
|
86 |
proof
|
bulwahn@46167
|
87 |
qed (simp add: equal_alist_def impl_of_inject)
|
bulwahn@46167
|
88 |
|
bulwahn@46167
|
89 |
end
|
bulwahn@46167
|
90 |
|
bulwahn@46167
|
91 |
subsubsection {* Size *}
|
bulwahn@46167
|
92 |
|
bulwahn@46167
|
93 |
instantiation alist :: (type, type) size begin
|
bulwahn@46167
|
94 |
|
bulwahn@46167
|
95 |
definition "size (al :: ('a, 'b) alist) = length (impl_of al)"
|
bulwahn@46167
|
96 |
|
bulwahn@46167
|
97 |
instance ..
|
bulwahn@46167
|
98 |
|
bulwahn@46167
|
99 |
end
|
bulwahn@46167
|
100 |
|
bulwahn@46167
|
101 |
subsection {* Quickcheck generators *}
|
bulwahn@46167
|
102 |
|
bulwahn@46167
|
103 |
notation fcomp (infixl "\<circ>>" 60)
|
bulwahn@46167
|
104 |
notation scomp (infixl "\<circ>\<rightarrow>" 60)
|
bulwahn@46167
|
105 |
|
bulwahn@46167
|
106 |
definition (in term_syntax)
|
bulwahn@46167
|
107 |
valterm_empty :: "('key :: typerep, 'value :: typerep) alist \<times> (unit \<Rightarrow> Code_Evaluation.term)"
|
bulwahn@46167
|
108 |
where
|
bulwahn@46167
|
109 |
"valterm_empty = Code_Evaluation.valtermify empty"
|
bulwahn@46167
|
110 |
|
bulwahn@46167
|
111 |
definition (in term_syntax)
|
bulwahn@46167
|
112 |
valterm_update :: "'key :: typerep \<times> (unit \<Rightarrow> Code_Evaluation.term) \<Rightarrow>
|
bulwahn@46167
|
113 |
'value :: typerep \<times> (unit \<Rightarrow> Code_Evaluation.term) \<Rightarrow>
|
bulwahn@46167
|
114 |
('key, 'value) alist \<times> (unit \<Rightarrow> Code_Evaluation.term) \<Rightarrow>
|
bulwahn@46167
|
115 |
('key, 'value) alist \<times> (unit \<Rightarrow> Code_Evaluation.term)" where
|
bulwahn@46167
|
116 |
[code_unfold]: "valterm_update k v a = Code_Evaluation.valtermify update {\<cdot>} k {\<cdot>} v {\<cdot>}a"
|
bulwahn@46167
|
117 |
|
bulwahn@46167
|
118 |
fun (in term_syntax) random_aux_alist
|
bulwahn@46167
|
119 |
where
|
haftmann@51126
|
120 |
"random_aux_alist i j = (if i = 0 then Pair valterm_empty else Quickcheck_Random.collapse (Random.select_weight [(i, Quickcheck_Random.random j \<circ>\<rightarrow> (%k. Quickcheck_Random.random j \<circ>\<rightarrow> (%v. random_aux_alist (i - 1) j \<circ>\<rightarrow> (%a. Pair (valterm_update k v a))))), (1, Pair valterm_empty)]))"
|
bulwahn@46167
|
121 |
|
bulwahn@46167
|
122 |
instantiation alist :: (random, random) random
|
bulwahn@46167
|
123 |
begin
|
bulwahn@46167
|
124 |
|
bulwahn@46167
|
125 |
definition random_alist
|
bulwahn@46167
|
126 |
where
|
bulwahn@46167
|
127 |
"random_alist i = random_aux_alist i i"
|
bulwahn@46167
|
128 |
|
bulwahn@46167
|
129 |
instance ..
|
bulwahn@46167
|
130 |
|
bulwahn@46167
|
131 |
end
|
bulwahn@46167
|
132 |
|
bulwahn@46167
|
133 |
no_notation fcomp (infixl "\<circ>>" 60)
|
bulwahn@46167
|
134 |
no_notation scomp (infixl "\<circ>\<rightarrow>" 60)
|
bulwahn@46167
|
135 |
|
bulwahn@46167
|
136 |
instantiation alist :: (exhaustive, exhaustive) exhaustive
|
bulwahn@46167
|
137 |
begin
|
bulwahn@46167
|
138 |
|
haftmann@51143
|
139 |
fun exhaustive_alist :: "(('a, 'b) alist => (bool * term list) option) => natural => (bool * term list) option"
|
bulwahn@46167
|
140 |
where
|
bulwahn@46167
|
141 |
"exhaustive_alist f i = (if i = 0 then None else case f empty of Some ts => Some ts | None =>
|
bulwahn@46167
|
142 |
exhaustive_alist (%a. Quickcheck_Exhaustive.exhaustive (%k. Quickcheck_Exhaustive.exhaustive (%v. f (update k v a)) (i - 1)) (i - 1)) (i - 1))"
|
bulwahn@46167
|
143 |
|
bulwahn@46167
|
144 |
instance ..
|
bulwahn@46167
|
145 |
|
bulwahn@46167
|
146 |
end
|
bulwahn@46167
|
147 |
|
bulwahn@46167
|
148 |
instantiation alist :: (full_exhaustive, full_exhaustive) full_exhaustive
|
bulwahn@46167
|
149 |
begin
|
bulwahn@46167
|
150 |
|
haftmann@51143
|
151 |
fun full_exhaustive_alist :: "(('a, 'b) alist * (unit => term) => (bool * term list) option) => natural => (bool * term list) option"
|
bulwahn@46167
|
152 |
where
|
bulwahn@46167
|
153 |
"full_exhaustive_alist f i = (if i = 0 then None else case f valterm_empty of Some ts => Some ts | None =>
|
bulwahn@46167
|
154 |
full_exhaustive_alist (%a. Quickcheck_Exhaustive.full_exhaustive (%k. Quickcheck_Exhaustive.full_exhaustive (%v. f (valterm_update k v a)) (i - 1)) (i - 1)) (i - 1))"
|
bulwahn@46167
|
155 |
|
bulwahn@46167
|
156 |
instance ..
|
bulwahn@46167
|
157 |
|
bulwahn@46167
|
158 |
end
|
bulwahn@46167
|
159 |
|
bulwahn@46167
|
160 |
hide_const valterm_empty valterm_update random_aux_alist
|
bulwahn@46167
|
161 |
|
bulwahn@46171
|
162 |
hide_fact (open) lookup_def empty_def update_def delete_def map_entry_def filter_def map_default_def
|
bulwahn@46167
|
163 |
hide_const (open) impl_of lookup empty update delete map_entry filter map_default
|
bulwahn@46167
|
164 |
|
bulwahn@46238
|
165 |
end
|