src/HOL/BNF/More_BNFs.thy
changeset 55066 4e5ddf3162ac
parent 54841 af71b753c459
child 55070 235c7661a96b
equal deleted inserted replaced
55065:6d0af3c10864 55066:4e5ddf3162ac
    14 imports
    14 imports
    15   Basic_BNFs
    15   Basic_BNFs
    16   "~~/src/HOL/Library/FSet"
    16   "~~/src/HOL/Library/FSet"
    17   "~~/src/HOL/Library/Multiset"
    17   "~~/src/HOL/Library/Multiset"
    18 begin
    18 begin
       
    19 
       
    20 notation
       
    21   ordLeq2 (infix "<=o" 50) and
       
    22   ordLeq3 (infix "\<le>o" 50) and
       
    23   ordLess2 (infix "<o" 50) and
       
    24   ordIso2 (infix "=o" 50) and
       
    25   csum (infixr "+c" 65) and
       
    26   cprod (infixr "*c" 80) and
       
    27   cexp (infixr "^c" 90) and
       
    28   convol ("<_ , _>")
    19 
    29 
    20 lemma option_rec_conv_option_case: "option_rec = option_case"
    30 lemma option_rec_conv_option_case: "option_rec = option_case"
    21 by (simp add: fun_eq_iff split: option.split)
    31 by (simp add: fun_eq_iff split: option.split)
    22 
    32 
    23 bnf "'a option"
    33 bnf "'a option"
    82   fix x f g
    92   fix x f g
    83   assume "\<And>z. z \<in> set x \<Longrightarrow> f z = g z"
    93   assume "\<And>z. z \<in> set x \<Longrightarrow> f z = g z"
    84   thus "map f x = map g x" by simp
    94   thus "map f x = map g x" by simp
    85 next
    95 next
    86   fix f
    96   fix f
    87   show "set o map f = image f o set" by (rule ext, unfold o_apply, rule set_map)
    97   show "set o map f = image f o set" by (rule ext, unfold comp_apply, rule set_map)
    88 next
    98 next
    89   show "card_order natLeq" by (rule natLeq_card_order)
    99   show "card_order natLeq" by (rule natLeq_card_order)
    90 next
   100 next
    91   show "cinfinite natLeq" by (rule natLeq_cinfinite)
   101   show "cinfinite natLeq" by (rule natLeq_cinfinite)
    92 next
   102 next
   235   also have "... = setsum (setsum (count f) o ?As) ?B"
   245   also have "... = setsum (setsum (count f) o ?As) ?B"
   236     by(intro setsum_reindex) (auto simp add: setsum_gt_0_iff inj_on_def)
   246     by(intro setsum_reindex) (auto simp add: setsum_gt_0_iff inj_on_def)
   237   also have "... = setsum (\<lambda> b. setsum (count f) (?As b)) ?B" unfolding comp_def ..
   247   also have "... = setsum (\<lambda> b. setsum (count f) (?As b)) ?B" unfolding comp_def ..
   238   finally have "setsum (count f) ?A = setsum (\<lambda> b. setsum (count f) (?As b)) ?B" .
   248   finally have "setsum (count f) ?A = setsum (\<lambda> b. setsum (count f) (?As b)) ?B" .
   239   thus "count (mmap (h2 \<circ> h1) f) c = count ((mmap h2 \<circ> mmap h1) f) c"
   249   thus "count (mmap (h2 \<circ> h1) f) c = count ((mmap h2 \<circ> mmap h1) f) c"
   240     by transfer (unfold o_apply, blast)
   250     by transfer (unfold comp_apply, blast)
   241 qed
   251 qed
   242 
   252 
   243 lemma mmap_cong:
   253 lemma mmap_cong:
   244 assumes "\<And>a. a \<in># M \<Longrightarrow> f a = g a"
   254 assumes "\<And>a. a \<in># M \<Longrightarrow> f a = g a"
   245 shows "mmap f M = mmap g M"
   255 shows "mmap f M = mmap g M"
   253   unfolding set_of_def pcr_multiset_def cr_multiset_def fun_rel_def by auto
   263   unfolding set_of_def pcr_multiset_def cr_multiset_def fun_rel_def by auto
   254 
   264 
   255 end
   265 end
   256 
   266 
   257 lemma set_of_mmap: "set_of o mmap h = image h o set_of"
   267 lemma set_of_mmap: "set_of o mmap h = image h o set_of"
   258 proof (rule ext, unfold o_apply)
   268 proof (rule ext, unfold comp_apply)
   259   fix M show "set_of (mmap h M) = h ` set_of M"
   269   fix M show "set_of (mmap h M) = h ` set_of M"
   260     by transfer (auto simp add: multiset_def setsum_gt_0_iff)
   270     by transfer (auto simp add: multiset_def setsum_gt_0_iff)
   261 qed
   271 qed
   262 
   272 
   263 lemma multiset_of_surj:
   273 lemma multiset_of_surj:
   685         thus ?thesis using False by auto
   695         thus ?thesis using False by auto
   686       next
   696       next
   687         case True
   697         case True
   688         def c \<equiv> "f1 b1"
   698         def c \<equiv> "f1 b1"
   689         have c: "c \<in> set_of P" and b1: "b1 \<in> set1 c"
   699         have c: "c \<in> set_of P" and b1: "b1 \<in> set1 c"
   690           unfolding set1_def c_def P1 using True by (auto simp: o_eq_dest[OF set_of_mmap])
   700           unfolding set1_def c_def P1 using True by (auto simp: comp_eq_dest[OF set_of_mmap])
   691         with sM(1) have "setsum (count M) ?K = setsum (count M) {a. p1 a = b1 \<and> a \<in> SET}"
   701         with sM(1) have "setsum (count M) ?K = setsum (count M) {a. p1 a = b1 \<and> a \<in> SET}"
   692           by transfer (force intro: setsum_mono_zero_cong_left split: split_if_asm)
   702           by transfer (force intro: setsum_mono_zero_cong_left split: split_if_asm)
   693         also have "... = setsum (count M) ((\<lambda> b2. u c b1 b2) ` (set2 c))"
   703         also have "... = setsum (count M) ((\<lambda> b2. u c b1 b2) ` (set2 c))"
   694           apply(rule setsum_cong) using c b1 proof safe
   704           apply(rule setsum_cong) using c b1 proof safe
   695           fix a assume p1a: "p1 a \<in> set1 c" and "c \<in> set_of P" and "a \<in> SET"
   705           fix a assume p1a: "p1 a \<in> set1 c" and "c \<in> set_of P" and "a \<in> SET"
   721         thus ?thesis using False by auto
   731         thus ?thesis using False by auto
   722       next
   732       next
   723         case True
   733         case True
   724         def c \<equiv> "f2 b2"
   734         def c \<equiv> "f2 b2"
   725         have c: "c \<in> set_of P" and b2: "b2 \<in> set2 c"
   735         have c: "c \<in> set_of P" and b2: "b2 \<in> set2 c"
   726           unfolding set2_def c_def P2 using True by (auto simp: o_eq_dest[OF set_of_mmap])
   736           unfolding set2_def c_def P2 using True by (auto simp: comp_eq_dest[OF set_of_mmap])
   727         with sM(1) have "setsum (count M) ?K = setsum (count M) {a. p2 a = b2 \<and> a \<in> SET}"
   737         with sM(1) have "setsum (count M) ?K = setsum (count M) {a. p2 a = b2 \<and> a \<in> SET}"
   728           by transfer (force intro: setsum_mono_zero_cong_left split: split_if_asm)
   738           by transfer (force intro: setsum_mono_zero_cong_left split: split_if_asm)
   729         also have "... = setsum (count M) ((\<lambda> b1. u c b1 b2) ` (set1 c))"
   739         also have "... = setsum (count M) ((\<lambda> b1. u c b1 b2) ` (set1 c))"
   730           apply(rule setsum_cong) using c b2 proof safe
   740           apply(rule setsum_cong) using c b2 proof safe
   731           fix a assume p2a: "p2 a \<in> set2 c" and "c \<in> set_of P" and "a \<in> SET"
   741           fix a assume p2a: "p2 a \<in> set2 c" and "c \<in> set_of P" and "a \<in> SET"
   736         qed auto
   746         qed auto
   737         also have "... = setsum (count M o (\<lambda> b1. u c b1 b2)) (set1 c)"
   747         also have "... = setsum (count M o (\<lambda> b1. u c b1 b2)) (set1 c)"
   738           apply(rule setsum_reindex)
   748           apply(rule setsum_reindex)
   739           using inj unfolding inj_on_def inj2_def using b2 c u(2) by blast
   749           using inj unfolding inj_on_def inj2_def using b2 c u(2) by blast
   740         also have "... = setsum (\<lambda> b1. count M (u c b1 b2)) (set1 c)" by simp
   750         also have "... = setsum (\<lambda> b1. count M (u c b1 b2)) (set1 c)" by simp
   741         also have "... = count N2 b2" unfolding ss2[OF c b2, symmetric] o_def
   751         also have "... = count N2 b2" unfolding ss2[OF c b2, symmetric] comp_def
   742           apply(rule setsum_cong[OF refl]) apply (transfer fixing: Ms u c b2 set1)
   752           apply(rule setsum_cong[OF refl]) apply (transfer fixing: Ms u c b2 set1)
   743           using True h_u1[OF c _ b2] u(2,3)[OF c _ b2] u_SET[OF c _ b2] set1_def by fastforce
   753           using True h_u1[OF c _ b2] u(2,3)[OF c _ b2] u_SET[OF c _ b2] set1_def by fastforce
   744         finally show ?thesis .
   754         finally show ?thesis .
   745       qed
   755       qed
   746       thus "count (mmap p2 M) b2 = count N2 b2" by transfer
   756       thus "count (mmap p2 M) b2 = count N2 b2" by transfer
   760   from assms obtain j where j: "\<forall>a'\<in>thePull B1 B2 f1 f2.
   770   from assms obtain j where j: "\<forall>a'\<in>thePull B1 B2 f1 f2.
   761     j a' \<in> A \<and> e1 (p1 (j a')) = e1 (fst a') \<and> e2 (p2 (j a')) = e2 (snd a')" 
   771     j a' \<in> A \<and> e1 (p1 (j a')) = e1 (fst a') \<and> e2 (p2 (j a')) = e2 (snd a')" 
   762     by (blast dest: wppull_thePull)
   772     by (blast dest: wppull_thePull)
   763   then show ?thesis
   773   then show ?thesis
   764     by (intro wpull_wppull[OF wpull_mmap[OF wpull_thePull], of _ _ _ _ "mmap j"])
   774     by (intro wpull_wppull[OF wpull_mmap[OF wpull_thePull], of _ _ _ _ "mmap j"])
   765       (auto simp: o_eq_dest_lhs[OF mmap_comp[symmetric]] o_eq_dest[OF set_of_mmap]
   775       (auto simp: comp_eq_dest_lhs[OF mmap_comp[symmetric]] comp_eq_dest[OF set_of_mmap]
   766         intro!: mmap_cong simp del: mem_set_of_iff simp: mem_set_of_iff[symmetric])
   776         intro!: mmap_cong simp del: mem_set_of_iff simp: mem_set_of_iff[symmetric])
   767 qed
   777 qed
   768 
   778 
   769 bnf "'a multiset"
   779 bnf "'a multiset"
   770   map: mmap
   780   map: mmap
   772   bd: natLeq
   782   bd: natLeq
   773   wits: "{#}"
   783   wits: "{#}"
   774 by (auto simp add: mmap_id0 mmap_comp set_of_mmap natLeq_card_order natLeq_cinfinite set_of_bd
   784 by (auto simp add: mmap_id0 mmap_comp set_of_mmap natLeq_card_order natLeq_cinfinite set_of_bd
   775   Grp_def relcompp.simps intro: mmap_cong)
   785   Grp_def relcompp.simps intro: mmap_cong)
   776   (metis wppull_mmap[OF wppull_fstOp_sndOp, unfolded wppull_def
   786   (metis wppull_mmap[OF wppull_fstOp_sndOp, unfolded wppull_def
   777     o_eq_dest_lhs[OF mmap_comp[symmetric]] fstOp_def sndOp_def o_def, simplified])
   787     o_eq_dest_lhs[OF mmap_comp[symmetric]] fstOp_def sndOp_def comp_def, simplified])
   778 
   788 
   779 inductive rel_multiset' where
   789 inductive rel_multiset' where
   780   Zero[intro]: "rel_multiset' R {#} {#}"
   790   Zero[intro]: "rel_multiset' R {#} {#}"
   781 | Plus[intro]: "\<lbrakk>R a b; rel_multiset' R M N\<rbrakk> \<Longrightarrow> rel_multiset' R (M + {#a#}) (N + {#b#})"
   791 | Plus[intro]: "\<lbrakk>R a b; rel_multiset' R M N\<rbrakk> \<Longrightarrow> rel_multiset' R (M + {#a#}) (N + {#b#})"
   782 
   792