added List_Set and Code_Set theories
authorhaftmann
Thu, 25 Jun 2009 17:07:18 +0200
changeset 31807039893a9a77d
parent 31804 2f0adf64985b
child 31808 235b12db0cf3
added List_Set and Code_Set theories
src/HOL/IsaMakefile
src/HOL/Library/Code_Set.thy
src/HOL/Library/Library.thy
src/HOL/Library/List_Set.thy
src/HOL/ex/Codegenerator_Candidates.thy
     1.1 --- a/src/HOL/IsaMakefile	Thu Jun 25 15:42:36 2009 +0200
     1.2 +++ b/src/HOL/IsaMakefile	Thu Jun 25 17:07:18 2009 +0200
     1.3 @@ -319,7 +319,7 @@
     1.4    Library/Abstract_Rat.thy \
     1.5    Library/BigO.thy Library/ContNotDenum.thy Library/Efficient_Nat.thy	\
     1.6    Library/Euclidean_Space.thy Library/Sum_Of_Squares.thy Library/positivstellensatz.ML	\
     1.7 -  Library/Convex_Euclidean_Space.thy \
     1.8 +  Library/Code_Set.thy  Library/Convex_Euclidean_Space.thy \
     1.9    Library/sum_of_squares.ML Library/Glbs.thy Library/normarith.ML \
    1.10    Library/Executable_Set.thy Library/Infinite_Set.thy			\
    1.11    Library/FuncSet.thy Library/Permutations.thy Library/Determinants.thy\
    1.12 @@ -329,7 +329,7 @@
    1.13    Library/Fundamental_Theorem_Algebra.thy \
    1.14    Library/Inner_Product.thy Library/Lattice_Syntax.thy \
    1.15    Library/Legacy_GCD.thy \
    1.16 -  Library/Library.thy Library/List_Prefix.thy Library/State_Monad.thy	\
    1.17 +  Library/Library.thy Library/List_Prefix.thy Library/List_Set.thy Library/State_Monad.thy	\
    1.18    Library/Nat_Int_Bij.thy Library/Multiset.thy Library/Permutation.thy	\
    1.19    Library/Primes.thy Library/Pocklington.thy Library/Quotient.thy	\
    1.20    Library/Quicksort.thy Library/Nat_Infinity.thy Library/Word.thy	\
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/src/HOL/Library/Code_Set.thy	Thu Jun 25 17:07:18 2009 +0200
     2.3 @@ -0,0 +1,169 @@
     2.4 +
     2.5 +(* Author: Florian Haftmann, TU Muenchen *)
     2.6 +
     2.7 +header {* Executable finite sets *}
     2.8 +
     2.9 +theory Code_Set
    2.10 +imports List_Set
    2.11 +begin
    2.12 +
    2.13 +lemma foldl_apply_inv:
    2.14 +  assumes "\<And>x. g (h x) = x"
    2.15 +  shows "foldl f (g s) xs = g (foldl (\<lambda>s x. h (f (g s) x)) s xs)"
    2.16 +  by (rule sym, induct xs arbitrary: s) (simp_all add: assms)
    2.17 +
    2.18 +subsection {* Lifting *}
    2.19 +
    2.20 +datatype 'a fset = Fset "'a set"
    2.21 +
    2.22 +primrec member :: "'a fset \<Rightarrow> 'a set" where
    2.23 +  "member (Fset A) = A"
    2.24 +
    2.25 +lemma Fset_member [simp]:
    2.26 +  "Fset (member A) = A"
    2.27 +  by (cases A) simp
    2.28 +
    2.29 +definition Set :: "'a list \<Rightarrow> 'a fset" where
    2.30 +  "Set xs = Fset (set xs)"
    2.31 +
    2.32 +lemma member_Set [simp]:
    2.33 +  "member (Set xs) = set xs"
    2.34 +  by (simp add: Set_def)
    2.35 +
    2.36 +code_datatype Set
    2.37 +
    2.38 +
    2.39 +subsection {* Basic operations *}
    2.40 +
    2.41 +definition is_empty :: "'a fset \<Rightarrow> bool" where
    2.42 +  "is_empty A \<longleftrightarrow> List_Set.is_empty (member A)"
    2.43 +
    2.44 +lemma is_empty_Set [code]:
    2.45 +  "is_empty (Set xs) \<longleftrightarrow> null xs"
    2.46 +  by (simp add: is_empty_def is_empty_set)
    2.47 +
    2.48 +definition empty :: "'a fset" where
    2.49 +  "empty = Fset {}"
    2.50 +
    2.51 +lemma empty_Set [code]:
    2.52 +  "empty = Set []"
    2.53 +  by (simp add: empty_def Set_def)
    2.54 +
    2.55 +definition insert :: "'a \<Rightarrow> 'a fset \<Rightarrow> 'a fset" where
    2.56 +  "insert x A = Fset (Set.insert x (member A))"
    2.57 +
    2.58 +lemma insert_Set [code]:
    2.59 +  "insert x (Set xs) = Set (List_Set.insert x xs)"
    2.60 +  by (simp add: insert_def Set_def insert_set)
    2.61 +
    2.62 +definition remove :: "'a \<Rightarrow> 'a fset \<Rightarrow> 'a fset" where
    2.63 +  "remove x A = Fset (List_Set.remove x (member A))"
    2.64 +
    2.65 +lemma remove_Set [code]:
    2.66 +  "remove x (Set xs) = Set (remove_all x xs)"
    2.67 +  by (simp add: remove_def Set_def remove_set)
    2.68 +
    2.69 +definition map :: "('a \<Rightarrow> 'b) \<Rightarrow> 'a fset \<Rightarrow> 'b fset" where
    2.70 +  "map f A = Fset (image f (member A))"
    2.71 +
    2.72 +lemma map_Set [code]:
    2.73 +  "map f (Set xs) = Set (remdups (List.map f xs))"
    2.74 +  by (simp add: map_def Set_def)
    2.75 +
    2.76 +definition project :: "('a \<Rightarrow> bool) \<Rightarrow> 'a fset \<Rightarrow> 'a fset" where
    2.77 +  "project P A = Fset (List_Set.project P (member A))"
    2.78 +
    2.79 +lemma project_Set [code]:
    2.80 +  "project P (Set xs) = Set (filter P xs)"
    2.81 +  by (simp add: project_def Set_def project_set)
    2.82 +
    2.83 +definition forall :: "('a \<Rightarrow> bool) \<Rightarrow> 'a fset \<Rightarrow> bool" where
    2.84 +  "forall P A \<longleftrightarrow> Ball (member A) P"
    2.85 +
    2.86 +lemma forall_Set [code]:
    2.87 +  "forall P (Set xs) \<longleftrightarrow> list_all P xs"
    2.88 +  by (simp add: forall_def Set_def ball_set)
    2.89 +
    2.90 +definition exists :: "('a \<Rightarrow> bool) \<Rightarrow> 'a fset \<Rightarrow> bool" where
    2.91 +  "exists P A \<longleftrightarrow> Bex (member A) P"
    2.92 +
    2.93 +lemma exists_Set [code]:
    2.94 +  "exists P (Set xs) \<longleftrightarrow> list_ex P xs"
    2.95 +  by (simp add: exists_def Set_def bex_set)
    2.96 +
    2.97 +
    2.98 +subsection {* Functorial operations *}
    2.99 +
   2.100 +definition union :: "'a fset \<Rightarrow> 'a fset \<Rightarrow> 'a fset" where
   2.101 +  "union A B = Fset (member A \<union> member B)"
   2.102 +
   2.103 +lemma union_insert [code]:
   2.104 +  "union (Set xs) A = foldl (\<lambda>A x. insert x A) A xs"
   2.105 +proof -
   2.106 +  have "foldl (\<lambda>A x. Set.insert x A) (member A) xs =
   2.107 +    member (foldl (\<lambda>A x. Fset (Set.insert x (member A))) A xs)"
   2.108 +    by (rule foldl_apply_inv) simp
   2.109 +  then show ?thesis by (simp add: union_def union_set insert_def)
   2.110 +qed
   2.111 +
   2.112 +definition subtract :: "'a fset \<Rightarrow> 'a fset \<Rightarrow> 'a fset" where
   2.113 +  "subtract A B = Fset (member B - member A)"
   2.114 +
   2.115 +lemma subtract_remove [code]:
   2.116 +  "subtract (Set xs) A = foldl (\<lambda>A x. remove x A) A xs"
   2.117 +proof -
   2.118 +  have "foldl (\<lambda>A x. List_Set.remove x A) (member A) xs =
   2.119 +    member (foldl (\<lambda>A x. Fset (List_Set.remove x (member A))) A xs)"
   2.120 +    by (rule foldl_apply_inv) simp
   2.121 +  then show ?thesis by (simp add: subtract_def minus_set remove_def)
   2.122 +qed
   2.123 +
   2.124 +
   2.125 +subsection {* Derived operations *}
   2.126 +
   2.127 +lemma member_exists [code]:
   2.128 +  "member A y \<longleftrightarrow> exists (\<lambda>x. y = x) A"
   2.129 +  by (simp add: exists_def mem_def)
   2.130 +
   2.131 +definition subfset_eq :: "'a fset \<Rightarrow> 'a fset \<Rightarrow> bool" where
   2.132 +  "subfset_eq A B \<longleftrightarrow> member A \<subseteq> member B"
   2.133 +
   2.134 +lemma subfset_eq_forall [code]:
   2.135 +  "subfset_eq A B \<longleftrightarrow> forall (\<lambda>x. member B x) A"
   2.136 +  by (simp add: subfset_eq_def subset_eq forall_def mem_def)
   2.137 +
   2.138 +definition subfset :: "'a fset \<Rightarrow> 'a fset \<Rightarrow> bool" where
   2.139 +  "subfset A B \<longleftrightarrow> member A \<subset> member B"
   2.140 +
   2.141 +lemma subfset_subfset_eq [code]:
   2.142 +  "subfset A B \<longleftrightarrow> subfset_eq A B \<and> \<not> subfset_eq B A"
   2.143 +  by (simp add: subfset_def subfset_eq_def subset)
   2.144 +
   2.145 +lemma eq_fset_subfset_eq [code]:
   2.146 +  "eq_class.eq A B \<longleftrightarrow> subfset_eq A B \<and> subfset_eq B A"
   2.147 +  by (cases A, cases B) (simp add: eq subfset_eq_def set_eq)
   2.148 +
   2.149 +definition inter :: "'a fset \<Rightarrow> 'a fset \<Rightarrow> 'a fset" where
   2.150 +  "inter A B = Fset (List_Set.project (member A) (member B))"
   2.151 +
   2.152 +lemma inter_project [code]:
   2.153 +  "inter A B = project (member A) B"
   2.154 +  by (simp add: inter_def project_def inter)
   2.155 +
   2.156 +
   2.157 +subsection {* Misc operations *}
   2.158 +
   2.159 +lemma size_fset [code]:
   2.160 +  "fset_size f A = 0"
   2.161 +  "size A = 0"
   2.162 +  by (cases A, simp) (cases A, simp)
   2.163 +
   2.164 +lemma fset_case_code [code]:
   2.165 +  "fset_case f A = f (member A)"
   2.166 +  by (cases A) simp
   2.167 +
   2.168 +lemma fset_rec_code [code]:
   2.169 +  "fset_rec f A = f (member A)"
   2.170 +  by (cases A) simp
   2.171 +
   2.172 +end
     3.1 --- a/src/HOL/Library/Library.thy	Thu Jun 25 15:42:36 2009 +0200
     3.2 +++ b/src/HOL/Library/Library.thy	Thu Jun 25 17:07:18 2009 +0200
     3.3 @@ -10,6 +10,7 @@
     3.4    Char_ord
     3.5    Code_Char_chr
     3.6    Code_Integer
     3.7 +  Code_Set
     3.8    Coinductive_List
     3.9    Commutative_Ring
    3.10    Continuity
     4.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     4.2 +++ b/src/HOL/Library/List_Set.thy	Thu Jun 25 17:07:18 2009 +0200
     4.3 @@ -0,0 +1,163 @@
     4.4 +
     4.5 +(* Author: Florian Haftmann, TU Muenchen *)
     4.6 +
     4.7 +header {* Relating (finite) sets and lists *}
     4.8 +
     4.9 +theory List_Set
    4.10 +imports Main
    4.11 +begin
    4.12 +
    4.13 +subsection {* Various additional list functions *}
    4.14 +
    4.15 +definition insert :: "'a \<Rightarrow> 'a list \<Rightarrow> 'a list" where
    4.16 +  "insert x xs = (if x \<in> set xs then xs else x # xs)"
    4.17 +
    4.18 +definition remove_all :: "'a \<Rightarrow> 'a list \<Rightarrow> 'a list" where
    4.19 +  "remove_all x xs = filter (Not o op = x) xs"
    4.20 +
    4.21 +
    4.22 +subsection {* Various additional set functions *}
    4.23 +
    4.24 +definition is_empty :: "'a set \<Rightarrow> bool" where
    4.25 +  "is_empty A \<longleftrightarrow> A = {}"
    4.26 +
    4.27 +definition remove :: "'a \<Rightarrow> 'a set \<Rightarrow> 'a set" where
    4.28 +  "remove x A = A - {x}"
    4.29 +
    4.30 +lemma fun_left_comm_idem_remove:
    4.31 +  "fun_left_comm_idem remove"
    4.32 +proof -
    4.33 +  have rem: "remove = (\<lambda>x A. A - {x})" by (simp add: expand_fun_eq remove_def)
    4.34 +  show ?thesis by (simp only: fun_left_comm_idem_remove rem)
    4.35 +qed
    4.36 +
    4.37 +lemma minus_fold_remove:
    4.38 +  assumes "finite A"
    4.39 +  shows "B - A = fold remove B A"
    4.40 +proof -
    4.41 +  have rem: "remove = (\<lambda>x A. A - {x})" by (simp add: expand_fun_eq remove_def)
    4.42 +  show ?thesis by (simp only: rem assms minus_fold_remove)
    4.43 +qed
    4.44 +
    4.45 +definition project :: "('a \<Rightarrow> bool) \<Rightarrow> 'a set \<Rightarrow> 'a set" where
    4.46 +  "project P A = {a\<in>A. P a}"
    4.47 +
    4.48 +
    4.49 +subsection {* Basic set operations *}
    4.50 +
    4.51 +lemma is_empty_set:
    4.52 +  "is_empty (set xs) \<longleftrightarrow> null xs"
    4.53 +  by (simp add: is_empty_def null_empty)
    4.54 +
    4.55 +lemma ball_set:
    4.56 +  "(\<forall>x\<in>set xs. P x) \<longleftrightarrow> list_all P xs"
    4.57 +  by (rule list_ball_code)
    4.58 +
    4.59 +lemma bex_set:
    4.60 +  "(\<exists>x\<in>set xs. P x) \<longleftrightarrow> list_ex P xs"
    4.61 +  by (rule list_bex_code)
    4.62 +
    4.63 +lemma empty_set:
    4.64 +  "{} = set []"
    4.65 +  by simp
    4.66 +
    4.67 +lemma insert_set:
    4.68 +  "Set.insert x (set xs) = set (insert x xs)"
    4.69 +  by (auto simp add: insert_def)
    4.70 +
    4.71 +lemma remove_set:
    4.72 +  "remove x (set xs) = set (remove_all x xs)"
    4.73 +  by (auto simp add: remove_def remove_all_def)
    4.74 +
    4.75 +lemma image_set:
    4.76 +  "image f (set xs) = set (remdups (map f xs))"
    4.77 +  by simp
    4.78 +
    4.79 +lemma project_set:
    4.80 +  "project P (set xs) = set (filter P xs)"
    4.81 +  by (auto simp add: project_def)
    4.82 +
    4.83 +
    4.84 +subsection {* Functorial set operations *}
    4.85 +
    4.86 +lemma union_set:
    4.87 +  "set xs \<union> A = foldl (\<lambda>A x. Set.insert x A) A xs"
    4.88 +proof -
    4.89 +  interpret fun_left_comm_idem Set.insert
    4.90 +    by (fact fun_left_comm_idem_insert)
    4.91 +  show ?thesis by (simp add: union_fold_insert fold_set)
    4.92 +qed
    4.93 +
    4.94 +lemma minus_set:
    4.95 +  "A - set xs = foldl (\<lambda>A x. remove x A) A xs"
    4.96 +proof -
    4.97 +  interpret fun_left_comm_idem remove
    4.98 +    by (fact fun_left_comm_idem_remove)
    4.99 +  show ?thesis
   4.100 +    by (simp add: minus_fold_remove [of _ A] fold_set)
   4.101 +qed
   4.102 +
   4.103 +lemma Inter_set:
   4.104 +  "Inter (set (A # As)) = foldl (op \<inter>) A As"
   4.105 +proof -
   4.106 +  have "finite (set (A # As))" by simp
   4.107 +  moreover have "fold (op \<inter>) UNIV (set (A # As)) = foldl (\<lambda>y x. x \<inter> y) UNIV (A # As)"
   4.108 +    by (rule fun_left_comm_idem.fold_set, unfold_locales, auto)
   4.109 +  ultimately have "Inter (set (A # As)) = foldl (op \<inter>) UNIV (A # As)"
   4.110 +    by (simp only: Inter_fold_inter Int_commute)
   4.111 +  then show ?thesis by simp
   4.112 +qed
   4.113 +
   4.114 +lemma Union_set:
   4.115 +  "Union (set As) = foldl (op \<union>) {} As"
   4.116 +proof -
   4.117 +  have "fold (op \<union>) {} (set As) = foldl (\<lambda>y x. x \<union> y) {} As"
   4.118 +    by (rule fun_left_comm_idem.fold_set, unfold_locales, auto)
   4.119 +  then show ?thesis
   4.120 +    by (simp only: Union_fold_union finite_set Un_commute)
   4.121 +qed
   4.122 +
   4.123 +lemma INTER_set:
   4.124 +  "INTER (set (A # As)) f = foldl (\<lambda>B A. f A \<inter> B) (f A) As"
   4.125 +proof -
   4.126 +  have "finite (set (A # As))" by simp
   4.127 +  moreover have "fold (\<lambda>A. op \<inter> (f A)) UNIV (set (A # As)) = foldl (\<lambda>B A. f A \<inter> B) UNIV (A # As)"
   4.128 +    by (rule fun_left_comm_idem.fold_set, unfold_locales, auto)
   4.129 +  ultimately have "INTER (set (A # As)) f = foldl (\<lambda>B A. f A \<inter> B) UNIV (A # As)"
   4.130 +    by (simp only: INTER_fold_inter) 
   4.131 +  then show ?thesis by simp
   4.132 +qed
   4.133 +
   4.134 +lemma UNION_set:
   4.135 +  "UNION (set As) f = foldl (\<lambda>B A. f A \<union> B) {} As"
   4.136 +proof -
   4.137 +  have "fold (\<lambda>A. op \<union> (f A)) {} (set As) = foldl (\<lambda>B A. f A \<union> B) {} As"
   4.138 +    by (rule fun_left_comm_idem.fold_set, unfold_locales, auto)
   4.139 +  then show ?thesis
   4.140 +    by (simp only: UNION_fold_union finite_set)
   4.141 +qed
   4.142 +
   4.143 +
   4.144 +subsection {* Derived set operations *}
   4.145 +
   4.146 +lemma member:
   4.147 +  "a \<in> A \<longleftrightarrow> (\<exists>x\<in>A. a = x)"
   4.148 +  by simp
   4.149 +
   4.150 +lemma subset_eq:
   4.151 +  "A \<subseteq> B \<longleftrightarrow> (\<forall>x\<in>A. x \<in> B)"
   4.152 +  by (fact subset_eq)
   4.153 +
   4.154 +lemma subset:
   4.155 +  "A \<subset> B \<longleftrightarrow> A \<subseteq> B \<and> \<not> B \<subseteq> A"
   4.156 +  by (fact less_le_not_le)
   4.157 +
   4.158 +lemma set_eq:
   4.159 +  "A = B \<longleftrightarrow> A \<subseteq> B \<and> B \<subseteq> A"
   4.160 +  by (fact eq_iff)
   4.161 +
   4.162 +lemma inter:
   4.163 +  "A \<inter> B = project (\<lambda>x. x \<in> A) B"
   4.164 +  by (auto simp add: project_def)
   4.165 +
   4.166 +end
   4.167 \ No newline at end of file
     5.1 --- a/src/HOL/ex/Codegenerator_Candidates.thy	Thu Jun 25 15:42:36 2009 +0200
     5.2 +++ b/src/HOL/ex/Codegenerator_Candidates.thy	Thu Jun 25 17:07:18 2009 +0200
     5.3 @@ -8,6 +8,7 @@
     5.4    Complex_Main
     5.5    AssocList
     5.6    Binomial
     5.7 +  Code_Set
     5.8    Commutative_Ring
     5.9    Enum
    5.10    List_Prefix