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