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




