
bulwahn@43146: (* Author: Florian Haftmann, TU Muenchen *)
bulwahn@43146: 
bulwahn@43146: header {* Canonical implementation of sets by distinct lists *}
bulwahn@43146: 
bulwahn@43146: theory Dlist_Cset
haftmann@44558: imports Dlist Cset
bulwahn@43146: begin
bulwahn@43146: 
bulwahn@43146: definition Set :: "'a dlist \<Rightarrow> 'a Cset.set" where
Andreas@43971:   "Set dxs = Cset.set (list_of_dlist dxs)"
bulwahn@43146: 
bulwahn@43146: definition Coset :: "'a dlist \<Rightarrow> 'a Cset.set" where
haftmann@44558:   "Coset dxs = Cset.coset (list_of_dlist dxs)"
bulwahn@43146: 
bulwahn@43146: code_datatype Set Coset
bulwahn@43146: 
bulwahn@43146: lemma Set_Dlist [simp]:
haftmann@44558:   "Set (Dlist xs) = Cset.set xs"
bulwahn@43146:   by (rule Cset.set_eqI) (simp add: Set_def)
bulwahn@43146: 
bulwahn@43146: lemma Coset_Dlist [simp]:
haftmann@44558:   "Coset (Dlist xs) = Cset.coset xs"
bulwahn@43146:   by (rule Cset.set_eqI) (simp add: Coset_def)
bulwahn@43146: 
bulwahn@43146: lemma member_Set [simp]:
bulwahn@43146:   "Cset.member (Set dxs) = List.member (list_of_dlist dxs)"
haftmann@44558:   by (simp add: Set_def fun_eq_iff List.member_def)
bulwahn@43146: 
bulwahn@43146: lemma member_Coset [simp]:
bulwahn@43146:   "Cset.member (Coset dxs) = Not \<circ> List.member (list_of_dlist dxs)"
haftmann@44558:   by (simp add: Coset_def fun_eq_iff List.member_def)
bulwahn@43146: 
bulwahn@43146: lemma Set_dlist_of_list [code]:
Andreas@43971:   "Cset.set xs = Set (dlist_of_list xs)"
bulwahn@43146:   by (rule Cset.set_eqI) simp
bulwahn@43146: 
bulwahn@43146: lemma Coset_dlist_of_list [code]:
haftmann@44558:   "Cset.coset xs = Coset (dlist_of_list xs)"
bulwahn@43146:   by (rule Cset.set_eqI) simp
bulwahn@43146: 
bulwahn@43146: lemma is_empty_Set [code]:
bulwahn@43146:   "Cset.is_empty (Set dxs) \<longleftrightarrow> Dlist.null dxs"
haftmann@44558:   by (simp add: Dlist.null_def List.null_def Set_def)
bulwahn@43146: 
bulwahn@43146: lemma bot_code [code]:
bulwahn@43146:   "bot = Set Dlist.empty"
bulwahn@43146:   by (simp add: empty_def)
bulwahn@43146: 
bulwahn@43146: lemma top_code [code]:
bulwahn@43146:   "top = Coset Dlist.empty"
haftmann@44558:   by (simp add: empty_def Cset.coset_def)
bulwahn@43146: 
bulwahn@43146: lemma insert_code [code]:
bulwahn@43146:   "Cset.insert x (Set dxs) = Set (Dlist.insert x dxs)"
bulwahn@43146:   "Cset.insert x (Coset dxs) = Coset (Dlist.remove x dxs)"
haftmann@44558:   by (simp_all add: Dlist.insert_def Dlist.remove_def Cset.set_def Cset.coset_def Set_def Coset_def)
bulwahn@43146: 
bulwahn@43146: lemma remove_code [code]:
bulwahn@43146:   "Cset.remove x (Set dxs) = Set (Dlist.remove x dxs)"
bulwahn@43146:   "Cset.remove x (Coset dxs) = Coset (Dlist.insert x dxs)"
haftmann@44558:   by (simp_all add: Dlist.insert_def Dlist.remove_def Cset.set_def Cset.coset_def Set_def Coset_def Compl_insert)
bulwahn@43146: 
bulwahn@43146: lemma member_code [code]:
bulwahn@43146:   "Cset.member (Set dxs) = Dlist.member dxs"
bulwahn@43146:   "Cset.member (Coset dxs) = Not \<circ> Dlist.member dxs"
haftmann@44558:   by (simp_all add: List.member_def member_def fun_eq_iff Dlist.member_def)
bulwahn@43146: 
bulwahn@43146: lemma compl_code [code]:
bulwahn@43146:   "- Set dxs = Coset dxs"
bulwahn@43146:   "- Coset dxs = Set dxs"
haftmann@44558:   by (rule Cset.set_eqI, simp add: fun_eq_iff List.member_def Set_def Coset_def)+
bulwahn@43146: 
bulwahn@43146: lemma map_code [code]:
bulwahn@43146:   "Cset.map f (Set dxs) = Set (Dlist.map f dxs)"
haftmann@44558:   by (rule Cset.set_eqI) (simp add: fun_eq_iff List.member_def Set_def)
bulwahn@43146:   
bulwahn@43146: lemma filter_code [code]:
bulwahn@43146:   "Cset.filter f (Set dxs) = Set (Dlist.filter f dxs)"
haftmann@44558:   by (rule Cset.set_eqI) (simp add: fun_eq_iff List.member_def Set_def)
bulwahn@43146: 
bulwahn@43146: lemma forall_Set [code]:
bulwahn@43146:   "Cset.forall P (Set xs) \<longleftrightarrow> list_all P (list_of_dlist xs)"
haftmann@44558:   by (simp add: Set_def list_all_iff)
bulwahn@43146: 
bulwahn@43146: lemma exists_Set [code]:
bulwahn@43146:   "Cset.exists P (Set xs) \<longleftrightarrow> list_ex P (list_of_dlist xs)"
haftmann@44558:   by (simp add: Set_def list_ex_iff)
bulwahn@43146: 
bulwahn@43146: lemma card_code [code]:
bulwahn@43146:   "Cset.card (Set dxs) = Dlist.length dxs"
haftmann@44558:   by (simp add: length_def Set_def distinct_card)
bulwahn@43146: 
bulwahn@43146: lemma inter_code [code]:
bulwahn@43146:   "inf A (Set xs) = Set (Dlist.filter (Cset.member A) xs)"
bulwahn@43146:   "inf A (Coset xs) = Dlist.foldr Cset.remove xs A"
bulwahn@43146:   by (simp_all only: Set_def Coset_def foldr_def inter_project list_of_dlist_filter)
bulwahn@43146: 
bulwahn@43146: lemma subtract_code [code]:
bulwahn@43146:   "A - Set xs = Dlist.foldr Cset.remove xs A"
bulwahn@43146:   "A - Coset xs = Set (Dlist.filter (Cset.member A) xs)"
bulwahn@43146:   by (simp_all only: Set_def Coset_def foldr_def subtract_remove list_of_dlist_filter)
bulwahn@43146: 
bulwahn@43146: lemma union_code [code]:
bulwahn@43146:   "sup (Set xs) A = Dlist.foldr Cset.insert xs A"
bulwahn@43146:   "sup (Coset xs) A = Coset (Dlist.filter (Not \<circ> Cset.member A) xs)"
bulwahn@43146:   by (simp_all only: Set_def Coset_def foldr_def union_insert list_of_dlist_filter)
bulwahn@43146: 
bulwahn@43146: context complete_lattice
bulwahn@43146: begin
bulwahn@43146: 
bulwahn@43146: lemma Infimum_code [code]:
bulwahn@43146:   "Infimum (Set As) = Dlist.foldr inf As top"
bulwahn@43146:   by (simp only: Set_def Infimum_inf foldr_def inf.commute)
bulwahn@43146: 
bulwahn@43146: lemma Supremum_code [code]:
bulwahn@43146:   "Supremum (Set As) = Dlist.foldr sup As bot"
bulwahn@43146:   by (simp only: Set_def Supremum_sup foldr_def sup.commute)
bulwahn@43146: 
bulwahn@43146: end
bulwahn@43146: 
haftmann@44563: declare Cset.single_code [code]
Andreas@43971: 
Andreas@43971: lemma bind_set [code]:
haftmann@44558:   "Cset.bind (Dlist_Cset.Set xs) f = fold (sup \<circ> f) (list_of_dlist xs) Cset.empty"
haftmann@44558:   by (simp add: Cset.bind_set Set_def)
Andreas@43971: hide_fact (open) bind_set
Andreas@43971: 
Andreas@43971: lemma pred_of_cset_set [code]:
Andreas@43971:   "pred_of_cset (Dlist_Cset.Set xs) = foldr sup (map Predicate.single (list_of_dlist xs)) bot"
haftmann@44558:   by (simp add: Cset.pred_of_cset_set Dlist_Cset.Set_def)
Andreas@43971: hide_fact (open) pred_of_cset_set
Andreas@43971: 
bulwahn@43146: end




