src/HOL/Library/List_Cset.thy
author Andreas Lochbihler
Mon Jul 25 16:55:48 2011 +0200 (2011-07-25)
changeset 43971 892030194015
parent 43307 1a32a953cef1
child 43979 9f27d2bf4087
permissions -rw-r--r--
added operations to Cset with code equations in backing implementations
     1 
     2 (* Author: Florian Haftmann, TU Muenchen *)
     3 
     4 header {* implementation of Cset.sets based on lists *}
     5 
     6 theory List_Cset
     7 imports Cset
     8 begin
     9 
    10 declare mem_def [simp]
    11 declare Cset.set_code [code del]
    12 
    13 definition coset :: "'a list \<Rightarrow> 'a Cset.set" where
    14   "coset xs = Set (- set xs)"
    15 hide_const (open) coset
    16 
    17 lemma member_coset [simp]:
    18   "member (List_Cset.coset xs) = - set xs"
    19   by (simp add: coset_def)
    20 hide_fact (open) member_coset
    21 
    22 code_datatype Cset.set List_Cset.coset
    23 
    24 lemma member_code [code]:
    25   "member (Cset.set xs) = List.member xs"
    26   "member (List_Cset.coset xs) = Not \<circ> List.member xs"
    27   by (simp_all add: fun_eq_iff member_def fun_Compl_def bool_Compl_def)
    28 
    29 lemma member_image_UNIV [simp]:
    30   "member ` UNIV = UNIV"
    31 proof -
    32   have "\<And>A \<Colon> 'a set. \<exists>B \<Colon> 'a Cset.set. A = member B"
    33   proof
    34     fix A :: "'a set"
    35     show "A = member (Set A)" by simp
    36   qed
    37   then show ?thesis by (simp add: image_def)
    38 qed
    39 
    40 definition (in term_syntax)
    41   setify :: "'a\<Colon>typerep list \<times> (unit \<Rightarrow> Code_Evaluation.term)
    42     \<Rightarrow> 'a Cset.set \<times> (unit \<Rightarrow> Code_Evaluation.term)" where
    43   [code_unfold]: "setify xs = Code_Evaluation.valtermify Cset.set {\<cdot>} xs"
    44 
    45 notation fcomp (infixl "\<circ>>" 60)
    46 notation scomp (infixl "\<circ>\<rightarrow>" 60)
    47 
    48 instantiation Cset.set :: (random) random
    49 begin
    50 
    51 definition
    52   "Quickcheck.random i = Quickcheck.random i \<circ>\<rightarrow> (\<lambda>xs. Pair (setify xs))"
    53 
    54 instance ..
    55 
    56 end
    57 
    58 no_notation fcomp (infixl "\<circ>>" 60)
    59 no_notation scomp (infixl "\<circ>\<rightarrow>" 60)
    60 
    61 subsection {* Basic operations *}
    62 
    63 lemma is_empty_set [code]:
    64   "Cset.is_empty (Cset.set xs) \<longleftrightarrow> List.null xs"
    65   by (simp add: is_empty_set null_def)
    66 hide_fact (open) is_empty_set
    67 
    68 lemma empty_set [code]:
    69   "Cset.empty = Cset.set []"
    70   by (simp add: set_def)
    71 hide_fact (open) empty_set
    72 
    73 lemma UNIV_set [code]:
    74   "top = List_Cset.coset []"
    75   by (simp add: coset_def)
    76 hide_fact (open) UNIV_set
    77 
    78 lemma remove_set [code]:
    79   "Cset.remove x (Cset.set xs) = Cset.set (removeAll x xs)"
    80   "Cset.remove x (List_Cset.coset xs) = List_Cset.coset (List.insert x xs)"
    81 by (simp_all add: Cset.set_def coset_def)
    82   (metis List.set_insert More_Set.remove_def remove_set_compl)
    83 
    84 lemma insert_set [code]:
    85   "Cset.insert x (Cset.set xs) = Cset.set (List.insert x xs)"
    86   "Cset.insert x (List_Cset.coset xs) = List_Cset.coset (removeAll x xs)"
    87   by (simp_all add: Cset.set_def coset_def)
    88 
    89 lemma map_set [code]:
    90   "Cset.map f (Cset.set xs) = Cset.set (remdups (List.map f xs))"
    91   by (simp add: Cset.set_def)
    92   
    93 lemma filter_set [code]:
    94   "Cset.filter P (Cset.set xs) = Cset.set (List.filter P xs)"
    95   by (simp add: Cset.set_def project_set)
    96 
    97 lemma forall_set [code]:
    98   "Cset.forall P (Cset.set xs) \<longleftrightarrow> list_all P xs"
    99   by (simp add: Cset.set_def list_all_iff)
   100 
   101 lemma exists_set [code]:
   102   "Cset.exists P (Cset.set xs) \<longleftrightarrow> list_ex P xs"
   103   by (simp add: Cset.set_def list_ex_iff)
   104 
   105 lemma card_set [code]:
   106   "Cset.card (Cset.set xs) = length (remdups xs)"
   107 proof -
   108   have "Finite_Set.card (set (remdups xs)) = length (remdups xs)"
   109     by (rule distinct_card) simp
   110   then show ?thesis by (simp add: Cset.set_def)
   111 qed
   112 
   113 lemma compl_set [simp, code]:
   114   "- Cset.set xs = List_Cset.coset xs"
   115   by (simp add: Cset.set_def coset_def)
   116 
   117 lemma compl_coset [simp, code]:
   118   "- List_Cset.coset xs = Cset.set xs"
   119   by (simp add: Cset.set_def coset_def)
   120 
   121 context complete_lattice
   122 begin
   123 
   124 lemma Infimum_inf [code]:
   125   "Infimum (Cset.set As) = foldr inf As top"
   126   "Infimum (List_Cset.coset []) = bot"
   127   by (simp_all add: Inf_set_foldr Inf_UNIV)
   128 
   129 lemma Supremum_sup [code]:
   130   "Supremum (Cset.set As) = foldr sup As bot"
   131   "Supremum (List_Cset.coset []) = top"
   132   by (simp_all add: Sup_set_foldr Sup_UNIV)
   133 
   134 end
   135 
   136 declare single_code [code del]
   137 lemma single_set [code]:
   138   "Cset.single a = Cset.set [a]"
   139 by(simp add: Cset.single_code)
   140 hide_fact (open) single_set
   141 
   142 lemma bind_set [code]:
   143   "Cset.bind (Cset.set xs) f = foldl (\<lambda>A x. sup A (f x)) (Cset.set []) xs"
   144 proof(rule sym)
   145   show "foldl (\<lambda>A x. sup A (f x)) (Cset.set []) xs = Cset.bind (Cset.set xs) f"
   146     by(induct xs rule: rev_induct)(auto simp add: Cset.bind_def Cset.set_def)
   147 qed
   148 hide_fact (open) bind_set
   149 
   150 lemma pred_of_cset_set [code]:
   151   "pred_of_cset (Cset.set xs) = foldr sup (map Predicate.single xs) bot"
   152 proof -
   153   have "pred_of_cset (Cset.set xs) = Predicate.Pred (\<lambda>x. x \<in> set xs)"
   154     by(auto simp add: Cset.pred_of_cset_def mem_def)
   155   moreover have "foldr sup (map Predicate.single xs) bot = \<dots>"
   156     by(induct xs)(auto simp add: bot_pred_def simp del: mem_def intro: pred_eqI, simp)
   157   ultimately show ?thesis by(simp)
   158 qed
   159 hide_fact (open) pred_of_cset_set
   160 
   161 subsection {* Derived operations *}
   162 
   163 lemma subset_eq_forall [code]:
   164   "A \<le> B \<longleftrightarrow> Cset.forall (member B) A"
   165   by (simp add: subset_eq)
   166 
   167 lemma subset_subset_eq [code]:
   168   "A < B \<longleftrightarrow> A \<le> B \<and> \<not> B \<le> (A :: 'a Cset.set)"
   169   by (fact less_le_not_le)
   170 
   171 instantiation Cset.set :: (type) equal
   172 begin
   173 
   174 definition [code]:
   175   "HOL.equal A B \<longleftrightarrow> A \<le> B \<and> B \<le> (A :: 'a Cset.set)"
   176 
   177 instance proof
   178 qed (simp add: equal_set_def set_eq [symmetric] Cset.set_eq_iff)
   179 
   180 end
   181 
   182 lemma [code nbe]:
   183   "HOL.equal (A :: 'a Cset.set) A \<longleftrightarrow> True"
   184   by (fact equal_refl)
   185 
   186 
   187 subsection {* Functorial operations *}
   188 
   189 lemma inter_project [code]:
   190   "inf A (Cset.set xs) = Cset.set (List.filter (Cset.member A) xs)"
   191   "inf A (List_Cset.coset xs) = foldr Cset.remove xs A"
   192 proof -
   193   show "inf A (Cset.set xs) = Cset.set (List.filter (member A) xs)"
   194     by (simp add: inter project_def Cset.set_def)
   195   have *: "\<And>x::'a. Cset.remove = (\<lambda>x. Set \<circ> More_Set.remove x \<circ> member)"
   196     by (simp add: fun_eq_iff More_Set.remove_def)
   197   have "member \<circ> fold (\<lambda>x. Set \<circ> More_Set.remove x \<circ> member) xs =
   198     fold More_Set.remove xs \<circ> member"
   199     by (rule fold_commute) (simp add: fun_eq_iff)
   200   then have "fold More_Set.remove xs (member A) = 
   201     member (fold (\<lambda>x. Set \<circ> More_Set.remove x \<circ> member) xs A)"
   202     by (simp add: fun_eq_iff)
   203   then have "inf A (List_Cset.coset xs) = fold Cset.remove xs A"
   204     by (simp add: Diff_eq [symmetric] minus_set *)
   205   moreover have "\<And>x y :: 'a. Cset.remove y \<circ> Cset.remove x = Cset.remove x \<circ> Cset.remove y"
   206     by (auto simp add: More_Set.remove_def * intro: ext)
   207   ultimately show "inf A (List_Cset.coset xs) = foldr Cset.remove xs A"
   208     by (simp add: foldr_fold)
   209 qed
   210 
   211 lemma subtract_remove [code]:
   212   "A - Cset.set xs = foldr Cset.remove xs A"
   213   "A - List_Cset.coset xs = Cset.set (List.filter (member A) xs)"
   214   by (simp_all only: diff_eq compl_set compl_coset inter_project)
   215 
   216 lemma union_insert [code]:
   217   "sup (Cset.set xs) A = foldr Cset.insert xs A"
   218   "sup (List_Cset.coset xs) A = List_Cset.coset (List.filter (Not \<circ> member A) xs)"
   219 proof -
   220   have *: "\<And>x::'a. Cset.insert = (\<lambda>x. Set \<circ> Set.insert x \<circ> member)"
   221     by (simp add: fun_eq_iff)
   222   have "member \<circ> fold (\<lambda>x. Set \<circ> Set.insert x \<circ> member) xs =
   223     fold Set.insert xs \<circ> member"
   224     by (rule fold_commute) (simp add: fun_eq_iff)
   225   then have "fold Set.insert xs (member A) =
   226     member (fold (\<lambda>x. Set \<circ> Set.insert x \<circ> member) xs A)"
   227     by (simp add: fun_eq_iff)
   228   then have "sup (Cset.set xs) A = fold Cset.insert xs A"
   229     by (simp add: union_set *)
   230   moreover have "\<And>x y :: 'a. Cset.insert y \<circ> Cset.insert x = Cset.insert x \<circ> Cset.insert y"
   231     by (auto simp add: * intro: ext)
   232   ultimately show "sup (Cset.set xs) A = foldr Cset.insert xs A"
   233     by (simp add: foldr_fold)
   234   show "sup (List_Cset.coset xs) A = List_Cset.coset (List.filter (Not \<circ> member A) xs)"
   235     by (auto simp add: coset_def)
   236 qed
   237 
   238 declare mem_def[simp del]
   239 
   240 end