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