src/HOL/Library/Multiset.thy
changeset 49822 0cfc1651be25
parent 49717 56494eedf493
child 49823 1c146fa7701e
     1.1 --- a/src/HOL/Library/Multiset.thy	Thu Oct 11 00:13:21 2012 +0200
     1.2 +++ b/src/HOL/Library/Multiset.thy	Thu Oct 11 11:56:42 2012 +0200
     1.3 @@ -657,146 +657,82 @@
     1.4  
     1.5  subsection {* The fold combinator *}
     1.6  
     1.7 -text {*
     1.8 -  The intended behaviour is
     1.9 -  @{text "fold_mset f z {#x\<^isub>1, ..., x\<^isub>n#} = f x\<^isub>1 (\<dots> (f x\<^isub>n z)\<dots>)"}
    1.10 -  if @{text f} is associative-commutative. 
    1.11 -*}
    1.12 -
    1.13 -text {*
    1.14 -  The graph of @{text "fold_mset"}, @{text "z"}: the start element,
    1.15 -  @{text "f"}: folding function, @{text "A"}: the multiset, @{text
    1.16 -  "y"}: the result.
    1.17 -*}
    1.18 -inductive 
    1.19 -  fold_msetG :: "('a \<Rightarrow> 'b \<Rightarrow> 'b) \<Rightarrow> 'b \<Rightarrow> 'a multiset \<Rightarrow> 'b \<Rightarrow> bool" 
    1.20 -  for f :: "'a \<Rightarrow> 'b \<Rightarrow> 'b" 
    1.21 -  and z :: 'b
    1.22 +definition fold :: "('a \<Rightarrow> 'b \<Rightarrow> 'b) \<Rightarrow> 'b \<Rightarrow> 'a multiset \<Rightarrow> 'b"
    1.23  where
    1.24 -  emptyI [intro]:  "fold_msetG f z {#} z"
    1.25 -| insertI [intro]: "fold_msetG f z A y \<Longrightarrow> fold_msetG f z (A + {#x#}) (f x y)"
    1.26 +  "fold f s M = Finite_Set.fold (\<lambda>x. f x ^^ count M x) s (set_of M)"
    1.27  
    1.28 -inductive_cases empty_fold_msetGE [elim!]: "fold_msetG f z {#} x"
    1.29 -inductive_cases insert_fold_msetGE: "fold_msetG f z (A + {#}) y" 
    1.30 -
    1.31 -definition
    1.32 -  fold_mset :: "('a \<Rightarrow> 'b \<Rightarrow> 'b) \<Rightarrow> 'b \<Rightarrow> 'a multiset \<Rightarrow> 'b" where
    1.33 -  "fold_mset f z A = (THE x. fold_msetG f z A x)"
    1.34 -
    1.35 -lemma Diff1_fold_msetG:
    1.36 -  "fold_msetG f z (A - {#x#}) y \<Longrightarrow> x \<in># A \<Longrightarrow> fold_msetG f z A (f x y)"
    1.37 -apply (frule_tac x = x in fold_msetG.insertI)
    1.38 -apply auto
    1.39 -done
    1.40 -
    1.41 -lemma fold_msetG_nonempty: "\<exists>x. fold_msetG f z A x"
    1.42 -apply (induct A)
    1.43 - apply blast
    1.44 -apply clarsimp
    1.45 -apply (drule_tac x = x in fold_msetG.insertI)
    1.46 -apply auto
    1.47 -done
    1.48 -
    1.49 -lemma fold_mset_empty[simp]: "fold_mset f z {#} = z"
    1.50 -unfolding fold_mset_def by blast
    1.51 +lemma fold_mset_empty [simp]:
    1.52 +  "fold f s {#} = s"
    1.53 +  by (simp add: fold_def)
    1.54  
    1.55  context comp_fun_commute
    1.56  begin
    1.57  
    1.58 -lemma fold_msetG_insertE_aux:
    1.59 -  "fold_msetG f z A y \<Longrightarrow> a \<in># A \<Longrightarrow> \<exists>y'. y = f a y' \<and> fold_msetG f z (A - {#a#}) y'"
    1.60 -proof (induct set: fold_msetG)
    1.61 -  case (insertI A y x) show ?case
    1.62 -  proof (cases "x = a")
    1.63 -    assume "x = a" with insertI show ?case by auto
    1.64 +lemma fold_mset_insert:
    1.65 +  "fold f s (M + {#x#}) = f x (fold f s M)"
    1.66 +proof -
    1.67 +  interpret mset: comp_fun_commute "\<lambda>y. f y ^^ count M y"
    1.68 +    by (fact comp_fun_commute_funpow)
    1.69 +  interpret mset_union: comp_fun_commute "\<lambda>y. f y ^^ count (M + {#x#}) y"
    1.70 +    by (fact comp_fun_commute_funpow)
    1.71 +  show ?thesis
    1.72 +  proof (cases "x \<in> set_of M")
    1.73 +    case False
    1.74 +    then have *: "count (M + {#x#}) x = 1" by simp
    1.75 +    from False have "Finite_Set.fold (\<lambda>y. f y ^^ count (M + {#x#}) y) s (set_of M) =
    1.76 +      Finite_Set.fold (\<lambda>y. f y ^^ count M y) s (set_of M)"
    1.77 +      by (auto intro!: Finite_Set.fold_cong comp_fun_commute_funpow)
    1.78 +    with False * show ?thesis
    1.79 +      by (simp add: fold_def del: count_union)
    1.80    next
    1.81 -    assume "x \<noteq> a"
    1.82 -    then obtain y' where y: "y = f a y'" and y': "fold_msetG f z (A - {#a#}) y'"
    1.83 -      using insertI by auto
    1.84 -    have "f x y = f a (f x y')"
    1.85 -      unfolding y by (rule fun_left_comm)
    1.86 -    moreover have "fold_msetG f z (A + {#x#} - {#a#}) (f x y')"
    1.87 -      using y' and `x \<noteq> a`
    1.88 -      by (simp add: diff_union_swap [symmetric] fold_msetG.insertI)
    1.89 -    ultimately show ?case by fast
    1.90 +    case True
    1.91 +    def N \<equiv> "set_of M - {x}"
    1.92 +    from N_def True have *: "set_of M = insert x N" "x \<notin> N" "finite N" by auto
    1.93 +    then have "Finite_Set.fold (\<lambda>y. f y ^^ count (M + {#x#}) y) s N =
    1.94 +      Finite_Set.fold (\<lambda>y. f y ^^ count M y) s N"
    1.95 +      by (auto intro!: Finite_Set.fold_cong comp_fun_commute_funpow)
    1.96 +    with * show ?thesis by (simp add: fold_def del: count_union) simp
    1.97    qed
    1.98 -qed simp
    1.99 -
   1.100 -lemma fold_msetG_insertE:
   1.101 -  assumes "fold_msetG f z (A + {#x#}) v"
   1.102 -  obtains y where "v = f x y" and "fold_msetG f z A y"
   1.103 -using assms by (auto dest: fold_msetG_insertE_aux [where a=x])
   1.104 -
   1.105 -lemma fold_msetG_determ:
   1.106 -  "fold_msetG f z A x \<Longrightarrow> fold_msetG f z A y \<Longrightarrow> y = x"
   1.107 -proof (induct arbitrary: y set: fold_msetG)
   1.108 -  case (insertI A y x v)
   1.109 -  from `fold_msetG f z (A + {#x#}) v`
   1.110 -  obtain y' where "v = f x y'" and "fold_msetG f z A y'"
   1.111 -    by (rule fold_msetG_insertE)
   1.112 -  from `fold_msetG f z A y'` have "y' = y" by (rule insertI)
   1.113 -  with `v = f x y'` show "v = f x y" by simp
   1.114 -qed fast
   1.115 -
   1.116 -lemma fold_mset_equality: "fold_msetG f z A y \<Longrightarrow> fold_mset f z A = y"
   1.117 -unfolding fold_mset_def by (blast intro: fold_msetG_determ)
   1.118 -
   1.119 -lemma fold_msetG_fold_mset: "fold_msetG f z A (fold_mset f z A)"
   1.120 -proof -
   1.121 -  from fold_msetG_nonempty fold_msetG_determ
   1.122 -  have "\<exists>!x. fold_msetG f z A x" by (rule ex_ex1I)
   1.123 -  then show ?thesis unfolding fold_mset_def by (rule theI')
   1.124  qed
   1.125  
   1.126 -lemma fold_mset_insert:
   1.127 -  "fold_mset f z (A + {#x#}) = f x (fold_mset f z A)"
   1.128 -by (intro fold_mset_equality fold_msetG.insertI fold_msetG_fold_mset)
   1.129 +corollary fold_mset_single [simp]:
   1.130 +  "fold f s {#x#} = f x s"
   1.131 +proof -
   1.132 +  have "fold f s ({#} + {#x#}) = f x s" by (simp only: fold_mset_insert) simp
   1.133 +  then show ?thesis by simp
   1.134 +qed
   1.135  
   1.136 -lemma fold_mset_commute: "f x (fold_mset f z A) = fold_mset f (f x z) A"
   1.137 -by (induct A) (auto simp: fold_mset_insert fun_left_comm [of x])
   1.138 -
   1.139 -lemma fold_mset_single [simp]: "fold_mset f z {#x#} = f x z"
   1.140 -using fold_mset_insert [of z "{#}"] by simp
   1.141 +lemma fold_mset_fun_comm:
   1.142 +  "f x (fold f s M) = fold f (f x s) M"
   1.143 +  by (induct M) (simp_all add: fold_mset_insert fun_left_comm)
   1.144  
   1.145  lemma fold_mset_union [simp]:
   1.146 -  "fold_mset f z (A+B) = fold_mset f (fold_mset f z A) B"
   1.147 -proof (induct A)
   1.148 +  "fold f s (M + N) = fold f (fold f s M) N"
   1.149 +proof (induct M)
   1.150    case empty then show ?case by simp
   1.151  next
   1.152 -  case (add A x)
   1.153 -  have "A + {#x#} + B = (A+B) + {#x#}" by (simp add: add_ac)
   1.154 -  then have "fold_mset f z (A + {#x#} + B) = f x (fold_mset f z (A + B))" 
   1.155 -    by (simp add: fold_mset_insert)
   1.156 -  also have "\<dots> = fold_mset f (fold_mset f z (A + {#x#})) B"
   1.157 -    by (simp add: fold_mset_commute[of x,symmetric] add fold_mset_insert)
   1.158 -  finally show ?case .
   1.159 +  case (add M x)
   1.160 +  have "M + {#x#} + N = (M + N) + {#x#}"
   1.161 +    by (simp add: add_ac)
   1.162 +  with add show ?case by (simp add: fold_mset_insert fold_mset_fun_comm)
   1.163  qed
   1.164  
   1.165  lemma fold_mset_fusion:
   1.166    assumes "comp_fun_commute g"
   1.167 -  shows "(\<And>x y. h (g x y) = f x (h y)) \<Longrightarrow> h (fold_mset g w A) = fold_mset f (h w) A" (is "PROP ?P")
   1.168 +  shows "(\<And>x y. h (g x y) = f x (h y)) \<Longrightarrow> h (fold g w A) = fold f (h w) A" (is "PROP ?P")
   1.169  proof -
   1.170    interpret comp_fun_commute g by (fact assms)
   1.171    show "PROP ?P" by (induct A) auto
   1.172  qed
   1.173  
   1.174 -lemma fold_mset_rec:
   1.175 -  assumes "a \<in># A" 
   1.176 -  shows "fold_mset f z A = f a (fold_mset f z (A - {#a#}))"
   1.177 -proof -
   1.178 -  from assms obtain A' where "A = A' + {#a#}"
   1.179 -    by (blast dest: multi_member_split)
   1.180 -  then show ?thesis by simp
   1.181 -qed
   1.182 -
   1.183  end
   1.184  
   1.185  text {*
   1.186    A note on code generation: When defining some function containing a
   1.187 -  subterm @{term"fold_mset F"}, code generation is not automatic. When
   1.188 +  subterm @{term "fold F"}, code generation is not automatic. When
   1.189    interpreting locale @{text left_commutative} with @{text F}, the
   1.190 -  would be code thms for @{const fold_mset} become thms like
   1.191 -  @{term"fold_mset F z {#} = z"} where @{text F} is not a pattern but
   1.192 +  would be code thms for @{const fold} become thms like
   1.193 +  @{term "fold F z {#} = z"} where @{text F} is not a pattern but
   1.194    contains defined symbols, i.e.\ is not a code thm. Hence a separate
   1.195    constant with its own code thms needs to be introduced for @{text
   1.196    F}. See the image operator below.
   1.197 @@ -806,7 +742,7 @@
   1.198  subsection {* Image *}
   1.199  
   1.200  definition image_mset :: "('a \<Rightarrow> 'b) \<Rightarrow> 'a multiset \<Rightarrow> 'b multiset" where
   1.201 -  "image_mset f = fold_mset (op + o single o f) {#}"
   1.202 +  "image_mset f = fold (plus o single o f) {#}"
   1.203  
   1.204  interpretation image_fun_commute: comp_fun_commute "op + o single o f" for f
   1.205  proof qed (simp add: add_ac fun_eq_iff)
   1.206 @@ -989,7 +925,7 @@
   1.207  lemma fold_multiset_equiv:
   1.208    assumes f: "\<And>x y. x \<in> set xs \<Longrightarrow> y \<in> set xs \<Longrightarrow> f x \<circ> f y = f y \<circ> f x"
   1.209      and equiv: "multiset_of xs = multiset_of ys"
   1.210 -  shows "fold f xs = fold f ys"
   1.211 +  shows "List.fold f xs = List.fold f ys"
   1.212  using f equiv [symmetric]
   1.213  proof (induct xs arbitrary: ys)
   1.214    case Nil then show ?case by simp
   1.215 @@ -999,8 +935,8 @@
   1.216    have "\<And>x y. x \<in> set ys \<Longrightarrow> y \<in> set ys \<Longrightarrow> f x \<circ> f y = f y \<circ> f x" 
   1.217      by (rule Cons.prems(1)) (simp_all add: *)
   1.218    moreover from * have "x \<in> set ys" by simp
   1.219 -  ultimately have "fold f ys = fold f (remove1 x ys) \<circ> f x" by (fact fold_remove1_split)
   1.220 -  moreover from Cons.prems have "fold f xs = fold f (remove1 x ys)" by (auto intro: Cons.hyps)
   1.221 +  ultimately have "List.fold f ys = List.fold f (remove1 x ys) \<circ> f x" by (fact fold_remove1_split)
   1.222 +  moreover from Cons.prems have "List.fold f xs = List.fold f (remove1 x ys)" by (auto intro: Cons.hyps)
   1.223    ultimately show ?case by simp
   1.224  qed
   1.225  
   1.226 @@ -1915,5 +1851,7 @@
   1.227      multiset_postproc
   1.228  *}
   1.229  
   1.230 +hide_const (open) fold
   1.231 +
   1.232  end
   1.233