Cset, Dlist_Cset, List_Cset: restructured
authorhaftmann
Sat, 27 Aug 2011 09:44:45 +0200
changeset 44558 cc878a312673
parent 44557 9ab8c88449a4
child 44559 5a12314dcf30
Cset, Dlist_Cset, List_Cset: restructured
src/HOL/Library/Cset.thy
src/HOL/Library/Dlist_Cset.thy
src/HOL/Library/List_Cset.thy
--- a/src/HOL/Library/Cset.thy	Sat Aug 27 09:02:25 2011 +0200
+++ b/src/HOL/Library/Cset.thy	Sat Aug 27 09:44:45 2011 +0200
@@ -152,6 +152,10 @@
   "set xs = Set (List.set xs)"
 hide_const (open) set
 
+definition coset :: "'a list \<Rightarrow> 'a Cset.set" where
+  "coset xs = Set (- List.set xs)"
+hide_const (open) coset
+
 text {* conversion from @{typ "'a Predicate.pred"} *}
 
 definition pred_of_cset :: "'a Cset.set \<Rightarrow> 'a Predicate.pred" where
@@ -200,6 +204,21 @@
   by (simp add: set_def)
 hide_fact (open) set_def
 
+lemma member_set [simp]:
+  "member (Cset.set xs) = (\<lambda>x. x \<in> set xs)"
+  by (simp add: fun_eq_iff member_def)
+hide_fact (open) member_set
+
+lemma set_of_coset [simp]:
+  "set_of (Cset.coset xs) = - set xs"
+  by (simp add: coset_def)
+hide_fact (open) coset_def
+
+lemma member_coset [simp]:
+  "member (Cset.coset xs) = (\<lambda>x. x \<in> - set xs)"
+  by (simp add: fun_eq_iff member_def)
+hide_fact (open) member_coset
+
 lemma set_simps [simp]:
   "Cset.set [] = Cset.empty"
   "Cset.set (x # xs) = insert x (Cset.set xs)"
@@ -268,6 +287,82 @@
   "single a = insert a Cset.empty"
   by (simp add: Cset.single_def)
 
+lemma compl_set [simp]:
+  "- Cset.set xs = Cset.coset xs"
+  by (simp add: Cset.set_def Cset.coset_def)
+
+lemma compl_coset [simp]:
+  "- Cset.coset xs = Cset.set xs"
+  by (simp add: Cset.set_def Cset.coset_def)
+
+lemma member_cset_of:
+  "member = set_of"
+  by (rule ext)+ (simp add: member_def mem_def)
+
+lemma inter_project:
+  "inf A (Cset.set xs) = Cset.set (List.filter (Cset.member A) xs)"
+  "inf A (Cset.coset xs) = foldr Cset.remove xs A"
+proof -
+  show "inf A (Cset.set xs) = Cset.set (List.filter (member A) xs)"
+    by (simp add: inter project_def Cset.set_def member_def)
+  have *: "\<And>x::'a. Cset.remove = (\<lambda>x. Set \<circ> More_Set.remove x \<circ> member)"
+    by (simp add: fun_eq_iff More_Set.remove_def member_cset_of)
+  have "member \<circ> fold (\<lambda>x. Set \<circ> More_Set.remove x \<circ> member) xs =
+    fold More_Set.remove xs \<circ> member"
+    by (rule fold_commute) (simp add: fun_eq_iff mem_def)
+  then have "fold More_Set.remove xs (member A) = 
+    member (fold (\<lambda>x. Set \<circ> More_Set.remove x \<circ> member) xs A)"
+    by (simp add: fun_eq_iff)
+  then have "inf A (Cset.coset xs) = fold Cset.remove xs A"
+    by (simp add: Diff_eq [symmetric] minus_set * member_cset_of)
+  moreover have "\<And>x y :: 'a. Cset.remove y \<circ> Cset.remove x = Cset.remove x \<circ> Cset.remove y"
+    by (auto simp add: More_Set.remove_def * member_cset_of)
+  ultimately show "inf A (Cset.coset xs) = foldr Cset.remove xs A"
+    by (simp add: foldr_fold)
+qed
+
+lemma subtract_remove:
+  "A - Cset.set xs = foldr Cset.remove xs A"
+  "A - Cset.coset xs = Cset.set (List.filter (member A) xs)"
+  by (simp_all only: diff_eq compl_set compl_coset inter_project)
+
+lemma union_insert:
+  "sup (Cset.set xs) A = foldr Cset.insert xs A"
+  "sup (Cset.coset xs) A = Cset.coset (List.filter (Not \<circ> member A) xs)"
+proof -
+  have *: "\<And>x::'a. Cset.insert = (\<lambda>x. Set \<circ> Set.insert x \<circ> member)"
+    by (simp add: fun_eq_iff member_cset_of)
+  have "member \<circ> fold (\<lambda>x. Set \<circ> Set.insert x \<circ> member) xs =
+    fold Set.insert xs \<circ> member"
+    by (rule fold_commute) (simp add: fun_eq_iff mem_def)
+  then have "fold Set.insert xs (member A) =
+    member (fold (\<lambda>x. Set \<circ> Set.insert x \<circ> member) xs A)"
+    by (simp add: fun_eq_iff)
+  then have "sup (Cset.set xs) A = fold Cset.insert xs A"
+    by (simp add: union_set * member_cset_of)
+  moreover have "\<And>x y :: 'a. Cset.insert y \<circ> Cset.insert x = Cset.insert x \<circ> Cset.insert y"
+    by (auto simp add: * member_cset_of)
+  ultimately show "sup (Cset.set xs) A = foldr Cset.insert xs A"
+    by (simp add: foldr_fold)
+  show "sup (Cset.coset xs) A = Cset.coset (List.filter (Not \<circ> member A) xs)"
+    by (auto simp add: Cset.coset_def member_cset_of mem_def)
+qed
+
+context complete_lattice
+begin
+
+lemma Infimum_inf:
+  "Infimum (Cset.set As) = foldr inf As top"
+  "Infimum (Cset.coset []) = bot"
+  by (simp_all add: Inf_set_foldr)
+
+lemma Supremum_sup:
+  "Supremum (Cset.set As) = foldr sup As bot"
+  "Supremum (Cset.coset []) = top"
+  by (simp_all add: Sup_set_foldr)
+
+end
+
 lemma of_pred_code [code]:
   "of_pred (Predicate.Seq f) = (case f () of
      Predicate.Empty \<Rightarrow> Cset.empty
@@ -287,6 +382,22 @@
   apply simp_all
   done
 
+lemma bind_set:
+  "Cset.bind (Cset.set xs) f = fold (sup \<circ> f) xs (Cset.set [])"
+  by (simp add: Cset.bind_def SUPR_set_fold)
+hide_fact (open) bind_set
+
+lemma pred_of_cset_set:
+  "pred_of_cset (Cset.set xs) = foldr sup (List.map Predicate.single xs) bot"
+proof -
+  have "pred_of_cset (Cset.set xs) = Predicate.Pred (\<lambda>x. x \<in> set xs)"
+    by (simp add: Cset.pred_of_cset_def member_set)
+  moreover have "foldr sup (List.map Predicate.single xs) bot = \<dots>"
+    by (induct xs) (auto simp add: bot_pred_def intro: pred_eqI, simp add: mem_def)
+  ultimately show ?thesis by simp
+qed
+hide_fact (open) pred_of_cset_set
+
 no_notation bind (infixl "\<guillemotright>=" 70)
 
 hide_const (open) is_empty insert remove map filter forall exists card
--- a/src/HOL/Library/Dlist_Cset.thy	Sat Aug 27 09:02:25 2011 +0200
+++ b/src/HOL/Library/Dlist_Cset.thy	Sat Aug 27 09:44:45 2011 +0200
@@ -3,66 +3,44 @@
 header {* Canonical implementation of sets by distinct lists *}
 
 theory Dlist_Cset
-imports Dlist List_Cset
+imports Dlist Cset
 begin
 
 definition Set :: "'a dlist \<Rightarrow> 'a Cset.set" where
   "Set dxs = Cset.set (list_of_dlist dxs)"
 
 definition Coset :: "'a dlist \<Rightarrow> 'a Cset.set" where
-  "Coset dxs = List_Cset.coset (list_of_dlist dxs)"
+  "Coset dxs = Cset.coset (list_of_dlist dxs)"
 
 code_datatype Set Coset
 
-declare member_code [code del]
-declare List_Cset.is_empty_set [code del]
-declare List_Cset.empty_set [code del]
-declare List_Cset.UNIV_set [code del]
-declare insert_set [code del]
-declare remove_set [code del]
-declare compl_set [code del]
-declare compl_coset [code del]
-declare map_set [code del]
-declare filter_set [code del]
-declare forall_set [code del]
-declare exists_set [code del]
-declare card_set [code del]
-declare List_Cset.single_set [code del]
-declare List_Cset.bind_set [code del]
-declare List_Cset.pred_of_cset_set [code del]
-declare inter_project [code del]
-declare subtract_remove [code del]
-declare union_insert [code del]
-declare Infimum_inf [code del]
-declare Supremum_sup [code del]
-
 lemma Set_Dlist [simp]:
-  "Set (Dlist xs) = Cset.Set (set xs)"
+  "Set (Dlist xs) = Cset.set xs"
   by (rule Cset.set_eqI) (simp add: Set_def)
 
 lemma Coset_Dlist [simp]:
-  "Coset (Dlist xs) = Cset.Set (- set xs)"
+  "Coset (Dlist xs) = Cset.coset xs"
   by (rule Cset.set_eqI) (simp add: Coset_def)
 
 lemma member_Set [simp]:
   "Cset.member (Set dxs) = List.member (list_of_dlist dxs)"
-  by (simp add: Set_def member_set)
+  by (simp add: Set_def fun_eq_iff List.member_def)
 
 lemma member_Coset [simp]:
   "Cset.member (Coset dxs) = Not \<circ> List.member (list_of_dlist dxs)"
-  by (simp add: Coset_def member_set not_set_compl)
+  by (simp add: Coset_def fun_eq_iff List.member_def)
 
 lemma Set_dlist_of_list [code]:
   "Cset.set xs = Set (dlist_of_list xs)"
   by (rule Cset.set_eqI) simp
 
 lemma Coset_dlist_of_list [code]:
-  "List_Cset.coset xs = Coset (dlist_of_list xs)"
+  "Cset.coset xs = Coset (dlist_of_list xs)"
   by (rule Cset.set_eqI) simp
 
 lemma is_empty_Set [code]:
   "Cset.is_empty (Set dxs) \<longleftrightarrow> Dlist.null dxs"
-  by (simp add: Dlist.null_def List.null_def member_set)
+  by (simp add: Dlist.null_def List.null_def Set_def)
 
 lemma bot_code [code]:
   "bot = Set Dlist.empty"
@@ -70,47 +48,47 @@
 
 lemma top_code [code]:
   "top = Coset Dlist.empty"
-  by (simp add: empty_def)
+  by (simp add: empty_def Cset.coset_def)
 
 lemma insert_code [code]:
   "Cset.insert x (Set dxs) = Set (Dlist.insert x dxs)"
   "Cset.insert x (Coset dxs) = Coset (Dlist.remove x dxs)"
-  by (simp_all add: Dlist.insert_def Dlist.remove_def member_set not_set_compl)
+  by (simp_all add: Dlist.insert_def Dlist.remove_def Cset.set_def Cset.coset_def Set_def Coset_def)
 
 lemma remove_code [code]:
   "Cset.remove x (Set dxs) = Set (Dlist.remove x dxs)"
   "Cset.remove x (Coset dxs) = Coset (Dlist.insert x dxs)"
-  by (auto simp add: Dlist.insert_def Dlist.remove_def member_set not_set_compl)
+  by (simp_all add: Dlist.insert_def Dlist.remove_def Cset.set_def Cset.coset_def Set_def Coset_def Compl_insert)
 
 lemma member_code [code]:
   "Cset.member (Set dxs) = Dlist.member dxs"
   "Cset.member (Coset dxs) = Not \<circ> Dlist.member dxs"
-  by (simp_all add: member_def)
+  by (simp_all add: List.member_def member_def fun_eq_iff Dlist.member_def)
 
 lemma compl_code [code]:
   "- Set dxs = Coset dxs"
   "- Coset dxs = Set dxs"
-  by (rule Cset.set_eqI, simp add: member_set not_set_compl)+
+  by (rule Cset.set_eqI, simp add: fun_eq_iff List.member_def Set_def Coset_def)+
 
 lemma map_code [code]:
   "Cset.map f (Set dxs) = Set (Dlist.map f dxs)"
-  by (rule Cset.set_eqI) (simp add: member_set)
+  by (rule Cset.set_eqI) (simp add: fun_eq_iff List.member_def Set_def)
   
 lemma filter_code [code]:
   "Cset.filter f (Set dxs) = Set (Dlist.filter f dxs)"
-  by (rule Cset.set_eqI) (simp add: member_set)
+  by (rule Cset.set_eqI) (simp add: fun_eq_iff List.member_def Set_def)
 
 lemma forall_Set [code]:
   "Cset.forall P (Set xs) \<longleftrightarrow> list_all P (list_of_dlist xs)"
-  by (simp add: member_set list_all_iff)
+  by (simp add: Set_def list_all_iff)
 
 lemma exists_Set [code]:
   "Cset.exists P (Set xs) \<longleftrightarrow> list_ex P (list_of_dlist xs)"
-  by (simp add: member_set list_ex_iff)
+  by (simp add: Set_def list_ex_iff)
 
 lemma card_code [code]:
   "Cset.card (Set dxs) = Dlist.length dxs"
-  by (simp add: length_def member_set distinct_card)
+  by (simp add: length_def Set_def distinct_card)
 
 lemma inter_code [code]:
   "inf A (Set xs) = Set (Dlist.filter (Cset.member A) xs)"
@@ -143,13 +121,15 @@
 declare Cset.single_code[code]
 
 lemma bind_set [code]:
-  "Cset.bind (Dlist_Cset.Set xs) f = foldl (\<lambda>A x. sup A (f x)) Cset.empty (list_of_dlist xs)"
-by(simp add: List_Cset.bind_set Dlist_Cset.Set_def)
+  "Cset.bind (Dlist_Cset.Set xs) f = fold (sup \<circ> f) (list_of_dlist xs) Cset.empty"
+  by (simp add: Cset.bind_set Set_def)
 hide_fact (open) bind_set
 
 lemma pred_of_cset_set [code]:
   "pred_of_cset (Dlist_Cset.Set xs) = foldr sup (map Predicate.single (list_of_dlist xs)) bot"
-by(simp add: List_Cset.pred_of_cset_set Dlist_Cset.Set_def)
+  by (simp add: Cset.pred_of_cset_set Dlist_Cset.Set_def)
 hide_fact (open) pred_of_cset_set
 
+export_code "Cset._" in Haskell
+
 end
--- a/src/HOL/Library/List_Cset.thy	Sat Aug 27 09:02:25 2011 +0200
+++ b/src/HOL/Library/List_Cset.thy	Sat Aug 27 09:44:45 2011 +0200
@@ -7,28 +7,12 @@
 imports Cset
 begin
 
-declare mem_def [simp]
-declare Cset.set_code [code del]
-
-definition coset :: "'a list \<Rightarrow> 'a Cset.set" where
-  "coset xs = Set (- set xs)"
-hide_const (open) coset
-
-lemma set_of_coset [simp]:
-  "set_of (List_Cset.coset xs) = - set xs"
-  by (simp add: coset_def)
-
-lemma member_coset [simp]:
-  "member (List_Cset.coset xs) = (\<lambda>x. x \<in> - set xs)"
-  by (simp add: coset_def fun_eq_iff)
-hide_fact (open) member_coset
-
-code_datatype Cset.set List_Cset.coset
+code_datatype Cset.set Cset.coset
 
 lemma member_code [code]:
   "member (Cset.set xs) = List.member xs"
-  "member (List_Cset.coset xs) = Not \<circ> List.member xs"
-  by (simp_all add: fun_eq_iff member_def fun_Compl_def member_set)
+  "member (Cset.coset xs) = Not \<circ> List.member xs"
+  by (simp_all add: fun_eq_iff List.member_def)
 
 definition (in term_syntax)
   setify :: "'a\<Colon>typerep list \<times> (unit \<Rightarrow> Code_Evaluation.term)
@@ -60,24 +44,27 @@
 
 lemma empty_set [code]:
   "Cset.empty = Cset.set []"
-  by (simp add: set_def)
+  by simp
 hide_fact (open) empty_set
 
 lemma UNIV_set [code]:
-  "top = List_Cset.coset []"
-  by (simp add: coset_def)
+  "top = Cset.coset []"
+  by (simp add: Cset.coset_def)
 hide_fact (open) UNIV_set
 
 lemma remove_set [code]:
   "Cset.remove x (Cset.set xs) = Cset.set (removeAll x xs)"
-  "Cset.remove x (List_Cset.coset xs) = List_Cset.coset (List.insert x xs)"
-by (simp_all add: Cset.set_def coset_def)
-  (metis List.set_insert More_Set.remove_def remove_set_compl)
+  "Cset.remove x (Cset.coset xs) = Cset.coset (List.insert x xs)"
+  by (simp_all add: Cset.set_def Cset.coset_def Compl_insert)
 
 lemma insert_set [code]:
   "Cset.insert x (Cset.set xs) = Cset.set (List.insert x xs)"
-  "Cset.insert x (List_Cset.coset xs) = List_Cset.coset (removeAll x xs)"
-  by (simp_all add: Cset.set_def coset_def)
+  "Cset.insert x (Cset.coset xs) = Cset.coset (removeAll x xs)"
+  by (simp_all add: Cset.set_def Cset.coset_def)
+
+declare compl_set [code]
+declare compl_coset [code]
+declare subtract_remove [cpde]
 
 lemma map_set [code]:
   "Cset.map f (Cset.set xs) = Cset.set (remdups (List.map f xs))"
@@ -103,26 +90,11 @@
   then show ?thesis by (simp add: Cset.set_def)
 qed
 
-lemma compl_set [simp, code]:
-  "- Cset.set xs = List_Cset.coset xs"
-  by (simp add: Cset.set_def coset_def)
-
-lemma compl_coset [simp, code]:
-  "- List_Cset.coset xs = Cset.set xs"
-  by (simp add: Cset.set_def coset_def)
-
 context complete_lattice
 begin
 
-lemma Infimum_inf [code]:
-  "Infimum (Cset.set As) = foldr inf As top"
-  "Infimum (List_Cset.coset []) = bot"
-  by (simp_all add: Inf_set_foldr)
-
-lemma Supremum_sup [code]:
-  "Supremum (Cset.set As) = foldr sup As bot"
-  "Supremum (List_Cset.coset []) = top"
-  by (simp_all add: Sup_set_foldr)
+declare Infimum_inf [code]
+declare Supremum_sup [code]
 
 end
 
@@ -132,20 +104,8 @@
 by(simp add: Cset.single_code)
 hide_fact (open) single_set
 
-lemma bind_set [code]:
-  "Cset.bind (Cset.set xs) f = fold (sup \<circ> f) xs (Cset.set [])"
-  by (simp add: Cset.bind_def SUPR_set_fold)
-
-lemma pred_of_cset_set [code]:
-  "pred_of_cset (Cset.set xs) = foldr sup (map Predicate.single xs) bot"
-proof -
-  have "pred_of_cset (Cset.set xs) = Predicate.Pred (\<lambda>x. x \<in> set xs)"
-    by (simp add: Cset.pred_of_cset_def member_code member_set)
-  moreover have "foldr sup (map Predicate.single xs) bot = \<dots>"
-    by (induct xs) (auto simp add: bot_pred_def simp del: mem_def intro: pred_eqI, simp)
-  ultimately show ?thesis by simp
-qed
-hide_fact (open) pred_of_cset_set
+declare Cset.bind_set [code]
+declare Cset.pred_of_cset_set [code]
 
 
 subsection {* Derived operations *}
@@ -165,7 +125,7 @@
   "HOL.equal A B \<longleftrightarrow> A \<le> B \<and> B \<le> (A :: 'a Cset.set)"
 
 instance proof
-qed (simp add: equal_set_def set_eq [symmetric] Cset.set_eq_iff fun_eq_iff member_def)
+qed (auto simp add: equal_set_def Cset.set_eq_iff Cset.member_def fun_eq_iff mem_def)
 
 end
 
@@ -176,59 +136,7 @@
 
 subsection {* Functorial operations *}
 
-lemma member_cset_of:
-  "member = set_of"
-  by (rule ext)+ (simp add: member_def)
-
-lemma inter_project [code]:
-  "inf A (Cset.set xs) = Cset.set (List.filter (Cset.member A) xs)"
-  "inf A (List_Cset.coset xs) = foldr Cset.remove xs A"
-proof -
-  show "inf A (Cset.set xs) = Cset.set (List.filter (member A) xs)"
-    by (simp add: inter project_def Cset.set_def member_cset_of)
-  have *: "\<And>x::'a. Cset.remove = (\<lambda>x. Set \<circ> More_Set.remove x \<circ> member)"
-    by (simp add: fun_eq_iff More_Set.remove_def member_cset_of)
-  have "member \<circ> fold (\<lambda>x. Set \<circ> More_Set.remove x \<circ> member) xs =
-    fold More_Set.remove xs \<circ> member"
-    by (rule fold_commute) (simp add: fun_eq_iff)
-  then have "fold More_Set.remove xs (member A) = 
-    member (fold (\<lambda>x. Set \<circ> More_Set.remove x \<circ> member) xs A)"
-    by (simp add: fun_eq_iff)
-  then have "inf A (List_Cset.coset xs) = fold Cset.remove xs A"
-    by (simp add: Diff_eq [symmetric] minus_set * member_cset_of)
-  moreover have "\<And>x y :: 'a. Cset.remove y \<circ> Cset.remove x = Cset.remove x \<circ> Cset.remove y"
-    by (auto simp add: More_Set.remove_def * member_cset_of intro: ext)
-  ultimately show "inf A (List_Cset.coset xs) = foldr Cset.remove xs A"
-    by (simp add: foldr_fold)
-qed
-
-lemma subtract_remove [code]:
-  "A - Cset.set xs = foldr Cset.remove xs A"
-  "A - List_Cset.coset xs = Cset.set (List.filter (member A) xs)"
-  by (simp_all only: diff_eq compl_set compl_coset inter_project)
-
-lemma union_insert [code]:
-  "sup (Cset.set xs) A = foldr Cset.insert xs A"
-  "sup (List_Cset.coset xs) A = List_Cset.coset (List.filter (Not \<circ> member A) xs)"
-proof -
-  have *: "\<And>x::'a. Cset.insert = (\<lambda>x. Set \<circ> Set.insert x \<circ> member)"
-    by (simp add: fun_eq_iff member_cset_of)
-  have "member \<circ> fold (\<lambda>x. Set \<circ> Set.insert x \<circ> member) xs =
-    fold Set.insert xs \<circ> member"
-    by (rule fold_commute) (simp add: fun_eq_iff)
-  then have "fold Set.insert xs (member A) =
-    member (fold (\<lambda>x. Set \<circ> Set.insert x \<circ> member) xs A)"
-    by (simp add: fun_eq_iff)
-  then have "sup (Cset.set xs) A = fold Cset.insert xs A"
-    by (simp add: union_set * member_cset_of)
-  moreover have "\<And>x y :: 'a. Cset.insert y \<circ> Cset.insert x = Cset.insert x \<circ> Cset.insert y"
-    by (auto simp add: * member_cset_of intro: ext)
-  ultimately show "sup (Cset.set xs) A = foldr Cset.insert xs A"
-    by (simp add: foldr_fold)
-  show "sup (List_Cset.coset xs) A = List_Cset.coset (List.filter (Not \<circ> member A) xs)"
-    by (auto simp add: coset_def member_cset_of)
-qed
-
-declare mem_def[simp del]
+declare inter_project [code]
+declare union_insert [code]
 
 end