src/HOL/BNF/More_BNFs.thy
changeset 51893 596baae88a88
parent 51836 4d6dcd51dd52
child 52544 0c4b140cff00
equal deleted inserted replaced
51892:e5432ec161ff 51893:596baae88a88
    81   fix z
    81   fix z
    82   assume "z \<in> Option.set None"
    82   assume "z \<in> Option.set None"
    83   thus False by simp
    83   thus False by simp
    84 next
    84 next
    85   fix R
    85   fix R
    86   show "{p. option_rel (\<lambda>x y. (x, y) \<in> R) (fst p) (snd p)} =
    86   show "option_rel R =
    87         (Gr {x. Option.set x \<subseteq> R} (Option.map fst))\<inverse> O Gr {x. Option.set x \<subseteq> R} (Option.map snd)"
    87         (Grp {x. Option.set x \<subseteq> Collect (split R)} (Option.map fst))\<inverse>\<inverse> OO
    88   unfolding option_rel_unfold Gr_def relcomp_unfold converse_unfold
    88          Grp {x. Option.set x \<subseteq> Collect (split R)} (Option.map snd)"
       
    89   unfolding option_rel_unfold Grp_def relcompp.simps conversep.simps fun_eq_iff prod.cases
    89   by (auto simp: trans[OF eq_commute option_map_is_None] trans[OF eq_commute option_map_eq_Some]
    90   by (auto simp: trans[OF eq_commute option_map_is_None] trans[OF eq_commute option_map_eq_Some]
    90            split: option.splits) blast
    91            split: option.splits)
    91 qed
    92 qed
    92 
    93 
    93 lemma card_of_list_in:
    94 lemma card_of_list_in:
    94   "|{xs. set xs \<subseteq> A}| \<le>o |Pfunc (UNIV :: nat set) A|" (is "|?LHS| \<le>o |?RHS|")
    95   "|{xs. set xs \<subseteq> A}| \<le>o |Pfunc (UNIV :: nat set) A|" (is "|?LHS| \<le>o |?RHS|")
    95 proof -
    96 proof -
   242      transfer, metis Collect_finite_eq_lists lists_UNIV mem_Collect_eq)
   243      transfer, metis Collect_finite_eq_lists lists_UNIV mem_Collect_eq)
   243 
   244 
   244 
   245 
   245 lemma fset_rel_aux:
   246 lemma fset_rel_aux:
   246 "(\<forall>t \<in> fset a. \<exists>u \<in> fset b. R t u) \<and> (\<forall>u \<in> fset b. \<exists>t \<in> fset a. R t u) \<longleftrightarrow>
   247 "(\<forall>t \<in> fset a. \<exists>u \<in> fset b. R t u) \<and> (\<forall>u \<in> fset b. \<exists>t \<in> fset a. R t u) \<longleftrightarrow>
   247  (a, b) \<in> (Gr {a. fset a \<subseteq> {(a, b). R a b}} (fmap fst))\<inverse> O
   248  ((Grp {a. fset a \<subseteq> {(a, b). R a b}} (fmap fst))\<inverse>\<inverse> OO
   248           Gr {a. fset a \<subseteq> {(a, b). R a b}} (fmap snd)" (is "?L = ?R")
   249   Grp {a. fset a \<subseteq> {(a, b). R a b}} (fmap snd)) a b" (is "?L = ?R")
   249 proof
   250 proof
   250   assume ?L
   251   assume ?L
   251   def R' \<equiv> "the_inv fset (Collect (split R) \<inter> (fset a \<times> fset b))" (is "the_inv fset ?L'")
   252   def R' \<equiv> "the_inv fset (Collect (split R) \<inter> (fset a \<times> fset b))" (is "the_inv fset ?L'")
   252   have "finite ?L'" by (intro finite_Int[OF disjI2] finite_cartesian_product) (transfer, simp)+
   253   have "finite ?L'" by (intro finite_Int[OF disjI2] finite_cartesian_product) (transfer, simp)+
   253   hence *: "fset R' = ?L'" unfolding R'_def by (intro fset_to_fset)
   254   hence *: "fset R' = ?L'" unfolding R'_def by (intro fset_to_fset)
   254   show ?R unfolding Gr_def relcomp_unfold converse_unfold
   255   show ?R unfolding Grp_def relcompp.simps conversep.simps
   255   proof (intro CollectI prod_caseI exI conjI)
   256   proof (intro CollectI prod_caseI exI[of _ a] exI[of _ b] exI[of _ R'] conjI refl)
   256     from * show "(R', a) = (R', fmap fst R')" using conjunct1[OF `?L`]
   257     from * show "a = fmap fst R'" using conjunct1[OF `?L`]
   257       by (clarify, transfer, auto simp add: image_def Int_def split: prod.splits)
   258       by (transfer, auto simp add: image_def Int_def split: prod.splits)
   258     from * show "(R', b) = (R', fmap snd R')" using conjunct2[OF `?L`]
   259     from * show "b = fmap snd R'" using conjunct2[OF `?L`]
   259       by (clarify, transfer, auto simp add: image_def Int_def split: prod.splits)
   260       by (transfer, auto simp add: image_def Int_def split: prod.splits)
   260   qed (auto simp add: *)
   261   qed (auto simp add: *)
   261 next
   262 next
   262   assume ?R thus ?L unfolding Gr_def relcomp_unfold converse_unfold
   263   assume ?R thus ?L unfolding Grp_def relcompp.simps conversep.simps
   263   apply (simp add: subset_eq Ball_def)
   264   apply (simp add: subset_eq Ball_def)
   264   apply (rule conjI)
   265   apply (rule conjI)
   265   apply (transfer, clarsimp, metis snd_conv)
   266   apply (transfer, clarsimp, metis snd_conv)
   266   by (transfer, clarsimp, metis fst_conv)
   267   by (transfer, clarsimp, metis fst_conv)
   267 qed
   268 qed
   337      apply (rule natLeq_cinfinite)
   338      apply (rule natLeq_cinfinite)
   338     apply transfer apply (metis ordLess_imp_ordLeq finite_iff_ordLess_natLeq finite_set)
   339     apply transfer apply (metis ordLess_imp_ordLeq finite_iff_ordLess_natLeq finite_set)
   339    apply (rule ordLeq_transitive[OF surj_imp_ordLeq[of _ abs_fset] list_in_bd])
   340    apply (rule ordLeq_transitive[OF surj_imp_ordLeq[of _ abs_fset] list_in_bd])
   340    apply (auto simp: fset_def intro!: image_eqI[of _ abs_fset]) []
   341    apply (auto simp: fset_def intro!: image_eqI[of _ abs_fset]) []
   341   apply (erule wpull_fmap)
   342   apply (erule wpull_fmap)
   342  apply (simp add: Gr_def relcomp_unfold converse_unfold fset_rel_def fset_rel_aux) 
   343  apply (simp add: Grp_def relcompp.simps conversep.simps fun_eq_iff fset_rel_def fset_rel_aux) 
   343 apply transfer apply simp
   344 apply transfer apply simp
   344 done
   345 done
   345 
   346 
   346 lemmas [simp] = fset.map_comp' fset.map_id' fset.set_map'
   347 lemmas [simp] = fset.map_comp' fset.map_id' fset.set_map'
   347 
   348 
   417  (\<forall>t \<in> rcset a. \<exists>u \<in> rcset b. R t u) \<and>
   418  (\<forall>t \<in> rcset a. \<exists>u \<in> rcset b. R t u) \<and>
   418  (\<forall>t \<in> rcset b. \<exists>u \<in> rcset a. R u t)"
   419  (\<forall>t \<in> rcset b. \<exists>u \<in> rcset a. R u t)"
   419 
   420 
   420 lemma cset_rel_aux:
   421 lemma cset_rel_aux:
   421 "(\<forall>t \<in> rcset a. \<exists>u \<in> rcset b. R t u) \<and> (\<forall>t \<in> rcset b. \<exists>u \<in> rcset a. R u t) \<longleftrightarrow>
   422 "(\<forall>t \<in> rcset a. \<exists>u \<in> rcset b. R t u) \<and> (\<forall>t \<in> rcset b. \<exists>u \<in> rcset a. R u t) \<longleftrightarrow>
   422  (a, b) \<in> (Gr {x. rcset x \<subseteq> {(a, b). R a b}} (cIm fst))\<inverse> O
   423  ((Grp {x. rcset x \<subseteq> {(a, b). R a b}} (cIm fst))\<inverse>\<inverse> OO
   423           Gr {x. rcset x \<subseteq> {(a, b). R a b}} (cIm snd)" (is "?L = ?R")
   424           Grp {x. rcset x \<subseteq> {(a, b). R a b}} (cIm snd)) a b" (is "?L = ?R")
   424 proof
   425 proof
   425   assume ?L
   426   assume ?L
   426   def R' \<equiv> "the_inv rcset (Collect (split R) \<inter> (rcset a \<times> rcset b))"
   427   def R' \<equiv> "the_inv rcset (Collect (split R) \<inter> (rcset a \<times> rcset b))"
   427   (is "the_inv rcset ?L'")
   428   (is "the_inv rcset ?L'")
   428   have "countable ?L'" by auto
   429   have "countable ?L'" by auto
   429   hence *: "rcset R' = ?L'" unfolding R'_def using fset_to_fset by (intro rcset_to_rcset)
   430   hence *: "rcset R' = ?L'" unfolding R'_def using fset_to_fset by (intro rcset_to_rcset)
   430   show ?R unfolding Gr_def relcomp_unfold converse_unfold
   431   show ?R unfolding Grp_def relcompp.simps conversep.simps
   431   proof (intro CollectI prod_caseI exI conjI)
   432   proof (intro CollectI prod_caseI exI[of _ a] exI[of _ b] exI[of _ R'] conjI refl)
   432     have "rcset a = fst ` ({(x, y). R x y} \<inter> rcset a \<times> rcset b)" (is "_ = ?A")
   433     have "rcset a = fst ` ({(x, y). R x y} \<inter> rcset a \<times> rcset b)" (is "_ = ?A")
   433     using conjunct1[OF `?L`] unfolding image_def by (auto simp add: Collect_Int_Times)
   434     using conjunct1[OF `?L`] unfolding image_def by (auto simp add: Collect_Int_Times)
   434     hence "a = acset ?A" by (metis acset_rcset)
   435     hence "a = acset ?A" by (metis acset_rcset)
   435     thus "(R', a) = (R', cIm fst R')" unfolding cIm_def * by auto
   436     thus "a = cIm fst R'" unfolding cIm_def * by auto
   436     have "rcset b = snd ` ({(x, y). R x y} \<inter> rcset a \<times> rcset b)" (is "_ = ?B")
   437     have "rcset b = snd ` ({(x, y). R x y} \<inter> rcset a \<times> rcset b)" (is "_ = ?B")
   437     using conjunct2[OF `?L`] unfolding image_def by (auto simp add: Collect_Int_Times)
   438     using conjunct2[OF `?L`] unfolding image_def by (auto simp add: Collect_Int_Times)
   438     hence "b = acset ?B" by (metis acset_rcset)
   439     hence "b = acset ?B" by (metis acset_rcset)
   439     thus "(R', b) = (R', cIm snd R')" unfolding cIm_def * by auto
   440     thus "b = cIm snd R'" unfolding cIm_def * by auto
   440   qed (auto simp add: *)
   441   qed (auto simp add: *)
   441 next
   442 next
   442   assume ?R thus ?L unfolding Gr_def relcomp_unfold converse_unfold
   443   assume ?R thus ?L unfolding Grp_def relcompp.simps conversep.simps
   443   apply (simp add: subset_eq Ball_def)
   444   apply (simp add: subset_eq Ball_def)
   444   apply (rule conjI)
   445   apply (rule conjI)
   445   apply (clarsimp, metis (lifting, no_types) rcset_map' image_iff surjective_pairing)
   446   apply (clarsimp, metis (lifting, no_types) rcset_map' image_iff surjective_pairing)
   446   apply (clarsimp)
   447   apply (clarsimp)
   447   by (metis Domain.intros Range.simps rcset_map' fst_eq_Domain snd_eq_Range)
   448   by (metis Domain.intros Range.simps rcset_map' fst_eq_Domain snd_eq_Range)
   509     show "\<exists>x\<in>{x. rcset x \<subseteq> A}. cIm p1 x = y1 \<and> cIm p2 x = y2"
   510     show "\<exists>x\<in>{x. rcset x \<subseteq> A}. cIm p1 x = y1 \<and> cIm p2 x = y2"
   510     apply(intro bexI[of _ "x"]) using X' Y1 Y2 unfolding X'eq cIm_def by auto
   511     apply(intro bexI[of _ "x"]) using X' Y1 Y2 unfolding X'eq cIm_def by auto
   511   qed
   512   qed
   512 next
   513 next
   513   fix R
   514   fix R
   514   show "{p. cset_rel (\<lambda>x y. (x, y) \<in> R) (fst p) (snd p)} =
   515   show "cset_rel R =
   515         (Gr {x. rcset x \<subseteq> R} (cIm fst))\<inverse> O Gr {x. rcset x \<subseteq> R} (cIm snd)"
   516         (Grp {x. rcset x \<subseteq> Collect (split R)} (cIm fst))\<inverse>\<inverse> OO Grp {x. rcset x \<subseteq> Collect (split R)} (cIm snd)"
   516   unfolding cset_rel_def cset_rel_aux by simp
   517   unfolding cset_rel_def[abs_def] cset_rel_aux by simp
   517 qed (unfold cEmp_def, auto)
   518 qed (unfold cEmp_def, auto)
   518 
   519 
   519 
   520 
   520 (* Multisets *)
   521 (* Multisets *)
   521 
   522 
  1141 by (metis image_is_empty multiset.set_map' set_of_eq_empty_iff)
  1142 by (metis image_is_empty multiset.set_map' set_of_eq_empty_iff)
  1142 
  1143 
  1143 lemma multiset_map_Zero[simp]: "multiset_map f {#} = {#}" by simp
  1144 lemma multiset_map_Zero[simp]: "multiset_map f {#} = {#}" by simp
  1144 
  1145 
  1145 lemma multiset_rel_Zero: "multiset_rel R {#} {#}"
  1146 lemma multiset_rel_Zero: "multiset_rel R {#} {#}"
  1146 unfolding multiset_rel_def Gr_def relcomp_unfold by auto
  1147 unfolding multiset_rel_def Grp_def by auto
  1147 
  1148 
  1148 declare multiset.count[simp]
  1149 declare multiset.count[simp]
  1149 declare mmap[simp]
  1150 declare mmap[simp]
  1150 declare Abs_multiset_inverse[simp]
  1151 declare Abs_multiset_inverse[simp]
  1151 declare multiset.count_inverse[simp]
  1152 declare multiset.count_inverse[simp]
  1194                set_of ya \<subseteq> {(x, y). R x y}"
  1195                set_of ya \<subseteq> {(x, y). R x y}"
  1195    apply(intro exI[of _ "y + {#(a,b)#}"]) by auto
  1196    apply(intro exI[of _ "y + {#(a,b)#}"]) by auto
  1196   }
  1197   }
  1197   thus ?thesis
  1198   thus ?thesis
  1198   using assms
  1199   using assms
  1199   unfolding multiset_rel_def Gr_def relcomp_unfold by force
  1200   unfolding multiset_rel_def Grp_def by force
  1200 qed
  1201 qed
  1201 
  1202 
  1202 lemma multiset_rel'_imp_multiset_rel:
  1203 lemma multiset_rel'_imp_multiset_rel:
  1203 "multiset_rel' R M N \<Longrightarrow> multiset_rel R M N"
  1204 "multiset_rel' R M N \<Longrightarrow> multiset_rel R M N"
  1204 apply(induct rule: multiset_rel'.induct)
  1205 apply(induct rule: multiset_rel'.induct)
  1236 qed
  1237 qed
  1237 
  1238 
  1238 lemma multiset_rel_mcard:
  1239 lemma multiset_rel_mcard:
  1239 assumes "multiset_rel R M N"
  1240 assumes "multiset_rel R M N"
  1240 shows "mcard M = mcard N"
  1241 shows "mcard M = mcard N"
  1241 using assms unfolding multiset_rel_def relcomp_unfold Gr_def by auto
  1242 using assms unfolding multiset_rel_def Grp_def by auto
  1242 
  1243 
  1243 lemma multiset_induct2[case_names empty addL addR]:
  1244 lemma multiset_induct2[case_names empty addL addR]:
  1244 assumes empty: "P {#} {#}"
  1245 assumes empty: "P {#} {#}"
  1245 and addL: "\<And>M N a. P M N \<Longrightarrow> P (M + {#a#}) N"
  1246 and addL: "\<And>M N a. P M N \<Longrightarrow> P (M + {#a#}) N"
  1246 and addR: "\<And>M N a. P M N \<Longrightarrow> P M (N + {#a#})"
  1247 and addR: "\<And>M N a. P M N \<Longrightarrow> P M (N + {#a#})"
  1297 shows "\<exists> N1 b. N = N1 + {#b#} \<and> R a b \<and> multiset_rel R M N1"
  1298 shows "\<exists> N1 b. N = N1 + {#b#} \<and> R a b \<and> multiset_rel R M N1"
  1298 proof-
  1299 proof-
  1299   obtain K where KM: "multiset_map fst K = M + {#a#}"
  1300   obtain K where KM: "multiset_map fst K = M + {#a#}"
  1300   and KN: "multiset_map snd K = N" and sK: "set_of K \<subseteq> {(a, b). R a b}"
  1301   and KN: "multiset_map snd K = N" and sK: "set_of K \<subseteq> {(a, b). R a b}"
  1301   using assms
  1302   using assms
  1302   unfolding multiset_rel_def Gr_def relcomp_unfold by auto
  1303   unfolding multiset_rel_def Grp_def by auto
  1303   obtain K1 ab where K: "K = K1 + {#ab#}" and a: "fst ab = a"
  1304   obtain K1 ab where K: "K = K1 + {#ab#}" and a: "fst ab = a"
  1304   and K1M: "multiset_map fst K1 = M" using msed_map_invR[OF KM] by auto
  1305   and K1M: "multiset_map fst K1 = M" using msed_map_invR[OF KM] by auto
  1305   obtain N1 where N: "N = N1 + {#snd ab#}" and K1N1: "multiset_map snd K1 = N1"
  1306   obtain N1 where N: "N = N1 + {#snd ab#}" and K1N1: "multiset_map snd K1 = N1"
  1306   using msed_map_invL[OF KN[unfolded K]] by auto
  1307   using msed_map_invL[OF KN[unfolded K]] by auto
  1307   have Rab: "R a (snd ab)" using sK a unfolding K by auto
  1308   have Rab: "R a (snd ab)" using sK a unfolding K by auto
  1308   have "multiset_rel R M N1" using sK K1M K1N1
  1309   have "multiset_rel R M N1" using sK K1M K1N1
  1309   unfolding K multiset_rel_def Gr_def relcomp_unfold by auto
  1310   unfolding K multiset_rel_def Grp_def by auto
  1310   thus ?thesis using N Rab by auto
  1311   thus ?thesis using N Rab by auto
  1311 qed
  1312 qed
  1312 
  1313 
  1313 lemma msed_rel_invR:
  1314 lemma msed_rel_invR:
  1314 assumes "multiset_rel R M (N + {#b#})"
  1315 assumes "multiset_rel R M (N + {#b#})"
  1315 shows "\<exists> M1 a. M = M1 + {#a#} \<and> R a b \<and> multiset_rel R M1 N"
  1316 shows "\<exists> M1 a. M = M1 + {#a#} \<and> R a b \<and> multiset_rel R M1 N"
  1316 proof-
  1317 proof-
  1317   obtain K where KN: "multiset_map snd K = N + {#b#}"
  1318   obtain K where KN: "multiset_map snd K = N + {#b#}"
  1318   and KM: "multiset_map fst K = M" and sK: "set_of K \<subseteq> {(a, b). R a b}"
  1319   and KM: "multiset_map fst K = M" and sK: "set_of K \<subseteq> {(a, b). R a b}"
  1319   using assms
  1320   using assms
  1320   unfolding multiset_rel_def Gr_def relcomp_unfold by auto
  1321   unfolding multiset_rel_def Grp_def by auto
  1321   obtain K1 ab where K: "K = K1 + {#ab#}" and b: "snd ab = b"
  1322   obtain K1 ab where K: "K = K1 + {#ab#}" and b: "snd ab = b"
  1322   and K1N: "multiset_map snd K1 = N" using msed_map_invR[OF KN] by auto
  1323   and K1N: "multiset_map snd K1 = N" using msed_map_invR[OF KN] by auto
  1323   obtain M1 where M: "M = M1 + {#fst ab#}" and K1M1: "multiset_map fst K1 = M1"
  1324   obtain M1 where M: "M = M1 + {#fst ab#}" and K1M1: "multiset_map fst K1 = M1"
  1324   using msed_map_invL[OF KM[unfolded K]] by auto
  1325   using msed_map_invL[OF KM[unfolded K]] by auto
  1325   have Rab: "R (fst ab) b" using sK b unfolding K by auto
  1326   have Rab: "R (fst ab) b" using sK b unfolding K by auto
  1326   have "multiset_rel R M1 N" using sK K1N K1M1
  1327   have "multiset_rel R M1 N" using sK K1N K1M1
  1327   unfolding K multiset_rel_def Gr_def relcomp_unfold by auto
  1328   unfolding K multiset_rel_def Grp_def by auto
  1328   thus ?thesis using M Rab by auto
  1329   thus ?thesis using M Rab by auto
  1329 qed
  1330 qed
  1330 
  1331 
  1331 lemma multiset_rel_imp_multiset_rel':
  1332 lemma multiset_rel_imp_multiset_rel':
  1332 assumes "multiset_rel R M N"
  1333 assumes "multiset_rel R M N"