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