Nominal/FSet.thy
changeset 1822 4465723e35e7
parent 1821 509a0ccc4f32
child 1823 91ba55dba5e0
equal deleted inserted replaced
1821:509a0ccc4f32 1822:4465723e35e7
   129   using a
   129   using a
   130   by (induct xs) (auto simp add: memb_def)
   130   by (induct xs) (auto simp add: memb_def)
   131 
   131 
   132 lemma fcard_raw_not_memb:
   132 lemma fcard_raw_not_memb:
   133   fixes x :: "'a"
   133   fixes x :: "'a"
   134   fixes xs :: "'a list"
       
   135   shows "\<not>(memb x xs) \<longleftrightarrow> (fcard_raw (x # xs) = Suc (fcard_raw xs))"
   134   shows "\<not>(memb x xs) \<longleftrightarrow> (fcard_raw (x # xs) = Suc (fcard_raw xs))"
   136   by auto
   135   by auto
   137 
   136 
   138 lemma fcard_raw_suc:
   137 lemma fcard_raw_suc:
   139   fixes xs :: "'a list"
   138   fixes xs :: "'a list"
   140   fixes n :: "nat"
       
   141   assumes c: "fcard_raw xs = Suc n"
   139   assumes c: "fcard_raw xs = Suc n"
   142   shows "\<exists>x ys. \<not>(memb x ys) \<and> xs \<approx> (x # ys) \<and> fcard_raw ys = n"
   140   shows "\<exists>x ys. \<not>(memb x ys) \<and> xs \<approx> (x # ys) \<and> fcard_raw ys = n"
   143   unfolding memb_def
   141   unfolding memb_def
   144   using c
   142   using c
   145   proof (induct xs)
   143   proof (induct xs)
   228   "memb x (xs @ ys) = (memb x xs \<or> memb x ys)"
   226   "memb x (xs @ ys) = (memb x xs \<or> memb x ys)"
   229   by (induct xs) (simp_all add: not_memb_nil memb_cons_iff)
   227   by (induct xs) (simp_all add: not_memb_nil memb_cons_iff)
   230 
   228 
   231 text {* raw section *}
   229 text {* raw section *}
   232 
   230 
   233 lemma map_rsp_aux:
       
   234   assumes a: "a \<approx> b"
       
   235   shows "map f a \<approx> map f b"
       
   236   using a
       
   237   apply(induct a arbitrary: b)
       
   238   apply(auto)
       
   239   apply(metis rev_image_eqI)
       
   240   done
       
   241 
       
   242 lemma map_rsp[quot_respect]:
   231 lemma map_rsp[quot_respect]:
   243   shows "(op = ===> op \<approx> ===> op \<approx>) map map"
   232   shows "(op = ===> op \<approx> ===> op \<approx>) map map"
   244   by (auto simp add: map_rsp_aux)
   233   by auto
   245 
   234 
   246 lemma cons_left_comm:
   235 lemma cons_left_comm:
   247   "x # y # xs \<approx> y # x # xs"
   236   "x # y # xs \<approx> y # x # xs"
   248   by auto
   237   by auto
   249 
   238 
   254 lemma none_mem_nil:
   243 lemma none_mem_nil:
   255   "(\<forall>x. x \<notin> set xs) = (xs \<approx> [])"
   244   "(\<forall>x. x \<notin> set xs) = (xs \<approx> [])"
   256   by simp
   245   by simp
   257 
   246 
   258 lemma fset_raw_strong_cases:
   247 lemma fset_raw_strong_cases:
   259   "(xs = []) \<or> (\<exists>x ys. ((x \<notin> set ys) \<and> (xs \<approx> x # ys)))"
   248   "(xs = []) \<or> (\<exists>x ys. ((\<not> memb x ys) \<and> (xs \<approx> x # ys)))"
   260   apply (induct xs)
   249   apply (induct xs)
   261   apply (simp)
   250   apply (simp)
   262   apply (rule disjI2)
   251   apply (rule disjI2)
   263   apply (erule disjE)
   252   apply (erule disjE)
   264   apply (rule_tac x="a" in exI)
   253   apply (rule_tac x="a" in exI)
   265   apply (rule_tac x="[]" in exI)
   254   apply (rule_tac x="[]" in exI)
   266   apply (simp)
   255   apply (simp add: memb_def)
   267   apply (erule exE)+
   256   apply (erule exE)+
   268   apply (case_tac "x = a")
   257   apply (case_tac "x = a")
   269   apply (rule_tac x="a" in exI)
   258   apply (rule_tac x="a" in exI)
   270   apply (rule_tac x="ys" in exI)
   259   apply (rule_tac x="ys" in exI)
   271   apply (simp)
   260   apply (simp)
   272   apply (rule_tac x="x" in exI)
   261   apply (rule_tac x="x" in exI)
   273   apply (rule_tac x="a # ys" in exI)
   262   apply (rule_tac x="a # ys" in exI)
   274   apply (auto)
   263   apply (auto simp add: memb_def)
   275   done
   264   done
   276 
   265 
   277 fun
   266 fun
   278   delete_raw :: "'a list \<Rightarrow> 'a \<Rightarrow> 'a list"
   267   delete_raw :: "'a list \<Rightarrow> 'a \<Rightarrow> 'a list"
   279 where
   268 where
   384 lemma finter_raw_empty:
   373 lemma finter_raw_empty:
   385   "finter_raw l [] = []"
   374   "finter_raw l [] = []"
   386   by (induct l) (simp_all add: not_memb_nil)
   375   by (induct l) (simp_all add: not_memb_nil)
   387 
   376 
   388 lemma memb_finter_raw:
   377 lemma memb_finter_raw:
   389   "memb e (finter_raw l r) = (memb e l \<and> memb e r)"
   378   "memb x (finter_raw xs ys) = (memb x xs \<and> memb x ys)"
   390   apply (induct l)
   379   apply (induct xs)
   391   apply (simp add: not_memb_nil)
   380   apply (simp add: not_memb_nil)
   392   apply (simp add: finter_raw.simps)
   381   apply (simp add: finter_raw.simps)
   393   apply (simp add: memb_cons_iff)
   382   apply (simp add: memb_cons_iff)
   394   apply auto
   383   apply auto
   395   done
   384   done
   417 where
   406 where
   418   "finter :: 'a fset \<Rightarrow> 'a fset \<Rightarrow> 'a fset"
   407   "finter :: 'a fset \<Rightarrow> 'a fset \<Rightarrow> 'a fset"
   419 is "finter_raw"
   408 is "finter_raw"
   420 
   409 
   421 lemma funion_sym_pre:
   410 lemma funion_sym_pre:
   422   "a @ b \<approx> b @ a"
   411   "xs @ ys \<approx> ys @ xs"
   423   by auto
   412   by auto
   424 
   413 
   425 lemma append_rsp[quot_respect]:
   414 lemma append_rsp[quot_respect]:
   426   shows "(op \<approx> ===> op \<approx> ===> op \<approx>) op @ op @"
   415   shows "(op \<approx> ===> op \<approx> ===> op \<approx>) op @ op @"
   427   by (auto)
   416   by auto
   428 
   417 
   429 lemma set_cong: 
   418 lemma set_cong: 
   430   shows "(set x = set y) = (x \<approx> y)"
   419   shows "(set x = set y) = (x \<approx> y)"
   431   by auto
   420   by auto
   432 
   421 
   465   "{||} \<noteq> finsert x S"
   454   "{||} \<noteq> finsert x S"
   466   "finsert x S \<noteq> {||}"
   455   "finsert x S \<noteq> {||}"
   467   by (lifting nil_not_cons)
   456   by (lifting nil_not_cons)
   468 
   457 
   469 lemma finsert_left_comm:
   458 lemma finsert_left_comm:
   470   "finsert a (finsert b S) = finsert b (finsert a S)"
   459   "finsert x (finsert y S) = finsert y (finsert x S)"
   471   by (lifting cons_left_comm)
   460   by (lifting cons_left_comm)
   472 
   461 
   473 lemma finsert_left_idem:
   462 lemma finsert_left_idem:
   474   "finsert a (finsert a S) = finsert a S"
   463   "finsert x (finsert x S) = finsert x S"
   475   by (lifting cons_left_idem)
   464   by (lifting cons_left_idem)
   476 
   465 
   477 lemma fsingleton_eq[simp]:
   466 lemma fsingleton_eq[simp]:
   478   shows "{|x|} = {|y|} \<longleftrightarrow> x = y"
   467   shows "{|x|} = {|y|} \<longleftrightarrow> x = y"
   479   by (lifting singleton_list_eq)
   468   by (lifting singleton_list_eq)
   483 lemma fset_to_set_simps[simp]:
   472 lemma fset_to_set_simps[simp]:
   484   "fset_to_set {||} = ({} :: 'a set)"
   473   "fset_to_set {||} = ({} :: 'a set)"
   485   "fset_to_set (finsert (h :: 'a) t) = insert h (fset_to_set t)"
   474   "fset_to_set (finsert (h :: 'a) t) = insert h (fset_to_set t)"
   486   by (lifting set.simps)
   475   by (lifting set.simps)
   487 
   476 
       
   477 thm memb_def[symmetric, THEN meta_eq_to_obj_eq]
       
   478 
   488 lemma in_fset_to_set:
   479 lemma in_fset_to_set:
   489   "x \<in> fset_to_set xs \<equiv> x |\<in>| xs"
   480   "x \<in> fset_to_set S \<equiv> x |\<in>| S"
   490   by (lifting memb_def[symmetric])
   481   by (lifting memb_def[symmetric])
   491 
   482 
   492 lemma none_fin_fempty:
   483 lemma none_fin_fempty:
   493   "(\<forall>a. a \<notin> fset_to_set A) = (A = {||})"
   484   "(\<forall>x. x \<notin> fset_to_set S) = (S = {||})"
   494   by (lifting none_mem_nil)
   485   by (lifting none_mem_nil)
   495 
   486 
   496 lemma fset_cong:
   487 lemma fset_cong:
   497   "(fset_to_set x = fset_to_set y) = (x = y)"
   488   "(fset_to_set S = fset_to_set T) = (S = T)"
   498   by (lifting set_cong)
   489   by (lifting set_cong)
   499 
   490 
   500 text {* fcard *}
   491 text {* fcard *}
   501 
   492 
   502 lemma fcard_fempty [simp]:
   493 lemma fcard_fempty [simp]:
   505 
   496 
   506 lemma fcard_finsert_if [simp]:
   497 lemma fcard_finsert_if [simp]:
   507   shows "fcard (finsert x S) = (if x |\<in>| S then fcard S else Suc (fcard S))"
   498   shows "fcard (finsert x S) = (if x |\<in>| S then fcard S else Suc (fcard S))"
   508   by (lifting fcard_raw_cons)
   499   by (lifting fcard_raw_cons)
   509 
   500 
   510 lemma fcard_0: "(fcard a = 0) = (a = {||})"
   501 lemma fcard_0: "(fcard S = 0) = (S = {||})"
   511   by (lifting fcard_raw_0)
   502   by (lifting fcard_raw_0)
   512 
   503 
   513 lemma fcard_1:
   504 lemma fcard_1:
   514   fixes xs::"'b fset"
   505   fixes S::"'b fset"
   515   shows "(fcard xs = 1) = (\<exists>x. xs = {|x|})"
   506   shows "(fcard S = 1) = (\<exists>x. S = {|x|})"
   516   by (lifting fcard_raw_1)
   507   by (lifting fcard_raw_1)
   517 
   508 
   518 lemma fcard_gt_0: "x \<in> fset_to_set xs \<Longrightarrow> 0 < fcard xs"
   509 lemma fcard_gt_0: "x \<in> fset_to_set S \<Longrightarrow> 0 < fcard S"
   519   by (lifting fcard_raw_gt_0)
   510   by (lifting fcard_raw_gt_0)
   520 
   511 
   521 lemma fcard_not_fin: "(x |\<notin>| xs) = (fcard (finsert x xs) = Suc (fcard xs))"
   512 lemma fcard_not_fin: "(x |\<notin>| S) = (fcard (finsert x S) = Suc (fcard S))"
   522   by (lifting fcard_raw_not_memb)
   513   by (lifting fcard_raw_not_memb)
   523 
   514 
   524 lemma fcard_suc: "fcard xs = Suc n \<Longrightarrow> \<exists>a ys. a |\<notin>| ys \<and> xs = finsert a ys \<and> fcard ys = n"
   515 lemma fcard_suc: "fcard S = Suc n \<Longrightarrow> \<exists>x T. x |\<notin>| T \<and> S = finsert x T \<and> fcard T = n"
   525   by (lifting fcard_raw_suc)
   516   by (lifting fcard_raw_suc)
   526 
   517 
   527 lemma fcard_delete:
   518 lemma fcard_delete:
   528   "fcard (fdelete xs y) = (if y |\<in>| xs then fcard xs - 1 else fcard xs)"
   519   "fcard (fdelete S y) = (if y |\<in>| S then fcard S - 1 else fcard S)"
   529   by (lifting fcard_raw_delete)
   520   by (lifting fcard_raw_delete)
   530 
   521 
   531 text {* funion *}
   522 text {* funion *}
   532 
   523 
   533 lemma funion_simps[simp]:
   524 lemma funion_simps[simp]:
   534   "{||} |\<union>| ys = ys"
   525   "{||} |\<union>| S = S"
   535   "finsert x xs |\<union>| ys = finsert x (xs |\<union>| ys)"
   526   "finsert x S |\<union>| T = finsert x (S |\<union>| T)"
   536   by (lifting append.simps)
   527   by (lifting append.simps)
   537 
   528 
   538 lemma funion_sym:
   529 lemma funion_sym:
   539   "a |\<union>| b = b |\<union>| a"
   530   "S |\<union>| T = T |\<union>| S"
   540   by (lifting funion_sym_pre)
   531   by (lifting funion_sym_pre)
   541 
   532 
   542 lemma funion_assoc:
   533 lemma funion_assoc:
   543  "x |\<union>| xa |\<union>| xb = x |\<union>| (xa |\<union>| xb)"
   534  "S |\<union>| T |\<union>| U = S |\<union>| (T |\<union>| U)"
   544   by (lifting append_assoc)
   535   by (lifting append_assoc)
   545 
   536 
   546 section {* Induction and Cases rules for finite sets *}
   537 section {* Induction and Cases rules for finite sets *}
   547 
   538 
   548 lemma fset_strong_cases:
   539 lemma fset_strong_cases:
   549   "X = {||} \<or> (\<exists>a Y. a \<notin> fset_to_set Y \<and> X = finsert a Y)"
   540   "S = {||} \<or> (\<exists>x T. x |\<notin>| T \<and> S = finsert x T)"
   550   by (lifting fset_raw_strong_cases)
   541   by (lifting fset_raw_strong_cases)
   551 
   542 
   552 lemma fset_exhaust[case_names fempty finsert, cases type: fset]:
   543 lemma fset_exhaust[case_names fempty finsert, cases type: fset]:
   553   shows "\<lbrakk>S = {||} \<Longrightarrow> P; \<And>x S'. S = finsert x S' \<Longrightarrow> P\<rbrakk> \<Longrightarrow> P"
   544   shows "\<lbrakk>S = {||} \<Longrightarrow> P; \<And>x S'. S = finsert x S' \<Longrightarrow> P\<rbrakk> \<Longrightarrow> P"
   554   by (lifting list.exhaust)
   545   by (lifting list.exhaust)
   594 
   585 
   595 text {* fmap *}
   586 text {* fmap *}
   596 
   587 
   597 lemma fmap_simps[simp]:
   588 lemma fmap_simps[simp]:
   598   "fmap (f :: 'a \<Rightarrow> 'b) {||} = {||}"
   589   "fmap (f :: 'a \<Rightarrow> 'b) {||} = {||}"
   599   "fmap f (finsert x xs) = finsert (f x) (fmap f xs)"
   590   "fmap f (finsert x S) = finsert (f x) (fmap f S)"
   600   by (lifting map.simps)
   591   by (lifting map.simps)
   601 
   592 
   602 lemma fmap_set_image:
   593 lemma fmap_set_image:
   603   "fset_to_set (fmap f fs) = f ` (fset_to_set fs)"
   594   "fset_to_set (fmap f S) = f ` (fset_to_set S)"
   604   apply (induct fs)
   595   by (induct S) (simp_all)
   605   apply (simp_all)
       
   606 done
       
   607 
   596 
   608 lemma inj_fmap_eq_iff:
   597 lemma inj_fmap_eq_iff:
   609   "inj f \<Longrightarrow> (fmap f l = fmap f m) = (l = m)"
   598   "inj f \<Longrightarrow> (fmap f S = fmap f T) = (S = T)"
   610   by (lifting inj_map_eq_iff)
   599   by (lifting inj_map_eq_iff)
   611 
   600 
   612 lemma fmap_funion: "fmap f (a |\<union>| b) = fmap f a |\<union>| fmap f b"
   601 lemma fmap_funion: "fmap f (S |\<union>| T) = fmap f S |\<union>| fmap f T"
   613   by (lifting map_append)
   602   by (lifting map_append)
   614 
   603 
   615 lemma fin_funion:
   604 lemma fin_funion:
   616   "(e |\<in>| l |\<union>| r) = (e |\<in>| l \<or> e |\<in>| r)"
   605   "x |\<in>| S |\<union>| T \<longleftrightarrow> x |\<in>| S \<or> x |\<in>| T"
   617   by (lifting memb_append)
   606   by (lifting memb_append)
   618 
   607 
   619 text {* ffold *}
   608 text {* ffold *}
   620 
   609 
   621 lemma ffold_nil: "ffold f z {||} = z"
   610 lemma ffold_nil: "ffold f z {||} = z"
   629   "\<lbrakk>rsp_fold f; h |\<in>| b\<rbrakk> \<Longrightarrow> ffold f z b = f h (ffold f z (fdelete b h))"
   618   "\<lbrakk>rsp_fold f; h |\<in>| b\<rbrakk> \<Longrightarrow> ffold f z b = f h (ffold f z (fdelete b h))"
   630   by (lifting memb_commute_ffold_raw)
   619   by (lifting memb_commute_ffold_raw)
   631 
   620 
   632 text {* fdelete *}
   621 text {* fdelete *}
   633 
   622 
   634 lemma fin_fdelete: "(x |\<in>| fdelete A a) = (x |\<in>| A \<and> x \<noteq> a)"
   623 lemma fin_fdelete: 
       
   624   shows "x |\<in>| fdelete S y \<longleftrightarrow> x |\<in>| S \<and> x \<noteq> y"
   635   by (lifting memb_delete_raw)
   625   by (lifting memb_delete_raw)
   636 
   626 
   637 lemma fin_fdelete_ident: "a |\<notin>| fdelete A a"
   627 lemma fin_fdelete_ident: 
       
   628   shows "x |\<notin>| fdelete S x"
   638   by (lifting memb_delete_raw_ident)
   629   by (lifting memb_delete_raw_ident)
   639 
   630 
   640 lemma not_memb_fdelete_ident: "b |\<notin>| A \<Longrightarrow> fdelete A b = A"
   631 lemma not_memb_fdelete_ident: 
       
   632   shows "x |\<notin>| S \<Longrightarrow> fdelete S x = S"
   641   by (lifting not_memb_delete_raw_ident)
   633   by (lifting not_memb_delete_raw_ident)
   642 
   634 
   643 lemma fset_fdelete_cases:
   635 lemma fset_fdelete_cases:
   644   "X = {||} \<or> (\<exists>a. a |\<in>| X \<and> X = finsert a (fdelete X a))"
   636   shows "S = {||} \<or> (\<exists>x. x |\<in>| S \<and> S = finsert x (fdelete S x))"
   645   by (lifting fset_raw_delete_raw_cases)
   637   by (lifting fset_raw_delete_raw_cases)
   646 
   638 
   647 text {* inter *}
   639 text {* inter *}
   648 
   640 
   649 lemma finter_empty_l: "({||} |\<inter>| r) = {||}"
   641 lemma finter_empty_l: "({||} |\<inter>| S) = {||}"
   650   by (lifting finter_raw.simps(1))
   642   by (lifting finter_raw.simps(1))
   651 
   643 
   652 lemma finter_empty_r: "(l |\<inter>| {||}) = {||}"
   644 lemma finter_empty_r: "(S |\<inter>| {||}) = {||}"
   653   by (lifting finter_raw_empty)
   645   by (lifting finter_raw_empty)
   654 
   646 
   655 lemma finter_finsert:
   647 lemma finter_finsert:
   656   "(finsert h t |\<inter>| l) = (if h |\<in>| l then finsert h (t |\<inter>| l) else t |\<inter>| l)"
   648   "finsert x S |\<inter>| T = (if x |\<in>| T then finsert x (S |\<inter>| T) else S |\<inter>| T)"
   657   by (lifting finter_raw.simps(2))
   649   by (lifting finter_raw.simps(2))
   658 
   650 
   659 lemma fin_finter:
   651 lemma fin_finter:
   660   "(e |\<in>| (l |\<inter>| r)) = (e |\<in>| l \<and> e |\<in>| r)"
   652   "x |\<in>| (S |\<inter>| T) \<longleftrightarrow> x |\<in>| S \<and> x |\<in>| T"
   661   by (lifting memb_finter_raw)
   653   by (lifting memb_finter_raw)
   662 
   654 
   663 lemma expand_fset_eq:
   655 lemma expand_fset_eq:
   664   "(xs = ys) = (\<forall>x. (x |\<in>| xs) = (x |\<in>| ys))"
   656   "(S = T) = (\<forall>x. (x |\<in>| S) = (x |\<in>| T))"
   665   by (lifting list_eq.simps[simplified memb_def[symmetric]])
   657   by (lifting list_eq.simps[simplified memb_def[symmetric]])
   666 
   658 
   667 
   659 
   668 ML {*
   660 ML {*
   669 fun dest_fsetT (Type ("FSet.fset", [T])) = T
   661 fun dest_fsetT (Type ("FSet.fset", [T])) = T