src/HOL/Library/DAList_Multiset.thy
author Andreas Lochbihler
Fri Sep 20 10:09:16 2013 +0200 (2013-09-20)
changeset 53745 788730ab7da4
parent 51623 1194b438426a
child 55808 488c3e8282c8
permissions -rw-r--r--
prefer Code.abort over code_abort
haftmann@51599
     1
(*  Title:      HOL/Library/DAList_Multiset.thy
haftmann@51599
     2
    Author:     Lukas Bulwahn, TU Muenchen
haftmann@51599
     3
*)
haftmann@51599
     4
haftmann@51599
     5
header {* Multisets partially implemented by association lists *}
haftmann@51599
     6
haftmann@51599
     7
theory DAList_Multiset
haftmann@51599
     8
imports Multiset DAList
haftmann@51599
     9
begin
haftmann@51599
    10
haftmann@51600
    11
text {* Delete prexisting code equations *}
haftmann@51600
    12
haftmann@51600
    13
lemma [code, code del]:
haftmann@51623
    14
  "{#} = {#}"
haftmann@51623
    15
  ..
haftmann@51623
    16
haftmann@51623
    17
lemma [code, code del]:
haftmann@51623
    18
  "single = single"
haftmann@51623
    19
  ..
haftmann@51623
    20
haftmann@51623
    21
lemma [code, code del]:
haftmann@51600
    22
  "plus = (plus :: 'a multiset \<Rightarrow> _)"
haftmann@51600
    23
  ..
haftmann@51600
    24
haftmann@51600
    25
lemma [code, code del]:
haftmann@51600
    26
  "minus = (minus :: 'a multiset \<Rightarrow> _)"
haftmann@51600
    27
  ..
haftmann@51600
    28
haftmann@51600
    29
lemma [code, code del]:
haftmann@51600
    30
  "inf = (inf :: 'a multiset \<Rightarrow> _)"
haftmann@51600
    31
  ..
haftmann@51600
    32
haftmann@51600
    33
lemma [code, code del]:
haftmann@51623
    34
  "sup = (sup :: 'a multiset \<Rightarrow> _)"
haftmann@51623
    35
  ..
haftmann@51623
    36
haftmann@51623
    37
lemma [code, code del]:
haftmann@51600
    38
  "image_mset = image_mset"
haftmann@51600
    39
  ..
haftmann@51600
    40
haftmann@51600
    41
lemma [code, code del]:
haftmann@51600
    42
  "Multiset.filter = Multiset.filter"
haftmann@51600
    43
  ..
haftmann@51600
    44
haftmann@51600
    45
lemma [code, code del]:
haftmann@51600
    46
  "count = count"
haftmann@51600
    47
  ..
haftmann@51600
    48
haftmann@51600
    49
lemma [code, code del]:
haftmann@51600
    50
  "mcard = mcard"
haftmann@51600
    51
  ..
haftmann@51600
    52
haftmann@51600
    53
lemma [code, code del]:
haftmann@51600
    54
  "msetsum = msetsum"
haftmann@51600
    55
  ..
haftmann@51600
    56
haftmann@51600
    57
lemma [code, code del]:
haftmann@51600
    58
  "msetprod = msetprod"
haftmann@51600
    59
  ..
haftmann@51600
    60
haftmann@51600
    61
lemma [code, code del]:
haftmann@51600
    62
  "set_of = set_of"
haftmann@51600
    63
  ..
haftmann@51600
    64
haftmann@51600
    65
lemma [code, code del]:
haftmann@51600
    66
  "sorted_list_of_multiset = sorted_list_of_multiset"
haftmann@51600
    67
  ..
haftmann@51600
    68
haftmann@51600
    69
haftmann@51599
    70
text {* Raw operations on lists *}
haftmann@51599
    71
haftmann@51599
    72
definition join_raw :: "('key \<Rightarrow> 'val \<times> 'val \<Rightarrow> 'val) \<Rightarrow> ('key \<times> 'val) list \<Rightarrow> ('key \<times> 'val) list \<Rightarrow> ('key \<times> 'val) list"
haftmann@51599
    73
where
haftmann@51599
    74
  "join_raw f xs ys = foldr (\<lambda>(k, v). map_default k v (%v'. f k (v', v))) ys xs"
haftmann@51599
    75
haftmann@51599
    76
lemma join_raw_Nil [simp]:
haftmann@51599
    77
  "join_raw f xs [] = xs"
haftmann@51599
    78
by (simp add: join_raw_def)
haftmann@51599
    79
haftmann@51599
    80
lemma join_raw_Cons [simp]:
haftmann@51599
    81
  "join_raw f xs ((k, v) # ys) = map_default k v (%v'. f k (v', v)) (join_raw f xs ys)"
haftmann@51599
    82
by (simp add: join_raw_def)
haftmann@51599
    83
haftmann@51599
    84
lemma map_of_join_raw:
haftmann@51599
    85
  assumes "distinct (map fst ys)"
haftmann@51599
    86
  shows "map_of (join_raw f xs ys) x = (case map_of xs x of None => map_of ys x | Some v =>
haftmann@51599
    87
    (case map_of ys x of None => Some v | Some v' => Some (f x (v, v'))))"
haftmann@51599
    88
using assms
haftmann@51599
    89
apply (induct ys)
haftmann@51599
    90
apply (auto simp add: map_of_map_default split: option.split)
haftmann@51599
    91
apply (metis map_of_eq_None_iff option.simps(2) weak_map_of_SomeI)
haftmann@51599
    92
by (metis Some_eq_map_of_iff map_of_eq_None_iff option.simps(2))
haftmann@51599
    93
haftmann@51599
    94
lemma distinct_join_raw:
haftmann@51599
    95
  assumes "distinct (map fst xs)"
haftmann@51599
    96
  shows "distinct (map fst (join_raw f xs ys))"
haftmann@51599
    97
using assms
haftmann@51599
    98
proof (induct ys)
haftmann@51599
    99
  case (Cons y ys)
haftmann@51599
   100
  thus ?case by (cases y) (simp add: distinct_map_default)
haftmann@51599
   101
qed auto
haftmann@51599
   102
haftmann@51599
   103
definition
haftmann@51599
   104
  "subtract_entries_raw xs ys = foldr (%(k, v). AList.map_entry k (%v'. v' - v)) ys xs"
haftmann@51599
   105
haftmann@51599
   106
lemma map_of_subtract_entries_raw:
haftmann@51599
   107
  assumes "distinct (map fst ys)"
haftmann@51599
   108
  shows "map_of (subtract_entries_raw xs ys) x = (case map_of xs x of None => None | Some v =>
haftmann@51599
   109
    (case map_of ys x of None => Some v | Some v' => Some (v - v')))"
haftmann@51599
   110
using assms unfolding subtract_entries_raw_def
haftmann@51599
   111
apply (induct ys)
haftmann@51599
   112
apply auto
haftmann@51599
   113
apply (simp split: option.split)
haftmann@51599
   114
apply (simp add: map_of_map_entry)
haftmann@51599
   115
apply (auto split: option.split)
haftmann@51599
   116
apply (metis map_of_eq_None_iff option.simps(3) option.simps(4))
haftmann@51599
   117
by (metis map_of_eq_None_iff option.simps(4) option.simps(5))
haftmann@51599
   118
haftmann@51599
   119
lemma distinct_subtract_entries_raw:
haftmann@51599
   120
  assumes "distinct (map fst xs)"
haftmann@51599
   121
  shows "distinct (map fst (subtract_entries_raw xs ys))"
haftmann@51599
   122
using assms
haftmann@51599
   123
unfolding subtract_entries_raw_def by (induct ys) (auto simp add: distinct_map_entry)
haftmann@51599
   124
haftmann@51599
   125
haftmann@51599
   126
text {* Operations on alists with distinct keys *}
haftmann@51599
   127
haftmann@51599
   128
lift_definition join :: "('a \<Rightarrow> 'b \<times> 'b \<Rightarrow> 'b) \<Rightarrow> ('a, 'b) alist \<Rightarrow> ('a, 'b) alist \<Rightarrow> ('a, 'b) alist" 
haftmann@51599
   129
is join_raw
haftmann@51599
   130
by (simp add: distinct_join_raw)
haftmann@51599
   131
haftmann@51599
   132
lift_definition subtract_entries :: "('a, ('b :: minus)) alist \<Rightarrow> ('a, 'b) alist \<Rightarrow> ('a, 'b) alist"
haftmann@51599
   133
is subtract_entries_raw 
haftmann@51599
   134
by (simp add: distinct_subtract_entries_raw)
haftmann@51599
   135
haftmann@51599
   136
haftmann@51599
   137
text {* Implementing multisets by means of association lists *}
haftmann@51599
   138
haftmann@51599
   139
definition count_of :: "('a \<times> nat) list \<Rightarrow> 'a \<Rightarrow> nat" where
haftmann@51599
   140
  "count_of xs x = (case map_of xs x of None \<Rightarrow> 0 | Some n \<Rightarrow> n)"
haftmann@51599
   141
haftmann@51599
   142
lemma count_of_multiset:
haftmann@51599
   143
  "count_of xs \<in> multiset"
haftmann@51599
   144
proof -
haftmann@51599
   145
  let ?A = "{x::'a. 0 < (case map_of xs x of None \<Rightarrow> 0\<Colon>nat | Some (n\<Colon>nat) \<Rightarrow> n)}"
haftmann@51599
   146
  have "?A \<subseteq> dom (map_of xs)"
haftmann@51599
   147
  proof
haftmann@51599
   148
    fix x
haftmann@51599
   149
    assume "x \<in> ?A"
haftmann@51599
   150
    then have "0 < (case map_of xs x of None \<Rightarrow> 0\<Colon>nat | Some (n\<Colon>nat) \<Rightarrow> n)" by simp
haftmann@51599
   151
    then have "map_of xs x \<noteq> None" by (cases "map_of xs x") auto
haftmann@51599
   152
    then show "x \<in> dom (map_of xs)" by auto
haftmann@51599
   153
  qed
haftmann@51599
   154
  with finite_dom_map_of [of xs] have "finite ?A"
haftmann@51599
   155
    by (auto intro: finite_subset)
haftmann@51599
   156
  then show ?thesis
haftmann@51599
   157
    by (simp add: count_of_def fun_eq_iff multiset_def)
haftmann@51599
   158
qed
haftmann@51599
   159
haftmann@51599
   160
lemma count_simps [simp]:
haftmann@51599
   161
  "count_of [] = (\<lambda>_. 0)"
haftmann@51599
   162
  "count_of ((x, n) # xs) = (\<lambda>y. if x = y then n else count_of xs y)"
haftmann@51599
   163
  by (simp_all add: count_of_def fun_eq_iff)
haftmann@51599
   164
haftmann@51599
   165
lemma count_of_empty:
haftmann@51599
   166
  "x \<notin> fst ` set xs \<Longrightarrow> count_of xs x = 0"
haftmann@51599
   167
  by (induct xs) (simp_all add: count_of_def)
haftmann@51599
   168
haftmann@51599
   169
lemma count_of_filter:
haftmann@51599
   170
  "count_of (List.filter (P \<circ> fst) xs) x = (if P x then count_of xs x else 0)"
haftmann@51599
   171
  by (induct xs) auto
haftmann@51599
   172
haftmann@51599
   173
lemma count_of_map_default [simp]:
haftmann@51599
   174
  "count_of (map_default x b (%x. x + b) xs) y = (if x = y then count_of xs x + b else count_of xs y)"
haftmann@51599
   175
unfolding count_of_def by (simp add: map_of_map_default split: option.split)
haftmann@51599
   176
haftmann@51599
   177
lemma count_of_join_raw:
haftmann@51599
   178
  "distinct (map fst ys) ==> count_of xs x + count_of ys x = count_of (join_raw (%x (x, y). x + y) xs ys) x"
haftmann@51599
   179
unfolding count_of_def by (simp add: map_of_join_raw split: option.split)
haftmann@51599
   180
haftmann@51599
   181
lemma count_of_subtract_entries_raw:
haftmann@51599
   182
  "distinct (map fst ys) ==> count_of xs x - count_of ys x = count_of (subtract_entries_raw xs ys) x"
haftmann@51599
   183
unfolding count_of_def by (simp add: map_of_subtract_entries_raw split: option.split)
haftmann@51599
   184
haftmann@51599
   185
haftmann@51599
   186
text {* Code equations for multiset operations *}
haftmann@51599
   187
haftmann@51599
   188
definition Bag :: "('a, nat) alist \<Rightarrow> 'a multiset" where
haftmann@51599
   189
  "Bag xs = Abs_multiset (count_of (DAList.impl_of xs))"
haftmann@51599
   190
haftmann@51599
   191
code_datatype Bag
haftmann@51599
   192
haftmann@51599
   193
lemma count_Bag [simp, code]:
haftmann@51599
   194
  "count (Bag xs) = count_of (DAList.impl_of xs)"
haftmann@51599
   195
  by (simp add: Bag_def count_of_multiset Abs_multiset_inverse)
haftmann@51599
   196
haftmann@51599
   197
lemma Mempty_Bag [code]:
haftmann@51599
   198
  "{#} = Bag (DAList.empty)"
haftmann@51599
   199
  by (simp add: multiset_eq_iff alist.Alist_inverse DAList.empty_def)
haftmann@51599
   200
haftmann@51599
   201
lemma single_Bag [code]:
haftmann@51599
   202
  "{#x#} = Bag (DAList.update x 1 DAList.empty)"
haftmann@51599
   203
  by (simp add: multiset_eq_iff alist.Alist_inverse update.rep_eq empty.rep_eq)
haftmann@51599
   204
haftmann@51599
   205
lemma union_Bag [code]:
haftmann@51599
   206
  "Bag xs + Bag ys = Bag (join (\<lambda>x (n1, n2). n1 + n2) xs ys)"
haftmann@51599
   207
by (rule multiset_eqI) (simp add: count_of_join_raw alist.Alist_inverse distinct_join_raw join_def)
haftmann@51599
   208
haftmann@51599
   209
lemma minus_Bag [code]:
haftmann@51599
   210
  "Bag xs - Bag ys = Bag (subtract_entries xs ys)"
haftmann@51599
   211
by (rule multiset_eqI)
haftmann@51599
   212
  (simp add: count_of_subtract_entries_raw alist.Alist_inverse distinct_subtract_entries_raw subtract_entries_def)
haftmann@51599
   213
haftmann@51599
   214
lemma filter_Bag [code]:
haftmann@51599
   215
  "Multiset.filter P (Bag xs) = Bag (DAList.filter (P \<circ> fst) xs)"
haftmann@51599
   216
by (rule multiset_eqI) (simp add: count_of_filter DAList.filter.rep_eq)
haftmann@51599
   217
haftmann@51599
   218
lemma mset_less_eq_Bag [code]:
haftmann@51599
   219
  "Bag xs \<le> A \<longleftrightarrow> (\<forall>(x, n) \<in> set (DAList.impl_of xs). count_of (DAList.impl_of xs) x \<le> count A x)"
haftmann@51599
   220
    (is "?lhs \<longleftrightarrow> ?rhs")
haftmann@51599
   221
proof
haftmann@51599
   222
  assume ?lhs then show ?rhs
haftmann@51599
   223
    by (auto simp add: mset_le_def)
haftmann@51599
   224
next
haftmann@51599
   225
  assume ?rhs
haftmann@51599
   226
  show ?lhs
haftmann@51599
   227
  proof (rule mset_less_eqI)
haftmann@51599
   228
    fix x
haftmann@51599
   229
    from `?rhs` have "count_of (DAList.impl_of xs) x \<le> count A x"
haftmann@51599
   230
      by (cases "x \<in> fst ` set (DAList.impl_of xs)") (auto simp add: count_of_empty)
haftmann@51599
   231
    then show "count (Bag xs) x \<le> count A x"
haftmann@51599
   232
      by (simp add: mset_le_def)
haftmann@51599
   233
  qed
haftmann@51599
   234
qed
haftmann@51599
   235
haftmann@51600
   236
declare multiset_inter_def [code]
haftmann@51623
   237
declare sup_multiset_def [code]
haftmann@51623
   238
declare multiset_of.simps [code]
haftmann@51600
   239
haftmann@51600
   240
instantiation multiset :: (exhaustive) exhaustive
haftmann@51599
   241
begin
haftmann@51599
   242
haftmann@51600
   243
definition exhaustive_multiset :: "('a multiset \<Rightarrow> (bool \<times> term list) option) \<Rightarrow> natural \<Rightarrow> (bool * term list) option"
haftmann@51600
   244
where
haftmann@51600
   245
  "exhaustive_multiset f i = Quickcheck_Exhaustive.exhaustive (\<lambda>xs. f (Bag xs)) i"
haftmann@51599
   246
haftmann@51599
   247
instance ..
haftmann@51599
   248
haftmann@51599
   249
end
haftmann@51599
   250
haftmann@51599
   251
end
haftmann@51599
   252