src/HOL/Library/DAList_Multiset.thy
author haftmann
Fri, 27 Jun 2025 08:09:26 +0200
changeset 82775 61c39a9e5415
parent 82774 2865a6618cba
permissions -rw-r--r--
typo
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
51599
1559e9266280 optionalized very specific code setup for multisets
haftmann
parents:
diff changeset
     1
(*  Title:      HOL/Library/DAList_Multiset.thy
1559e9266280 optionalized very specific code setup for multisets
haftmann
parents:
diff changeset
     2
    Author:     Lukas Bulwahn, TU Muenchen
1559e9266280 optionalized very specific code setup for multisets
haftmann
parents:
diff changeset
     3
*)
1559e9266280 optionalized very specific code setup for multisets
haftmann
parents:
diff changeset
     4
58881
b9556a055632 modernized header;
wenzelm
parents: 58806
diff changeset
     5
section \<open>Multisets partially implemented by association lists\<close>
51599
1559e9266280 optionalized very specific code setup for multisets
haftmann
parents:
diff changeset
     6
1559e9266280 optionalized very specific code setup for multisets
haftmann
parents:
diff changeset
     7
theory DAList_Multiset
1559e9266280 optionalized very specific code setup for multisets
haftmann
parents:
diff changeset
     8
imports Multiset DAList
1559e9266280 optionalized very specific code setup for multisets
haftmann
parents:
diff changeset
     9
begin
1559e9266280 optionalized very specific code setup for multisets
haftmann
parents:
diff changeset
    10
58806
bb5ab5fce93a tuned proofs;
wenzelm
parents: 55887
diff changeset
    11
text \<open>Raw operations on lists\<close>
51599
1559e9266280 optionalized very specific code setup for multisets
haftmann
parents:
diff changeset
    12
58806
bb5ab5fce93a tuned proofs;
wenzelm
parents: 55887
diff changeset
    13
definition join_raw ::
bb5ab5fce93a tuned proofs;
wenzelm
parents: 55887
diff changeset
    14
    "('key \<Rightarrow> 'val \<times> 'val \<Rightarrow> 'val) \<Rightarrow>
bb5ab5fce93a tuned proofs;
wenzelm
parents: 55887
diff changeset
    15
      ('key \<times> 'val) list \<Rightarrow> ('key \<times> 'val) list \<Rightarrow> ('key \<times> 'val) list"
bb5ab5fce93a tuned proofs;
wenzelm
parents: 55887
diff changeset
    16
  where "join_raw f xs ys = foldr (\<lambda>(k, v). map_default k v (\<lambda>v'. f k (v', v))) ys xs"
51599
1559e9266280 optionalized very specific code setup for multisets
haftmann
parents:
diff changeset
    17
58806
bb5ab5fce93a tuned proofs;
wenzelm
parents: 55887
diff changeset
    18
lemma join_raw_Nil [simp]: "join_raw f xs [] = xs"
bb5ab5fce93a tuned proofs;
wenzelm
parents: 55887
diff changeset
    19
  by (simp add: join_raw_def)
51599
1559e9266280 optionalized very specific code setup for multisets
haftmann
parents:
diff changeset
    20
1559e9266280 optionalized very specific code setup for multisets
haftmann
parents:
diff changeset
    21
lemma join_raw_Cons [simp]:
58806
bb5ab5fce93a tuned proofs;
wenzelm
parents: 55887
diff changeset
    22
  "join_raw f xs ((k, v) # ys) = map_default k v (\<lambda>v'. f k (v', v)) (join_raw f xs ys)"
bb5ab5fce93a tuned proofs;
wenzelm
parents: 55887
diff changeset
    23
  by (simp add: join_raw_def)
51599
1559e9266280 optionalized very specific code setup for multisets
haftmann
parents:
diff changeset
    24
1559e9266280 optionalized very specific code setup for multisets
haftmann
parents:
diff changeset
    25
lemma map_of_join_raw:
1559e9266280 optionalized very specific code setup for multisets
haftmann
parents:
diff changeset
    26
  assumes "distinct (map fst ys)"
58806
bb5ab5fce93a tuned proofs;
wenzelm
parents: 55887
diff changeset
    27
  shows "map_of (join_raw f xs ys) x =
bb5ab5fce93a tuned proofs;
wenzelm
parents: 55887
diff changeset
    28
    (case map_of xs x of
bb5ab5fce93a tuned proofs;
wenzelm
parents: 55887
diff changeset
    29
      None \<Rightarrow> map_of ys x
bb5ab5fce93a tuned proofs;
wenzelm
parents: 55887
diff changeset
    30
    | Some v \<Rightarrow> (case map_of ys x of None \<Rightarrow> Some v | Some v' \<Rightarrow> Some (f x (v, v'))))"
bb5ab5fce93a tuned proofs;
wenzelm
parents: 55887
diff changeset
    31
  using assms
bb5ab5fce93a tuned proofs;
wenzelm
parents: 55887
diff changeset
    32
  apply (induct ys)
bb5ab5fce93a tuned proofs;
wenzelm
parents: 55887
diff changeset
    33
  apply (auto simp add: map_of_map_default split: option.split)
bb5ab5fce93a tuned proofs;
wenzelm
parents: 55887
diff changeset
    34
  apply (metis map_of_eq_None_iff option.simps(2) weak_map_of_SomeI)
bb5ab5fce93a tuned proofs;
wenzelm
parents: 55887
diff changeset
    35
  apply (metis Some_eq_map_of_iff map_of_eq_None_iff option.simps(2))
bb5ab5fce93a tuned proofs;
wenzelm
parents: 55887
diff changeset
    36
  done
51599
1559e9266280 optionalized very specific code setup for multisets
haftmann
parents:
diff changeset
    37
1559e9266280 optionalized very specific code setup for multisets
haftmann
parents:
diff changeset
    38
lemma distinct_join_raw:
1559e9266280 optionalized very specific code setup for multisets
haftmann
parents:
diff changeset
    39
  assumes "distinct (map fst xs)"
1559e9266280 optionalized very specific code setup for multisets
haftmann
parents:
diff changeset
    40
  shows "distinct (map fst (join_raw f xs ys))"
58806
bb5ab5fce93a tuned proofs;
wenzelm
parents: 55887
diff changeset
    41
  using assms
51599
1559e9266280 optionalized very specific code setup for multisets
haftmann
parents:
diff changeset
    42
proof (induct ys)
58806
bb5ab5fce93a tuned proofs;
wenzelm
parents: 55887
diff changeset
    43
  case Nil
bb5ab5fce93a tuned proofs;
wenzelm
parents: 55887
diff changeset
    44
  then show ?case by simp
bb5ab5fce93a tuned proofs;
wenzelm
parents: 55887
diff changeset
    45
next
51599
1559e9266280 optionalized very specific code setup for multisets
haftmann
parents:
diff changeset
    46
  case (Cons y ys)
58806
bb5ab5fce93a tuned proofs;
wenzelm
parents: 55887
diff changeset
    47
  then show ?case by (cases y) (simp add: distinct_map_default)
bb5ab5fce93a tuned proofs;
wenzelm
parents: 55887
diff changeset
    48
qed
51599
1559e9266280 optionalized very specific code setup for multisets
haftmann
parents:
diff changeset
    49
58806
bb5ab5fce93a tuned proofs;
wenzelm
parents: 55887
diff changeset
    50
definition "subtract_entries_raw xs ys = foldr (\<lambda>(k, v). AList.map_entry k (\<lambda>v'. v' - v)) ys xs"
51599
1559e9266280 optionalized very specific code setup for multisets
haftmann
parents:
diff changeset
    51
1559e9266280 optionalized very specific code setup for multisets
haftmann
parents:
diff changeset
    52
lemma map_of_subtract_entries_raw:
1559e9266280 optionalized very specific code setup for multisets
haftmann
parents:
diff changeset
    53
  assumes "distinct (map fst ys)"
58806
bb5ab5fce93a tuned proofs;
wenzelm
parents: 55887
diff changeset
    54
  shows "map_of (subtract_entries_raw xs ys) x =
bb5ab5fce93a tuned proofs;
wenzelm
parents: 55887
diff changeset
    55
    (case map_of xs x of
bb5ab5fce93a tuned proofs;
wenzelm
parents: 55887
diff changeset
    56
      None \<Rightarrow> None
bb5ab5fce93a tuned proofs;
wenzelm
parents: 55887
diff changeset
    57
    | Some v \<Rightarrow> (case map_of ys x of None \<Rightarrow> Some v | Some v' \<Rightarrow> Some (v - v')))"
bb5ab5fce93a tuned proofs;
wenzelm
parents: 55887
diff changeset
    58
  using assms
bb5ab5fce93a tuned proofs;
wenzelm
parents: 55887
diff changeset
    59
  unfolding subtract_entries_raw_def
bb5ab5fce93a tuned proofs;
wenzelm
parents: 55887
diff changeset
    60
  apply (induct ys)
bb5ab5fce93a tuned proofs;
wenzelm
parents: 55887
diff changeset
    61
  apply auto
bb5ab5fce93a tuned proofs;
wenzelm
parents: 55887
diff changeset
    62
  apply (simp split: option.split)
bb5ab5fce93a tuned proofs;
wenzelm
parents: 55887
diff changeset
    63
  apply (simp add: map_of_map_entry)
bb5ab5fce93a tuned proofs;
wenzelm
parents: 55887
diff changeset
    64
  apply (auto split: option.split)
bb5ab5fce93a tuned proofs;
wenzelm
parents: 55887
diff changeset
    65
  apply (metis map_of_eq_None_iff option.simps(3) option.simps(4))
bb5ab5fce93a tuned proofs;
wenzelm
parents: 55887
diff changeset
    66
  apply (metis map_of_eq_None_iff option.simps(4) option.simps(5))
bb5ab5fce93a tuned proofs;
wenzelm
parents: 55887
diff changeset
    67
  done
51599
1559e9266280 optionalized very specific code setup for multisets
haftmann
parents:
diff changeset
    68
1559e9266280 optionalized very specific code setup for multisets
haftmann
parents:
diff changeset
    69
lemma distinct_subtract_entries_raw:
1559e9266280 optionalized very specific code setup for multisets
haftmann
parents:
diff changeset
    70
  assumes "distinct (map fst xs)"
1559e9266280 optionalized very specific code setup for multisets
haftmann
parents:
diff changeset
    71
  shows "distinct (map fst (subtract_entries_raw xs ys))"
58806
bb5ab5fce93a tuned proofs;
wenzelm
parents: 55887
diff changeset
    72
  using assms
bb5ab5fce93a tuned proofs;
wenzelm
parents: 55887
diff changeset
    73
  unfolding subtract_entries_raw_def
bb5ab5fce93a tuned proofs;
wenzelm
parents: 55887
diff changeset
    74
  by (induct ys) (auto simp add: distinct_map_entry)
51599
1559e9266280 optionalized very specific code setup for multisets
haftmann
parents:
diff changeset
    75
1559e9266280 optionalized very specific code setup for multisets
haftmann
parents:
diff changeset
    76
58806
bb5ab5fce93a tuned proofs;
wenzelm
parents: 55887
diff changeset
    77
text \<open>Operations on alists with distinct keys\<close>
51599
1559e9266280 optionalized very specific code setup for multisets
haftmann
parents:
diff changeset
    78
58806
bb5ab5fce93a tuned proofs;
wenzelm
parents: 55887
diff changeset
    79
lift_definition join :: "('a \<Rightarrow> 'b \<times> 'b \<Rightarrow> 'b) \<Rightarrow> ('a, 'b) alist \<Rightarrow> ('a, 'b) alist \<Rightarrow> ('a, 'b) alist"
bb5ab5fce93a tuned proofs;
wenzelm
parents: 55887
diff changeset
    80
  is join_raw
bb5ab5fce93a tuned proofs;
wenzelm
parents: 55887
diff changeset
    81
  by (simp add: distinct_join_raw)
51599
1559e9266280 optionalized very specific code setup for multisets
haftmann
parents:
diff changeset
    82
1559e9266280 optionalized very specific code setup for multisets
haftmann
parents:
diff changeset
    83
lift_definition subtract_entries :: "('a, ('b :: minus)) alist \<Rightarrow> ('a, 'b) alist \<Rightarrow> ('a, 'b) alist"
58806
bb5ab5fce93a tuned proofs;
wenzelm
parents: 55887
diff changeset
    84
  is subtract_entries_raw
bb5ab5fce93a tuned proofs;
wenzelm
parents: 55887
diff changeset
    85
  by (simp add: distinct_subtract_entries_raw)
51599
1559e9266280 optionalized very specific code setup for multisets
haftmann
parents:
diff changeset
    86
1559e9266280 optionalized very specific code setup for multisets
haftmann
parents:
diff changeset
    87
58806
bb5ab5fce93a tuned proofs;
wenzelm
parents: 55887
diff changeset
    88
text \<open>Implementing multisets by means of association lists\<close>
51599
1559e9266280 optionalized very specific code setup for multisets
haftmann
parents:
diff changeset
    89
58806
bb5ab5fce93a tuned proofs;
wenzelm
parents: 55887
diff changeset
    90
definition count_of :: "('a \<times> nat) list \<Rightarrow> 'a \<Rightarrow> nat"
bb5ab5fce93a tuned proofs;
wenzelm
parents: 55887
diff changeset
    91
  where "count_of xs x = (case map_of xs x of None \<Rightarrow> 0 | Some n \<Rightarrow> n)"
bb5ab5fce93a tuned proofs;
wenzelm
parents: 55887
diff changeset
    92
73270
e2d03448d5b5 get rid of traditional predicate
haftmann
parents: 69593
diff changeset
    93
lemma count_of_multiset: "finite {x. 0 < count_of xs x}"
51599
1559e9266280 optionalized very specific code setup for multisets
haftmann
parents:
diff changeset
    94
proof -
58806
bb5ab5fce93a tuned proofs;
wenzelm
parents: 55887
diff changeset
    95
  let ?A = "{x::'a. 0 < (case map_of xs x of None \<Rightarrow> 0::nat | Some n \<Rightarrow> n)}"
51599
1559e9266280 optionalized very specific code setup for multisets
haftmann
parents:
diff changeset
    96
  have "?A \<subseteq> dom (map_of xs)"
1559e9266280 optionalized very specific code setup for multisets
haftmann
parents:
diff changeset
    97
  proof
1559e9266280 optionalized very specific code setup for multisets
haftmann
parents:
diff changeset
    98
    fix x
1559e9266280 optionalized very specific code setup for multisets
haftmann
parents:
diff changeset
    99
    assume "x \<in> ?A"
58806
bb5ab5fce93a tuned proofs;
wenzelm
parents: 55887
diff changeset
   100
    then have "0 < (case map_of xs x of None \<Rightarrow> 0::nat | Some n \<Rightarrow> n)"
bb5ab5fce93a tuned proofs;
wenzelm
parents: 55887
diff changeset
   101
      by simp
bb5ab5fce93a tuned proofs;
wenzelm
parents: 55887
diff changeset
   102
    then have "map_of xs x \<noteq> None"
bb5ab5fce93a tuned proofs;
wenzelm
parents: 55887
diff changeset
   103
      by (cases "map_of xs x") auto
bb5ab5fce93a tuned proofs;
wenzelm
parents: 55887
diff changeset
   104
    then show "x \<in> dom (map_of xs)"
bb5ab5fce93a tuned proofs;
wenzelm
parents: 55887
diff changeset
   105
      by auto
51599
1559e9266280 optionalized very specific code setup for multisets
haftmann
parents:
diff changeset
   106
  qed
1559e9266280 optionalized very specific code setup for multisets
haftmann
parents:
diff changeset
   107
  with finite_dom_map_of [of xs] have "finite ?A"
1559e9266280 optionalized very specific code setup for multisets
haftmann
parents:
diff changeset
   108
    by (auto intro: finite_subset)
1559e9266280 optionalized very specific code setup for multisets
haftmann
parents:
diff changeset
   109
  then show ?thesis
73270
e2d03448d5b5 get rid of traditional predicate
haftmann
parents: 69593
diff changeset
   110
    by (simp add: count_of_def fun_eq_iff)
51599
1559e9266280 optionalized very specific code setup for multisets
haftmann
parents:
diff changeset
   111
qed
1559e9266280 optionalized very specific code setup for multisets
haftmann
parents:
diff changeset
   112
1559e9266280 optionalized very specific code setup for multisets
haftmann
parents:
diff changeset
   113
lemma count_simps [simp]:
1559e9266280 optionalized very specific code setup for multisets
haftmann
parents:
diff changeset
   114
  "count_of [] = (\<lambda>_. 0)"
1559e9266280 optionalized very specific code setup for multisets
haftmann
parents:
diff changeset
   115
  "count_of ((x, n) # xs) = (\<lambda>y. if x = y then n else count_of xs y)"
1559e9266280 optionalized very specific code setup for multisets
haftmann
parents:
diff changeset
   116
  by (simp_all add: count_of_def fun_eq_iff)
1559e9266280 optionalized very specific code setup for multisets
haftmann
parents:
diff changeset
   117
58806
bb5ab5fce93a tuned proofs;
wenzelm
parents: 55887
diff changeset
   118
lemma count_of_empty: "x \<notin> fst ` set xs \<Longrightarrow> count_of xs x = 0"
51599
1559e9266280 optionalized very specific code setup for multisets
haftmann
parents:
diff changeset
   119
  by (induct xs) (simp_all add: count_of_def)
1559e9266280 optionalized very specific code setup for multisets
haftmann
parents:
diff changeset
   120
58806
bb5ab5fce93a tuned proofs;
wenzelm
parents: 55887
diff changeset
   121
lemma count_of_filter: "count_of (List.filter (P \<circ> fst) xs) x = (if P x then count_of xs x else 0)"
51599
1559e9266280 optionalized very specific code setup for multisets
haftmann
parents:
diff changeset
   122
  by (induct xs) auto
1559e9266280 optionalized very specific code setup for multisets
haftmann
parents:
diff changeset
   123
1559e9266280 optionalized very specific code setup for multisets
haftmann
parents:
diff changeset
   124
lemma count_of_map_default [simp]:
58806
bb5ab5fce93a tuned proofs;
wenzelm
parents: 55887
diff changeset
   125
  "count_of (map_default x b (\<lambda>x. x + b) xs) y =
bb5ab5fce93a tuned proofs;
wenzelm
parents: 55887
diff changeset
   126
    (if x = y then count_of xs x + b else count_of xs y)"
bb5ab5fce93a tuned proofs;
wenzelm
parents: 55887
diff changeset
   127
  unfolding count_of_def by (simp add: map_of_map_default split: option.split)
51599
1559e9266280 optionalized very specific code setup for multisets
haftmann
parents:
diff changeset
   128
1559e9266280 optionalized very specific code setup for multisets
haftmann
parents:
diff changeset
   129
lemma count_of_join_raw:
58806
bb5ab5fce93a tuned proofs;
wenzelm
parents: 55887
diff changeset
   130
  "distinct (map fst ys) \<Longrightarrow>
bb5ab5fce93a tuned proofs;
wenzelm
parents: 55887
diff changeset
   131
    count_of xs x + count_of ys x = count_of (join_raw (\<lambda>x (x, y). x + y) xs ys) x"
bb5ab5fce93a tuned proofs;
wenzelm
parents: 55887
diff changeset
   132
  unfolding count_of_def by (simp add: map_of_join_raw split: option.split)
51599
1559e9266280 optionalized very specific code setup for multisets
haftmann
parents:
diff changeset
   133
1559e9266280 optionalized very specific code setup for multisets
haftmann
parents:
diff changeset
   134
lemma count_of_subtract_entries_raw:
58806
bb5ab5fce93a tuned proofs;
wenzelm
parents: 55887
diff changeset
   135
  "distinct (map fst ys) \<Longrightarrow>
bb5ab5fce93a tuned proofs;
wenzelm
parents: 55887
diff changeset
   136
    count_of xs x - count_of ys x = count_of (subtract_entries_raw xs ys) x"
bb5ab5fce93a tuned proofs;
wenzelm
parents: 55887
diff changeset
   137
  unfolding count_of_def by (simp add: map_of_subtract_entries_raw split: option.split)
51599
1559e9266280 optionalized very specific code setup for multisets
haftmann
parents:
diff changeset
   138
1559e9266280 optionalized very specific code setup for multisets
haftmann
parents:
diff changeset
   139
58806
bb5ab5fce93a tuned proofs;
wenzelm
parents: 55887
diff changeset
   140
text \<open>Code equations for multiset operations\<close>
51599
1559e9266280 optionalized very specific code setup for multisets
haftmann
parents:
diff changeset
   141
58806
bb5ab5fce93a tuned proofs;
wenzelm
parents: 55887
diff changeset
   142
definition Bag :: "('a, nat) alist \<Rightarrow> 'a multiset"
bb5ab5fce93a tuned proofs;
wenzelm
parents: 55887
diff changeset
   143
  where "Bag xs = Abs_multiset (count_of (DAList.impl_of xs))"
51599
1559e9266280 optionalized very specific code setup for multisets
haftmann
parents:
diff changeset
   144
1559e9266280 optionalized very specific code setup for multisets
haftmann
parents:
diff changeset
   145
code_datatype Bag
1559e9266280 optionalized very specific code setup for multisets
haftmann
parents:
diff changeset
   146
58806
bb5ab5fce93a tuned proofs;
wenzelm
parents: 55887
diff changeset
   147
lemma count_Bag [simp, code]: "count (Bag xs) = count_of (DAList.impl_of xs)"
bb5ab5fce93a tuned proofs;
wenzelm
parents: 55887
diff changeset
   148
  by (simp add: Bag_def count_of_multiset)
51599
1559e9266280 optionalized very specific code setup for multisets
haftmann
parents:
diff changeset
   149
82596
267db8c321c4 executable sorted_list_of_multiset
haftmann
parents: 73411
diff changeset
   150
lemma Bag_eq:
267db8c321c4 executable sorted_list_of_multiset
haftmann
parents: 73411
diff changeset
   151
  \<open>Bag ms = (\<Sum>(a, n)\<leftarrow>alist.impl_of ms. replicate_mset n a)\<close>
267db8c321c4 executable sorted_list_of_multiset
haftmann
parents: 73411
diff changeset
   152
  for ms :: \<open>('a, nat) alist\<close>
267db8c321c4 executable sorted_list_of_multiset
haftmann
parents: 73411
diff changeset
   153
proof -
267db8c321c4 executable sorted_list_of_multiset
haftmann
parents: 73411
diff changeset
   154
  have *: \<open>count_of xs a = count (\<Sum>(a, n)\<leftarrow>xs. replicate_mset n a) a\<close>
267db8c321c4 executable sorted_list_of_multiset
haftmann
parents: 73411
diff changeset
   155
    if \<open>distinct (map fst xs)\<close>
267db8c321c4 executable sorted_list_of_multiset
haftmann
parents: 73411
diff changeset
   156
    for xs and a :: 'a
267db8c321c4 executable sorted_list_of_multiset
haftmann
parents: 73411
diff changeset
   157
  using that proof (induction xs)
267db8c321c4 executable sorted_list_of_multiset
haftmann
parents: 73411
diff changeset
   158
    case Nil
267db8c321c4 executable sorted_list_of_multiset
haftmann
parents: 73411
diff changeset
   159
    then show ?case
267db8c321c4 executable sorted_list_of_multiset
haftmann
parents: 73411
diff changeset
   160
      by simp
267db8c321c4 executable sorted_list_of_multiset
haftmann
parents: 73411
diff changeset
   161
  next
267db8c321c4 executable sorted_list_of_multiset
haftmann
parents: 73411
diff changeset
   162
    case (Cons xn xs)
267db8c321c4 executable sorted_list_of_multiset
haftmann
parents: 73411
diff changeset
   163
    then show ?case by (cases xn)
267db8c321c4 executable sorted_list_of_multiset
haftmann
parents: 73411
diff changeset
   164
      (auto simp add: count_eq_zero_iff if_split_mem2 image_iff)
267db8c321c4 executable sorted_list_of_multiset
haftmann
parents: 73411
diff changeset
   165
  qed
267db8c321c4 executable sorted_list_of_multiset
haftmann
parents: 73411
diff changeset
   166
  show ?thesis
267db8c321c4 executable sorted_list_of_multiset
haftmann
parents: 73411
diff changeset
   167
    by (rule multiset_eqI) (simp add: *)
267db8c321c4 executable sorted_list_of_multiset
haftmann
parents: 73411
diff changeset
   168
qed
267db8c321c4 executable sorted_list_of_multiset
haftmann
parents: 73411
diff changeset
   169
58806
bb5ab5fce93a tuned proofs;
wenzelm
parents: 55887
diff changeset
   170
lemma Mempty_Bag [code]: "{#} = Bag (DAList.empty)"
51599
1559e9266280 optionalized very specific code setup for multisets
haftmann
parents:
diff changeset
   171
  by (simp add: multiset_eq_iff alist.Alist_inverse DAList.empty_def)
1559e9266280 optionalized very specific code setup for multisets
haftmann
parents:
diff changeset
   172
63195
f3f08c0d4aaf Tuned code equations for mappings and PMFs
eberlm
parents: 63040
diff changeset
   173
lift_definition is_empty_Bag_impl :: "('a, nat) alist \<Rightarrow> bool" is
f3f08c0d4aaf Tuned code equations for mappings and PMFs
eberlm
parents: 63040
diff changeset
   174
  "\<lambda>xs. list_all (\<lambda>x. snd x = 0) xs" .
f3f08c0d4aaf Tuned code equations for mappings and PMFs
eberlm
parents: 63040
diff changeset
   175
f3f08c0d4aaf Tuned code equations for mappings and PMFs
eberlm
parents: 63040
diff changeset
   176
lemma is_empty_Bag [code]: "Multiset.is_empty (Bag xs) \<longleftrightarrow> is_empty_Bag_impl xs"
f3f08c0d4aaf Tuned code equations for mappings and PMFs
eberlm
parents: 63040
diff changeset
   177
proof -
f3f08c0d4aaf Tuned code equations for mappings and PMFs
eberlm
parents: 63040
diff changeset
   178
  have "Multiset.is_empty (Bag xs) \<longleftrightarrow> (\<forall>x. count (Bag xs) x = 0)"
f3f08c0d4aaf Tuned code equations for mappings and PMFs
eberlm
parents: 63040
diff changeset
   179
    unfolding Multiset.is_empty_def multiset_eq_iff by simp
f3f08c0d4aaf Tuned code equations for mappings and PMFs
eberlm
parents: 63040
diff changeset
   180
  also have "\<dots> \<longleftrightarrow> (\<forall>x\<in>fst ` set (alist.impl_of xs). count (Bag xs) x = 0)"
f3f08c0d4aaf Tuned code equations for mappings and PMFs
eberlm
parents: 63040
diff changeset
   181
  proof (intro iffI allI ballI)
f3f08c0d4aaf Tuned code equations for mappings and PMFs
eberlm
parents: 63040
diff changeset
   182
    fix x assume A: "\<forall>x\<in>fst ` set (alist.impl_of xs). count (Bag xs) x = 0"
f3f08c0d4aaf Tuned code equations for mappings and PMFs
eberlm
parents: 63040
diff changeset
   183
    thus "count (Bag xs) x = 0"
f3f08c0d4aaf Tuned code equations for mappings and PMFs
eberlm
parents: 63040
diff changeset
   184
    proof (cases "x \<in> fst ` set (alist.impl_of xs)")
f3f08c0d4aaf Tuned code equations for mappings and PMFs
eberlm
parents: 63040
diff changeset
   185
      case False
f3f08c0d4aaf Tuned code equations for mappings and PMFs
eberlm
parents: 63040
diff changeset
   186
      thus ?thesis by (force simp: count_of_def split: option.splits)
f3f08c0d4aaf Tuned code equations for mappings and PMFs
eberlm
parents: 63040
diff changeset
   187
    qed (insert A, auto)
f3f08c0d4aaf Tuned code equations for mappings and PMFs
eberlm
parents: 63040
diff changeset
   188
  qed simp_all
f3f08c0d4aaf Tuned code equations for mappings and PMFs
eberlm
parents: 63040
diff changeset
   189
  also have "\<dots> \<longleftrightarrow> list_all (\<lambda>x. snd x = 0) (alist.impl_of xs)" 
f3f08c0d4aaf Tuned code equations for mappings and PMFs
eberlm
parents: 63040
diff changeset
   190
    by (auto simp: count_of_def list_all_def)
f3f08c0d4aaf Tuned code equations for mappings and PMFs
eberlm
parents: 63040
diff changeset
   191
  finally show ?thesis by (simp add: is_empty_Bag_impl.rep_eq)
f3f08c0d4aaf Tuned code equations for mappings and PMFs
eberlm
parents: 63040
diff changeset
   192
qed
f3f08c0d4aaf Tuned code equations for mappings and PMFs
eberlm
parents: 63040
diff changeset
   193
58806
bb5ab5fce93a tuned proofs;
wenzelm
parents: 55887
diff changeset
   194
lemma union_Bag [code]: "Bag xs + Bag ys = Bag (join (\<lambda>x (n1, n2). n1 + n2) xs ys)"
bb5ab5fce93a tuned proofs;
wenzelm
parents: 55887
diff changeset
   195
  by (rule multiset_eqI)
bb5ab5fce93a tuned proofs;
wenzelm
parents: 55887
diff changeset
   196
    (simp add: count_of_join_raw alist.Alist_inverse distinct_join_raw join_def)
51599
1559e9266280 optionalized very specific code setup for multisets
haftmann
parents:
diff changeset
   197
63793
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63310
diff changeset
   198
lemma add_mset_Bag [code]: "add_mset x (Bag xs) =
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63310
diff changeset
   199
    Bag (join (\<lambda>x (n1, n2). n1 + n2) (DAList.update x 1 DAList.empty) xs)"
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63310
diff changeset
   200
  unfolding add_mset_add_single[of x "Bag xs"] union_Bag[symmetric]
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63310
diff changeset
   201
  by (simp add: multiset_eq_iff update.rep_eq empty.rep_eq)
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63310
diff changeset
   202
58806
bb5ab5fce93a tuned proofs;
wenzelm
parents: 55887
diff changeset
   203
lemma minus_Bag [code]: "Bag xs - Bag ys = Bag (subtract_entries xs ys)"
bb5ab5fce93a tuned proofs;
wenzelm
parents: 55887
diff changeset
   204
  by (rule multiset_eqI)
bb5ab5fce93a tuned proofs;
wenzelm
parents: 55887
diff changeset
   205
    (simp add: count_of_subtract_entries_raw alist.Alist_inverse
bb5ab5fce93a tuned proofs;
wenzelm
parents: 55887
diff changeset
   206
      distinct_subtract_entries_raw subtract_entries_def)
51599
1559e9266280 optionalized very specific code setup for multisets
haftmann
parents:
diff changeset
   207
59998
c54d36be22ef renamed Multiset.fold -> fold_mset, Multiset.filter -> filter_mset
nipkow
parents: 59949
diff changeset
   208
lemma filter_Bag [code]: "filter_mset P (Bag xs) = Bag (DAList.filter (P \<circ> fst) xs)"
58806
bb5ab5fce93a tuned proofs;
wenzelm
parents: 55887
diff changeset
   209
  by (rule multiset_eqI) (simp add: count_of_filter DAList.filter.rep_eq)
51599
1559e9266280 optionalized very specific code setup for multisets
haftmann
parents:
diff changeset
   210
55808
488c3e8282c8 added Rene Thiemann's patch for the nonterminating equality/subset test code for multisets
nipkow
parents: 51623
diff changeset
   211
64587
8355a6e2df79 standardized notation
haftmann
parents: 63830
diff changeset
   212
lemma mset_eq [code]: "HOL.equal (m1::'a::equal multiset) m2 \<longleftrightarrow> m1 \<subseteq># m2 \<and> m2 \<subseteq># m1"
73411
1f1366966296 avoid name clash
haftmann
parents: 73393
diff changeset
   213
  by (metis equal_multiset_def subset_mset.order_eq_iff)
55808
488c3e8282c8 added Rene Thiemann's patch for the nonterminating equality/subset test code for multisets
nipkow
parents: 51623
diff changeset
   214
69593
3dda49e08b9d isabelle update -u control_cartouches;
wenzelm
parents: 69064
diff changeset
   215
text \<open>By default the code for \<open><\<close> is \<^prop>\<open>xs < ys \<longleftrightarrow> xs \<le> ys \<and> \<not> xs = ys\<close>.
61585
a9599d3d7610 isabelle update_cartouches -c -t;
wenzelm
parents: 61115
diff changeset
   216
With equality implemented by \<open>\<le>\<close>, this leads to three calls of  \<open>\<le>\<close>.
58806
bb5ab5fce93a tuned proofs;
wenzelm
parents: 55887
diff changeset
   217
Here is a more efficient version:\<close>
64587
8355a6e2df79 standardized notation
haftmann
parents: 63830
diff changeset
   218
lemma mset_less[code]: "xs \<subset># (ys :: 'a multiset) \<longleftrightarrow> xs \<subseteq># ys \<and> \<not> ys \<subseteq># xs"
60397
f8a513fedb31 Renaming multiset operators < ~> <#,...
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 59998
diff changeset
   219
  by (rule subset_mset.less_le_not_le)
55887
25bd4745ee38 more code lemmas by Rene Thiemann
nipkow
parents: 55808
diff changeset
   220
25bd4745ee38 more code lemmas by Rene Thiemann
nipkow
parents: 55808
diff changeset
   221
lemma mset_less_eq_Bag0:
64587
8355a6e2df79 standardized notation
haftmann
parents: 63830
diff changeset
   222
  "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)"
51599
1559e9266280 optionalized very specific code setup for multisets
haftmann
parents:
diff changeset
   223
    (is "?lhs \<longleftrightarrow> ?rhs")
1559e9266280 optionalized very specific code setup for multisets
haftmann
parents:
diff changeset
   224
proof
58806
bb5ab5fce93a tuned proofs;
wenzelm
parents: 55887
diff changeset
   225
  assume ?lhs
60397
f8a513fedb31 Renaming multiset operators < ~> <#,...
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 59998
diff changeset
   226
  then show ?rhs by (auto simp add: subseteq_mset_def)
51599
1559e9266280 optionalized very specific code setup for multisets
haftmann
parents:
diff changeset
   227
next
1559e9266280 optionalized very specific code setup for multisets
haftmann
parents:
diff changeset
   228
  assume ?rhs
1559e9266280 optionalized very specific code setup for multisets
haftmann
parents:
diff changeset
   229
  show ?lhs
63310
caaacf37943f normalising multiset theorem names
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63195
diff changeset
   230
  proof (rule mset_subset_eqI)
51599
1559e9266280 optionalized very specific code setup for multisets
haftmann
parents:
diff changeset
   231
    fix x
58806
bb5ab5fce93a tuned proofs;
wenzelm
parents: 55887
diff changeset
   232
    from \<open>?rhs\<close> have "count_of (DAList.impl_of xs) x \<le> count A x"
51599
1559e9266280 optionalized very specific code setup for multisets
haftmann
parents:
diff changeset
   233
      by (cases "x \<in> fst ` set (DAList.impl_of xs)") (auto simp add: count_of_empty)
60397
f8a513fedb31 Renaming multiset operators < ~> <#,...
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 59998
diff changeset
   234
    then show "count (Bag xs) x \<le> count A x" by (simp add: subset_mset_def)
51599
1559e9266280 optionalized very specific code setup for multisets
haftmann
parents:
diff changeset
   235
  qed
1559e9266280 optionalized very specific code setup for multisets
haftmann
parents:
diff changeset
   236
qed
1559e9266280 optionalized very specific code setup for multisets
haftmann
parents:
diff changeset
   237
55887
25bd4745ee38 more code lemmas by Rene Thiemann
nipkow
parents: 55808
diff changeset
   238
lemma mset_less_eq_Bag [code]:
64587
8355a6e2df79 standardized notation
haftmann
parents: 63830
diff changeset
   239
  "Bag xs \<subseteq># (A :: 'a multiset) \<longleftrightarrow> (\<forall>(x, n) \<in> set (DAList.impl_of xs). n \<le> count A x)"
55887
25bd4745ee38 more code lemmas by Rene Thiemann
nipkow
parents: 55808
diff changeset
   240
proof -
25bd4745ee38 more code lemmas by Rene Thiemann
nipkow
parents: 55808
diff changeset
   241
  {
25bd4745ee38 more code lemmas by Rene Thiemann
nipkow
parents: 55808
diff changeset
   242
    fix x n
25bd4745ee38 more code lemmas by Rene Thiemann
nipkow
parents: 55808
diff changeset
   243
    assume "(x,n) \<in> set (DAList.impl_of xs)"
58806
bb5ab5fce93a tuned proofs;
wenzelm
parents: 55887
diff changeset
   244
    then have "count_of (DAList.impl_of xs) x = n"
bb5ab5fce93a tuned proofs;
wenzelm
parents: 55887
diff changeset
   245
    proof transfer
bb5ab5fce93a tuned proofs;
wenzelm
parents: 55887
diff changeset
   246
      fix x n
bb5ab5fce93a tuned proofs;
wenzelm
parents: 55887
diff changeset
   247
      fix xs :: "('a \<times> nat) list"
55887
25bd4745ee38 more code lemmas by Rene Thiemann
nipkow
parents: 55808
diff changeset
   248
      show "(distinct \<circ> map fst) xs \<Longrightarrow> (x, n) \<in> set xs \<Longrightarrow> count_of xs x = n"
58806
bb5ab5fce93a tuned proofs;
wenzelm
parents: 55887
diff changeset
   249
      proof (induct xs)
bb5ab5fce93a tuned proofs;
wenzelm
parents: 55887
diff changeset
   250
        case Nil
bb5ab5fce93a tuned proofs;
wenzelm
parents: 55887
diff changeset
   251
        then show ?case by simp
bb5ab5fce93a tuned proofs;
wenzelm
parents: 55887
diff changeset
   252
      next
bb5ab5fce93a tuned proofs;
wenzelm
parents: 55887
diff changeset
   253
        case (Cons ym ys)
55887
25bd4745ee38 more code lemmas by Rene Thiemann
nipkow
parents: 55808
diff changeset
   254
        obtain y m where ym: "ym = (y,m)" by force
25bd4745ee38 more code lemmas by Rene Thiemann
nipkow
parents: 55808
diff changeset
   255
        note Cons = Cons[unfolded ym]
25bd4745ee38 more code lemmas by Rene Thiemann
nipkow
parents: 55808
diff changeset
   256
        show ?case
25bd4745ee38 more code lemmas by Rene Thiemann
nipkow
parents: 55808
diff changeset
   257
        proof (cases "x = y")
25bd4745ee38 more code lemmas by Rene Thiemann
nipkow
parents: 55808
diff changeset
   258
          case False
58806
bb5ab5fce93a tuned proofs;
wenzelm
parents: 55887
diff changeset
   259
          with Cons show ?thesis
bb5ab5fce93a tuned proofs;
wenzelm
parents: 55887
diff changeset
   260
            unfolding ym by auto
55887
25bd4745ee38 more code lemmas by Rene Thiemann
nipkow
parents: 55808
diff changeset
   261
        next
25bd4745ee38 more code lemmas by Rene Thiemann
nipkow
parents: 55808
diff changeset
   262
          case True
25bd4745ee38 more code lemmas by Rene Thiemann
nipkow
parents: 55808
diff changeset
   263
          with Cons(2-3) have "m = n" by force
58806
bb5ab5fce93a tuned proofs;
wenzelm
parents: 55887
diff changeset
   264
          with True show ?thesis
bb5ab5fce93a tuned proofs;
wenzelm
parents: 55887
diff changeset
   265
            unfolding ym by auto
55887
25bd4745ee38 more code lemmas by Rene Thiemann
nipkow
parents: 55808
diff changeset
   266
        qed
58806
bb5ab5fce93a tuned proofs;
wenzelm
parents: 55887
diff changeset
   267
      qed
55887
25bd4745ee38 more code lemmas by Rene Thiemann
nipkow
parents: 55808
diff changeset
   268
    qed
25bd4745ee38 more code lemmas by Rene Thiemann
nipkow
parents: 55808
diff changeset
   269
  }
58806
bb5ab5fce93a tuned proofs;
wenzelm
parents: 55887
diff changeset
   270
  then show ?thesis
bb5ab5fce93a tuned proofs;
wenzelm
parents: 55887
diff changeset
   271
    unfolding mset_less_eq_Bag0 by auto
55887
25bd4745ee38 more code lemmas by Rene Thiemann
nipkow
parents: 55808
diff changeset
   272
qed
25bd4745ee38 more code lemmas by Rene Thiemann
nipkow
parents: 55808
diff changeset
   273
73393
716d256259d5 consolidated names
haftmann
parents: 73270
diff changeset
   274
declare inter_mset_def [code]
716d256259d5 consolidated names
haftmann
parents: 73270
diff changeset
   275
declare union_mset_def [code]
60515
484559628038 renamed multiset_of -> mset
nipkow
parents: 60495
diff changeset
   276
declare mset.simps [code]
51600
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
   277
55887
25bd4745ee38 more code lemmas by Rene Thiemann
nipkow
parents: 55808
diff changeset
   278
58806
bb5ab5fce93a tuned proofs;
wenzelm
parents: 55887
diff changeset
   279
fun fold_impl :: "('a \<Rightarrow> nat \<Rightarrow> 'b \<Rightarrow> 'b) \<Rightarrow> 'b \<Rightarrow> ('a \<times> nat) list \<Rightarrow> 'b"
bb5ab5fce93a tuned proofs;
wenzelm
parents: 55887
diff changeset
   280
where
55887
25bd4745ee38 more code lemmas by Rene Thiemann
nipkow
parents: 55808
diff changeset
   281
  "fold_impl fn e ((a,n) # ms) = (fold_impl fn ((fn a n) e) ms)"
25bd4745ee38 more code lemmas by Rene Thiemann
nipkow
parents: 55808
diff changeset
   282
| "fold_impl fn e [] = e"
25bd4745ee38 more code lemmas by Rene Thiemann
nipkow
parents: 55808
diff changeset
   283
61115
3a4400985780 modernized name space management -- more uniform qualification;
wenzelm
parents: 60679
diff changeset
   284
context
3a4400985780 modernized name space management -- more uniform qualification;
wenzelm
parents: 60679
diff changeset
   285
begin
3a4400985780 modernized name space management -- more uniform qualification;
wenzelm
parents: 60679
diff changeset
   286
3a4400985780 modernized name space management -- more uniform qualification;
wenzelm
parents: 60679
diff changeset
   287
qualified definition fold :: "('a \<Rightarrow> nat \<Rightarrow> 'b \<Rightarrow> 'b) \<Rightarrow> 'b \<Rightarrow> ('a, nat) alist \<Rightarrow> 'b"
58806
bb5ab5fce93a tuned proofs;
wenzelm
parents: 55887
diff changeset
   288
  where "fold f e al = fold_impl f e (DAList.impl_of al)"
55887
25bd4745ee38 more code lemmas by Rene Thiemann
nipkow
parents: 55808
diff changeset
   289
61115
3a4400985780 modernized name space management -- more uniform qualification;
wenzelm
parents: 60679
diff changeset
   290
end
55887
25bd4745ee38 more code lemmas by Rene Thiemann
nipkow
parents: 55808
diff changeset
   291
25bd4745ee38 more code lemmas by Rene Thiemann
nipkow
parents: 55808
diff changeset
   292
context comp_fun_commute
25bd4745ee38 more code lemmas by Rene Thiemann
nipkow
parents: 55808
diff changeset
   293
begin
25bd4745ee38 more code lemmas by Rene Thiemann
nipkow
parents: 55808
diff changeset
   294
58806
bb5ab5fce93a tuned proofs;
wenzelm
parents: 55887
diff changeset
   295
lemma DAList_Multiset_fold:
bb5ab5fce93a tuned proofs;
wenzelm
parents: 55887
diff changeset
   296
  assumes fn: "\<And>a n x. fn a n x = (f a ^^ n) x"
59998
c54d36be22ef renamed Multiset.fold -> fold_mset, Multiset.filter -> filter_mset
nipkow
parents: 59949
diff changeset
   297
  shows "fold_mset f e (Bag al) = DAList_Multiset.fold fn e al"
58806
bb5ab5fce93a tuned proofs;
wenzelm
parents: 55887
diff changeset
   298
  unfolding DAList_Multiset.fold_def
55887
25bd4745ee38 more code lemmas by Rene Thiemann
nipkow
parents: 55808
diff changeset
   299
proof (induct al)
25bd4745ee38 more code lemmas by Rene Thiemann
nipkow
parents: 55808
diff changeset
   300
  fix ys
58806
bb5ab5fce93a tuned proofs;
wenzelm
parents: 55887
diff changeset
   301
  let ?inv = "{xs :: ('a \<times> nat) list. (distinct \<circ> map fst) xs}"
55887
25bd4745ee38 more code lemmas by Rene Thiemann
nipkow
parents: 55808
diff changeset
   302
  note cs[simp del] = count_simps
58806
bb5ab5fce93a tuned proofs;
wenzelm
parents: 55887
diff changeset
   303
  have count[simp]: "\<And>x. count (Abs_multiset (count_of x)) = count_of x"
73270
e2d03448d5b5 get rid of traditional predicate
haftmann
parents: 69593
diff changeset
   304
    by (rule Abs_multiset_inverse) (simp add: count_of_multiset)
55887
25bd4745ee38 more code lemmas by Rene Thiemann
nipkow
parents: 55808
diff changeset
   305
  assume ys: "ys \<in> ?inv"
59998
c54d36be22ef renamed Multiset.fold -> fold_mset, Multiset.filter -> filter_mset
nipkow
parents: 59949
diff changeset
   306
  then show "fold_mset f e (Bag (Alist ys)) = fold_impl fn e (DAList.impl_of (Alist ys))"
55887
25bd4745ee38 more code lemmas by Rene Thiemann
nipkow
parents: 55808
diff changeset
   307
    unfolding Bag_def unfolding Alist_inverse[OF ys]
25bd4745ee38 more code lemmas by Rene Thiemann
nipkow
parents: 55808
diff changeset
   308
  proof (induct ys arbitrary: e rule: list.induct)
25bd4745ee38 more code lemmas by Rene Thiemann
nipkow
parents: 55808
diff changeset
   309
    case Nil
25bd4745ee38 more code lemmas by Rene Thiemann
nipkow
parents: 55808
diff changeset
   310
    show ?case
59998
c54d36be22ef renamed Multiset.fold -> fold_mset, Multiset.filter -> filter_mset
nipkow
parents: 59949
diff changeset
   311
      by (rule trans[OF arg_cong[of _ "{#}" "fold_mset f e", OF multiset_eqI]])
55887
25bd4745ee38 more code lemmas by Rene Thiemann
nipkow
parents: 55808
diff changeset
   312
         (auto, simp add: cs)
25bd4745ee38 more code lemmas by Rene Thiemann
nipkow
parents: 55808
diff changeset
   313
  next
25bd4745ee38 more code lemmas by Rene Thiemann
nipkow
parents: 55808
diff changeset
   314
    case (Cons pair ys e)
58806
bb5ab5fce93a tuned proofs;
wenzelm
parents: 55887
diff changeset
   315
    obtain a n where pair: "pair = (a,n)"
bb5ab5fce93a tuned proofs;
wenzelm
parents: 55887
diff changeset
   316
      by force
bb5ab5fce93a tuned proofs;
wenzelm
parents: 55887
diff changeset
   317
    from fn[of a n] have [simp]: "fn a n = (f a ^^ n)"
bb5ab5fce93a tuned proofs;
wenzelm
parents: 55887
diff changeset
   318
      by auto
bb5ab5fce93a tuned proofs;
wenzelm
parents: 55887
diff changeset
   319
    have inv: "ys \<in> ?inv"
bb5ab5fce93a tuned proofs;
wenzelm
parents: 55887
diff changeset
   320
      using Cons(2) by auto
55887
25bd4745ee38 more code lemmas by Rene Thiemann
nipkow
parents: 55808
diff changeset
   321
    note IH = Cons(1)[OF inv]
63040
eb4ddd18d635 eliminated old 'def';
wenzelm
parents: 61585
diff changeset
   322
    define Ys where "Ys = Abs_multiset (count_of ys)"
67399
eab6ce8368fa ran isabelle update_op on all sources
nipkow
parents: 66148
diff changeset
   323
    have id: "Abs_multiset (count_of ((a, n) # ys)) = (((+) {# a #}) ^^ n) Ys"
55887
25bd4745ee38 more code lemmas by Rene Thiemann
nipkow
parents: 55808
diff changeset
   324
      unfolding Ys_def
25bd4745ee38 more code lemmas by Rene Thiemann
nipkow
parents: 55808
diff changeset
   325
    proof (rule multiset_eqI, unfold count)
58806
bb5ab5fce93a tuned proofs;
wenzelm
parents: 55887
diff changeset
   326
      fix c
bb5ab5fce93a tuned proofs;
wenzelm
parents: 55887
diff changeset
   327
      show "count_of ((a, n) # ys) c =
67399
eab6ce8368fa ran isabelle update_op on all sources
nipkow
parents: 66148
diff changeset
   328
        count (((+) {#a#} ^^ n) (Abs_multiset (count_of ys))) c" (is "?l = ?r")
55887
25bd4745ee38 more code lemmas by Rene Thiemann
nipkow
parents: 55808
diff changeset
   329
      proof (cases "c = a")
58806
bb5ab5fce93a tuned proofs;
wenzelm
parents: 55887
diff changeset
   330
        case False
bb5ab5fce93a tuned proofs;
wenzelm
parents: 55887
diff changeset
   331
        then show ?thesis
bb5ab5fce93a tuned proofs;
wenzelm
parents: 55887
diff changeset
   332
          unfolding cs by (induct n) auto
55887
25bd4745ee38 more code lemmas by Rene Thiemann
nipkow
parents: 55808
diff changeset
   333
      next
25bd4745ee38 more code lemmas by Rene Thiemann
nipkow
parents: 55808
diff changeset
   334
        case True
58806
bb5ab5fce93a tuned proofs;
wenzelm
parents: 55887
diff changeset
   335
        then have "?l = n" by (simp add: cs)
55887
25bd4745ee38 more code lemmas by Rene Thiemann
nipkow
parents: 55808
diff changeset
   336
        also have "n = ?r" unfolding True
25bd4745ee38 more code lemmas by Rene Thiemann
nipkow
parents: 55808
diff changeset
   337
        proof (induct n)
25bd4745ee38 more code lemmas by Rene Thiemann
nipkow
parents: 55808
diff changeset
   338
          case 0
25bd4745ee38 more code lemmas by Rene Thiemann
nipkow
parents: 55808
diff changeset
   339
          from Cons(2)[unfolded pair] have "a \<notin> fst ` set ys" by auto
58806
bb5ab5fce93a tuned proofs;
wenzelm
parents: 55887
diff changeset
   340
          then show ?case by (induct ys) (simp, auto simp: cs)
bb5ab5fce93a tuned proofs;
wenzelm
parents: 55887
diff changeset
   341
        next
bb5ab5fce93a tuned proofs;
wenzelm
parents: 55887
diff changeset
   342
          case Suc
bb5ab5fce93a tuned proofs;
wenzelm
parents: 55887
diff changeset
   343
          then show ?case by simp
bb5ab5fce93a tuned proofs;
wenzelm
parents: 55887
diff changeset
   344
        qed
55887
25bd4745ee38 more code lemmas by Rene Thiemann
nipkow
parents: 55808
diff changeset
   345
        finally show ?thesis .
25bd4745ee38 more code lemmas by Rene Thiemann
nipkow
parents: 55808
diff changeset
   346
      qed
25bd4745ee38 more code lemmas by Rene Thiemann
nipkow
parents: 55808
diff changeset
   347
    qed
58806
bb5ab5fce93a tuned proofs;
wenzelm
parents: 55887
diff changeset
   348
    show ?case
bb5ab5fce93a tuned proofs;
wenzelm
parents: 55887
diff changeset
   349
      unfolding pair
bb5ab5fce93a tuned proofs;
wenzelm
parents: 55887
diff changeset
   350
      apply (simp add: IH[symmetric])
bb5ab5fce93a tuned proofs;
wenzelm
parents: 55887
diff changeset
   351
      unfolding id Ys_def[symmetric]
bb5ab5fce93a tuned proofs;
wenzelm
parents: 55887
diff changeset
   352
      apply (induct n)
bb5ab5fce93a tuned proofs;
wenzelm
parents: 55887
diff changeset
   353
      apply (auto simp: fold_mset_fun_left_comm[symmetric])
bb5ab5fce93a tuned proofs;
wenzelm
parents: 55887
diff changeset
   354
      done
55887
25bd4745ee38 more code lemmas by Rene Thiemann
nipkow
parents: 55808
diff changeset
   355
  qed
25bd4745ee38 more code lemmas by Rene Thiemann
nipkow
parents: 55808
diff changeset
   356
qed
25bd4745ee38 more code lemmas by Rene Thiemann
nipkow
parents: 55808
diff changeset
   357
58806
bb5ab5fce93a tuned proofs;
wenzelm
parents: 55887
diff changeset
   358
end
55887
25bd4745ee38 more code lemmas by Rene Thiemann
nipkow
parents: 55808
diff changeset
   359
61115
3a4400985780 modernized name space management -- more uniform qualification;
wenzelm
parents: 60679
diff changeset
   360
context
3a4400985780 modernized name space management -- more uniform qualification;
wenzelm
parents: 60679
diff changeset
   361
begin
3a4400985780 modernized name space management -- more uniform qualification;
wenzelm
parents: 60679
diff changeset
   362
3a4400985780 modernized name space management -- more uniform qualification;
wenzelm
parents: 60679
diff changeset
   363
private lift_definition single_alist_entry :: "'a \<Rightarrow> 'b \<Rightarrow> ('a, 'b) alist" is "\<lambda>a b. [(a, b)]"
58806
bb5ab5fce93a tuned proofs;
wenzelm
parents: 55887
diff changeset
   364
  by auto
55887
25bd4745ee38 more code lemmas by Rene Thiemann
nipkow
parents: 55808
diff changeset
   365
58806
bb5ab5fce93a tuned proofs;
wenzelm
parents: 55887
diff changeset
   366
lemma image_mset_Bag [code]:
55887
25bd4745ee38 more code lemmas by Rene Thiemann
nipkow
parents: 55808
diff changeset
   367
  "image_mset f (Bag ms) =
58806
bb5ab5fce93a tuned proofs;
wenzelm
parents: 55887
diff changeset
   368
    DAList_Multiset.fold (\<lambda>a n m. Bag (single_alist_entry (f a) n) + m) {#} ms"
bb5ab5fce93a tuned proofs;
wenzelm
parents: 55887
diff changeset
   369
  unfolding image_mset_def
55887
25bd4745ee38 more code lemmas by Rene Thiemann
nipkow
parents: 55808
diff changeset
   370
proof (rule comp_fun_commute.DAList_Multiset_fold, unfold_locales, (auto simp: ac_simps)[1])
25bd4745ee38 more code lemmas by Rene Thiemann
nipkow
parents: 55808
diff changeset
   371
  fix a n m
63793
e68a0b651eb5 add_mset constructor in multisets
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 63310
diff changeset
   372
  show "Bag (single_alist_entry (f a) n) + m = ((add_mset \<circ> f) a ^^ n) m" (is "?l = ?r")
55887
25bd4745ee38 more code lemmas by Rene Thiemann
nipkow
parents: 55808
diff changeset
   373
  proof (rule multiset_eqI)
25bd4745ee38 more code lemmas by Rene Thiemann
nipkow
parents: 55808
diff changeset
   374
    fix x
25bd4745ee38 more code lemmas by Rene Thiemann
nipkow
parents: 55808
diff changeset
   375
    have "count ?r x = (if x = f a then n + count m x else count m x)"
58806
bb5ab5fce93a tuned proofs;
wenzelm
parents: 55887
diff changeset
   376
      by (induct n) auto
bb5ab5fce93a tuned proofs;
wenzelm
parents: 55887
diff changeset
   377
    also have "\<dots> = count ?l x"
bb5ab5fce93a tuned proofs;
wenzelm
parents: 55887
diff changeset
   378
      by (simp add: single_alist_entry.rep_eq)
55887
25bd4745ee38 more code lemmas by Rene Thiemann
nipkow
parents: 55808
diff changeset
   379
    finally show "count ?l x = count ?r x" ..
25bd4745ee38 more code lemmas by Rene Thiemann
nipkow
parents: 55808
diff changeset
   380
  qed
25bd4745ee38 more code lemmas by Rene Thiemann
nipkow
parents: 55808
diff changeset
   381
qed
25bd4745ee38 more code lemmas by Rene Thiemann
nipkow
parents: 55808
diff changeset
   382
61115
3a4400985780 modernized name space management -- more uniform qualification;
wenzelm
parents: 60679
diff changeset
   383
end
55887
25bd4745ee38 more code lemmas by Rene Thiemann
nipkow
parents: 55808
diff changeset
   384
69064
5840724b1d71 Prefix form of infix with * on either side no longer needs special treatment
nipkow
parents: 67408
diff changeset
   385
\<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>
67399
eab6ce8368fa ran isabelle update_op on all sources
nipkow
parents: 66148
diff changeset
   386
lemma sum_mset_Bag[code]: "sum_mset (Bag ms) = DAList_Multiset.fold (\<lambda>a n. (((+) a) ^^ n)) 0 ms"
63830
2ea3725a34bd msetsum -> set_mset, msetprod -> prod_mset
nipkow
parents: 63793
diff changeset
   387
  unfolding sum_mset.eq_fold
58806
bb5ab5fce93a tuned proofs;
wenzelm
parents: 55887
diff changeset
   388
  apply (rule comp_fun_commute.DAList_Multiset_fold)
bb5ab5fce93a tuned proofs;
wenzelm
parents: 55887
diff changeset
   389
  apply unfold_locales
bb5ab5fce93a tuned proofs;
wenzelm
parents: 55887
diff changeset
   390
  apply (auto simp: ac_simps)
bb5ab5fce93a tuned proofs;
wenzelm
parents: 55887
diff changeset
   391
  done
55887
25bd4745ee38 more code lemmas by Rene Thiemann
nipkow
parents: 55808
diff changeset
   392
69064
5840724b1d71 Prefix form of infix with * on either side no longer needs special treatment
nipkow
parents: 67408
diff changeset
   393
\<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>
5840724b1d71 Prefix form of infix with * on either side no longer needs special treatment
nipkow
parents: 67408
diff changeset
   394
lemma prod_mset_Bag[code]: "prod_mset (Bag ms) = DAList_Multiset.fold (\<lambda>a n. (((*) a) ^^ n)) 1 ms"
63830
2ea3725a34bd msetsum -> set_mset, msetprod -> prod_mset
nipkow
parents: 63793
diff changeset
   395
  unfolding prod_mset.eq_fold
58806
bb5ab5fce93a tuned proofs;
wenzelm
parents: 55887
diff changeset
   396
  apply (rule comp_fun_commute.DAList_Multiset_fold)
bb5ab5fce93a tuned proofs;
wenzelm
parents: 55887
diff changeset
   397
  apply unfold_locales
bb5ab5fce93a tuned proofs;
wenzelm
parents: 55887
diff changeset
   398
  apply (auto simp: ac_simps)
bb5ab5fce93a tuned proofs;
wenzelm
parents: 55887
diff changeset
   399
  done
55887
25bd4745ee38 more code lemmas by Rene Thiemann
nipkow
parents: 55808
diff changeset
   400
59998
c54d36be22ef renamed Multiset.fold -> fold_mset, Multiset.filter -> filter_mset
nipkow
parents: 59949
diff changeset
   401
lemma size_fold: "size A = fold_mset (\<lambda>_. Suc) 0 A" (is "_ = fold_mset ?f _ _")
55887
25bd4745ee38 more code lemmas by Rene Thiemann
nipkow
parents: 55808
diff changeset
   402
proof -
60679
ade12ef2773c tuned proofs;
wenzelm
parents: 60515
diff changeset
   403
  interpret comp_fun_commute ?f by standard auto
55887
25bd4745ee38 more code lemmas by Rene Thiemann
nipkow
parents: 55808
diff changeset
   404
  show ?thesis by (induct A) auto
25bd4745ee38 more code lemmas by Rene Thiemann
nipkow
parents: 55808
diff changeset
   405
qed
25bd4745ee38 more code lemmas by Rene Thiemann
nipkow
parents: 55808
diff changeset
   406
67399
eab6ce8368fa ran isabelle update_op on all sources
nipkow
parents: 66148
diff changeset
   407
lemma size_Bag[code]: "size (Bag ms) = DAList_Multiset.fold (\<lambda>a n. (+) n) 0 ms"
59949
fc4c896c8e74 Removed mcard because it is equal to size
nipkow
parents: 58881
diff changeset
   408
  unfolding size_fold
55887
25bd4745ee38 more code lemmas by Rene Thiemann
nipkow
parents: 55808
diff changeset
   409
proof (rule comp_fun_commute.DAList_Multiset_fold, unfold_locales, simp)
25bd4745ee38 more code lemmas by Rene Thiemann
nipkow
parents: 55808
diff changeset
   410
  fix a n x
58806
bb5ab5fce93a tuned proofs;
wenzelm
parents: 55887
diff changeset
   411
  show "n + x = (Suc ^^ n) x"
bb5ab5fce93a tuned proofs;
wenzelm
parents: 55887
diff changeset
   412
    by (induct n) auto
55887
25bd4745ee38 more code lemmas by Rene Thiemann
nipkow
parents: 55808
diff changeset
   413
qed
25bd4745ee38 more code lemmas by Rene Thiemann
nipkow
parents: 55808
diff changeset
   414
25bd4745ee38 more code lemmas by Rene Thiemann
nipkow
parents: 55808
diff changeset
   415
60495
d7ff0a1df90a renamed Multiset.set_of to the canonical set_mset
nipkow
parents: 60397
diff changeset
   416
lemma set_mset_fold: "set_mset A = fold_mset insert {} A" (is "_ = fold_mset ?f _ _")
55887
25bd4745ee38 more code lemmas by Rene Thiemann
nipkow
parents: 55808
diff changeset
   417
proof -
60679
ade12ef2773c tuned proofs;
wenzelm
parents: 60515
diff changeset
   418
  interpret comp_fun_commute ?f by standard auto
58806
bb5ab5fce93a tuned proofs;
wenzelm
parents: 55887
diff changeset
   419
  show ?thesis by (induct A) auto
55887
25bd4745ee38 more code lemmas by Rene Thiemann
nipkow
parents: 55808
diff changeset
   420
qed
25bd4745ee38 more code lemmas by Rene Thiemann
nipkow
parents: 55808
diff changeset
   421
60495
d7ff0a1df90a renamed Multiset.set_of to the canonical set_mset
nipkow
parents: 60397
diff changeset
   422
lemma set_mset_Bag[code]:
d7ff0a1df90a renamed Multiset.set_of to the canonical set_mset
nipkow
parents: 60397
diff changeset
   423
  "set_mset (Bag ms) = DAList_Multiset.fold (\<lambda>a n. (if n = 0 then (\<lambda>m. m) else insert a)) {} ms"
d7ff0a1df90a renamed Multiset.set_of to the canonical set_mset
nipkow
parents: 60397
diff changeset
   424
  unfolding set_mset_fold
55887
25bd4745ee38 more code lemmas by Rene Thiemann
nipkow
parents: 55808
diff changeset
   425
proof (rule comp_fun_commute.DAList_Multiset_fold, unfold_locales, (auto simp: ac_simps)[1])
25bd4745ee38 more code lemmas by Rene Thiemann
nipkow
parents: 55808
diff changeset
   426
  fix a n x
25bd4745ee38 more code lemmas by Rene Thiemann
nipkow
parents: 55808
diff changeset
   427
  show "(if n = 0 then \<lambda>m. m else insert a) x = (insert a ^^ n) x" (is "?l n = ?r n")
25bd4745ee38 more code lemmas by Rene Thiemann
nipkow
parents: 55808
diff changeset
   428
  proof (cases n)
58806
bb5ab5fce93a tuned proofs;
wenzelm
parents: 55887
diff changeset
   429
    case 0
bb5ab5fce93a tuned proofs;
wenzelm
parents: 55887
diff changeset
   430
    then show ?thesis by simp
bb5ab5fce93a tuned proofs;
wenzelm
parents: 55887
diff changeset
   431
  next
55887
25bd4745ee38 more code lemmas by Rene Thiemann
nipkow
parents: 55808
diff changeset
   432
    case (Suc m)
58806
bb5ab5fce93a tuned proofs;
wenzelm
parents: 55887
diff changeset
   433
    then have "?l n = insert a x" by simp
55887
25bd4745ee38 more code lemmas by Rene Thiemann
nipkow
parents: 55808
diff changeset
   434
    moreover have "?r n = insert a x" unfolding Suc by (induct m) auto
25bd4745ee38 more code lemmas by Rene Thiemann
nipkow
parents: 55808
diff changeset
   435
    ultimately show ?thesis by auto
58806
bb5ab5fce93a tuned proofs;
wenzelm
parents: 55887
diff changeset
   436
  qed
55887
25bd4745ee38 more code lemmas by Rene Thiemann
nipkow
parents: 55808
diff changeset
   437
qed
25bd4745ee38 more code lemmas by Rene Thiemann
nipkow
parents: 55808
diff changeset
   438
82596
267db8c321c4 executable sorted_list_of_multiset
haftmann
parents: 73411
diff changeset
   439
lemma sorted_list_of_multiset_Bag [code]:
267db8c321c4 executable sorted_list_of_multiset
haftmann
parents: 73411
diff changeset
   440
  \<open>sorted_list_of_multiset (Bag ms) = concat (map (\<lambda>(a, n). replicate n a)
267db8c321c4 executable sorted_list_of_multiset
haftmann
parents: 73411
diff changeset
   441
    (sort_key fst (DAList.impl_of ms)))\<close>  (is \<open>?lhs = ?rhs\<close>)
267db8c321c4 executable sorted_list_of_multiset
haftmann
parents: 73411
diff changeset
   442
proof -
267db8c321c4 executable sorted_list_of_multiset
haftmann
parents: 73411
diff changeset
   443
  have *: \<open>sorted (concat (map (\<lambda>(a, n). replicate n a) ans))\<close>
267db8c321c4 executable sorted_list_of_multiset
haftmann
parents: 73411
diff changeset
   444
    if \<open>sorted (map fst ans)\<close>
267db8c321c4 executable sorted_list_of_multiset
haftmann
parents: 73411
diff changeset
   445
    for ans :: \<open>('a \<times> nat) list\<close>
267db8c321c4 executable sorted_list_of_multiset
haftmann
parents: 73411
diff changeset
   446
    using that by (induction ans) (auto simp add: sorted_append)
267db8c321c4 executable sorted_list_of_multiset
haftmann
parents: 73411
diff changeset
   447
  have \<open>mset ?rhs = mset ?lhs\<close>
267db8c321c4 executable sorted_list_of_multiset
haftmann
parents: 73411
diff changeset
   448
    by (simp add: Bag_eq mset_concat comp_def split_def flip: sum_mset_sum_list)
267db8c321c4 executable sorted_list_of_multiset
haftmann
parents: 73411
diff changeset
   449
  moreover have \<open>sorted ?rhs\<close>
267db8c321c4 executable sorted_list_of_multiset
haftmann
parents: 73411
diff changeset
   450
    by (rule *) simp
267db8c321c4 executable sorted_list_of_multiset
haftmann
parents: 73411
diff changeset
   451
  ultimately have \<open>sort ?lhs = ?rhs\<close>
267db8c321c4 executable sorted_list_of_multiset
haftmann
parents: 73411
diff changeset
   452
    by (rule properties_for_sort)
267db8c321c4 executable sorted_list_of_multiset
haftmann
parents: 73411
diff changeset
   453
  then show ?thesis
267db8c321c4 executable sorted_list_of_multiset
haftmann
parents: 73411
diff changeset
   454
    by simp
267db8c321c4 executable sorted_list_of_multiset
haftmann
parents: 73411
diff changeset
   455
qed
55887
25bd4745ee38 more code lemmas by Rene Thiemann
nipkow
parents: 55808
diff changeset
   456
51600
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
   457
instantiation multiset :: (exhaustive) exhaustive
51599
1559e9266280 optionalized very specific code setup for multisets
haftmann
parents:
diff changeset
   458
begin
1559e9266280 optionalized very specific code setup for multisets
haftmann
parents:
diff changeset
   459
58806
bb5ab5fce93a tuned proofs;
wenzelm
parents: 55887
diff changeset
   460
definition exhaustive_multiset ::
bb5ab5fce93a tuned proofs;
wenzelm
parents: 55887
diff changeset
   461
  "('a multiset \<Rightarrow> (bool \<times> term list) option) \<Rightarrow> natural \<Rightarrow> (bool \<times> term list) option"
bb5ab5fce93a tuned proofs;
wenzelm
parents: 55887
diff changeset
   462
  where "exhaustive_multiset f i = Quickcheck_Exhaustive.exhaustive (\<lambda>xs. f (Bag xs)) i"
51599
1559e9266280 optionalized very specific code setup for multisets
haftmann
parents:
diff changeset
   463
1559e9266280 optionalized very specific code setup for multisets
haftmann
parents:
diff changeset
   464
instance ..
1559e9266280 optionalized very specific code setup for multisets
haftmann
parents:
diff changeset
   465
1559e9266280 optionalized very specific code setup for multisets
haftmann
parents:
diff changeset
   466
end
1559e9266280 optionalized very specific code setup for multisets
haftmann
parents:
diff changeset
   467
1559e9266280 optionalized very specific code setup for multisets
haftmann
parents:
diff changeset
   468
end
1559e9266280 optionalized very specific code setup for multisets
haftmann
parents:
diff changeset
   469