src/HOL/Library/DAList_Multiset.thy
author wenzelm
Mon Dec 28 01:28:28 2015 +0100 (2015-12-28)
changeset 61945 1135b8de26c3
parent 61585 a9599d3d7610
child 63040 eb4ddd18d635
permissions -rw-r--r--
more symbols;
haftmann@51599
     1
(*  Title:      HOL/Library/DAList_Multiset.thy
haftmann@51599
     2
    Author:     Lukas Bulwahn, TU Muenchen
haftmann@51599
     3
*)
haftmann@51599
     4
wenzelm@58881
     5
section \<open>Multisets partially implemented by association lists\<close>
haftmann@51599
     6
haftmann@51599
     7
theory DAList_Multiset
haftmann@51599
     8
imports Multiset DAList
haftmann@51599
     9
begin
haftmann@51599
    10
wenzelm@58806
    11
text \<open>Delete prexisting code equations\<close>
haftmann@51600
    12
wenzelm@58806
    13
lemma [code, code del]: "{#} = {#}" ..
haftmann@51623
    14
wenzelm@58806
    15
lemma [code, code del]: "single = single" ..
haftmann@51623
    16
wenzelm@58806
    17
lemma [code, code del]: "plus = (plus :: 'a multiset \<Rightarrow> _)" ..
haftmann@51600
    18
wenzelm@58806
    19
lemma [code, code del]: "minus = (minus :: 'a multiset \<Rightarrow> _)" ..
haftmann@51600
    20
Mathias@60397
    21
lemma [code, code del]: "inf_subset_mset = (inf_subset_mset :: 'a multiset \<Rightarrow> _)" ..
haftmann@51600
    22
Mathias@60397
    23
lemma [code, code del]: "sup_subset_mset = (sup_subset_mset :: 'a multiset \<Rightarrow> _)" ..
haftmann@51623
    24
wenzelm@58806
    25
lemma [code, code del]: "image_mset = image_mset" ..
haftmann@51600
    26
nipkow@59998
    27
lemma [code, code del]: "filter_mset = filter_mset" ..
haftmann@51600
    28
wenzelm@58806
    29
lemma [code, code del]: "count = count" ..
haftmann@51600
    30
nipkow@59949
    31
lemma [code, code del]: "size = (size :: _ multiset \<Rightarrow> nat)" ..
haftmann@51600
    32
wenzelm@58806
    33
lemma [code, code del]: "msetsum = msetsum" ..
haftmann@51600
    34
wenzelm@58806
    35
lemma [code, code del]: "msetprod = msetprod" ..
haftmann@51600
    36
nipkow@60495
    37
lemma [code, code del]: "set_mset = set_mset" ..
haftmann@51600
    38
wenzelm@58806
    39
lemma [code, code del]: "sorted_list_of_multiset = sorted_list_of_multiset" ..
haftmann@51600
    40
Mathias@60397
    41
lemma [code, code del]: "subset_mset = subset_mset" ..
nipkow@55808
    42
Mathias@60397
    43
lemma [code, code del]: "subseteq_mset = subseteq_mset" ..
nipkow@55808
    44
wenzelm@58806
    45
lemma [code, code del]: "equal_multiset_inst.equal_multiset = equal_multiset_inst.equal_multiset" ..
nipkow@55808
    46
haftmann@51600
    47
wenzelm@58806
    48
text \<open>Raw operations on lists\<close>
haftmann@51599
    49
wenzelm@58806
    50
definition join_raw ::
wenzelm@58806
    51
    "('key \<Rightarrow> 'val \<times> 'val \<Rightarrow> 'val) \<Rightarrow>
wenzelm@58806
    52
      ('key \<times> 'val) list \<Rightarrow> ('key \<times> 'val) list \<Rightarrow> ('key \<times> 'val) list"
wenzelm@58806
    53
  where "join_raw f xs ys = foldr (\<lambda>(k, v). map_default k v (\<lambda>v'. f k (v', v))) ys xs"
haftmann@51599
    54
wenzelm@58806
    55
lemma join_raw_Nil [simp]: "join_raw f xs [] = xs"
wenzelm@58806
    56
  by (simp add: join_raw_def)
haftmann@51599
    57
haftmann@51599
    58
lemma join_raw_Cons [simp]:
wenzelm@58806
    59
  "join_raw f xs ((k, v) # ys) = map_default k v (\<lambda>v'. f k (v', v)) (join_raw f xs ys)"
wenzelm@58806
    60
  by (simp add: join_raw_def)
haftmann@51599
    61
haftmann@51599
    62
lemma map_of_join_raw:
haftmann@51599
    63
  assumes "distinct (map fst ys)"
wenzelm@58806
    64
  shows "map_of (join_raw f xs ys) x =
wenzelm@58806
    65
    (case map_of xs x of
wenzelm@58806
    66
      None \<Rightarrow> map_of ys x
wenzelm@58806
    67
    | Some v \<Rightarrow> (case map_of ys x of None \<Rightarrow> Some v | Some v' \<Rightarrow> Some (f x (v, v'))))"
wenzelm@58806
    68
  using assms
wenzelm@58806
    69
  apply (induct ys)
wenzelm@58806
    70
  apply (auto simp add: map_of_map_default split: option.split)
wenzelm@58806
    71
  apply (metis map_of_eq_None_iff option.simps(2) weak_map_of_SomeI)
wenzelm@58806
    72
  apply (metis Some_eq_map_of_iff map_of_eq_None_iff option.simps(2))
wenzelm@58806
    73
  done
haftmann@51599
    74
haftmann@51599
    75
lemma distinct_join_raw:
haftmann@51599
    76
  assumes "distinct (map fst xs)"
haftmann@51599
    77
  shows "distinct (map fst (join_raw f xs ys))"
wenzelm@58806
    78
  using assms
haftmann@51599
    79
proof (induct ys)
wenzelm@58806
    80
  case Nil
wenzelm@58806
    81
  then show ?case by simp
wenzelm@58806
    82
next
haftmann@51599
    83
  case (Cons y ys)
wenzelm@58806
    84
  then show ?case by (cases y) (simp add: distinct_map_default)
wenzelm@58806
    85
qed
haftmann@51599
    86
wenzelm@58806
    87
definition "subtract_entries_raw xs ys = foldr (\<lambda>(k, v). AList.map_entry k (\<lambda>v'. v' - v)) ys xs"
haftmann@51599
    88
haftmann@51599
    89
lemma map_of_subtract_entries_raw:
haftmann@51599
    90
  assumes "distinct (map fst ys)"
wenzelm@58806
    91
  shows "map_of (subtract_entries_raw xs ys) x =
wenzelm@58806
    92
    (case map_of xs x of
wenzelm@58806
    93
      None \<Rightarrow> None
wenzelm@58806
    94
    | Some v \<Rightarrow> (case map_of ys x of None \<Rightarrow> Some v | Some v' \<Rightarrow> Some (v - v')))"
wenzelm@58806
    95
  using assms
wenzelm@58806
    96
  unfolding subtract_entries_raw_def
wenzelm@58806
    97
  apply (induct ys)
wenzelm@58806
    98
  apply auto
wenzelm@58806
    99
  apply (simp split: option.split)
wenzelm@58806
   100
  apply (simp add: map_of_map_entry)
wenzelm@58806
   101
  apply (auto split: option.split)
wenzelm@58806
   102
  apply (metis map_of_eq_None_iff option.simps(3) option.simps(4))
wenzelm@58806
   103
  apply (metis map_of_eq_None_iff option.simps(4) option.simps(5))
wenzelm@58806
   104
  done
haftmann@51599
   105
haftmann@51599
   106
lemma distinct_subtract_entries_raw:
haftmann@51599
   107
  assumes "distinct (map fst xs)"
haftmann@51599
   108
  shows "distinct (map fst (subtract_entries_raw xs ys))"
wenzelm@58806
   109
  using assms
wenzelm@58806
   110
  unfolding subtract_entries_raw_def
wenzelm@58806
   111
  by (induct ys) (auto simp add: distinct_map_entry)
haftmann@51599
   112
haftmann@51599
   113
wenzelm@58806
   114
text \<open>Operations on alists with distinct keys\<close>
haftmann@51599
   115
wenzelm@58806
   116
lift_definition join :: "('a \<Rightarrow> 'b \<times> 'b \<Rightarrow> 'b) \<Rightarrow> ('a, 'b) alist \<Rightarrow> ('a, 'b) alist \<Rightarrow> ('a, 'b) alist"
wenzelm@58806
   117
  is join_raw
wenzelm@58806
   118
  by (simp add: distinct_join_raw)
haftmann@51599
   119
haftmann@51599
   120
lift_definition subtract_entries :: "('a, ('b :: minus)) alist \<Rightarrow> ('a, 'b) alist \<Rightarrow> ('a, 'b) alist"
wenzelm@58806
   121
  is subtract_entries_raw
wenzelm@58806
   122
  by (simp add: distinct_subtract_entries_raw)
haftmann@51599
   123
haftmann@51599
   124
wenzelm@58806
   125
text \<open>Implementing multisets by means of association lists\<close>
haftmann@51599
   126
wenzelm@58806
   127
definition count_of :: "('a \<times> nat) list \<Rightarrow> 'a \<Rightarrow> nat"
wenzelm@58806
   128
  where "count_of xs x = (case map_of xs x of None \<Rightarrow> 0 | Some n \<Rightarrow> n)"
wenzelm@58806
   129
wenzelm@58806
   130
lemma count_of_multiset: "count_of xs \<in> multiset"
haftmann@51599
   131
proof -
wenzelm@58806
   132
  let ?A = "{x::'a. 0 < (case map_of xs x of None \<Rightarrow> 0::nat | Some n \<Rightarrow> n)}"
haftmann@51599
   133
  have "?A \<subseteq> dom (map_of xs)"
haftmann@51599
   134
  proof
haftmann@51599
   135
    fix x
haftmann@51599
   136
    assume "x \<in> ?A"
wenzelm@58806
   137
    then have "0 < (case map_of xs x of None \<Rightarrow> 0::nat | Some n \<Rightarrow> n)"
wenzelm@58806
   138
      by simp
wenzelm@58806
   139
    then have "map_of xs x \<noteq> None"
wenzelm@58806
   140
      by (cases "map_of xs x") auto
wenzelm@58806
   141
    then show "x \<in> dom (map_of xs)"
wenzelm@58806
   142
      by auto
haftmann@51599
   143
  qed
haftmann@51599
   144
  with finite_dom_map_of [of xs] have "finite ?A"
haftmann@51599
   145
    by (auto intro: finite_subset)
haftmann@51599
   146
  then show ?thesis
haftmann@51599
   147
    by (simp add: count_of_def fun_eq_iff multiset_def)
haftmann@51599
   148
qed
haftmann@51599
   149
haftmann@51599
   150
lemma count_simps [simp]:
haftmann@51599
   151
  "count_of [] = (\<lambda>_. 0)"
haftmann@51599
   152
  "count_of ((x, n) # xs) = (\<lambda>y. if x = y then n else count_of xs y)"
haftmann@51599
   153
  by (simp_all add: count_of_def fun_eq_iff)
haftmann@51599
   154
wenzelm@58806
   155
lemma count_of_empty: "x \<notin> fst ` set xs \<Longrightarrow> count_of xs x = 0"
haftmann@51599
   156
  by (induct xs) (simp_all add: count_of_def)
haftmann@51599
   157
wenzelm@58806
   158
lemma count_of_filter: "count_of (List.filter (P \<circ> fst) xs) x = (if P x then count_of xs x else 0)"
haftmann@51599
   159
  by (induct xs) auto
haftmann@51599
   160
haftmann@51599
   161
lemma count_of_map_default [simp]:
wenzelm@58806
   162
  "count_of (map_default x b (\<lambda>x. x + b) xs) y =
wenzelm@58806
   163
    (if x = y then count_of xs x + b else count_of xs y)"
wenzelm@58806
   164
  unfolding count_of_def by (simp add: map_of_map_default split: option.split)
haftmann@51599
   165
haftmann@51599
   166
lemma count_of_join_raw:
wenzelm@58806
   167
  "distinct (map fst ys) \<Longrightarrow>
wenzelm@58806
   168
    count_of xs x + count_of ys x = count_of (join_raw (\<lambda>x (x, y). x + y) xs ys) x"
wenzelm@58806
   169
  unfolding count_of_def by (simp add: map_of_join_raw split: option.split)
haftmann@51599
   170
haftmann@51599
   171
lemma count_of_subtract_entries_raw:
wenzelm@58806
   172
  "distinct (map fst ys) \<Longrightarrow>
wenzelm@58806
   173
    count_of xs x - count_of ys x = count_of (subtract_entries_raw xs ys) x"
wenzelm@58806
   174
  unfolding count_of_def by (simp add: map_of_subtract_entries_raw split: option.split)
haftmann@51599
   175
haftmann@51599
   176
wenzelm@58806
   177
text \<open>Code equations for multiset operations\<close>
haftmann@51599
   178
wenzelm@58806
   179
definition Bag :: "('a, nat) alist \<Rightarrow> 'a multiset"
wenzelm@58806
   180
  where "Bag xs = Abs_multiset (count_of (DAList.impl_of xs))"
haftmann@51599
   181
haftmann@51599
   182
code_datatype Bag
haftmann@51599
   183
wenzelm@58806
   184
lemma count_Bag [simp, code]: "count (Bag xs) = count_of (DAList.impl_of xs)"
wenzelm@58806
   185
  by (simp add: Bag_def count_of_multiset)
haftmann@51599
   186
wenzelm@58806
   187
lemma Mempty_Bag [code]: "{#} = Bag (DAList.empty)"
haftmann@51599
   188
  by (simp add: multiset_eq_iff alist.Alist_inverse DAList.empty_def)
haftmann@51599
   189
wenzelm@58806
   190
lemma single_Bag [code]: "{#x#} = Bag (DAList.update x 1 DAList.empty)"
haftmann@51599
   191
  by (simp add: multiset_eq_iff alist.Alist_inverse update.rep_eq empty.rep_eq)
haftmann@51599
   192
wenzelm@58806
   193
lemma union_Bag [code]: "Bag xs + Bag ys = Bag (join (\<lambda>x (n1, n2). n1 + n2) xs ys)"
wenzelm@58806
   194
  by (rule multiset_eqI)
wenzelm@58806
   195
    (simp add: count_of_join_raw alist.Alist_inverse distinct_join_raw join_def)
haftmann@51599
   196
wenzelm@58806
   197
lemma minus_Bag [code]: "Bag xs - Bag ys = Bag (subtract_entries xs ys)"
wenzelm@58806
   198
  by (rule multiset_eqI)
wenzelm@58806
   199
    (simp add: count_of_subtract_entries_raw alist.Alist_inverse
wenzelm@58806
   200
      distinct_subtract_entries_raw subtract_entries_def)
haftmann@51599
   201
nipkow@59998
   202
lemma filter_Bag [code]: "filter_mset P (Bag xs) = Bag (DAList.filter (P \<circ> fst) xs)"
wenzelm@58806
   203
  by (rule multiset_eqI) (simp add: count_of_filter DAList.filter.rep_eq)
haftmann@51599
   204
nipkow@55808
   205
Mathias@60397
   206
lemma mset_eq [code]: "HOL.equal (m1::'a::equal multiset) m2 \<longleftrightarrow> m1 \<le># m2 \<and> m2 \<le># m1"
Mathias@60397
   207
  by (metis equal_multiset_def subset_mset.eq_iff)
nipkow@55808
   208
wenzelm@61585
   209
text \<open>By default the code for \<open><\<close> is @{prop"xs < ys \<longleftrightarrow> xs \<le> ys \<and> \<not> xs = ys"}.
wenzelm@61585
   210
With equality implemented by \<open>\<le>\<close>, this leads to three calls of  \<open>\<le>\<close>.
wenzelm@58806
   211
Here is a more efficient version:\<close>
Mathias@60397
   212
lemma mset_less[code]: "xs <# (ys :: 'a multiset) \<longleftrightarrow> xs \<le># ys \<and> \<not> ys \<le># xs"
Mathias@60397
   213
  by (rule subset_mset.less_le_not_le)
nipkow@55887
   214
nipkow@55887
   215
lemma mset_less_eq_Bag0:
Mathias@60397
   216
  "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
   217
    (is "?lhs \<longleftrightarrow> ?rhs")
haftmann@51599
   218
proof
wenzelm@58806
   219
  assume ?lhs
Mathias@60397
   220
  then show ?rhs by (auto simp add: subseteq_mset_def)
haftmann@51599
   221
next
haftmann@51599
   222
  assume ?rhs
haftmann@51599
   223
  show ?lhs
haftmann@51599
   224
  proof (rule mset_less_eqI)
haftmann@51599
   225
    fix x
wenzelm@58806
   226
    from \<open>?rhs\<close> have "count_of (DAList.impl_of xs) x \<le> count A x"
haftmann@51599
   227
      by (cases "x \<in> fst ` set (DAList.impl_of xs)") (auto simp add: count_of_empty)
Mathias@60397
   228
    then show "count (Bag xs) x \<le> count A x" by (simp add: subset_mset_def)
haftmann@51599
   229
  qed
haftmann@51599
   230
qed
haftmann@51599
   231
nipkow@55887
   232
lemma mset_less_eq_Bag [code]:
Mathias@60397
   233
  "Bag xs \<le># (A :: 'a multiset) \<longleftrightarrow> (\<forall>(x, n) \<in> set (DAList.impl_of xs). n \<le> count A x)"
nipkow@55887
   234
proof -
nipkow@55887
   235
  {
nipkow@55887
   236
    fix x n
nipkow@55887
   237
    assume "(x,n) \<in> set (DAList.impl_of xs)"
wenzelm@58806
   238
    then have "count_of (DAList.impl_of xs) x = n"
wenzelm@58806
   239
    proof transfer
wenzelm@58806
   240
      fix x n
wenzelm@58806
   241
      fix xs :: "('a \<times> nat) list"
nipkow@55887
   242
      show "(distinct \<circ> map fst) xs \<Longrightarrow> (x, n) \<in> set xs \<Longrightarrow> count_of xs x = n"
wenzelm@58806
   243
      proof (induct xs)
wenzelm@58806
   244
        case Nil
wenzelm@58806
   245
        then show ?case by simp
wenzelm@58806
   246
      next
wenzelm@58806
   247
        case (Cons ym ys)
nipkow@55887
   248
        obtain y m where ym: "ym = (y,m)" by force
nipkow@55887
   249
        note Cons = Cons[unfolded ym]
nipkow@55887
   250
        show ?case
nipkow@55887
   251
        proof (cases "x = y")
nipkow@55887
   252
          case False
wenzelm@58806
   253
          with Cons show ?thesis
wenzelm@58806
   254
            unfolding ym by auto
nipkow@55887
   255
        next
nipkow@55887
   256
          case True
nipkow@55887
   257
          with Cons(2-3) have "m = n" by force
wenzelm@58806
   258
          with True show ?thesis
wenzelm@58806
   259
            unfolding ym by auto
nipkow@55887
   260
        qed
wenzelm@58806
   261
      qed
nipkow@55887
   262
    qed
nipkow@55887
   263
  }
wenzelm@58806
   264
  then show ?thesis
wenzelm@58806
   265
    unfolding mset_less_eq_Bag0 by auto
nipkow@55887
   266
qed
nipkow@55887
   267
haftmann@51600
   268
declare multiset_inter_def [code]
Mathias@60397
   269
declare sup_subset_mset_def [code]
nipkow@60515
   270
declare mset.simps [code]
haftmann@51600
   271
nipkow@55887
   272
wenzelm@58806
   273
fun fold_impl :: "('a \<Rightarrow> nat \<Rightarrow> 'b \<Rightarrow> 'b) \<Rightarrow> 'b \<Rightarrow> ('a \<times> nat) list \<Rightarrow> 'b"
wenzelm@58806
   274
where
nipkow@55887
   275
  "fold_impl fn e ((a,n) # ms) = (fold_impl fn ((fn a n) e) ms)"
nipkow@55887
   276
| "fold_impl fn e [] = e"
nipkow@55887
   277
wenzelm@61115
   278
context
wenzelm@61115
   279
begin
wenzelm@61115
   280
wenzelm@61115
   281
qualified definition fold :: "('a \<Rightarrow> nat \<Rightarrow> 'b \<Rightarrow> 'b) \<Rightarrow> 'b \<Rightarrow> ('a, nat) alist \<Rightarrow> 'b"
wenzelm@58806
   282
  where "fold f e al = fold_impl f e (DAList.impl_of al)"
nipkow@55887
   283
wenzelm@61115
   284
end
nipkow@55887
   285
nipkow@55887
   286
context comp_fun_commute
nipkow@55887
   287
begin
nipkow@55887
   288
wenzelm@58806
   289
lemma DAList_Multiset_fold:
wenzelm@58806
   290
  assumes fn: "\<And>a n x. fn a n x = (f a ^^ n) x"
nipkow@59998
   291
  shows "fold_mset f e (Bag al) = DAList_Multiset.fold fn e al"
wenzelm@58806
   292
  unfolding DAList_Multiset.fold_def
nipkow@55887
   293
proof (induct al)
nipkow@55887
   294
  fix ys
wenzelm@58806
   295
  let ?inv = "{xs :: ('a \<times> nat) list. (distinct \<circ> map fst) xs}"
nipkow@55887
   296
  note cs[simp del] = count_simps
wenzelm@58806
   297
  have count[simp]: "\<And>x. count (Abs_multiset (count_of x)) = count_of x"
nipkow@55887
   298
    by (rule Abs_multiset_inverse[OF count_of_multiset])
nipkow@55887
   299
  assume ys: "ys \<in> ?inv"
nipkow@59998
   300
  then show "fold_mset f e (Bag (Alist ys)) = fold_impl fn e (DAList.impl_of (Alist ys))"
nipkow@55887
   301
    unfolding Bag_def unfolding Alist_inverse[OF ys]
nipkow@55887
   302
  proof (induct ys arbitrary: e rule: list.induct)
nipkow@55887
   303
    case Nil
nipkow@55887
   304
    show ?case
nipkow@59998
   305
      by (rule trans[OF arg_cong[of _ "{#}" "fold_mset f e", OF multiset_eqI]])
nipkow@55887
   306
         (auto, simp add: cs)
nipkow@55887
   307
  next
nipkow@55887
   308
    case (Cons pair ys e)
wenzelm@58806
   309
    obtain a n where pair: "pair = (a,n)"
wenzelm@58806
   310
      by force
wenzelm@58806
   311
    from fn[of a n] have [simp]: "fn a n = (f a ^^ n)"
wenzelm@58806
   312
      by auto
wenzelm@58806
   313
    have inv: "ys \<in> ?inv"
wenzelm@58806
   314
      using Cons(2) by auto
nipkow@55887
   315
    note IH = Cons(1)[OF inv]
nipkow@55887
   316
    def Ys \<equiv> "Abs_multiset (count_of ys)"
nipkow@55887
   317
    have id: "Abs_multiset (count_of ((a, n) # ys)) = ((op + {# a #}) ^^ n) Ys"
nipkow@55887
   318
      unfolding Ys_def
nipkow@55887
   319
    proof (rule multiset_eqI, unfold count)
wenzelm@58806
   320
      fix c
wenzelm@58806
   321
      show "count_of ((a, n) # ys) c =
wenzelm@58806
   322
        count ((op + {#a#} ^^ n) (Abs_multiset (count_of ys))) c" (is "?l = ?r")
nipkow@55887
   323
      proof (cases "c = a")
wenzelm@58806
   324
        case False
wenzelm@58806
   325
        then show ?thesis
wenzelm@58806
   326
          unfolding cs by (induct n) auto
nipkow@55887
   327
      next
nipkow@55887
   328
        case True
wenzelm@58806
   329
        then have "?l = n" by (simp add: cs)
nipkow@55887
   330
        also have "n = ?r" unfolding True
nipkow@55887
   331
        proof (induct n)
nipkow@55887
   332
          case 0
nipkow@55887
   333
          from Cons(2)[unfolded pair] have "a \<notin> fst ` set ys" by auto
wenzelm@58806
   334
          then show ?case by (induct ys) (simp, auto simp: cs)
wenzelm@58806
   335
        next
wenzelm@58806
   336
          case Suc
wenzelm@58806
   337
          then show ?case by simp
wenzelm@58806
   338
        qed
nipkow@55887
   339
        finally show ?thesis .
nipkow@55887
   340
      qed
nipkow@55887
   341
    qed
wenzelm@58806
   342
    show ?case
wenzelm@58806
   343
      unfolding pair
wenzelm@58806
   344
      apply (simp add: IH[symmetric])
wenzelm@58806
   345
      unfolding id Ys_def[symmetric]
wenzelm@58806
   346
      apply (induct n)
wenzelm@58806
   347
      apply (auto simp: fold_mset_fun_left_comm[symmetric])
wenzelm@58806
   348
      done
nipkow@55887
   349
  qed
nipkow@55887
   350
qed
nipkow@55887
   351
wenzelm@58806
   352
end
nipkow@55887
   353
wenzelm@61115
   354
context
wenzelm@61115
   355
begin
wenzelm@61115
   356
wenzelm@61115
   357
private lift_definition single_alist_entry :: "'a \<Rightarrow> 'b \<Rightarrow> ('a, 'b) alist" is "\<lambda>a b. [(a, b)]"
wenzelm@58806
   358
  by auto
nipkow@55887
   359
wenzelm@58806
   360
lemma image_mset_Bag [code]:
nipkow@55887
   361
  "image_mset f (Bag ms) =
wenzelm@58806
   362
    DAList_Multiset.fold (\<lambda>a n m. Bag (single_alist_entry (f a) n) + m) {#} ms"
wenzelm@58806
   363
  unfolding image_mset_def
nipkow@55887
   364
proof (rule comp_fun_commute.DAList_Multiset_fold, unfold_locales, (auto simp: ac_simps)[1])
nipkow@55887
   365
  fix a n m
nipkow@55887
   366
  show "Bag (single_alist_entry (f a) n) + m = ((op + \<circ> single \<circ> f) a ^^ n) m" (is "?l = ?r")
nipkow@55887
   367
  proof (rule multiset_eqI)
nipkow@55887
   368
    fix x
nipkow@55887
   369
    have "count ?r x = (if x = f a then n + count m x else count m x)"
wenzelm@58806
   370
      by (induct n) auto
wenzelm@58806
   371
    also have "\<dots> = count ?l x"
wenzelm@58806
   372
      by (simp add: single_alist_entry.rep_eq)
nipkow@55887
   373
    finally show "count ?l x = count ?r x" ..
nipkow@55887
   374
  qed
nipkow@55887
   375
qed
nipkow@55887
   376
wenzelm@61115
   377
end
nipkow@55887
   378
wenzelm@58806
   379
(* we cannot use (\<lambda>a n. op + (a * n)) for folding, since * is not defined
nipkow@55887
   380
   in comm_monoid_add *)
wenzelm@58806
   381
lemma msetsum_Bag[code]: "msetsum (Bag ms) = DAList_Multiset.fold (\<lambda>a n. ((op + a) ^^ n)) 0 ms"
wenzelm@58806
   382
  unfolding msetsum.eq_fold
wenzelm@58806
   383
  apply (rule comp_fun_commute.DAList_Multiset_fold)
wenzelm@58806
   384
  apply unfold_locales
wenzelm@58806
   385
  apply (auto simp: ac_simps)
wenzelm@58806
   386
  done
nipkow@55887
   387
wenzelm@58806
   388
(* we cannot use (\<lambda>a n. op * (a ^ n)) for folding, since ^ is not defined
nipkow@55887
   389
   in comm_monoid_mult *)
wenzelm@58806
   390
lemma msetprod_Bag[code]: "msetprod (Bag ms) = DAList_Multiset.fold (\<lambda>a n. ((op * a) ^^ n)) 1 ms"
wenzelm@58806
   391
  unfolding msetprod.eq_fold
wenzelm@58806
   392
  apply (rule comp_fun_commute.DAList_Multiset_fold)
wenzelm@58806
   393
  apply unfold_locales
wenzelm@58806
   394
  apply (auto simp: ac_simps)
wenzelm@58806
   395
  done
nipkow@55887
   396
nipkow@59998
   397
lemma size_fold: "size A = fold_mset (\<lambda>_. Suc) 0 A" (is "_ = fold_mset ?f _ _")
nipkow@55887
   398
proof -
wenzelm@60679
   399
  interpret comp_fun_commute ?f by standard auto
nipkow@55887
   400
  show ?thesis by (induct A) auto
nipkow@55887
   401
qed
nipkow@55887
   402
nipkow@59949
   403
lemma size_Bag[code]: "size (Bag ms) = DAList_Multiset.fold (\<lambda>a n. op + n) 0 ms"
nipkow@59949
   404
  unfolding size_fold
nipkow@55887
   405
proof (rule comp_fun_commute.DAList_Multiset_fold, unfold_locales, simp)
nipkow@55887
   406
  fix a n x
wenzelm@58806
   407
  show "n + x = (Suc ^^ n) x"
wenzelm@58806
   408
    by (induct n) auto
nipkow@55887
   409
qed
nipkow@55887
   410
nipkow@55887
   411
nipkow@60495
   412
lemma set_mset_fold: "set_mset A = fold_mset insert {} A" (is "_ = fold_mset ?f _ _")
nipkow@55887
   413
proof -
wenzelm@60679
   414
  interpret comp_fun_commute ?f by standard auto
wenzelm@58806
   415
  show ?thesis by (induct A) auto
nipkow@55887
   416
qed
nipkow@55887
   417
nipkow@60495
   418
lemma set_mset_Bag[code]:
nipkow@60495
   419
  "set_mset (Bag ms) = DAList_Multiset.fold (\<lambda>a n. (if n = 0 then (\<lambda>m. m) else insert a)) {} ms"
nipkow@60495
   420
  unfolding set_mset_fold
nipkow@55887
   421
proof (rule comp_fun_commute.DAList_Multiset_fold, unfold_locales, (auto simp: ac_simps)[1])
nipkow@55887
   422
  fix a n x
nipkow@55887
   423
  show "(if n = 0 then \<lambda>m. m else insert a) x = (insert a ^^ n) x" (is "?l n = ?r n")
nipkow@55887
   424
  proof (cases n)
wenzelm@58806
   425
    case 0
wenzelm@58806
   426
    then show ?thesis by simp
wenzelm@58806
   427
  next
nipkow@55887
   428
    case (Suc m)
wenzelm@58806
   429
    then have "?l n = insert a x" by simp
nipkow@55887
   430
    moreover have "?r n = insert a x" unfolding Suc by (induct m) auto
nipkow@55887
   431
    ultimately show ?thesis by auto
wenzelm@58806
   432
  qed
nipkow@55887
   433
qed
nipkow@55887
   434
nipkow@55887
   435
haftmann@51600
   436
instantiation multiset :: (exhaustive) exhaustive
haftmann@51599
   437
begin
haftmann@51599
   438
wenzelm@58806
   439
definition exhaustive_multiset ::
wenzelm@58806
   440
  "('a multiset \<Rightarrow> (bool \<times> term list) option) \<Rightarrow> natural \<Rightarrow> (bool \<times> term list) option"
wenzelm@58806
   441
  where "exhaustive_multiset f i = Quickcheck_Exhaustive.exhaustive (\<lambda>xs. f (Bag xs)) i"
haftmann@51599
   442
haftmann@51599
   443
instance ..
haftmann@51599
   444
haftmann@51599
   445
end
haftmann@51599
   446
haftmann@51599
   447
end
haftmann@51599
   448