added Dlist
authorhaftmann
Mon Feb 22 15:53:18 2010 +0100 (2010-02-22)
changeset 35303816e48d60b13
parent 35302 4bc6b4d70e08
child 35304 57b6cc52c14c
added Dlist
src/HOL/Library/Dlist.thy
src/HOL/Library/Library.thy
src/HOL/ex/Codegenerator_Candidates.thy
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/HOL/Library/Dlist.thy	Mon Feb 22 15:53:18 2010 +0100
     1.3 @@ -0,0 +1,256 @@
     1.4 +(* Author: Florian Haftmann, TU Muenchen *)
     1.5 +
     1.6 +header {* Lists with elements distinct as canonical example for datatype invariants *}
     1.7 +
     1.8 +theory Dlist
     1.9 +imports Main Fset
    1.10 +begin
    1.11 +
    1.12 +section {* Prelude *}
    1.13 +
    1.14 +text {* Without canonical argument order, higher-order things tend to get confusing quite fast: *}
    1.15 +
    1.16 +setup {* Sign.map_naming (Name_Space.add_path "List") *}
    1.17 +
    1.18 +primrec member :: "'a list \<Rightarrow> 'a \<Rightarrow> bool" where
    1.19 +    "member [] y \<longleftrightarrow> False"
    1.20 +  | "member (x#xs) y \<longleftrightarrow> x = y \<or> member xs y"
    1.21 +
    1.22 +lemma member_set:
    1.23 +  "member = set"
    1.24 +proof (rule ext)+
    1.25 +  fix xs :: "'a list" and x :: 'a
    1.26 +  have "member xs x \<longleftrightarrow> x \<in> set xs" by (induct xs) auto
    1.27 +  then show "member xs x = set xs x" by (simp add: mem_def)
    1.28 +qed
    1.29 +
    1.30 +lemma not_set_compl:
    1.31 +  "Not \<circ> set xs = - set xs"
    1.32 +  by (simp add: fun_Compl_def bool_Compl_def comp_def expand_fun_eq)
    1.33 +
    1.34 +primrec fold :: "('a \<Rightarrow> 'b \<Rightarrow> 'b) \<Rightarrow> 'a list \<Rightarrow> 'b \<Rightarrow> 'b" where
    1.35 +    "fold f [] s = s"
    1.36 +  | "fold f (x#xs) s = fold f xs (f x s)"
    1.37 +
    1.38 +lemma foldl_fold:
    1.39 +  "foldl f s xs = List.fold (\<lambda>x s. f s x) xs s"
    1.40 +  by (induct xs arbitrary: s) simp_all
    1.41 +
    1.42 +setup {* Sign.map_naming Name_Space.parent_path *}
    1.43 +
    1.44 +
    1.45 +section {* The type of distinct lists *}
    1.46 +
    1.47 +typedef (open) 'a dlist = "{xs::'a list. distinct xs}"
    1.48 +  morphisms list_of_dlist Abs_dlist
    1.49 +proof
    1.50 +  show "[] \<in> ?dlist" by simp
    1.51 +qed
    1.52 +
    1.53 +text {* Formal, totalized constructor for @{typ "'a dlist"}: *}
    1.54 +
    1.55 +definition Dlist :: "'a list \<Rightarrow> 'a dlist" where
    1.56 +  [code del]: "Dlist xs = Abs_dlist (remdups xs)"
    1.57 +
    1.58 +lemma distinct_list_of_dlist [simp]:
    1.59 +  "distinct (list_of_dlist dxs)"
    1.60 +  using list_of_dlist [of dxs] by simp
    1.61 +
    1.62 +lemma list_of_dlist_Dlist [simp]:
    1.63 +  "list_of_dlist (Dlist xs) = remdups xs"
    1.64 +  by (simp add: Dlist_def Abs_dlist_inverse)
    1.65 +
    1.66 +lemma Dlist_list_of_dlist [simp]:
    1.67 +  "Dlist (list_of_dlist dxs) = dxs"
    1.68 +  by (simp add: Dlist_def list_of_dlist_inverse distinct_remdups_id)
    1.69 +
    1.70 +
    1.71 +text {* Fundamental operations: *}
    1.72 +
    1.73 +definition empty :: "'a dlist" where
    1.74 +  "empty = Dlist []"
    1.75 +
    1.76 +definition insert :: "'a \<Rightarrow> 'a dlist \<Rightarrow> 'a dlist" where
    1.77 +  "insert x dxs = Dlist (List.insert x (list_of_dlist dxs))"
    1.78 +
    1.79 +definition remove :: "'a \<Rightarrow> 'a dlist \<Rightarrow> 'a dlist" where
    1.80 +  "remove x dxs = Dlist (remove1 x (list_of_dlist dxs))"
    1.81 +
    1.82 +definition map :: "('a \<Rightarrow> 'b) \<Rightarrow> 'a dlist \<Rightarrow> 'b dlist" where
    1.83 +  "map f dxs = Dlist (remdups (List.map f (list_of_dlist dxs)))"
    1.84 +
    1.85 +definition filter :: "('a \<Rightarrow> bool) \<Rightarrow> 'a dlist \<Rightarrow> 'a dlist" where
    1.86 +  "filter P dxs = Dlist (List.filter P (list_of_dlist dxs))"
    1.87 +
    1.88 +
    1.89 +text {* Derived operations: *}
    1.90 +
    1.91 +definition null :: "'a dlist \<Rightarrow> bool" where
    1.92 +  "null dxs = List.null (list_of_dlist dxs)"
    1.93 +
    1.94 +definition member :: "'a dlist \<Rightarrow> 'a \<Rightarrow> bool" where
    1.95 +  "member dxs = List.member (list_of_dlist dxs)"
    1.96 +
    1.97 +definition length :: "'a dlist \<Rightarrow> nat" where
    1.98 +  "length dxs = List.length (list_of_dlist dxs)"
    1.99 +
   1.100 +definition fold :: "('a \<Rightarrow> 'b \<Rightarrow> 'b) \<Rightarrow> 'a dlist \<Rightarrow> 'b \<Rightarrow> 'b" where
   1.101 +  "fold f dxs = List.fold f (list_of_dlist dxs)"
   1.102 +
   1.103 +
   1.104 +section {* Executable version obeying invariant *}
   1.105 +
   1.106 +code_abstype Dlist list_of_dlist
   1.107 +  by simp
   1.108 +
   1.109 +lemma list_of_dlist_empty [simp, code abstract]:
   1.110 +  "list_of_dlist empty = []"
   1.111 +  by (simp add: empty_def)
   1.112 +
   1.113 +lemma list_of_dlist_insert [simp, code abstract]:
   1.114 +  "list_of_dlist (insert x dxs) = List.insert x (list_of_dlist dxs)"
   1.115 +  by (simp add: insert_def)
   1.116 +
   1.117 +lemma list_of_dlist_remove [simp, code abstract]:
   1.118 +  "list_of_dlist (remove x dxs) = remove1 x (list_of_dlist dxs)"
   1.119 +  by (simp add: remove_def)
   1.120 +
   1.121 +lemma list_of_dlist_map [simp, code abstract]:
   1.122 +  "list_of_dlist (map f dxs) = remdups (List.map f (list_of_dlist dxs))"
   1.123 +  by (simp add: map_def)
   1.124 +
   1.125 +lemma list_of_dlist_filter [simp, code abstract]:
   1.126 +  "list_of_dlist (filter P dxs) = List.filter P (list_of_dlist dxs)"
   1.127 +  by (simp add: filter_def)
   1.128 +
   1.129 +declare null_def [code] member_def [code] length_def [code] fold_def [code] -- {* explicit is better than implicit *}
   1.130 +
   1.131 +
   1.132 +section {* Implementation of sets by distinct lists -- canonical! *}
   1.133 +
   1.134 +definition Set :: "'a dlist \<Rightarrow> 'a fset" where
   1.135 +  "Set dxs = Fset.Set (list_of_dlist dxs)"
   1.136 +
   1.137 +definition Coset :: "'a dlist \<Rightarrow> 'a fset" where
   1.138 +  "Coset dxs = Fset.Coset (list_of_dlist dxs)"
   1.139 +
   1.140 +code_datatype Set Coset
   1.141 +
   1.142 +declare member_code [code del]
   1.143 +declare is_empty_Set [code del]
   1.144 +declare empty_Set [code del]
   1.145 +declare UNIV_Set [code del]
   1.146 +declare insert_Set [code del]
   1.147 +declare remove_Set [code del]
   1.148 +declare map_Set [code del]
   1.149 +declare filter_Set [code del]
   1.150 +declare forall_Set [code del]
   1.151 +declare exists_Set [code del]
   1.152 +declare card_Set [code del]
   1.153 +declare subfset_eq_forall [code del]
   1.154 +declare subfset_subfset_eq [code del]
   1.155 +declare eq_fset_subfset_eq [code del]
   1.156 +declare inter_project [code del]
   1.157 +declare subtract_remove [code del]
   1.158 +declare union_insert [code del]
   1.159 +declare Infimum_inf [code del]
   1.160 +declare Supremum_sup [code del]
   1.161 +
   1.162 +lemma Set_Dlist [simp]:
   1.163 +  "Set (Dlist xs) = Fset (set xs)"
   1.164 +  by (simp add: Set_def Fset.Set_def)
   1.165 +
   1.166 +lemma Coset_Dlist [simp]:
   1.167 +  "Coset (Dlist xs) = Fset (- set xs)"
   1.168 +  by (simp add: Coset_def Fset.Coset_def)
   1.169 +
   1.170 +lemma member_Set [simp]:
   1.171 +  "Fset.member (Set dxs) = List.member (list_of_dlist dxs)"
   1.172 +  by (simp add: Set_def member_set)
   1.173 +
   1.174 +lemma member_Coset [simp]:
   1.175 +  "Fset.member (Coset dxs) = Not \<circ> List.member (list_of_dlist dxs)"
   1.176 +  by (simp add: Coset_def member_set not_set_compl)
   1.177 +
   1.178 +lemma is_empty_Set [code]:
   1.179 +  "Fset.is_empty (Set dxs) \<longleftrightarrow> null dxs"
   1.180 +  by (simp add: null_def null_empty member_set)
   1.181 +
   1.182 +lemma bot_code [code]:
   1.183 +  "bot = Set empty"
   1.184 +  by (simp add: empty_def)
   1.185 +
   1.186 +lemma top_code [code]:
   1.187 +  "top = Coset empty"
   1.188 +  by (simp add: empty_def)
   1.189 +
   1.190 +lemma insert_code [code]:
   1.191 +  "Fset.insert x (Set dxs) = Set (insert x dxs)"
   1.192 +  "Fset.insert x (Coset dxs) = Coset (remove x dxs)"
   1.193 +  by (simp_all add: insert_def remove_def member_set not_set_compl)
   1.194 +
   1.195 +lemma remove_code [code]:
   1.196 +  "Fset.remove x (Set dxs) = Set (remove x dxs)"
   1.197 +  "Fset.remove x (Coset dxs) = Coset (insert x dxs)"
   1.198 +  by (auto simp add: insert_def remove_def member_set not_set_compl)
   1.199 +
   1.200 +lemma member_code [code]:
   1.201 +  "Fset.member (Set dxs) = member dxs"
   1.202 +  "Fset.member (Coset dxs) = Not \<circ> member dxs"
   1.203 +  by (simp_all add: member_def)
   1.204 +
   1.205 +lemma map_code [code]:
   1.206 +  "Fset.map f (Set dxs) = Set (map f dxs)"
   1.207 +  by (simp add: member_set)
   1.208 +  
   1.209 +lemma filter_code [code]:
   1.210 +  "Fset.filter f (Set dxs) = Set (filter f dxs)"
   1.211 +  by (simp add: member_set)
   1.212 +
   1.213 +lemma forall_Set [code]:
   1.214 +  "Fset.forall P (Set xs) \<longleftrightarrow> list_all P (list_of_dlist xs)"
   1.215 +  by (simp add: member_set list_all_iff)
   1.216 +
   1.217 +lemma exists_Set [code]:
   1.218 +  "Fset.exists P (Set xs) \<longleftrightarrow> list_ex P (list_of_dlist xs)"
   1.219 +  by (simp add: member_set list_ex_iff)
   1.220 +
   1.221 +lemma card_code [code]:
   1.222 +  "Fset.card (Set dxs) = length dxs"
   1.223 +  by (simp add: length_def member_set distinct_card)
   1.224 +
   1.225 +lemma foldl_list_of_dlist:
   1.226 +  "foldl f s (list_of_dlist dxs) = fold (\<lambda>x s. f s x) dxs s"
   1.227 +  by (simp add: foldl_fold fold_def)
   1.228 +
   1.229 +lemma inter_code [code]:
   1.230 +  "inf A (Set xs) = Set (filter (Fset.member A) xs)"
   1.231 +  "inf A (Coset xs) = fold Fset.remove xs A"
   1.232 +  by (simp_all only: Set_def Coset_def foldl_list_of_dlist inter_project list_of_dlist_filter)
   1.233 +
   1.234 +lemma subtract_code [code]:
   1.235 +  "A - Set xs = fold Fset.remove xs A"
   1.236 +  "A - Coset xs = Set (filter (Fset.member A) xs)"
   1.237 +  by (simp_all only: Set_def Coset_def foldl_list_of_dlist subtract_remove list_of_dlist_filter)
   1.238 +
   1.239 +lemma union_code [code]:
   1.240 +  "sup (Set xs) A = fold Fset.insert xs A"
   1.241 +  "sup (Coset xs) A = Coset (filter (Not \<circ> Fset.member A) xs)"
   1.242 +  by (simp_all only: Set_def Coset_def foldl_list_of_dlist union_insert list_of_dlist_filter)
   1.243 +
   1.244 +context complete_lattice
   1.245 +begin
   1.246 +
   1.247 +lemma Infimum_code [code]:
   1.248 +  "Infimum (Set As) = fold inf As top"
   1.249 +  by (simp only: Set_def Infimum_inf foldl_list_of_dlist inf.commute)
   1.250 +
   1.251 +lemma Supremum_code [code]:
   1.252 +  "Supremum (Set As) = fold sup As bot"
   1.253 +  by (simp only: Set_def Supremum_sup foldl_list_of_dlist sup.commute)
   1.254 +
   1.255 +end
   1.256 +
   1.257 +hide (open) const member fold empty insert remove map filter null member length fold
   1.258 +
   1.259 +end
     2.1 --- a/src/HOL/Library/Library.thy	Mon Feb 22 15:53:18 2010 +0100
     2.2 +++ b/src/HOL/Library/Library.thy	Mon Feb 22 15:53:18 2010 +0100
     2.3 @@ -15,6 +15,7 @@
     2.4    ContNotDenum
     2.5    Countable
     2.6    Diagonalize
     2.7 +  Dlist
     2.8    Efficient_Nat
     2.9    Enum
    2.10    Eval_Witness
     3.1 --- a/src/HOL/ex/Codegenerator_Candidates.thy	Mon Feb 22 15:53:18 2010 +0100
     3.2 +++ b/src/HOL/ex/Codegenerator_Candidates.thy	Mon Feb 22 15:53:18 2010 +0100
     3.3 @@ -8,6 +8,8 @@
     3.4    Complex_Main
     3.5    AssocList
     3.6    Binomial
     3.7 +  "~~/src/HOL/Decision_Procs/Commutative_Ring_Complete"
     3.8 +  Dlist
     3.9    Fset
    3.10    Enum
    3.11    List_Prefix
    3.12 @@ -17,12 +19,11 @@
    3.13    Permutation
    3.14    "~~/src/HOL/Number_Theory/Primes"
    3.15    Product_ord
    3.16 +  "~~/src/HOL/ex/Records"
    3.17    SetsAndFunctions
    3.18    Tree
    3.19    While_Combinator
    3.20    Word
    3.21 -  "~~/src/HOL/Decision_Procs/Commutative_Ring_Complete"
    3.22 -  "~~/src/HOL/ex/Records"
    3.23  begin
    3.24  
    3.25  inductive sublist :: "'a list \<Rightarrow> 'a list \<Rightarrow> bool" where