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