src/HOL/Library/Cset.thy
changeset 44842 892030194015
parent 44082 93b1183e43e5
child 45426 da75ffe3d988
equal deleted inserted replaced
44837:bb11faa6a79e 44842:892030194015
    84 end
    84 end
    85 
    85 
    86 
    86 
    87 subsection {* Basic operations *}
    87 subsection {* Basic operations *}
    88 
    88 
       
    89 abbreviation empty :: "'a Cset.set" where "empty \<equiv> bot"
       
    90 hide_const (open) empty
       
    91 
       
    92 abbreviation UNIV :: "'a Cset.set" where "UNIV \<equiv> top"
       
    93 hide_const (open) UNIV
       
    94 
    89 definition is_empty :: "'a Cset.set \<Rightarrow> bool" where
    95 definition is_empty :: "'a Cset.set \<Rightarrow> bool" where
    90   [simp]: "is_empty A \<longleftrightarrow> More_Set.is_empty (member A)"
    96   [simp]: "is_empty A \<longleftrightarrow> More_Set.is_empty (member A)"
    91 
    97 
    92 definition insert :: "'a \<Rightarrow> 'a Cset.set \<Rightarrow> 'a Cset.set" where
    98 definition insert :: "'a \<Rightarrow> 'a Cset.set \<Rightarrow> 'a Cset.set" where
    93   [simp]: "insert x A = Set (Set.insert x (member A))"
    99   [simp]: "insert x A = Set (Set.insert x (member A))"
   122 definition Supremum :: "'a Cset.set \<Rightarrow> 'a" where
   128 definition Supremum :: "'a Cset.set \<Rightarrow> 'a" where
   123   [simp]: "Supremum A = Sup (member A)"
   129   [simp]: "Supremum A = Sup (member A)"
   124 
   130 
   125 end
   131 end
   126 
   132 
       
   133 subsection {* More operations *}
       
   134 
       
   135 text {* conversion from @{typ "'a list"} *}
       
   136 
       
   137 definition set :: "'a list \<Rightarrow> 'a Cset.set" where
       
   138   "set xs = Set (List.set xs)"
       
   139 hide_const (open) set
       
   140 
       
   141 text {* conversion from @{typ "'a Predicate.pred"} *}
       
   142 
       
   143 definition pred_of_cset :: "'a Cset.set \<Rightarrow> 'a Predicate.pred"
       
   144 where [code del]: "pred_of_cset = Predicate.Pred \<circ> Cset.member"
       
   145 
       
   146 definition of_pred :: "'a Predicate.pred \<Rightarrow> 'a Cset.set"
       
   147 where "of_pred = Cset.Set \<circ> Predicate.eval"
       
   148 
       
   149 definition of_seq :: "'a Predicate.seq \<Rightarrow> 'a Cset.set"
       
   150 where "of_seq = of_pred \<circ> Predicate.pred_of_seq"
       
   151 
       
   152 text {* monad operations *}
       
   153 
       
   154 definition single :: "'a \<Rightarrow> 'a Cset.set" where
       
   155   "single a = Set {a}"
       
   156 
       
   157 definition bind :: "'a Cset.set \<Rightarrow> ('a \<Rightarrow> 'b Cset.set) \<Rightarrow> 'b Cset.set"
       
   158                 (infixl "\<guillemotright>=" 70)
       
   159   where "A \<guillemotright>= f = Set (\<Union>x \<in> member A. member (f x))"
   127 
   160 
   128 subsection {* Simplified simprules *}
   161 subsection {* Simplified simprules *}
       
   162 
       
   163 lemma empty_simp [simp]: "member Cset.empty = {}"
       
   164   by(simp)
       
   165 
       
   166 lemma UNIV_simp [simp]: "member Cset.UNIV = UNIV"
       
   167   by simp
   129 
   168 
   130 lemma is_empty_simp [simp]:
   169 lemma is_empty_simp [simp]:
   131   "is_empty A \<longleftrightarrow> member A = {}"
   170   "is_empty A \<longleftrightarrow> member A = {}"
   132   by (simp add: More_Set.is_empty_def)
   171   by (simp add: More_Set.is_empty_def)
   133 declare is_empty_def [simp del]
   172 declare is_empty_def [simp del]
   140 lemma filter_simp [simp]:
   179 lemma filter_simp [simp]:
   141   "filter P A = Set {x \<in> member A. P x}"
   180   "filter P A = Set {x \<in> member A. P x}"
   142   by (simp add: More_Set.project_def)
   181   by (simp add: More_Set.project_def)
   143 declare filter_def [simp del]
   182 declare filter_def [simp del]
   144 
   183 
       
   184 lemma member_set [simp]:
       
   185   "member (Cset.set xs) = set xs"
       
   186   by (simp add: set_def)
       
   187 hide_fact (open) member_set set_def
       
   188 
       
   189 lemma set_simps [simp]:
       
   190   "Cset.set [] = Cset.empty"
       
   191   "Cset.set (x # xs) = insert x (Cset.set xs)"
       
   192 by(simp_all add: Cset.set_def)
       
   193 
       
   194 lemma member_SUPR [simp]:
       
   195   "member (SUPR A f) = SUPR A (member \<circ> f)"
       
   196 unfolding SUPR_def by simp
       
   197 
       
   198 lemma member_bind [simp]:
       
   199   "member (P \<guillemotright>= f) = member (SUPR (member P) f)"
       
   200 by (simp add: bind_def Cset.set_eq_iff)
       
   201 
       
   202 lemma member_single [simp]:
       
   203   "member (single a) = {a}"
       
   204 by(simp add: single_def)
       
   205 
       
   206 lemma single_sup_simps [simp]:
       
   207   shows single_sup: "sup (single a) A = insert a A"
       
   208   and sup_single: "sup A (single a) = insert a A"
       
   209 by(auto simp add: Cset.set_eq_iff)
       
   210 
       
   211 lemma single_bind [simp]:
       
   212   "single a \<guillemotright>= B = B a"
       
   213 by(simp add: bind_def single_def)
       
   214 
       
   215 lemma bind_bind:
       
   216   "(A \<guillemotright>= B) \<guillemotright>= C = A \<guillemotright>= (\<lambda>x. B x \<guillemotright>= C)"
       
   217 by(simp add: bind_def)
       
   218 
       
   219 lemma bind_single [simp]:
       
   220   "A \<guillemotright>= single = A"
       
   221 by(simp add: bind_def single_def)
       
   222 
       
   223 lemma bind_const: "A \<guillemotright>= (\<lambda>_. B) = (if Cset.is_empty A then Cset.empty else B)"
       
   224 by(auto simp add: Cset.set_eq_iff)
       
   225 
       
   226 lemma empty_bind [simp]:
       
   227   "Cset.empty \<guillemotright>= f = Cset.empty"
       
   228 by(simp add: Cset.set_eq_iff)
       
   229 
       
   230 lemma member_of_pred [simp]:
       
   231   "member (of_pred P) = {x. Predicate.eval P x}"
       
   232 by(simp add: of_pred_def Collect_def)
       
   233 
       
   234 lemma member_of_seq [simp]:
       
   235   "member (of_seq xq) = {x. Predicate.member xq x}"
       
   236 by(simp add: of_seq_def eval_member)
       
   237 
       
   238 lemma eval_pred_of_cset [simp]: 
       
   239   "Predicate.eval (pred_of_cset A) = Cset.member A"
       
   240 by(simp add: pred_of_cset_def)
       
   241 
       
   242 subsection {* Default implementations *}
       
   243 
       
   244 lemma set_code [code]:
       
   245   "Cset.set = foldl (\<lambda>A x. insert x A) Cset.empty"
       
   246 proof(rule ext, rule Cset.set_eqI)
       
   247   fix xs
       
   248   show "member (Cset.set xs) = member (foldl (\<lambda>A x. insert x A) Cset.empty xs)"
       
   249     by(induct xs rule: rev_induct)(simp_all)
       
   250 qed
       
   251 
       
   252 lemma single_code [code]:
       
   253   "single a = insert a Cset.empty"
       
   254 by(simp add: Cset.single_def)
       
   255 
       
   256 lemma of_pred_code [code]:
       
   257   "of_pred (Predicate.Seq f) = (case f () of
       
   258      Predicate.Empty \<Rightarrow> Cset.empty
       
   259    | Predicate.Insert x P \<Rightarrow> Cset.insert x (of_pred P)
       
   260    | Predicate.Join P xq \<Rightarrow> sup (of_pred P) (of_seq xq))"
       
   261 by(auto split: seq.split 
       
   262         simp add: Predicate.Seq_def of_pred_def eval_member Cset.set_eq_iff)
       
   263 
       
   264 lemma of_seq_code [code]:
       
   265   "of_seq Predicate.Empty = Cset.empty"
       
   266   "of_seq (Predicate.Insert x P) = Cset.insert x (of_pred P)"
       
   267   "of_seq (Predicate.Join P xq) = sup (of_pred P) (of_seq xq)"
       
   268 by(auto simp add: of_seq_def of_pred_def Cset.set_eq_iff)
       
   269 
   145 declare mem_def [simp del]
   270 declare mem_def [simp del]
   146 
   271 
       
   272 no_notation bind (infixl "\<guillemotright>=" 70)
   147 
   273 
   148 hide_const (open) is_empty insert remove map filter forall exists card
   274 hide_const (open) is_empty insert remove map filter forall exists card
   149   Inter Union
   275   Inter Union bind single of_pred of_seq
   150 
   276 
   151 end
   277 hide_fact (open) set_def pred_of_cset_def of_pred_def of_seq_def single_def 
       
   278   bind_def empty_simp UNIV_simp member_set set_simps member_SUPR member_bind 
       
   279   member_single single_sup_simps single_sup sup_single single_bind
       
   280   bind_bind bind_single bind_const empty_bind member_of_pred member_of_seq
       
   281   eval_pred_of_cset set_code single_code of_pred_code of_seq_code
       
   282 
       
   283 end