src/HOL/Library/DAList_Multiset.thy
author wenzelm
Sat, 02 Aug 2014 19:29:02 +0200
changeset 57843 d8966c09025c
parent 55887 25bd4745ee38
child 58806 bb5ab5fce93a
permissions -rw-r--r--
proper priority for error over warning also for node_status (see 9c5220e05e04);
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
55808
488c3e8282c8 added Rene Thiemann's patch for the nonterminating equality/subset test code for multisets
nipkow
parents: 51623
diff changeset
    69
lemma [code, code del]:
488c3e8282c8 added Rene Thiemann's patch for the nonterminating equality/subset test code for multisets
nipkow
parents: 51623
diff changeset
    70
  "ord_multiset_inst.less_eq_multiset = ord_multiset_inst.less_eq_multiset"
488c3e8282c8 added Rene Thiemann's patch for the nonterminating equality/subset test code for multisets
nipkow
parents: 51623
diff changeset
    71
  ..
488c3e8282c8 added Rene Thiemann's patch for the nonterminating equality/subset test code for multisets
nipkow
parents: 51623
diff changeset
    72
488c3e8282c8 added Rene Thiemann's patch for the nonterminating equality/subset test code for multisets
nipkow
parents: 51623
diff changeset
    73
lemma [code, code del]:
488c3e8282c8 added Rene Thiemann's patch for the nonterminating equality/subset test code for multisets
nipkow
parents: 51623
diff changeset
    74
  "ord_multiset_inst.less_multiset = ord_multiset_inst.less_multiset"
488c3e8282c8 added Rene Thiemann's patch for the nonterminating equality/subset test code for multisets
nipkow
parents: 51623
diff changeset
    75
  ..
488c3e8282c8 added Rene Thiemann's patch for the nonterminating equality/subset test code for multisets
nipkow
parents: 51623
diff changeset
    76
488c3e8282c8 added Rene Thiemann's patch for the nonterminating equality/subset test code for multisets
nipkow
parents: 51623
diff changeset
    77
lemma [code, code del]:
488c3e8282c8 added Rene Thiemann's patch for the nonterminating equality/subset test code for multisets
nipkow
parents: 51623
diff changeset
    78
  "equal_multiset_inst.equal_multiset = equal_multiset_inst.equal_multiset"
488c3e8282c8 added Rene Thiemann's patch for the nonterminating equality/subset test code for multisets
nipkow
parents: 51623
diff changeset
    79
  ..
488c3e8282c8 added Rene Thiemann's patch for the nonterminating equality/subset test code for multisets
nipkow
parents: 51623
diff changeset
    80
51600
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
    81
51599
1559e9266280 optionalized very specific code setup for multisets
haftmann
parents:
diff changeset
    82
text {* Raw operations on lists *}
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
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
    85
where
1559e9266280 optionalized very specific code setup for multisets
haftmann
parents:
diff changeset
    86
  "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
    87
1559e9266280 optionalized very specific code setup for multisets
haftmann
parents:
diff changeset
    88
lemma join_raw_Nil [simp]:
1559e9266280 optionalized very specific code setup for multisets
haftmann
parents:
diff changeset
    89
  "join_raw f xs [] = xs"
1559e9266280 optionalized very specific code setup for multisets
haftmann
parents:
diff changeset
    90
by (simp add: join_raw_def)
1559e9266280 optionalized very specific code setup for multisets
haftmann
parents:
diff changeset
    91
1559e9266280 optionalized very specific code setup for multisets
haftmann
parents:
diff changeset
    92
lemma join_raw_Cons [simp]:
1559e9266280 optionalized very specific code setup for multisets
haftmann
parents:
diff changeset
    93
  "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
    94
by (simp add: join_raw_def)
1559e9266280 optionalized very specific code setup for multisets
haftmann
parents:
diff changeset
    95
1559e9266280 optionalized very specific code setup for multisets
haftmann
parents:
diff changeset
    96
lemma map_of_join_raw:
1559e9266280 optionalized very specific code setup for multisets
haftmann
parents:
diff changeset
    97
  assumes "distinct (map fst ys)"
1559e9266280 optionalized very specific code setup for multisets
haftmann
parents:
diff changeset
    98
  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
    99
    (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
   100
using assms
1559e9266280 optionalized very specific code setup for multisets
haftmann
parents:
diff changeset
   101
apply (induct ys)
1559e9266280 optionalized very specific code setup for multisets
haftmann
parents:
diff changeset
   102
apply (auto simp add: map_of_map_default split: option.split)
1559e9266280 optionalized very specific code setup for multisets
haftmann
parents:
diff changeset
   103
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
   104
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
   105
1559e9266280 optionalized very specific code setup for multisets
haftmann
parents:
diff changeset
   106
lemma distinct_join_raw:
1559e9266280 optionalized very specific code setup for multisets
haftmann
parents:
diff changeset
   107
  assumes "distinct (map fst xs)"
1559e9266280 optionalized very specific code setup for multisets
haftmann
parents:
diff changeset
   108
  shows "distinct (map fst (join_raw f xs ys))"
1559e9266280 optionalized very specific code setup for multisets
haftmann
parents:
diff changeset
   109
using assms
1559e9266280 optionalized very specific code setup for multisets
haftmann
parents:
diff changeset
   110
proof (induct ys)
1559e9266280 optionalized very specific code setup for multisets
haftmann
parents:
diff changeset
   111
  case (Cons y ys)
1559e9266280 optionalized very specific code setup for multisets
haftmann
parents:
diff changeset
   112
  thus ?case by (cases y) (simp add: distinct_map_default)
1559e9266280 optionalized very specific code setup for multisets
haftmann
parents:
diff changeset
   113
qed auto
1559e9266280 optionalized very specific code setup for multisets
haftmann
parents:
diff changeset
   114
1559e9266280 optionalized very specific code setup for multisets
haftmann
parents:
diff changeset
   115
definition
1559e9266280 optionalized very specific code setup for multisets
haftmann
parents:
diff changeset
   116
  "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
   117
1559e9266280 optionalized very specific code setup for multisets
haftmann
parents:
diff changeset
   118
lemma map_of_subtract_entries_raw:
1559e9266280 optionalized very specific code setup for multisets
haftmann
parents:
diff changeset
   119
  assumes "distinct (map fst ys)"
1559e9266280 optionalized very specific code setup for multisets
haftmann
parents:
diff changeset
   120
  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
   121
    (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
   122
using assms unfolding subtract_entries_raw_def
1559e9266280 optionalized very specific code setup for multisets
haftmann
parents:
diff changeset
   123
apply (induct ys)
1559e9266280 optionalized very specific code setup for multisets
haftmann
parents:
diff changeset
   124
apply auto
1559e9266280 optionalized very specific code setup for multisets
haftmann
parents:
diff changeset
   125
apply (simp split: option.split)
1559e9266280 optionalized very specific code setup for multisets
haftmann
parents:
diff changeset
   126
apply (simp add: map_of_map_entry)
1559e9266280 optionalized very specific code setup for multisets
haftmann
parents:
diff changeset
   127
apply (auto split: option.split)
1559e9266280 optionalized very specific code setup for multisets
haftmann
parents:
diff changeset
   128
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
   129
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
   130
1559e9266280 optionalized very specific code setup for multisets
haftmann
parents:
diff changeset
   131
lemma distinct_subtract_entries_raw:
1559e9266280 optionalized very specific code setup for multisets
haftmann
parents:
diff changeset
   132
  assumes "distinct (map fst xs)"
1559e9266280 optionalized very specific code setup for multisets
haftmann
parents:
diff changeset
   133
  shows "distinct (map fst (subtract_entries_raw xs ys))"
1559e9266280 optionalized very specific code setup for multisets
haftmann
parents:
diff changeset
   134
using assms
1559e9266280 optionalized very specific code setup for multisets
haftmann
parents:
diff changeset
   135
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
   136
1559e9266280 optionalized very specific code setup for multisets
haftmann
parents:
diff changeset
   137
1559e9266280 optionalized very specific code setup for multisets
haftmann
parents:
diff changeset
   138
text {* Operations on alists with distinct keys *}
1559e9266280 optionalized very specific code setup for multisets
haftmann
parents:
diff changeset
   139
1559e9266280 optionalized very specific code setup for multisets
haftmann
parents:
diff changeset
   140
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
   141
is join_raw
1559e9266280 optionalized very specific code setup for multisets
haftmann
parents:
diff changeset
   142
by (simp add: distinct_join_raw)
1559e9266280 optionalized very specific code setup for multisets
haftmann
parents:
diff changeset
   143
1559e9266280 optionalized very specific code setup for multisets
haftmann
parents:
diff changeset
   144
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
   145
is subtract_entries_raw 
1559e9266280 optionalized very specific code setup for multisets
haftmann
parents:
diff changeset
   146
by (simp add: distinct_subtract_entries_raw)
1559e9266280 optionalized very specific code setup for multisets
haftmann
parents:
diff changeset
   147
1559e9266280 optionalized very specific code setup for multisets
haftmann
parents:
diff changeset
   148
1559e9266280 optionalized very specific code setup for multisets
haftmann
parents:
diff changeset
   149
text {* Implementing multisets by means of association lists *}
1559e9266280 optionalized very specific code setup for multisets
haftmann
parents:
diff changeset
   150
1559e9266280 optionalized very specific code setup for multisets
haftmann
parents:
diff changeset
   151
definition count_of :: "('a \<times> nat) list \<Rightarrow> 'a \<Rightarrow> nat" where
1559e9266280 optionalized very specific code setup for multisets
haftmann
parents:
diff changeset
   152
  "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
   153
1559e9266280 optionalized very specific code setup for multisets
haftmann
parents:
diff changeset
   154
lemma count_of_multiset:
1559e9266280 optionalized very specific code setup for multisets
haftmann
parents:
diff changeset
   155
  "count_of xs \<in> multiset"
1559e9266280 optionalized very specific code setup for multisets
haftmann
parents:
diff changeset
   156
proof -
1559e9266280 optionalized very specific code setup for multisets
haftmann
parents:
diff changeset
   157
  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
   158
  have "?A \<subseteq> dom (map_of xs)"
1559e9266280 optionalized very specific code setup for multisets
haftmann
parents:
diff changeset
   159
  proof
1559e9266280 optionalized very specific code setup for multisets
haftmann
parents:
diff changeset
   160
    fix x
1559e9266280 optionalized very specific code setup for multisets
haftmann
parents:
diff changeset
   161
    assume "x \<in> ?A"
1559e9266280 optionalized very specific code setup for multisets
haftmann
parents:
diff changeset
   162
    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
   163
    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
   164
    then show "x \<in> dom (map_of xs)" by auto
1559e9266280 optionalized very specific code setup for multisets
haftmann
parents:
diff changeset
   165
  qed
1559e9266280 optionalized very specific code setup for multisets
haftmann
parents:
diff changeset
   166
  with finite_dom_map_of [of xs] have "finite ?A"
1559e9266280 optionalized very specific code setup for multisets
haftmann
parents:
diff changeset
   167
    by (auto intro: finite_subset)
1559e9266280 optionalized very specific code setup for multisets
haftmann
parents:
diff changeset
   168
  then show ?thesis
1559e9266280 optionalized very specific code setup for multisets
haftmann
parents:
diff changeset
   169
    by (simp add: count_of_def fun_eq_iff multiset_def)
1559e9266280 optionalized very specific code setup for multisets
haftmann
parents:
diff changeset
   170
qed
1559e9266280 optionalized very specific code setup for multisets
haftmann
parents:
diff changeset
   171
1559e9266280 optionalized very specific code setup for multisets
haftmann
parents:
diff changeset
   172
lemma count_simps [simp]:
1559e9266280 optionalized very specific code setup for multisets
haftmann
parents:
diff changeset
   173
  "count_of [] = (\<lambda>_. 0)"
1559e9266280 optionalized very specific code setup for multisets
haftmann
parents:
diff changeset
   174
  "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
   175
  by (simp_all add: count_of_def fun_eq_iff)
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_empty:
1559e9266280 optionalized very specific code setup for multisets
haftmann
parents:
diff changeset
   178
  "x \<notin> fst ` set xs \<Longrightarrow> count_of xs x = 0"
1559e9266280 optionalized very specific code setup for multisets
haftmann
parents:
diff changeset
   179
  by (induct xs) (simp_all add: count_of_def)
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_filter:
1559e9266280 optionalized very specific code setup for multisets
haftmann
parents:
diff changeset
   182
  "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
   183
  by (induct xs) auto
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
lemma count_of_map_default [simp]:
1559e9266280 optionalized very specific code setup for multisets
haftmann
parents:
diff changeset
   186
  "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
   187
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
   188
1559e9266280 optionalized very specific code setup for multisets
haftmann
parents:
diff changeset
   189
lemma count_of_join_raw:
1559e9266280 optionalized very specific code setup for multisets
haftmann
parents:
diff changeset
   190
  "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
   191
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
   192
1559e9266280 optionalized very specific code setup for multisets
haftmann
parents:
diff changeset
   193
lemma count_of_subtract_entries_raw:
1559e9266280 optionalized very specific code setup for multisets
haftmann
parents:
diff changeset
   194
  "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
   195
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
   196
1559e9266280 optionalized very specific code setup for multisets
haftmann
parents:
diff changeset
   197
1559e9266280 optionalized very specific code setup for multisets
haftmann
parents:
diff changeset
   198
text {* Code equations for multiset operations *}
1559e9266280 optionalized very specific code setup for multisets
haftmann
parents:
diff changeset
   199
1559e9266280 optionalized very specific code setup for multisets
haftmann
parents:
diff changeset
   200
definition Bag :: "('a, nat) alist \<Rightarrow> 'a multiset" where
1559e9266280 optionalized very specific code setup for multisets
haftmann
parents:
diff changeset
   201
  "Bag xs = Abs_multiset (count_of (DAList.impl_of xs))"
1559e9266280 optionalized very specific code setup for multisets
haftmann
parents:
diff changeset
   202
1559e9266280 optionalized very specific code setup for multisets
haftmann
parents:
diff changeset
   203
code_datatype Bag
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 count_Bag [simp, code]:
1559e9266280 optionalized very specific code setup for multisets
haftmann
parents:
diff changeset
   206
  "count (Bag xs) = count_of (DAList.impl_of xs)"
1559e9266280 optionalized very specific code setup for multisets
haftmann
parents:
diff changeset
   207
  by (simp add: Bag_def count_of_multiset Abs_multiset_inverse)
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 Mempty_Bag [code]:
1559e9266280 optionalized very specific code setup for multisets
haftmann
parents:
diff changeset
   210
  "{#} = Bag (DAList.empty)"
1559e9266280 optionalized very specific code setup for multisets
haftmann
parents:
diff changeset
   211
  by (simp add: multiset_eq_iff alist.Alist_inverse DAList.empty_def)
1559e9266280 optionalized very specific code setup for multisets
haftmann
parents:
diff changeset
   212
1559e9266280 optionalized very specific code setup for multisets
haftmann
parents:
diff changeset
   213
lemma single_Bag [code]:
1559e9266280 optionalized very specific code setup for multisets
haftmann
parents:
diff changeset
   214
  "{#x#} = Bag (DAList.update x 1 DAList.empty)"
1559e9266280 optionalized very specific code setup for multisets
haftmann
parents:
diff changeset
   215
  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
   216
1559e9266280 optionalized very specific code setup for multisets
haftmann
parents:
diff changeset
   217
lemma union_Bag [code]:
1559e9266280 optionalized very specific code setup for multisets
haftmann
parents:
diff changeset
   218
  "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
   219
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
   220
1559e9266280 optionalized very specific code setup for multisets
haftmann
parents:
diff changeset
   221
lemma minus_Bag [code]:
1559e9266280 optionalized very specific code setup for multisets
haftmann
parents:
diff changeset
   222
  "Bag xs - Bag ys = Bag (subtract_entries xs ys)"
1559e9266280 optionalized very specific code setup for multisets
haftmann
parents:
diff changeset
   223
by (rule multiset_eqI)
1559e9266280 optionalized very specific code setup for multisets
haftmann
parents:
diff changeset
   224
  (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
   225
1559e9266280 optionalized very specific code setup for multisets
haftmann
parents:
diff changeset
   226
lemma filter_Bag [code]:
1559e9266280 optionalized very specific code setup for multisets
haftmann
parents:
diff changeset
   227
  "Multiset.filter P (Bag xs) = Bag (DAList.filter (P \<circ> fst) xs)"
1559e9266280 optionalized very specific code setup for multisets
haftmann
parents:
diff changeset
   228
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
   229
55808
488c3e8282c8 added Rene Thiemann's patch for the nonterminating equality/subset test code for multisets
nipkow
parents: 51623
diff changeset
   230
488c3e8282c8 added Rene Thiemann's patch for the nonterminating equality/subset test code for multisets
nipkow
parents: 51623
diff changeset
   231
lemma mset_eq [code]: "HOL.equal (m1::'a::equal multiset) m2 \<longleftrightarrow> m1 \<le> m2 \<and> m2 \<le> m1"
488c3e8282c8 added Rene Thiemann's patch for the nonterminating equality/subset test code for multisets
nipkow
parents: 51623
diff changeset
   232
by (metis equal_multiset_def eq_iff)
488c3e8282c8 added Rene Thiemann's patch for the nonterminating equality/subset test code for multisets
nipkow
parents: 51623
diff changeset
   233
55887
25bd4745ee38 more code lemmas by Rene Thiemann
nipkow
parents: 55808
diff changeset
   234
text{* By default the code for @{text "<"} is @{prop"xs < ys \<longleftrightarrow> xs \<le> ys \<and> \<not> xs = ys"}.
25bd4745ee38 more code lemmas by Rene Thiemann
nipkow
parents: 55808
diff changeset
   235
With equality implemented by @{text"\<le>"}, this leads to three calls of  @{text"\<le>"}.
25bd4745ee38 more code lemmas by Rene Thiemann
nipkow
parents: 55808
diff changeset
   236
Here is a more efficient version: *}
25bd4745ee38 more code lemmas by Rene Thiemann
nipkow
parents: 55808
diff changeset
   237
lemma mset_less[code]: "xs < (ys :: 'a multiset) \<longleftrightarrow> xs \<le> ys \<and> \<not> ys \<le> xs" 
25bd4745ee38 more code lemmas by Rene Thiemann
nipkow
parents: 55808
diff changeset
   238
by (rule less_le_not_le)
25bd4745ee38 more code lemmas by Rene Thiemann
nipkow
parents: 55808
diff changeset
   239
25bd4745ee38 more code lemmas by Rene Thiemann
nipkow
parents: 55808
diff changeset
   240
lemma mset_less_eq_Bag0:
51599
1559e9266280 optionalized very specific code setup for multisets
haftmann
parents:
diff changeset
   241
  "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
   242
    (is "?lhs \<longleftrightarrow> ?rhs")
1559e9266280 optionalized very specific code setup for multisets
haftmann
parents:
diff changeset
   243
proof
55887
25bd4745ee38 more code lemmas by Rene Thiemann
nipkow
parents: 55808
diff changeset
   244
  assume ?lhs thus ?rhs by (auto simp add: mset_le_def)
51599
1559e9266280 optionalized very specific code setup for multisets
haftmann
parents:
diff changeset
   245
next
1559e9266280 optionalized very specific code setup for multisets
haftmann
parents:
diff changeset
   246
  assume ?rhs
1559e9266280 optionalized very specific code setup for multisets
haftmann
parents:
diff changeset
   247
  show ?lhs
1559e9266280 optionalized very specific code setup for multisets
haftmann
parents:
diff changeset
   248
  proof (rule mset_less_eqI)
1559e9266280 optionalized very specific code setup for multisets
haftmann
parents:
diff changeset
   249
    fix x
1559e9266280 optionalized very specific code setup for multisets
haftmann
parents:
diff changeset
   250
    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
   251
      by (cases "x \<in> fst ` set (DAList.impl_of xs)") (auto simp add: count_of_empty)
55887
25bd4745ee38 more code lemmas by Rene Thiemann
nipkow
parents: 55808
diff changeset
   252
    thus "count (Bag xs) x \<le> count A x" by (simp add: mset_le_def)
51599
1559e9266280 optionalized very specific code setup for multisets
haftmann
parents:
diff changeset
   253
  qed
1559e9266280 optionalized very specific code setup for multisets
haftmann
parents:
diff changeset
   254
qed
1559e9266280 optionalized very specific code setup for multisets
haftmann
parents:
diff changeset
   255
55887
25bd4745ee38 more code lemmas by Rene Thiemann
nipkow
parents: 55808
diff changeset
   256
lemma mset_less_eq_Bag [code]:
25bd4745ee38 more code lemmas by Rene Thiemann
nipkow
parents: 55808
diff changeset
   257
  "Bag xs \<le> (A :: 'a multiset) \<longleftrightarrow> (\<forall>(x, n) \<in> set (DAList.impl_of xs). n \<le> count A x)"
25bd4745ee38 more code lemmas by Rene Thiemann
nipkow
parents: 55808
diff changeset
   258
proof -
25bd4745ee38 more code lemmas by Rene Thiemann
nipkow
parents: 55808
diff changeset
   259
  {
25bd4745ee38 more code lemmas by Rene Thiemann
nipkow
parents: 55808
diff changeset
   260
    fix x n
25bd4745ee38 more code lemmas by Rene Thiemann
nipkow
parents: 55808
diff changeset
   261
    assume "(x,n) \<in> set (DAList.impl_of xs)"
25bd4745ee38 more code lemmas by Rene Thiemann
nipkow
parents: 55808
diff changeset
   262
    hence "count_of (DAList.impl_of xs) x = n" 
25bd4745ee38 more code lemmas by Rene Thiemann
nipkow
parents: 55808
diff changeset
   263
    proof (transfer)
25bd4745ee38 more code lemmas by Rene Thiemann
nipkow
parents: 55808
diff changeset
   264
      fix x n and xs :: "('a \<times> nat) list"
25bd4745ee38 more code lemmas by Rene Thiemann
nipkow
parents: 55808
diff changeset
   265
      show "(distinct \<circ> map fst) xs \<Longrightarrow> (x, n) \<in> set xs \<Longrightarrow> count_of xs x = n"
25bd4745ee38 more code lemmas by Rene Thiemann
nipkow
parents: 55808
diff changeset
   266
      proof (induct xs) 
25bd4745ee38 more code lemmas by Rene Thiemann
nipkow
parents: 55808
diff changeset
   267
        case (Cons ym ys)        
25bd4745ee38 more code lemmas by Rene Thiemann
nipkow
parents: 55808
diff changeset
   268
        obtain y m where ym: "ym = (y,m)" by force
25bd4745ee38 more code lemmas by Rene Thiemann
nipkow
parents: 55808
diff changeset
   269
        note Cons = Cons[unfolded ym]
25bd4745ee38 more code lemmas by Rene Thiemann
nipkow
parents: 55808
diff changeset
   270
        show ?case
25bd4745ee38 more code lemmas by Rene Thiemann
nipkow
parents: 55808
diff changeset
   271
        proof (cases "x = y")
25bd4745ee38 more code lemmas by Rene Thiemann
nipkow
parents: 55808
diff changeset
   272
          case False
25bd4745ee38 more code lemmas by Rene Thiemann
nipkow
parents: 55808
diff changeset
   273
          with Cons show ?thesis unfolding ym by auto
25bd4745ee38 more code lemmas by Rene Thiemann
nipkow
parents: 55808
diff changeset
   274
        next
25bd4745ee38 more code lemmas by Rene Thiemann
nipkow
parents: 55808
diff changeset
   275
          case True
25bd4745ee38 more code lemmas by Rene Thiemann
nipkow
parents: 55808
diff changeset
   276
          with Cons(2-3) have "m = n" by force
25bd4745ee38 more code lemmas by Rene Thiemann
nipkow
parents: 55808
diff changeset
   277
          with True show ?thesis unfolding ym by auto
25bd4745ee38 more code lemmas by Rene Thiemann
nipkow
parents: 55808
diff changeset
   278
        qed
25bd4745ee38 more code lemmas by Rene Thiemann
nipkow
parents: 55808
diff changeset
   279
      qed auto
25bd4745ee38 more code lemmas by Rene Thiemann
nipkow
parents: 55808
diff changeset
   280
    qed
25bd4745ee38 more code lemmas by Rene Thiemann
nipkow
parents: 55808
diff changeset
   281
  }
25bd4745ee38 more code lemmas by Rene Thiemann
nipkow
parents: 55808
diff changeset
   282
  thus ?thesis unfolding mset_less_eq_Bag0 by auto
25bd4745ee38 more code lemmas by Rene Thiemann
nipkow
parents: 55808
diff changeset
   283
qed
25bd4745ee38 more code lemmas by Rene Thiemann
nipkow
parents: 55808
diff changeset
   284
51600
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
   285
declare multiset_inter_def [code]
51623
1194b438426a sup on multisets
haftmann
parents: 51600
diff changeset
   286
declare sup_multiset_def [code]
1194b438426a sup on multisets
haftmann
parents: 51600
diff changeset
   287
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
   288
55887
25bd4745ee38 more code lemmas by Rene Thiemann
nipkow
parents: 55808
diff changeset
   289
25bd4745ee38 more code lemmas by Rene Thiemann
nipkow
parents: 55808
diff changeset
   290
fun fold_impl :: "('a \<Rightarrow> nat \<Rightarrow> 'b \<Rightarrow> 'b) \<Rightarrow> 'b \<Rightarrow> ('a \<times> nat)list \<Rightarrow> 'b" where
25bd4745ee38 more code lemmas by Rene Thiemann
nipkow
parents: 55808
diff changeset
   291
  "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
   292
| "fold_impl fn e [] = e"
25bd4745ee38 more code lemmas by Rene Thiemann
nipkow
parents: 55808
diff changeset
   293
25bd4745ee38 more code lemmas by Rene Thiemann
nipkow
parents: 55808
diff changeset
   294
definition fold :: "('a \<Rightarrow> nat \<Rightarrow> 'b \<Rightarrow> 'b) \<Rightarrow> 'b \<Rightarrow> ('a, nat)alist \<Rightarrow> 'b" where
25bd4745ee38 more code lemmas by Rene Thiemann
nipkow
parents: 55808
diff changeset
   295
"fold f e al = fold_impl f e (DAList.impl_of al)"
25bd4745ee38 more code lemmas by Rene Thiemann
nipkow
parents: 55808
diff changeset
   296
25bd4745ee38 more code lemmas by Rene Thiemann
nipkow
parents: 55808
diff changeset
   297
hide_const (open) fold
25bd4745ee38 more code lemmas by Rene Thiemann
nipkow
parents: 55808
diff changeset
   298
25bd4745ee38 more code lemmas by Rene Thiemann
nipkow
parents: 55808
diff changeset
   299
context comp_fun_commute
25bd4745ee38 more code lemmas by Rene Thiemann
nipkow
parents: 55808
diff changeset
   300
begin
25bd4745ee38 more code lemmas by Rene Thiemann
nipkow
parents: 55808
diff changeset
   301
25bd4745ee38 more code lemmas by Rene Thiemann
nipkow
parents: 55808
diff changeset
   302
lemma DAList_Multiset_fold: assumes fn: "\<And> a n x. fn a n x = (f a ^^ n) x"
25bd4745ee38 more code lemmas by Rene Thiemann
nipkow
parents: 55808
diff changeset
   303
  shows "Multiset.fold f e (Bag al) = DAList_Multiset.fold fn e al"
25bd4745ee38 more code lemmas by Rene Thiemann
nipkow
parents: 55808
diff changeset
   304
unfolding DAList_Multiset.fold_def
25bd4745ee38 more code lemmas by Rene Thiemann
nipkow
parents: 55808
diff changeset
   305
proof (induct al)
25bd4745ee38 more code lemmas by Rene Thiemann
nipkow
parents: 55808
diff changeset
   306
  fix ys
25bd4745ee38 more code lemmas by Rene Thiemann
nipkow
parents: 55808
diff changeset
   307
  let ?inv = "{xs :: ('a \<times> nat)list. (distinct \<circ> map fst) xs}"
25bd4745ee38 more code lemmas by Rene Thiemann
nipkow
parents: 55808
diff changeset
   308
  note cs[simp del] = count_simps
25bd4745ee38 more code lemmas by Rene Thiemann
nipkow
parents: 55808
diff changeset
   309
  have count[simp]: "\<And> x. count (Abs_multiset (count_of x)) = count_of x"
25bd4745ee38 more code lemmas by Rene Thiemann
nipkow
parents: 55808
diff changeset
   310
    by (rule Abs_multiset_inverse[OF count_of_multiset])
25bd4745ee38 more code lemmas by Rene Thiemann
nipkow
parents: 55808
diff changeset
   311
  assume ys: "ys \<in> ?inv"
25bd4745ee38 more code lemmas by Rene Thiemann
nipkow
parents: 55808
diff changeset
   312
  thus "Multiset.fold f e (Bag (Alist ys)) = fold_impl fn e (DAList.impl_of (Alist ys))"
25bd4745ee38 more code lemmas by Rene Thiemann
nipkow
parents: 55808
diff changeset
   313
    unfolding Bag_def unfolding Alist_inverse[OF ys]
25bd4745ee38 more code lemmas by Rene Thiemann
nipkow
parents: 55808
diff changeset
   314
  proof (induct ys arbitrary: e rule: list.induct)
25bd4745ee38 more code lemmas by Rene Thiemann
nipkow
parents: 55808
diff changeset
   315
    case Nil
25bd4745ee38 more code lemmas by Rene Thiemann
nipkow
parents: 55808
diff changeset
   316
    show ?case
25bd4745ee38 more code lemmas by Rene Thiemann
nipkow
parents: 55808
diff changeset
   317
      by (rule trans[OF arg_cong[of _ "{#}" "Multiset.fold f e", OF multiset_eqI]])
25bd4745ee38 more code lemmas by Rene Thiemann
nipkow
parents: 55808
diff changeset
   318
         (auto, simp add: cs)
25bd4745ee38 more code lemmas by Rene Thiemann
nipkow
parents: 55808
diff changeset
   319
  next
25bd4745ee38 more code lemmas by Rene Thiemann
nipkow
parents: 55808
diff changeset
   320
    case (Cons pair ys e)
25bd4745ee38 more code lemmas by Rene Thiemann
nipkow
parents: 55808
diff changeset
   321
    obtain a n where pair: "pair = (a,n)" by force
25bd4745ee38 more code lemmas by Rene Thiemann
nipkow
parents: 55808
diff changeset
   322
    from fn[of a n] have [simp]: "fn a n = (f a ^^ n)" by auto
25bd4745ee38 more code lemmas by Rene Thiemann
nipkow
parents: 55808
diff changeset
   323
    have inv: "ys \<in> ?inv" using Cons(2) by auto
25bd4745ee38 more code lemmas by Rene Thiemann
nipkow
parents: 55808
diff changeset
   324
    note IH = Cons(1)[OF inv]
25bd4745ee38 more code lemmas by Rene Thiemann
nipkow
parents: 55808
diff changeset
   325
    def Ys \<equiv> "Abs_multiset (count_of ys)"
25bd4745ee38 more code lemmas by Rene Thiemann
nipkow
parents: 55808
diff changeset
   326
    have id: "Abs_multiset (count_of ((a, n) # ys)) = ((op + {# a #}) ^^ n) Ys"
25bd4745ee38 more code lemmas by Rene Thiemann
nipkow
parents: 55808
diff changeset
   327
      unfolding Ys_def
25bd4745ee38 more code lemmas by Rene Thiemann
nipkow
parents: 55808
diff changeset
   328
    proof (rule multiset_eqI, unfold count)
25bd4745ee38 more code lemmas by Rene Thiemann
nipkow
parents: 55808
diff changeset
   329
      fix c      
25bd4745ee38 more code lemmas by Rene Thiemann
nipkow
parents: 55808
diff changeset
   330
      show "count_of ((a, n) # ys) c = count ((op + {#a#} ^^ n) (Abs_multiset (count_of ys))) c" (is "?l = ?r")
25bd4745ee38 more code lemmas by Rene Thiemann
nipkow
parents: 55808
diff changeset
   331
      proof (cases "c = a")
25bd4745ee38 more code lemmas by Rene Thiemann
nipkow
parents: 55808
diff changeset
   332
        case False thus ?thesis unfolding cs by (induct n) auto
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
25bd4745ee38 more code lemmas by Rene Thiemann
nipkow
parents: 55808
diff changeset
   335
        hence "?l = n" by (simp add: cs)
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
25bd4745ee38 more code lemmas by Rene Thiemann
nipkow
parents: 55808
diff changeset
   340
          thus ?case by (induct ys) (simp, auto simp: cs)
25bd4745ee38 more code lemmas by Rene Thiemann
nipkow
parents: 55808
diff changeset
   341
        qed auto
25bd4745ee38 more code lemmas by Rene Thiemann
nipkow
parents: 55808
diff changeset
   342
        finally show ?thesis .
25bd4745ee38 more code lemmas by Rene Thiemann
nipkow
parents: 55808
diff changeset
   343
      qed
25bd4745ee38 more code lemmas by Rene Thiemann
nipkow
parents: 55808
diff changeset
   344
    qed
25bd4745ee38 more code lemmas by Rene Thiemann
nipkow
parents: 55808
diff changeset
   345
    show ?case unfolding pair    
25bd4745ee38 more code lemmas by Rene Thiemann
nipkow
parents: 55808
diff changeset
   346
      by (simp add: IH[symmetric], unfold id Ys_def[symmetric],
25bd4745ee38 more code lemmas by Rene Thiemann
nipkow
parents: 55808
diff changeset
   347
      induct n, auto simp: fold_mset_fun_left_comm[symmetric])
25bd4745ee38 more code lemmas by Rene Thiemann
nipkow
parents: 55808
diff changeset
   348
  qed
25bd4745ee38 more code lemmas by Rene Thiemann
nipkow
parents: 55808
diff changeset
   349
qed
25bd4745ee38 more code lemmas by Rene Thiemann
nipkow
parents: 55808
diff changeset
   350
25bd4745ee38 more code lemmas by Rene Thiemann
nipkow
parents: 55808
diff changeset
   351
end 
25bd4745ee38 more code lemmas by Rene Thiemann
nipkow
parents: 55808
diff changeset
   352
25bd4745ee38 more code lemmas by Rene Thiemann
nipkow
parents: 55808
diff changeset
   353
lift_definition single_alist_entry :: "'a \<Rightarrow> 'b \<Rightarrow> ('a,'b)alist" is "\<lambda> a b. [(a,b)]" by auto
25bd4745ee38 more code lemmas by Rene Thiemann
nipkow
parents: 55808
diff changeset
   354
25bd4745ee38 more code lemmas by Rene Thiemann
nipkow
parents: 55808
diff changeset
   355
lemma image_mset_Bag[code]:
25bd4745ee38 more code lemmas by Rene Thiemann
nipkow
parents: 55808
diff changeset
   356
  "image_mset f (Bag ms) =
25bd4745ee38 more code lemmas by Rene Thiemann
nipkow
parents: 55808
diff changeset
   357
   DAList_Multiset.fold (\<lambda> a n m. Bag (single_alist_entry (f a) n) + m) {#} ms"
25bd4745ee38 more code lemmas by Rene Thiemann
nipkow
parents: 55808
diff changeset
   358
unfolding image_mset_def 
25bd4745ee38 more code lemmas by Rene Thiemann
nipkow
parents: 55808
diff changeset
   359
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
   360
  fix a n m
25bd4745ee38 more code lemmas by Rene Thiemann
nipkow
parents: 55808
diff changeset
   361
  show "Bag (single_alist_entry (f a) n) + m = ((op + \<circ> single \<circ> f) a ^^ n) m" (is "?l = ?r")
25bd4745ee38 more code lemmas by Rene Thiemann
nipkow
parents: 55808
diff changeset
   362
  proof (rule multiset_eqI)
25bd4745ee38 more code lemmas by Rene Thiemann
nipkow
parents: 55808
diff changeset
   363
    fix x
25bd4745ee38 more code lemmas by Rene Thiemann
nipkow
parents: 55808
diff changeset
   364
    have "count ?r x = (if x = f a then n + count m x else count m x)"
25bd4745ee38 more code lemmas by Rene Thiemann
nipkow
parents: 55808
diff changeset
   365
      by (induct n, auto)
25bd4745ee38 more code lemmas by Rene Thiemann
nipkow
parents: 55808
diff changeset
   366
    also have "\<dots> = count ?l x" by (simp add: single_alist_entry.rep_eq)
25bd4745ee38 more code lemmas by Rene Thiemann
nipkow
parents: 55808
diff changeset
   367
    finally show "count ?l x = count ?r x" ..
25bd4745ee38 more code lemmas by Rene Thiemann
nipkow
parents: 55808
diff changeset
   368
  qed
25bd4745ee38 more code lemmas by Rene Thiemann
nipkow
parents: 55808
diff changeset
   369
qed
25bd4745ee38 more code lemmas by Rene Thiemann
nipkow
parents: 55808
diff changeset
   370
25bd4745ee38 more code lemmas by Rene Thiemann
nipkow
parents: 55808
diff changeset
   371
hide_const single_alist_entry
25bd4745ee38 more code lemmas by Rene Thiemann
nipkow
parents: 55808
diff changeset
   372
25bd4745ee38 more code lemmas by Rene Thiemann
nipkow
parents: 55808
diff changeset
   373
(* we cannot use (\<lambda> a n. op + (a * n)) for folding, since * is not defined
25bd4745ee38 more code lemmas by Rene Thiemann
nipkow
parents: 55808
diff changeset
   374
   in comm_monoid_add *)
25bd4745ee38 more code lemmas by Rene Thiemann
nipkow
parents: 55808
diff changeset
   375
lemma msetsum_Bag[code]:
25bd4745ee38 more code lemmas by Rene Thiemann
nipkow
parents: 55808
diff changeset
   376
  "msetsum (Bag ms) = DAList_Multiset.fold (\<lambda> a n. ((op + a) ^^ n)) 0 ms"
25bd4745ee38 more code lemmas by Rene Thiemann
nipkow
parents: 55808
diff changeset
   377
unfolding msetsum.eq_fold
25bd4745ee38 more code lemmas by Rene Thiemann
nipkow
parents: 55808
diff changeset
   378
by (rule comp_fun_commute.DAList_Multiset_fold, unfold_locales, auto simp: ac_simps)
25bd4745ee38 more code lemmas by Rene Thiemann
nipkow
parents: 55808
diff changeset
   379
25bd4745ee38 more code lemmas by Rene Thiemann
nipkow
parents: 55808
diff changeset
   380
(* we cannot use (\<lambda> a n. op * (a ^ n)) for folding, since ^ is not defined
25bd4745ee38 more code lemmas by Rene Thiemann
nipkow
parents: 55808
diff changeset
   381
   in comm_monoid_mult *)
25bd4745ee38 more code lemmas by Rene Thiemann
nipkow
parents: 55808
diff changeset
   382
lemma msetprod_Bag[code]:
25bd4745ee38 more code lemmas by Rene Thiemann
nipkow
parents: 55808
diff changeset
   383
  "msetprod (Bag ms) = DAList_Multiset.fold (\<lambda> a n. ((op * a) ^^ n)) 1 ms"
25bd4745ee38 more code lemmas by Rene Thiemann
nipkow
parents: 55808
diff changeset
   384
unfolding msetprod.eq_fold
25bd4745ee38 more code lemmas by Rene Thiemann
nipkow
parents: 55808
diff changeset
   385
by (rule comp_fun_commute.DAList_Multiset_fold, unfold_locales, auto simp: ac_simps)
25bd4745ee38 more code lemmas by Rene Thiemann
nipkow
parents: 55808
diff changeset
   386
25bd4745ee38 more code lemmas by Rene Thiemann
nipkow
parents: 55808
diff changeset
   387
lemma mcard_fold: "mcard A = Multiset.fold (\<lambda> _. Suc) 0 A" (is "_ = Multiset.fold ?f _ _")
25bd4745ee38 more code lemmas by Rene Thiemann
nipkow
parents: 55808
diff changeset
   388
proof -
25bd4745ee38 more code lemmas by Rene Thiemann
nipkow
parents: 55808
diff changeset
   389
  interpret comp_fun_commute ?f by (default, auto)
25bd4745ee38 more code lemmas by Rene Thiemann
nipkow
parents: 55808
diff changeset
   390
  show ?thesis by (induct A) auto
25bd4745ee38 more code lemmas by Rene Thiemann
nipkow
parents: 55808
diff changeset
   391
qed
25bd4745ee38 more code lemmas by Rene Thiemann
nipkow
parents: 55808
diff changeset
   392
25bd4745ee38 more code lemmas by Rene Thiemann
nipkow
parents: 55808
diff changeset
   393
lemma mcard_Bag[code]:
25bd4745ee38 more code lemmas by Rene Thiemann
nipkow
parents: 55808
diff changeset
   394
  "mcard (Bag ms) = DAList_Multiset.fold (\<lambda> a n. op + n) 0 ms"
25bd4745ee38 more code lemmas by Rene Thiemann
nipkow
parents: 55808
diff changeset
   395
unfolding mcard_fold
25bd4745ee38 more code lemmas by Rene Thiemann
nipkow
parents: 55808
diff changeset
   396
proof (rule comp_fun_commute.DAList_Multiset_fold, unfold_locales, simp)
25bd4745ee38 more code lemmas by Rene Thiemann
nipkow
parents: 55808
diff changeset
   397
  fix a n x
25bd4745ee38 more code lemmas by Rene Thiemann
nipkow
parents: 55808
diff changeset
   398
  show "n + x = (Suc ^^ n) x" by (induct n) auto
25bd4745ee38 more code lemmas by Rene Thiemann
nipkow
parents: 55808
diff changeset
   399
qed
25bd4745ee38 more code lemmas by Rene Thiemann
nipkow
parents: 55808
diff changeset
   400
25bd4745ee38 more code lemmas by Rene Thiemann
nipkow
parents: 55808
diff changeset
   401
25bd4745ee38 more code lemmas by Rene Thiemann
nipkow
parents: 55808
diff changeset
   402
lemma set_of_fold: "set_of A = Multiset.fold insert {} A" (is "_ = Multiset.fold ?f _ _")
25bd4745ee38 more code lemmas by Rene Thiemann
nipkow
parents: 55808
diff changeset
   403
proof -
25bd4745ee38 more code lemmas by Rene Thiemann
nipkow
parents: 55808
diff changeset
   404
  interpret comp_fun_commute ?f by (default, auto)
25bd4745ee38 more code lemmas by Rene Thiemann
nipkow
parents: 55808
diff changeset
   405
  show ?thesis by (induct A, auto)
25bd4745ee38 more code lemmas by Rene Thiemann
nipkow
parents: 55808
diff changeset
   406
qed
25bd4745ee38 more code lemmas by Rene Thiemann
nipkow
parents: 55808
diff changeset
   407
25bd4745ee38 more code lemmas by Rene Thiemann
nipkow
parents: 55808
diff changeset
   408
lemma set_of_Bag[code]:
25bd4745ee38 more code lemmas by Rene Thiemann
nipkow
parents: 55808
diff changeset
   409
  "set_of (Bag ms) = DAList_Multiset.fold (\<lambda> a n. (if n = 0 then (\<lambda> m. m) else insert a)) {} ms"
25bd4745ee38 more code lemmas by Rene Thiemann
nipkow
parents: 55808
diff changeset
   410
unfolding set_of_fold
25bd4745ee38 more code lemmas by Rene Thiemann
nipkow
parents: 55808
diff changeset
   411
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
   412
  fix a n x
25bd4745ee38 more code lemmas by Rene Thiemann
nipkow
parents: 55808
diff changeset
   413
  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
   414
  proof (cases n)
25bd4745ee38 more code lemmas by Rene Thiemann
nipkow
parents: 55808
diff changeset
   415
    case (Suc m)
25bd4745ee38 more code lemmas by Rene Thiemann
nipkow
parents: 55808
diff changeset
   416
    hence "?l n = insert a x" by simp
25bd4745ee38 more code lemmas by Rene Thiemann
nipkow
parents: 55808
diff changeset
   417
    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
   418
    ultimately show ?thesis by auto
25bd4745ee38 more code lemmas by Rene Thiemann
nipkow
parents: 55808
diff changeset
   419
  qed auto
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
25bd4745ee38 more code lemmas by Rene Thiemann
nipkow
parents: 55808
diff changeset
   422
51600
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
   423
instantiation multiset :: (exhaustive) exhaustive
51599
1559e9266280 optionalized very specific code setup for multisets
haftmann
parents:
diff changeset
   424
begin
1559e9266280 optionalized very specific code setup for multisets
haftmann
parents:
diff changeset
   425
51600
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
   426
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
   427
where
197e25f13f0c default implementation of multisets by list with reasonable coverage of operations on multisets
haftmann
parents: 51599
diff changeset
   428
  "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
   429
1559e9266280 optionalized very specific code setup for multisets
haftmann
parents:
diff changeset
   430
instance ..
1559e9266280 optionalized very specific code setup for multisets
haftmann
parents:
diff changeset
   431
1559e9266280 optionalized very specific code setup for multisets
haftmann
parents:
diff changeset
   432
end
1559e9266280 optionalized very specific code setup for multisets
haftmann
parents:
diff changeset
   433
1559e9266280 optionalized very specific code setup for multisets
haftmann
parents:
diff changeset
   434
end
1559e9266280 optionalized very specific code setup for multisets
haftmann
parents:
diff changeset
   435