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