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