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